blob: 28847c6b570cbed100f1616d38b042a34c28e9a7 [file] [log] [blame]
Charles Chand66d6712018-03-29 16:03:41 -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.segmentrouting;
17
18import com.google.common.collect.Sets;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.IpAddress;
22import org.onosproject.cluster.ClusterService;
23import org.onosproject.cluster.DefaultControllerNode;
24import org.onosproject.cluster.NodeId;
25import org.onosproject.mastership.MastershipService;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.segmentrouting.config.DeviceConfiguration;
29import org.onosproject.store.service.StorageService;
30import org.onosproject.store.service.TestConsistentMap;
31
32import java.util.Optional;
33
34import static org.easymock.EasyMock.createMock;
35import static org.easymock.EasyMock.expect;
36import static org.easymock.EasyMock.replay;
37import static org.easymock.EasyMock.reset;
38import static org.junit.Assert.*;
39
40public class DefaultRoutingHandlerTest {
41 private SegmentRoutingManager srManager;
42 private DefaultRoutingHandler dfh;
43
44 private static final DeviceId DEV1A = DeviceId.deviceId("of:1a");
45 private static final DeviceId DEV1B = DeviceId.deviceId("of:1b");
46 private static final DeviceId DEV2 = DeviceId.deviceId("of:2");
47
48 private static final NodeId NODE1 = NodeId.nodeId("192.168.1.1");
49 private static final NodeId NODE2 = NodeId.nodeId("192.168.1.2");
50 private static final NodeId NODE3 = NodeId.nodeId("192.168.1.3");
51 private static final IpAddress IP1 = IpAddress.valueOf("192.168.1.1");
52 private static final IpAddress IP2 = IpAddress.valueOf("192.168.1.2");
53 private static final IpAddress IP3 = IpAddress.valueOf("192.168.1.3");
54
55 @Before
56 public void setUp() {
57 srManager = createMock(SegmentRoutingManager.class);
58 srManager.storageService = createMock(StorageService.class);
59 expect(srManager.storageService.consistentMapBuilder()).andReturn(new TestConsistentMap.Builder<>()).anyTimes();
60 replay(srManager.storageService);
61 srManager.routingRulePopulator = createMock(RoutingRulePopulator.class);
62 srManager.deviceService = createMock(DeviceService.class);
63 srManager.deviceConfiguration = createMock(DeviceConfiguration.class);
64 srManager.mastershipService = createMock(MastershipService.class);
65 srManager.clusterService = createMock(ClusterService.class);
66 dfh = new DefaultRoutingHandler(srManager);
67 }
68
Charles Chanfbcb8812018-04-18 18:41:05 -070069 private void clearCache() {
70 dfh.invalidateShouldProgramCache(DEV1A);
71 dfh.invalidateShouldProgramCache(DEV1B);
72 dfh.invalidateShouldProgramCache(DEV2);
73 }
74
Charles Chand66d6712018-03-29 16:03:41 -070075 // Node 1 is the master of switch 1A, 1B, and 2
76 @Test
77 public void testShouldHandleRoutingCase1() {
78 expect(srManager.mastershipService.getMasterFor(DEV1A)).andReturn(NODE1).anyTimes();
79 expect(srManager.mastershipService.getMasterFor(DEV1B)).andReturn(NODE1).anyTimes();
80 expect(srManager.mastershipService.getMasterFor(DEV2)).andReturn(NODE1).anyTimes();
81 replay(srManager.mastershipService);
82
83 expect(srManager.getPairDeviceId(DEV1A)).andReturn(Optional.of(DEV1B)).anyTimes();
84 expect(srManager.getPairDeviceId(DEV1B)).andReturn(Optional.of(DEV1A)).anyTimes();
85 expect(srManager.getPairDeviceId(DEV2)).andReturn(Optional.empty()).anyTimes();
86 replay(srManager);
87
88 // Node 1 should program every device
89 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
90 replay(srManager.clusterService);
91 assertTrue(dfh.shouldProgram(DEV1A));
92 assertTrue(dfh.shouldProgram(DEV1B));
93 assertTrue(dfh.shouldProgram(DEV2));
94
95 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -070096 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -070097
98 // Node 2 should program no device
99 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
100 replay(srManager.clusterService);
101 assertFalse(dfh.shouldProgram(DEV1A));
102 assertFalse(dfh.shouldProgram(DEV1B));
103 assertFalse(dfh.shouldProgram(DEV2));
104
105 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700106 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700107
108 // Node 3 should program no device
109 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE3, IP3)).anyTimes();
110 replay(srManager.clusterService);
111 assertFalse(dfh.shouldProgram(DEV1A));
112 assertFalse(dfh.shouldProgram(DEV1B));
113 assertFalse(dfh.shouldProgram(DEV2));
114 }
115
116 // Node 1 is the master of switch 1A, 1B
117 // Node 2 is the master of switch 2
118 @Test
119 public void testShouldHandleRoutingCase2() {
120 expect(srManager.mastershipService.getMasterFor(DEV1A)).andReturn(NODE1).anyTimes();
121 expect(srManager.mastershipService.getMasterFor(DEV1B)).andReturn(NODE1).anyTimes();
122 expect(srManager.mastershipService.getMasterFor(DEV2)).andReturn(NODE2).anyTimes();
123 replay(srManager.mastershipService);
124
125 expect(srManager.getPairDeviceId(DEV1A)).andReturn(Optional.of(DEV1B)).anyTimes();
126 expect(srManager.getPairDeviceId(DEV1B)).andReturn(Optional.of(DEV1A)).anyTimes();
127 expect(srManager.getPairDeviceId(DEV2)).andReturn(Optional.empty()).anyTimes();
128 replay(srManager);
129
130 // Node 1 should program 1A, 1B
131 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
132 replay(srManager.clusterService);
133 assertTrue(dfh.shouldProgram(DEV1A));
134 assertTrue(dfh.shouldProgram(DEV1B));
135 assertFalse(dfh.shouldProgram(DEV2));
136
137 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700138 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700139
140 // Node 2 should program 2
141 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
142 replay(srManager.clusterService);
143 assertFalse(dfh.shouldProgram(DEV1A));
144 assertFalse(dfh.shouldProgram(DEV1B));
145 assertTrue(dfh.shouldProgram(DEV2));
146
147 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700148 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700149
150 // Node 3 should program no device
151 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE3, IP3)).anyTimes();
152 replay(srManager.clusterService);
153 assertFalse(dfh.shouldProgram(DEV1A));
154 assertFalse(dfh.shouldProgram(DEV1B));
155 assertFalse(dfh.shouldProgram(DEV2));
156 }
157
158 // Node 1 is the master of switch 1A
159 // Node 2 is the master of switch 1B
160 // Node 3 is the master of switch 2
161 @Test
162 public void testShouldHandleRoutingCase3() {
163 expect(srManager.mastershipService.getMasterFor(DEV1A)).andReturn(NODE1).anyTimes();
164 expect(srManager.mastershipService.getMasterFor(DEV1B)).andReturn(NODE2).anyTimes();
165 expect(srManager.mastershipService.getMasterFor(DEV2)).andReturn(NODE3).anyTimes();
166 replay(srManager.mastershipService);
167
168 expect(srManager.getPairDeviceId(DEV1A)).andReturn(Optional.of(DEV1B)).anyTimes();
169 expect(srManager.getPairDeviceId(DEV1B)).andReturn(Optional.of(DEV1A)).anyTimes();
170 expect(srManager.getPairDeviceId(DEV2)).andReturn(Optional.empty()).anyTimes();
171 replay(srManager);
172
173 // Node 1 should program 1A, 1B
174 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
175 replay(srManager.clusterService);
176 assertTrue(dfh.shouldProgram(DEV1A));
177 assertTrue(dfh.shouldProgram(DEV1B));
178 assertFalse(dfh.shouldProgram(DEV2));
179
180 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700181 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700182
183 // Node 2 should program no device
184 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
185 replay(srManager.clusterService);
186 assertFalse(dfh.shouldProgram(DEV1A));
187 assertFalse(dfh.shouldProgram(DEV1B));
188 assertFalse(dfh.shouldProgram(DEV2));
189
190 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700191 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700192
193 // Node 3 should program 2
194 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE3, IP3)).anyTimes();
195 replay(srManager.clusterService);
196 assertFalse(dfh.shouldProgram(DEV1A));
197 assertFalse(dfh.shouldProgram(DEV1B));
198 assertTrue(dfh.shouldProgram(DEV2));
199 }
200
201 // Node 3 is the master of switch 1A, 1B, 2
202 // Later on, node 1 becomes the master of 1A; Node 2 becomes the master of 1B.
203 @Test
204 public void testShouldHandleRoutingCase4() {
205 expect(srManager.mastershipService.getMasterFor(DEV1A)).andReturn(NODE3).anyTimes();
206 expect(srManager.mastershipService.getMasterFor(DEV1B)).andReturn(NODE3).anyTimes();
207 expect(srManager.mastershipService.getMasterFor(DEV2)).andReturn(NODE3).anyTimes();
208 replay(srManager.mastershipService);
209
210 expect(srManager.getPairDeviceId(DEV1A)).andReturn(Optional.of(DEV1B)).anyTimes();
211 expect(srManager.getPairDeviceId(DEV1B)).andReturn(Optional.of(DEV1A)).anyTimes();
212 expect(srManager.getPairDeviceId(DEV2)).andReturn(Optional.empty()).anyTimes();
213 replay(srManager);
214
215 // Node 1 should program no device
216 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
217 replay(srManager.clusterService);
218 assertFalse(dfh.shouldProgram(DEV1A));
219 assertFalse(dfh.shouldProgram(DEV1B));
220 assertFalse(dfh.shouldProgram(DEV2));
221
222 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700223 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700224
225 // Node 2 should program no device
226 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
227 replay(srManager.clusterService);
228 assertFalse(dfh.shouldProgram(DEV1A));
229 assertFalse(dfh.shouldProgram(DEV1B));
230 assertFalse(dfh.shouldProgram(DEV2));
231
232 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700233 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700234
235 // Node 3 should program 1A, 1B and 2
236 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE3, IP3)).anyTimes();
237 replay(srManager.clusterService);
238 assertTrue(dfh.shouldProgram(DEV1A));
239 assertTrue(dfh.shouldProgram(DEV1B));
240 assertTrue(dfh.shouldProgram(DEV2));
241
242 // Mastership of switch 1A moves to Node 1
243 reset(srManager.mastershipService);
244 expect(srManager.mastershipService.getMasterFor(DEV1A)).andReturn(NODE1).anyTimes();
245 expect(srManager.mastershipService.getMasterFor(DEV1B)).andReturn(NODE2).anyTimes();
246 expect(srManager.mastershipService.getMasterFor(DEV2)).andReturn(NODE3).anyTimes();
247 replay(srManager.mastershipService);
248
249 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700250 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700251
252 // Node 1 should program 1A, 1B
253 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
254 replay(srManager.clusterService);
255 assertTrue(dfh.shouldProgram(DEV1A));
256 assertTrue(dfh.shouldProgram(DEV1B));
257 assertFalse(dfh.shouldProgram(DEV2));
258
259 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700260 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700261
262 // Node 2 should program no device
263 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
264 replay(srManager.clusterService);
265 assertFalse(dfh.shouldProgram(DEV1A));
266 assertFalse(dfh.shouldProgram(DEV1B));
267 assertFalse(dfh.shouldProgram(DEV2));
268
269 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700270 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700271
272 // Node 3 should program 2
273 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE3, IP3)).anyTimes();
274 replay(srManager.clusterService);
275 assertFalse(dfh.shouldProgram(DEV1A));
276 assertFalse(dfh.shouldProgram(DEV1B));
277 assertTrue(dfh.shouldProgram(DEV2));
278 }
279
280 // Node 1 is the master of 1A. 1B has no master
281 // Node 2 becomes the master of 1B later
282 @Test
283 public void testShouldHandleRoutingCase5() {
284 expect(srManager.mastershipService.getMasterFor(DEV1A)).andReturn(NODE1).anyTimes();
285 expect(srManager.mastershipService.getMasterFor(DEV1B)).andReturn(null).anyTimes();
286 replay(srManager.mastershipService);
287
288 expect(srManager.getPairDeviceId(DEV1A)).andReturn(Optional.of(DEV1B)).anyTimes();
289 expect(srManager.getPairDeviceId(DEV1B)).andReturn(Optional.of(DEV1A)).anyTimes();
290 replay(srManager);
291
292 // Node 1 should program both 1A and 1B
293 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
294 replay(srManager.clusterService);
295 assertTrue(dfh.shouldProgram(DEV1A));
296 assertTrue(dfh.shouldProgram(DEV1B));
297
298 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700299 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700300
301 // Node 2 should program no device
302 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
303 replay(srManager.clusterService);
304 assertFalse(dfh.shouldProgram(DEV1A));
305 assertFalse(dfh.shouldProgram(DEV1B));
306
307 // Mastership of switch 1B moves to Node 2
308 reset(srManager.mastershipService);
309 expect(srManager.mastershipService.getMasterFor(DEV1A)).andReturn(NODE1).anyTimes();
310 expect(srManager.mastershipService.getMasterFor(DEV1B)).andReturn(NODE2).anyTimes();
311 replay(srManager.mastershipService);
312
313 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700314 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700315
316 // Node 1 should program 1A, 1B
317 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
318 replay(srManager.clusterService);
319 assertTrue(dfh.shouldProgram(DEV1A));
320 assertTrue(dfh.shouldProgram(DEV1B));
321
322 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700323 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700324
325 // Node 2 should program no device
326 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
327 replay(srManager.clusterService);
328 assertFalse(dfh.shouldProgram(DEV1A));
329 assertFalse(dfh.shouldProgram(DEV1B));
330 }
331
332 // Neither 1A or 1B has master
333 @Test
334 public void testShouldHandleRoutingCase6() {
335 expect(srManager.mastershipService.getMasterFor(DEV1A)).andReturn(null).anyTimes();
336 expect(srManager.mastershipService.getMasterFor(DEV1B)).andReturn(null).anyTimes();
337 replay(srManager.mastershipService);
338
339 expect(srManager.getPairDeviceId(DEV1A)).andReturn(Optional.of(DEV1B)).anyTimes();
340 expect(srManager.getPairDeviceId(DEV1B)).andReturn(Optional.of(DEV1A)).anyTimes();
341 replay(srManager);
342
343 // Node 1 should program no device
344 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
345 replay(srManager.clusterService);
346 assertFalse(dfh.shouldProgram(DEV1A));
347 assertFalse(dfh.shouldProgram(DEV1B));
348
349 reset(srManager.clusterService);
Charles Chanfbcb8812018-04-18 18:41:05 -0700350 clearCache();
Charles Chand66d6712018-03-29 16:03:41 -0700351
352 // Node 2 should program no device
353 expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
354 replay(srManager.clusterService);
355 assertFalse(dfh.shouldProgram(DEV1A));
356 assertFalse(dfh.shouldProgram(DEV1B));
357
358 assertFalse(dfh.shouldProgram.containsKey(Sets.newHashSet(DEV1A, DEV1B)));
359 }
360}