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; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 19 | import net.onrc.onos.intent.persist.PersistIntent; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 21 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 24 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 25 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 26 | import org.easymock.EasyMock; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 30 | import org.junit.runner.RunWith; |
| 31 | import org.powermock.api.easymock.PowerMock; |
| 32 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 33 | import org.powermock.modules.junit4.PowerMockRunner; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * @author Toshio Koide (t-koide@onlab.us) |
| 37 | */ |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 38 | @RunWith(PowerMockRunner.class) |
| 39 | @PrepareForTest(PathCalcRuntimeModule.class) |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 40 | public class UseCaseTest { |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 41 | private NetworkGraph g; |
| 42 | private FloodlightModuleContext modContext; |
| 43 | private IDatagridService datagridService; |
| 44 | private INetworkGraphService networkGraphService; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 45 | private IControllerRegistryService controllerRegistryService; |
| 46 | private PersistIntent persistIntent; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 47 | @SuppressWarnings("rawtypes") |
| 48 | private IEventChannel eventChannel; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 49 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 50 | @SuppressWarnings("unchecked") |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 51 | @Before |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 52 | public void setUp() throws Exception { |
Toshio Koide | ebdbb62 | 2014-02-12 20:28:38 -0800 | [diff] [blame] | 53 | MockNetworkGraph graph = new MockNetworkGraph(); |
| 54 | graph.createSampleTopology(); |
| 55 | g = graph; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 56 | |
| 57 | datagridService = EasyMock.createMock(IDatagridService.class); |
| 58 | networkGraphService = EasyMock.createMock(INetworkGraphService.class); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 59 | controllerRegistryService = EasyMock.createMock(IControllerRegistryService.class); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 60 | modContext = EasyMock.createMock(FloodlightModuleContext.class); |
| 61 | eventChannel = EasyMock.createMock(IEventChannel.class); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 62 | persistIntent = PowerMock.createMock(PersistIntent.class); |
| 63 | |
| 64 | PowerMock.expectNew(PersistIntent.class, |
| 65 | EasyMock.anyObject(IControllerRegistryService.class), |
| 66 | EasyMock.anyObject(INetworkGraphService.class)).andReturn(persistIntent); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 67 | |
| 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 Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 72 | 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 Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 77 | |
| 78 | EasyMock.expect(networkGraphService.getNetworkGraph()).andReturn(g).anyTimes(); |
| 79 | networkGraphService.registerNetworkGraphListener(EasyMock.anyObject(INetworkGraphListener.class)); |
| 80 | EasyMock.expectLastCall(); |
| 81 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 82 | EasyMock.expect(datagridService.createChannel("onos.pathintent", Long.class, IntentOperationList.class)) |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 83 | .andReturn(eventChannel).once(); |
| 84 | |
| 85 | EasyMock.replay(datagridService); |
| 86 | EasyMock.replay(networkGraphService); |
| 87 | EasyMock.replay(modContext); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 88 | EasyMock.replay(controllerRegistryService); |
| 89 | PowerMock.replay(persistIntent, PersistIntent.class); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @After |
| 93 | public void tearDown() { |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 94 | EasyMock.verify(datagridService); |
| 95 | EasyMock.verify(networkGraphService); |
| 96 | EasyMock.verify(modContext); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 97 | EasyMock.verify(controllerRegistryService); |
| 98 | PowerMock.verify(persistIntent, PersistIntent.class); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 101 | private void showResult(PathIntentMap intents) { |
| 102 | for (Intent intent: intents.getAllIntents()) { |
| 103 | PathIntent pathIntent = (PathIntent)intent; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 104 | System.out.println("Parent intent: " + pathIntent.getParentIntent().toString()); |
| 105 | System.out.println("Path:"); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 106 | for (LinkEvent linkEvent: pathIntent.getPath()) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 107 | System.out.println(linkEvent); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Test |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 113 | public void createShortestPaths() throws FloodlightModuleException { |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 114 | // create shortest path intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 115 | 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 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); |
| 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); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 129 | |
| 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 createConstrainedShortestPaths() throws FloodlightModuleException { |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 137 | // create constrained 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, 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 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 | } |
| 159 | |
| 160 | @Test |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 161 | public void createMixedShortestPaths() throws FloodlightModuleException { |
| 162 | // create constrained & best effort shortest path intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 163 | 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 169 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 170 | // 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 175 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 176 | // compile low-level intents into flow entry installation plan |
| 177 | PlanCalcRuntime runtime2 = new PlanCalcRuntime(g); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 178 | List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 179 | |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 180 | // show results |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 181 | showResult((PathIntentMap) runtime1.getPathIntents()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 182 | System.out.println(plan); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 183 | } |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 184 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 185 | @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'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 201 | List<Set<FlowEntry>> plan = runtime2.computePlan(pathIntentOpList); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 202 | |
| 203 | // show results step1 |
| 204 | showResult((PathIntentMap) runtime1.getPathIntents()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 205 | System.out.println(plan); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 206 | |
| 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'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 219 | // TODO: show results of plan computation |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 220 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 221 | } |