Merge "added demo rest api for adding and withdrawing intents in a mesh"
diff --git a/apps/demo/pom.xml b/apps/demo/pom.xml
new file mode 100644
index 0000000..fbc7f91
--- /dev/null
+++ b/apps/demo/pom.xml
@@ -0,0 +1,121 @@
+<?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.onlab.onos</groupId>
+        <artifactId>onos-apps</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>onos-app-demo</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>ONOS demo app bundle</description>
+
+    <properties>
+        <web.context>/onos/demo</web.context>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.onlab.onos</groupId>
+            <artifactId>onlab-rest</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onlab.onos</groupId>
+            <artifactId>onos-rest</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.sun.jersey</groupId>
+            <artifactId>jersey-servlet</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.sun.jersey.jersey-test-framework</groupId>
+            <artifactId>jersey-test-framework-core</artifactId>
+            <version>1.18.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.sun.jersey.jersey-test-framework</groupId>
+            <artifactId>jersey-test-framework-grizzly2</artifactId>
+            <version>1.18.1</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-annotations</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <_wab>src/main/webapp/</_wab>
+                        <Bundle-SymbolicName>
+                            ${project.groupId}.${project.artifactId}
+                        </Bundle-SymbolicName>
+                        <Import-Package>
+                            org.slf4j,
+                            org.osgi.framework,
+                            javax.ws.rs,javax.ws.rs.core,
+                            com.sun.jersey.api.core,
+                            com.sun.jersey.spi.container.servlet,
+                            com.sun.jersey.server.impl.container.servlet,
+                            com.fasterxml.jackson.databind,
+                            com.fasterxml.jackson.databind.node,
+                            com.google.common.*,
+                            org.onlab.packet.*,
+                            org.onlab.rest.*,
+                            org.onlab.onos.*
+                        </Import-Package>
+                        <Web-ContextPath>${web.context}</Web-ContextPath>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git a/apps/demo/src/main/java/org/onlab/onos/demo/DemoAPI.java b/apps/demo/src/main/java/org/onlab/onos/demo/DemoAPI.java
new file mode 100644
index 0000000..2a267f3
--- /dev/null
+++ b/apps/demo/src/main/java/org/onlab/onos/demo/DemoAPI.java
@@ -0,0 +1,21 @@
+package org.onlab.onos.demo;
+
+/**
+ * Simple demo api interface.
+ */
+public interface DemoAPI {
+
+    enum InstallType { MESH, RANDOM };
+
+    /**
+     * Installs intents based on the installation type.
+     * @param type the installation type.
+     */
+    void setup(InstallType type);
+
+    /**
+     * Uninstalls all existing intents.
+     */
+    void tearDown();
+
+}
diff --git a/apps/demo/src/main/java/org/onlab/onos/demo/DemoInstaller.java b/apps/demo/src/main/java/org/onlab/onos/demo/DemoInstaller.java
new file mode 100644
index 0000000..ca2d6db
--- /dev/null
+++ b/apps/demo/src/main/java/org/onlab/onos/demo/DemoInstaller.java
@@ -0,0 +1,142 @@
+/*
+ * 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.onlab.onos.demo;
+
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+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.apache.felix.scr.annotations.Service;
+import org.onlab.onos.core.ApplicationId;
+import org.onlab.onos.core.CoreService;
+import org.onlab.onos.net.Host;
+import org.onlab.onos.net.flow.DefaultTrafficSelector;
+import org.onlab.onos.net.flow.DefaultTrafficTreatment;
+import org.onlab.onos.net.flow.TrafficSelector;
+import org.onlab.onos.net.flow.TrafficTreatment;
+import org.onlab.onos.net.host.HostService;
+import org.onlab.onos.net.intent.HostToHostIntent;
+import org.onlab.onos.net.intent.Intent;
+import org.onlab.onos.net.intent.IntentService;
+import org.slf4j.Logger;
+
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Application to set up demos.
+ */
+@Component(immediate = true)
+@Service
+public class DemoInstaller implements DemoAPI {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected IntentService intentService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected HostService hostService;
+
+    private ExecutorService worker;
+
+    private ApplicationId appId;
+
+    private final Set<Intent> existingIntents = new HashSet<>();
+
+
+
+    @Activate
+    public void activate() {
+        appId = coreService.registerApplication("org.onlab.onos.demo.installer");
+        worker = Executors.newFixedThreadPool(1,
+                                              new ThreadFactoryBuilder()
+                                                      .setNameFormat("demo-app-worker")
+                                                      .build());
+        log.info("Started with Application ID {}", appId.id());
+    }
+
+    @Deactivate
+    public void deactivate() {
+        worker.shutdownNow();
+        log.info("Stopped");
+    }
+
+    @Override
+    public void setup(InstallType type) {
+        switch (type) {
+            case MESH:
+                log.debug("Installing mesh intents");
+                worker.execute(new MeshInstaller());
+                break;
+            case RANDOM:
+                throw new IllegalArgumentException("Not yet implemented.");
+            default:
+                throw new IllegalArgumentException("What is it you want exactly?");
+        }
+    }
+
+    @Override
+    public void tearDown() {
+        worker.submit(new UnInstaller());
+    }
+
+
+    private class MeshInstaller implements Runnable {
+
+        @Override
+        public void run() {
+            TrafficSelector selector = DefaultTrafficSelector.builder().build();
+            TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
+
+            List<Host> hosts = Lists.newArrayList(hostService.getHosts());
+            while (!hosts.isEmpty()) {
+                Host src = hosts.remove(0);
+                for (Host dst : hosts) {
+                    HostToHostIntent intent = new HostToHostIntent(appId, src.id(), dst.id(),
+                                                                   selector, treatment,
+                                                                   null);
+                    existingIntents.add(intent);
+                    intentService.submit(intent);
+                }
+            }
+        }
+    }
+
+
+    private class UnInstaller implements Runnable {
+        @Override
+        public void run() {
+            for (Intent i : existingIntents) {
+                intentService.withdraw(i);
+            }
+        }
+    }
+}
+
+
diff --git a/apps/demo/src/main/java/org/onlab/onos/demo/DemoResource.java b/apps/demo/src/main/java/org/onlab/onos/demo/DemoResource.java
new file mode 100644
index 0000000..f533072
--- /dev/null
+++ b/apps/demo/src/main/java/org/onlab/onos/demo/DemoResource.java
@@ -0,0 +1,54 @@
+package org.onlab.onos.demo;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.onlab.rest.BaseResource;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Rest API for demos.
+ */
+@Path("intents")
+public class DemoResource extends BaseResource {
+
+
+    @POST
+    @Path("setup")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response setup(InputStream input) throws IOException {
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode cfg = mapper.readTree(input);
+        if (!cfg.has("type")) {
+            return Response.status(Response.Status.BAD_REQUEST)
+                    .entity("Expected type field containing either mesh or random.").build();
+        }
+
+        DemoAPI.InstallType type = DemoAPI.InstallType.valueOf(
+                cfg.get("type").asText().toUpperCase());
+        DemoAPI demo = get(DemoAPI.class);
+        demo.setup(type);
+
+        return Response.ok(mapper.createObjectNode().toString()).build();
+    }
+
+    @GET
+    @Path("teardown")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response tearDown() throws IOException {
+        ObjectMapper mapper = new ObjectMapper();
+        DemoAPI demo = get(DemoAPI.class);
+        demo.tearDown();
+        return Response.ok(mapper.createObjectNode().toString()).build();
+    }
+
+}
diff --git a/apps/demo/src/main/java/org/onlab/onos/demo/package-info.java b/apps/demo/src/main/java/org/onlab/onos/demo/package-info.java
new file mode 100644
index 0000000..b60eb15
--- /dev/null
+++ b/apps/demo/src/main/java/org/onlab/onos/demo/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.
+ */
+
+/**
+ *  Demo applications live here.
+ */
+package org.onlab.onos.demo;
diff --git a/apps/demo/src/main/webapp/WEB-INF/web.xml b/apps/demo/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..429287e
--- /dev/null
+++ b/apps/demo/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,44 @@
+<?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.
+  -->
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
+         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+         id="ONOS" version="2.5">
+    <display-name>ONOS DEMO APP API v1.0</display-name>
+
+    <servlet>
+        <servlet-name>JAX-RS Service</servlet-name>
+        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
+        <init-param>
+            <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
+            <param-value>com.sun.jersey.api.core.ClassNamesResourceConfig</param-value>
+        </init-param>
+        <init-param>
+            <param-name>com.sun.jersey.config.property.classnames</param-name>
+            <param-value>
+                org.onlab.onos.demo.DemoResource
+            </param-value>
+        </init-param>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+
+    <servlet-mapping>
+        <servlet-name>JAX-RS Service</servlet-name>
+        <url-pattern>/*</url-pattern>
+    </servlet-mapping>
+
+</web-app>
\ No newline at end of file
diff --git a/apps/pom.xml b/apps/pom.xml
index 3084916..c021ca4 100644
--- a/apps/pom.xml
+++ b/apps/pom.xml
@@ -44,6 +44,7 @@
         <module>optical</module>
         <module>metrics</module>
         <module>oecfg</module>
+        <module>demo</module>
     </modules>
 
     <properties>
diff --git a/features/features.xml b/features/features.xml
index 551c4de..5ede89c 100644
--- a/features/features.xml
+++ b/features/features.xml
@@ -227,4 +227,12 @@
         <bundle>mvn:org.onlab.onos/onos-app-metrics-topology/1.0.0-SNAPSHOT</bundle>
     </feature>
 
+    <feature name="onos-app-demo" version="1.0.0"
+             description="ONOS demo applications">
+        <feature>onos-api</feature>
+        <bundle>mvn:org.onlab.onos/onos-app-demo/1.0.0-SNAPSHOT</bundle>
+    </feature>
+
+
+
 </features>