blob: 5ac60278d24850a75447edc6c0669e30a6e8dbca [file] [log] [blame]
Toshio Koidec87810e2014-02-11 13:03:21 -08001package net.onrc.onos.intent.runtime;
2
Toshio Koide066506e2014-02-20 19:52:09 -08003import static org.easymock.EasyMock.*;
4
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08005import java.util.LinkedList;
Brian O'Connor12861f72014-02-19 20:40:32 -08006import java.util.List;
7import java.util.Set;
8
Toshio Koide27ffd412014-02-18 19:15:27 -08009import net.floodlightcontroller.core.module.FloodlightModuleContext;
10import net.floodlightcontroller.core.module.FloodlightModuleException;
11import net.onrc.onos.datagrid.IDatagridService;
12import net.onrc.onos.datagrid.IEventChannel;
Toshio Koide066506e2014-02-20 19:52:09 -080013import net.onrc.onos.datagrid.IEventChannelListener;
Toshio Koidec87810e2014-02-11 13:03:21 -080014import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Brian O'Connor12861f72014-02-19 20:40:32 -080015import net.onrc.onos.intent.FlowEntry;
Toshio Koidec87810e2014-02-11 13:03:21 -080016import net.onrc.onos.intent.Intent;
Toshio Koidea10c0372014-02-20 17:28:10 -080017import net.onrc.onos.intent.Intent.IntentState;
Toshio Koide27ffd412014-02-18 19:15:27 -080018import net.onrc.onos.intent.IntentOperation.Operator;
19import net.onrc.onos.intent.IntentOperationList;
Toshio Koideebdbb622014-02-12 20:28:38 -080020import net.onrc.onos.intent.MockNetworkGraph;
Toshio Koidec87810e2014-02-11 13:03:21 -080021import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080022import net.onrc.onos.intent.PathIntentMap;
Toshio Koidec87810e2014-02-11 13:03:21 -080023import net.onrc.onos.intent.ShortestPathIntent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080024import net.onrc.onos.intent.persist.PersistIntent;
Toshio Koide798bc1b2014-02-20 14:02:40 -080025import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080026import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080027import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080028import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koide798bc1b2014-02-20 14:02:40 -080029import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -080030import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
31import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080032import net.onrc.onos.registry.controller.IControllerRegistryService;
Toshio Koidec87810e2014-02-11 13:03:21 -080033
34import org.junit.After;
35import org.junit.Before;
36import org.junit.Test;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080037import org.junit.runner.RunWith;
38import org.powermock.api.easymock.PowerMock;
39import org.powermock.core.classloader.annotations.PrepareForTest;
40import org.powermock.modules.junit4.PowerMockRunner;
Toshio Koidec87810e2014-02-11 13:03:21 -080041
42/**
43 * @author Toshio Koide (t-koide@onlab.us)
44 */
Nick Karanatsios8abe7172014-02-19 20:31:48 -080045@RunWith(PowerMockRunner.class)
46@PrepareForTest(PathCalcRuntimeModule.class)
Toshio Koidec87810e2014-02-11 13:03:21 -080047public class UseCaseTest {
Toshio Koide27ffd412014-02-18 19:15:27 -080048 private NetworkGraph g;
49 private FloodlightModuleContext modContext;
50 private IDatagridService datagridService;
51 private INetworkGraphService networkGraphService;
Toshio Koide798bc1b2014-02-20 14:02:40 -080052 private IControllerRegistryService controllerRegistryService;
53 private PersistIntent persistIntent;
Toshio Koide27ffd412014-02-18 19:15:27 -080054 @SuppressWarnings("rawtypes")
55 private IEventChannel eventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080056
Toshio Koidefe1d5d92014-02-26 20:09:48 -080057 private static Long LOCAL_PORT = 0xFFFEL;
58
Toshio Koide27ffd412014-02-18 19:15:27 -080059 @SuppressWarnings("unchecked")
Toshio Koidec87810e2014-02-11 13:03:21 -080060 @Before
Nick Karanatsios8abe7172014-02-19 20:31:48 -080061 public void setUp() throws Exception {
Toshio Koideebdbb622014-02-12 20:28:38 -080062 MockNetworkGraph graph = new MockNetworkGraph();
Toshio Koidefe1d5d92014-02-26 20:09:48 -080063 graph.createSampleTopology1();
Toshio Koideebdbb622014-02-12 20:28:38 -080064 g = graph;
Toshio Koide27ffd412014-02-18 19:15:27 -080065
Toshio Koide066506e2014-02-20 19:52:09 -080066 datagridService = createMock(IDatagridService.class);
67 networkGraphService = createMock(INetworkGraphService.class);
68 controllerRegistryService = createMock(IControllerRegistryService.class);
69 modContext = createMock(FloodlightModuleContext.class);
70 eventChannel = createMock(IEventChannel.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -080071 persistIntent = PowerMock.createMock(PersistIntent.class);
72
73 PowerMock.expectNew(PersistIntent.class,
Toshio Koide066506e2014-02-20 19:52:09 -080074 anyObject(IControllerRegistryService.class),
75 anyObject(INetworkGraphService.class)).andReturn(persistIntent);
Toshio Koide27ffd412014-02-18 19:15:27 -080076
Toshio Koide066506e2014-02-20 19:52:09 -080077 expect(modContext.getServiceImpl(IDatagridService.class))
Toshio Koide27ffd412014-02-18 19:15:27 -080078 .andReturn(datagridService).once();
Toshio Koide066506e2014-02-20 19:52:09 -080079 expect(modContext.getServiceImpl(INetworkGraphService.class))
Toshio Koide27ffd412014-02-18 19:15:27 -080080 .andReturn(networkGraphService).once();
Toshio Koide066506e2014-02-20 19:52:09 -080081 expect(modContext.getServiceImpl(IControllerRegistryService.class))
Toshio Koide798bc1b2014-02-20 14:02:40 -080082 .andReturn(controllerRegistryService).once();
Toshio Koide066506e2014-02-20 19:52:09 -080083 expect(persistIntent.getKey()).andReturn(1L).anyTimes();
84 expect(persistIntent.persistIfLeader(eq(1L),
85 anyObject(IntentOperationList.class))).andReturn(true).anyTimes();
Toshio Koide0c9106d2014-02-19 15:26:38 -080086
Toshio Koide066506e2014-02-20 19:52:09 -080087 expect(networkGraphService.getNetworkGraph()).andReturn(g).anyTimes();
88 networkGraphService.registerNetworkGraphListener(anyObject(INetworkGraphListener.class));
89 expectLastCall();
Toshio Koide0c9106d2014-02-19 15:26:38 -080090
Toshio Koide066506e2014-02-20 19:52:09 -080091 expect(datagridService.createChannel("onos.pathintent", Long.class, IntentOperationList.class))
Toshio Koide27ffd412014-02-18 19:15:27 -080092 .andReturn(eventChannel).once();
93
Toshio Koide066506e2014-02-20 19:52:09 -080094 expect(datagridService.addListener(
95 eq("onos.pathintent_state"),
96 anyObject(IEventChannelListener.class),
97 eq(Long.class),
98 eq(IntentStateList.class)))
99 .andReturn(eventChannel).once();
100
101 replay(datagridService);
102 replay(networkGraphService);
103 replay(modContext);
104 replay(controllerRegistryService);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800105 PowerMock.replay(persistIntent, PersistIntent.class);
Toshio Koidec87810e2014-02-11 13:03:21 -0800106 }
107
108 @After
109 public void tearDown() {
Toshio Koide066506e2014-02-20 19:52:09 -0800110 verify(datagridService);
111 verify(networkGraphService);
112 verify(modContext);
113 verify(controllerRegistryService);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800114 PowerMock.verify(persistIntent, PersistIntent.class);
Toshio Koidec87810e2014-02-11 13:03:21 -0800115 }
116
Toshio Koide4f308732014-02-18 15:19:48 -0800117 private void showResult(PathIntentMap intents) {
118 for (Intent intent: intents.getAllIntents()) {
119 PathIntent pathIntent = (PathIntent)intent;
Toshio Koidebf875662014-02-24 12:19:15 -0800120 System.out.println("Path intent:" + pathIntent);
Toshio Koidec87810e2014-02-11 13:03:21 -0800121 System.out.println("Parent intent: " + pathIntent.getParentIntent().toString());
Toshio Koidec87810e2014-02-11 13:03:21 -0800122 }
123 }
124
125 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800126 public void createShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800127 // create shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800128 IntentOperationList opList = new IntentOperationList();
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800129 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
130 opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
131 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
Toshio Koidec87810e2014-02-11 13:03:21 -0800132
Toshio Koide27ffd412014-02-18 19:15:27 -0800133 // compile high-level intent operations into low-level intent operations (calculate paths)
134 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
135 runtime1.init(modContext);
136 runtime1.startUp(modContext);
137 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
138
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800139 // compile low-level intents into flow entry installation plan
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800140 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
Brian O'Connor12861f72014-02-19 20:40:32 -0800141 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800142
143 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800144 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800145 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800146 }
147
148 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800149 public void createConstrainedShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800150 // create constrained shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800151 IntentOperationList opList = new IntentOperationList();
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800152 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT, 400.0));
153 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT, 400.0));
154 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 2L, 24L, LOCAL_PORT, 4L, 42L, LOCAL_PORT, 400.0));
155 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT, 400.0));
156 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 3L, 34L, LOCAL_PORT, 4L, 43L, LOCAL_PORT, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800157
Toshio Koide27ffd412014-02-18 19:15:27 -0800158 // compile high-level intent operations into low-level intent operations (calculate paths)
159 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
160 runtime1.init(modContext);
161 runtime1.startUp(modContext);
162 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800163
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800164 // compile low-level intents into flow entry installation plan
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800165 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
Brian O'Connor12861f72014-02-19 20:40:32 -0800166 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800167
Toshio Koidec87810e2014-02-11 13:03:21 -0800168 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800169 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800170 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800171 }
172
173 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800174 public void createMixedShortestPaths() throws FloodlightModuleException {
175 // create constrained & best effort shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800176 IntentOperationList opList = new IntentOperationList();
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800177 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT, 400.0));
178 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT, 400.0));
179 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 24L, LOCAL_PORT, 4L, 42L, LOCAL_PORT));
180 opList.add(Operator.ADD, new ShortestPathIntent("4", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
181 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 3L, 34L, LOCAL_PORT, 4L, 43L, LOCAL_PORT, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800182
Toshio Koide27ffd412014-02-18 19:15:27 -0800183 // compile high-level intent operations into low-level intent operations (calculate paths)
184 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
185 runtime1.init(modContext);
186 runtime1.startUp(modContext);
187 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800188
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800189 // compile low-level intents into flow entry installation plan
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800190 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
Brian O'Connor12861f72014-02-19 20:40:32 -0800191 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800192
Toshio Koidec87810e2014-02-11 13:03:21 -0800193 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800194 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800195 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800196 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800197
Toshio Koide0c9106d2014-02-19 15:26:38 -0800198 @Test
199 public void rerouteShortestPaths() throws FloodlightModuleException {
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800200 List<SwitchEvent> addedSwitchEvents = new LinkedList<>();
201 List<SwitchEvent> removedSwitchEvents = new LinkedList<>();
202 List<PortEvent> addedPortEvents = new LinkedList<>();
203 List<PortEvent> removedPortEvents = new LinkedList<>();
204 List<LinkEvent> addedLinkEvents = new LinkedList<>();
205 List<LinkEvent> removedLinkEvents = new LinkedList<>();
206 List<DeviceEvent> addedDeviceEvents = new LinkedList<>();
207 List<DeviceEvent> removedDeviceEvents = new LinkedList<>();
208
Toshio Koide0c9106d2014-02-19 15:26:38 -0800209 // create shortest path intents
210 IntentOperationList opList = new IntentOperationList();
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800211 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
212 opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
213 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
Toshio Koide0c9106d2014-02-19 15:26:38 -0800214
215 // compile high-level intent operations into low-level intent operations (calculate paths)
216 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
217 runtime1.init(modContext);
218 runtime1.startUp(modContext);
219 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
220
221 // compile low-level intents into flow entry installation plan
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800222 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
Brian O'Connor12861f72014-02-19 20:40:32 -0800223 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800224
225 // show results step1
226 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800227 System.out.println(plan);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800228
Toshio Koidea10c0372014-02-20 17:28:10 -0800229 // TODO this state changes should be triggered by notification of plan module
Toshio Koide066506e2014-02-20 19:52:09 -0800230 IntentStateList states = new IntentStateList();
Toshio Koidea10c0372014-02-20 17:28:10 -0800231 states.put("1", IntentState.INST_ACK);
232 states.put("2", IntentState.INST_ACK);
233 states.put("3", IntentState.INST_ACK);
234 runtime1.getHighLevelIntents().changeStates(states);
Toshio Koidebf875662014-02-24 12:19:15 -0800235 states.clear();
236 states.put("1___0", IntentState.INST_ACK);
237 states.put("2___0", IntentState.INST_ACK);
238 states.put("3___0", IntentState.INST_ACK);
239 runtime1.getPathIntents().changeStates(states);
Toshio Koidea10c0372014-02-20 17:28:10 -0800240
Toshio Koide0c9106d2014-02-19 15:26:38 -0800241 // link down
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800242 ((MockNetworkGraph)g).removeLink(1L, 12L, 2L, 21L); // This link is used by the intent "1"
243 ((MockNetworkGraph)g).removeLink(2L, 21L, 1L, 12L);
244 LinkEvent linkEvent1 = new LinkEvent(1L, 12L, 2L, 21L);
245 LinkEvent linkEvent2 = new LinkEvent(2L, 21L, 1L, 12L);
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800246 removedLinkEvents.clear();
Toshio Koidea10c0372014-02-20 17:28:10 -0800247 removedLinkEvents.add(linkEvent1);
248 removedLinkEvents.add(linkEvent2);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800249 runtime1.networkGraphEvents(
250 addedSwitchEvents,
251 removedSwitchEvents,
252 addedPortEvents,
253 removedPortEvents,
254 addedLinkEvents,
255 removedLinkEvents,
256 addedDeviceEvents,
257 removedDeviceEvents);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800258 System.out.println("Link goes down.");
259
260 // show results step2
261 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800262 // TODO: show results of plan computation
Toshio Koide0c9106d2014-02-19 15:26:38 -0800263 }
Toshio Koidefa735a12014-03-28 10:49:07 -0700264
265
266 @Test
267 public void createAndRemoveShortestPaths() throws FloodlightModuleException {
268 // create shortest path intents
269 IntentOperationList opList = new IntentOperationList();
270 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
271 opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
272 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
273
274 // compile high-level intent operations into low-level intent operations (calculate paths)
275 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
276 runtime1.init(modContext);
277 runtime1.startUp(modContext);
278 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
279
280 // compile low-level intents into flow entry installation plan
281 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
282 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
283
284 // show results
285 showResult((PathIntentMap) runtime1.getPathIntents());
286 System.out.println(plan);
287
288 // create remove operations
289 opList.clear();
290 opList.add(Operator.REMOVE, new Intent("1"));
291 opList.add(Operator.REMOVE, new Intent("2"));
292
293 // compile
294 runtime1.executeIntentOperations(opList);
295
296 // show results
297 showResult((PathIntentMap) runtime1.getPathIntents());
298 System.out.println(plan);
299 }
300
Toshio Koidec87810e2014-02-11 13:03:21 -0800301}