Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent.runtime; |
| 2 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 3 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 4 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 5 | import net.onrc.onos.datagrid.IDatagridService; |
| 6 | import net.onrc.onos.datagrid.IEventChannel; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 7 | import net.onrc.onos.intent.ConstrainedShortestPathIntent; |
| 8 | import net.onrc.onos.intent.Intent; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 9 | import net.onrc.onos.intent.IntentOperation.Operator; |
| 10 | import net.onrc.onos.intent.IntentOperationList; |
Toshio Koide | ebdbb62 | 2014-02-12 20:28:38 -0800 | [diff] [blame] | 11 | import net.onrc.onos.intent.MockNetworkGraph; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 12 | import net.onrc.onos.intent.PathIntent; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 13 | import net.onrc.onos.intent.PathIntentMap; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 14 | import net.onrc.onos.intent.ShortestPathIntent; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 15 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 16 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 17 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 18 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 19 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 20 | import org.easymock.EasyMock; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | |
| 25 | /** |
| 26 | * @author Toshio Koide (t-koide@onlab.us) |
| 27 | */ |
| 28 | public class UseCaseTest { |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 29 | private NetworkGraph g; |
| 30 | private FloodlightModuleContext modContext; |
| 31 | private IDatagridService datagridService; |
| 32 | private INetworkGraphService networkGraphService; |
| 33 | @SuppressWarnings("rawtypes") |
| 34 | private IEventChannel eventChannel; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 35 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 36 | @SuppressWarnings("unchecked") |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 37 | @Before |
| 38 | public void setUp() { |
Toshio Koide | ebdbb62 | 2014-02-12 20:28:38 -0800 | [diff] [blame] | 39 | MockNetworkGraph graph = new MockNetworkGraph(); |
| 40 | graph.createSampleTopology(); |
| 41 | g = graph; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 42 | |
| 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 Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 52 | |
| 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 Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 58 | .andReturn(eventChannel).once(); |
| 59 | |
| 60 | EasyMock.replay(datagridService); |
| 61 | EasyMock.replay(networkGraphService); |
| 62 | EasyMock.replay(modContext); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @After |
| 66 | public void tearDown() { |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 67 | EasyMock.verify(datagridService); |
| 68 | EasyMock.verify(networkGraphService); |
| 69 | EasyMock.verify(modContext); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 72 | private void showResult(PathIntentMap intents) { |
| 73 | for (Intent intent: intents.getAllIntents()) { |
| 74 | PathIntent pathIntent = (PathIntent)intent; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 75 | System.out.println("Parent intent: " + pathIntent.getParentIntent().toString()); |
| 76 | System.out.println("Path:"); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 77 | for (LinkEvent linkEvent: pathIntent.getPath()) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 78 | System.out.println(linkEvent); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Test |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 84 | public void createShortestPaths() throws FloodlightModuleException { |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 85 | // create shortest path intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 86 | 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 90 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 91 | // 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'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 97 | // compile low-level intents into flow entry installation plan |
| 98 | PlanCalcRuntime runtime2 = new PlanCalcRuntime(g); |
Toshio Koide | 103ccb2 | 2014-02-18 21:51:25 -0800 | [diff] [blame] | 99 | runtime2.addIntents(pathIntentOpList); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 100 | |
| 101 | // show results |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 102 | showResult((PathIntentMap) runtime1.getPathIntents()); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 103 | System.out.println(runtime2.getPlan()); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Test |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 107 | public void createConstrainedShortestPaths() throws FloodlightModuleException { |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 108 | // create constrained shortest path intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 109 | 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 115 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 116 | // 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 121 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 122 | // compile low-level intents into flow entry installation plan |
| 123 | PlanCalcRuntime runtime2 = new PlanCalcRuntime(g); |
Toshio Koide | 103ccb2 | 2014-02-18 21:51:25 -0800 | [diff] [blame] | 124 | runtime2.addIntents(pathIntentOpList); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 125 | |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 126 | // show results |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 127 | showResult((PathIntentMap) runtime1.getPathIntents()); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 128 | System.out.println(runtime2.getPlan()); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Test |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 132 | public void createMixedShortestPaths() throws FloodlightModuleException { |
| 133 | // create constrained & best effort shortest path intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 134 | 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 140 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 141 | // 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 Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 146 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 147 | // compile low-level intents into flow entry installation plan |
| 148 | PlanCalcRuntime runtime2 = new PlanCalcRuntime(g); |
Toshio Koide | 103ccb2 | 2014-02-18 21:51:25 -0800 | [diff] [blame] | 149 | runtime2.addIntents(pathIntentOpList); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 150 | |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 151 | // show results |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 152 | showResult((PathIntentMap) runtime1.getPathIntents()); |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 153 | System.out.println(runtime2.getPlan()); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 154 | } |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 155 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 156 | @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 Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 190 | System.out.println(runtime2.getPlan()); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 191 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 192 | } |