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