gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.routing.impl; |
| 17 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 20 | import org.easymock.EasyMock; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Ethernet; |
| 24 | import org.onlab.packet.Ip4Address; |
| 25 | import org.onlab.packet.Ip4Prefix; |
| 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.IpPrefix; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onosproject.TestApplicationId; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 31 | import org.onosproject.cfg.ComponentConfigService; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.core.CoreServiceAdapter; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 35 | import org.onosproject.incubator.net.intf.Interface; |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 36 | import org.onosproject.incubator.net.intf.InterfaceListener; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 37 | import org.onosproject.incubator.net.intf.InterfaceService; |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 38 | import org.onosproject.incubator.net.intf.InterfaceServiceAdapter; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 39 | import org.onosproject.net.ConnectPoint; |
| 40 | import org.onosproject.net.DeviceId; |
| 41 | import org.onosproject.net.PortNumber; |
| 42 | import org.onosproject.net.config.NetworkConfigService; |
| 43 | import org.onosproject.net.device.DeviceListener; |
| 44 | import org.onosproject.net.device.DeviceService; |
| 45 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 46 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 47 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 48 | import org.onosproject.net.flow.TrafficSelector; |
| 49 | import org.onosproject.net.flow.TrafficTreatment; |
| 50 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 51 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 52 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 53 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 54 | import org.onosproject.net.flowobjective.NextObjective; |
| 55 | import org.onosproject.net.host.InterfaceIpAddress; |
| 56 | import org.onosproject.net.intent.AbstractIntentTest; |
| 57 | import org.onosproject.routing.FibEntry; |
| 58 | import org.onosproject.routing.FibListener; |
| 59 | import org.onosproject.routing.FibUpdate; |
| 60 | import org.onosproject.routing.RoutingService; |
| 61 | import org.onosproject.routing.RoutingServiceAdapter; |
| 62 | import org.onosproject.routing.config.RouterConfig; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 63 | import org.osgi.service.component.ComponentContext; |
| 64 | |
| 65 | import java.util.ArrayList; |
| 66 | import java.util.Collections; |
| 67 | import java.util.Dictionary; |
| 68 | import java.util.HashSet; |
| 69 | import java.util.List; |
| 70 | import java.util.Set; |
| 71 | |
| 72 | import static org.easymock.EasyMock.anyObject; |
| 73 | import static org.easymock.EasyMock.createMock; |
| 74 | import static org.easymock.EasyMock.expect; |
| 75 | import static org.easymock.EasyMock.replay; |
| 76 | import static org.easymock.EasyMock.reset; |
| 77 | import static org.easymock.EasyMock.verify; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Unit tests for SingleSwitchFibInstaller. |
| 81 | */ |
| 82 | public class SingleSwitchFibInstallerTest extends AbstractIntentTest { |
| 83 | |
| 84 | //for interface service setup |
| 85 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 86 | DeviceId.deviceId("of:0000000000000001"), |
| 87 | PortNumber.portNumber(1)); |
| 88 | |
| 89 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 90 | DeviceId.deviceId("of:0000000000000002"), |
| 91 | PortNumber.portNumber(1)); |
| 92 | |
| 93 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 94 | DeviceId.deviceId("of:0000000000000003"), |
| 95 | PortNumber.portNumber(1)); |
| 96 | |
| 97 | private static final ConnectPoint SW4_ETH1 = new ConnectPoint( |
| 98 | DeviceId.deviceId("of:0000000000000004"), |
| 99 | PortNumber.portNumber(1)); |
| 100 | |
| 101 | private DeviceId deviceId = DeviceId.deviceId("of:0000000000000001"); |
| 102 | private final Set<Interface> interfaces = Sets.newHashSet(); |
| 103 | private InterfaceService interfaceService; |
| 104 | private NetworkConfigService networkConfigService; |
| 105 | private FlowObjectiveService flowObjectiveService; |
| 106 | private DeviceService deviceService; |
| 107 | private static final ApplicationId APPID = TestApplicationId.create("update fib"); |
| 108 | private FibListener fibListener; |
| 109 | private DeviceListener deviceListener; |
| 110 | private CoreService coreService; |
| 111 | private RouterConfig routerConfig; |
| 112 | private RoutingService routingService; |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 113 | private SingleSwitchFibInstaller sSfibInstaller; |
| 114 | private InterfaceListener interfaceListener; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 115 | |
| 116 | @Before |
| 117 | public void setUp() throws Exception { |
| 118 | super.setUp(); |
| 119 | sSfibInstaller = new SingleSwitchFibInstaller(); |
| 120 | |
| 121 | //component config service |
| 122 | ComponentConfigService mockComponenetConfigServ = EasyMock.createMock(ComponentConfigService.class); |
| 123 | expect(mockComponenetConfigServ.getProperties(anyObject())).andReturn(ImmutableSet.of()); |
| 124 | mockComponenetConfigServ.registerProperties(sSfibInstaller.getClass()); |
| 125 | EasyMock.expectLastCall(); |
| 126 | mockComponenetConfigServ.unregisterProperties(sSfibInstaller.getClass(), false); |
| 127 | EasyMock.expectLastCall(); |
| 128 | expect(mockComponenetConfigServ.getProperties(anyObject())).andReturn(ImmutableSet.of()); |
| 129 | sSfibInstaller.componentConfigService = mockComponenetConfigServ; |
| 130 | replay(mockComponenetConfigServ); |
| 131 | |
| 132 | //component context |
| 133 | ComponentContext mockContext = EasyMock.createMock(ComponentContext.class); |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 134 | Dictionary properties = null; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 135 | expect(mockContext.getProperties()).andReturn(properties); |
| 136 | replay(mockContext); |
| 137 | |
| 138 | coreService = new TestCoreService(); |
| 139 | routingService = new TestRoutingService(); |
| 140 | routerConfig = new TestRouterConfig(); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 141 | //interfaceService = createMock(InterfaceService.class); |
| 142 | interfaceService = new TestInterfaceService(); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 143 | networkConfigService = createMock(NetworkConfigService.class); |
| 144 | flowObjectiveService = createMock(FlowObjectiveService.class); |
| 145 | deviceService = new TestDeviceService(); |
| 146 | |
| 147 | sSfibInstaller.networkConfigService = networkConfigService; |
| 148 | sSfibInstaller.interfaceService = interfaceService; |
| 149 | sSfibInstaller.flowObjectiveService = flowObjectiveService; |
| 150 | sSfibInstaller.coreService = coreService; |
| 151 | sSfibInstaller.routingService = new TestRoutingService(); |
| 152 | sSfibInstaller.deviceService = deviceService; |
| 153 | |
| 154 | setUpNetworkConfigService(); |
| 155 | setUpInterfaceService(); |
| 156 | sSfibInstaller.activate(mockContext); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets up InterfaceService. |
| 161 | */ |
| 162 | private void setUpInterfaceService() { |
| 163 | Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet(); |
| 164 | interfaceIpAddresses1.add(new InterfaceIpAddress( |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 165 | IpAddress.valueOf("192.168.10.1"), |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 166 | IpPrefix.valueOf("192.168.10.0/24"))); |
| 167 | Interface sw1Eth1 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH1, |
| 168 | interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"), |
| 169 | VlanId.NONE); |
| 170 | interfaces.add(sw1Eth1); |
| 171 | |
| 172 | Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet(); |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 173 | interfaceIpAddresses2.add(new InterfaceIpAddress(IpAddress.valueOf("192.168.20.1"), |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 174 | IpPrefix.valueOf("192.168.20.0/24"))); |
| 175 | Interface sw2Eth1 = new Interface(SW2_ETH1.deviceId().toString(), SW2_ETH1, |
| 176 | interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"), |
| 177 | VlanId.NONE); |
| 178 | interfaces.add(sw2Eth1); |
| 179 | |
| 180 | Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet(); |
| 181 | interfaceIpAddresses3.add( |
| 182 | new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"), |
| 183 | IpPrefix.valueOf("192.168.30.0/24"))); |
| 184 | Interface sw3Eth1 = new Interface(SW3_ETH1.deviceId().toString(), SW3_ETH1, |
| 185 | interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"), VlanId.NONE); |
| 186 | interfaces.add(sw3Eth1); |
| 187 | |
| 188 | InterfaceIpAddress interfaceIpAddress4 = |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 189 | new InterfaceIpAddress(IpAddress.valueOf("192.168.40.1"), |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 190 | IpPrefix.valueOf("192.168.40.0/24")); |
| 191 | |
| 192 | Interface sw4Eth1 = new Interface(SW4_ETH1.deviceId().toString(), SW4_ETH1, |
| 193 | Sets.newHashSet(interfaceIpAddress4), |
| 194 | MacAddress.valueOf("00:00:00:00:00:04"), |
| 195 | VlanId.vlanId((short) 1)); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 196 | interfaces.add(sw4Eth1); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Sets up NetworkConfigService. |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 201 | */ |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 202 | private void setUpNetworkConfigService() { |
| 203 | ApplicationId routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID); |
| 204 | expect(networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS)). |
| 205 | andReturn(routerConfig); |
| 206 | replay(networkConfigService); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Sets up FlowObjectiveService. |
| 211 | */ |
| 212 | private void setUpFlowObjectiveService() { |
| 213 | expect(flowObjectiveService.allocateNextId()).andReturn(11); |
| 214 | replay(flowObjectiveService); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Tests adding a FIB entry to the flowObjectiveService. |
| 219 | * |
| 220 | * We verify that the flowObjectiveService records the correct state and that the |
| 221 | * correct flow is submitted to the flowObjectiveService. |
| 222 | */ |
| 223 | @Test |
| 224 | public void testFibAdd() { |
| 225 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 226 | FibEntry fibEntry = new FibEntry(prefix, |
| 227 | Ip4Address.valueOf("192.168.10.1"), |
| 228 | MacAddress.valueOf("00:00:00:00:00:01")); |
| 229 | |
| 230 | //create the next Objective |
| 231 | Interface egressIntf = interfaceService.getMatchingInterface(fibEntry.nextHopIp()); |
| 232 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
| 233 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 234 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 235 | TrafficSelector.Builder metabuilder = null; |
| 236 | if (!egressIntf.vlan().equals(VlanId.NONE)) { |
| 237 | treatment.pushVlan() |
| 238 | .setVlanId(egressIntf.vlan()) |
| 239 | .setVlanPcp((byte) 0); |
| 240 | } else { |
| 241 | metabuilder = DefaultTrafficSelector.builder(); |
| 242 | metabuilder.matchVlanId(VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN)); |
| 243 | } |
| 244 | treatment.setOutput(PortNumber.portNumber(1)); |
| 245 | int nextId = 11; |
| 246 | NextObjective.Builder nextBuilder = DefaultNextObjective.builder() |
| 247 | .withId(nextId) |
| 248 | .addTreatment(treatment.build()) |
| 249 | .withType(NextObjective.Type.SIMPLE) |
| 250 | .fromApp(APPID); |
| 251 | if (metabuilder != null) { |
| 252 | nextBuilder.withMeta(metabuilder.build()); |
| 253 | } |
| 254 | |
| 255 | NextObjective nextObjective = nextBuilder.add(); |
| 256 | flowObjectiveService.next(deviceId, nextObjective); |
| 257 | |
| 258 | //set up the flowObjective |
| 259 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 260 | .matchEthType(Ethernet.TYPE_IPV4) |
| 261 | .matchIPDst(prefix) |
| 262 | .build(); |
| 263 | |
| 264 | int priority = prefix.prefixLength() * 5 + 100; |
| 265 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
| 266 | .fromApp(APPID) |
| 267 | .makePermanent() |
| 268 | .withSelector(selector) |
| 269 | .withPriority(priority) |
| 270 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 271 | |
| 272 | Integer nextId1 = 11; |
| 273 | fwdBuilder.nextStep(nextId1); |
| 274 | flowObjectiveService.forward(deviceId, fwdBuilder.add()); |
| 275 | EasyMock.expectLastCall().once(); |
| 276 | setUpFlowObjectiveService(); |
| 277 | |
| 278 | // Send in the UPDATE FibUpdate |
| 279 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry); |
| 280 | fibListener.update(Collections.singleton(fibUpdate), Collections.emptyList()); |
| 281 | verify(flowObjectiveService); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Tests adding a FIB entry with to a next hop in a VLAN. |
| 286 | * |
| 287 | * We verify that the flowObjectiveService records the correct state and that the |
| 288 | * correct flowObjectiveService is submitted to the flowObjectiveService. |
| 289 | */ |
| 290 | @Test |
| 291 | public void testFibAddWithVlan() { |
| 292 | IpPrefix prefix = Ip4Prefix.valueOf("3.3.3.0/24"); |
| 293 | FibEntry fibEntry = new FibEntry(prefix, |
| 294 | Ip4Address.valueOf("192.168.40.1"), |
| 295 | MacAddress.valueOf("00:00:00:00:00:04")); |
| 296 | |
| 297 | //create the next Objective |
| 298 | Interface egressIntf = interfaceService.getMatchingInterface(fibEntry.nextHopIp()); |
| 299 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
| 300 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:04")) |
| 301 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:04")); |
| 302 | TrafficSelector.Builder metabuilder = null; |
| 303 | if (!egressIntf.vlan().equals(VlanId.NONE)) { |
| 304 | treatment.pushVlan() |
| 305 | .setVlanId(egressIntf.vlan()) |
| 306 | .setVlanPcp((byte) 0); |
| 307 | } else { |
| 308 | metabuilder = DefaultTrafficSelector.builder(); |
| 309 | metabuilder.matchVlanId(VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN)); |
| 310 | } |
| 311 | treatment.setOutput(PortNumber.portNumber(1)); |
| 312 | int nextId = 11; |
| 313 | NextObjective.Builder nextBuilder = DefaultNextObjective.builder() |
| 314 | .withId(nextId) |
| 315 | .addTreatment(treatment.build()) |
| 316 | .withType(NextObjective.Type.SIMPLE) |
| 317 | .fromApp(APPID); |
| 318 | if (metabuilder != null) { |
| 319 | nextBuilder.withMeta(metabuilder.build()); |
| 320 | } |
| 321 | |
| 322 | NextObjective nextObjective = nextBuilder.add(); |
| 323 | flowObjectiveService.next(deviceId, nextObjective); |
| 324 | |
| 325 | //set up the flowObjective |
| 326 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 327 | .matchEthType(Ethernet.TYPE_IPV4) |
| 328 | .matchIPDst(prefix) |
| 329 | .build(); |
| 330 | |
| 331 | int priority = prefix.prefixLength() * 5 + 100; |
| 332 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
| 333 | .fromApp(APPID) |
| 334 | .makePermanent() |
| 335 | .withSelector(selector) |
| 336 | .withPriority(priority) |
| 337 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 338 | |
| 339 | Integer nextId1 = 11; |
| 340 | fwdBuilder.nextStep(nextId1); |
| 341 | flowObjectiveService.forward(deviceId, fwdBuilder.add()); |
| 342 | EasyMock.expectLastCall().once(); |
| 343 | setUpFlowObjectiveService(); |
| 344 | |
| 345 | // Send in the UPDATE FibUpdate |
| 346 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry); |
| 347 | fibListener.update(Collections.singleton(fibUpdate), Collections.emptyList()); |
| 348 | |
| 349 | verify(flowObjectiveService); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Tests updating a FIB entry. |
| 354 | * |
| 355 | * We verify that the flowObjectiveService records the correct state and that the |
| 356 | * correct flow is submitted to the flowObjectiveService. |
| 357 | */ |
| 358 | @Test |
| 359 | public void testFibUpdate() { |
| 360 | // Firstly add a route |
| 361 | testFibAdd(); |
| 362 | reset(flowObjectiveService); |
| 363 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 364 | // Start to construct a new route entry and new intent |
| 365 | FibEntry fibEntryUpdate = new FibEntry(prefix, |
| 366 | Ip4Address.valueOf("192.168.20.1"), |
| 367 | MacAddress.valueOf("00:00:00:00:00:02")); |
| 368 | |
| 369 | //create the next Objective |
| 370 | Interface egressIntf = interfaceService.getMatchingInterface(fibEntryUpdate.nextHopIp()); |
| 371 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
| 372 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:02")) |
| 373 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")); |
| 374 | TrafficSelector.Builder metabuilder = null; |
| 375 | if (!egressIntf.vlan().equals(VlanId.NONE)) { |
| 376 | treatment.pushVlan() |
| 377 | .setVlanId(egressIntf.vlan()) |
| 378 | .setVlanPcp((byte) 0); |
| 379 | } else { |
| 380 | metabuilder = DefaultTrafficSelector.builder(); |
| 381 | metabuilder.matchVlanId(VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN)); |
| 382 | } |
| 383 | treatment.setOutput(PortNumber.portNumber(1)); |
| 384 | int nextId = 11; |
| 385 | NextObjective.Builder nextBuilder = DefaultNextObjective.builder() |
| 386 | .withId(nextId) |
| 387 | .addTreatment(treatment.build()) |
| 388 | .withType(NextObjective.Type.SIMPLE) |
| 389 | .fromApp(APPID); |
| 390 | if (metabuilder != null) { |
| 391 | nextBuilder.withMeta(metabuilder.build()); |
| 392 | } |
| 393 | |
| 394 | NextObjective nextObjective = nextBuilder.add(); |
| 395 | flowObjectiveService.next(deviceId, nextObjective); |
| 396 | |
| 397 | //set up the flowObjective |
| 398 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 399 | .matchEthType(Ethernet.TYPE_IPV4) |
| 400 | .matchIPDst(prefix) |
| 401 | .build(); |
| 402 | |
| 403 | int priority = prefix.prefixLength() * 5 + 100; |
| 404 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
| 405 | .fromApp(APPID) |
| 406 | .makePermanent() |
| 407 | .withSelector(selector) |
| 408 | .withPriority(priority) |
| 409 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 410 | |
| 411 | Integer nextId1 = 11; |
| 412 | fwdBuilder.nextStep(nextId1); |
| 413 | flowObjectiveService.forward(deviceId, fwdBuilder.add()); |
| 414 | EasyMock.expectLastCall().once(); |
| 415 | setUpFlowObjectiveService(); |
| 416 | |
| 417 | // Send in the UPDATE FibUpdate |
| 418 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, |
| 419 | fibEntryUpdate); |
| 420 | fibListener.update(Collections.singletonList(fibUpdate), |
| 421 | Collections.emptyList()); |
| 422 | |
| 423 | verify(flowObjectiveService); |
| 424 | } |
| 425 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 426 | /** |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 427 | * Tests deleting a FIB entry. |
| 428 | * |
| 429 | * We verify that the flowObjectiveService records the correct state and that the |
| 430 | * correct flow is withdrawn from the flowObjectiveService. |
| 431 | */ |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 432 | @Test |
| 433 | public void testFibDelete() { |
| 434 | // Firstly add a route |
| 435 | testFibAdd(); |
| 436 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 437 | |
| 438 | // Construct the existing route entry |
| 439 | FibEntry fibEntry = new FibEntry(prefix, null, null); |
| 440 | |
| 441 | //set up the flowObjective |
| 442 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 443 | .matchEthType(Ethernet.TYPE_IPV4) |
| 444 | .matchIPDst(prefix) |
| 445 | .build(); |
| 446 | |
| 447 | int priority = prefix.prefixLength() * 5 + 100; |
| 448 | |
| 449 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
| 450 | .fromApp(APPID) |
| 451 | .makePermanent() |
| 452 | .withSelector(selector) |
| 453 | .withPriority(priority) |
| 454 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 455 | fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build()); |
| 456 | reset(flowObjectiveService); |
| 457 | flowObjectiveService.forward(deviceId, fwdBuilder.remove()); |
| 458 | replay(flowObjectiveService); |
| 459 | |
| 460 | // Send in the DELETE FibUpdate |
| 461 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.DELETE, fibEntry); |
| 462 | fibListener.update(Collections.emptyList(), Collections.singletonList(fibUpdate)); |
| 463 | |
| 464 | verify(flowObjectiveService); |
| 465 | } |
| 466 | |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 467 | private class TestInterfaceService extends InterfaceServiceAdapter { |
| 468 | |
| 469 | @Override |
| 470 | public void addListener(InterfaceListener listener) { |
| 471 | SingleSwitchFibInstallerTest.this.interfaceListener = listener; |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public Set<Interface> getInterfaces() { |
| 476 | return interfaces; |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public Set<Interface> getInterfacesByPort(ConnectPoint port) { |
| 481 | |
| 482 | Set<Interface> setIntf = new HashSet<Interface>(); |
| 483 | for (Interface intf : interfaces) { |
| 484 | if (intf.connectPoint().equals(port)) { |
| 485 | setIntf.add(intf); |
| 486 | |
| 487 | } |
| 488 | } |
| 489 | return setIntf; |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public Interface getMatchingInterface(IpAddress ip) { |
| 494 | Interface intff = null; |
| 495 | for (Interface intf : interfaces) { |
| 496 | for (InterfaceIpAddress address : intf.ipAddresses()) { |
| 497 | if (address.ipAddress().equals(ip)) { |
| 498 | intff = intf; |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return intff; |
| 505 | } |
| 506 | } |
| 507 | |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 508 | private class TestCoreService extends CoreServiceAdapter { |
| 509 | |
| 510 | @Override |
| 511 | public ApplicationId getAppId(String name) { |
| 512 | return APPID; |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public ApplicationId registerApplication(String name) { |
| 517 | return APPID; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | private class TestRoutingService extends RoutingServiceAdapter { |
| 522 | |
| 523 | @Override |
| 524 | public void addFibListener(FibListener fibListener) { |
| 525 | SingleSwitchFibInstallerTest.this.fibListener = fibListener; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | private class TestRouterConfig extends RouterConfig { |
| 530 | |
| 531 | @Override |
| 532 | public List<String> getInterfaces() { |
| 533 | ArrayList<String> interfaces = new ArrayList<>(); |
| 534 | interfaces.add("of:0000000000000001/1"); |
| 535 | interfaces.add("of:0000000000000002/1"); |
| 536 | interfaces.add("of:0000000000000003/1"); |
| 537 | interfaces.add("of:0000000000000004/1"); |
| 538 | return interfaces; |
| 539 | } |
| 540 | |
| 541 | @Override |
| 542 | public ConnectPoint getControlPlaneConnectPoint() { |
| 543 | return SW1_ETH1; |
| 544 | } |
| 545 | |
| 546 | @Override |
| 547 | public boolean getOspfEnabled() { |
| 548 | return true; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | private class TestDeviceService extends DeviceServiceAdapter { |
| 553 | |
| 554 | @Override |
| 555 | public boolean isAvailable(DeviceId deviceId) { |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public void addListener(DeviceListener listener) { |
| 561 | SingleSwitchFibInstallerTest.this.deviceListener = listener; |
| 562 | } |
| 563 | } |
| 564 | } |