Adding rest API for new Mcast app
Change-Id: I8879879f0406295b61db11b9a66efef4acc0b0c1
diff --git a/apps/mcast/api/src/main/java/org/onosproject/mcast/api/MulticastRouteService.java b/apps/mcast/api/src/main/java/org/onosproject/mcast/api/MulticastRouteService.java
index 37ebcd5..bcee070 100644
--- a/apps/mcast/api/src/main/java/org/onosproject/mcast/api/MulticastRouteService.java
+++ b/apps/mcast/api/src/main/java/org/onosproject/mcast/api/MulticastRouteService.java
@@ -54,7 +54,7 @@
/**
* Gets a Multicast route in the system.
*
- * @param groupIp Multicast group IP address
+ * @param groupIp Multicast group IP address
* @param sourceIp Multicasto source Ip address
* @return set of Multicast routes
*/
@@ -94,6 +94,16 @@
void addSink(McastRoute route, HostId hostId);
/**
+ * Adds a set of sink connect points for a given host sink to the route to
+ * which a data stream should be sent to.
+ *
+ * @param route a Multicast route
+ * @param hostId a sink host
+ * @param connectPoints the sink for the specific host
+ */
+ void addSinks(McastRoute route, HostId hostId, Set<ConnectPoint> connectPoints);
+
+ /**
* Adds a set of sink to the route to which a data stream should be
* sent to. If this method is used the connect points will all be
* used as different sinks for that Mcast Tree. For dual-homed sinks
diff --git a/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastRoutesListCommand.java b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastRoutesListCommand.java
new file mode 100644
index 0000000..8e43911
--- /dev/null
+++ b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastRoutesListCommand.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.mcast.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.mcast.api.McastRoute;
+import org.onosproject.mcast.api.MulticastRouteService;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Comparator;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Displays the source, multicast group flows entries.
+ */
+@Command(scope = "onos", name = "mcast-host-routes",
+ description = "Lists routes in the mcast route store")
+public class McastRoutesListCommand extends AbstractShellCommand {
+
+ // Format for total
+ private static final String FORMAT_TOTAL = " Total: %d";
+
+ // Format for ipv4
+ private static final String FORMAT_ROUTE = "%-1s %-18s %-15s %s %s %s";
+ // Format for ipv6
+ private static final String FORMAT_ROUTE6 = "%-1s %-40s %-36s %s %s %s";
+
+ // Table header
+ private static final String FORMAT_TABLE = "Table: %s";
+ private static final String GROUP = "Group";
+ private static final String SOURCE = "Source";
+ private static final String ORIGIN = "Origin";
+ private static final String SOURCES = "Sources";
+ private static final String SINKS = "Sinks";
+
+ @Override
+ protected void execute() {
+ // Get the service
+ MulticastRouteService mcastService = get(MulticastRouteService.class);
+ // Get the routes
+ Set<McastRoute> routes = mcastService.getRoutes();
+ // Filter ipv4
+ Set<McastRoute> ipv4Routes = routes.stream()
+ .filter(mcastRoute -> mcastRoute.group().isIp4())
+ .collect(Collectors.toSet());
+ // Filter ipv6
+ Set<McastRoute> ipv6Routes = routes.stream()
+ .filter(mcastRoute -> mcastRoute.group().isIp6())
+ .collect(Collectors.toSet());
+ // Print header
+ print(FORMAT_TABLE, "ipv4");
+ print(FORMAT_ROUTE, "", GROUP, SOURCE, ORIGIN, SOURCES, SINKS);
+ // Print ipv4 mcast routing entries
+ ipv4Routes.stream()
+ .sorted(Comparator.comparing(McastRoute::group))
+ .forEach(route -> {
+ // Get sinks and sources
+ Set<ConnectPoint> sources = mcastService.sources(route);
+ Set<ConnectPoint> sinks = mcastService.sinks(route);
+ Optional<IpAddress> sourceIp = route.source();
+ String src = "* ";
+ if (sourceIp.isPresent()) {
+ src = sourceIp.get().toString();
+ }
+ print(FORMAT_ROUTE, "", route.group(), src,
+ route.type(), sources.size(), " " + sinks.size());
+ });
+ print(FORMAT_TOTAL, ipv4Routes.size());
+ print("");
+
+ // Print header
+ print(FORMAT_TABLE, "ipv6");
+ print(FORMAT_ROUTE6, "", GROUP, SOURCE, ORIGIN, SOURCES, SINKS);
+ // Print ipv6 mcast routing entries
+ ipv6Routes.stream()
+ .sorted(Comparator.comparing(McastRoute::group))
+ .forEach(route -> {
+ // Get sinks and sources
+ Set<ConnectPoint> sources = mcastService.sources(route);
+ Set<ConnectPoint> sinks = mcastService.sinks(route);
+ Optional<IpAddress> sourceIp = route.source();
+ String src = "* ";
+ if (sourceIp.isPresent()) {
+ src = sourceIp.get().toString();
+ }
+ print(FORMAT_ROUTE6, "", route.group(), src,
+ route.type(), sources.size(), " " + sinks.size());
+ });
+ print(FORMAT_TOTAL, ipv6Routes.size());
+ print("");
+ }
+
+
+}
diff --git a/apps/mcast/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/mcast/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 0c3cc10..1525938 100644
--- a/apps/mcast/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/mcast/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -46,7 +46,9 @@
<entry key="-src" value-ref="connectpointCompleter"/>
</optional-completers>
</command>
-
+ <command>
+ <action class="org.onosproject.mcast.cli.McastRoutesListCommand"/>
+ </command>
</command-bundle>
<bean id="hostIdCompleter" class="org.onosproject.cli.net.HostIdCompleter"/>
diff --git a/apps/mcast/impl/src/main/java/org/onosproject/mcast/impl/MulticastRouteManager.java b/apps/mcast/impl/src/main/java/org/onosproject/mcast/impl/MulticastRouteManager.java
index 43b145c..055d8de 100644
--- a/apps/mcast/impl/src/main/java/org/onosproject/mcast/impl/MulticastRouteManager.java
+++ b/apps/mcast/impl/src/main/java/org/onosproject/mcast/impl/MulticastRouteManager.java
@@ -158,6 +158,14 @@
}
@Override
+ public void addSinks(McastRoute route, HostId hostId, Set<ConnectPoint> sinks) {
+ if (checkRoute(route)) {
+ store.addSink(route, hostId, sinks);
+ }
+
+ }
+
+ @Override
public void addSink(McastRoute route, Set<ConnectPoint> sinks) {
checkNotNull(route, "Route cannot be null");
checkNotNull(sinks, "Sinks cannot be null");
diff --git a/apps/mcast/web/BUCK b/apps/mcast/web/BUCK
index c036dc0..7fdc057 100644
--- a/apps/mcast/web/BUCK
+++ b/apps/mcast/web/BUCK
@@ -3,6 +3,7 @@
'//lib:JACKSON',
'//utils/rest:onlab-rest',
'//lib:javax.ws.rs-api',
+ '//apps/mcast/api:onos-apps-mcast-api'
]
TEST_DEPS = [
@@ -16,4 +17,9 @@
osgi_jar_with_tests (
deps = COMPILE_DEPS,
test_deps = TEST_DEPS,
+ web_context = '/onos/mcast',
+ api_title = 'Multicast API',
+ api_version = '1.0',
+ api_description = 'REST API for Multicast',
+ api_package = 'org.onosproject.mcast.web',
)
diff --git a/apps/mcast/web/pom.xml b/apps/mcast/web/pom.xml
index cc43c34..e470b9e 100644
--- a/apps/mcast/web/pom.xml
+++ b/apps/mcast/web/pom.xml
@@ -49,6 +49,11 @@
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
+ <artifactId>onos-app-mcast-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
<artifactId>onlab-osgi</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
diff --git a/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastHostRouteCodec.java b/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastHostRouteCodec.java
new file mode 100644
index 0000000..cb41e34
--- /dev/null
+++ b/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastHostRouteCodec.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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.mcast.web;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.mcast.api.McastRoute;
+import org.onosproject.mcast.api.MulticastRouteService;
+import org.slf4j.Logger;
+
+import java.util.Optional;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Codec to encode and decode a multicast route to and from JSON.
+ */
+public class McastHostRouteCodec extends JsonCodec<McastRoute> {
+
+ private Logger log = getLogger(getClass());
+
+ private static final String SOURCE = "source";
+ private static final String GROUP = "group";
+ private static final String TYPE = "type";
+ private static final String SOURCES = "sources";
+ private static final String SINKS = "sinks";
+
+ @Override
+ public ObjectNode encode(McastRoute route, CodecContext context) {
+ checkNotNull(route);
+
+ ObjectNode root = context.mapper().createObjectNode()
+ .put(TYPE, route.type().toString())
+ .put(GROUP, route.group().toString());
+ Optional<IpAddress> sourceIp = route.source();
+ if (sourceIp.isPresent()) {
+ root.put(SOURCE, sourceIp.get().toString());
+ } else {
+ root.put(SOURCE, "*");
+ }
+
+ ArrayNode sources = context.mapper().createArrayNode();
+ context.getService(MulticastRouteService.class).sources(route).forEach(source -> {
+ sources.add(source.toString());
+ });
+ root.putPOJO(SOURCES, sources);
+
+ ObjectNode sinks = context.mapper().createObjectNode();
+ context.getService(MulticastRouteService.class).routeData(route).sinks().forEach((k, v) -> {
+ ArrayNode node = context.mapper().createArrayNode();
+ v.forEach(sink -> {
+ node.add(sink.toString());
+ });
+ sinks.putPOJO(k.toString(), node);
+ });
+ root.putPOJO(SINKS, sinks);
+
+ return root;
+ }
+
+ @Override
+ public McastRoute decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ String source = json.path(SOURCE).asText();
+
+ IpAddress sourceIp = null;
+
+ if (!source.equals("*")) {
+ sourceIp = IpAddress.valueOf(source);
+ }
+
+ IpAddress group = IpAddress.valueOf(json.path(GROUP).asText());
+
+ McastRoute route = new McastRoute(sourceIp, group, McastRoute.Type.STATIC);
+
+ return route;
+ }
+}
diff --git a/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastRouteWebResource.java b/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastRouteWebResource.java
index c198a66..26e3317 100644
--- a/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastRouteWebResource.java
+++ b/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastRouteWebResource.java
@@ -16,14 +16,35 @@
package org.onosproject.mcast.web;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.annotations.Beta;
+import com.google.common.collect.ImmutableSet;
+import org.onlab.packet.IpAddress;
+import org.onosproject.mcast.api.McastRoute;
+import org.onosproject.mcast.api.MulticastRouteService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.HostId;
import org.onosproject.rest.AbstractWebResource;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
+import javax.ws.rs.POST;
import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
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;
+import java.net.URI;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+import static org.onlab.util.Tools.nullIsIllegal;
/**
* Manage the multicast routing information.
@@ -32,6 +53,20 @@
@Path("mcast")
public class McastRouteWebResource extends AbstractWebResource {
+ //TODO return error messages
+
+ private static final String SOURCES = "sources";
+ private static final String SINKS = "sinks";
+ private static final String ROUTES = "routes";
+ private static final String ROUTES_KEY_ERROR = "No routes";
+ private static final String ASM = "*";
+
+ private Optional<McastRoute> getStaticRoute(Set<McastRoute> mcastRoutes) {
+ return mcastRoutes.stream()
+ .filter(mcastRoute -> mcastRoute.type() == McastRoute.Type.STATIC)
+ .findAny();
+ }
+
/**
* Get all multicast routes.
* Returns array of all known multicast routes.
@@ -41,7 +76,462 @@
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getRoutes() {
+ Set<McastRoute> routes = get(MulticastRouteService.class).getRoutes();
+ ObjectNode root = encodeArray(McastRoute.class, ROUTES, routes);
+ return ok(root).build();
+ }
+
+ /**
+ * Gets a multicast route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @return 200 OK with a multicast routes
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("{group}/{srcIp}")
+ public Response getRoute(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp) {
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ if (route.isPresent()) {
+ ObjectNode root = encode(route.get(), McastRoute.class);
+ return ok(root).build();
+ }
return Response.noContent().build();
}
+ /**
+ * Get all sources connect points for a multicast route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @return 200 OK with array of all sources for multicast route
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("sources/{group}/{srcIp}")
+ public Response getSources(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp) {
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ if (route.isPresent()) {
+ get(MulticastRouteService.class).sources(route.get());
+ ArrayNode node = this.mapper().createArrayNode();
+ get(MulticastRouteService.class).sources(route.get()).forEach(source -> {
+ node.add(source.toString());
+ });
+ ObjectNode root = this.mapper().createObjectNode().putPOJO(SOURCES, node);
+ return ok(root).build();
+ }
+ return Response.noContent().build();
+ }
+
+ /**
+ * Get all sinks connect points for a multicast route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @return 200 OK with array of all sinks for multicast route
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("sinks/{group}/{srcIp}")
+ public Response getSinks(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp) {
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ if (route.isPresent()) {
+ get(MulticastRouteService.class).sources(route.get());
+ ObjectNode sinks = this.mapper().createObjectNode();
+ get(MulticastRouteService.class).routeData(route.get()).sinks().forEach((k, v) -> {
+ ArrayNode node = this.mapper().createArrayNode();
+ v.forEach(sink -> {
+ node.add(sink.toString());
+ });
+ sinks.putPOJO(k.toString(), node);
+ });
+ ObjectNode root = this.mapper().createObjectNode().putPOJO(SINKS, sinks);
+ return ok(root).build();
+ }
+ return Response.noContent().build();
+ }
+
+ /**
+ * Get all sink connect points for a given sink host in a multicast route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @param hostId host Id
+ * @return 200 OK with array of all sinks for multicast route
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("sinks/{group}/{srcIp}/{hostId}")
+ public Response getHostSinks(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp,
+ @PathParam("hostId") String hostId) {
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ if (route.isPresent()) {
+ get(MulticastRouteService.class).sources(route.get());
+ ArrayNode node = this.mapper().createArrayNode();
+ get(MulticastRouteService.class).sinks(route.get(), HostId.hostId(hostId))
+ .forEach(source -> {
+ node.add(source.toString());
+ });
+ ObjectNode root = this.mapper().createObjectNode().putPOJO(SINKS, node);
+ return ok(root).build();
+ }
+ return Response.noContent().build();
+ }
+
+ /**
+ * Creates a set of new multicast routes.
+ *
+ * @param stream multicast routes JSON
+ * @return status of the request - CREATED if the JSON is correct,
+ * BAD_REQUEST if the JSON is invalid
+ * @onos.rsModel McastRouteBulk
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Path("bulk/")
+ public Response createRoutes(InputStream stream) {
+ MulticastRouteService service = get(MulticastRouteService.class);
+ try {
+ ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+ ArrayNode routesArray = nullIsIllegal((ArrayNode) jsonTree.get(ROUTES),
+ ROUTES_KEY_ERROR);
+ routesArray.forEach(routeJson -> {
+ McastRoute route = codec(McastRoute.class).decode((ObjectNode) routeJson, this);
+ service.add(route);
+
+ Set<ConnectPoint> sources = new HashSet<>();
+ routeJson.path(SOURCES).elements().forEachRemaining(src -> {
+ sources.add(ConnectPoint.deviceConnectPoint(src.asText()));
+ });
+ Set<HostId> sinks = new HashSet<>();
+ routeJson.path(SINKS).elements().forEachRemaining(sink -> {
+ sinks.add(HostId.hostId(sink.asText()));
+ });
+
+ if (!sources.isEmpty()) {
+ service.addSources(route, sources);
+ }
+ if (!sinks.isEmpty()) {
+ sinks.forEach(sink -> {
+ service.addSink(route, sink);
+ });
+ }
+ });
+ } catch (IOException ex) {
+ throw new IllegalArgumentException(ex);
+ }
+
+ return Response
+ .created(URI.create(""))
+ .build();
+ }
+
+ /**
+ * Create new multicast route.
+ *
+ * @param stream multicast route JSON
+ * @return status of the request - CREATED if the JSON is correct,
+ * BAD_REQUEST if the JSON is invalid
+ * @onos.rsModel McastRoute
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response createRoute(InputStream stream) {
+ MulticastRouteService service = get(MulticastRouteService.class);
+ try {
+ ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+ McastRoute route = codec(McastRoute.class).decode(jsonTree, this);
+ service.add(route);
+
+ Set<ConnectPoint> sources = new HashSet<>();
+ jsonTree.path(SOURCES).elements().forEachRemaining(src -> {
+ sources.add(ConnectPoint.deviceConnectPoint(src.asText()));
+ });
+ Set<HostId> sinks = new HashSet<>();
+ jsonTree.path(SINKS).elements().forEachRemaining(sink -> {
+ sinks.add(HostId.hostId(sink.asText()));
+ });
+
+ if (!sources.isEmpty()) {
+ service.addSources(route, sources);
+ }
+ if (!sinks.isEmpty()) {
+ sinks.forEach(sink -> {
+ service.addSink(route, sink);
+ });
+ }
+
+ } catch (IOException ex) {
+ throw new IllegalArgumentException(ex);
+ }
+
+ return Response
+ .created(URI.create(""))
+ .build();
+ }
+
+ /**
+ * Adds sources for a given existing multicast route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @param stream host sinks JSON
+ * @return status of the request - CREATED if the JSON is correct,
+ * BAD_REQUEST if the JSON is invalid
+ * @onos.rsModel McastSourcesAdd
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("sources/{group}/{srcIp}")
+ public Response addSources(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp,
+ InputStream stream) {
+ MulticastRouteService service = get(MulticastRouteService.class);
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ if (route.isPresent()) {
+ ArrayNode jsonTree;
+ try {
+ jsonTree = (ArrayNode) mapper().readTree(stream).get(SOURCES);
+ Set<ConnectPoint> sources = new HashSet<>();
+ jsonTree.elements().forEachRemaining(src -> {
+ sources.add(ConnectPoint.deviceConnectPoint(src.asText()));
+ });
+ if (!sources.isEmpty()) {
+ service.addSources(route.get(), sources);
+ }
+ } catch (IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+ return Response.ok().build();
+ }
+ return Response.noContent().build();
+
+ }
+
+ /**
+ * Adds sinks for a given existing multicast route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @param stream host sinks JSON
+ * @return status of the request - CREATED if the JSON is correct,
+ * BAD_REQUEST if the JSON is invalid
+ * @onos.rsModel McastSinksAdd
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("sinks/{group}/{srcIp}")
+ public Response addSinks(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp,
+ InputStream stream) {
+ MulticastRouteService service = get(MulticastRouteService.class);
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ if (route.isPresent()) {
+ ArrayNode jsonTree;
+ try {
+ jsonTree = (ArrayNode) mapper().readTree(stream).get(SINKS);
+ Set<HostId> sinks = new HashSet<>();
+ jsonTree.elements().forEachRemaining(sink -> {
+ sinks.add(HostId.hostId(sink.asText()));
+ });
+ if (!sinks.isEmpty()) {
+ sinks.forEach(sink -> {
+ service.addSink(route.get(), sink);
+ });
+ }
+ } catch (IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+ return Response.ok().build();
+ }
+ return Response.noContent().build();
+ }
+
+
+ /**
+ * Adds a new sink for an existing host in a given multicast route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @param hostId the host Id
+ * @param stream sink connect points JSON
+ * @return status of the request - CREATED if the JSON is correct,
+ * BAD_REQUEST if the JSON is invalid
+ * @onos.rsModel McastHostSinksAdd
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("sinks/{group}/{srcIp}/{hostId}")
+ public Response addHostSinks(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp,
+ @PathParam("hostId") String hostId,
+ InputStream stream) {
+ MulticastRouteService service = get(MulticastRouteService.class);
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ if (route.isPresent()) {
+ ArrayNode jsonTree;
+ try {
+ jsonTree = (ArrayNode) mapper().readTree(stream).get(SINKS);
+ Set<ConnectPoint> sinks = new HashSet<>();
+ jsonTree.elements().forEachRemaining(src -> {
+ sinks.add(ConnectPoint.deviceConnectPoint(src.asText()));
+ });
+ if (!sinks.isEmpty()) {
+ service.addSinks(route.get(), HostId.hostId(hostId), sinks);
+ }
+ } catch (IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+ return Response.ok().build();
+ }
+ return Response.noContent().build();
+ }
+
+ /**
+ * Removes all the multicast routes.
+ *
+ * @return 204 NO CONTENT
+ */
+ @DELETE
+ public Response deleteRoutes() {
+ MulticastRouteService service = get(MulticastRouteService.class);
+ service.getRoutes().forEach(service::remove);
+ return Response.noContent().build();
+ }
+
+ /**
+ * Removes all the given multicast routes.
+ *
+ * @param stream the set of multicast routes
+ * @return 204 NO CONTENT
+ */
+ @DELETE
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Path("bulk/")
+ public Response deleteRoutes(InputStream stream) {
+ MulticastRouteService service = get(MulticastRouteService.class);
+ try {
+ ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+ ArrayNode routesArray = nullIsIllegal((ArrayNode) jsonTree.get(ROUTES),
+ ROUTES_KEY_ERROR);
+ List<McastRoute> routes = codec(McastRoute.class).decode(routesArray, this);
+ routes.forEach(service::remove);
+ } catch (IOException ex) {
+ throw new IllegalArgumentException(ex);
+ }
+ return Response.noContent().build();
+ }
+
+ /**
+ * Deletes a specific route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @return 204 NO CONTENT
+ */
+ @DELETE
+ @Path("{group}/{srcIp}")
+ public Response deleteRoute(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp) {
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ route.ifPresent(mcastRoute -> {
+ get(MulticastRouteService.class).remove(mcastRoute);
+ });
+ return Response.noContent().build();
+ }
+
+ /**
+ * Deletes all the source connect points for a specific route.
+ * If the sources are empty the entire route is removed.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @return 204 NO CONTENT
+ */
+ @DELETE
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Path("sources/{group}/{srcIp}")
+ public Response deleteSources(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp) {
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ route.ifPresent(mcastRoute -> get(MulticastRouteService.class).removeSources(mcastRoute));
+ return Response.noContent().build();
+ }
+
+ /**
+ * Deletes a source connect point for a specific route.
+ * If the sources are empty the entire route is removed.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @param srcCp source connect point
+ * @return 204 NO CONTENT
+ */
+ @DELETE
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Path("sources/{group}/{srcIp}/{srcCp}")
+ public Response deleteSource(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp,
+ @PathParam("srcCp") String srcCp) {
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ route.ifPresent(mcastRoute -> get(MulticastRouteService.class)
+ .removeSources(mcastRoute, ImmutableSet.of(ConnectPoint.deviceConnectPoint(srcCp))));
+ return Response.noContent().build();
+ }
+
+ /**
+ * Deletes all the sinks for a specific route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @return 204 NO CONTENT
+ */
+ @DELETE
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Path("sinks/{group}/{srcIp}")
+ public Response deleteHostsSinks(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp) {
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ route.ifPresent(mcastRoute -> get(MulticastRouteService.class)
+ .removeSinks(mcastRoute));
+ return Response.noContent().build();
+ }
+
+ /**
+ * Deletes a sink connect points for a given host for a specific route.
+ *
+ * @param group group IP address
+ * @param srcIp source IP address
+ * @param hostId sink host
+ * @return 204 NO CONTENT
+ */
+ @DELETE
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Path("sinks/{group}/{srcIp}/{hostId}")
+ public Response deleteHostSinks(@PathParam("group") String group,
+ @PathParam("srcIp") String srcIp,
+ @PathParam("hostId") String hostId) {
+ Optional<McastRoute> route = getMcastRoute(group, srcIp);
+ route.ifPresent(mcastRoute -> get(MulticastRouteService.class)
+ .removeSink(mcastRoute, HostId.hostId(hostId)));
+ return Response.noContent().build();
+ }
+
+ private Optional<McastRoute> getMcastRoute(String group, String srcIp) {
+ IpAddress ipAddress = null;
+ if (!srcIp.equals(ASM)) {
+ ipAddress = IpAddress.valueOf(srcIp);
+ }
+ return getStaticRoute(get(MulticastRouteService.class)
+ .getRoute(IpAddress.valueOf(group), ipAddress));
+ }
+
}
diff --git a/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastServiceCodecRegistrator.java b/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastServiceCodecRegistrator.java
new file mode 100644
index 0000000..90ba170
--- /dev/null
+++ b/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastServiceCodecRegistrator.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.mcast.web;
+
+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.codec.CodecService;
+import org.onosproject.mcast.api.McastRoute;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Implementation of the JSON codec brokering service for route service app.
+ */
+@Component(immediate = true)
+public class McastServiceCodecRegistrator {
+
+ private static Logger log = LoggerFactory.getLogger(McastServiceCodecRegistrator.class);
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CodecService codecService;
+
+ @Activate
+ public void activate() {
+ codecService.registerCodec(McastRoute.class, new McastHostRouteCodec());
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ codecService.unregisterCodec(McastRoute.class);
+ log.info("Stopped");
+ }
+}
diff --git a/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastWebApplication.java b/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastWebApplication.java
new file mode 100644
index 0000000..a1d36a8
--- /dev/null
+++ b/apps/mcast/web/src/main/java/org/onosproject/mcast/web/McastWebApplication.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.mcast.web;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * Route Service Web application.
+ */
+public class McastWebApplication extends AbstractWebApplication {
+ @Override
+ public Set<Class<?>> getClasses() {
+ return getClasses(McastRouteWebResource.class);
+ }
+}
diff --git a/apps/mcast/web/src/main/resources/definitions/McastHostSinksAdd.json b/apps/mcast/web/src/main/resources/definitions/McastHostSinksAdd.json
new file mode 100644
index 0000000..6f03797
--- /dev/null
+++ b/apps/mcast/web/src/main/resources/definitions/McastHostSinksAdd.json
@@ -0,0 +1,22 @@
+{
+ "type": "object",
+ "title": "sinks",
+ "required": [
+ "sinks"
+ ],
+ "properties": {
+ "sinks": {
+ "type": "array",
+ "xml": {
+ "name": "sinks",
+ "wrapped": true
+ },
+ "items": {
+ "type": "string",
+ "example": "of:0000000000000206/8",
+ "description": "A sink connect point for a host in the route"
+ },
+ "description": "Sink connect points for a host in the route"
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/mcast/web/src/main/resources/definitions/McastRoute.json b/apps/mcast/web/src/main/resources/definitions/McastRoute.json
new file mode 100644
index 0000000..006ed70
--- /dev/null
+++ b/apps/mcast/web/src/main/resources/definitions/McastRoute.json
@@ -0,0 +1,50 @@
+{
+ "type": "object",
+ "title": "multicast-route-creation",
+ "required": [
+ "group",
+ "source"
+ ],
+ "optional": [
+ "sources",
+ "sinks"
+ ],
+ "properties": {
+ "group": {
+ "type": "string",
+ "example": "224.0.1.1",
+ "description": "Multicast Route Group IP Address"
+ },
+ "source": {
+ "type": "string",
+ "example": "10.0.1.1",
+ "description": "Multicast Route Source IP Address"
+ },
+ "sources": {
+ "type": "array",
+ "xml": {
+ "name": "sources",
+ "wrapped": true
+ },
+ "items": {
+ "type": "string",
+ "example": "of:0000000000000206/8",
+ "description": "A source connect point for the route"
+ },
+ "description": "Source connect points for the route"
+ },
+ "sinks": {
+ "type": "array",
+ "xml": {
+ "name": "sinks",
+ "wrapped": true
+ },
+ "items": {
+ "type": "string",
+ "example": "00:CC:00:00:00:01/None",
+ "description": "A host sink for the route"
+ },
+ "description": "Host sinks for the route"
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/mcast/web/src/main/resources/definitions/McastRouteBulk.json b/apps/mcast/web/src/main/resources/definitions/McastRouteBulk.json
new file mode 100644
index 0000000..e3efdb7
--- /dev/null
+++ b/apps/mcast/web/src/main/resources/definitions/McastRouteBulk.json
@@ -0,0 +1,53 @@
+{
+ "type": "object",
+ "title": "multicast-routes-creation-bulk",
+ "required": [
+ "routes"
+ ],
+ "properties": {
+ "routes": {
+ "type": "array",
+ "items": {
+ "properties": {
+ "group": {
+ "type": "string",
+ "example": "224.0.1.1",
+ "description": "Multicast Route Group IP Address"
+ },
+ "source": {
+ "type": "string",
+ "example": "10.0.1.1",
+ "description": "Multicast Route Source IP Address"
+ },
+ "sources": {
+ "type": "array",
+ "xml": {
+ "name": "sources",
+ "wrapped": true
+ },
+ "items": {
+ "type": "string",
+ "example": "of:0000000000000206/8",
+ "description": "A source connect point for the route"
+ },
+ "description": "Source connect points for the route"
+ },
+ "sinks": {
+ "type": "array",
+ "xml": {
+ "name": "sinks",
+ "wrapped": true
+ },
+ "items": {
+ "type": "string",
+ "example": "00:CC:00:00:00:01/None",
+ "description": "A host sink for the route"
+ },
+ "description": "Host sinks for the route"
+ }
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/mcast/web/src/main/resources/definitions/McastSinksAdd.json b/apps/mcast/web/src/main/resources/definitions/McastSinksAdd.json
new file mode 100644
index 0000000..eecf0d8
--- /dev/null
+++ b/apps/mcast/web/src/main/resources/definitions/McastSinksAdd.json
@@ -0,0 +1,22 @@
+{
+ "type": "object",
+ "title": "sinks",
+ "required": [
+ "sinks"
+ ],
+ "properties": {
+ "sinks": {
+ "type": "array",
+ "xml": {
+ "name": "sinks",
+ "wrapped": true
+ },
+ "items": {
+ "type": "string",
+ "example": "00:CC:00:00:00:01/None",
+ "description": "A host sink for the route"
+ },
+ "description": "Host sinks for the route"
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/mcast/web/src/main/resources/definitions/McastSourcesAdd.json b/apps/mcast/web/src/main/resources/definitions/McastSourcesAdd.json
new file mode 100644
index 0000000..72910a9
--- /dev/null
+++ b/apps/mcast/web/src/main/resources/definitions/McastSourcesAdd.json
@@ -0,0 +1,22 @@
+{
+ "type": "object",
+ "title": "sources",
+ "required": [
+ "sources"
+ ],
+ "properties": {
+ "sources": {
+ "type": "array",
+ "xml": {
+ "name": "sources",
+ "wrapped": true
+ },
+ "items": {
+ "type": "string",
+ "example": "of:0000000000000206/8",
+ "description": "A source connect point for the route"
+ },
+ "description": "Source connect points for the route"
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/mcast/web/src/main/webapp/WEB-INF/web.xml b/apps/mcast/web/src/main/webapp/WEB-INF/web.xml
index 27b89ec..d1f8718 100644
--- a/apps/mcast/web/src/main/webapp/WEB-INF/web.xml
+++ b/apps/mcast/web/src/main/webapp/WEB-INF/web.xml
@@ -20,15 +20,33 @@
id="ONOS" version="2.5">
<display-name>Mcast REST API v1.0</display-name>
+ <security-constraint>
+ <web-resource-collection>
+ <web-resource-name>Secured</web-resource-name>
+ <url-pattern>/*</url-pattern>
+ </web-resource-collection>
+ <auth-constraint>
+ <role-name>admin</role-name>
+ </auth-constraint>
+ </security-constraint>
+
+ <security-role>
+ <role-name>admin</role-name>
+ </security-role>
+
+ <login-config>
+ <auth-method>BASIC</auth-method>
+ <realm-name>karaf</realm-name>
+ </login-config>
+
<servlet>
<servlet-name>JAX-RS Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
- <param-name>jersey.config.server.provider.classnames</param-name>
- <param-value>
- org.onosproject.mcast.web.McastRouteWebResource</param-value>
+ <param-name>javax.ws.rs.Application</param-name>
+ <param-value>org.onosproject.mcast.web.McastWebApplication</param-value>
</init-param>
- <load-on-startup>10</load-on-startup>
+ <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>