blob: 36162dab38ccf30a31213120e9125c9b7d58d40f [file] [log] [blame]
Toshio Koidec87810e2014-02-11 13:03:21 -08001package net.onrc.onos.intent.runtime;
2
Brian O'Connor12861f72014-02-19 20:40:32 -08003import java.util.List;
4import java.util.Set;
5
Toshio Koide27ffd412014-02-18 19:15:27 -08006import net.floodlightcontroller.core.module.FloodlightModuleContext;
7import net.floodlightcontroller.core.module.FloodlightModuleException;
8import net.onrc.onos.datagrid.IDatagridService;
9import net.onrc.onos.datagrid.IEventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080010import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Brian O'Connor12861f72014-02-19 20:40:32 -080011import net.onrc.onos.intent.FlowEntry;
Toshio Koidec87810e2014-02-11 13:03:21 -080012import net.onrc.onos.intent.Intent;
Toshio Koide27ffd412014-02-18 19:15:27 -080013import net.onrc.onos.intent.IntentOperation.Operator;
14import net.onrc.onos.intent.IntentOperationList;
Toshio Koideebdbb622014-02-12 20:28:38 -080015import net.onrc.onos.intent.MockNetworkGraph;
Toshio Koidec87810e2014-02-11 13:03:21 -080016import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080017import net.onrc.onos.intent.PathIntentMap;
Toshio Koidec87810e2014-02-11 13:03:21 -080018import net.onrc.onos.intent.ShortestPathIntent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080019import net.onrc.onos.intent.persist.PersistIntent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080020import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080021import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080022import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koide27ffd412014-02-18 19:15:27 -080023import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080024import net.onrc.onos.registry.controller.IControllerRegistryService;
Toshio Koidec87810e2014-02-11 13:03:21 -080025
Toshio Koide27ffd412014-02-18 19:15:27 -080026import org.easymock.EasyMock;
Toshio Koidec87810e2014-02-11 13:03:21 -080027import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080030import org.junit.runner.RunWith;
31import org.powermock.api.easymock.PowerMock;
32import org.powermock.core.classloader.annotations.PrepareForTest;
33import org.powermock.modules.junit4.PowerMockRunner;
Toshio Koidec87810e2014-02-11 13:03:21 -080034
35/**
36 * @author Toshio Koide (t-koide@onlab.us)
37 */
Nick Karanatsios8abe7172014-02-19 20:31:48 -080038@RunWith(PowerMockRunner.class)
39@PrepareForTest(PathCalcRuntimeModule.class)
Toshio Koidec87810e2014-02-11 13:03:21 -080040public class UseCaseTest {
Toshio Koide27ffd412014-02-18 19:15:27 -080041 private NetworkGraph g;
42 private FloodlightModuleContext modContext;
43 private IDatagridService datagridService;
44 private INetworkGraphService networkGraphService;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080045 private IControllerRegistryService controllerRegistryService;
46 private PersistIntent persistIntent;
Toshio Koide27ffd412014-02-18 19:15:27 -080047 @SuppressWarnings("rawtypes")
48 private IEventChannel eventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080049
Toshio Koide27ffd412014-02-18 19:15:27 -080050 @SuppressWarnings("unchecked")
Toshio Koidec87810e2014-02-11 13:03:21 -080051 @Before
Nick Karanatsios8abe7172014-02-19 20:31:48 -080052 public void setUp() throws Exception {
Toshio Koideebdbb622014-02-12 20:28:38 -080053 MockNetworkGraph graph = new MockNetworkGraph();
54 graph.createSampleTopology();
55 g = graph;
Toshio Koide27ffd412014-02-18 19:15:27 -080056
57 datagridService = EasyMock.createMock(IDatagridService.class);
58 networkGraphService = EasyMock.createMock(INetworkGraphService.class);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080059 controllerRegistryService = EasyMock.createMock(IControllerRegistryService.class);
Toshio Koide27ffd412014-02-18 19:15:27 -080060 modContext = EasyMock.createMock(FloodlightModuleContext.class);
61 eventChannel = EasyMock.createMock(IEventChannel.class);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080062 persistIntent = PowerMock.createMock(PersistIntent.class);
63
64 PowerMock.expectNew(PersistIntent.class,
65 EasyMock.anyObject(IControllerRegistryService.class),
66 EasyMock.anyObject(INetworkGraphService.class)).andReturn(persistIntent);
Toshio Koide27ffd412014-02-18 19:15:27 -080067
68 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IDatagridService.class)))
69 .andReturn(datagridService).once();
70 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(INetworkGraphService.class)))
71 .andReturn(networkGraphService).once();
Nick Karanatsios8abe7172014-02-19 20:31:48 -080072 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IControllerRegistryService.class)))
73 .andReturn(controllerRegistryService).once();
74 EasyMock.expect(persistIntent.getKey()).andReturn(1L).anyTimes();
75 EasyMock.expect(persistIntent.persistIfLeader(EasyMock.eq(1L),
76 EasyMock.anyObject(IntentOperationList.class))).andReturn(true).anyTimes();
Toshio Koide0c9106d2014-02-19 15:26:38 -080077
78 EasyMock.expect(networkGraphService.getNetworkGraph()).andReturn(g).anyTimes();
79 networkGraphService.registerNetworkGraphListener(EasyMock.anyObject(INetworkGraphListener.class));
80 EasyMock.expectLastCall();
81
Nick Karanatsios8abe7172014-02-19 20:31:48 -080082 EasyMock.expect(datagridService.createChannel("onos.pathintent", Long.class, IntentOperationList.class))
Toshio Koide27ffd412014-02-18 19:15:27 -080083 .andReturn(eventChannel).once();
84
85 EasyMock.replay(datagridService);
86 EasyMock.replay(networkGraphService);
87 EasyMock.replay(modContext);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080088 EasyMock.replay(controllerRegistryService);
89 PowerMock.replay(persistIntent, PersistIntent.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080090 }
91
92 @After
93 public void tearDown() {
Toshio Koide27ffd412014-02-18 19:15:27 -080094 EasyMock.verify(datagridService);
95 EasyMock.verify(networkGraphService);
96 EasyMock.verify(modContext);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080097 EasyMock.verify(controllerRegistryService);
98 PowerMock.verify(persistIntent, PersistIntent.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080099 }
100
Toshio Koide4f308732014-02-18 15:19:48 -0800101 private void showResult(PathIntentMap intents) {
102 for (Intent intent: intents.getAllIntents()) {
103 PathIntent pathIntent = (PathIntent)intent;
Toshio Koidec87810e2014-02-11 13:03:21 -0800104 System.out.println("Parent intent: " + pathIntent.getParentIntent().toString());
105 System.out.println("Path:");
Toshio Koided9fa2a82014-02-19 17:35:18 -0800106 for (LinkEvent linkEvent: pathIntent.getPath()) {
Toshio Koide0c9106d2014-02-19 15:26:38 -0800107 System.out.println(linkEvent);
Toshio Koidec87810e2014-02-11 13:03:21 -0800108 }
109 }
110 }
111
112 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800113 public void createShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800114 // create shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800115 IntentOperationList opList = new IntentOperationList();
116 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
117 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
118 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
Toshio Koidec87810e2014-02-11 13:03:21 -0800119
Toshio Koide27ffd412014-02-18 19:15:27 -0800120 // compile high-level intent operations into low-level intent operations (calculate paths)
121 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
122 runtime1.init(modContext);
123 runtime1.startUp(modContext);
124 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
125
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800126 // compile low-level intents into flow entry installation plan
127 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800128 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800129
130 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800131 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800132 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800133 }
134
135 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800136 public void createConstrainedShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800137 // create constrained shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800138 IntentOperationList opList = new IntentOperationList();
139 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 17L, 400.0));
140 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 18L, 400.0));
141 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 19L, 400.0));
142 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 3L, 20L, 4L, 8L, 20L, 20L, 400.0));
143 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 21L, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800144
Toshio Koide27ffd412014-02-18 19:15:27 -0800145 // compile high-level intent operations into low-level intent operations (calculate paths)
146 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
147 runtime1.init(modContext);
148 runtime1.startUp(modContext);
149 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800150
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800151 // compile low-level intents into flow entry installation plan
152 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800153 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800154
Toshio Koidec87810e2014-02-11 13:03:21 -0800155 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800156 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800157 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800158 }
159
160 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800161 public void createMixedShortestPaths() throws FloodlightModuleException {
162 // create constrained & best effort shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800163 IntentOperationList opList = new IntentOperationList();
164 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 6L, 600.0));
165 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 7L, 600.0));
166 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 8L));
167 opList.add(Operator.ADD, new ShortestPathIntent("4", 4L, 20L, 4L, 8L, 20L, 9L));
168 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 10L, 600.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800169
Toshio Koide27ffd412014-02-18 19:15:27 -0800170 // compile high-level intent operations into low-level intent operations (calculate paths)
171 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
172 runtime1.init(modContext);
173 runtime1.startUp(modContext);
174 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800175
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800176 // compile low-level intents into flow entry installation plan
177 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800178 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800179
Toshio Koidec87810e2014-02-11 13:03:21 -0800180 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800181 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800182 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800183 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800184
Toshio Koide0c9106d2014-02-19 15:26:38 -0800185 @Test
186 public void rerouteShortestPaths() throws FloodlightModuleException {
187 // create shortest path intents
188 IntentOperationList opList = new IntentOperationList();
189 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
190 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
191 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
192
193 // compile high-level intent operations into low-level intent operations (calculate paths)
194 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
195 runtime1.init(modContext);
196 runtime1.startUp(modContext);
197 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
198
199 // compile low-level intents into flow entry installation plan
200 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800201 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800202
203 // show results step1
204 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800205 System.out.println(plan);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800206
207 // link down
208 ((MockNetworkGraph)g).removeLink(1L, 2L, 9L, 1L); // This link is used by the intent "1"
209 LinkEvent linkEvent = new LinkEvent(1L, 2L, 9L, 1L);
210 runtime1.removeLinkEvent(linkEvent);
211 ((MockNetworkGraph)g).removeLink(9L, 1L, 1L, 2L);
212 linkEvent = new LinkEvent(9L, 1L, 1L, 2L);
213 runtime1.removeLinkEvent(linkEvent);
214
215 System.out.println("Link goes down.");
216
217 // show results step2
218 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800219 // TODO: show results of plan computation
Toshio Koide0c9106d2014-02-19 15:26:38 -0800220 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800221}