Jordan Halterman | 95feda0 | 2018-07-02 17:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.proxytest; |
| 17 | |
Jordan Halterman | 95feda0 | 2018-07-02 17:40:31 -0700 | [diff] [blame] | 18 | import org.onosproject.cluster.NodeId; |
| 19 | import org.onosproject.cluster.ProxyFactory; |
| 20 | import org.onosproject.cluster.ProxyService; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.mastership.MastershipProxyFactory; |
| 23 | import org.onosproject.mastership.MastershipProxyService; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.store.serializers.KryoNamespaces; |
| 26 | import org.onosproject.store.service.Serializer; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 27 | import org.osgi.service.component.annotations.Activate; |
| 28 | import org.osgi.service.component.annotations.Component; |
| 29 | import org.osgi.service.component.annotations.Deactivate; |
| 30 | import org.osgi.service.component.annotations.Reference; |
| 31 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jordan Halterman | 95feda0 | 2018-07-02 17:40:31 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Proxy test application. |
| 38 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | @Component(immediate = true, service = ProxyTest.class) |
Jordan Halterman | 95feda0 | 2018-07-02 17:40:31 -0700 | [diff] [blame] | 40 | public class ProxyTest { |
| 41 | |
| 42 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API); |
| 43 | |
| 44 | private final Logger log = getLogger(getClass()); |
| 45 | |
| 46 | private static final String APP_NAME = "org.onosproject.proxytest"; |
| 47 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 95feda0 | 2018-07-02 17:40:31 -0700 | [diff] [blame] | 49 | protected CoreService coreService; |
| 50 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 95feda0 | 2018-07-02 17:40:31 -0700 | [diff] [blame] | 52 | protected ProxyService proxyService; |
| 53 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 95feda0 | 2018-07-02 17:40:31 -0700 | [diff] [blame] | 55 | protected MastershipProxyService mastershipProxyService; |
| 56 | |
| 57 | private ProxyFactory<TestProxy> proxyFactory; |
| 58 | private MastershipProxyFactory<TestProxy> mastershipProxyFactory; |
| 59 | |
| 60 | @Activate |
| 61 | protected void activate() { |
| 62 | coreService.registerApplication(APP_NAME); |
| 63 | proxyService.registerProxyService(TestProxy.class, new TestProxyImpl(), SERIALIZER); |
| 64 | mastershipProxyService.registerProxyService(TestProxy.class, new TestProxyImpl(), SERIALIZER); |
| 65 | proxyFactory = proxyService.getProxyFactory(TestProxy.class, SERIALIZER); |
| 66 | mastershipProxyFactory = mastershipProxyService.getProxyFactory(TestProxy.class, SERIALIZER); |
| 67 | log.info("Started"); |
| 68 | } |
| 69 | |
| 70 | @Deactivate |
| 71 | protected void deactivate() { |
| 72 | proxyService.unregisterProxyService(TestProxy.class); |
| 73 | mastershipProxyService.unregisterProxyService(TestProxy.class); |
| 74 | log.info("Stopped"); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns a proxy for the given node. |
| 79 | * |
| 80 | * @param nodeId the node for which to return the proxy |
| 81 | * @return the proxy for the given node |
| 82 | */ |
| 83 | public TestProxy getProxyFor(NodeId nodeId) { |
| 84 | return proxyFactory.getProxyFor(nodeId); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns a mastership-based proxy for the given device. |
| 89 | * |
| 90 | * @param deviceId the device for which to return the proxy |
| 91 | * @return the proxy for the given device |
| 92 | */ |
| 93 | public TestProxy getProxyFor(DeviceId deviceId) { |
| 94 | return mastershipProxyFactory.getProxyFor(deviceId); |
| 95 | } |
| 96 | } |