blob: 4ea8e50019826046a39790a7e5905f2534799023 [file] [log] [blame]
Jonathan Hart097c8f52016-06-09 18:08:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart097c8f52016-06-09 18:08:11 -07003 *
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.impl;
18
19import io.grpc.Server;
20import io.grpc.netty.NettyServerBuilder;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.osgi.service.component.annotations.Activate;
22import org.osgi.service.component.annotations.Component;
23import org.osgi.service.component.annotations.Deactivate;
24import org.osgi.service.component.annotations.Reference;
25import org.osgi.service.component.annotations.ReferenceCardinality;
Jonathan Hart097c8f52016-06-09 18:08:11 -070026import org.onosproject.incubator.rpc.nb.mcast.MulticastRouteGrpcService;
27import org.onosproject.net.mcast.MulticastRouteService;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import java.io.IOException;
32
33/**
34 * gRPC server for northbound APIs.
35 */
36@Component(immediate = true)
37public class GrpcServer {
38
39 private final Logger log = LoggerFactory.getLogger(getClass());
40
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart097c8f52016-06-09 18:08:11 -070042 protected MulticastRouteService multicastRouteService;
43
44 // TODO make configurable
45 private int port = 50051;
46
47 private Server server;
48
49 @Activate
50 public void activate() {
51 start();
52 log.info("Started");
53 }
54
55 @Deactivate
56 public void deactivate() {
57 stop();
58 log.info("Stopped");
59 }
60
61 private void start() {
62 try {
63 server = NettyServerBuilder.forPort(port)
64 .addService(new MulticastRouteGrpcService(multicastRouteService))
65 .build()
66 .start();
67 log.info("gRPC server started listening on " + port);
68 } catch (IOException e) {
69 log.error("Failed to start gRPC server", e);
70 }
71 }
72
73 private void stop() {
74 if (server != null) {
75 server.shutdown();
76 }
77 }
78}