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