blob: c12a718d41970bd848d23fa83f0cf8d976a4cae5 [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 Koide27ffd412014-02-18 19:15:27 -080015import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
16import net.onrc.onos.ofcontroller.networkgraph.Link;
17import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Toshio Koidec87810e2014-02-11 13:03:21 -080018
Toshio Koide27ffd412014-02-18 19:15:27 -080019import org.easymock.EasyMock;
Toshio Koidec87810e2014-02-11 13:03:21 -080020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23
24/**
25 * @author Toshio Koide (t-koide@onlab.us)
26 */
27public class UseCaseTest {
Toshio Koide27ffd412014-02-18 19:15:27 -080028 private NetworkGraph g;
29 private FloodlightModuleContext modContext;
30 private IDatagridService datagridService;
31 private INetworkGraphService networkGraphService;
32 @SuppressWarnings("rawtypes")
33 private IEventChannel eventChannel;
Toshio Koidec87810e2014-02-11 13:03:21 -080034
Toshio Koide27ffd412014-02-18 19:15:27 -080035 @SuppressWarnings("unchecked")
Toshio Koidec87810e2014-02-11 13:03:21 -080036 @Before
37 public void setUp() {
Toshio Koideebdbb622014-02-12 20:28:38 -080038 MockNetworkGraph graph = new MockNetworkGraph();
39 graph.createSampleTopology();
40 g = graph;
Toshio Koide27ffd412014-02-18 19:15:27 -080041
42 datagridService = EasyMock.createMock(IDatagridService.class);
43 networkGraphService = EasyMock.createMock(INetworkGraphService.class);
44 modContext = EasyMock.createMock(FloodlightModuleContext.class);
45 eventChannel = EasyMock.createMock(IEventChannel.class);
46
47 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IDatagridService.class)))
48 .andReturn(datagridService).once();
49 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(INetworkGraphService.class)))
50 .andReturn(networkGraphService).once();
51
52 networkGraphService.getNetworkGraph();
53 EasyMock.expectLastCall().andReturn(g).anyTimes();
54
55 EasyMock.expect(datagridService.createChannel("onos.pathintent", byte[].class, IntentOperationList.class))
56 .andReturn(eventChannel).once();
57
58 EasyMock.replay(datagridService);
59 EasyMock.replay(networkGraphService);
60 EasyMock.replay(modContext);
Toshio Koidec87810e2014-02-11 13:03:21 -080061 }
62
63 @After
64 public void tearDown() {
Toshio Koide27ffd412014-02-18 19:15:27 -080065 EasyMock.verify(datagridService);
66 EasyMock.verify(networkGraphService);
67 EasyMock.verify(modContext);
Toshio Koidec87810e2014-02-11 13:03:21 -080068 }
69
Toshio Koide4f308732014-02-18 15:19:48 -080070 private void showResult(PathIntentMap intents) {
71 for (Intent intent: intents.getAllIntents()) {
72 PathIntent pathIntent = (PathIntent)intent;
Toshio Koidec87810e2014-02-11 13:03:21 -080073 System.out.println("Parent intent: " + pathIntent.getParentIntent().toString());
74 System.out.println("Path:");
Toshio Koidec406e792014-02-14 16:52:42 -080075 for (Link link: pathIntent.getPath(g)) {
Toshio Koidec87810e2014-02-11 13:03:21 -080076 System.out.printf("%s --(%f/%f)--> %s\n",
77 link.getSourcePort(),
78 link.getCapacity() - intents.getAvailableBandwidth(link),
79 link.getCapacity(),
80 link.getDestinationPort());
81 }
82 }
83 }
84
85 @Test
Toshio Koide27ffd412014-02-18 19:15:27 -080086 public void useCase1() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -080087 // create shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -080088 IntentOperationList opList = new IntentOperationList();
89 opList.add(Operator.ADD, new ShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 4L));
90 opList.add(Operator.ADD, new ShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 5L));
91 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 6L));
Toshio Koidec87810e2014-02-11 13:03:21 -080092
Toshio Koide27ffd412014-02-18 19:15:27 -080093 // compile high-level intent operations into low-level intent operations (calculate paths)
94 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
95 runtime1.init(modContext);
96 runtime1.startUp(modContext);
97 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
98
Brian O'Connor7f8e3012014-02-15 23:59:29 -080099 // compile low-level intents into flow entry installation plan
100 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Toshio Koide27ffd412014-02-18 19:15:27 -0800101 runtime2.addIntents((PathIntentMap) runtime1.getPathIntents()); // TODO use pathIntentOpList
Toshio Koidec87810e2014-02-11 13:03:21 -0800102
103 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800104 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800105 System.out.println(runtime2.getPlan());
Toshio Koidec87810e2014-02-11 13:03:21 -0800106 }
107
108 @Test
Toshio Koide27ffd412014-02-18 19:15:27 -0800109 public void useCase2() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800110 // create constrained shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800111 IntentOperationList opList = new IntentOperationList();
112 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 17L, 400.0));
113 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 18L, 400.0));
114 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 19L, 400.0));
115 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("4", 3L, 20L, 4L, 8L, 20L, 20L, 400.0));
116 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 21L, 400.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800117
Toshio Koide27ffd412014-02-18 19:15:27 -0800118 // compile high-level intent operations into low-level intent operations (calculate paths)
119 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
120 runtime1.init(modContext);
121 runtime1.startUp(modContext);
122 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800123
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800124 // compile low-level intents into flow entry installation plan
125 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Toshio Koide27ffd412014-02-18 19:15:27 -0800126 runtime2.addIntents((PathIntentMap) runtime1.getPathIntents()); // TODO use pathIntentOpList
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800127
Toshio Koidec87810e2014-02-11 13:03:21 -0800128 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800129 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800130 System.out.println(runtime2.getPlan());
Toshio Koidec87810e2014-02-11 13:03:21 -0800131 }
132
133 @Test
Toshio Koide27ffd412014-02-18 19:15:27 -0800134 public void useCase3() throws FloodlightModuleException {
Toshio Koidec87810e2014-02-11 13:03:21 -0800135 // create constrained & not best effort shortest path intents
Toshio Koide27ffd412014-02-18 19:15:27 -0800136 IntentOperationList opList = new IntentOperationList();
137 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("1", 1L, 20L, 1L, 4L, 20L, 6L, 600.0));
138 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("2", 2L, 20L, 2L, 6L, 20L, 7L, 600.0));
139 opList.add(Operator.ADD, new ShortestPathIntent("3", 4L, 20L, 3L, 8L, 20L, 8L));
140 opList.add(Operator.ADD, new ShortestPathIntent("4", 4L, 20L, 4L, 8L, 20L, 9L));
141 opList.add(Operator.ADD, new ConstrainedShortestPathIntent("5", 4L, 20L, 5L, 8L, 20L, 10L, 600.0));
Toshio Koidec87810e2014-02-11 13:03:21 -0800142
Toshio Koide27ffd412014-02-18 19:15:27 -0800143 // compile high-level intent operations into low-level intent operations (calculate paths)
144 PathCalcRuntimeModule runtime1 = new PathCalcRuntimeModule();
145 runtime1.init(modContext);
146 runtime1.startUp(modContext);
147 IntentOperationList pathIntentOpList = runtime1.executeIntentOperations(opList);
Toshio Koidec87810e2014-02-11 13:03:21 -0800148
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800149 // compile low-level intents into flow entry installation plan
150 PlanCalcRuntime runtime2 = new PlanCalcRuntime(g);
Toshio Koide27ffd412014-02-18 19:15:27 -0800151 runtime2.addIntents((PathIntentMap) runtime1.getPathIntents()); // TODO use pathIntentOpList
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800152
Toshio Koidec87810e2014-02-11 13:03:21 -0800153 // show results
Toshio Koide27ffd412014-02-18 19:15:27 -0800154 showResult((PathIntentMap) runtime1.getPathIntents());
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800155 System.out.println(runtime2.getPlan());
Toshio Koidec87810e2014-02-11 13:03:21 -0800156 }
157}