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