blob: 4a0df3a3d670cb19669ff088ca1b3d68ac150ae7 [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/**
Toshio Koidee04a9df2014-05-01 15:49:28 -070048 * Temporary test cases for the ONS2014 demo.
49 * These test cases should be modified and be moved to appropriate classes
50 * (ex. PathCalcRuntimeModuleTest, PlanInstallModuleTest, etc.)
51 *
Toshio Koidec87810e2014-02-11 13:03:21 -080052 * @author Toshio Koide (t-koide@onlab.us)
53 */
Nick Karanatsios8abe7172014-02-19 20:31:48 -080054@RunWith(PowerMockRunner.class)
55@PrepareForTest(PathCalcRuntimeModule.class)
Toshio Koidec87810e2014-02-11 13:03:21 -080056public class UseCaseTest {
Ray Milkey269ffb92014-04-03 14:43:30 -070057 private NetworkGraph g;
58 private FloodlightModuleContext modContext;
59 private IDatagridService datagridService;
60 private INetworkGraphService networkGraphService;
61 private IControllerRegistryService controllerRegistryService;
62 private PersistIntent persistIntent;
Toshio Koidee04a9df2014-05-01 15:49:28 -070063 private IEventChannel<Long, IntentOperationList> intentOperationChannel;
64 private IEventChannel<Long, IntentStateList> intentStateChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 private static Long LOCAL_PORT = 0xFFFEL;
Toshio Koidefe1d5d92014-02-26 20:09:48 -080067
Ray Milkey269ffb92014-04-03 14:43:30 -070068 @SuppressWarnings("unchecked")
69 @Before
70 public void setUp() throws Exception {
71 MockNetworkGraph graph = new MockNetworkGraph();
72 graph.createSampleTopology1();
73 g = graph;
Toshio Koide27ffd412014-02-18 19:15:27 -080074
Ray Milkey269ffb92014-04-03 14:43:30 -070075 datagridService = createMock(IDatagridService.class);
76 networkGraphService = createMock(INetworkGraphService.class);
77 controllerRegistryService = createMock(IControllerRegistryService.class);
78 modContext = createMock(FloodlightModuleContext.class);
Toshio Koidee04a9df2014-05-01 15:49:28 -070079 intentOperationChannel = createMock(IEventChannel.class);
80 intentStateChannel = createMock(IEventChannel.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070081 persistIntent = PowerMock.createMock(PersistIntent.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -080082
Ray Milkey269ffb92014-04-03 14:43:30 -070083 PowerMock.expectNew(PersistIntent.class,
Pavlin Radoslavov0294e052014-04-10 13:36:45 -070084 anyObject(IControllerRegistryService.class)).andReturn(persistIntent);
Toshio Koide27ffd412014-02-18 19:15:27 -080085
Ray Milkey269ffb92014-04-03 14:43:30 -070086 expect(modContext.getServiceImpl(IDatagridService.class))
87 .andReturn(datagridService).once();
88 expect(modContext.getServiceImpl(INetworkGraphService.class))
89 .andReturn(networkGraphService).once();
90 expect(modContext.getServiceImpl(IControllerRegistryService.class))
91 .andReturn(controllerRegistryService).once();
92 expect(persistIntent.getKey()).andReturn(1L).anyTimes();
93 expect(persistIntent.persistIfLeader(eq(1L),
94 anyObject(IntentOperationList.class))).andReturn(true).anyTimes();
Toshio Koide0c9106d2014-02-19 15:26:38 -080095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 expect(networkGraphService.getNetworkGraph()).andReturn(g).anyTimes();
97 networkGraphService.registerNetworkGraphListener(anyObject(INetworkGraphListener.class));
98 expectLastCall();
Toshio Koide0c9106d2014-02-19 15:26:38 -080099
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 expect(datagridService.createChannel("onos.pathintent", Long.class, IntentOperationList.class))
Toshio Koidee04a9df2014-05-01 15:49:28 -0700101 .andReturn(intentOperationChannel).once();
Toshio Koide27ffd412014-02-18 19:15:27 -0800102
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 expect(datagridService.addListener(
104 eq("onos.pathintent_state"),
105 anyObject(IEventChannelListener.class),
106 eq(Long.class),
107 eq(IntentStateList.class)))
Toshio Koidee04a9df2014-05-01 15:49:28 -0700108 .andReturn(intentStateChannel).once();
Toshio Koide066506e2014-02-20 19:52:09 -0800109
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 replay(datagridService);
111 replay(networkGraphService);
112 replay(modContext);
113 replay(controllerRegistryService);
114 PowerMock.replay(persistIntent, PersistIntent.class);
115 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800116
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 @After
118 public void tearDown() {
119 verify(datagridService);
120 verify(networkGraphService);
121 verify(modContext);
122 verify(controllerRegistryService);
123 PowerMock.verify(persistIntent, PersistIntent.class);
124 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800125
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 private void showResult(PathIntentMap intents) {
127 for (Intent intent : intents.getAllIntents()) {
128 PathIntent pathIntent = (PathIntent) intent;
129 System.out.println("Path intent:" + pathIntent);
130 System.out.println("Parent intent: " + pathIntent.getParentIntent().toString());
131 }
132 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800133
Ray Milkey269ffb92014-04-03 14:43:30 -0700134 @Test
135 public void createShortestPaths() throws FloodlightModuleException {
136 // create shortest path intents
137 IntentOperationList opList = new IntentOperationList();
138 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
139 opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
140 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
Toshio Koidec87810e2014-02-11 13:03:21 -0800141
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 // compile high-level intent operations into low-level intent operations (calculate paths)
143 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
144 runtime1.init(modContext);
145 runtime1.startUp(modContext);
146 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koide27ffd412014-02-18 19:15:27 -0800147
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 // compile low-level intents into flow entry installation plan
149 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
150 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800151
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 // show results
153 showResult((PathIntentMap) runtime1.getPathIntents());
154 System.out.println(plan);
155 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800156
Ray Milkey269ffb92014-04-03 14:43:30 -0700157 @Test
158 public void createConstrainedShortestPaths() throws FloodlightModuleException {
159 // create constrained shortest path intents
160 IntentOperationList opList = new IntentOperationList();
161 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT, 400.0));
162 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT, 400.0));
163 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 2L, 24L, LOCAL_PORT, 4L, 42L, LOCAL_PORT, 400.0));
164 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT, 400.0));
165 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 3L, 34L, LOCAL_PORT, 4L, 43L, LOCAL_PORT, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800166
Ray Milkey269ffb92014-04-03 14:43:30 -0700167 // compile high-level intent operations into low-level intent operations (calculate paths)
168 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
169 runtime1.init(modContext);
170 runtime1.startUp(modContext);
171 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800172
Ray Milkey269ffb92014-04-03 14:43:30 -0700173 // compile low-level intents into flow entry installation plan
174 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
175 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800176
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 // show results
178 showResult((PathIntentMap) runtime1.getPathIntents());
179 System.out.println(plan);
180 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800181
Ray Milkey269ffb92014-04-03 14:43:30 -0700182 @Test
183 public void createMixedShortestPaths() throws FloodlightModuleException {
184 // create constrained & best effort shortest path intents
185 IntentOperationList opList = new IntentOperationList();
186 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT, 400.0));
187 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT, 400.0));
188 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 24L, LOCAL_PORT, 4L, 42L, LOCAL_PORT));
189 opList.add(Operator.ADD, new ShortestPathIntent("4", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
190 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 3L, 34L, LOCAL_PORT, 4L, 43L, LOCAL_PORT, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800191
Ray Milkey269ffb92014-04-03 14:43:30 -0700192 // compile high-level intent operations into low-level intent operations (calculate paths)
193 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
194 runtime1.init(modContext);
195 runtime1.startUp(modContext);
196 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800197
Ray Milkey269ffb92014-04-03 14:43:30 -0700198 // compile low-level intents into flow entry installation plan
199 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
200 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800201
Ray Milkey269ffb92014-04-03 14:43:30 -0700202 // show results
203 showResult((PathIntentMap) runtime1.getPathIntents());
204 System.out.println(plan);
205 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800206
Ray Milkey269ffb92014-04-03 14:43:30 -0700207 @Test
208 public void rerouteShortestPaths() throws FloodlightModuleException {
209 List<SwitchEvent> addedSwitchEvents = new LinkedList<>();
210 List<SwitchEvent> removedSwitchEvents = new LinkedList<>();
211 List<PortEvent> addedPortEvents = new LinkedList<>();
212 List<PortEvent> removedPortEvents = new LinkedList<>();
213 List<LinkEvent> addedLinkEvents = new LinkedList<>();
214 List<LinkEvent> removedLinkEvents = new LinkedList<>();
215 List<DeviceEvent> addedDeviceEvents = new LinkedList<>();
216 List<DeviceEvent> removedDeviceEvents = new LinkedList<>();
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800217
Ray Milkey269ffb92014-04-03 14:43:30 -0700218 // create shortest path intents
219 IntentOperationList opList = new IntentOperationList();
220 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
221 opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
222 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
Toshio Koide0c9106d2014-02-19 15:26:38 -0800223
Ray Milkey269ffb92014-04-03 14:43:30 -0700224 // compile high-level intent operations into low-level intent operations (calculate paths)
225 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
226 runtime1.init(modContext);
227 runtime1.startUp(modContext);
228 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800229
Ray Milkey269ffb92014-04-03 14:43:30 -0700230 // compile low-level intents into flow entry installation plan
231 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
232 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800233
Ray Milkey269ffb92014-04-03 14:43:30 -0700234 // show results step1
235 showResult((PathIntentMap) runtime1.getPathIntents());
236 System.out.println(plan);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800237
Ray Milkey269ffb92014-04-03 14:43:30 -0700238 // TODO this state changes should be triggered by notification of plan module
239 IntentStateList states = new IntentStateList();
240 states.put("1", IntentState.INST_ACK);
241 states.put("2", IntentState.INST_ACK);
242 states.put("3", IntentState.INST_ACK);
243 runtime1.getHighLevelIntents().changeStates(states);
244 states.clear();
245 states.put("1___0", IntentState.INST_ACK);
246 states.put("2___0", IntentState.INST_ACK);
247 states.put("3___0", IntentState.INST_ACK);
248 runtime1.getPathIntents().changeStates(states);
Toshio Koidea10c0372014-02-20 17:28:10 -0800249
Ray Milkey269ffb92014-04-03 14:43:30 -0700250 // link down
251 ((MockNetworkGraph) g).removeLink(1L, 12L, 2L, 21L); // This link is used by the intent "1"
252 ((MockNetworkGraph) g).removeLink(2L, 21L, 1L, 12L);
253 LinkEvent linkEvent1 = new LinkEvent(1L, 12L, 2L, 21L);
254 LinkEvent linkEvent2 = new LinkEvent(2L, 21L, 1L, 12L);
255 removedLinkEvents.clear();
256 removedLinkEvents.add(linkEvent1);
257 removedLinkEvents.add(linkEvent2);
258 runtime1.networkGraphEvents(
259 addedSwitchEvents,
260 removedSwitchEvents,
261 addedPortEvents,
262 removedPortEvents,
263 addedLinkEvents,
264 removedLinkEvents,
265 addedDeviceEvents,
266 removedDeviceEvents);
267 System.out.println("Link goes down.");
Toshio Koide0c9106d2014-02-19 15:26:38 -0800268
Ray Milkey269ffb92014-04-03 14:43:30 -0700269 // show results step2
270 showResult((PathIntentMap) runtime1.getPathIntents());
271 // TODO: show results of plan computation
272 }
Toshio Koidefa735a12014-03-28 10:49:07 -0700273
274
Ray Milkey269ffb92014-04-03 14:43:30 -0700275 @Test
276 public void createAndRemoveShortestPaths() throws FloodlightModuleException {
277 // create shortest path intents
278 IntentOperationList opList = new IntentOperationList();
279 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 12L, LOCAL_PORT, 2L, 21L, LOCAL_PORT));
280 opList.add(Operator.ADD, new ShortestPathIntent("2", 1L, 14L, LOCAL_PORT, 4L, 41L, LOCAL_PORT));
281 opList.add(Operator.ADD, new ShortestPathIntent("3", 2L, 23L, LOCAL_PORT, 3L, 32L, LOCAL_PORT));
Toshio Koidefa735a12014-03-28 10:49:07 -0700282
Ray Milkey269ffb92014-04-03 14:43:30 -0700283 // compile high-level intent operations into low-level intent operations (calculate paths)
284 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
285 runtime1.init(modContext);
286 runtime1.startUp(modContext);
287 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidefa735a12014-03-28 10:49:07 -0700288
Ray Milkey269ffb92014-04-03 14:43:30 -0700289 // compile low-level intents into flow entry installation plan
290 PlanCalcRuntime runtime2 = new PlanCalcRuntime();
291 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koidefa735a12014-03-28 10:49:07 -0700292
Ray Milkey269ffb92014-04-03 14:43:30 -0700293 // show results
294 showResult((PathIntentMap) runtime1.getPathIntents());
295 System.out.println(plan);
Toshio Koidefa735a12014-03-28 10:49:07 -0700296
Ray Milkey269ffb92014-04-03 14:43:30 -0700297 // create remove operations
298 opList.clear();
299 opList.add(Operator.REMOVE, new Intent("1"));
300 opList.add(Operator.REMOVE, new Intent("2"));
Toshio Koidefa735a12014-03-28 10:49:07 -0700301
Ray Milkey269ffb92014-04-03 14:43:30 -0700302 // compile
303 runtime1.executeIntentOperations(opList);
Toshio Koidefa735a12014-03-28 10:49:07 -0700304
Ray Milkey269ffb92014-04-03 14:43:30 -0700305 // show results
306 showResult((PathIntentMap) runtime1.getPathIntents());
307 System.out.println(plan);
308 }
Toshio Koidefa735a12014-03-28 10:49:07 -0700309
Toshio Koidec87810e2014-02-11 13:03:21 -0800310}