blob: a1f3e802a58c274810134183cc913e3bcfc8bd0a [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;
Toshio Koide0c9106d2014-02-19 15:26:38 -080019import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080020import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080021import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koide27ffd412014-02-18 19:15:27 -080022import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Toshio Koidec87810e2014-02-11 13:03:21 -080023
Toshio Koide27ffd412014-02-18 19:15:27 -080024import org.easymock.EasyMock;
Toshio Koidec87810e2014-02-11 13:03:21 -080025import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28
29/**
30 * @author Toshio Koide (t-koide@onlab.us)
31 */
32public class UseCaseTest {
Toshio Koide27ffd412014-02-18 19:15:27 -080033 private NetworkGraph g;
34 private FloodlightModuleContext modContext;
35 private IDatagridService datagridService;
36 private INetworkGraphService networkGraphService;
37 @SuppressWarnings("rawtypes")
38 private IEventChannel eventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080039
Toshio Koide27ffd412014-02-18 19:15:27 -080040 @SuppressWarnings("unchecked")
Toshio Koidec87810e2014-02-11 13:03:21 -080041 @Before
42 public void setUp() {
Toshio Koideebdbb622014-02-12 20:28:38 -080043 MockNetworkGraph graph = new MockNetworkGraph();
44 graph.createSampleTopology();
45 g = graph;
Toshio Koide27ffd412014-02-18 19:15:27 -080046
47 datagridService = EasyMock.createMock(IDatagridService.class);
48 networkGraphService = EasyMock.createMock(INetworkGraphService.class);
49 modContext = EasyMock.createMock(FloodlightModuleContext.class);
50 eventChannel = EasyMock.createMock(IEventChannel.class);
51
52 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IDatagridService.class)))
53 .andReturn(datagridService).once();
54 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(INetworkGraphService.class)))
55 .andReturn(networkGraphService).once();
Toshio Koide0c9106d2014-02-19 15:26:38 -080056
57 EasyMock.expect(networkGraphService.getNetworkGraph()).andReturn(g).anyTimes();
58 networkGraphService.registerNetworkGraphListener(EasyMock.anyObject(INetworkGraphListener.class));
59 EasyMock.expectLastCall();
60
61 EasyMock.expect(datagridService.createChannel("onos.pathintent", String.class, IntentOperationList.class))
Toshio Koide27ffd412014-02-18 19:15:27 -080062 .andReturn(eventChannel).once();
63
64 EasyMock.replay(datagridService);
65 EasyMock.replay(networkGraphService);
66 EasyMock.replay(modContext);
Toshio Koidec87810e2014-02-11 13:03:21 -080067 }
68
69 @After
70 public void tearDown() {
Toshio Koide27ffd412014-02-18 19:15:27 -080071 EasyMock.verify(datagridService);
72 EasyMock.verify(networkGraphService);
73 EasyMock.verify(modContext);
Toshio Koidec87810e2014-02-11 13:03:21 -080074 }
75
Toshio Koide4f308732014-02-18 15:19:48 -080076 private void showResult(PathIntentMap intents) {
77 for (Intent intent: intents.getAllIntents()) {
78 PathIntent pathIntent = (PathIntent)intent;
Toshio Koidec87810e2014-02-11 13:03:21 -080079 System.out.println("Parent intent: " + pathIntent.getParentIntent().toString());
80 System.out.println("Path:");
Toshio Koided9fa2a82014-02-19 17:35:18 -080081 for (LinkEvent linkEvent: pathIntent.getPath()) {
Toshio Koide0c9106d2014-02-19 15:26:38 -080082 System.out.println(linkEvent);
Toshio Koidec87810e2014-02-11 13:03:21 -080083 }
84 }
85 }
86
87 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -080088 public void createShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -080089 // create shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -080090 IntentOperationList opList = new IntentOperationList();
91 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
92 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
93 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
Toshio Koidec87810e2014-02-11 13:03:21 -080094
Toshio Koide27ffd412014-02-18 19:15:27 -080095 // compile high-level intent operations into low-level intent operations (calculate paths)
96 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
97 runtime1.init(modContext);
98 runtime1.startUp(modContext);
99 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
100
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800101 // compile low-level intents into flow entry installation plan
102 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800103 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800104
105 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800106 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800107 System.out.println(plan);
Toshio Koidec87810e2014-02-11 13:03:21 -0800108 }
109
110 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800111 public void createConstrainedShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800112 // create constrained shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800113 IntentOperationList opList = new IntentOperationList();
114 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 17L, 400.0));
115 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 18L, 400.0));
116 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 19L, 400.0));
117 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 3L, 20L, 4L, 8L, 20L, 20L, 400.0));
118 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 21L, 400.0));
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);
Toshio Koidec87810e2014-02-11 13:03:21 -0800125
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);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800129
Toshio Koidec87810e2014-02-11 13:03:21 -0800130 // 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 createMixedShortestPaths() throws FloodlightModuleException {
137 // create constrained & best effort 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, 6L, 600.0));
140 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 7L, 600.0));
141 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 8L));
142 opList.add(Operator.ADD, new ShortestPathIntent("4", 4L, 20L, 4L, 8L, 20L, 9L));
143 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 10L, 600.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 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800159
Toshio Koide0c9106d2014-02-19 15:26:38 -0800160 @Test
161 public void rerouteShortestPaths() throws FloodlightModuleException {
162 // create shortest path intents
163 IntentOperationList opList = new IntentOperationList();
164 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
165 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
166 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
167
168 // compile high-level intent operations into low-level intent operations (calculate paths)
169 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
170 runtime1.init(modContext);
171 runtime1.startUp(modContext);
172 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
173
174 // compile low-level intents into flow entry installation plan
175 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Brian O'Connor12861f72014-02-19 20:40:32 -0800176 List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800177
178 // show results step1
179 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800180 System.out.println(plan);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800181
182 // link down
183 ((MockNetworkGraph)g).removeLink(1L, 2L, 9L, 1L); // This link is used by the intent "1"
184 LinkEvent linkEvent = new LinkEvent(1L, 2L, 9L, 1L);
185 runtime1.removeLinkEvent(linkEvent);
186 ((MockNetworkGraph)g).removeLink(9L, 1L, 1L, 2L);
187 linkEvent = new LinkEvent(9L, 1L, 1L, 2L);
188 runtime1.removeLinkEvent(linkEvent);
189
190 System.out.println("Link goes down.");
191
192 // show results step2
193 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor12861f72014-02-19 20:40:32 -0800194 // TODO: show results of plan computation
Toshio Koide0c9106d2014-02-19 15:26:38 -0800195 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800196}