Jonathan Hart
Committed by Gerrit Code Review

Implemented REST API for multicast RIB.

Change-Id: I5de6fe0aae412083597113e355ba0e8c984be5f1
......@@ -51,7 +51,7 @@ public interface McastStore extends Store<McastEvent, McastStoreDelegate> {
void storeSink(McastRoute route, ConnectPoint sink, Type operation);
/**
* Obtain the source for a multicast route.
* Obtains the source for a multicast route.
*
* @param route a multicast route
* @return a connect point
......@@ -59,10 +59,17 @@ public interface McastStore extends Store<McastEvent, McastStoreDelegate> {
ConnectPoint sourceFor(McastRoute route);
/**
* Obtain the sinks for a multicast route.
* Obtains the sinks for a multicast route.
*
* @param route a multicast route
* @return a set of sinks
*/
Set<ConnectPoint> sinksFor(McastRoute route);
/**
* Gets the set of all known multicast routes.
*
* @return set of multicast routes
*/
Set<McastRoute> getRoutes();
}
......
......@@ -43,6 +43,13 @@ public interface MulticastRouteService
void remove(McastRoute route);
/**
* Gets all multicast routes in the system.
*
* @return set of multicast routes
*/
Set<McastRoute> getRoutes();
/**
* Adds a source connection to the route from where the
* data stream is originating.
*
......
......@@ -53,6 +53,7 @@ import org.onosproject.net.intent.Constraint;
import org.onosproject.net.intent.HostToHostIntent;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.PointToPointIntent;
import org.onosproject.net.mcast.McastRoute;
import org.onosproject.net.meter.Band;
import org.onosproject.net.meter.Meter;
import org.onosproject.net.meter.MeterRequest;
......@@ -117,6 +118,7 @@ public class CodecManager implements CodecService {
registerCodec(FilteringObjective.class, new FilteringObjectiveCodec());
registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec());
registerCodec(NextObjective.class, new NextObjectiveCodec());
registerCodec(McastRoute.class, new McastRouteCodec());
log.info("Started");
}
......
package org.onosproject.codec.impl;
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.net.mcast.McastRoute;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Codec to encode and decode a multicast route to and from JSON.
*/
public class McastRouteCodec extends JsonCodec<McastRoute> {
private static final String SOURCE = "source";
private static final String GROUP = "group";
private static final String TYPE = "type";
@Override
public ObjectNode encode(McastRoute route, CodecContext context) {
checkNotNull(route);
ObjectNode root = context.mapper().createObjectNode()
.put(TYPE, route.type().toString())
.put(SOURCE, route.source().toString())
.put(GROUP, route.group().toString());
return root;
}
@Override
public McastRoute decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
IpAddress source = IpAddress.valueOf(json.path(SOURCE).asText());
IpAddress group = IpAddress.valueOf(json.path(GROUP).asText());
McastRoute route = new McastRoute(source, group, McastRoute.Type.STATIC);
return route;
}
}
......@@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.onlab.packet.ChassisId;
import org.onlab.packet.EthType;
import org.onlab.packet.Ip4Address;
......@@ -219,6 +220,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
public final class KryoNamespaces {
......@@ -228,6 +230,7 @@ public final class KryoNamespaces {
.register(AtomicBoolean.class)
.register(AtomicInteger.class)
.register(AtomicLong.class)
.register(AtomicReference.class)
.register(new ImmutableListSerializer(),
ImmutableList.class,
ImmutableList.of(1).getClass(),
......@@ -245,6 +248,7 @@ public final class KryoNamespaces {
.register(HashMap.class)
.register(ConcurrentHashMap.class)
.register(CopyOnWriteArraySet.class)
.register(Sets.newConcurrentHashSet().getClass())
.register(ArrayList.class,
LinkedList.class,
HashSet.class,
......
......@@ -55,7 +55,6 @@ public class MulticastRouteManager
@Activate
public void activate() {
eventDispatcher.addSink(McastEvent.class, listenerRegistry);
store.setDelegate(delegate);
......@@ -80,6 +79,11 @@ public class MulticastRouteManager
}
@Override
public Set<McastRoute> getRoutes() {
return store.getRoutes();
}
@Override
public void addSource(McastRoute route, ConnectPoint connectPoint) {
checkNotNull(route, "Route cannot be null");
checkNotNull(connectPoint, "Source cannot be null");
......@@ -94,7 +98,6 @@ public class MulticastRouteManager
}
@Override
public void removeSink(McastRoute route, ConnectPoint connectPoint) {
......
......@@ -6,7 +6,6 @@ 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.packet.IpAddress;
import org.onlab.util.KryoNamespace;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.mcast.McastEvent;
......@@ -15,12 +14,12 @@ import org.onosproject.net.mcast.McastRouteInfo;
import org.onosproject.net.mcast.McastStore;
import org.onosproject.net.mcast.McastStoreDelegate;
import org.onosproject.store.AbstractStore;
import org.onosproject.store.serializers.KryoNamespaces;
import org.onosproject.store.service.ConsistentMap;
import org.onosproject.store.service.Serializer;
import org.onosproject.store.service.StorageService;
import org.slf4j.Logger;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -43,7 +42,7 @@ public class DistributedMcastStore extends AbstractStore<McastEvent, McastStoreD
private Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
private StorageService storageService;
protected StorageService storageService;
protected ConsistentMap<McastRoute, MulticastData> mcastRib;
protected Map<McastRoute, MulticastData> mcastRoutes;
......@@ -54,12 +53,13 @@ public class DistributedMcastStore extends AbstractStore<McastEvent, McastStoreD
mcastRib = storageService.<McastRoute, MulticastData>consistentMapBuilder()
.withName(MCASTRIB)
.withSerializer(Serializer.using(KryoNamespace.newBuilder().register(
.withSerializer(Serializer.using(KryoNamespace.newBuilder()
.register(KryoNamespaces.BASIC)
.register(KryoNamespaces.MISC)
.register(
MulticastData.class,
McastRoute.class,
McastRoute.Type.class,
IpAddress.class,
List.class,
ConnectPoint.class
).build()))
.withRelaxedReadConsistency()
......@@ -174,4 +174,9 @@ public class DistributedMcastStore extends AbstractStore<McastEvent, McastStoreD
return mcastRoutes.getOrDefault(route, MulticastData.empty()).sinks();
}
@Override
public Set<McastRoute> getRoutes() {
return mcastRoutes.keySet();
}
}
......
......@@ -61,10 +61,12 @@ public final class MulticastData {
}
public void appendSink(ConnectPoint sink) {
checkNotNull(sink);
sinks.add(sink);
}
public boolean removeSink(ConnectPoint sink) {
checkNotNull(sink);
return sinks.remove(sink);
}
......
......@@ -44,7 +44,8 @@ public class CoreWebApplication extends AbstractWebApplication {
PathsWebResource.class,
StatisticsWebResource.class,
MetricsWebResource.class,
FlowObjectiveWebResource.class
FlowObjectiveWebResource.class,
MulticastRouteWebResource.class
);
}
}
......
/*
* Copyright 2016 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.rest.resources;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.net.mcast.McastRoute;
import org.onosproject.net.mcast.MulticastRouteService;
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.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.Set;
/**
* Manage the multicast routing information.
*/
@Path("mcast")
public class MulticastRouteWebResource extends AbstractWebResource {
/**
* Get all multicast routes.
* Returns array of all known multicast routes.
*
* @return 200 OK
* @onos.rsModel McastRoutesGet
*/
@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();
}
/**
* Create new multicast route.
* Creates a new route in the multicast RIB.
*
* @onos.rsModel McastRoutePost
* @param stream multicast route JSON
* @return status of the request - CREATED if the JSON is correct,
* BAD_REQUEST if the JSON is invalid
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(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);
} catch (IOException ex) {
throw new IllegalArgumentException(ex);
}
return Response
.created(URI.create(""))
.build();
}
/**
* Remove a multicast route.
* Removes a route from the multicast RIB.
*
* @param stream multicast route JSON
* @onos.rsModel McastRoutePost
*/
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public void deleteRoute(InputStream stream) {
MulticastRouteService service = get(MulticastRouteService.class);
try {
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
McastRoute route = codec(McastRoute.class).decode(jsonTree, this);
service.remove(route);
} catch (IOException ex) {
throw new IllegalArgumentException(ex);
}
}
}
{
"type": "object",
"title": "route",
"required": [
"source",
"group"
],
"properties": {
"source": {
"type": "string",
"example": "10.1.1.0",
"description": "Multicast source IP address"
},
"group": {
"type": "string",
"example": "10.1.1.0",
"description": "Multicast group IP address"
}
}
}
{
"type": "object",
"title": "routes",
"required": [
"routes"
],
"properties": {
"routes": {
"type": "array",
"xml": {
"name": "routes",
"wrapped": true
},
"items": {
"type": "object",
"title": "route",
"required": [
"source",
"group",
"type"
],
"properties": {
"source": {
"type": "string",
"example": "10.1.1.0",
"description": "Multicast source IP address"
},
"group": {
"type": "string",
"example": "10.1.1.0",
"description": "Multicast group IP address"
},
"type": {
"type": "string",
"example": "STATIC",
"description": "Type of the multicast route"
}
}
}
}
}
}