Add gRPC API for multicast service.
Change-Id: I003b982145c788e74c39f525122d58f3204b1c0e
diff --git a/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/impl/GrpcServer.java b/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/impl/GrpcServer.java
new file mode 100644
index 0000000..abd4c4a
--- /dev/null
+++ b/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/impl/GrpcServer.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2016-present 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.incubator.rpc.nb.impl;
+
+import io.grpc.Server;
+import io.grpc.netty.NettyServerBuilder;
+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.incubator.rpc.nb.mcast.MulticastRouteGrpcService;
+import org.onosproject.net.mcast.MulticastRouteService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+/**
+ * gRPC server for northbound APIs.
+ */
+@Component(immediate = true)
+public class GrpcServer {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected MulticastRouteService multicastRouteService;
+
+ // TODO make configurable
+ private int port = 50051;
+
+ private Server server;
+
+ @Activate
+ public void activate() {
+ start();
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ stop();
+ log.info("Stopped");
+ }
+
+ private void start() {
+ try {
+ server = NettyServerBuilder.forPort(port)
+ .addService(new MulticastRouteGrpcService(multicastRouteService))
+ .build()
+ .start();
+ log.info("gRPC server started listening on " + port);
+ } catch (IOException e) {
+ log.error("Failed to start gRPC server", e);
+ }
+ }
+
+ private void stop() {
+ if (server != null) {
+ server.shutdown();
+ }
+ }
+}
diff --git a/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/impl/package-info.java b/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/impl/package-info.java
new file mode 100644
index 0000000..4aadc11
--- /dev/null
+++ b/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present 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.
+ */
+
+/**
+ * Northbound gPRC implementation.
+ */
+package org.onosproject.incubator.rpc.nb.impl;
diff --git a/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/mcast/MulticastRouteGrpcService.java b/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/mcast/MulticastRouteGrpcService.java
new file mode 100644
index 0000000..c6347f5
--- /dev/null
+++ b/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/mcast/MulticastRouteGrpcService.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2016-present 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.incubator.rpc.nb.mcast;
+
+import io.grpc.stub.StreamObserver;
+import org.onlab.packet.IpAddress;
+import org.onosproject.grpc.net.mcast.MulticastRouteServiceGrpc;
+import org.onosproject.grpc.net.mcast.MulticastRouteServiceOuterClass;
+import org.onosproject.net.mcast.McastRoute;
+import org.onosproject.net.mcast.MulticastRouteService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Implementation of multicast gRPC service.
+ */
+@Beta
+public class MulticastRouteGrpcService
+ extends MulticastRouteServiceGrpc.MulticastRouteServiceImplBase {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private final MulticastRouteService multicastRouteService;
+
+ public MulticastRouteGrpcService(MulticastRouteService service) {
+ this.multicastRouteService = service;
+ }
+
+ @Override
+ public StreamObserver<MulticastRouteServiceOuterClass.MulticastRequest>
+ operation(StreamObserver<MulticastRouteServiceOuterClass.MulticastReply> responseObserver) {
+
+ return new MulticastServiceServerProxy(responseObserver);
+ }
+
+ private final class MulticastServiceServerProxy
+ implements StreamObserver<MulticastRouteServiceOuterClass.MulticastRequest> {
+
+ private final StreamObserver<MulticastRouteServiceOuterClass.MulticastReply> responseObserver;
+
+ public MulticastServiceServerProxy(
+ StreamObserver<MulticastRouteServiceOuterClass.MulticastReply> responseObserver) {
+ this.responseObserver = responseObserver;
+ }
+
+ @Override
+ public void onNext(MulticastRouteServiceOuterClass.MulticastRequest value) {
+ MulticastRouteServiceOuterClass.MulticastRoute route = value.getRoute();
+
+ switch (value.getOperation()) {
+ case ADD_ROUTE:
+ multicastRouteService.add(
+ new McastRoute(IpAddress.valueOf(route.getSource()),
+ IpAddress.valueOf(route.getGroup()),
+ McastRoute.Type.STATIC));
+ break;
+ case ADD_SOURCE:
+ break;
+ case ADD_SINK:
+ break;
+ case REMOVE_ROUTE:
+ break;
+ case REMOVE_SOURCE:
+ break;
+ case REMOVE_SINK:
+ break;
+ case UNRECOGNIZED:
+ default:
+ break;
+ }
+
+ responseObserver.onNext(MulticastRouteServiceOuterClass.MulticastReply.newBuilder().build());
+ }
+
+ @Override
+ public void onError(Throwable t) {
+ log.warn("Error receiving multicast route", t);
+ }
+
+ @Override
+ public void onCompleted() {
+ // When the client closes their stream, we'll close ours too
+ responseObserver.onCompleted();
+ }
+ }
+}
diff --git a/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/mcast/package-info.java b/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/mcast/package-info.java
new file mode 100644
index 0000000..9b42471
--- /dev/null
+++ b/incubator/rpc-nb/src/main/java/org/onosproject/incubator/rpc/nb/mcast/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present 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.
+ */
+
+/**
+ * Multicast gRPC implementation.
+ */
+package org.onosproject.incubator.rpc.nb.mcast;