blob: 40902f24c7836628b69928f438e6bfd58c39d878 [file] [log] [blame]
Ayaka Koshibe406d0102014-09-24 16:08:12 -07001package org.onlab.onos.cluster.impl;
2
3import java.util.Set;
4
5import org.junit.After;
6import org.junit.Before;
7import org.junit.Test;
8import org.onlab.onos.cluster.ClusterEventListener;
9import org.onlab.onos.cluster.ClusterService;
10import org.onlab.onos.cluster.ControllerNode;
11import org.onlab.onos.cluster.ControllerNode.State;
12import org.onlab.onos.cluster.DefaultControllerNode;
13import org.onlab.onos.cluster.MastershipService;
14import org.onlab.onos.cluster.NodeId;
15import org.onlab.onos.event.impl.TestEventDispatcher;
16import org.onlab.onos.net.DeviceId;
17import org.onlab.onos.net.trivial.impl.SimpleMastershipStore;
18import org.onlab.packet.IpPrefix;
19
20import static org.junit.Assert.assertEquals;
21import static org.onlab.onos.net.MastershipRole.*;
22
23/**
24 * Test codifying the mastership service contracts.
25 */
26public class MastershipManagerTest {
27
28 private static final NodeId NID_LOCAL = new NodeId("local");
29 private static final NodeId NID_OTHER = new NodeId("foo");
30 private static final IpPrefix LOCALHOST = IpPrefix.valueOf("127.0.0.1");
31 private static final DeviceId DEV_MASTER = DeviceId.deviceId("of:1");
32 private static final DeviceId DEV_OTHER = DeviceId.deviceId("of:2");
33
34 private MastershipManager mgr;
35 protected MastershipService service;
36
37 @Before
38 public void setUp() {
39 mgr = new MastershipManager();
40 service = mgr;
41 mgr.store = new SimpleMastershipStore();
42 mgr.eventDispatcher = new TestEventDispatcher();
43 mgr.clusterService = new TestClusterService();
44 mgr.activate();
45 }
46
47 @After
48 public void tearDown() {
49 mgr.deactivate();
50 mgr.clusterService = null;
51 mgr.eventDispatcher = null;
52 mgr.store = null;
53 }
54
55 @Test
56 public void setRole() {
57 mgr.setRole(NID_OTHER, DEV_MASTER, MASTER);
58 assertEquals("wrong local role:", STANDBY, mgr.getLocalRole(DEV_MASTER));
59
60 //set to master
61 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
62 assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DEV_MASTER));
63 }
64
65 @Test
66 public void relinquishMastership() {
67 //TODO
68 }
69
70 @Test
71 public void requestRoleFor() {
72 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
73 mgr.setRole(NID_OTHER, DEV_OTHER, MASTER);
74
75 //local should be master for one but standby for other
76 assertEquals("wrong role:", MASTER, mgr.requestRoleFor(DEV_MASTER));
77 assertEquals("wrong role:", STANDBY, mgr.requestRoleFor(DEV_OTHER));
78 }
79
80 @Test
81 public void getMasterFor() {
82 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
83 mgr.setRole(NID_OTHER, DEV_OTHER, MASTER);
84 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER));
85 assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_OTHER));
86
87 //have NID_OTHER hand over DEV_OTHER to NID_LOCAL
88 mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER);
89 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_OTHER));
90 }
91
92 @Test
93 public void getDevicesOf() {
94 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
95 mgr.setRole(NID_LOCAL, DEV_OTHER, STANDBY);
96 assertEquals("should be one device:", 1, mgr.getDevicesOf(NID_LOCAL).size());
97
98 //hand both devices to NID_LOCAL
99 mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER);
100 assertEquals("should be two devices:", 2, mgr.getDevicesOf(NID_LOCAL).size());
101 }
102
103 private final class TestClusterService implements ClusterService {
104
105 ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
106
107 @Override
108 public ControllerNode getLocalNode() {
109 return local;
110 }
111
112 @Override
113 public Set<ControllerNode> getNodes() {
114 return null;
115 }
116
117 @Override
118 public ControllerNode getNode(NodeId nodeId) {
119 return null;
120 }
121
122 @Override
123 public State getState(NodeId nodeId) {
124 return null;
125 }
126
127 @Override
128 public void addListener(ClusterEventListener listener) {
129 }
130
131 @Override
132 public void removeListener(ClusterEventListener listener) {
133 }
134
135 }
136}