HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 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 | package org.onosproject.incubator.rpc.grpc; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import java.net.URI; |
| 21 | import java.util.Map; |
| 22 | import java.util.Objects; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Component; |
| 27 | import org.osgi.service.component.annotations.Deactivate; |
| 28 | import org.osgi.service.component.annotations.Reference; |
| 29 | import org.osgi.service.component.annotations.ReferenceCardinality; |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 30 | import org.onosproject.incubator.rpc.RemoteServiceContext; |
| 31 | import org.onosproject.incubator.rpc.RemoteServiceContextProvider; |
| 32 | import org.onosproject.incubator.rpc.RemoteServiceContextProviderService; |
| 33 | import org.onosproject.incubator.rpc.RemoteServiceProviderRegistry; |
| 34 | import org.onosproject.net.provider.ProviderId; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import io.grpc.ManagedChannel; |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 39 | import io.grpc.internal.DnsNameResolverProvider; |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 40 | import io.grpc.netty.NegotiationType; |
| 41 | import io.grpc.netty.NettyChannelBuilder; |
| 42 | |
| 43 | |
| 44 | // gRPC Client side |
| 45 | /** |
| 46 | * RemoteServiceContextProvider based on gRPC. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | public 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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 60 | protected RemoteServiceProviderRegistry rpcRegistry; |
| 61 | |
HIGUCHI Yuta | 6381a24 | 2016-03-13 23:29:10 -0700 | [diff] [blame] | 62 | private final Map<URI, ManagedChannel> channels = new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 63 | |
| 64 | private RemoteServiceContextProviderService providerService; |
| 65 | |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 66 | @Activate |
| 67 | protected void activate() { |
| 68 | providerService = rpcRegistry.register(this); |
| 69 | |
HIGUCHI Yuta | 6381a24 | 2016-03-13 23:29:10 -0700 | [diff] [blame] | 70 | // Uncomment to test if gRPC can be loaded in karaf |
| 71 | //getChannel(URI.create("grpc://localhost:11984")); |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 72 | |
| 73 | log.info("Started"); |
| 74 | } |
| 75 | |
| 76 | @Deactivate |
| 77 | protected void deactivate() { |
| 78 | rpcRegistry.unregister(this); |
| 79 | |
| 80 | // shutdown all channels |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 81 | channels.values().forEach(ManagedChannel::shutdown); |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 82 | // 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 Yuta | 6381a24 | 2016-03-13 23:29:10 -0700 | [diff] [blame] | 113 | int port = GrpcRemoteServiceServer.DEFAULT_LISTEN_PORT; |
| 114 | if (uri.getPort() != -1) { |
| 115 | port = uri.getPort(); |
| 116 | } |
| 117 | return NettyChannelBuilder.forAddress(uri.getHost(), port) |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 118 | .negotiationType(NegotiationType.PLAINTEXT) |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 119 | // 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 Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 123 | .build(); |
| 124 | } |
| 125 | |
| 126 | } |