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