HIGUCHI Yuta | 279b8d1 | 2015-11-04 10:58:06 -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 | 279b8d1 | 2015-11-04 10:58:06 -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.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import java.net.URI; |
| 21 | import java.util.Map; |
| 22 | import java.util.Objects; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Component; |
| 27 | import org.osgi.service.component.annotations.Deactivate; |
| 28 | import org.osgi.service.component.annotations.Reference; |
| 29 | import org.osgi.service.component.annotations.ReferenceCardinality; |
HIGUCHI Yuta | 279b8d1 | 2015-11-04 10:58:06 -0800 | [diff] [blame] | 30 | import org.onlab.osgi.DefaultServiceDirectory; |
| 31 | import org.onlab.osgi.ServiceDirectory; |
| 32 | import org.onosproject.incubator.rpc.RemoteServiceContext; |
| 33 | import org.onosproject.incubator.rpc.RemoteServiceContextProvider; |
| 34 | import org.onosproject.incubator.rpc.RemoteServiceContextProviderService; |
| 35 | import org.onosproject.incubator.rpc.RemoteServiceProviderRegistry; |
| 36 | import org.onosproject.net.provider.ProviderId; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import 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) |
| 53 | public 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
HIGUCHI Yuta | 279b8d1 | 2015-11-04 10:58:06 -0800 | [diff] [blame] | 62 | 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 | } |