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