blob: 3b62cdbd89dbe4cb182600ce5e442f79ded6685e [file] [log] [blame]
Jordan Haltermand04e3442018-07-02 17:40:07 -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.cluster.impl;
17
18import java.util.concurrent.CompletableFuture;
19import java.util.concurrent.atomic.AtomicInteger;
20
21import org.junit.Test;
22import org.onlab.packet.IpAddress;
23import org.onosproject.cluster.ClusterServiceAdapter;
24import org.onosproject.cluster.ControllerNode;
25import org.onosproject.cluster.DefaultControllerNode;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.mastership.MastershipProxyFactory;
28import org.onosproject.mastership.MastershipServiceAdapter;
29import org.onosproject.net.DeviceId;
30import org.onosproject.store.serializers.KryoNamespaces;
31import org.onosproject.store.service.Serializer;
32
33import static org.junit.Assert.assertEquals;
34
35/**
36 * Mastership proxy manager test.
37 */
38public class MastershipProxyManagerTest {
39 @Test
40 public void testProxyManager() throws Exception {
41 TestClusterCommunicationServiceFactory clusterCommunicatorFactory =
42 new TestClusterCommunicationServiceFactory();
43
44 NodeId a = NodeId.nodeId("a");
45 NodeId b = NodeId.nodeId("b");
46
47 DeviceId deviceId = DeviceId.deviceId("a");
48
49 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
50
51 ProxyInterfaceImpl proxyInterface1 = new ProxyInterfaceImpl();
52 MastershipProxyManager proxyManager1 = new MastershipProxyManager();
53 proxyManager1.clusterService = new ClusterServiceAdapter() {
54 @Override
55 public ControllerNode getLocalNode() {
56 return new DefaultControllerNode(a, IpAddress.valueOf(0));
57 }
58 };
59 proxyManager1.clusterCommunicator = clusterCommunicatorFactory.newCommunicationService(a);
60 proxyManager1.mastershipService = new MastershipServiceAdapter() {
61 @Override
62 public NodeId getMasterFor(DeviceId deviceId) {
63 return b;
64 }
65 };
66 proxyManager1.activate();
67 proxyManager1.registerProxyService(ProxyInterface.class, proxyInterface1, serializer);
68
69 ProxyInterfaceImpl proxyInterface2 = new ProxyInterfaceImpl();
70 MastershipProxyManager proxyManager2 = new MastershipProxyManager();
71 proxyManager2.clusterService = new ClusterServiceAdapter() {
72 @Override
73 public ControllerNode getLocalNode() {
74 return new DefaultControllerNode(b, IpAddress.valueOf(0));
75 }
76 };
77 proxyManager2.clusterCommunicator = clusterCommunicatorFactory.newCommunicationService(b);
78 proxyManager2.mastershipService = new MastershipServiceAdapter() {
79 @Override
80 public NodeId getMasterFor(DeviceId deviceId) {
81 return b;
82 }
83 };
84 proxyManager2.activate();
85 proxyManager2.registerProxyService(ProxyInterface.class, proxyInterface2, serializer);
86
87 MastershipProxyFactory<ProxyInterface> proxyFactory1 =
88 proxyManager1.getProxyFactory(ProxyInterface.class, serializer);
89 assertEquals("Hello world!", proxyFactory1.getProxyFor(deviceId).sync("Hello world!"));
90 assertEquals(1, proxyInterface2.syncCalls.get());
91 assertEquals("Hello world!", proxyFactory1.getProxyFor(deviceId).async("Hello world!").join());
92 assertEquals(1, proxyInterface2.asyncCalls.get());
93
94 MastershipProxyFactory<ProxyInterface> proxyFactory2 =
95 proxyManager2.getProxyFactory(ProxyInterface.class, serializer);
96 assertEquals("Hello world!", proxyFactory2.getProxyFor(deviceId).sync("Hello world!"));
97 assertEquals(2, proxyInterface2.syncCalls.get());
98 assertEquals("Hello world!", proxyFactory2.getProxyFor(deviceId).async("Hello world!").join());
99 assertEquals(2, proxyInterface2.asyncCalls.get());
100
101 proxyManager1.deactivate();
102 proxyManager2.deactivate();
103 }
104
105 interface ProxyInterface {
106 String sync(String arg);
107 CompletableFuture<String> async(String arg);
108 }
109
110 class ProxyInterfaceImpl implements ProxyInterface {
111 private final AtomicInteger syncCalls = new AtomicInteger();
112 private final AtomicInteger asyncCalls = new AtomicInteger();
113
114 @Override
115 public String sync(String arg) {
116 syncCalls.incrementAndGet();
117 return arg;
118 }
119
120 @Override
121 public CompletableFuture<String> async(String arg) {
122 asyncCalls.incrementAndGet();
123 return CompletableFuture.completedFuture(arg);
124 }
125 }
126}