blob: 5ef0d203e2da12eec3647b6f53b95f01bd4b4efd [file] [log] [blame]
HIGUCHI Yuta279b8d12015-11-04 10:58:06 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
HIGUCHI Yuta279b8d12015-11-04 10:58:06 -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.impl;
17
18import static com.google.common.base.Preconditions.checkArgument;
19
20import java.net.URI;
21import java.util.Map;
22import java.util.Objects;
23import java.util.concurrent.ConcurrentHashMap;
24
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.osgi.service.component.annotations.Activate;
26import org.osgi.service.component.annotations.Component;
27import org.osgi.service.component.annotations.Deactivate;
28import org.osgi.service.component.annotations.Reference;
29import org.osgi.service.component.annotations.ReferenceCardinality;
HIGUCHI Yuta279b8d12015-11-04 10:58:06 -080030import org.onlab.osgi.DefaultServiceDirectory;
31import org.onlab.osgi.ServiceDirectory;
32import org.onosproject.incubator.rpc.RemoteServiceContext;
33import org.onosproject.incubator.rpc.RemoteServiceContextProvider;
34import org.onosproject.incubator.rpc.RemoteServiceContextProviderService;
35import org.onosproject.incubator.rpc.RemoteServiceProviderRegistry;
36import org.onosproject.net.provider.ProviderId;
37import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40import com.google.common.annotations.Beta;
41
42/**
43 * Sample implementation of RemoteServiceContextProvider.
44 *
45 * Scheme: "local", calling corresponding local service on request.
46 * Only expected for testing until real RPC implementation is ready.
47 *
48 * Note: This is expected to be removed or separated out as separate bundle
49 * once other RPC implementaion became available.
50 */
51@Beta
52@Component(immediate = true)
53public class LocalRemoteServiceProvider implements RemoteServiceContextProvider {
54
55 private final Logger log = LoggerFactory.getLogger(getClass());
56
57 private RemoteServiceContext theOne = new LocalServiceContext();
58
59 private static final ProviderId PID = new ProviderId("local", "org.onosproject.rpc.provider.local");
60
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta279b8d12015-11-04 10:58:06 -080062 protected RemoteServiceProviderRegistry rpcRegistry;
63
64 private final Map<Class<? extends Object>, Object> services = new ConcurrentHashMap<>();
65
66 private RemoteServiceContextProviderService providerService;
67
68 @Activate
69 protected void activate() {
70
71 services.put(SomeOtherService.class, new SomeOtherServiceImpl());
72
73 providerService = rpcRegistry.register(this);
74 log.info("Started");
75 }
76
77 @Deactivate
78 protected void deactivate() {
79 rpcRegistry.unregister(this);
80 log.info("Stopped");
81 }
82
83 @Override
84 public ProviderId id() {
85 return PID;
86 }
87
88 @Override
89 public RemoteServiceContext get(URI uri) {
90 checkArgument(Objects.equals(uri.getScheme(), "local"));
91 return theOne;
92 }
93
94 private final class LocalServiceContext implements RemoteServiceContext {
95
96 private final ServiceDirectory directory = new DefaultServiceDirectory();
97
98 @Override
99 public <T> T get(Class<T> serviceClass) {
100 @SuppressWarnings("unchecked")
101 T service = (T) services.get(serviceClass);
102 if (service != null) {
103 return service;
104 }
105 // look up OSGi services on this host.
106 // provided to unblock development depending on RPC.
107 return directory.get(serviceClass);
108 }
109 }
110
111 // Service provided by RPC can be non-OSGi Service
112 public static interface SomeOtherService {
113 String hello();
114 }
115
116 public static class SomeOtherServiceImpl implements SomeOtherService {
117
118 @Override
119 public String hello() {
120 return "Goodbye";
121 }
122 }
123
124}