blob: 698f7903726e2e19bd56a0a2259193fc3572803a [file] [log] [blame]
Toshio Koidec87810e2014-02-11 13:03:21 -08001package net.onrc.onos.intent.runtime;
2
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08003import java.util.LinkedList;
Brian O'Connor12861f72014-02-19 20:40:32 -08004import java.util.List;
5import java.util.Set;
6
Toshio Koide27ffd412014-02-18 19:15:27 -08007import net.floodlightcontroller.core.module.FloodlightModuleContext;
8import net.floodlightcontroller.core.module.FloodlightModuleException;
9import net.onrc.onos.datagrid.IDatagridService;
10import net.onrc.onos.datagrid.IEventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080011import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Brian O'Connor12861f72014-02-19 20:40:32 -080012import net.onrc.onos.intent.FlowEntry;
Toshio Koidec87810e2014-02-11 13:03:21 -080013import net.onrc.onos.intent.Intent;
Toshio Koide27ffd412014-02-18 19:15:27 -080014import net.onrc.onos.intent.IntentOperation.Operator;
15import net.onrc.onos.intent.IntentOperationList;
Toshio Koideebdbb622014-02-12 20:28:38 -080016import net.onrc.onos.intent.MockNetworkGraph;
Toshio Koidec87810e2014-02-11 13:03:21 -080017import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080018import net.onrc.onos.intent.PathIntentMap;
Toshio Koidec87810e2014-02-11 13:03:21 -080019import net.onrc.onos.intent.ShortestPathIntent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080020import net.onrc.onos.intent.persist.PersistIntent;
Toshio Koide798bc1b2014-02-20 14:02:40 -080021import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080022import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080023import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080024import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koide798bc1b2014-02-20 14:02:40 -080025import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -080026import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
27import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080028import net.onrc.onos.registry.controller.IControllerRegistryService;
Toshio Koidec87810e2014-02-11 13:03:21 -080029
Toshio Koide27ffd412014-02-18 19:15:27 -080030import org.easymock.EasyMock;
Toshio Koidec87810e2014-02-11 13:03:21 -080031import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080034import org.junit.runner.RunWith;
35import org.powermock.api.easymock.PowerMock;
36import org.powermock.core.classloader.annotations.PrepareForTest;
37import org.powermock.modules.junit4.PowerMockRunner;
Toshio Koidec87810e2014-02-11 13:03:21 -080038
39/**
40 * @author Toshio Koide (t-koide@onlab.us)
41 */
Nick Karanatsios8abe7172014-02-19 20:31:48 -080042@RunWith(PowerMockRunner.class)
43@PrepareForTest(PathCalcRuntimeModule.class)
Toshio Koidec87810e2014-02-11 13:03:21 -080044public class UseCaseTest {
Toshio Koide27ffd412014-02-18 19:15:27 -080045 private NetworkGraph g;
46 private FloodlightModuleContext modContext;
47 private IDatagridService datagridService;
48 private INetworkGraphService networkGraphService;
Toshio Koide798bc1b2014-02-20 14:02:40 -080049 private IControllerRegistryService controllerRegistryService;
50 private PersistIntent persistIntent;
Toshio Koide27ffd412014-02-18 19:15:27 -080051 @SuppressWarnings("rawtypes")
52 private IEventChannel eventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080053
Toshio Koide27ffd412014-02-18 19:15:27 -080054 @SuppressWarnings("unchecked")
Toshio Koidec87810e2014-02-11 13:03:21 -080055 @Before
Nick Karanatsios8abe7172014-02-19 20:31:48 -080056 public void setUp() throws Exception {
Toshio Koideebdbb622014-02-12 20:28:38 -080057 MockNetworkGraph graph = new MockNetworkGraph();
58 graph.createSampleTopology();
59 g = graph;
Toshio Koide27ffd412014-02-18 19:15:27 -080060
61 datagridService = EasyMock.createMock(IDatagridService.class);
62 networkGraphService = EasyMock.createMock(INetworkGraphService.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -080063 controllerRegistryService = EasyMock.createMock(IControllerRegistryService.class);
Toshio Koide27ffd412014-02-18 19:15:27 -080064 modContext = EasyMock.createMock(FloodlightModuleContext.class);
65 eventChannel = EasyMock.createMock(IEventChannel.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -080066 persistIntent = PowerMock.createMock(PersistIntent.class);
67
68 PowerMock.expectNew(PersistIntent.class,
69 EasyMock.anyObject(IControllerRegistryService.class),
70 EasyMock.anyObject(INetworkGraphService.class)).andReturn(persistIntent);
Toshio Koide27ffd412014-02-18 19:15:27 -080071
72 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IDatagridService.class)))
73 .andReturn(datagridService).once();
74 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(INetworkGraphService.class)))
75 .andReturn(networkGraphService).once();
Toshio Koide798bc1b2014-02-20 14:02:40 -080076 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IControllerRegistryService.class)))
77 .andReturn(controllerRegistryService).once();
78 EasyMock.expect(persistIntent.getKey()).andReturn(1L).anyTimes();
79 EasyMock.expect(persistIntent.persistIfLeader(EasyMock.eq(1L),
80 EasyMock.anyObject(IntentOperationList.class))).andReturn(true).anyTimes();
Toshio Koide0c9106d2014-02-19 15:26:38 -080081
82 EasyMock.expect(networkGraphService.getNetworkGraph()).andReturn(g).anyTimes();
83 networkGraphService.registerNetworkGraphListener(EasyMock.anyObject(INetworkGraphListener.class));
84 EasyMock.expectLastCall();
85
Nick Karanatsios8abe7172014-02-19 20:31:48 -080086 EasyMock.expect(datagridService.createChannel("onos.pathintent", Long.class, IntentOperationList.class))
Toshio Koide27ffd412014-02-18 19:15:27 -080087 .andReturn(eventChannel).once();
88
89 EasyMock.replay(datagridService);
90 EasyMock.replay(networkGraphService);
91 EasyMock.replay(modContext);
Toshio Koide798bc1b2014-02-20 14:02:40 -080092 EasyMock.replay(controllerRegistryService);
93 PowerMock.replay(persistIntent, PersistIntent.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080094 }
95
96 @After
97 public void tearDown() {
Toshio Koide27ffd412014-02-18 19:15:27 -080098 EasyMock.verify(datagridService);
99 EasyMock.verify(networkGraphService);
100 EasyMock.verify(modContext);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800101 EasyMock.verify(controllerRegistryService);
102 PowerMock.verify(persistIntent, PersistIntent.class);
Toshio Koidec87810e2014-02-11 13:03:21 -0800103 }
104
Toshio Koide4f308732014-02-18 15:19:48 -0800105 private void showResult(PathIntentMap intents) {
106 for (Intent intent: intents.getAllIntents()) {
107 PathIntent pathIntent = (PathIntent)intent;
Toshio Koidec87810e2014-02-11 13:03:21 -0800108 System.out.println("Parent intent: " + pathIntent.getParentIntent().toString());
109 System.out.println("Path:");
Toshio Koided9fa2a82014-02-19 17:35:18 -0800110 for (LinkEvent linkEvent: pathIntent.getPath()) {
Toshio Koide0c9106d2014-02-19 15:26:38 -0800111 System.out.println(linkEvent);
Toshio Koidec87810e2014-02-11 13:03:21 -0800112 }
113 }
114 }
115
116 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800117 public void createShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800118 // create shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800119 IntentOperationList opList = new IntentOperationList();
120 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
121 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
122 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
Toshio Koidec87810e2014-02-11 13:03:21 -0800123
Toshio Koide27ffd412014-02-18 19:15:27 -0800124 // compile high-level intent operations into low-level intent operations (calculate paths)
125 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
126 runtime1.init(modContext);
127 runtime1.startUp(modContext);
128 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
129
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800130 // compile low-level intents into flow entry installation plan
131 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800132 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800133
134 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800135 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800136 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800137 }
138
139 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800140 public void createConstrainedShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800141 // create constrained shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800142 IntentOperationList opList = new IntentOperationList();
143 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 17L, 400.0));
144 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 18L, 400.0));
145 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 19L, 400.0));
146 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 3L, 20L, 4L, 8L, 20L, 20L, 400.0));
147 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 21L, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800148
Toshio Koide27ffd412014-02-18 19:15:27 -0800149 // compile high-level intent operations into low-level intent operations (calculate paths)
150 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
151 runtime1.init(modContext);
152 runtime1.startUp(modContext);
153 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800154
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800155 // compile low-level intents into flow entry installation plan
156 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800157 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800158
Toshio Koidec87810e2014-02-11 13:03:21 -0800159 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800160 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800161 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800162 }
163
164 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800165 public void createMixedShortestPaths() throws FloodlightModuleException {
166 // create constrained & best effort shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800167 IntentOperationList opList = new IntentOperationList();
168 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 6L, 600.0));
169 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 7L, 600.0));
170 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 8L));
171 opList.add(Operator.ADD, new ShortestPathIntent("4", 4L, 20L, 4L, 8L, 20L, 9L));
172 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 10L, 600.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800173
Toshio Koide27ffd412014-02-18 19:15:27 -0800174 // compile high-level intent operations into low-level intent operations (calculate paths)
175 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
176 runtime1.init(modContext);
177 runtime1.startUp(modContext);
178 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800179
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800180 // compile low-level intents into flow entry installation plan
181 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800182 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800183
Toshio Koidec87810e2014-02-11 13:03:21 -0800184 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800185 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800186 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800187 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800188
Toshio Koide0c9106d2014-02-19 15:26:38 -0800189 @Test
190 public void rerouteShortestPaths() throws FloodlightModuleException {
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800191 List<SwitchEvent> addedSwitchEvents = new LinkedList<>();
192 List<SwitchEvent> removedSwitchEvents = new LinkedList<>();
193 List<PortEvent> addedPortEvents = new LinkedList<>();
194 List<PortEvent> removedPortEvents = new LinkedList<>();
195 List<LinkEvent> addedLinkEvents = new LinkedList<>();
196 List<LinkEvent> removedLinkEvents = new LinkedList<>();
197 List<DeviceEvent> addedDeviceEvents = new LinkedList<>();
198 List<DeviceEvent> removedDeviceEvents = new LinkedList<>();
199
Toshio Koide0c9106d2014-02-19 15:26:38 -0800200 // create shortest path intents
201 IntentOperationList opList = new IntentOperationList();
202 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
203 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
204 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
205
206 // compile high-level intent operations into low-level intent operations (calculate paths)
207 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
208 runtime1.init(modContext);
209 runtime1.startUp(modContext);
210 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
211
212 // compile low-level intents into flow entry installation plan
213 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800214 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800215
216 // show results step1
217 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800218 System.out.println(plan);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800219
220 // link down
221 ((MockNetworkGraph)g).removeLink(1L, 2L, 9L, 1L); // This link is used by the intent "1"
222 LinkEvent linkEvent = new LinkEvent(1L, 2L, 9L, 1L);
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800223 removedLinkEvents.clear();
224 removedLinkEvents.add(linkEvent);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800225 runtime1.networkGraphEvents(
226 addedSwitchEvents,
227 removedSwitchEvents,
228 addedPortEvents,
229 removedPortEvents,
230 addedLinkEvents,
231 removedLinkEvents,
232 addedDeviceEvents,
233 removedDeviceEvents);
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800234
Toshio Koide0c9106d2014-02-19 15:26:38 -0800235 ((MockNetworkGraph)g).removeLink(9L, 1L, 1L, 2L);
236 linkEvent = new LinkEvent(9L, 1L, 1L, 2L);
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800237 removedLinkEvents.clear();
238 removedLinkEvents.add(linkEvent);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800239 runtime1.networkGraphEvents(
240 addedSwitchEvents,
241 removedSwitchEvents,
242 addedPortEvents,
243 removedPortEvents,
244 addedLinkEvents,
245 removedLinkEvents,
246 addedDeviceEvents,
247 removedDeviceEvents);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800248 System.out.println("Link goes down.");
249
250 // show results step2
251 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800252 // TODO: show results of plan computation
Toshio Koide0c9106d2014-02-19 15:26:38 -0800253 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800254}