Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.sdnip; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.packet.Ip4Prefix; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onosproject.TestApplicationId; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.incubator.net.intf.Interface; |
| 32 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.PortNumber; |
| 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
| 39 | import org.onosproject.net.flow.TrafficTreatment; |
| 40 | import org.onosproject.net.host.InterfaceIpAddress; |
| 41 | import org.onosproject.net.intent.AbstractIntentTest; |
| 42 | import org.onosproject.net.intent.Key; |
| 43 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 44 | import org.onosproject.routing.FibEntry; |
| 45 | import org.onosproject.routing.FibUpdate; |
| 46 | import org.onosproject.routing.IntentSynchronizationService; |
| 47 | import org.onosproject.routing.config.BgpPeer; |
| 48 | import org.onosproject.routing.config.RoutingConfigurationService; |
| 49 | |
| 50 | import java.util.Collections; |
| 51 | import java.util.HashMap; |
| 52 | import java.util.HashSet; |
| 53 | import java.util.Map; |
| 54 | import java.util.Set; |
| 55 | |
| 56 | import static org.easymock.EasyMock.createMock; |
| 57 | import static org.easymock.EasyMock.expect; |
| 58 | import static org.easymock.EasyMock.replay; |
| 59 | import static org.easymock.EasyMock.reset; |
| 60 | import static org.easymock.EasyMock.verify; |
| 61 | import static org.onosproject.sdnip.TestIntentServiceHelper.eqExceptId; |
| 62 | |
| 63 | /** |
| 64 | * Unit tests for SdnIpFib. |
| 65 | */ |
| 66 | public class SdnIpFibTest extends AbstractIntentTest { |
| 67 | |
| 68 | private RoutingConfigurationService routingConfig; |
| 69 | private InterfaceService interfaceService; |
| 70 | |
| 71 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 72 | DeviceId.deviceId("of:0000000000000001"), |
| 73 | PortNumber.portNumber(1)); |
| 74 | |
| 75 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 76 | DeviceId.deviceId("of:0000000000000002"), |
| 77 | PortNumber.portNumber(1)); |
| 78 | |
| 79 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 80 | DeviceId.deviceId("of:0000000000000003"), |
| 81 | PortNumber.portNumber(1)); |
| 82 | |
| 83 | private static final ConnectPoint SW4_ETH1 = new ConnectPoint( |
| 84 | DeviceId.deviceId("of:0000000000000004"), |
| 85 | PortNumber.portNumber(1)); |
| 86 | |
| 87 | private SdnIpFib sdnipFib; |
| 88 | private IntentSynchronizationService intentSynchronizer; |
| 89 | private final Set<Interface> interfaces = Sets.newHashSet(); |
| 90 | |
| 91 | private static final ApplicationId APPID = TestApplicationId.create("SDNIP"); |
| 92 | |
| 93 | @Before |
| 94 | public void setUp() throws Exception { |
| 95 | super.setUp(); |
| 96 | |
| 97 | routingConfig = createMock(RoutingConfigurationService.class); |
| 98 | interfaceService = createMock(InterfaceService.class); |
| 99 | |
| 100 | // These will set expectations on routingConfig and interfaceService |
| 101 | setUpInterfaceService(); |
| 102 | setUpBgpPeers(); |
| 103 | |
| 104 | replay(routingConfig); |
| 105 | replay(interfaceService); |
| 106 | |
| 107 | intentSynchronizer = createMock(IntentSynchronizationService.class); |
| 108 | |
| 109 | sdnipFib = new SdnIpFib(APPID, interfaceService, intentSynchronizer); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Sets up BGP peers in external networks. |
| 114 | */ |
| 115 | private void setUpBgpPeers() { |
| 116 | |
| 117 | Map<IpAddress, BgpPeer> peers = new HashMap<>(); |
| 118 | |
| 119 | String peerSw1Eth1 = "192.168.10.1"; |
| 120 | peers.put(IpAddress.valueOf(peerSw1Eth1), |
| 121 | new BgpPeer("00:00:00:00:00:00:00:01", 1, peerSw1Eth1)); |
| 122 | |
| 123 | // Two BGP peers are connected to switch 2 port 1. |
| 124 | String peer1Sw2Eth1 = "192.168.20.1"; |
| 125 | peers.put(IpAddress.valueOf(peer1Sw2Eth1), |
| 126 | new BgpPeer("00:00:00:00:00:00:00:02", 1, peer1Sw2Eth1)); |
| 127 | |
| 128 | String peer2Sw2Eth1 = "192.168.20.2"; |
| 129 | peers.put(IpAddress.valueOf(peer2Sw2Eth1), |
| 130 | new BgpPeer("00:00:00:00:00:00:00:02", 1, peer2Sw2Eth1)); |
| 131 | |
| 132 | String peer1Sw4Eth1 = "192.168.40.1"; |
| 133 | peers.put(IpAddress.valueOf(peer1Sw4Eth1), |
| 134 | new BgpPeer("00:00:00:00:00:00:00:04", 1, peer1Sw4Eth1)); |
| 135 | |
| 136 | expect(routingConfig.getBgpPeers()).andReturn(peers).anyTimes(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets up InterfaceService. |
| 141 | */ |
| 142 | private void setUpInterfaceService() { |
| 143 | Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet(); |
| 144 | interfaceIpAddresses1.add(new InterfaceIpAddress( |
| 145 | IpAddress.valueOf("192.168.10.101"), |
| 146 | IpPrefix.valueOf("192.168.10.0/24"))); |
| 147 | Interface sw1Eth1 = new Interface(SW1_ETH1, |
| 148 | interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"), |
| 149 | VlanId.NONE); |
| 150 | interfaces.add(sw1Eth1); |
| 151 | |
| 152 | Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet(); |
| 153 | interfaceIpAddresses2.add( |
| 154 | new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"), |
| 155 | IpPrefix.valueOf("192.168.20.0/24"))); |
| 156 | Interface sw2Eth1 = new Interface(SW2_ETH1, |
| 157 | interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"), |
| 158 | VlanId.NONE); |
| 159 | interfaces.add(sw2Eth1); |
| 160 | |
| 161 | Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet(); |
| 162 | interfaceIpAddresses3.add( |
| 163 | new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"), |
| 164 | IpPrefix.valueOf("192.168.30.0/24"))); |
| 165 | Interface sw3Eth1 = new Interface(SW3_ETH1, |
| 166 | interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"), |
| 167 | VlanId.NONE); |
| 168 | interfaces.add(sw3Eth1); |
| 169 | |
| 170 | InterfaceIpAddress interfaceIpAddress4 = |
| 171 | new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), |
| 172 | IpPrefix.valueOf("192.168.40.0/24")); |
| 173 | Interface sw4Eth1 = new Interface(SW4_ETH1, |
| 174 | Sets.newHashSet(interfaceIpAddress4), |
| 175 | MacAddress.valueOf("00:00:00:00:00:04"), |
| 176 | VlanId.vlanId((short) 1)); |
| 177 | |
| 178 | expect(interfaceService.getInterfacesByPort(SW4_ETH1)).andReturn( |
| 179 | Collections.singleton(sw4Eth1)).anyTimes(); |
| 180 | expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.40.1"))) |
| 181 | .andReturn(sw4Eth1).anyTimes(); |
| 182 | |
| 183 | interfaces.add(sw4Eth1); |
| 184 | |
| 185 | expect(interfaceService.getInterfacesByPort(SW1_ETH1)).andReturn( |
| 186 | Collections.singleton(sw1Eth1)).anyTimes(); |
| 187 | expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.10.1"))) |
| 188 | .andReturn(sw1Eth1).anyTimes(); |
| 189 | expect(interfaceService.getInterfacesByPort(SW2_ETH1)).andReturn( |
| 190 | Collections.singleton(sw2Eth1)).anyTimes(); |
| 191 | expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.20.1"))) |
| 192 | .andReturn(sw2Eth1).anyTimes(); |
| 193 | expect(interfaceService.getInterfacesByPort(SW3_ETH1)).andReturn( |
| 194 | Collections.singleton(sw3Eth1)).anyTimes(); |
| 195 | expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.30.1"))) |
| 196 | .andReturn(sw3Eth1).anyTimes(); |
| 197 | expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes(); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Tests adding a FIB entry to the IntentSynchronizer. |
| 202 | * |
| 203 | * We verify that the synchronizer records the correct state and that the |
| 204 | * correct intent is submitted to the IntentService. |
| 205 | */ |
| 206 | @Test |
| 207 | public void testFibAdd() { |
| 208 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 209 | FibEntry fibEntry = new FibEntry(prefix, |
| 210 | Ip4Address.valueOf("192.168.10.1"), |
| 211 | MacAddress.valueOf("00:00:00:00:00:01")); |
| 212 | |
| 213 | // Construct a MultiPointToSinglePointIntent intent |
| 214 | TrafficSelector.Builder selectorBuilder = |
| 215 | DefaultTrafficSelector.builder(); |
| 216 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 217 | fibEntry.prefix()); |
| 218 | |
| 219 | TrafficTreatment.Builder treatmentBuilder = |
| 220 | DefaultTrafficTreatment.builder(); |
| 221 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 222 | |
| 223 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 224 | ingressPoints.add(SW2_ETH1); |
| 225 | ingressPoints.add(SW3_ETH1); |
| 226 | ingressPoints.add(SW4_ETH1); |
| 227 | |
| 228 | MultiPointToSinglePointIntent intent = |
| 229 | MultiPointToSinglePointIntent.builder() |
| 230 | .appId(APPID) |
| 231 | .key(Key.of(prefix.toString(), APPID)) |
| 232 | .selector(selectorBuilder.build()) |
| 233 | .treatment(treatmentBuilder.build()) |
| 234 | .ingressPoints(ingressPoints) |
| 235 | .egressPoint(SW1_ETH1) |
| 236 | .constraints(SdnIpFib.CONSTRAINTS) |
| 237 | .build(); |
| 238 | |
| 239 | // Setup the expected intents |
| 240 | intentSynchronizer.submit(eqExceptId(intent)); |
| 241 | replay(intentSynchronizer); |
| 242 | |
| 243 | // Send in the UPDATE FibUpdate |
| 244 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry); |
| 245 | sdnipFib.update(Collections.singleton(fibUpdate), Collections.emptyList()); |
| 246 | |
| 247 | verify(intentSynchronizer); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Tests adding a FIB entry with to a next hop in a VLAN. |
| 252 | * |
| 253 | * We verify that the synchronizer records the correct state and that the |
| 254 | * correct intent is submitted to the IntentService. |
| 255 | */ |
| 256 | @Test |
| 257 | public void testFibAddWithVlan() { |
| 258 | IpPrefix prefix = Ip4Prefix.valueOf("3.3.3.0/24"); |
| 259 | FibEntry fibEntry = new FibEntry(prefix, |
| 260 | Ip4Address.valueOf("192.168.40.1"), |
| 261 | MacAddress.valueOf("00:00:00:00:00:04")); |
| 262 | |
| 263 | // Construct a MultiPointToSinglePointIntent intent |
| 264 | TrafficSelector.Builder selectorBuilder = |
| 265 | DefaultTrafficSelector.builder(); |
| 266 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 267 | .matchIPDst(fibEntry.prefix()) |
| 268 | .matchVlanId(VlanId.ANY); |
| 269 | |
| 270 | TrafficTreatment.Builder treatmentBuilder = |
| 271 | DefaultTrafficTreatment.builder(); |
| 272 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:04")) |
| 273 | .setVlanId(VlanId.vlanId((short) 1)); |
| 274 | |
| 275 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 276 | ingressPoints.add(SW1_ETH1); |
| 277 | ingressPoints.add(SW2_ETH1); |
| 278 | ingressPoints.add(SW3_ETH1); |
| 279 | |
| 280 | MultiPointToSinglePointIntent intent = |
| 281 | MultiPointToSinglePointIntent.builder() |
| 282 | .appId(APPID) |
| 283 | .key(Key.of(prefix.toString(), APPID)) |
| 284 | .selector(selectorBuilder.build()) |
| 285 | .treatment(treatmentBuilder.build()) |
| 286 | .ingressPoints(ingressPoints) |
| 287 | .egressPoint(SW4_ETH1) |
| 288 | .constraints(SdnIpFib.CONSTRAINTS) |
| 289 | .build(); |
| 290 | |
| 291 | // Setup the expected intents |
| 292 | intentSynchronizer.submit(eqExceptId(intent)); |
| 293 | |
| 294 | replay(intentSynchronizer); |
| 295 | |
| 296 | // Send in the UPDATE FibUpdate |
| 297 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry); |
| 298 | sdnipFib.update(Collections.singleton(fibUpdate), Collections.emptyList()); |
| 299 | |
| 300 | verify(intentSynchronizer); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Tests updating a FIB entry. |
| 305 | * |
| 306 | * We verify that the synchronizer records the correct state and that the |
| 307 | * correct intent is submitted to the IntentService. |
| 308 | */ |
| 309 | @Test |
| 310 | public void testFibUpdate() { |
| 311 | // Firstly add a route |
| 312 | testFibAdd(); |
| 313 | |
| 314 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 315 | |
| 316 | // Start to construct a new route entry and new intent |
| 317 | FibEntry fibEntryUpdate = new FibEntry(prefix, |
| 318 | Ip4Address.valueOf("192.168.20.1"), |
| 319 | MacAddress.valueOf("00:00:00:00:00:02")); |
| 320 | |
| 321 | // Construct a new MultiPointToSinglePointIntent intent |
| 322 | TrafficSelector.Builder selectorBuilderNew = |
| 323 | DefaultTrafficSelector.builder(); |
| 324 | selectorBuilderNew.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 325 | fibEntryUpdate.prefix()); |
| 326 | |
| 327 | TrafficTreatment.Builder treatmentBuilderNew = |
| 328 | DefaultTrafficTreatment.builder(); |
| 329 | treatmentBuilderNew.setEthDst(MacAddress.valueOf("00:00:00:00:00:02")); |
| 330 | |
| 331 | Set<ConnectPoint> ingressPointsNew = new HashSet<>(); |
| 332 | ingressPointsNew.add(SW1_ETH1); |
| 333 | ingressPointsNew.add(SW3_ETH1); |
| 334 | ingressPointsNew.add(SW4_ETH1); |
| 335 | |
| 336 | MultiPointToSinglePointIntent intentNew = |
| 337 | MultiPointToSinglePointIntent.builder() |
| 338 | .appId(APPID) |
| 339 | .key(Key.of(prefix.toString(), APPID)) |
| 340 | .selector(selectorBuilderNew.build()) |
| 341 | .treatment(treatmentBuilderNew.build()) |
| 342 | .ingressPoints(ingressPointsNew) |
| 343 | .egressPoint(SW2_ETH1) |
| 344 | .constraints(SdnIpFib.CONSTRAINTS) |
| 345 | .build(); |
| 346 | |
| 347 | // Set up test expectation |
| 348 | reset(intentSynchronizer); |
| 349 | |
| 350 | // Setup the expected intents |
| 351 | intentSynchronizer.submit(eqExceptId(intentNew)); |
| 352 | replay(intentSynchronizer); |
| 353 | |
| 354 | // Send in the UPDATE FibUpdate |
| 355 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, |
| 356 | fibEntryUpdate); |
| 357 | sdnipFib.update(Collections.singletonList(fibUpdate), |
| 358 | Collections.emptyList()); |
| 359 | |
| 360 | verify(intentSynchronizer); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Tests deleting a FIB entry. |
| 365 | * |
| 366 | * We verify that the synchronizer records the correct state and that the |
| 367 | * correct intent is withdrawn from the IntentService. |
| 368 | */ |
| 369 | @Test |
| 370 | public void testFibDelete() { |
| 371 | // Firstly add a route |
| 372 | testFibAdd(); |
| 373 | |
| 374 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 375 | |
| 376 | // Construct the existing route entry |
| 377 | FibEntry fibEntry = new FibEntry(prefix, null, null); |
| 378 | |
| 379 | // Construct the existing MultiPointToSinglePoint intent |
| 380 | TrafficSelector.Builder selectorBuilder = |
| 381 | DefaultTrafficSelector.builder(); |
| 382 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 383 | fibEntry.prefix()); |
| 384 | |
| 385 | TrafficTreatment.Builder treatmentBuilder = |
| 386 | DefaultTrafficTreatment.builder(); |
| 387 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 388 | |
| 389 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 390 | ingressPoints.add(SW2_ETH1); |
| 391 | ingressPoints.add(SW3_ETH1); |
| 392 | ingressPoints.add(SW4_ETH1); |
| 393 | |
| 394 | MultiPointToSinglePointIntent addedIntent = |
| 395 | MultiPointToSinglePointIntent.builder() |
| 396 | .appId(APPID) |
| 397 | .key(Key.of(prefix.toString(), APPID)) |
| 398 | .selector(selectorBuilder.build()) |
| 399 | .treatment(treatmentBuilder.build()) |
| 400 | .ingressPoints(ingressPoints) |
| 401 | .egressPoint(SW1_ETH1) |
| 402 | .constraints(SdnIpFib.CONSTRAINTS) |
| 403 | .build(); |
| 404 | |
| 405 | // Set up expectation |
| 406 | reset(intentSynchronizer); |
| 407 | // Setup the expected intents |
| 408 | intentSynchronizer.withdraw(eqExceptId(addedIntent)); |
| 409 | replay(intentSynchronizer); |
| 410 | |
| 411 | // Send in the DELETE FibUpdate |
| 412 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.DELETE, fibEntry); |
| 413 | sdnipFib.update(Collections.emptyList(), Collections.singletonList(fibUpdate)); |
| 414 | |
| 415 | verify(intentSynchronizer); |
| 416 | } |
| 417 | } |