blob: 44674e36d92dd70c00c902da943ac37da0f7df19 [file] [log] [blame]
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Map;
21import java.util.NoSuchElementException;
22import java.util.concurrent.ConcurrentHashMap;
23
24import org.onosproject.incubator.rpc.RemoteServiceContext;
25import org.onosproject.net.device.DeviceProviderRegistry;
HIGUCHI Yuta7c1583c2015-12-03 23:08:54 -080026import org.onosproject.net.link.LinkProviderRegistry;
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.google.common.base.MoreObjects;
31
32import 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 */
42public 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 Yuta7c1583c2015-12-03 23:08:54 -080053 services.put(LinkProviderRegistry.class, new LinkProviderRegistryClientProxy(channel));
HIGUCHI Yuta15653fd2015-11-09 11:05:09 -080054 }
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}