HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 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; |
| 39 | import io.grpc.netty.NegotiationType; |
| 40 | import io.grpc.netty.NettyChannelBuilder; |
| 41 | |
| 42 | |
| 43 | // gRPC Client side |
| 44 | /** |
| 45 | * RemoteServiceContextProvider based on gRPC. |
| 46 | */ |
| 47 | @Component(immediate = true) |
| 48 | public 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 Yuta | 6381a24 | 2016-03-13 23:29:10 -0700 | [diff] [blame] | 61 | private final Map<URI, ManagedChannel> channels = new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 62 | |
| 63 | private RemoteServiceContextProviderService providerService; |
| 64 | |
| 65 | |
| 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 |
| 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 Yuta | 6381a24 | 2016-03-13 23:29:10 -0700 | [diff] [blame] | 114 | int port = GrpcRemoteServiceServer.DEFAULT_LISTEN_PORT; |
| 115 | if (uri.getPort() != -1) { |
| 116 | port = uri.getPort(); |
| 117 | } |
| 118 | return NettyChannelBuilder.forAddress(uri.getHost(), port) |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 119 | .negotiationType(NegotiationType.PLAINTEXT) |
| 120 | .build(); |
| 121 | } |
| 122 | |
| 123 | } |