blob: 168e94b29fba2d8d69d59941e1359b2bb5010898 [file] [log] [blame]
Yoonseon Hanea8ac722017-05-24 17:25:01 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yoonseon Hanea8ac722017-05-24 17:25:01 -07003 *
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
17package org.onosproject.incubator.net.virtual.impl;
18
19import com.google.common.collect.Sets;
20import com.google.common.util.concurrent.Futures;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.junit.TestUtils;
25import org.onlab.osgi.ServiceDirectory;
26import org.onlab.osgi.TestServiceDirectory;
27import org.onlab.packet.IpAddress;
28import org.onosproject.cluster.ClusterService;
29import org.onosproject.cluster.ClusterServiceAdapter;
30import org.onosproject.cluster.ControllerNode;
31import org.onosproject.cluster.DefaultControllerNode;
32import org.onosproject.cluster.NodeId;
33import org.onosproject.common.event.impl.TestEventDispatcher;
34import org.onosproject.core.CoreService;
35import org.onosproject.event.EventDeliveryService;
36import org.onosproject.incubator.net.virtual.TenantId;
37import org.onosproject.incubator.net.virtual.VirtualNetwork;
38import org.onosproject.incubator.net.virtual.VirtualNetworkMastershipStore;
39import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
40import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
41import org.onosproject.incubator.store.virtual.impl.SimpleVirtualMastershipStore;
42import org.onosproject.mastership.MastershipService;
43import org.onosproject.mastership.MastershipTermService;
44import org.onosproject.net.DeviceId;
45import org.onosproject.net.NetTestTools;
46import org.onosproject.store.service.TestStorageService;
47
48import java.util.HashMap;
49import java.util.Map;
50import java.util.Set;
51
52import static org.junit.Assert.assertEquals;
53import static org.junit.Assert.assertNull;
54import static org.onosproject.net.MastershipRole.MASTER;
55import static org.onosproject.net.MastershipRole.STANDBY;
56import static org.onosproject.net.MastershipRole.NONE;
57
58public class VirtualNetworkMastershipManagerTest {
59
60 private static final NodeId NID_LOCAL = new NodeId("local");
61 private static final NodeId NID_OTHER = new NodeId("foo");
62 private static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1");
63
64 private static final TenantId TID = TenantId.tenantId("1");
65
66 private static final DeviceId VDID1 = DeviceId.deviceId("foo:vd1");
67 private static final DeviceId VDID2 = DeviceId.deviceId("foo:vd2");
68 private static final DeviceId VDID3 = DeviceId.deviceId("foo:vd3");
69 private static final DeviceId VDID4 = DeviceId.deviceId("foo:vd4");
70
71 private static final NodeId NID1 = NodeId.nodeId("n1");
72 private static final NodeId NID2 = NodeId.nodeId("n2");
73 private static final NodeId NID3 = NodeId.nodeId("n3");
74 private static final NodeId NID4 = NodeId.nodeId("n4");
75
76 private static final ControllerNode CNODE1 =
77 new DefaultControllerNode(NID1, IpAddress.valueOf("127.0.1.1"));
78 private static final ControllerNode CNODE2 =
79 new DefaultControllerNode(NID2, IpAddress.valueOf("127.0.1.2"));
80 private static final ControllerNode CNODE3 =
81 new DefaultControllerNode(NID3, IpAddress.valueOf("127.0.1.3"));
82 private static final ControllerNode CNODE4 =
83 new DefaultControllerNode(NID4, IpAddress.valueOf("127.0.1.4"));
84
85 private VirtualNetworkManager manager;
86 private DistributedVirtualNetworkStore virtualNetworkManagerStore;
87
88 private VirtualNetworkMastershipManager mastershipMgr1;
89 private VirtualNetworkMastershipManager mastershipMgr2;
90 protected MastershipService service;
91 private TestClusterService testClusterService;
92 private EventDeliveryService eventDeliveryService;
93
94 private VirtualNetwork vnet1;
95 private VirtualNetwork vnet2;
96
97 @Before
98 public void setUp() throws Exception {
99 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
100
101 CoreService coreService = new TestCoreService();
102 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
103 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
104 virtualNetworkManagerStore.activate();
105
106 manager = new VirtualNetworkManager();
107 manager.store = virtualNetworkManagerStore;
108 TestUtils.setField(manager, "coreService", coreService);
109
110 eventDeliveryService = new TestEventDispatcher();
111 NetTestTools.injectEventDispatcher(manager, eventDeliveryService);
112
113 SimpleVirtualMastershipStore store = new SimpleVirtualMastershipStore();
114 TestUtils.setField(store, "coreService", coreService);
115 store.activate();
116
117 testClusterService = new TestClusterService();
118
119 ServiceDirectory testDirectory = new TestServiceDirectory()
120 .add(VirtualNetworkStore.class, virtualNetworkManagerStore)
121 .add(CoreService.class, coreService)
122 .add(EventDeliveryService.class, eventDeliveryService)
123 .add(ClusterService.class, testClusterService)
124 .add(VirtualNetworkMastershipStore.class, store);
125 TestUtils.setField(manager, "serviceDirectory", testDirectory);
126
127 manager.activate();
128
129 createVnets();
130
131 mastershipMgr1 = new VirtualNetworkMastershipManager(manager, vnet1.id());
132 mastershipMgr2 = new VirtualNetworkMastershipManager(manager, vnet2.id());
133 service = mastershipMgr1;
134 }
135
136 private void createVnets() {
137 manager.registerTenantId(TID);
138
139 vnet1 = manager.createVirtualNetwork(TID);
140 manager.createVirtualDevice(vnet1.id(), VDID1);
141 manager.createVirtualDevice(vnet1.id(), VDID2);
142
143 vnet2 = manager.createVirtualNetwork(TID);
144 manager.createVirtualDevice(vnet2.id(), VDID3);
145 manager.createVirtualDevice(vnet2.id(), VDID4);
146 }
147
148 @After
149 public void tearDown() {
150 manager.deactivate();
151 virtualNetworkManagerStore.deactivate();
152 }
153
154 @Test
155 public void setRole() {
156 mastershipMgr1.setRole(NID_OTHER, VDID1, MASTER);
157 assertEquals("wrong local role:", NONE, mastershipMgr1.getLocalRole(VDID1));
158 assertEquals("wrong obtained role:", STANDBY, Futures.getUnchecked(mastershipMgr1.requestRoleFor(VDID1)));
159
160 //set to master
161 mastershipMgr1.setRole(NID_LOCAL, VDID1, MASTER);
162 assertEquals("wrong local role:", MASTER, mastershipMgr1.getLocalRole(VDID1));
163 }
164
165 @Test
166 public void relinquishMastership() {
167 //no backups - should just turn to NONE for device.
168 mastershipMgr1.setRole(NID_LOCAL, VDID1, MASTER);
169 assertEquals("wrong role:", MASTER, mastershipMgr1.getLocalRole(VDID1));
170 mastershipMgr1.relinquishMastership(VDID1);
171 assertNull("wrong master:", mastershipMgr1.getMasterFor(VDID2));
172 assertEquals("wrong role:", NONE, mastershipMgr1.getLocalRole(VDID1));
173
174 //not master, nothing should happen
175 mastershipMgr1.setRole(NID_LOCAL, VDID2, NONE);
176 mastershipMgr1.relinquishMastership(VDID2);
177 assertNull("wrong role:", mastershipMgr1.getMasterFor(VDID2));
178
179 //provide NID_OTHER as backup and relinquish
180 mastershipMgr1.setRole(NID_LOCAL, VDID1, MASTER);
181 assertEquals("wrong master:", NID_LOCAL, mastershipMgr1.getMasterFor(VDID1));
182 mastershipMgr1.setRole(NID_OTHER, VDID1, STANDBY);
183 mastershipMgr1.relinquishMastership(VDID1);
184 assertEquals("wrong master:", NID_OTHER, mastershipMgr1.getMasterFor(VDID1));
185 }
186
187 @Test
188 public void requestRoleFor() {
189 mastershipMgr1.setRole(NID_LOCAL, VDID1, MASTER);
190 mastershipMgr1.setRole(NID_OTHER, VDID2, MASTER);
191
192 //local should be master for one but standby for other
193 assertEquals("wrong role:", MASTER, Futures.getUnchecked(mastershipMgr1.requestRoleFor(VDID1)));
194 assertEquals("wrong role:", STANDBY, Futures.getUnchecked(mastershipMgr1.requestRoleFor(VDID2)));
195 }
196
197 @Test
198 public void getMasterFor() {
199 mastershipMgr1.setRole(NID_LOCAL, VDID1, MASTER);
200 mastershipMgr1.setRole(NID_OTHER, VDID2, MASTER);
201 assertEquals("wrong master:", NID_LOCAL, mastershipMgr1.getMasterFor(VDID1));
202 assertEquals("wrong master:", NID_OTHER, mastershipMgr1.getMasterFor(VDID2));
203
204 //have NID_OTHER hand over VDID2 to NID_LOCAL
205 mastershipMgr1.setRole(NID_LOCAL, VDID2, MASTER);
206 assertEquals("wrong master:", NID_LOCAL, mastershipMgr1.getMasterFor(VDID2));
207 }
208
209 @Test
210 public void getDevicesOf() {
211 mastershipMgr1.setRole(NID_LOCAL, VDID1, MASTER);
212 mastershipMgr1.setRole(NID_LOCAL, VDID2, STANDBY);
213 assertEquals("should be one device:", 1, mastershipMgr1.getDevicesOf(NID_LOCAL).size());
214 //hand both devices to NID_LOCAL
215 mastershipMgr1.setRole(NID_LOCAL, VDID2, MASTER);
216 assertEquals("should be two devices:", 2, mastershipMgr1.getDevicesOf(NID_LOCAL).size());
217 }
218
219 @Test
220 public void termService() {
221 MastershipTermService ts = mastershipMgr1;
222
223 //term = 1 for both
224 mastershipMgr1.setRole(NID_LOCAL, VDID1, MASTER);
225 assertEquals("inconsistent term: ", 1,
226 ts.getMastershipTerm(VDID1).termNumber());
227
228 //hand devices to NID_LOCAL and back: term = 1 + 2
229 mastershipMgr1.setRole(NID_OTHER, VDID1, MASTER);
230 mastershipMgr1.setRole(NID_LOCAL, VDID1, MASTER);
231 assertEquals("inconsistent terms: ",
232 3, ts.getMastershipTerm(VDID1).termNumber());
233 }
234
235 @Test
236 public void balanceWithVnets() {
237
238 testClusterService.put(CNODE1, ControllerNode.State.ACTIVE);
239 testClusterService.put(CNODE2, ControllerNode.State.ACTIVE);
240
241 mastershipMgr1.setRole(NID_LOCAL, VDID1, MASTER);
242 mastershipMgr1.setRole(NID_LOCAL, VDID2, MASTER);
243 assertEquals("wrong local role:", MASTER, mastershipMgr1.getLocalRole(VDID1));
244 assertEquals("wrong local role:", MASTER, mastershipMgr1.getLocalRole(VDID2));
245 assertEquals("wrong master:", NID_LOCAL, mastershipMgr1.getMasterFor(VDID1));
246 assertEquals("wrong master:", NID_LOCAL, mastershipMgr1.getMasterFor(VDID2));
247
248 //do balancing according to vnet Id.
249 mastershipMgr1.balanceRoles();
250 assertEquals("wrong master:", NID1, mastershipMgr1.getMasterFor(VDID1));
251 assertEquals("wrong master:", NID1, mastershipMgr1.getMasterFor(VDID2));
252
253 mastershipMgr2.setRole(NID_LOCAL, VDID3, MASTER);
254 mastershipMgr2.setRole(NID_LOCAL, VDID4, MASTER);
255 assertEquals("wrong local role:", MASTER, mastershipMgr2.getLocalRole(VDID3));
256 assertEquals("wrong local role:", MASTER, mastershipMgr2.getLocalRole(VDID4));
257 assertEquals("wrong master:", NID_LOCAL, mastershipMgr2.getMasterFor(VDID3));
258 assertEquals("wrong master:", NID_LOCAL, mastershipMgr2.getMasterFor(VDID4));
259
260 //do balancing according to vnet Id.
261 mastershipMgr2.balanceRoles();
262 assertEquals("wrong master:", NID2, mastershipMgr2.getMasterFor(VDID3));
263 assertEquals("wrong master:", NID2, mastershipMgr2.getMasterFor(VDID4));
264
265 // make N1 inactive
266 testClusterService.put(CNODE1, ControllerNode.State.INACTIVE);
267 mastershipMgr1.balanceRoles();
268 assertEquals("wrong master:", NID2, mastershipMgr1.getMasterFor(VDID1));
269 assertEquals("wrong master:", NID2, mastershipMgr1.getMasterFor(VDID2));
270 }
271
272 private final class TestClusterService extends ClusterServiceAdapter {
273
274 final Map<NodeId, ControllerNode> nodes = new HashMap<>();
275 final Map<NodeId, ControllerNode.State> nodeStates = new HashMap<>();
276
277 ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
278
279 @Override
280 public ControllerNode getLocalNode() {
281 return local;
282 }
283
284 @Override
285 public Set<ControllerNode> getNodes() {
286 return Sets.newHashSet(nodes.values());
287 }
288
289 @Override
290 public ControllerNode getNode(NodeId nodeId) {
291 return nodes.get(nodeId);
292 }
293
294 @Override
295 public ControllerNode.State getState(NodeId nodeId) {
296 return nodeStates.get(nodeId);
297 }
298
299 public void put(ControllerNode cn, ControllerNode.State state) {
300 nodes.put(cn.id(), cn);
301 nodeStates.put(cn.id(), state);
302 }
303 }
304
305 private final class TestSimpleMastershipStore extends SimpleVirtualMastershipStore
306 implements VirtualNetworkMastershipStore {
307
308 public TestSimpleMastershipStore(ClusterService clusterService) {
309 super.clusterService = clusterService;
310 }
311 }
312}