blob: 766dd1a5c8f25b1874f91f7e8d36c352d06d12b6 [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 java.util.Map;
19
20import org.onosproject.net.device.DeviceProvider;
21import org.onosproject.net.device.DeviceProviderRegistry;
22import org.onosproject.net.device.DeviceProviderService;
23import org.onosproject.net.provider.AbstractProviderRegistry;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import com.google.common.collect.Maps;
28
29import io.grpc.Channel;
30import io.grpc.ManagedChannel;
31
32// gRPC Client side
33/**
34 * Proxy object to handle DeviceProviderRegistry calls.
35 *
36 * RPC wise, this will start/stop bidirectional streaming service sessions.
37 */
38final class DeviceProviderRegistryClientProxy
39 extends AbstractProviderRegistry<DeviceProvider, DeviceProviderService>
40 implements DeviceProviderRegistry {
41
42 private final Logger log = LoggerFactory.getLogger(getClass());
43
44 private final Channel channel;
45
46 private final Map<DeviceProvider, DeviceProviderServiceClientProxy> pServices;
47
48 DeviceProviderRegistryClientProxy(ManagedChannel channel) {
49 this.channel = channel;
50 pServices = Maps.newIdentityHashMap();
51 }
52
53 @Override
54 protected synchronized DeviceProviderService createProviderService(DeviceProvider provider) {
55
56 // Create session
57 DeviceProviderServiceClientProxy pService = new DeviceProviderServiceClientProxy(provider, channel);
HIGUCHI Yuta6381a242016-03-13 23:29:10 -070058 log.debug("Created DeviceProviderServiceClientProxy {}", pService);
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080059
60 DeviceProviderServiceClientProxy old = pServices.put(provider, pService);
61 if (old != null) {
62 // sanity check, can go away
63 log.warn("Duplicate registration detected for {}", provider.id());
64 }
65 return pService;
66 }
67
68 @Override
69 public synchronized void unregister(DeviceProvider provider) {
70 DeviceProviderServiceClientProxy pService = pServices.remove(provider);
HIGUCHI Yuta6381a242016-03-13 23:29:10 -070071 log.debug("Unregistering DeviceProviderServiceClientProxy {}", pService);
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080072 super.unregister(provider);
73 if (pService != null) {
74 pService.shutdown();
75 }
76 }
77}