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 java.util.Map; |
| 19 | |
| 20 | import org.onosproject.net.device.DeviceProvider; |
| 21 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 22 | import org.onosproject.net.device.DeviceProviderService; |
| 23 | import org.onosproject.net.provider.AbstractProviderRegistry; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import com.google.common.collect.Maps; |
| 28 | |
| 29 | import io.grpc.Channel; |
| 30 | import 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 | */ |
| 38 | final 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 Yuta | 6381a24 | 2016-03-13 23:29:10 -0700 | [diff] [blame] | 58 | log.debug("Created DeviceProviderServiceClientProxy {}", pService); |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 59 | |
| 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 Yuta | 6381a24 | 2016-03-13 23:29:10 -0700 | [diff] [blame] | 71 | log.debug("Unregistering DeviceProviderServiceClientProxy {}", pService); |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 72 | super.unregister(provider); |
| 73 | if (pService != null) { |
| 74 | pService.shutdown(); |
| 75 | } |
| 76 | } |
| 77 | } |