blob: 5e1a11db6fbc458e371c67f841a0708bce107ba5 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent.runtime;
Ray Milkey6688cd82014-03-11 16:40:46 -07002
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.easymock.EasyMock.anyObject;
4import static org.easymock.EasyMock.createMock;
5import static org.easymock.EasyMock.eq;
6import static org.easymock.EasyMock.expect;
7import static org.easymock.EasyMock.expectLastCall;
8import static org.easymock.EasyMock.replay;
9import static org.easymock.EasyMock.verify;
10import static org.hamcrest.MatcherAssert.assertThat;
11import static org.hamcrest.Matchers.equalTo;
12import static org.hamcrest.Matchers.hasItem;
13import static org.hamcrest.Matchers.hasSize;
14import static org.hamcrest.Matchers.notNullValue;
15
16import java.util.Collection;
17import java.util.LinkedList;
18import java.util.List;
19
Ray Milkey6688cd82014-03-11 16:40:46 -070020import net.floodlightcontroller.core.module.FloodlightModuleContext;
21import net.floodlightcontroller.core.module.FloodlightModuleException;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070022import net.floodlightcontroller.restserver.IRestApiService;
23
Jonathan Hart6df90172014-04-03 10:13:11 -070024import net.onrc.onos.core.datagrid.IDatagridService;
25import net.onrc.onos.core.datagrid.IEventChannel;
26import net.onrc.onos.core.datagrid.IEventChannelListener;
Jonathan Hartaa380972014-04-03 10:24:46 -070027import net.onrc.onos.core.intent.Intent;
Jonathan Harta88fd242014-04-03 11:24:54 -070028import net.onrc.onos.core.intent.Intent.IntentState;
Jonathan Hartaa380972014-04-03 10:24:46 -070029import net.onrc.onos.core.intent.IntentMap;
Jonathan Harta88fd242014-04-03 11:24:54 -070030import net.onrc.onos.core.intent.IntentOperation.Operator;
Jonathan Hartaa380972014-04-03 10:24:46 -070031import net.onrc.onos.core.intent.IntentOperationList;
32import net.onrc.onos.core.intent.MockNetworkGraph;
33import net.onrc.onos.core.intent.ShortestPathIntent;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070034import net.onrc.onos.core.intent.runtime.web.IntentWebRoutable;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070035import net.onrc.onos.core.registry.IControllerRegistryService;
Jonathan Hart472062d2014-04-03 10:56:48 -070036import net.onrc.onos.core.topology.DeviceEvent;
37import net.onrc.onos.core.topology.INetworkGraphListener;
38import net.onrc.onos.core.topology.INetworkGraphService;
39import net.onrc.onos.core.topology.LinkEvent;
40import net.onrc.onos.core.topology.PortEvent;
41import net.onrc.onos.core.topology.SwitchEvent;
Ray Milkey6688cd82014-03-11 16:40:46 -070042
Jonathan Harta88fd242014-04-03 11:24:54 -070043import org.hamcrest.Description;
44import org.hamcrest.Factory;
45import org.hamcrest.Matcher;
46import org.hamcrest.Matchers;
47import org.hamcrest.TypeSafeMatcher;
Ray Milkey6688cd82014-03-11 16:40:46 -070048import org.junit.After;
49import org.junit.Before;
50import org.junit.Test;
51import org.junit.runner.RunWith;
52import org.powermock.api.easymock.PowerMock;
53import org.powermock.core.classloader.annotations.PrepareForTest;
54import org.powermock.modules.junit4.PowerMockRunner;
55
Ray Milkey6688cd82014-03-11 16:40:46 -070056/**
57 * @author Ray Milkey (ray@onlab.us)
Ray Milkey269ffb92014-04-03 14:43:30 -070058 * <p/>
59 * Unit tests for the Path Calculation Runtime module (PathCalcRuntimeModule).
60 * These test cases check the results of creating paths, deleting paths, and
61 * rerouting paths. The network graph, controller registry, and data grid are
62 * mocked out. The individual tests check the high level intents and the
63 * resulting operation lists to be sure they match the intended APIs.
Ray Milkey6688cd82014-03-11 16:40:46 -070064 */
65@RunWith(PowerMockRunner.class)
66@PrepareForTest(PathCalcRuntimeModule.class)
67public class PathCalcRuntimeModuleTest {
Ray Milkeydc659c42014-03-28 16:30:42 -070068 private static final Long LOCAL_PORT = 0xFFFEL;
69
Ray Milkey6688cd82014-03-11 16:40:46 -070070 private FloodlightModuleContext modContext;
71 private IDatagridService datagridService;
72 private INetworkGraphService networkGraphService;
73 private IControllerRegistryService controllerRegistryService;
74 private PersistIntent persistIntent;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070075 private IRestApiService restApi;
Ray Milkeydc659c42014-03-28 16:30:42 -070076 private MockNetworkGraph graph;
Toshio Koidee04a9df2014-05-01 15:49:28 -070077 private IEventChannel<Long, IntentOperationList> intentOperationChannel;
78 private IEventChannel<Long, IntentStateList> intentStateChannel;
Ray Milkey6688cd82014-03-11 16:40:46 -070079
80 @SuppressWarnings("unchecked")
81 @Before
82 public void setUp() throws Exception {
Ray Milkeydc659c42014-03-28 16:30:42 -070083 graph = new MockNetworkGraph();
Ray Milkey6688cd82014-03-11 16:40:46 -070084 graph.createSampleTopology1();
85
86 datagridService = createMock(IDatagridService.class);
87 networkGraphService = createMock(INetworkGraphService.class);
88 controllerRegistryService = createMock(IControllerRegistryService.class);
89 modContext = createMock(FloodlightModuleContext.class);
Toshio Koidee04a9df2014-05-01 15:49:28 -070090 intentOperationChannel = createMock(IEventChannel.class);
91 intentStateChannel = createMock(IEventChannel.class);
Ray Milkey6688cd82014-03-11 16:40:46 -070092 persistIntent = PowerMock.createMock(PersistIntent.class);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070093 restApi = createMock(IRestApiService.class);
Ray Milkey6688cd82014-03-11 16:40:46 -070094
95 PowerMock.expectNew(PersistIntent.class,
Pavlin Radoslavov0294e052014-04-10 13:36:45 -070096 anyObject(IControllerRegistryService.class)).andReturn(persistIntent);
Ray Milkey6688cd82014-03-11 16:40:46 -070097
98 expect(modContext.getServiceImpl(IDatagridService.class))
99 .andReturn(datagridService).once();
100 expect(modContext.getServiceImpl(INetworkGraphService.class))
101 .andReturn(networkGraphService).once();
102 expect(modContext.getServiceImpl(IControllerRegistryService.class))
103 .andReturn(controllerRegistryService).once();
104 expect(persistIntent.getKey()).andReturn(1L).anyTimes();
105 expect(persistIntent.persistIfLeader(eq(1L),
106 anyObject(IntentOperationList.class))).andReturn(true)
107 .anyTimes();
Pavlin Radoslavov13669052014-05-13 10:33:39 -0700108 expect(modContext.getServiceImpl(IRestApiService.class))
109 .andReturn(restApi).once();
Ray Milkey6688cd82014-03-11 16:40:46 -0700110
111 expect(networkGraphService.getNetworkGraph()).andReturn(graph)
112 .anyTimes();
113 networkGraphService.registerNetworkGraphListener(
114 anyObject(INetworkGraphListener.class));
115 expectLastCall();
116
117 expect(datagridService.createChannel("onos.pathintent",
118 Long.class, IntentOperationList.class))
Toshio Koidee04a9df2014-05-01 15:49:28 -0700119 .andReturn(intentOperationChannel).once();
Ray Milkey6688cd82014-03-11 16:40:46 -0700120
121 expect(datagridService.addListener(
122 eq("onos.pathintent_state"),
123 anyObject(IEventChannelListener.class),
124 eq(Long.class),
125 eq(IntentStateList.class)))
Toshio Koidee04a9df2014-05-01 15:49:28 -0700126 .andReturn(intentStateChannel).once();
Pavlin Radoslavov13669052014-05-13 10:33:39 -0700127 restApi.addRestletRoutable(anyObject(IntentWebRoutable.class));
Ray Milkey6688cd82014-03-11 16:40:46 -0700128
129 replay(datagridService);
130 replay(networkGraphService);
131 replay(modContext);
132 replay(controllerRegistryService);
133 PowerMock.replay(persistIntent, PersistIntent.class);
Pavlin Radoslavov13669052014-05-13 10:33:39 -0700134 replay(restApi);
Ray Milkey6688cd82014-03-11 16:40:46 -0700135 }
136
Ray Milkeydc659c42014-03-28 16:30:42 -0700137
138 /**
139 * Hamcrest matcher to check that a collection of Intents contains an
140 * Intent with the specified Intent Id.
141 */
142 public static class EntryForIntentMatcher extends TypeSafeMatcher<Collection<Intent>> {
143 final private String id;
144
145 public EntryForIntentMatcher(String idValue) {
146 id = idValue;
147 }
148
149 @Override
150 public boolean matchesSafely(Collection<Intent> intents) {
151 assertThat(intents,
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 hasItem(Matchers.<Intent>hasProperty("id", equalTo(id))));
Ray Milkeydc659c42014-03-28 16:30:42 -0700153 return true;
154 }
155
156 @Override
157 public void describeTo(Description description) {
158 description.appendText("an intent with id \" ").
159 appendText(id).
160 appendText("\"");
161 }
162 }
163
164
165 /**
166 * Factory method to create an Intent entry Matcher. Returns a matcher
167 * for the Intent with the given id.
Ray Milkey269ffb92014-04-03 14:43:30 -0700168 *
Ray Milkeydc659c42014-03-28 16:30:42 -0700169 * @param id id of the intent to match
170 * @return Matcher object
171 */
172 @Factory
173 public static Matcher<Collection<Intent>> hasIntentWithId(String id) {
174 return new EntryForIntentMatcher(id);
175 }
176
177
178 /**
179 * Matcher to determine if an IntentMap contains an entry with a given id,
180 * and that entry has a given state.
181 */
182 public static class IntentsHaveIntentWithStateMatcher extends TypeSafeMatcher<IntentMap> {
183 final private String id;
184 final private IntentState state;
185 private Intent intent;
186
187 public IntentsHaveIntentWithStateMatcher(String idValue,
188 IntentState stateValue) {
189 id = idValue;
190 state = stateValue;
191 }
192
193 @Override
194 public boolean matchesSafely(IntentMap intents) {
195 intent = intents.getIntent(id);
196
197 return intent != null && intent.getState() == state;
198 }
199
200 @Override
201 public void describeTo(Description description) {
202 if (intent == null) {
203 description.appendText("intent lookup for id \"");
204 description.appendText(id);
205 description.appendText("\"");
206 } else {
207 description.appendText("state ");
208 description.appendText(state.toString());
209 }
210 }
211
212 @Override
213 public void describeMismatchSafely(IntentMap intents,
214 Description mismatchDescription) {
215 if (intent != null) {
216 mismatchDescription.appendText("was ").
Ray Milkey269ffb92014-04-03 14:43:30 -0700217 appendText(intent.getState().toString());
Ray Milkeydc659c42014-03-28 16:30:42 -0700218 } else {
219 mismatchDescription.appendText("that intent was not found");
220 }
221 }
222 }
223
224
225 /**
226 * Factory method to create a Matcher for an IntentMap that looks for an
227 * Intent with a given id and state.
228 *
Ray Milkey269ffb92014-04-03 14:43:30 -0700229 * @param id id of the Intent to match
Ray Milkeydc659c42014-03-28 16:30:42 -0700230 * @param state if the Intent is found, its state must match this
231 * @return Matcher object
232 */
233 @Factory
234 public static Matcher<IntentMap> hasIntentWithIdAndState(String id,
235 IntentState state) {
236 return new IntentsHaveIntentWithStateMatcher(id, state);
237 }
238
239
Ray Milkey6688cd82014-03-11 16:40:46 -0700240 @After
241 public void tearDown() {
242 verify(datagridService);
243 verify(networkGraphService);
244 verify(modContext);
245 verify(controllerRegistryService);
246 PowerMock.verify(persistIntent, PersistIntent.class);
Pavlin Radoslavov13669052014-05-13 10:33:39 -0700247 verify(restApi);
Ray Milkey6688cd82014-03-11 16:40:46 -0700248 }
249
250 /**
251 * Test the result of executing a path calculation on an
252 * Intent Operation List which contains a path that references a
253 * non-existent switch.
Ray Milkey269ffb92014-04-03 14:43:30 -0700254 * <p/>
Ray Milkey6688cd82014-03-11 16:40:46 -0700255 * A 3 path list is created where one of the paths references a switch
256 * that is not in the topology. The test checks that the resulting
257 * Operation List has entries for the 2 correct paths, and that the
258 * high level intents contain a proper error entry for the bad path.
259 */
260 @Test
261 public void testInvalidSwitchName() throws FloodlightModuleException {
Ray Milkey6688cd82014-03-11 16:40:46 -0700262 final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent";
263
264 // create shortest path intents
265 final IntentOperationList opList = new IntentOperationList();
266 opList.add(Operator.ADD,
267 new ShortestPathIntent(BAD_SWITCH_INTENT_NAME, 111L, 12L,
268 LOCAL_PORT, 2L, 21L, LOCAL_PORT));
269 opList.add(Operator.ADD,
270 new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L,
271 LOCAL_PORT));
272 opList.add(Operator.ADD,
273 new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L,
274 LOCAL_PORT));
275
276 // compile high-level intent operations into low-level intent
277 // operations (calculate paths)
278 final PathCalcRuntimeModule runtime = new PathCalcRuntimeModule();
279 runtime.init(modContext);
280 runtime.startUp(modContext);
281 final IntentOperationList pathIntentOpList =
282 runtime.executeIntentOperations(opList);
283 assertThat(pathIntentOpList, notNullValue());
284
285 final IntentMap highLevelIntents = runtime.getHighLevelIntents();
286 assertThat(highLevelIntents, notNullValue());
287
288 final Collection<Intent> allIntents = highLevelIntents.getAllIntents();
289 assertThat(allIntents, notNullValue());
290
291 // One intent had an error and should not create a path list entry
292 assertThat(pathIntentOpList, hasSize(opList.size() - 1));
293
294 // Should be a high level intent for each operation
Ray Milkeydc659c42014-03-28 16:30:42 -0700295 assertThat(allIntents, hasSize(opList.size()));
Ray Milkey6688cd82014-03-11 16:40:46 -0700296
297 // Check that we got a high level intent for each operation
Ray Milkeydc659c42014-03-28 16:30:42 -0700298 assertThat(allIntents, hasIntentWithId("3"));
299 assertThat(allIntents, hasIntentWithId("2"));
300 assertThat(allIntents, hasIntentWithId(BAD_SWITCH_INTENT_NAME));
Ray Milkey6688cd82014-03-11 16:40:46 -0700301
302 // Check that the non existent switch was NACKed
Ray Milkeydc659c42014-03-28 16:30:42 -0700303 assertThat(highLevelIntents, hasIntentWithIdAndState(BAD_SWITCH_INTENT_NAME, IntentState.INST_NACK));
Ray Milkey6688cd82014-03-11 16:40:46 -0700304
305 // Check that switch 2 was correctly processed
Ray Milkeydc659c42014-03-28 16:30:42 -0700306 assertThat(highLevelIntents, hasIntentWithIdAndState("2", IntentState.INST_REQ));
Ray Milkey6688cd82014-03-11 16:40:46 -0700307
308 // Check that switch 3 was correctly processed
Ray Milkeydc659c42014-03-28 16:30:42 -0700309 assertThat(highLevelIntents, hasIntentWithIdAndState("3", IntentState.INST_REQ));
310
311 }
312
313
314 /**
315 * Test the result of executing a path calculation on an
316 * Intent Operation List and then removing one of the switches.
Ray Milkey269ffb92014-04-03 14:43:30 -0700317 * <p/>
Ray Milkeydc659c42014-03-28 16:30:42 -0700318 * A 3 path list is created and then one of the paths is removed.
319 * The test checks that the resulting Operation List is correct,
320 * and that the high level intents contain a proper "delete requested"
321 * entry for the deleted path.
322 */
323 @Test
324 public void testIntentRemoval() throws FloodlightModuleException {
325
326 // create shortest path intents
327 final IntentOperationList opList = new IntentOperationList();
328 opList.add(Operator.ADD,
329 new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L,
Ray Milkey269ffb92014-04-03 14:43:30 -0700330 LOCAL_PORT));
Ray Milkeydc659c42014-03-28 16:30:42 -0700331 opList.add(Operator.ADD,
332 new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L,
Ray Milkey269ffb92014-04-03 14:43:30 -0700333 LOCAL_PORT));
Ray Milkeydc659c42014-03-28 16:30:42 -0700334 opList.add(Operator.ADD,
335 new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L,
Ray Milkey269ffb92014-04-03 14:43:30 -0700336 LOCAL_PORT));
Ray Milkeydc659c42014-03-28 16:30:42 -0700337
338 // compile high-level intent operations into low-level intent
339 // operations (calculate paths)
340 final PathCalcRuntimeModule runtime = new PathCalcRuntimeModule();
341 runtime.init(modContext);
342 runtime.startUp(modContext);
343 final IntentOperationList pathIntentOpList =
344 runtime.executeIntentOperations(opList);
345 assertThat(pathIntentOpList, notNullValue());
346
347 final IntentMap highLevelIntents = runtime.getHighLevelIntents();
348 assertThat(highLevelIntents, notNullValue());
349
350 final Collection<Intent> allIntents = highLevelIntents.getAllIntents();
351 assertThat(allIntents, notNullValue());
352
353 // Should be one operation per path
354 assertThat(pathIntentOpList, hasSize(opList.size()));
355
356 // Should be a high level intent for each operation
357 assertThat(allIntents, hasSize(opList.size()));
358
359 // Check that we got a high level intent for each operation
360 assertThat(allIntents, hasIntentWithId("3"));
361 assertThat(allIntents, hasIntentWithId("2"));
362 assertThat(allIntents, hasIntentWithId("1"));
363
364 // Check that switch 1 was correctly processed
365 assertThat(highLevelIntents,
Ray Milkey269ffb92014-04-03 14:43:30 -0700366 hasIntentWithIdAndState("1", IntentState.INST_REQ));
Ray Milkeydc659c42014-03-28 16:30:42 -0700367
368 // Check that switch 2 was correctly processed
369 assertThat(highLevelIntents,
Ray Milkey269ffb92014-04-03 14:43:30 -0700370 hasIntentWithIdAndState("2", IntentState.INST_REQ));
Ray Milkeydc659c42014-03-28 16:30:42 -0700371
372 // Check that switch 3 was correctly processed
373 assertThat(highLevelIntents,
Ray Milkey269ffb92014-04-03 14:43:30 -0700374 hasIntentWithIdAndState("3", IntentState.INST_REQ));
Ray Milkeydc659c42014-03-28 16:30:42 -0700375
376 // Now delete one path and check the results
377 final IntentOperationList opListForRemoval = new IntentOperationList();
378 opListForRemoval.add(Operator.REMOVE,
379 new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L,
380 LOCAL_PORT));
381
382 final IntentOperationList pathIntentOpListAfterRemoval =
383 runtime.executeIntentOperations(opListForRemoval);
384 assertThat(pathIntentOpListAfterRemoval, notNullValue());
385 assertThat(pathIntentOpListAfterRemoval, hasSize(1));
386
387 // Check the high level intents.
388 final IntentMap highLevelIntentsAfterRemoval = runtime.getHighLevelIntents();
389 assertThat(highLevelIntentsAfterRemoval, notNullValue());
390
391 final Collection<Intent> allIntentsAfterRemoval = highLevelIntentsAfterRemoval.getAllIntents();
392 assertThat(allIntentsAfterRemoval, notNullValue());
393 assertThat(allIntentsAfterRemoval, hasSize(3));
394
395 // Check that we got a high level intent for each operation
396 assertThat(allIntentsAfterRemoval, hasIntentWithId("3"));
397 assertThat(allIntentsAfterRemoval, hasIntentWithId("2"));
398 assertThat(allIntentsAfterRemoval, hasIntentWithId("1"));
399
400 // Check the states of the high level intents
401 // Check that switch 1 was correctly processed
402 assertThat(highLevelIntents,
403 hasIntentWithIdAndState("1", IntentState.DEL_REQ));
404
405 // Check that switch 2 was correctly processed
406 assertThat(highLevelIntents,
407 hasIntentWithIdAndState("2", IntentState.INST_REQ));
408
409 // Check that switch 3 was correctly processed
410 assertThat(highLevelIntents,
411 hasIntentWithIdAndState("3", IntentState.INST_REQ));
412 }
413
414 /**
415 * Test the result of executing a path calculation on an
416 * Intent Operation List and then forcing a reroute.
Ray Milkey269ffb92014-04-03 14:43:30 -0700417 * <p/>
Ray Milkeydc659c42014-03-28 16:30:42 -0700418 * A 3 path list is created and then one of the links is removed.
419 * The test checks that the resulting Operation List is correct,
420 * and that the high level intents contain a proper "reroute requested"
421 * entry for the deleted link.
422 */
423 @Test
424 public void testIntentReroute() throws FloodlightModuleException {
425
426 // create shortest path intents
427 final IntentOperationList opList = new IntentOperationList();
428 final ShortestPathIntent pathIntent1 =
429 new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L,
Ray Milkey269ffb92014-04-03 14:43:30 -0700430 LOCAL_PORT);
Ray Milkeydc659c42014-03-28 16:30:42 -0700431
432 opList.add(Operator.ADD, pathIntent1);
433 opList.add(Operator.ADD,
434 new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L,
Ray Milkey269ffb92014-04-03 14:43:30 -0700435 LOCAL_PORT));
Ray Milkeydc659c42014-03-28 16:30:42 -0700436 opList.add(Operator.ADD,
437 new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L,
Ray Milkey269ffb92014-04-03 14:43:30 -0700438 LOCAL_PORT));
Ray Milkeydc659c42014-03-28 16:30:42 -0700439
440 // compile high-level intent operations into low-level intent
441 // operations (calculate paths)
442 final PathCalcRuntimeModule runtime = new PathCalcRuntimeModule();
443 runtime.init(modContext);
444 runtime.startUp(modContext);
445 final IntentOperationList pathIntentOpList =
446 runtime.executeIntentOperations(opList);
447 assertThat(pathIntentOpList, notNullValue());
448
449 final IntentMap highLevelIntents = runtime.getHighLevelIntents();
450 assertThat(highLevelIntents, notNullValue());
451
452 final Collection<Intent> allIntents = highLevelIntents.getAllIntents();
453 assertThat(allIntents, notNullValue());
454
455 // Should be one operation per path
456 assertThat(pathIntentOpList, hasSize(opList.size()));
457
458 // Should be a high level intent for each operation
459 assertThat(allIntents, hasSize(opList.size()));
460
461 // Check that we got a high level intent for each operation
462 assertThat(allIntents, hasIntentWithId("3"));
463 assertThat(allIntents, hasIntentWithId("2"));
464 assertThat(allIntents, hasIntentWithId("1"));
465
466 // Check that switch 1 was correctly processed
467 assertThat(highLevelIntents,
Ray Milkey269ffb92014-04-03 14:43:30 -0700468 hasIntentWithIdAndState("1", IntentState.INST_REQ));
Ray Milkeydc659c42014-03-28 16:30:42 -0700469
470 // Check that switch 2 was correctly processed
471 assertThat(highLevelIntents,
Ray Milkey269ffb92014-04-03 14:43:30 -0700472 hasIntentWithIdAndState("2", IntentState.INST_REQ));
Ray Milkeydc659c42014-03-28 16:30:42 -0700473
474 // Check that switch 3 was correctly processed
475 assertThat(highLevelIntents,
Ray Milkey269ffb92014-04-03 14:43:30 -0700476 hasIntentWithIdAndState("3", IntentState.INST_REQ));
Ray Milkeydc659c42014-03-28 16:30:42 -0700477
478 // Now add a different path to one of the switches path and check
479 // the results
480 IntentStateList states = new IntentStateList();
481 states.put("1", IntentState.INST_ACK);
482 states.put("2", IntentState.INST_ACK);
483 states.put("3", IntentState.INST_ACK);
484 runtime.getHighLevelIntents().changeStates(states);
485 states.clear();
486 states.put("1___0", IntentState.INST_ACK);
487 states.put("2___0", IntentState.INST_ACK);
488 states.put("3___0", IntentState.INST_ACK);
489 runtime.getPathIntents().changeStates(states);
490
491 final List<SwitchEvent> emptySwitchEvents = new LinkedList<>();
492 final List<PortEvent> emptyPortEvents = new LinkedList<>();
493 final List<DeviceEvent> emptyDeviceEvents = new LinkedList<>();
494 final List<LinkEvent> addedLinkEvents = new LinkedList<>();
495 final List<LinkEvent> removedLinkEvents = new LinkedList<>();
496
497 graph.removeLink(1L, 12L, 2L, 21L); // This link is used by the intent "1"
498 graph.removeLink(2L, 21L, 1L, 12L);
499 LinkEvent linkEvent1 = new LinkEvent(1L, 12L, 2L, 21L);
500 LinkEvent linkEvent2 = new LinkEvent(2L, 21L, 1L, 12L);
501 removedLinkEvents.add(linkEvent1);
502 removedLinkEvents.add(linkEvent2);
503 runtime.networkGraphEvents(
504 emptySwitchEvents,
505 emptySwitchEvents,
506 emptyPortEvents,
507 emptyPortEvents,
508 addedLinkEvents,
509 removedLinkEvents,
510 emptyDeviceEvents,
511 emptyDeviceEvents);
512 final IntentOperationList opListForReroute = new IntentOperationList();
513 opListForReroute.add(Operator.ADD, pathIntent1);
514
515 final IntentOperationList pathIntentOpListAfterReroute =
516 runtime.executeIntentOperations(opListForReroute);
517 assertThat(pathIntentOpListAfterReroute, notNullValue());
518 assertThat(pathIntentOpListAfterReroute, hasSize(2));
519
520 // Check the high level intents.
521 final IntentMap highLevelIntentsAfterReroute = runtime.getHighLevelIntents();
522 assertThat(highLevelIntentsAfterReroute, notNullValue());
523
524 final Collection<Intent> allIntentsAfterReroute = highLevelIntentsAfterReroute.getAllIntents();
525 assertThat(allIntentsAfterReroute, notNullValue());
526 assertThat(allIntentsAfterReroute, hasSize(3));
527
528 // Check that we got a high level intent for each operation
529 assertThat(allIntentsAfterReroute, hasIntentWithId("3"));
530 assertThat(allIntentsAfterReroute, hasIntentWithId("2"));
531 assertThat(allIntentsAfterReroute, hasIntentWithId("1"));
532
533 // Check the states of the high level intents
534 // Check that switch 1 was correctly processed
535 assertThat(highLevelIntents,
536 hasIntentWithIdAndState("1", IntentState.REROUTE_REQ));
537
538 // Check that switch 2 was correctly processed
539 assertThat(highLevelIntents,
540 hasIntentWithIdAndState("2", IntentState.INST_ACK));
541
542 // Check that switch 3 was correctly processed
543 assertThat(highLevelIntents,
544 hasIntentWithIdAndState("3", IntentState.INST_ACK));
545
Ray Milkey6688cd82014-03-11 16:40:46 -0700546
547 }
548
549
550}