Thomas Vachuska
Committed by Gerrit Code Review

Merge "added demo rest api for adding and withdrawing intents in a mesh"

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 +<project xmlns="http://maven.apache.org/POM/4.0.0"
18 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20 + <modelVersion>4.0.0</modelVersion>
21 +
22 + <parent>
23 + <groupId>org.onlab.onos</groupId>
24 + <artifactId>onos-apps</artifactId>
25 + <version>1.0.0-SNAPSHOT</version>
26 + <relativePath>../pom.xml</relativePath>
27 + </parent>
28 +
29 + <artifactId>onos-app-demo</artifactId>
30 + <packaging>bundle</packaging>
31 +
32 + <description>ONOS demo app bundle</description>
33 +
34 + <properties>
35 + <web.context>/onos/demo</web.context>
36 + </properties>
37 +
38 + <dependencies>
39 + <dependency>
40 + <groupId>org.osgi</groupId>
41 + <artifactId>org.osgi.compendium</artifactId>
42 + </dependency>
43 + <dependency>
44 + <groupId>org.onlab.onos</groupId>
45 + <artifactId>onlab-rest</artifactId>
46 + <version>${project.version}</version>
47 + </dependency>
48 +
49 + <dependency>
50 + <groupId>org.onlab.onos</groupId>
51 + <artifactId>onos-rest</artifactId>
52 + <version>${project.version}</version>
53 + </dependency>
54 +
55 + <dependency>
56 + <groupId>com.sun.jersey</groupId>
57 + <artifactId>jersey-servlet</artifactId>
58 + </dependency>
59 + <dependency>
60 + <groupId>com.sun.jersey.jersey-test-framework</groupId>
61 + <artifactId>jersey-test-framework-core</artifactId>
62 + <version>1.18.1</version>
63 + <scope>test</scope>
64 + </dependency>
65 + <dependency>
66 + <groupId>com.sun.jersey.jersey-test-framework</groupId>
67 + <artifactId>jersey-test-framework-grizzly2</artifactId>
68 + <version>1.18.1</version>
69 + <scope>test</scope>
70 + </dependency>
71 +
72 + <dependency>
73 + <groupId>com.fasterxml.jackson.core</groupId>
74 + <artifactId>jackson-databind</artifactId>
75 + </dependency>
76 +
77 + <dependency>
78 + <groupId>com.fasterxml.jackson.core</groupId>
79 + <artifactId>jackson-annotations</artifactId>
80 + </dependency>
81 +
82 + <dependency>
83 + <groupId>org.osgi</groupId>
84 + <artifactId>org.osgi.core</artifactId>
85 + </dependency>
86 + </dependencies>
87 +
88 + <build>
89 + <plugins>
90 + <plugin>
91 + <groupId>org.apache.felix</groupId>
92 + <artifactId>maven-bundle-plugin</artifactId>
93 + <extensions>true</extensions>
94 + <configuration>
95 + <instructions>
96 + <_wab>src/main/webapp/</_wab>
97 + <Bundle-SymbolicName>
98 + ${project.groupId}.${project.artifactId}
99 + </Bundle-SymbolicName>
100 + <Import-Package>
101 + org.slf4j,
102 + org.osgi.framework,
103 + javax.ws.rs,javax.ws.rs.core,
104 + com.sun.jersey.api.core,
105 + com.sun.jersey.spi.container.servlet,
106 + com.sun.jersey.server.impl.container.servlet,
107 + com.fasterxml.jackson.databind,
108 + com.fasterxml.jackson.databind.node,
109 + com.google.common.*,
110 + org.onlab.packet.*,
111 + org.onlab.rest.*,
112 + org.onlab.onos.*
113 + </Import-Package>
114 + <Web-ContextPath>${web.context}</Web-ContextPath>
115 + </instructions>
116 + </configuration>
117 + </plugin>
118 + </plugins>
119 + </build>
120 +
121 +</project>
1 +package org.onlab.onos.demo;
2 +
3 +/**
4 + * Simple demo api interface.
5 + */
6 +public interface DemoAPI {
7 +
8 + enum InstallType { MESH, RANDOM };
9 +
10 + /**
11 + * Installs intents based on the installation type.
12 + * @param type the installation type.
13 + */
14 + void setup(InstallType type);
15 +
16 + /**
17 + * Uninstalls all existing intents.
18 + */
19 + void tearDown();
20 +
21 +}
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.onlab.onos.demo;
17 +
18 +import com.google.common.collect.Lists;
19 +import com.google.common.util.concurrent.ThreadFactoryBuilder;
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.apache.felix.scr.annotations.Service;
26 +import org.onlab.onos.core.ApplicationId;
27 +import org.onlab.onos.core.CoreService;
28 +import org.onlab.onos.net.Host;
29 +import org.onlab.onos.net.flow.DefaultTrafficSelector;
30 +import org.onlab.onos.net.flow.DefaultTrafficTreatment;
31 +import org.onlab.onos.net.flow.TrafficSelector;
32 +import org.onlab.onos.net.flow.TrafficTreatment;
33 +import org.onlab.onos.net.host.HostService;
34 +import org.onlab.onos.net.intent.HostToHostIntent;
35 +import org.onlab.onos.net.intent.Intent;
36 +import org.onlab.onos.net.intent.IntentService;
37 +import org.slf4j.Logger;
38 +
39 +
40 +import java.util.HashSet;
41 +import java.util.List;
42 +import java.util.Set;
43 +import java.util.concurrent.ExecutorService;
44 +import java.util.concurrent.Executors;
45 +
46 +import static org.slf4j.LoggerFactory.getLogger;
47 +
48 +/**
49 + * Application to set up demos.
50 + */
51 +@Component(immediate = true)
52 +@Service
53 +public class DemoInstaller implements DemoAPI {
54 +
55 + private final Logger log = getLogger(getClass());
56 +
57 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 + protected CoreService coreService;
59 +
60 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 + protected IntentService intentService;
62 +
63 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 + protected HostService hostService;
65 +
66 + private ExecutorService worker;
67 +
68 + private ApplicationId appId;
69 +
70 + private final Set<Intent> existingIntents = new HashSet<>();
71 +
72 +
73 +
74 + @Activate
75 + public void activate() {
76 + appId = coreService.registerApplication("org.onlab.onos.demo.installer");
77 + worker = Executors.newFixedThreadPool(1,
78 + new ThreadFactoryBuilder()
79 + .setNameFormat("demo-app-worker")
80 + .build());
81 + log.info("Started with Application ID {}", appId.id());
82 + }
83 +
84 + @Deactivate
85 + public void deactivate() {
86 + worker.shutdownNow();
87 + log.info("Stopped");
88 + }
89 +
90 + @Override
91 + public void setup(InstallType type) {
92 + switch (type) {
93 + case MESH:
94 + log.debug("Installing mesh intents");
95 + worker.execute(new MeshInstaller());
96 + break;
97 + case RANDOM:
98 + throw new IllegalArgumentException("Not yet implemented.");
99 + default:
100 + throw new IllegalArgumentException("What is it you want exactly?");
101 + }
102 + }
103 +
104 + @Override
105 + public void tearDown() {
106 + worker.submit(new UnInstaller());
107 + }
108 +
109 +
110 + private class MeshInstaller implements Runnable {
111 +
112 + @Override
113 + public void run() {
114 + TrafficSelector selector = DefaultTrafficSelector.builder().build();
115 + TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
116 +
117 + List<Host> hosts = Lists.newArrayList(hostService.getHosts());
118 + while (!hosts.isEmpty()) {
119 + Host src = hosts.remove(0);
120 + for (Host dst : hosts) {
121 + HostToHostIntent intent = new HostToHostIntent(appId, src.id(), dst.id(),
122 + selector, treatment,
123 + null);
124 + existingIntents.add(intent);
125 + intentService.submit(intent);
126 + }
127 + }
128 + }
129 + }
130 +
131 +
132 + private class UnInstaller implements Runnable {
133 + @Override
134 + public void run() {
135 + for (Intent i : existingIntents) {
136 + intentService.withdraw(i);
137 + }
138 + }
139 + }
140 +}
141 +
142 +
1 +package org.onlab.onos.demo;
2 +
3 +import com.fasterxml.jackson.databind.JsonNode;
4 +import com.fasterxml.jackson.databind.ObjectMapper;
5 +import org.onlab.rest.BaseResource;
6 +
7 +import javax.ws.rs.Consumes;
8 +import javax.ws.rs.GET;
9 +import javax.ws.rs.POST;
10 +import javax.ws.rs.Path;
11 +import javax.ws.rs.Produces;
12 +import javax.ws.rs.core.MediaType;
13 +import javax.ws.rs.core.Response;
14 +import java.io.IOException;
15 +import java.io.InputStream;
16 +
17 +/**
18 + * Rest API for demos.
19 + */
20 +@Path("intents")
21 +public class DemoResource extends BaseResource {
22 +
23 +
24 + @POST
25 + @Path("setup")
26 + @Consumes(MediaType.APPLICATION_JSON)
27 + @Produces(MediaType.APPLICATION_JSON)
28 + public Response setup(InputStream input) throws IOException {
29 + ObjectMapper mapper = new ObjectMapper();
30 + JsonNode cfg = mapper.readTree(input);
31 + if (!cfg.has("type")) {
32 + return Response.status(Response.Status.BAD_REQUEST)
33 + .entity("Expected type field containing either mesh or random.").build();
34 + }
35 +
36 + DemoAPI.InstallType type = DemoAPI.InstallType.valueOf(
37 + cfg.get("type").asText().toUpperCase());
38 + DemoAPI demo = get(DemoAPI.class);
39 + demo.setup(type);
40 +
41 + return Response.ok(mapper.createObjectNode().toString()).build();
42 + }
43 +
44 + @GET
45 + @Path("teardown")
46 + @Produces(MediaType.APPLICATION_JSON)
47 + public Response tearDown() throws IOException {
48 + ObjectMapper mapper = new ObjectMapper();
49 + DemoAPI demo = get(DemoAPI.class);
50 + demo.tearDown();
51 + return Response.ok(mapper.createObjectNode().toString()).build();
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 +
17 +/**
18 + * Demo applications live here.
19 + */
20 +package org.onlab.onos.demo;
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 +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
18 + xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
19 + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
20 + id="ONOS" version="2.5">
21 + <display-name>ONOS DEMO APP API v1.0</display-name>
22 +
23 + <servlet>
24 + <servlet-name>JAX-RS Service</servlet-name>
25 + <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
26 + <init-param>
27 + <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
28 + <param-value>com.sun.jersey.api.core.ClassNamesResourceConfig</param-value>
29 + </init-param>
30 + <init-param>
31 + <param-name>com.sun.jersey.config.property.classnames</param-name>
32 + <param-value>
33 + org.onlab.onos.demo.DemoResource
34 + </param-value>
35 + </init-param>
36 + <load-on-startup>1</load-on-startup>
37 + </servlet>
38 +
39 + <servlet-mapping>
40 + <servlet-name>JAX-RS Service</servlet-name>
41 + <url-pattern>/*</url-pattern>
42 + </servlet-mapping>
43 +
44 +</web-app>
...\ No newline at end of file ...\ No newline at end of file
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
44 <module>optical</module> 44 <module>optical</module>
45 <module>metrics</module> 45 <module>metrics</module>
46 <module>oecfg</module> 46 <module>oecfg</module>
47 + <module>demo</module>
47 </modules> 48 </modules>
48 49
49 <properties> 50 <properties>
......
...@@ -227,4 +227,12 @@ ...@@ -227,4 +227,12 @@
227 <bundle>mvn:org.onlab.onos/onos-app-metrics-topology/1.0.0-SNAPSHOT</bundle> 227 <bundle>mvn:org.onlab.onos/onos-app-metrics-topology/1.0.0-SNAPSHOT</bundle>
228 </feature> 228 </feature>
229 229
230 + <feature name="onos-app-demo" version="1.0.0"
231 + description="ONOS demo applications">
232 + <feature>onos-api</feature>
233 + <bundle>mvn:org.onlab.onos/onos-app-demo/1.0.0-SNAPSHOT</bundle>
234 + </feature>
235 +
236 +
237 +
230 </features> 238 </features>
......