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