blob: c6347f51f5dce922864755776667eaa87fb82665 [file] [log] [blame]
Jonathan Hart097c8f52016-06-09 18:08:11 -07001/*
2 * Copyright 2016-present 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
17package org.onosproject.incubator.rpc.nb.mcast;
18
19import io.grpc.stub.StreamObserver;
20import org.onlab.packet.IpAddress;
21import org.onosproject.grpc.net.mcast.MulticastRouteServiceGrpc;
22import org.onosproject.grpc.net.mcast.MulticastRouteServiceOuterClass;
23import org.onosproject.net.mcast.McastRoute;
24import org.onosproject.net.mcast.MulticastRouteService;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.common.annotations.Beta;
29
30/**
31 * Implementation of multicast gRPC service.
32 */
33@Beta
34public class MulticastRouteGrpcService
35 extends MulticastRouteServiceGrpc.MulticastRouteServiceImplBase {
36
37 private final Logger log = LoggerFactory.getLogger(getClass());
38
39 private final MulticastRouteService multicastRouteService;
40
41 public MulticastRouteGrpcService(MulticastRouteService service) {
42 this.multicastRouteService = service;
43 }
44
45 @Override
46 public StreamObserver<MulticastRouteServiceOuterClass.MulticastRequest>
47 operation(StreamObserver<MulticastRouteServiceOuterClass.MulticastReply> responseObserver) {
48
49 return new MulticastServiceServerProxy(responseObserver);
50 }
51
52 private final class MulticastServiceServerProxy
53 implements StreamObserver<MulticastRouteServiceOuterClass.MulticastRequest> {
54
55 private final StreamObserver<MulticastRouteServiceOuterClass.MulticastReply> responseObserver;
56
57 public MulticastServiceServerProxy(
58 StreamObserver<MulticastRouteServiceOuterClass.MulticastReply> responseObserver) {
59 this.responseObserver = responseObserver;
60 }
61
62 @Override
63 public void onNext(MulticastRouteServiceOuterClass.MulticastRequest value) {
64 MulticastRouteServiceOuterClass.MulticastRoute route = value.getRoute();
65
66 switch (value.getOperation()) {
67 case ADD_ROUTE:
68 multicastRouteService.add(
69 new McastRoute(IpAddress.valueOf(route.getSource()),
70 IpAddress.valueOf(route.getGroup()),
71 McastRoute.Type.STATIC));
72 break;
73 case ADD_SOURCE:
74 break;
75 case ADD_SINK:
76 break;
77 case REMOVE_ROUTE:
78 break;
79 case REMOVE_SOURCE:
80 break;
81 case REMOVE_SINK:
82 break;
83 case UNRECOGNIZED:
84 default:
85 break;
86 }
87
88 responseObserver.onNext(MulticastRouteServiceOuterClass.MulticastReply.newBuilder().build());
89 }
90
91 @Override
92 public void onError(Throwable t) {
93 log.warn("Error receiving multicast route", t);
94 }
95
96 @Override
97 public void onCompleted() {
98 // When the client closes their stream, we'll close ours too
99 responseObserver.onCompleted();
100 }
101 }
102}