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.checkNotNull; |
| 19 | |
| 20 | import java.util.Map; |
| 21 | import java.util.NoSuchElementException; |
| 22 | import java.util.concurrent.ConcurrentHashMap; |
| 23 | |
| 24 | import org.onosproject.incubator.rpc.RemoteServiceContext; |
| 25 | import org.onosproject.net.device.DeviceProviderRegistry; |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 26 | import org.onosproject.net.link.LinkProviderRegistry; |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import com.google.common.base.MoreObjects; |
| 31 | |
| 32 | import io.grpc.ManagedChannel; |
| 33 | |
| 34 | // gRPC Client side |
| 35 | // Probably there should be plug-in mechanism in the future. |
| 36 | /** |
| 37 | * RemoteServiceContext based on gRPC. |
| 38 | * |
| 39 | * <p> |
| 40 | * Currently it supports {@link DeviceProviderRegistry}. |
| 41 | */ |
| 42 | public class GrpcRemoteServiceContext implements RemoteServiceContext { |
| 43 | |
| 44 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 45 | |
| 46 | private final Map<Class<? extends Object>, Object> services = new ConcurrentHashMap<>(); |
| 47 | |
| 48 | private final ManagedChannel channel; |
| 49 | |
| 50 | public GrpcRemoteServiceContext(ManagedChannel channel) { |
| 51 | this.channel = checkNotNull(channel); |
| 52 | services.put(DeviceProviderRegistry.class, new DeviceProviderRegistryClientProxy(channel)); |
HIGUCHI Yuta | 7c1583c | 2015-12-03 23:08:54 -0800 | [diff] [blame] | 53 | services.put(LinkProviderRegistry.class, new LinkProviderRegistryClientProxy(channel)); |
HIGUCHI Yuta | 15653fd | 2015-11-09 11:05:09 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | @Override |
| 58 | public <T> T get(Class<T> serviceClass) { |
| 59 | @SuppressWarnings("unchecked") |
| 60 | T service = (T) services.get(serviceClass); |
| 61 | if (service != null) { |
| 62 | return service; |
| 63 | } |
| 64 | log.error("{} not supported", serviceClass); |
| 65 | throw new NoSuchElementException(serviceClass.getTypeName() + " not supported"); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | @Override |
| 70 | public String toString() { |
| 71 | return MoreObjects.toStringHelper(this) |
| 72 | .add("services", services.keySet()) |
| 73 | .add("channel", channel.authority()) |
| 74 | .toString(); |
| 75 | } |
| 76 | |
| 77 | } |