Jon Hall
Committed by Gerrit Code Review

Simple app to test leadership election

Change-Id: I3a1803847463915c10cf36fde0d250dcf9c8c9b1
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2014 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +
18 +<project xmlns="http://maven.apache.org/POM/4.0.0"
19 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21 + <modelVersion>4.0.0</modelVersion>
22 +
23 + <parent>
24 + <groupId>org.onosproject</groupId>
25 + <artifactId>onos-apps</artifactId>
26 + <version>1.0.0-SNAPSHOT</version>
27 + <relativePath>../pom.xml</relativePath>
28 + </parent>
29 +
30 + <artifactId>onos-app-election</artifactId>
31 + <packaging>bundle</packaging>
32 +
33 + <description>ONOS app leadership election test</description>
34 +
35 + <dependencies>
36 +
37 + <dependency>
38 + <groupId>org.onosproject</groupId>
39 + <artifactId>onos-api</artifactId>
40 + <version>${project.version}</version>
41 + <scope>test</scope>
42 + <classifier>tests</classifier>
43 + </dependency>
44 +
45 + <dependency>
46 + <groupId>org.onosproject</groupId>
47 + <artifactId>onos-cli</artifactId>
48 + <version>${project.version}</version>
49 + </dependency>
50 +
51 + <dependency>
52 + <groupId>org.apache.karaf.shell</groupId>
53 + <artifactId>org.apache.karaf.shell.console</artifactId>
54 + </dependency>
55 +
56 + </dependencies>
57 +
58 +</project>
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.election;
17 +
18 +import static org.slf4j.LoggerFactory.getLogger;
19 +
20 +import org.apache.felix.scr.annotations.Activate;
21 +import org.apache.felix.scr.annotations.Component;
22 +import org.apache.felix.scr.annotations.Deactivate;
23 +import org.apache.felix.scr.annotations.Reference;
24 +import org.apache.felix.scr.annotations.ReferenceCardinality;
25 +import org.onosproject.cluster.ClusterService;
26 +import org.onosproject.core.CoreService;
27 +import org.onosproject.cluster.ControllerNode;
28 +import org.onosproject.cluster.LeadershipEvent;
29 +import org.onosproject.cluster.LeadershipEventListener;
30 +import org.onosproject.cluster.LeadershipService;
31 +import org.onosproject.core.ApplicationId;
32 +
33 +import org.slf4j.Logger;
34 +
35 +
36 +/**
37 + * Simple application to test leadership election.
38 + */
39 +@Component(immediate = true)
40 +public class ElectionTest {
41 +
42 + private final Logger log = getLogger(getClass());
43 +
44 + private static final String ELECTION_APP = "org.onosproject.election";
45 + private ApplicationId appId;
46 +
47 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 + protected ClusterService clusterService;
49 +
50 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 + protected CoreService coreService;
52 +
53 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 + protected LeadershipService leadershipService;
55 +
56 + private LeadershipEventListener leadershipEventListener =
57 + new InnerLeadershipEventListener();
58 +
59 + private ControllerNode localControllerNode;
60 +
61 + // TODO write cli command to get leader
62 +
63 + @Activate
64 + protected void activate() {
65 + log.info("Election-test app started");
66 +
67 + appId = coreService.registerApplication(ELECTION_APP);
68 +
69 + localControllerNode = clusterService.getLocalNode();
70 +
71 + leadershipService.addListener(leadershipEventListener);
72 + leadershipService.runForLeadership(appId.name());
73 + }
74 +
75 + @Deactivate
76 + protected void deactivate() {
77 +
78 + leadershipService.withdraw(appId.name());
79 + leadershipService.removeListener(leadershipEventListener);
80 +
81 + log.info("Election-test app Stopped");
82 + }
83 +
84 + /**
85 + * A listener for Leadership Events.
86 + */
87 + private class InnerLeadershipEventListener
88 + implements LeadershipEventListener {
89 +
90 + @Override
91 + public void event(LeadershipEvent event) {
92 +
93 +
94 + if (!event.subject().topic().equals(appId.name())) {
95 + return; // Not our topic: ignore
96 + }
97 +
98 + //only log what pertains to us
99 + log.debug("Leadership Event: time = {} type = {} event = {}",
100 + event.time(), event.type(), event);
101 +
102 + if (!event.subject().leader().equals(
103 + localControllerNode.id())) {
104 + return; // The event is not about this instance: ignore
105 + }
106 +
107 + switch (event.type()) {
108 + case LEADER_ELECTED:
109 + log.info("Election-test app leader elected");
110 + break;
111 + case LEADER_BOOTED:
112 + log.info("Election-test app lost election");
113 + break;
114 + case LEADER_REELECTED:
115 + log.debug("Election-test app was re-elected");
116 + break;
117 + default:
118 + break;
119 + }
120 + }
121 + }
122 +
123 +}
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.election.cli;
17 +
18 +import org.onosproject.cluster.NodeId;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.cluster.LeadershipService;
22 +
23 +/**
24 + * CLI command to get the current leader for the Election test application.
25 + */
26 +@Command(scope = "onos", name = "election-test-leader",
27 + description = "Get the current leader for the Election test application")
28 +public class ElectionTestLeaderCommand extends AbstractShellCommand {
29 +
30 + private NodeId leader;
31 + private static final String ELECTION_APP = "org.onosproject.election";
32 +
33 + @Override
34 + protected void execute() {
35 + LeadershipService service = get(LeadershipService.class);
36 +
37 + //print the current leader
38 + leader = service.getLeader(ELECTION_APP);
39 + printLeader(leader);
40 + }
41 +
42 + /**
43 + * Prints the leader.
44 + *
45 + * @param leader the leader to print
46 + */
47 + private void printLeader(NodeId leader) {
48 + if (leader != null) {
49 + print("The current leader for the Election app is %s.", leader);
50 + } else {
51 + print("There is currently no leader elected for the Election app");
52 + }
53 + }
54 +}
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.election.cli;
17 +
18 +import org.apache.karaf.shell.commands.Command;
19 +import org.onosproject.cli.AbstractShellCommand;
20 +import org.onosproject.cluster.LeadershipService;
21 +
22 +/**
23 + * CLI command to run for leadership of the Election test application.
24 + */
25 +@Command(scope = "onos", name = "election-test-run",
26 + description = "Run for leader of the Election test application")
27 +public class ElectionTestRunCommand extends AbstractShellCommand {
28 +
29 + private static final String ELECTION_APP = "org.onosproject.election";
30 +
31 + @Override
32 + protected void execute() {
33 + LeadershipService service = get(LeadershipService.class);
34 +
35 + service.runForLeadership(ELECTION_APP);
36 + //print the current leader
37 + print("Entering leadership elections for the Election app.");
38 + }
39 +}
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.election.cli;
17 +
18 +import org.apache.karaf.shell.commands.Command;
19 +import org.onosproject.cli.AbstractShellCommand;
20 +import org.onosproject.cluster.LeadershipService;
21 +
22 +/**
23 + * CLI command to withdraw the local node from leadership election for
24 + * the Election test application.
25 + */
26 +@Command(scope = "onos", name = "election-test-withdraw",
27 + description = "Withdraw node from leadership election for the Election test application")
28 +public class ElectionTestWithdrawCommand extends AbstractShellCommand {
29 +
30 + private static final String ELECTION_APP = "org.onosproject.election";
31 +
32 + @Override
33 + protected void execute() {
34 + LeadershipService service = get(LeadershipService.class);
35 +
36 + service.withdraw(ELECTION_APP);
37 + //print the current leader
38 + print("Withdrawing from leadership elections for the Election app.");
39 + }
40 +}
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Election test command-line handlers.
19 + */
20 +package org.onosproject.election.cli;
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Sample application for use in various experiments.
19 + */
20 +package org.onosproject.election;
1 +<!--
2 + ~ Copyright 2014 Open Networking Laboratory
3 + ~
4 + ~ Licensed under the Apache License, Version 2.0 (the "License");
5 + ~ you may not use this file except in compliance with the License.
6 + ~ You may obtain a copy of the License at
7 + ~
8 + ~ http://www.apache.org/licenses/LICENSE-2.0
9 + ~
10 + ~ Unless required by applicable law or agreed to in writing, software
11 + ~ distributed under the License is distributed on an "AS IS" BASIS,
12 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + ~ See the License for the specific language governing permissions and
14 + ~ limitations under the License.
15 + -->
16 +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
17 +
18 + <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
19 + <command>
20 + <action class="org.onosproject.election.cli.ElectionTestLeaderCommand"/>
21 + </command>
22 + <command>
23 + <action class="org.onosproject.election.cli.ElectionTestRunCommand"/>
24 + </command>
25 + <command>
26 + <action class="org.onosproject.election.cli.ElectionTestWithdrawCommand"/>
27 + </command>
28 + </command-bundle>
29 +
30 +</blueprint>
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
45 <module>metrics</module> 45 <module>metrics</module>
46 <module>oecfg</module> 46 <module>oecfg</module>
47 <module>demo</module> 47 <module>demo</module>
48 + <module>election</module>
48 </modules> 49 </modules>
49 50
50 <properties> 51 <properties>
......
...@@ -224,6 +224,12 @@ ...@@ -224,6 +224,12 @@
224 <bundle>mvn:org.onosproject/onos-app-demo/@ONOS-VERSION</bundle> 224 <bundle>mvn:org.onosproject/onos-app-demo/@ONOS-VERSION</bundle>
225 </feature> 225 </feature>
226 226
227 + <feature name="onos-app-election" version="1.0.0"
228 + description="ONOS app leadership election test">
229 + <feature>onos-api</feature>
230 + <bundle>mvn:org.onosproject/onos-app-election/1.0.0-SNAPSHOT</bundle>
231 + </feature>
232 +
227 233
228 234
229 </features> 235 </features>
......