Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime; |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 2 | |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 3 | import static org.easymock.EasyMock.anyObject; |
| 4 | import static org.easymock.EasyMock.createMock; |
| 5 | import static org.easymock.EasyMock.eq; |
| 6 | import static org.easymock.EasyMock.expect; |
| 7 | import static org.easymock.EasyMock.expectLastCall; |
| 8 | import static org.easymock.EasyMock.replay; |
| 9 | import static org.easymock.EasyMock.verify; |
| 10 | import static org.hamcrest.MatcherAssert.assertThat; |
| 11 | import static org.hamcrest.Matchers.equalTo; |
| 12 | import static org.hamcrest.Matchers.hasItem; |
| 13 | import static org.hamcrest.Matchers.hasSize; |
| 14 | import static org.hamcrest.Matchers.notNullValue; |
| 15 | |
| 16 | import java.util.Collection; |
| 17 | import java.util.LinkedList; |
| 18 | import java.util.List; |
| 19 | |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 21 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 23 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 24 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.intent.Intent; |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.intent.Intent.IntentState; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.intent.IntentMap; |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.intent.IntentOperation.Operator; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.intent.IntentOperationList; |
| 30 | import net.onrc.onos.core.intent.MockNetworkGraph; |
| 31 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 33 | import net.onrc.onos.core.topology.DeviceEvent; |
| 34 | import net.onrc.onos.core.topology.INetworkGraphListener; |
| 35 | import net.onrc.onos.core.topology.INetworkGraphService; |
| 36 | import net.onrc.onos.core.topology.LinkEvent; |
| 37 | import net.onrc.onos.core.topology.PortEvent; |
| 38 | import net.onrc.onos.core.topology.SwitchEvent; |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 39 | |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 40 | import org.hamcrest.Description; |
| 41 | import org.hamcrest.Factory; |
| 42 | import org.hamcrest.Matcher; |
| 43 | import org.hamcrest.Matchers; |
| 44 | import org.hamcrest.TypeSafeMatcher; |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 45 | import org.junit.After; |
| 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | import org.powermock.api.easymock.PowerMock; |
| 50 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 51 | import org.powermock.modules.junit4.PowerMockRunner; |
| 52 | |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 53 | /** |
| 54 | * @author Ray Milkey (ray@onlab.us) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | * <p/> |
| 56 | * Unit tests for the Path Calculation Runtime module (PathCalcRuntimeModule). |
| 57 | * These test cases check the results of creating paths, deleting paths, and |
| 58 | * rerouting paths. The network graph, controller registry, and data grid are |
| 59 | * mocked out. The individual tests check the high level intents and the |
| 60 | * resulting operation lists to be sure they match the intended APIs. |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 61 | */ |
| 62 | @RunWith(PowerMockRunner.class) |
| 63 | @PrepareForTest(PathCalcRuntimeModule.class) |
| 64 | public class PathCalcRuntimeModuleTest { |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 65 | private static final Long LOCAL_PORT = 0xFFFEL; |
| 66 | |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 67 | private FloodlightModuleContext modContext; |
| 68 | private IDatagridService datagridService; |
| 69 | private INetworkGraphService networkGraphService; |
| 70 | private IControllerRegistryService controllerRegistryService; |
| 71 | private PersistIntent persistIntent; |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 72 | private MockNetworkGraph graph; |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 73 | |
| 74 | @SuppressWarnings("unchecked") |
| 75 | @Before |
| 76 | public void setUp() throws Exception { |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 77 | graph = new MockNetworkGraph(); |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 78 | graph.createSampleTopology1(); |
| 79 | |
| 80 | datagridService = createMock(IDatagridService.class); |
| 81 | networkGraphService = createMock(INetworkGraphService.class); |
| 82 | controllerRegistryService = createMock(IControllerRegistryService.class); |
| 83 | modContext = createMock(FloodlightModuleContext.class); |
| 84 | final IEventChannel eventChannel = createMock(IEventChannel.class); |
| 85 | persistIntent = PowerMock.createMock(PersistIntent.class); |
| 86 | |
| 87 | PowerMock.expectNew(PersistIntent.class, |
Pavlin Radoslavov | 0294e05 | 2014-04-10 13:36:45 -0700 | [diff] [blame] | 88 | anyObject(IControllerRegistryService.class)).andReturn(persistIntent); |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 89 | |
| 90 | expect(modContext.getServiceImpl(IDatagridService.class)) |
| 91 | .andReturn(datagridService).once(); |
| 92 | expect(modContext.getServiceImpl(INetworkGraphService.class)) |
| 93 | .andReturn(networkGraphService).once(); |
| 94 | expect(modContext.getServiceImpl(IControllerRegistryService.class)) |
| 95 | .andReturn(controllerRegistryService).once(); |
| 96 | expect(persistIntent.getKey()).andReturn(1L).anyTimes(); |
| 97 | expect(persistIntent.persistIfLeader(eq(1L), |
| 98 | anyObject(IntentOperationList.class))).andReturn(true) |
| 99 | .anyTimes(); |
| 100 | |
| 101 | expect(networkGraphService.getNetworkGraph()).andReturn(graph) |
| 102 | .anyTimes(); |
| 103 | networkGraphService.registerNetworkGraphListener( |
| 104 | anyObject(INetworkGraphListener.class)); |
| 105 | expectLastCall(); |
| 106 | |
| 107 | expect(datagridService.createChannel("onos.pathintent", |
| 108 | Long.class, IntentOperationList.class)) |
| 109 | .andReturn(eventChannel).once(); |
| 110 | |
| 111 | expect(datagridService.addListener( |
| 112 | eq("onos.pathintent_state"), |
| 113 | anyObject(IEventChannelListener.class), |
| 114 | eq(Long.class), |
| 115 | eq(IntentStateList.class))) |
| 116 | .andReturn(eventChannel).once(); |
| 117 | |
| 118 | replay(datagridService); |
| 119 | replay(networkGraphService); |
| 120 | replay(modContext); |
| 121 | replay(controllerRegistryService); |
| 122 | PowerMock.replay(persistIntent, PersistIntent.class); |
| 123 | } |
| 124 | |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Hamcrest matcher to check that a collection of Intents contains an |
| 128 | * Intent with the specified Intent Id. |
| 129 | */ |
| 130 | public static class EntryForIntentMatcher extends TypeSafeMatcher<Collection<Intent>> { |
| 131 | final private String id; |
| 132 | |
| 133 | public EntryForIntentMatcher(String idValue) { |
| 134 | id = idValue; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean matchesSafely(Collection<Intent> intents) { |
| 139 | assertThat(intents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | hasItem(Matchers.<Intent>hasProperty("id", equalTo(id)))); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void describeTo(Description description) { |
| 146 | description.appendText("an intent with id \" "). |
| 147 | appendText(id). |
| 148 | appendText("\""); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /** |
| 154 | * Factory method to create an Intent entry Matcher. Returns a matcher |
| 155 | * for the Intent with the given id. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | * |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 157 | * @param id id of the intent to match |
| 158 | * @return Matcher object |
| 159 | */ |
| 160 | @Factory |
| 161 | public static Matcher<Collection<Intent>> hasIntentWithId(String id) { |
| 162 | return new EntryForIntentMatcher(id); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * Matcher to determine if an IntentMap contains an entry with a given id, |
| 168 | * and that entry has a given state. |
| 169 | */ |
| 170 | public static class IntentsHaveIntentWithStateMatcher extends TypeSafeMatcher<IntentMap> { |
| 171 | final private String id; |
| 172 | final private IntentState state; |
| 173 | private Intent intent; |
| 174 | |
| 175 | public IntentsHaveIntentWithStateMatcher(String idValue, |
| 176 | IntentState stateValue) { |
| 177 | id = idValue; |
| 178 | state = stateValue; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean matchesSafely(IntentMap intents) { |
| 183 | intent = intents.getIntent(id); |
| 184 | |
| 185 | return intent != null && intent.getState() == state; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void describeTo(Description description) { |
| 190 | if (intent == null) { |
| 191 | description.appendText("intent lookup for id \""); |
| 192 | description.appendText(id); |
| 193 | description.appendText("\""); |
| 194 | } else { |
| 195 | description.appendText("state "); |
| 196 | description.appendText(state.toString()); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void describeMismatchSafely(IntentMap intents, |
| 202 | Description mismatchDescription) { |
| 203 | if (intent != null) { |
| 204 | mismatchDescription.appendText("was "). |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | appendText(intent.getState().toString()); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 206 | } else { |
| 207 | mismatchDescription.appendText("that intent was not found"); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /** |
| 214 | * Factory method to create a Matcher for an IntentMap that looks for an |
| 215 | * Intent with a given id and state. |
| 216 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 217 | * @param id id of the Intent to match |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 218 | * @param state if the Intent is found, its state must match this |
| 219 | * @return Matcher object |
| 220 | */ |
| 221 | @Factory |
| 222 | public static Matcher<IntentMap> hasIntentWithIdAndState(String id, |
| 223 | IntentState state) { |
| 224 | return new IntentsHaveIntentWithStateMatcher(id, state); |
| 225 | } |
| 226 | |
| 227 | |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 228 | @After |
| 229 | public void tearDown() { |
| 230 | verify(datagridService); |
| 231 | verify(networkGraphService); |
| 232 | verify(modContext); |
| 233 | verify(controllerRegistryService); |
| 234 | PowerMock.verify(persistIntent, PersistIntent.class); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Test the result of executing a path calculation on an |
| 239 | * Intent Operation List which contains a path that references a |
| 240 | * non-existent switch. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 241 | * <p/> |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 242 | * A 3 path list is created where one of the paths references a switch |
| 243 | * that is not in the topology. The test checks that the resulting |
| 244 | * Operation List has entries for the 2 correct paths, and that the |
| 245 | * high level intents contain a proper error entry for the bad path. |
| 246 | */ |
| 247 | @Test |
| 248 | public void testInvalidSwitchName() throws FloodlightModuleException { |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 249 | final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent"; |
| 250 | |
| 251 | // create shortest path intents |
| 252 | final IntentOperationList opList = new IntentOperationList(); |
| 253 | opList.add(Operator.ADD, |
| 254 | new ShortestPathIntent(BAD_SWITCH_INTENT_NAME, 111L, 12L, |
| 255 | LOCAL_PORT, 2L, 21L, LOCAL_PORT)); |
| 256 | opList.add(Operator.ADD, |
| 257 | new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, |
| 258 | LOCAL_PORT)); |
| 259 | opList.add(Operator.ADD, |
| 260 | new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, |
| 261 | LOCAL_PORT)); |
| 262 | |
| 263 | // compile high-level intent operations into low-level intent |
| 264 | // operations (calculate paths) |
| 265 | final PathCalcRuntimeModule runtime = new PathCalcRuntimeModule(); |
| 266 | runtime.init(modContext); |
| 267 | runtime.startUp(modContext); |
| 268 | final IntentOperationList pathIntentOpList = |
| 269 | runtime.executeIntentOperations(opList); |
| 270 | assertThat(pathIntentOpList, notNullValue()); |
| 271 | |
| 272 | final IntentMap highLevelIntents = runtime.getHighLevelIntents(); |
| 273 | assertThat(highLevelIntents, notNullValue()); |
| 274 | |
| 275 | final Collection<Intent> allIntents = highLevelIntents.getAllIntents(); |
| 276 | assertThat(allIntents, notNullValue()); |
| 277 | |
| 278 | // One intent had an error and should not create a path list entry |
| 279 | assertThat(pathIntentOpList, hasSize(opList.size() - 1)); |
| 280 | |
| 281 | // Should be a high level intent for each operation |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 282 | assertThat(allIntents, hasSize(opList.size())); |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 283 | |
| 284 | // Check that we got a high level intent for each operation |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 285 | assertThat(allIntents, hasIntentWithId("3")); |
| 286 | assertThat(allIntents, hasIntentWithId("2")); |
| 287 | assertThat(allIntents, hasIntentWithId(BAD_SWITCH_INTENT_NAME)); |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 288 | |
| 289 | // Check that the non existent switch was NACKed |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 290 | assertThat(highLevelIntents, hasIntentWithIdAndState(BAD_SWITCH_INTENT_NAME, IntentState.INST_NACK)); |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 291 | |
| 292 | // Check that switch 2 was correctly processed |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 293 | assertThat(highLevelIntents, hasIntentWithIdAndState("2", IntentState.INST_REQ)); |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 294 | |
| 295 | // Check that switch 3 was correctly processed |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 296 | assertThat(highLevelIntents, hasIntentWithIdAndState("3", IntentState.INST_REQ)); |
| 297 | |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /** |
| 302 | * Test the result of executing a path calculation on an |
| 303 | * Intent Operation List and then removing one of the switches. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 304 | * <p/> |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 305 | * A 3 path list is created and then one of the paths is removed. |
| 306 | * The test checks that the resulting Operation List is correct, |
| 307 | * and that the high level intents contain a proper "delete requested" |
| 308 | * entry for the deleted path. |
| 309 | */ |
| 310 | @Test |
| 311 | public void testIntentRemoval() throws FloodlightModuleException { |
| 312 | |
| 313 | // create shortest path intents |
| 314 | final IntentOperationList opList = new IntentOperationList(); |
| 315 | opList.add(Operator.ADD, |
| 316 | new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 317 | LOCAL_PORT)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 318 | opList.add(Operator.ADD, |
| 319 | new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 320 | LOCAL_PORT)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 321 | opList.add(Operator.ADD, |
| 322 | new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 323 | LOCAL_PORT)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 324 | |
| 325 | // compile high-level intent operations into low-level intent |
| 326 | // operations (calculate paths) |
| 327 | final PathCalcRuntimeModule runtime = new PathCalcRuntimeModule(); |
| 328 | runtime.init(modContext); |
| 329 | runtime.startUp(modContext); |
| 330 | final IntentOperationList pathIntentOpList = |
| 331 | runtime.executeIntentOperations(opList); |
| 332 | assertThat(pathIntentOpList, notNullValue()); |
| 333 | |
| 334 | final IntentMap highLevelIntents = runtime.getHighLevelIntents(); |
| 335 | assertThat(highLevelIntents, notNullValue()); |
| 336 | |
| 337 | final Collection<Intent> allIntents = highLevelIntents.getAllIntents(); |
| 338 | assertThat(allIntents, notNullValue()); |
| 339 | |
| 340 | // Should be one operation per path |
| 341 | assertThat(pathIntentOpList, hasSize(opList.size())); |
| 342 | |
| 343 | // Should be a high level intent for each operation |
| 344 | assertThat(allIntents, hasSize(opList.size())); |
| 345 | |
| 346 | // Check that we got a high level intent for each operation |
| 347 | assertThat(allIntents, hasIntentWithId("3")); |
| 348 | assertThat(allIntents, hasIntentWithId("2")); |
| 349 | assertThat(allIntents, hasIntentWithId("1")); |
| 350 | |
| 351 | // Check that switch 1 was correctly processed |
| 352 | assertThat(highLevelIntents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 353 | hasIntentWithIdAndState("1", IntentState.INST_REQ)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 354 | |
| 355 | // Check that switch 2 was correctly processed |
| 356 | assertThat(highLevelIntents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 357 | hasIntentWithIdAndState("2", IntentState.INST_REQ)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 358 | |
| 359 | // Check that switch 3 was correctly processed |
| 360 | assertThat(highLevelIntents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 361 | hasIntentWithIdAndState("3", IntentState.INST_REQ)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 362 | |
| 363 | // Now delete one path and check the results |
| 364 | final IntentOperationList opListForRemoval = new IntentOperationList(); |
| 365 | opListForRemoval.add(Operator.REMOVE, |
| 366 | new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, |
| 367 | LOCAL_PORT)); |
| 368 | |
| 369 | final IntentOperationList pathIntentOpListAfterRemoval = |
| 370 | runtime.executeIntentOperations(opListForRemoval); |
| 371 | assertThat(pathIntentOpListAfterRemoval, notNullValue()); |
| 372 | assertThat(pathIntentOpListAfterRemoval, hasSize(1)); |
| 373 | |
| 374 | // Check the high level intents. |
| 375 | final IntentMap highLevelIntentsAfterRemoval = runtime.getHighLevelIntents(); |
| 376 | assertThat(highLevelIntentsAfterRemoval, notNullValue()); |
| 377 | |
| 378 | final Collection<Intent> allIntentsAfterRemoval = highLevelIntentsAfterRemoval.getAllIntents(); |
| 379 | assertThat(allIntentsAfterRemoval, notNullValue()); |
| 380 | assertThat(allIntentsAfterRemoval, hasSize(3)); |
| 381 | |
| 382 | // Check that we got a high level intent for each operation |
| 383 | assertThat(allIntentsAfterRemoval, hasIntentWithId("3")); |
| 384 | assertThat(allIntentsAfterRemoval, hasIntentWithId("2")); |
| 385 | assertThat(allIntentsAfterRemoval, hasIntentWithId("1")); |
| 386 | |
| 387 | // Check the states of the high level intents |
| 388 | // Check that switch 1 was correctly processed |
| 389 | assertThat(highLevelIntents, |
| 390 | hasIntentWithIdAndState("1", IntentState.DEL_REQ)); |
| 391 | |
| 392 | // Check that switch 2 was correctly processed |
| 393 | assertThat(highLevelIntents, |
| 394 | hasIntentWithIdAndState("2", IntentState.INST_REQ)); |
| 395 | |
| 396 | // Check that switch 3 was correctly processed |
| 397 | assertThat(highLevelIntents, |
| 398 | hasIntentWithIdAndState("3", IntentState.INST_REQ)); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Test the result of executing a path calculation on an |
| 403 | * Intent Operation List and then forcing a reroute. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 404 | * <p/> |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 405 | * A 3 path list is created and then one of the links is removed. |
| 406 | * The test checks that the resulting Operation List is correct, |
| 407 | * and that the high level intents contain a proper "reroute requested" |
| 408 | * entry for the deleted link. |
| 409 | */ |
| 410 | @Test |
| 411 | public void testIntentReroute() throws FloodlightModuleException { |
| 412 | |
| 413 | // create shortest path intents |
| 414 | final IntentOperationList opList = new IntentOperationList(); |
| 415 | final ShortestPathIntent pathIntent1 = |
| 416 | new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 417 | LOCAL_PORT); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 418 | |
| 419 | opList.add(Operator.ADD, pathIntent1); |
| 420 | opList.add(Operator.ADD, |
| 421 | new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 422 | LOCAL_PORT)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 423 | opList.add(Operator.ADD, |
| 424 | new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 425 | LOCAL_PORT)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 426 | |
| 427 | // compile high-level intent operations into low-level intent |
| 428 | // operations (calculate paths) |
| 429 | final PathCalcRuntimeModule runtime = new PathCalcRuntimeModule(); |
| 430 | runtime.init(modContext); |
| 431 | runtime.startUp(modContext); |
| 432 | final IntentOperationList pathIntentOpList = |
| 433 | runtime.executeIntentOperations(opList); |
| 434 | assertThat(pathIntentOpList, notNullValue()); |
| 435 | |
| 436 | final IntentMap highLevelIntents = runtime.getHighLevelIntents(); |
| 437 | assertThat(highLevelIntents, notNullValue()); |
| 438 | |
| 439 | final Collection<Intent> allIntents = highLevelIntents.getAllIntents(); |
| 440 | assertThat(allIntents, notNullValue()); |
| 441 | |
| 442 | // Should be one operation per path |
| 443 | assertThat(pathIntentOpList, hasSize(opList.size())); |
| 444 | |
| 445 | // Should be a high level intent for each operation |
| 446 | assertThat(allIntents, hasSize(opList.size())); |
| 447 | |
| 448 | // Check that we got a high level intent for each operation |
| 449 | assertThat(allIntents, hasIntentWithId("3")); |
| 450 | assertThat(allIntents, hasIntentWithId("2")); |
| 451 | assertThat(allIntents, hasIntentWithId("1")); |
| 452 | |
| 453 | // Check that switch 1 was correctly processed |
| 454 | assertThat(highLevelIntents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 455 | hasIntentWithIdAndState("1", IntentState.INST_REQ)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 456 | |
| 457 | // Check that switch 2 was correctly processed |
| 458 | assertThat(highLevelIntents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 459 | hasIntentWithIdAndState("2", IntentState.INST_REQ)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 460 | |
| 461 | // Check that switch 3 was correctly processed |
| 462 | assertThat(highLevelIntents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 463 | hasIntentWithIdAndState("3", IntentState.INST_REQ)); |
Ray Milkey | dc659c4 | 2014-03-28 16:30:42 -0700 | [diff] [blame] | 464 | |
| 465 | // Now add a different path to one of the switches path and check |
| 466 | // the results |
| 467 | IntentStateList states = new IntentStateList(); |
| 468 | states.put("1", IntentState.INST_ACK); |
| 469 | states.put("2", IntentState.INST_ACK); |
| 470 | states.put("3", IntentState.INST_ACK); |
| 471 | runtime.getHighLevelIntents().changeStates(states); |
| 472 | states.clear(); |
| 473 | states.put("1___0", IntentState.INST_ACK); |
| 474 | states.put("2___0", IntentState.INST_ACK); |
| 475 | states.put("3___0", IntentState.INST_ACK); |
| 476 | runtime.getPathIntents().changeStates(states); |
| 477 | |
| 478 | final List<SwitchEvent> emptySwitchEvents = new LinkedList<>(); |
| 479 | final List<PortEvent> emptyPortEvents = new LinkedList<>(); |
| 480 | final List<DeviceEvent> emptyDeviceEvents = new LinkedList<>(); |
| 481 | final List<LinkEvent> addedLinkEvents = new LinkedList<>(); |
| 482 | final List<LinkEvent> removedLinkEvents = new LinkedList<>(); |
| 483 | |
| 484 | graph.removeLink(1L, 12L, 2L, 21L); // This link is used by the intent "1" |
| 485 | graph.removeLink(2L, 21L, 1L, 12L); |
| 486 | LinkEvent linkEvent1 = new LinkEvent(1L, 12L, 2L, 21L); |
| 487 | LinkEvent linkEvent2 = new LinkEvent(2L, 21L, 1L, 12L); |
| 488 | removedLinkEvents.add(linkEvent1); |
| 489 | removedLinkEvents.add(linkEvent2); |
| 490 | runtime.networkGraphEvents( |
| 491 | emptySwitchEvents, |
| 492 | emptySwitchEvents, |
| 493 | emptyPortEvents, |
| 494 | emptyPortEvents, |
| 495 | addedLinkEvents, |
| 496 | removedLinkEvents, |
| 497 | emptyDeviceEvents, |
| 498 | emptyDeviceEvents); |
| 499 | final IntentOperationList opListForReroute = new IntentOperationList(); |
| 500 | opListForReroute.add(Operator.ADD, pathIntent1); |
| 501 | |
| 502 | final IntentOperationList pathIntentOpListAfterReroute = |
| 503 | runtime.executeIntentOperations(opListForReroute); |
| 504 | assertThat(pathIntentOpListAfterReroute, notNullValue()); |
| 505 | assertThat(pathIntentOpListAfterReroute, hasSize(2)); |
| 506 | |
| 507 | // Check the high level intents. |
| 508 | final IntentMap highLevelIntentsAfterReroute = runtime.getHighLevelIntents(); |
| 509 | assertThat(highLevelIntentsAfterReroute, notNullValue()); |
| 510 | |
| 511 | final Collection<Intent> allIntentsAfterReroute = highLevelIntentsAfterReroute.getAllIntents(); |
| 512 | assertThat(allIntentsAfterReroute, notNullValue()); |
| 513 | assertThat(allIntentsAfterReroute, hasSize(3)); |
| 514 | |
| 515 | // Check that we got a high level intent for each operation |
| 516 | assertThat(allIntentsAfterReroute, hasIntentWithId("3")); |
| 517 | assertThat(allIntentsAfterReroute, hasIntentWithId("2")); |
| 518 | assertThat(allIntentsAfterReroute, hasIntentWithId("1")); |
| 519 | |
| 520 | // Check the states of the high level intents |
| 521 | // Check that switch 1 was correctly processed |
| 522 | assertThat(highLevelIntents, |
| 523 | hasIntentWithIdAndState("1", IntentState.REROUTE_REQ)); |
| 524 | |
| 525 | // Check that switch 2 was correctly processed |
| 526 | assertThat(highLevelIntents, |
| 527 | hasIntentWithIdAndState("2", IntentState.INST_ACK)); |
| 528 | |
| 529 | // Check that switch 3 was correctly processed |
| 530 | assertThat(highLevelIntents, |
| 531 | hasIntentWithIdAndState("3", IntentState.INST_ACK)); |
| 532 | |
Ray Milkey | 6688cd8 | 2014-03-11 16:40:46 -0700 | [diff] [blame] | 533 | |
| 534 | } |
| 535 | |
| 536 | |
| 537 | } |