Simple app to test leadership election

Change-Id: I3a1803847463915c10cf36fde0d250dcf9c8c9b1
diff --git a/apps/election/pom.xml b/apps/election/pom.xml
new file mode 100644
index 0000000..05c5ddb
--- /dev/null
+++ b/apps/election/pom.xml
@@ -0,0 +1,58 @@
+<?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</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>onos-app-election</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>ONOS app leadership election test</description>
+
+    <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.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.console</artifactId>
+        </dependency>
+
+    </dependencies>
+
+</project>
diff --git a/apps/election/src/main/java/org/onosproject/election/ElectionTest.java b/apps/election/src/main/java/org/onosproject/election/ElectionTest.java
new file mode 100644
index 0000000..3e47cc1
--- /dev/null
+++ b/apps/election/src/main/java/org/onosproject/election/ElectionTest.java
@@ -0,0 +1,123 @@
+/*
+ * 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.
+ */
+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;
+
+    // TODO write cli command to get leader
+
+    @Activate
+    protected void activate() {
+        log.info("Election-test app started");
+
+        appId = coreService.registerApplication(ELECTION_APP);
+
+        localControllerNode = clusterService.getLocalNode();
+
+        leadershipService.addListener(leadershipEventListener);
+        leadershipService.runForLeadership(appId.name());
+    }
+
+    @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.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/apps/election/src/main/java/org/onosproject/election/cli/ElectionTestLeaderCommand.java b/apps/election/src/main/java/org/onosproject/election/cli/ElectionTestLeaderCommand.java
new file mode 100644
index 0000000..8f0d09a
--- /dev/null
+++ b/apps/election/src/main/java/org/onosproject/election/cli/ElectionTestLeaderCommand.java
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+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/apps/election/src/main/java/org/onosproject/election/cli/ElectionTestRunCommand.java b/apps/election/src/main/java/org/onosproject/election/cli/ElectionTestRunCommand.java
new file mode 100644
index 0000000..1f4b8c9
--- /dev/null
+++ b/apps/election/src/main/java/org/onosproject/election/cli/ElectionTestRunCommand.java
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+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/apps/election/src/main/java/org/onosproject/election/cli/ElectionTestWithdrawCommand.java b/apps/election/src/main/java/org/onosproject/election/cli/ElectionTestWithdrawCommand.java
new file mode 100644
index 0000000..3134350
--- /dev/null
+++ b/apps/election/src/main/java/org/onosproject/election/cli/ElectionTestWithdrawCommand.java
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+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/apps/election/src/main/java/org/onosproject/election/cli/package-info.java b/apps/election/src/main/java/org/onosproject/election/cli/package-info.java
new file mode 100644
index 0000000..7dc4cf3
--- /dev/null
+++ b/apps/election/src/main/java/org/onosproject/election/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Election test command-line handlers.
+ */
+package org.onosproject.election.cli;
\ No newline at end of file
diff --git a/apps/election/src/main/java/org/onosproject/election/package-info.java b/apps/election/src/main/java/org/onosproject/election/package-info.java
new file mode 100644
index 0000000..fb35baf
--- /dev/null
+++ b/apps/election/src/main/java/org/onosproject/election/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Sample application for use in various experiments.
+ */
+package org.onosproject.election;
diff --git a/apps/election/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/election/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..e9f05b4
--- /dev/null
+++ b/apps/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>
diff --git a/apps/pom.xml b/apps/pom.xml
index 87ebb31..01c3671 100644
--- a/apps/pom.xml
+++ b/apps/pom.xml
@@ -45,6 +45,7 @@
         <module>metrics</module>
         <module>oecfg</module>
         <module>demo</module>
+        <module>election</module>
     </modules>
 
     <properties>
diff --git a/features/features.xml b/features/features.xml
index d959208..3458062 100644
--- a/features/features.xml
+++ b/features/features.xml
@@ -224,6 +224,12 @@
         <bundle>mvn:org.onosproject/onos-app-demo/@ONOS-VERSION</bundle>
     </feature>
 
+    <feature name="onos-app-election" version="1.0.0"
+             description="ONOS app leadership election test">
+        <feature>onos-api</feature>
+        <bundle>mvn:org.onosproject/onos-app-election/1.0.0-SNAPSHOT</bundle>
+    </feature>
+
 
 
 </features>