1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.rpc.grpc;
import static com.google.common.base.Preconditions.checkArgument;
import java.net.URI;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onosproject.incubator.rpc.RemoteServiceContext;
import org.onosproject.incubator.rpc.RemoteServiceContextProvider;
import org.onosproject.incubator.rpc.RemoteServiceContextProviderService;
import org.onosproject.incubator.rpc.RemoteServiceProviderRegistry;
import org.onosproject.net.provider.ProviderId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.grpc.ManagedChannel;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
// gRPC Client side
/**
* RemoteServiceContextProvider based on gRPC.
*/
@Component(immediate = true)
public class GrpcRemoteServiceProvider implements RemoteServiceContextProvider {
public static final String GRPC_SCHEME = "grpc";
public static final String RPC_PROVIDER_NAME = "org.onosproject.rpc.provider.grpc";
private static final ProviderId PID = new ProviderId(GRPC_SCHEME, RPC_PROVIDER_NAME);
private final Logger log = LoggerFactory.getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected RemoteServiceProviderRegistry rpcRegistry;
private Map<URI, ManagedChannel> channels = new ConcurrentHashMap<>();
private RemoteServiceContextProviderService providerService;
@Activate
protected void activate() {
providerService = rpcRegistry.register(this);
// FIXME remove me. test code to see if gRPC loads in karaf
//getChannel(URI.create("grpc://localhost:8080"));
log.info("Started");
}
@Deactivate
protected void deactivate() {
rpcRegistry.unregister(this);
// shutdown all channels
channels.values().stream()
.forEach(ManagedChannel::shutdown);
// Should we wait for shutdown? How?
channels.clear();
log.info("Stopped");
}
@Override
public ProviderId id() {
return PID;
}
@Override
public RemoteServiceContext get(URI uri) {
// Create gRPC client
return new GrpcRemoteServiceContext(getChannel(uri));
}
private ManagedChannel getChannel(URI uri) {
checkArgument(Objects.equals(GRPC_SCHEME, uri.getScheme()),
"Invalid URI scheme: %s", uri.getScheme());
return channels.compute(uri, (u, ch) -> {
if (ch != null && !ch.isShutdown()) {
return ch;
} else {
return createChannel(u);
}
});
}
private ManagedChannel createChannel(URI uri) {
log.debug("Creating channel for {}", uri);
return NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort())
.negotiationType(NegotiationType.PLAINTEXT)
.build();
}
}
|