blob: 02703fc845d44c2c3d8197ad66b67c9fcc2c9d8a [file] [log] [blame]
Toshio Koidec87810e2014-02-11 13:03:21 -08001package net.onrc.onos.intent.runtime;
2
Toshio Koide27ffd412014-02-18 19:15:27 -08003import net.floodlightcontroller.core.module.FloodlightModuleContext;
4import net.floodlightcontroller.core.module.FloodlightModuleException;
5import net.onrc.onos.datagrid.IDatagridService;
6import net.onrc.onos.datagrid.IEventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -08007import net.onrc.onos.intent.ConstrainedShortestPathIntent;
8import net.onrc.onos.intent.Intent;
Toshio Koide27ffd412014-02-18 19:15:27 -08009import net.onrc.onos.intent.IntentOperation.Operator;
10import net.onrc.onos.intent.IntentOperationList;
Toshio Koideebdbb622014-02-12 20:28:38 -080011import net.onrc.onos.intent.MockNetworkGraph;
Toshio Koidec87810e2014-02-11 13:03:21 -080012import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080013import net.onrc.onos.intent.PathIntentMap;
Toshio Koidec87810e2014-02-11 13:03:21 -080014import net.onrc.onos.intent.ShortestPathIntent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080015import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080016import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080017import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koide27ffd412014-02-18 19:15:27 -080018import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Toshio Koidec87810e2014-02-11 13:03:21 -080019
Toshio Koide27ffd412014-02-18 19:15:27 -080020import org.easymock.EasyMock;
Toshio Koidec87810e2014-02-11 13:03:21 -080021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24
25/**
26 * @author Toshio Koide (t-koide@onlab.us)
27 */
28public class UseCaseTest {
Toshio Koide27ffd412014-02-18 19:15:27 -080029 private NetworkGraph g;
30 private FloodlightModuleContext modContext;
31 private IDatagridService datagridService;
32 private INetworkGraphService networkGraphService;
33 @SuppressWarnings("rawtypes")
34 private IEventChannel eventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080035
Toshio Koide27ffd412014-02-18 19:15:27 -080036 @SuppressWarnings("unchecked")
Toshio Koidec87810e2014-02-11 13:03:21 -080037 @Before
38 public void setUp() {
Toshio Koideebdbb622014-02-12 20:28:38 -080039 MockNetworkGraph graph = new MockNetworkGraph();
40 graph.createSampleTopology();
41 g = graph;
Toshio Koide27ffd412014-02-18 19:15:27 -080042
43 datagridService = EasyMock.createMock(IDatagridService.class);
44 networkGraphService = EasyMock.createMock(INetworkGraphService.class);
45 modContext = EasyMock.createMock(FloodlightModuleContext.class);
46 eventChannel = EasyMock.createMock(IEventChannel.class);
47
48 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IDatagridService.class)))
49 .andReturn(datagridService).once();
50 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(INetworkGraphService.class)))
51 .andReturn(networkGraphService).once();
Toshio Koide0c9106d2014-02-19 15:26:38 -080052
53 EasyMock.expect(networkGraphService.getNetworkGraph()).andReturn(g).anyTimes();
54 networkGraphService.registerNetworkGraphListener(EasyMock.anyObject(INetworkGraphListener.class));
55 EasyMock.expectLastCall();
56
57 EasyMock.expect(datagridService.createChannel("onos.pathintent", String.class, IntentOperationList.class))
Toshio Koide27ffd412014-02-18 19:15:27 -080058 .andReturn(eventChannel).once();
59
60 EasyMock.replay(datagridService);
61 EasyMock.replay(networkGraphService);
62 EasyMock.replay(modContext);
Toshio Koidec87810e2014-02-11 13:03:21 -080063 }
64
65 @After
66 public void tearDown() {
Toshio Koide27ffd412014-02-18 19:15:27 -080067 EasyMock.verify(datagridService);
68 EasyMock.verify(networkGraphService);
69 EasyMock.verify(modContext);
Toshio Koidec87810e2014-02-11 13:03:21 -080070 }
71
Toshio Koide4f308732014-02-18 15:19:48 -080072 private void showResult(PathIntentMap intents) {
73 for (Intent intent: intents.getAllIntents()) {
74 PathIntent pathIntent = (PathIntent)intent;
Toshio Koidec87810e2014-02-11 13:03:21 -080075 System.out.println("Parent intent: " + pathIntent.getParentIntent().toString());
76 System.out.println("Path:");
Toshio Koided9fa2a82014-02-19 17:35:18 -080077 for (LinkEvent linkEvent: pathIntent.getPath()) {
Toshio Koide0c9106d2014-02-19 15:26:38 -080078 System.out.println(linkEvent);
Toshio Koidec87810e2014-02-11 13:03:21 -080079 }
80 }
81 }
82
83 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -080084 public void createShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -080085 // create shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -080086 IntentOperationList opList = new IntentOperationList();
87 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
88 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
89 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
Toshio Koidec87810e2014-02-11 13:03:21 -080090
Toshio Koide27ffd412014-02-18 19:15:27 -080091 // compile high-level intent operations into low-level intent operations (calculate paths)
92 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
93 runtime1.init(modContext);
94 runtime1.startUp(modContext);
95 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
96
Brian O'Connor7f8e3012014-02-15 23:59:29 -080097 // compile low-level intents into flow entry installation plan
98 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Toshio Koide103ccb22014-02-18 21:51:25 -080099 runtime2.addIntents(pathIntentOpList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800100
101 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800102 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800103 System.out.println(runtime2.getPlan());
Toshio Koidec87810e2014-02-11 13:03:21 -0800104 }
105
106 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800107 public void createConstrainedShortestPaths() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800108 // create constrained shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800109 IntentOperationList opList = new IntentOperationList();
110 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 17L, 400.0));
111 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 18L, 400.0));
112 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 19L, 400.0));
113 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 3L, 20L, 4L, 8L, 20L, 20L, 400.0));
114 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 21L, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800115
Toshio Koide27ffd412014-02-18 19:15:27 -0800116 // compile high-level intent operations into low-level intent operations (calculate paths)
117 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
118 runtime1.init(modContext);
119 runtime1.startUp(modContext);
120 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800121
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800122 // compile low-level intents into flow entry installation plan
123 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Toshio Koide103ccb22014-02-18 21:51:25 -0800124 runtime2.addIntents(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800125
Toshio Koidec87810e2014-02-11 13:03:21 -0800126 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800127 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800128 System.out.println(runtime2.getPlan());
Toshio Koidec87810e2014-02-11 13:03:21 -0800129 }
130
131 @Test
Toshio Koide0c9106d2014-02-19 15:26:38 -0800132 public void createMixedShortestPaths() throws FloodlightModuleException {
133 // create constrained & best effort shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800134 IntentOperationList opList = new IntentOperationList();
135 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 6L, 600.0));
136 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 7L, 600.0));
137 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 8L));
138 opList.add(Operator.ADD, new ShortestPathIntent("4", 4L, 20L, 4L, 8L, 20L, 9L));
139 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 10L, 600.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800140
Toshio Koide27ffd412014-02-18 19:15:27 -0800141 // compile high-level intent operations into low-level intent operations (calculate paths)
142 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
143 runtime1.init(modContext);
144 runtime1.startUp(modContext);
145 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800146
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800147 // compile low-level intents into flow entry installation plan
148 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Toshio Koide103ccb22014-02-18 21:51:25 -0800149 runtime2.addIntents(pathIntentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800150
Toshio Koidec87810e2014-02-11 13:03:21 -0800151 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800152 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800153 System.out.println(runtime2.getPlan());
Toshio Koidec87810e2014-02-11 13:03:21 -0800154 }
Toshio Koided9fa2a82014-02-19 17:35:18 -0800155
Toshio Koide0c9106d2014-02-19 15:26:38 -0800156 @Test
157 public void rerouteShortestPaths() throws FloodlightModuleException {
158 // create shortest path intents
159 IntentOperationList opList = new IntentOperationList();
160 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
161 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
162 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
163
164 // compile high-level intent operations into low-level intent operations (calculate paths)
165 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
166 runtime1.init(modContext);
167 runtime1.startUp(modContext);
168 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
169
170 // compile low-level intents into flow entry installation plan
171 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
172 runtime2.addIntents(pathIntentOpList);
173
174 // show results step1
175 showResult((PathIntentMap) runtime1.getPathIntents());
176 System.out.println(runtime2.getPlan());
177
178 // link down
179 ((MockNetworkGraph)g).removeLink(1L, 2L, 9L, 1L); // This link is used by the intent "1"
180 LinkEvent linkEvent = new LinkEvent(1L, 2L, 9L, 1L);
181 runtime1.removeLinkEvent(linkEvent);
182 ((MockNetworkGraph)g).removeLink(9L, 1L, 1L, 2L);
183 linkEvent = new LinkEvent(9L, 1L, 1L, 2L);
184 runtime1.removeLinkEvent(linkEvent);
185
186 System.out.println("Link goes down.");
187
188 // show results step2
189 showResult((PathIntentMap) runtime1.getPathIntents());
Toshio Koided9fa2a82014-02-19 17:35:18 -0800190 System.out.println(runtime2.getPlan());
Toshio Koide0c9106d2014-02-19 15:26:38 -0800191 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800192}