blob: 00da877cd2d79e584538821c1e62c1df98e1fb91 [file] [log] [blame]
Toshio Koidec87810e2014-02-11 13:03:21 -08001package net.onrc.onos.intent.runtime;
2
Toshio Koidea10c0372014-02-20 17:28:10 -08003import java.util.HashMap;
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08004import java.util.LinkedList;
Brian O'Connor12861f72014-02-19 20:40:32 -08005import java.util.List;
6import java.util.Set;
7
Toshio Koide27ffd412014-02-18 19:15:27 -08008import net.floodlightcontroller.core.module.FloodlightModuleContext;
9import net.floodlightcontroller.core.module.FloodlightModuleException;
10import net.onrc.onos.datagrid.IDatagridService;
11import net.onrc.onos.datagrid.IEventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080012import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Brian O'Connor12861f72014-02-19 20:40:32 -080013import net.onrc.onos.intent.FlowEntry;
Toshio Koidec87810e2014-02-11 13:03:21 -080014import net.onrc.onos.intent.Intent;
Toshio Koidea10c0372014-02-20 17:28:10 -080015import net.onrc.onos.intent.Intent.IntentState;
Toshio Koide27ffd412014-02-18 19:15:27 -080016import net.onrc.onos.intent.IntentOperation.Operator;
Toshio Koidea10c0372014-02-20 17:28:10 -080017import net.onrc.onos.intent.IntentOperation;
Toshio Koide27ffd412014-02-18 19:15:27 -080018import net.onrc.onos.intent.IntentOperationList;
Toshio Koideebdbb622014-02-12 20:28:38 -080019import net.onrc.onos.intent.MockNetworkGraph;
Toshio Koidec87810e2014-02-11 13:03:21 -080020import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080021import net.onrc.onos.intent.PathIntentMap;
Toshio Koidec87810e2014-02-11 13:03:21 -080022import net.onrc.onos.intent.ShortestPathIntent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080023import net.onrc.onos.intent.persist.PersistIntent;
Toshio Koide798bc1b2014-02-20 14:02:40 -080024import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080025import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080026import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080027import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koide798bc1b2014-02-20 14:02:40 -080028import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -080029import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
30import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080031import net.onrc.onos.registry.controller.IControllerRegistryService;
Toshio Koidec87810e2014-02-11 13:03:21 -080032
Toshio Koide27ffd412014-02-18 19:15:27 -080033import org.easymock.EasyMock;
Toshio Koidec87810e2014-02-11 13:03:21 -080034import 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 Koide27ffd412014-02-18 19:15:27 -080057 @SuppressWarnings("unchecked")
Toshio Koidec87810e2014-02-11 13:03:21 -080058 @Before
Nick Karanatsios8abe7172014-02-19 20:31:48 -080059 public void setUp() throws Exception {
Toshio Koideebdbb622014-02-12 20:28:38 -080060 MockNetworkGraph graph = new MockNetworkGraph();
61 graph.createSampleTopology();
62 g = graph;
Toshio Koide27ffd412014-02-18 19:15:27 -080063
64 datagridService = EasyMock.createMock(IDatagridService.class);
65 networkGraphService = EasyMock.createMock(INetworkGraphService.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -080066 controllerRegistryService = EasyMock.createMock(IControllerRegistryService.class);
Toshio Koide27ffd412014-02-18 19:15:27 -080067 modContext = EasyMock.createMock(FloodlightModuleContext.class);
68 eventChannel = EasyMock.createMock(IEventChannel.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -080069 persistIntent = PowerMock.createMock(PersistIntent.class);
70
71 PowerMock.expectNew(PersistIntent.class,
72 EasyMock.anyObject(IControllerRegistryService.class),
73 EasyMock.anyObject(INetworkGraphService.class)).andReturn(persistIntent);
Toshio Koide27ffd412014-02-18 19:15:27 -080074
75 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IDatagridService.class)))
76 .andReturn(datagridService).once();
77 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(INetworkGraphService.class)))
78 .andReturn(networkGraphService).once();
Toshio Koide798bc1b2014-02-20 14:02:40 -080079 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IControllerRegistryService.class)))
80 .andReturn(controllerRegistryService).once();
81 EasyMock.expect(persistIntent.getKey()).andReturn(1L).anyTimes();
82 EasyMock.expect(persistIntent.persistIfLeader(EasyMock.eq(1L),
83 EasyMock.anyObject(IntentOperationList.class))).andReturn(true).anyTimes();
Toshio Koide0c9106d2014-02-19 15:26:38 -080084
85 EasyMock.expect(networkGraphService.getNetworkGraph()).andReturn(g).anyTimes();
86 networkGraphService.registerNetworkGraphListener(EasyMock.anyObject(INetworkGraphListener.class));
87 EasyMock.expectLastCall();
88
Nick Karanatsios8abe7172014-02-19 20:31:48 -080089 EasyMock.expect(datagridService.createChannel("onos.pathintent", Long.class, IntentOperationList.class))
Toshio Koide27ffd412014-02-18 19:15:27 -080090 .andReturn(eventChannel).once();
91
92 EasyMock.replay(datagridService);
93 EasyMock.replay(networkGraphService);
94 EasyMock.replay(modContext);
Toshio Koide798bc1b2014-02-20 14:02:40 -080095 EasyMock.replay(controllerRegistryService);
96 PowerMock.replay(persistIntent, PersistIntent.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080097 }
98
99 @After
100 public void tearDown() {
Toshio Koide27ffd412014-02-18 19:15:27 -0800101 EasyMock.verify(datagridService);
102 EasyMock.verify(networkGraphService);
103 EasyMock.verify(modContext);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800104 EasyMock.verify(controllerRegistryService);
105 PowerMock.verify(persistIntent, PersistIntent.class);
Toshio Koidec87810e2014-02-11 13:03:21 -0800106 }
107
Toshio Koide4f308732014-02-18 15:19:48 -0800108 private void showResult(PathIntentMap intents) {
109 for (Intent intent: intents.getAllIntents()) {
110 PathIntent pathIntent = (PathIntent)intent;
Toshio Koidec87810e2014-02-11 13:03:21 -0800111 System.out.println("Parent intent: " + pathIntent.getParentIntent().toString());
112 System.out.println("Path:");
Toshio Koided9fa2a82014-02-19 17:35:18 -0800113 for (LinkEvent linkEvent: pathIntent.getPath()) {
Toshio Koide0c9106d2014-02-19 15:26:38 -0800114 System.out.println(linkEvent);
Toshio Koidec87810e2014-02-11 13:03:21 -0800115 }
116 }
117 }
118
119 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800120 public void createShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800121 // create shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800122 IntentOperationList opList = new IntentOperationList();
123 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
124 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
125 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
Toshio Koidec87810e2014-02-11 13:03:21 -0800126
Toshio Koide27ffd412014-02-18 19:15:27 -0800127 // compile high-level intent operations into low-level intent operations (calculate paths)
128 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
129 runtime1.init(modContext);
130 runtime1.startUp(modContext);
131 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
132
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800133 // compile low-level intents into flow entry installation plan
134 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800135 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800136
137 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800138 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800139 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800140 }
141
142 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800143 public void createConstrainedShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800144 // create constrained shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800145 IntentOperationList opList = new IntentOperationList();
146 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 17L, 400.0));
147 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 18L, 400.0));
148 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 19L, 400.0));
149 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 3L, 20L, 4L, 8L, 20L, 20L, 400.0));
150 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 21L, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800151
Toshio Koide27ffd412014-02-18 19:15:27 -0800152 // compile high-level intent operations into low-level intent operations (calculate paths)
153 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
154 runtime1.init(modContext);
155 runtime1.startUp(modContext);
156 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800157
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800158 // compile low-level intents into flow entry installation plan
159 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800160 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800161
Toshio Koidec87810e2014-02-11 13:03:21 -0800162 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800163 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800164 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800165 }
166
167 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800168 public void createMixedShortestPaths() throws FloodlightModuleException {
169 // create constrained & best effort shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800170 IntentOperationList opList = new IntentOperationList();
171 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 6L, 600.0));
172 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 7L, 600.0));
173 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 8L));
174 opList.add(Operator.ADD, new ShortestPathIntent("4", 4L, 20L, 4L, 8L, 20L, 9L));
175 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 10L, 600.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800176
Toshio Koide27ffd412014-02-18 19:15:27 -0800177 // compile high-level intent operations into low-level intent operations (calculate paths)
178 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
179 runtime1.init(modContext);
180 runtime1.startUp(modContext);
181 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800182
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800183 // compile low-level intents into flow entry installation plan
184 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800185 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800186
Toshio Koidec87810e2014-02-11 13:03:21 -0800187 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800188 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800189 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800190 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800191
Toshio Koide0c9106d2014-02-19 15:26:38 -0800192 @Test
193 public void rerouteShortestPaths() throws FloodlightModuleException {
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800194 List<SwitchEvent> addedSwitchEvents = new LinkedList<>();
195 List<SwitchEvent> removedSwitchEvents = new LinkedList<>();
196 List<PortEvent> addedPortEvents = new LinkedList<>();
197 List<PortEvent> removedPortEvents = new LinkedList<>();
198 List<LinkEvent> addedLinkEvents = new LinkedList<>();
199 List<LinkEvent> removedLinkEvents = new LinkedList<>();
200 List<DeviceEvent> addedDeviceEvents = new LinkedList<>();
201 List<DeviceEvent> removedDeviceEvents = new LinkedList<>();
202
Toshio Koide0c9106d2014-02-19 15:26:38 -0800203 // create shortest path intents
204 IntentOperationList opList = new IntentOperationList();
205 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
206 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
207 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
208
209 // compile high-level intent operations into low-level intent operations (calculate paths)
210 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
211 runtime1.init(modContext);
212 runtime1.startUp(modContext);
213 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
214
215 // compile low-level intents into flow entry installation plan
216 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800217 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800218
219 // show results step1
220 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800221 System.out.println(plan);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800222
Toshio Koidea10c0372014-02-20 17:28:10 -0800223 // TODO this state changes should be triggered by notification of plan module
224 HashMap<String, IntentState> states = new HashMap<>();
225 states.put("1", IntentState.INST_ACK);
226 states.put("2", IntentState.INST_ACK);
227 states.put("3", IntentState.INST_ACK);
228 runtime1.getHighLevelIntents().changeStates(states);
229
Toshio Koide0c9106d2014-02-19 15:26:38 -0800230 // link down
231 ((MockNetworkGraph)g).removeLink(1L, 2L, 9L, 1L); // This link is used by the intent "1"
Toshio Koide0c9106d2014-02-19 15:26:38 -0800232 ((MockNetworkGraph)g).removeLink(9L, 1L, 1L, 2L);
Toshio Koidea10c0372014-02-20 17:28:10 -0800233 LinkEvent linkEvent1 = new LinkEvent(1L, 2L, 9L, 1L);
234 LinkEvent linkEvent2 = new LinkEvent(9L, 1L, 1L, 2L);
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800235 removedLinkEvents.clear();
Toshio Koidea10c0372014-02-20 17:28:10 -0800236 removedLinkEvents.add(linkEvent1);
237 removedLinkEvents.add(linkEvent2);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800238 runtime1.networkGraphEvents(
239 addedSwitchEvents,
240 removedSwitchEvents,
241 addedPortEvents,
242 removedPortEvents,
243 addedLinkEvents,
244 removedLinkEvents,
245 addedDeviceEvents,
246 removedDeviceEvents);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800247 System.out.println("Link goes down.");
248
249 // show results step2
250 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800251 // TODO: show results of plan computation
Toshio Koide0c9106d2014-02-19 15:26:38 -0800252 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800253}