blob: 955a6576b0926b8dd44009eccd7c93c0ed61e778 [file] [log] [blame]
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -08003 *
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 */
16package org.onosproject.incubator.rpc.grpc;
17
18import static com.google.common.base.Preconditions.checkArgument;
19
20import java.net.URI;
21import java.util.Map;
22import java.util.Objects;
23import java.util.concurrent.ConcurrentHashMap;
24
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.onosproject.incubator.rpc.RemoteServiceContext;
31import org.onosproject.incubator.rpc.RemoteServiceContextProvider;
32import org.onosproject.incubator.rpc.RemoteServiceContextProviderService;
33import org.onosproject.incubator.rpc.RemoteServiceProviderRegistry;
34import org.onosproject.net.provider.ProviderId;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import io.grpc.ManagedChannel;
39import io.grpc.netty.NegotiationType;
40import io.grpc.netty.NettyChannelBuilder;
41
42
43// gRPC Client side
44/**
45 * RemoteServiceContextProvider based on gRPC.
46 */
47@Component(immediate = true)
48public class GrpcRemoteServiceProvider implements RemoteServiceContextProvider {
49
50 public static final String GRPC_SCHEME = "grpc";
51
52 public static final String RPC_PROVIDER_NAME = "org.onosproject.rpc.provider.grpc";
53
54 private static final ProviderId PID = new ProviderId(GRPC_SCHEME, RPC_PROVIDER_NAME);
55
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected RemoteServiceProviderRegistry rpcRegistry;
60
HIGUCHI Yuta6381a242016-03-13 23:29:10 -070061 private final Map<URI, ManagedChannel> channels = new ConcurrentHashMap<>();
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080062
63 private RemoteServiceContextProviderService providerService;
64
65
66 @Activate
67 protected void activate() {
68 providerService = rpcRegistry.register(this);
69
HIGUCHI Yuta6381a242016-03-13 23:29:10 -070070 // Uncomment to test if gRPC can be loaded in karaf
71 //getChannel(URI.create("grpc://localhost:11984"));
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080072
73 log.info("Started");
74 }
75
76 @Deactivate
77 protected void deactivate() {
78 rpcRegistry.unregister(this);
79
80 // shutdown all channels
81 channels.values().stream()
82 .forEach(ManagedChannel::shutdown);
83 // Should we wait for shutdown? How?
84 channels.clear();
85 log.info("Stopped");
86 }
87
88 @Override
89 public ProviderId id() {
90 return PID;
91 }
92
93 @Override
94 public RemoteServiceContext get(URI uri) {
95 // Create gRPC client
96 return new GrpcRemoteServiceContext(getChannel(uri));
97 }
98
99 private ManagedChannel getChannel(URI uri) {
100 checkArgument(Objects.equals(GRPC_SCHEME, uri.getScheme()),
101 "Invalid URI scheme: %s", uri.getScheme());
102
103 return channels.compute(uri, (u, ch) -> {
104 if (ch != null && !ch.isShutdown()) {
105 return ch;
106 } else {
107 return createChannel(u);
108 }
109 });
110 }
111
112 private ManagedChannel createChannel(URI uri) {
113 log.debug("Creating channel for {}", uri);
HIGUCHI Yuta6381a242016-03-13 23:29:10 -0700114 int port = GrpcRemoteServiceServer.DEFAULT_LISTEN_PORT;
115 if (uri.getPort() != -1) {
116 port = uri.getPort();
117 }
118 return NettyChannelBuilder.forAddress(uri.getHost(), port)
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800119 .negotiationType(NegotiationType.PLAINTEXT)
120 .build();
121 }
122
123}