diff options
Diffstat (limited to 'framework/src/onos/apps/test/election')
8 files changed, 392 insertions, 0 deletions
diff --git a/framework/src/onos/apps/test/election/pom.xml b/framework/src/onos/apps/test/election/pom.xml new file mode 100644 index 00000000..4c18baee --- /dev/null +++ b/framework/src/onos/apps/test/election/pom.xml @@ -0,0 +1,65 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Copyright 2014 Open Networking Laboratory + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onosproject</groupId> + <artifactId>onos-apps-test</artifactId> + <version>1.3.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>onos-app-election</artifactId> + <packaging>bundle</packaging> + + <description>Master election test application</description> + + <properties> + <onos.app.name>org.onosproject.election</onos.app.name> + </properties> + + <dependencies> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-api</artifactId> + <version>${project.version}</version> + <scope>test</scope> + <classifier>tests</classifier> + </dependency> + + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-cli</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.karaf.shell</groupId> + <artifactId>org.apache.karaf.shell.console</artifactId> + </dependency> + + </dependencies> + +</project> diff --git a/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/ElectionTest.java b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/ElectionTest.java new file mode 100644 index 00000000..b2a72075 --- /dev/null +++ b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/ElectionTest.java @@ -0,0 +1,124 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.election; + +import static org.slf4j.LoggerFactory.getLogger; + +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.ReferenceCardinality; +import org.onosproject.cluster.ClusterService; +import org.onosproject.core.CoreService; +import org.onosproject.cluster.ControllerNode; +import org.onosproject.cluster.LeadershipEvent; +import org.onosproject.cluster.LeadershipEventListener; +import org.onosproject.cluster.LeadershipService; +import org.onosproject.core.ApplicationId; + +import org.slf4j.Logger; + + +/** + * Simple application to test leadership election. + */ +@Component(immediate = true) +public class ElectionTest { + + private final Logger log = getLogger(getClass()); + + private static final String ELECTION_APP = "org.onosproject.election"; + private ApplicationId appId; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected ClusterService clusterService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected CoreService coreService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected LeadershipService leadershipService; + + private LeadershipEventListener leadershipEventListener = + new InnerLeadershipEventListener(); + + private ControllerNode localControllerNode; + + + @Activate + protected void activate() { + log.info("Election-test app started"); + + appId = coreService.registerApplication(ELECTION_APP); + + localControllerNode = clusterService.getLocalNode(); + + leadershipService.addListener(leadershipEventListener); + } + + @Deactivate + protected void deactivate() { + + leadershipService.withdraw(appId.name()); + leadershipService.removeListener(leadershipEventListener); + + log.info("Election-test app Stopped"); + } + + /** + * A listener for Leadership Events. + */ + private class InnerLeadershipEventListener + implements LeadershipEventListener { + + @Override + public void event(LeadershipEvent event) { + + + if (event.type().equals(LeadershipEvent.Type.CANDIDATES_CHANGED)) { + return; + } + if (!event.subject().topic().equals(appId.name())) { + return; // Not our topic: ignore + } + + //only log what pertains to us + log.debug("Leadership Event: time = {} type = {} event = {}", + event.time(), event.type(), event); + + if (!event.subject().leader().equals( + localControllerNode.id())) { + return; // The event is not about this instance: ignore + } + + switch (event.type()) { + case LEADER_ELECTED: + log.info("Election-test app leader elected"); + break; + case LEADER_BOOTED: + log.info("Election-test app lost election"); + break; + case LEADER_REELECTED: + log.debug("Election-test app was re-elected"); + break; + default: + break; + } + } + } + +} diff --git a/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/ElectionTestLeaderCommand.java b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/ElectionTestLeaderCommand.java new file mode 100644 index 00000000..5b3fab3f --- /dev/null +++ b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/ElectionTestLeaderCommand.java @@ -0,0 +1,54 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.election.cli; + +import org.onosproject.cluster.NodeId; +import org.apache.karaf.shell.commands.Command; +import org.onosproject.cli.AbstractShellCommand; +import org.onosproject.cluster.LeadershipService; + +/** + * CLI command to get the current leader for the Election test application. + */ +@Command(scope = "onos", name = "election-test-leader", + description = "Get the current leader for the Election test application") +public class ElectionTestLeaderCommand extends AbstractShellCommand { + + private NodeId leader; + private static final String ELECTION_APP = "org.onosproject.election"; + + @Override + protected void execute() { + LeadershipService service = get(LeadershipService.class); + + //print the current leader + leader = service.getLeader(ELECTION_APP); + printLeader(leader); + } + + /** + * Prints the leader. + * + * @param leader the leader to print + */ + private void printLeader(NodeId leader) { + if (leader != null) { + print("The current leader for the Election app is %s.", leader); + } else { + print("There is currently no leader elected for the Election app"); + } + } +} diff --git a/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/ElectionTestRunCommand.java b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/ElectionTestRunCommand.java new file mode 100644 index 00000000..5ea8ceb5 --- /dev/null +++ b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/ElectionTestRunCommand.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.election.cli; + +import org.apache.karaf.shell.commands.Command; +import org.onosproject.cli.AbstractShellCommand; +import org.onosproject.cluster.LeadershipService; + +/** + * CLI command to run for leadership of the Election test application. + */ +@Command(scope = "onos", name = "election-test-run", + description = "Run for leader of the Election test application") +public class ElectionTestRunCommand extends AbstractShellCommand { + + private static final String ELECTION_APP = "org.onosproject.election"; + + @Override + protected void execute() { + LeadershipService service = get(LeadershipService.class); + + service.runForLeadership(ELECTION_APP); + //print the current leader + print("Entering leadership elections for the Election app."); + } +} diff --git a/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/ElectionTestWithdrawCommand.java b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/ElectionTestWithdrawCommand.java new file mode 100644 index 00000000..0732a1fa --- /dev/null +++ b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/ElectionTestWithdrawCommand.java @@ -0,0 +1,40 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.election.cli; + +import org.apache.karaf.shell.commands.Command; +import org.onosproject.cli.AbstractShellCommand; +import org.onosproject.cluster.LeadershipService; + +/** + * CLI command to withdraw the local node from leadership election for + * the Election test application. + */ +@Command(scope = "onos", name = "election-test-withdraw", + description = "Withdraw node from leadership election for the Election test application") +public class ElectionTestWithdrawCommand extends AbstractShellCommand { + + private static final String ELECTION_APP = "org.onosproject.election"; + + @Override + protected void execute() { + LeadershipService service = get(LeadershipService.class); + + service.withdraw(ELECTION_APP); + //print the current leader + print("Withdrawing from leadership elections for the Election app."); + } +} diff --git a/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/package-info.java b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/package-info.java new file mode 100644 index 00000000..6c366378 --- /dev/null +++ b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/cli/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Election test command-line handlers. + */ +package org.onosproject.election.cli; diff --git a/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/package-info.java b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/package-info.java new file mode 100644 index 00000000..fcc48894 --- /dev/null +++ b/framework/src/onos/apps/test/election/src/main/java/org/onosproject/election/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Sample application for use in various experiments. + */ +package org.onosproject.election; diff --git a/framework/src/onos/apps/test/election/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/framework/src/onos/apps/test/election/src/main/resources/OSGI-INF/blueprint/shell-config.xml new file mode 100644 index 00000000..e9f05b43 --- /dev/null +++ b/framework/src/onos/apps/test/election/src/main/resources/OSGI-INF/blueprint/shell-config.xml @@ -0,0 +1,30 @@ +<!-- + ~ Copyright 2014 Open Networking Laboratory + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> + + <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> + <command> + <action class="org.onosproject.election.cli.ElectionTestLeaderCommand"/> + </command> + <command> + <action class="org.onosproject.election.cli.ElectionTestRunCommand"/> + </command> + <command> + <action class="org.onosproject.election.cli.ElectionTestWithdrawCommand"/> + </command> + </command-bundle> + +</blueprint> |