blob: 0e58739b4c0b7782a15580297d064b2b0296401c [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent.runtime;
Toshio Koidec87810e2014-02-11 13:03:21 -08002
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.easymock.EasyMock.anyObject;
4import static org.easymock.EasyMock.createMock;
5import static org.easymock.EasyMock.eq;
6import static org.easymock.EasyMock.expect;
7import static org.easymock.EasyMock.expectLastCall;
8import static org.easymock.EasyMock.replay;
9import static org.easymock.EasyMock.verify;
Toshio Koide066506e2014-02-20 19:52:09 -080010
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -080011import java.util.LinkedList;
Brian O'Connor12861f72014-02-19 20:40:32 -080012import java.util.List;
13import java.util.Set;
14
Toshio Koide27ffd412014-02-18 19:15:27 -080015import net.floodlightcontroller.core.module.FloodlightModuleContext;
16import net.floodlightcontroller.core.module.FloodlightModuleException;
Jonathan Hart6df90172014-04-03 10:13:11 -070017import net.onrc.onos.core.datagrid.IDatagridService;
18import net.onrc.onos.core.datagrid.IEventChannel;
19import net.onrc.onos.core.datagrid.IEventChannelListener;
Jonathan Hartaa380972014-04-03 10:24:46 -070020import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
21import net.onrc.onos.core.intent.FlowEntry;
22import net.onrc.onos.core.intent.Intent;
Jonathan Harta88fd242014-04-03 11:24:54 -070023import net.onrc.onos.core.intent.Intent.IntentState;
24import net.onrc.onos.core.intent.IntentOperation.Operator;
Jonathan Hartaa380972014-04-03 10:24:46 -070025import net.onrc.onos.core.intent.IntentOperationList;
26import net.onrc.onos.core.intent.MockNetworkGraph;
27import net.onrc.onos.core.intent.PathIntent;
28import net.onrc.onos.core.intent.PathIntentMap;
29import net.onrc.onos.core.intent.ShortestPathIntent;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070030import net.onrc.onos.core.registry.IControllerRegistryService;
Jonathan Hart472062d2014-04-03 10:56:48 -070031import net.onrc.onos.core.topology.DeviceEvent;
32import net.onrc.onos.core.topology.INetworkGraphListener;
33import net.onrc.onos.core.topology.INetworkGraphService;
34import net.onrc.onos.core.topology.LinkEvent;
35import net.onrc.onos.core.topology.NetworkGraph;
36import net.onrc.onos.core.topology.PortEvent;
37import net.onrc.onos.core.topology.SwitchEvent;
Toshio Koidec87810e2014-02-11 13:03:21 -080038
39import org.junit.After;
40import org.junit.Before;
41import org.junit.Test;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080042import org.junit.runner.RunWith;
43import org.powermock.api.easymock.PowerMock;
44import org.powermock.core.classloader.annotations.PrepareForTest;
45import org.powermock.modules.junit4.PowerMockRunner;
Toshio Koidec87810e2014-02-11 13:03:21 -080046
47/**
48 * @author Toshio Koide (t-koide@onlab.us)
49 */
Nick Karanatsios8abe7172014-02-19 20:31:48 -080050@RunWith(PowerMockRunner.class)
51@PrepareForTest(PathCalcRuntimeModule.class)
Toshio Koidec87810e2014-02-11 13:03:21 -080052public class UseCaseTest {
Ray Milkey269ffb92014-04-03 14:43:30 -070053 private NetworkGraph g;
54 private FloodlightModuleContext modContext;
55 private IDatagridService datagridService;
56 private INetworkGraphService networkGraphService;
57 private IControllerRegistryService controllerRegistryService;
58 private PersistIntent persistIntent;
59 @SuppressWarnings("rawtypes")
60 private IEventChannel eventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 private static Long LOCAL_PORT = 0xFFFEL;
Toshio Koidefe1d5d92014-02-26 20:09:48 -080063
Ray Milkey269ffb92014-04-03 14:43:30 -070064 @SuppressWarnings("unchecked")
65 @Before
66 public void setUp() throws Exception {
67 MockNetworkGraph graph = new MockNetworkGraph();
68 graph.createSampleTopology1();
69 g = graph;
Toshio Koide27ffd412014-02-18 19:15:27 -080070
Ray Milkey269ffb92014-04-03 14:43:30 -070071 datagridService = createMock(IDatagridService.class);
72 networkGraphService = createMock(INetworkGraphService.class);
73 controllerRegistryService = createMock(IControllerRegistryService.class);
74 modContext = createMock(FloodlightModuleContext.class);
75 eventChannel = createMock(IEventChannel.class);
76 persistIntent = PowerMock.createMock(PersistIntent.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -080077
Ray Milkey269ffb92014-04-03 14:43:30 -070078 PowerMock.expectNew(PersistIntent.class,
Pavlin Radoslavov0294e052014-04-10 13:36:45 -070079 anyObject(IControllerRegistryService.class)).andReturn(persistIntent);
Toshio Koide27ffd412014-02-18 19:15:27 -080080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 expect(modContext.getServiceImpl(IDatagridService.class))
82 .andReturn(datagridService).once();
83 expect(modContext.getServiceImpl(INetworkGraphService.class))
84 .andReturn(networkGraphService).once();
85 expect(modContext.getServiceImpl(IControllerRegistryService.class))
86 .andReturn(controllerRegistryService).once();
87 expect(persistIntent.getKey()).andReturn(1L).anyTimes();
88 expect(persistIntent.persistIfLeader(eq(1L),
89 anyObject(IntentOperationList.class))).andReturn(true).anyTimes();
Toshio Koide0c9106d2014-02-19 15:26:38 -080090
Ray Milkey269ffb92014-04-03 14:43:30 -070091 expect(networkGraphService.getNetworkGraph()).andReturn(g).anyTimes();
92 networkGraphService.registerNetworkGraphListener(anyObject(INetworkGraphListener.class));
93 expectLastCall();
Toshio Koide0c9106d2014-02-19 15:26:38 -080094
Ray Milkey269ffb92014-04-03 14:43:30 -070095 expect(datagridService.createChannel("onos.pathintent", Long.class, IntentOperationList.class))
96 .andReturn(eventChannel).once();
Toshio Koide27ffd412014-02-18 19:15:27 -080097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 expect(datagridService.addListener(
99 eq("onos.pathintent_state"),
100 anyObject(IEventChannelListener.class),
101 eq(Long.class),
102 eq(IntentStateList.class)))
103 .andReturn(eventChannel).once();
Toshio Koide066506e2014-02-20 19:52:09 -0800104
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 replay(datagridService);
106 replay(networkGraphService);
107 replay(modContext);
108 replay(controllerRegistryService);
109 PowerMock.replay(persistIntent, PersistIntent.class);
110 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800111
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 @After
113 public void tearDown() {
114 verify(datagridService);
115 verify(networkGraphService);
116 verify(modContext);
117 verify(controllerRegistryService);
118 PowerMock.verify(persistIntent, PersistIntent.class);
119 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800120
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 private void showResult(PathIntentMap intents) {
122 for (Intent intent : intents.getAllIntents()) {
123 PathIntent pathIntent = (PathIntent) intent;
124 System.out.println("Path intent:" + pathIntent);
125 System.out.println("Parent intent: " + pathIntent.getParentIntent().toString());
126 }
127 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800128
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 @Test
130 public void createShortestPaths() throws FloodlightModuleException {
131 // create shortest path intents
132 IntentOperationList opList = new IntentOperationList();
133 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
134 opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
135 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
Toshio Koidec87810e2014-02-11 13:03:21 -0800136
Ray Milkey269ffb92014-04-03 14:43:30 -0700137 // compile high-level intent operations into low-level intent operations (calculate paths)
138 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
139 runtime1.init(modContext);
140 runtime1.startUp(modContext);
141 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koide27ffd412014-02-18 19:15:27 -0800142
Ray Milkey269ffb92014-04-03 14:43:30 -0700143 // compile low-level intents into flow entry installation plan
144 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
145 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800146
Ray Milkey269ffb92014-04-03 14:43:30 -0700147 // show results
148 showResult((PathIntentMap) runtime1.getPathIntents());
149 System.out.println(plan);
150 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800151
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 @Test
153 public void createConstrainedShortestPaths() throws FloodlightModuleException {
154 // create constrained shortest path intents
155 IntentOperationList opList = new IntentOperationList();
156 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT, 400.0));
157 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT, 400.0));
158 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 2L, 24L, LOCAL_PORT, 4L, 42L, LOCAL_PORT, 400.0));
159 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT, 400.0));
160 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 3L, 34L, LOCAL_PORT, 4L, 43L, LOCAL_PORT, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800161
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 // compile high-level intent operations into low-level intent operations (calculate paths)
163 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
164 runtime1.init(modContext);
165 runtime1.startUp(modContext);
166 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800167
Ray Milkey269ffb92014-04-03 14:43:30 -0700168 // compile low-level intents into flow entry installation plan
169 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
170 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800171
Ray Milkey269ffb92014-04-03 14:43:30 -0700172 // show results
173 showResult((PathIntentMap) runtime1.getPathIntents());
174 System.out.println(plan);
175 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800176
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 @Test
178 public void createMixedShortestPaths() throws FloodlightModuleException {
179 // create constrained & best effort shortest path intents
180 IntentOperationList opList = new IntentOperationList();
181 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT, 400.0));
182 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT, 400.0));
183 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 24L, LOCAL_PORT, 4L, 42L, LOCAL_PORT));
184 opList.add(Operator.ADD, new ShortestPathIntent("4", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
185 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 3L, 34L, LOCAL_PORT, 4L, 43L, LOCAL_PORT, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800186
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 // compile high-level intent operations into low-level intent operations (calculate paths)
188 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
189 runtime1.init(modContext);
190 runtime1.startUp(modContext);
191 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800192
Ray Milkey269ffb92014-04-03 14:43:30 -0700193 // compile low-level intents into flow entry installation plan
194 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
195 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800196
Ray Milkey269ffb92014-04-03 14:43:30 -0700197 // show results
198 showResult((PathIntentMap) runtime1.getPathIntents());
199 System.out.println(plan);
200 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800201
Ray Milkey269ffb92014-04-03 14:43:30 -0700202 @Test
203 public void rerouteShortestPaths() throws FloodlightModuleException {
204 List<SwitchEvent> addedSwitchEvents = new LinkedList<>();
205 List<SwitchEvent> removedSwitchEvents = new LinkedList<>();
206 List<PortEvent> addedPortEvents = new LinkedList<>();
207 List<PortEvent> removedPortEvents = new LinkedList<>();
208 List<LinkEvent> addedLinkEvents = new LinkedList<>();
209 List<LinkEvent> removedLinkEvents = new LinkedList<>();
210 List<DeviceEvent> addedDeviceEvents = new LinkedList<>();
211 List<DeviceEvent> removedDeviceEvents = new LinkedList<>();
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800212
Ray Milkey269ffb92014-04-03 14:43:30 -0700213 // create shortest path intents
214 IntentOperationList opList = new IntentOperationList();
215 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
216 opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
217 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
Toshio Koide0c9106d2014-02-19 15:26:38 -0800218
Ray Milkey269ffb92014-04-03 14:43:30 -0700219 // compile high-level intent operations into low-level intent operations (calculate paths)
220 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
221 runtime1.init(modContext);
222 runtime1.startUp(modContext);
223 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800224
Ray Milkey269ffb92014-04-03 14:43:30 -0700225 // compile low-level intents into flow entry installation plan
226 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
227 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800228
Ray Milkey269ffb92014-04-03 14:43:30 -0700229 // show results step1
230 showResult((PathIntentMap) runtime1.getPathIntents());
231 System.out.println(plan);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800232
Ray Milkey269ffb92014-04-03 14:43:30 -0700233 // TODO this state changes should be triggered by notification of plan module
234 IntentStateList states = new IntentStateList();
235 states.put("1", IntentState.INST_ACK);
236 states.put("2", IntentState.INST_ACK);
237 states.put("3", IntentState.INST_ACK);
238 runtime1.getHighLevelIntents().changeStates(states);
239 states.clear();
240 states.put("1___0", IntentState.INST_ACK);
241 states.put("2___0", IntentState.INST_ACK);
242 states.put("3___0", IntentState.INST_ACK);
243 runtime1.getPathIntents().changeStates(states);
Toshio Koidea10c0372014-02-20 17:28:10 -0800244
Ray Milkey269ffb92014-04-03 14:43:30 -0700245 // link down
246 ((MockNetworkGraph) g).removeLink(1L, 12L, 2L, 21L); // This link is used by the intent "1"
247 ((MockNetworkGraph) g).removeLink(2L, 21L, 1L, 12L);
248 LinkEvent linkEvent1 = new LinkEvent(1L, 12L, 2L, 21L);
249 LinkEvent linkEvent2 = new LinkEvent(2L, 21L, 1L, 12L);
250 removedLinkEvents.clear();
251 removedLinkEvents.add(linkEvent1);
252 removedLinkEvents.add(linkEvent2);
253 runtime1.networkGraphEvents(
254 addedSwitchEvents,
255 removedSwitchEvents,
256 addedPortEvents,
257 removedPortEvents,
258 addedLinkEvents,
259 removedLinkEvents,
260 addedDeviceEvents,
261 removedDeviceEvents);
262 System.out.println("Link goes down.");
Toshio Koide0c9106d2014-02-19 15:26:38 -0800263
Ray Milkey269ffb92014-04-03 14:43:30 -0700264 // show results step2
265 showResult((PathIntentMap) runtime1.getPathIntents());
266 // TODO: show results of plan computation
267 }
Toshio Koidefa735a12014-03-28 10:49:07 -0700268
269
Ray Milkey269ffb92014-04-03 14:43:30 -0700270 @Test
271 public void createAndRemoveShortestPaths() throws FloodlightModuleException {
272 // create shortest path intents
273 IntentOperationList opList = new IntentOperationList();
274 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
275 opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
276 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
Toshio Koidefa735a12014-03-28 10:49:07 -0700277
Ray Milkey269ffb92014-04-03 14:43:30 -0700278 // compile high-level intent operations into low-level intent operations (calculate paths)
279 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
280 runtime1.init(modContext);
281 runtime1.startUp(modContext);
282 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidefa735a12014-03-28 10:49:07 -0700283
Ray Milkey269ffb92014-04-03 14:43:30 -0700284 // compile low-level intents into flow entry installation plan
285 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
286 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koidefa735a12014-03-28 10:49:07 -0700287
Ray Milkey269ffb92014-04-03 14:43:30 -0700288 // show results
289 showResult((PathIntentMap) runtime1.getPathIntents());
290 System.out.println(plan);
Toshio Koidefa735a12014-03-28 10:49:07 -0700291
Ray Milkey269ffb92014-04-03 14:43:30 -0700292 // create remove operations
293 opList.clear();
294 opList.add(Operator.REMOVE, new Intent("1"));
295 opList.add(Operator.REMOVE, new Intent("2"));
Toshio Koidefa735a12014-03-28 10:49:07 -0700296
Ray Milkey269ffb92014-04-03 14:43:30 -0700297 // compile
298 runtime1.executeIntentOperations(opList);
Toshio Koidefa735a12014-03-28 10:49:07 -0700299
Ray Milkey269ffb92014-04-03 14:43:30 -0700300 // show results
301 showResult((PathIntentMap) runtime1.getPathIntents());
302 System.out.println(plan);
303 }
Toshio Koidefa735a12014-03-28 10:49:07 -0700304
Toshio Koidec87810e2014-02-11 13:03:21 -0800305}