blob: 9fe33f79edf0facff54baa5bf3410bb96efd1be8 [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;
Yuta HIGUCHI9efba1e2016-07-09 11:07:13 -070039import io.grpc.internal.DnsNameResolverProvider;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080040import io.grpc.netty.NegotiationType;
41import io.grpc.netty.NettyChannelBuilder;
42
43
44// gRPC Client side
45/**
46 * RemoteServiceContextProvider based on gRPC.
47 */
48@Component(immediate = true)
49public class GrpcRemoteServiceProvider implements RemoteServiceContextProvider {
50
51 public static final String GRPC_SCHEME = "grpc";
52
53 public static final String RPC_PROVIDER_NAME = "org.onosproject.rpc.provider.grpc";
54
55 private static final ProviderId PID = new ProviderId(GRPC_SCHEME, RPC_PROVIDER_NAME);
56
57 private final Logger log = LoggerFactory.getLogger(getClass());
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected RemoteServiceProviderRegistry rpcRegistry;
61
HIGUCHI Yuta6381a242016-03-13 23:29:10 -070062 private final Map<URI, ManagedChannel> channels = new ConcurrentHashMap<>();
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080063
64 private RemoteServiceContextProviderService providerService;
65
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080066 @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
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -070081 channels.values().forEach(ManagedChannel::shutdown);
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080082 // Should we wait for shutdown? How?
83 channels.clear();
84 log.info("Stopped");
85 }
86
87 @Override
88 public ProviderId id() {
89 return PID;
90 }
91
92 @Override
93 public RemoteServiceContext get(URI uri) {
94 // Create gRPC client
95 return new GrpcRemoteServiceContext(getChannel(uri));
96 }
97
98 private ManagedChannel getChannel(URI uri) {
99 checkArgument(Objects.equals(GRPC_SCHEME, uri.getScheme()),
100 "Invalid URI scheme: %s", uri.getScheme());
101
102 return channels.compute(uri, (u, ch) -> {
103 if (ch != null && !ch.isShutdown()) {
104 return ch;
105 } else {
106 return createChannel(u);
107 }
108 });
109 }
110
111 private ManagedChannel createChannel(URI uri) {
112 log.debug("Creating channel for {}", uri);
HIGUCHI Yuta6381a242016-03-13 23:29:10 -0700113 int port = GrpcRemoteServiceServer.DEFAULT_LISTEN_PORT;
114 if (uri.getPort() != -1) {
115 port = uri.getPort();
116 }
117 return NettyChannelBuilder.forAddress(uri.getHost(), port)
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800118 .negotiationType(NegotiationType.PLAINTEXT)
Yuta HIGUCHI9efba1e2016-07-09 11:07:13 -0700119 // TODO Not ideal fix, gRPC discovers name resolvers
120 // in the class path, but OSGi was preventing it.
121 // Manually specifying the default dns resolver for now.
122 .nameResolverFactory(new DnsNameResolverProvider())
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -0800123 .build();
124 }
125
126}