blob: 920f53da3a54f04760f4c725d111d0f35419416c [file] [log] [blame]
Jordan Halterman95feda02018-07-02 17:40:31 -07001/*
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 */
16package org.onosproject.proxytest;
17
Jordan Halterman95feda02018-07-02 17:40:31 -070018import org.onosproject.cluster.NodeId;
19import org.onosproject.cluster.ProxyFactory;
20import org.onosproject.cluster.ProxyService;
21import org.onosproject.core.CoreService;
22import org.onosproject.mastership.MastershipProxyFactory;
23import org.onosproject.mastership.MastershipProxyService;
24import org.onosproject.net.DeviceId;
25import org.onosproject.store.serializers.KryoNamespaces;
26import org.onosproject.store.service.Serializer;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070027import org.osgi.service.component.annotations.Activate;
28import org.osgi.service.component.annotations.Component;
29import org.osgi.service.component.annotations.Deactivate;
30import org.osgi.service.component.annotations.Reference;
31import org.osgi.service.component.annotations.ReferenceCardinality;
Jordan Halterman95feda02018-07-02 17:40:31 -070032import org.slf4j.Logger;
33
34import static org.slf4j.LoggerFactory.getLogger;
35
36/**
37 * Proxy test application.
38 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Component(immediate = true, service = ProxyTest.class)
Jordan Halterman95feda02018-07-02 17:40:31 -070040public 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 Milkeyd84f89b2018-08-17 14:54:17 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman95feda02018-07-02 17:40:31 -070049 protected CoreService coreService;
50
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman95feda02018-07-02 17:40:31 -070052 protected ProxyService proxyService;
53
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman95feda02018-07-02 17:40:31 -070055 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}