Jonathan Hart | 097c8f5 | 2016-06-09 18:08:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.incubator.rpc.nb.impl; |
| 18 | |
| 19 | import io.grpc.Server; |
| 20 | import io.grpc.netty.NettyServerBuilder; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onosproject.incubator.rpc.nb.mcast.MulticastRouteGrpcService; |
| 27 | import org.onosproject.net.mcast.MulticastRouteService; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | |
| 33 | /** |
| 34 | * gRPC server for northbound APIs. |
| 35 | */ |
| 36 | @Component(immediate = true) |
| 37 | public class GrpcServer { |
| 38 | |
| 39 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 40 | |
| 41 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 42 | 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 | } |