Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent.runtime; |
| 2 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 3 | import java.util.List; |
| 4 | import java.util.Set; |
| 5 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 6 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 7 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 8 | import net.onrc.onos.datagrid.IDatagridService; |
| 9 | import net.onrc.onos.datagrid.IEventChannel; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 10 | import net.onrc.onos.intent.ConstrainedShortestPathIntent; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 11 | import net.onrc.onos.intent.FlowEntry; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 12 | import net.onrc.onos.intent.Intent; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 13 | import net.onrc.onos.intent.IntentOperation.Operator; |
| 14 | import net.onrc.onos.intent.IntentOperationList; |
Toshio Koide | ebdbb62 | 2014-02-12 20:28:38 -0800 | [diff] [blame] | 15 | import net.onrc.onos.intent.MockNetworkGraph; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 16 | import net.onrc.onos.intent.PathIntent; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 17 | import net.onrc.onos.intent.PathIntentMap; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 18 | import net.onrc.onos.intent.ShortestPathIntent; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 19 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 21 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 23 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 24 | import org.easymock.EasyMock; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | |
| 29 | /** |
| 30 | * @author Toshio Koide (t-koide@onlab.us) |
| 31 | */ |
| 32 | public class UseCaseTest { |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 33 | private NetworkGraph g; |
| 34 | private FloodlightModuleContext modContext; |
| 35 | private IDatagridService datagridService; |
| 36 | private INetworkGraphService networkGraphService; |
| 37 | @SuppressWarnings("rawtypes") |
| 38 | private IEventChannel eventChannel; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 39 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 40 | @SuppressWarnings("unchecked") |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 41 | @Before |
| 42 | public void setUp() { |
Toshio Koide | ebdbb62 | 2014-02-12 20:28:38 -0800 | [diff] [blame] | 43 | MockNetworkGraph graph = new MockNetworkGraph(); |
| 44 | graph.createSampleTopology(); |
| 45 | g = graph; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 46 | |
| 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 Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 56 | |
| 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 Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 62 | .andReturn(eventChannel).once(); |
| 63 | |
| 64 | EasyMock.replay(datagridService); |
| 65 | EasyMock.replay(networkGraphService); |
| 66 | EasyMock.replay(modContext); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @After |
| 70 | public void tearDown() { |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 71 | EasyMock.verify(datagridService); |
| 72 | EasyMock.verify(networkGraphService); |
| 73 | EasyMock.verify(modContext); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 76 | private void showResult(PathIntentMap intents) { |
| 77 | for (Intent intent: intents.getAllIntents()) { |
| 78 | PathIntent pathIntent = (PathIntent)intent; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 79 | System.out.println("Parent intent: " + pathIntent.getParentIntent().toString()); |
| 80 | System.out.println("Path:"); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 81 | for (LinkEvent linkEvent: pathIntent.getPath()) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 82 | System.out.println(linkEvent); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @Test |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 88 | public void createShortestPaths() throws FloodlightModuleException { |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 89 | // create shortest path intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 90 | 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 94 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 95 | // 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'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 101 | // compile low-level intents into flow entry installation plan |
| 102 | PlanCalcRuntime runtime2 = new PlanCalcRuntime(g); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 103 | List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 104 | |
| 105 | // show results |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 106 | showResult((PathIntentMap) runtime1.getPathIntents()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 107 | System.out.println(plan); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 111 | public void createConstrainedShortestPaths() throws FloodlightModuleException { |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 112 | // create constrained shortest path intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 113 | 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 119 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 120 | // 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 125 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 126 | // compile low-level intents into flow entry installation plan |
| 127 | PlanCalcRuntime runtime2 = new PlanCalcRuntime(g); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 128 | List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 129 | |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 130 | // show results |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 131 | showResult((PathIntentMap) runtime1.getPathIntents()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 132 | System.out.println(plan); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Test |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 136 | public void createMixedShortestPaths() throws FloodlightModuleException { |
| 137 | // create constrained & best effort shortest path intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 138 | 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 144 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 145 | // 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 150 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 151 | // compile low-level intents into flow entry installation plan |
| 152 | PlanCalcRuntime runtime2 = new PlanCalcRuntime(g); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 153 | List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 154 | |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 155 | // show results |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 156 | showResult((PathIntentMap) runtime1.getPathIntents()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 157 | System.out.println(plan); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 158 | } |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 159 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 160 | @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'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 176 | List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 177 | |
| 178 | // show results step1 |
| 179 | showResult((PathIntentMap) runtime1.getPathIntents()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 180 | System.out.println(plan); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 181 | |
| 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'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 194 | // TODO: show results of plan computation |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 195 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 196 | } |