Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [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 | |
| 17 | package org.onosproject.sdnip; |
| 18 | |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.Ethernet; |
| 26 | import org.onlab.packet.Ip4Address; |
| 27 | import org.onlab.packet.Ip4Prefix; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.IpPrefix; |
| 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.packet.VlanId; |
| 32 | import org.onosproject.TestApplicationId; |
| 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreServiceAdapter; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 35 | import org.onosproject.net.intf.Interface; |
| 36 | import org.onosproject.net.intf.InterfaceEvent; |
| 37 | import org.onosproject.net.intf.InterfaceListener; |
| 38 | import org.onosproject.net.intf.InterfaceService; |
| 39 | import org.onosproject.net.intf.InterfaceServiceAdapter; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 40 | import org.onosproject.routeservice.ResolvedRoute; |
| 41 | import org.onosproject.routeservice.Route; |
| 42 | import org.onosproject.routeservice.RouteEvent; |
| 43 | import org.onosproject.routeservice.RouteListener; |
| 44 | import org.onosproject.routeservice.RouteServiceAdapter; |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 45 | import org.onosproject.intentsync.IntentSynchronizationService; |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 46 | import org.onosproject.net.ConnectPoint; |
| 47 | import org.onosproject.net.DeviceId; |
| 48 | import org.onosproject.net.FilteredConnectPoint; |
| 49 | import org.onosproject.net.PortNumber; |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 50 | import org.onosproject.net.config.Config; |
| 51 | import org.onosproject.net.config.ConfigApplyDelegate; |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 52 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 54 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 55 | import org.onosproject.net.flow.TrafficSelector; |
| 56 | import org.onosproject.net.flow.TrafficTreatment; |
| 57 | import org.onosproject.net.host.InterfaceIpAddress; |
| 58 | import org.onosproject.net.intent.AbstractIntentTest; |
| 59 | import org.onosproject.net.intent.Key; |
| 60 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 61 | import org.onosproject.sdnip.config.SdnIpConfig; |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 62 | |
| 63 | import java.util.Collections; |
| 64 | import java.util.List; |
| 65 | import java.util.Set; |
| 66 | |
Thomas Vachuska | d832fc5 | 2017-01-11 13:13:06 -0800 | [diff] [blame] | 67 | import static org.easymock.EasyMock.*; |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 68 | import static org.onosproject.routing.TestIntentServiceHelper.eqExceptId; |
| 69 | |
| 70 | /** |
| 71 | * Unit tests for SdnIpFib. |
| 72 | */ |
| 73 | public class SdnIpFibTest extends AbstractIntentTest { |
| 74 | |
| 75 | private InterfaceService interfaceService; |
| 76 | |
| 77 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 78 | DeviceId.deviceId("of:0000000000000001"), |
| 79 | PortNumber.portNumber(1)); |
| 80 | |
| 81 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 82 | DeviceId.deviceId("of:0000000000000002"), |
| 83 | PortNumber.portNumber(1)); |
| 84 | |
| 85 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 86 | DeviceId.deviceId("of:0000000000000003"), |
| 87 | PortNumber.portNumber(1)); |
| 88 | |
| 89 | private static final ConnectPoint SW4_ETH1 = new ConnectPoint( |
| 90 | DeviceId.deviceId("of:0000000000000004"), |
| 91 | PortNumber.portNumber(1)); |
| 92 | |
| 93 | private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 94 | private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 95 | private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03"); |
| 96 | private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04"); |
| 97 | |
| 98 | private static final VlanId NO_VLAN = VlanId.NONE; |
| 99 | private static final VlanId VLAN10 = VlanId.vlanId(Short.valueOf("10")); |
| 100 | private static final VlanId VLAN20 = VlanId.vlanId(Short.valueOf("20")); |
| 101 | |
| 102 | private static final InterfaceIpAddress IIP1 = |
| 103 | InterfaceIpAddress.valueOf("192.168.10.101/24"); |
| 104 | private static final InterfaceIpAddress IIP2 = |
| 105 | InterfaceIpAddress.valueOf("192.168.20.101/24"); |
| 106 | private static final InterfaceIpAddress IIP3 = |
| 107 | InterfaceIpAddress.valueOf("192.168.30.101/24"); |
| 108 | private static final InterfaceIpAddress IIP4 = |
| 109 | InterfaceIpAddress.valueOf("192.168.40.101/24"); |
| 110 | |
| 111 | private static final IpAddress IP1 = Ip4Address.valueOf("192.168.10.1"); |
| 112 | private static final IpAddress IP2 = Ip4Address.valueOf("192.168.20.1"); |
| 113 | private static final IpAddress IP3 = Ip4Address.valueOf("192.168.30.1"); |
| 114 | |
| 115 | private static final IpPrefix PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 116 | private static final IpPrefix PREFIX2 = Ip4Prefix.valueOf("1.1.2.0/24"); |
| 117 | |
| 118 | private SdnIpFib sdnipFib; |
| 119 | private IntentSynchronizationService intentSynchronizer; |
| 120 | private final Set<Interface> interfaces = Sets.newHashSet(); |
| 121 | |
| 122 | private static final ApplicationId APPID = TestApplicationId.create("SDNIP"); |
| 123 | |
| 124 | private RouteListener routeListener; |
| 125 | private InterfaceListener interfaceListener; |
| 126 | |
| 127 | @Before |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 128 | public void setUp() { |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 129 | super.setUp(); |
| 130 | |
| 131 | interfaceService = createMock(InterfaceService.class); |
| 132 | |
| 133 | interfaceService.addListener(anyObject(InterfaceListener.class)); |
| 134 | expectLastCall().andDelegateTo(new InterfaceServiceDelegate()); |
| 135 | |
| 136 | // These will set expectations on routingConfig and interfaceService |
| 137 | setUpInterfaceService(); |
| 138 | |
| 139 | replay(interfaceService); |
| 140 | |
| 141 | intentSynchronizer = createMock(IntentSynchronizationService.class); |
| 142 | |
| 143 | sdnipFib = new SdnIpFib(); |
| 144 | sdnipFib.routeService = new TestRouteService(); |
| 145 | sdnipFib.coreService = new TestCoreService(); |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 146 | sdnipFib.networkConfigService = new TestNetworkConfigService(); |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 147 | sdnipFib.interfaceService = interfaceService; |
| 148 | sdnipFib.intentSynchronizer = intentSynchronizer; |
| 149 | |
| 150 | sdnipFib.activate(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sets up the interface service. |
| 155 | */ |
| 156 | private void setUpInterfaceService() { |
| 157 | List<InterfaceIpAddress> iIps1 = Lists.newArrayList(); |
| 158 | iIps1.add(IIP1); |
| 159 | Interface sw1Eth1 = new Interface("sw1-eth1", SW1_ETH1, iIps1, MAC1, VLAN10); |
| 160 | interfaces.add(sw1Eth1); |
| 161 | |
| 162 | List<InterfaceIpAddress> iIps2 = Lists.newArrayList(); |
| 163 | iIps2.add(IIP2); |
| 164 | Interface sw2Eth1 = new Interface("sw2-eth1", SW2_ETH1, iIps2, MAC2, VLAN20); |
| 165 | interfaces.add(sw2Eth1); |
| 166 | |
| 167 | List<InterfaceIpAddress> iIps3 = Lists.newArrayList(); |
| 168 | iIps3.add(IIP3); |
| 169 | Interface sw3Eth1 = new Interface("sw3-eth1", SW3_ETH1, iIps3, MAC3, NO_VLAN); |
| 170 | interfaces.add(sw3Eth1); |
| 171 | |
| 172 | expect(interfaceService.getInterfacesByPort(SW1_ETH1)).andReturn( |
| 173 | Collections.singleton(sw1Eth1)).anyTimes(); |
| 174 | expect(interfaceService.getMatchingInterface(IP1)) |
| 175 | .andReturn(sw1Eth1).anyTimes(); |
| 176 | expect(interfaceService.getInterfacesByPort(SW2_ETH1)).andReturn( |
| 177 | Collections.singleton(sw2Eth1)).anyTimes(); |
| 178 | expect(interfaceService.getMatchingInterface(IP2)) |
| 179 | .andReturn(sw2Eth1).anyTimes(); |
| 180 | expect(interfaceService.getInterfacesByPort(SW3_ETH1)).andReturn( |
| 181 | Collections.singleton(sw3Eth1)).anyTimes(); |
| 182 | expect(interfaceService.getMatchingInterface(IP3)) |
| 183 | .andReturn(sw3Eth1).anyTimes(); |
| 184 | expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Tests adding a route. The egress interface has no VLAN configured. |
| 189 | * |
| 190 | * We verify that the synchronizer records the correct state and that the |
| 191 | * correct intent is submitted to the IntentService. |
| 192 | */ |
| 193 | @Test |
| 194 | public void testRouteAddToNoVlan() { |
| 195 | // Build the expected route |
Ray Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 196 | ResolvedRoute route = createRoute(PREFIX1, IP3, MAC3); |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 197 | |
| 198 | MultiPointToSinglePointIntent intent = |
| 199 | createIntentToThreeSrcOneTwo(PREFIX1); |
| 200 | |
| 201 | // Setup the expected intents |
| 202 | intentSynchronizer.submit(eqExceptId(intent)); |
| 203 | replay(intentSynchronizer); |
| 204 | |
| 205 | // Send in the added event |
| 206 | routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route)); |
| 207 | |
| 208 | verify(intentSynchronizer); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Tests adding a route. The egress interface has a VLAN configured. |
| 213 | * |
| 214 | * We verify that the synchronizer records the correct state and that the |
| 215 | * correct intent is submitted to the IntentService. |
| 216 | */ |
| 217 | @Test |
| 218 | public void testRouteAddToVlan() { |
| 219 | // Build the expected route |
Ray Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 220 | ResolvedRoute route = createRoute(PREFIX2, IP1, MAC1); |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 221 | |
| 222 | MultiPointToSinglePointIntent intent = createIntentToOne(PREFIX2); |
| 223 | |
| 224 | // Setup the expected intents |
| 225 | intentSynchronizer.submit(eqExceptId(intent)); |
| 226 | replay(intentSynchronizer); |
| 227 | |
| 228 | // Send in the added event |
| 229 | routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route)); |
| 230 | |
| 231 | verify(intentSynchronizer); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Tests updating a route. |
| 236 | * |
| 237 | * We first add a route from a next-hop with no vlan. We then announce the |
| 238 | * same route from another next-hop with a vlan. |
| 239 | * |
| 240 | * We verify that the synchronizer records the correct state and that the |
| 241 | * correct intent is submitted to the IntentService. |
| 242 | */ |
| 243 | @Test |
| 244 | public void testRouteUpdatesToVlan() { |
| 245 | // Add a route first to a destination with no VLAN |
| 246 | testRouteAddToNoVlan(); |
| 247 | |
| 248 | // Build the new route entries for prefix1 and prefix2 |
Ray Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 249 | ResolvedRoute oldRoutePrefixOne = createRoute(PREFIX1, IP3, MAC3); |
| 250 | ResolvedRoute routePrefixOne = createRoute(PREFIX1, IP1, MAC1); |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 251 | |
| 252 | // Create the new expected intents |
| 253 | MultiPointToSinglePointIntent newPrefixOneIntent = createIntentToOne(PREFIX1); |
| 254 | |
| 255 | // Set up test expectation |
| 256 | reset(intentSynchronizer); |
| 257 | |
| 258 | // Setup the expected intents |
| 259 | intentSynchronizer.submit(eqExceptId(newPrefixOneIntent)); |
| 260 | replay(intentSynchronizer); |
| 261 | |
| 262 | // Send in the update events |
| 263 | routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 264 | routePrefixOne, oldRoutePrefixOne)); |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 265 | |
| 266 | verify(intentSynchronizer); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Tests updating a route. |
| 271 | * |
| 272 | * We first add a route from a next-hop with a vlan. We then announce the |
| 273 | * same route from another next-hop with no vlan. |
| 274 | * |
| 275 | * We verify that the synchronizer records the correct state and that the |
| 276 | * correct intent is submitted to the IntentService. |
| 277 | */ |
| 278 | @Test |
| 279 | public void testRouteUpdatesToNoVlan() { |
| 280 | // Add a route first to a destination with no VLAN |
| 281 | testRouteAddToVlan(); |
| 282 | |
| 283 | // Build the new route entries for prefix1 and prefix2 |
Ray Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 284 | ResolvedRoute oldRoutePrefix = createRoute(PREFIX2, IP1, MAC1); |
| 285 | ResolvedRoute routePrefix = createRoute(PREFIX2, IP3, MAC3); |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 286 | |
| 287 | // Create the new expected intents |
| 288 | MultiPointToSinglePointIntent newPrefixIntent = |
| 289 | createIntentToThreeSrcOneTwo(PREFIX2); |
| 290 | |
| 291 | // Set up test expectation |
| 292 | reset(intentSynchronizer); |
| 293 | |
| 294 | // Setup the expected intents |
| 295 | intentSynchronizer.submit(eqExceptId(newPrefixIntent)); |
| 296 | replay(intentSynchronizer); |
| 297 | |
| 298 | // Send in the update events |
| 299 | routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 300 | routePrefix, oldRoutePrefix)); |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 301 | |
| 302 | verify(intentSynchronizer); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Tests deleting a route. |
| 307 | * |
| 308 | * We verify that the synchronizer records the correct state and that the |
| 309 | * correct intent is withdrawn from the IntentService. |
| 310 | */ |
| 311 | @Test |
| 312 | public void testRouteDelete() { |
| 313 | // Add a route first |
| 314 | testRouteAddToNoVlan(); |
| 315 | |
| 316 | // Construct the existing route entry |
Ray Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 317 | ResolvedRoute route = createRoute(PREFIX1, IP3, MAC3); |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 318 | |
| 319 | // Create existing intent |
| 320 | MultiPointToSinglePointIntent removedIntent = |
| 321 | createIntentToThreeSrcOneTwo(PREFIX1); |
| 322 | |
| 323 | // Set up expectation |
| 324 | reset(intentSynchronizer); |
| 325 | // Setup the expected intents |
| 326 | intentSynchronizer.withdraw(eqExceptId(removedIntent)); |
| 327 | replay(intentSynchronizer); |
| 328 | |
| 329 | // Send in the removed event |
| 330 | routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, route)); |
| 331 | |
| 332 | verify(intentSynchronizer); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Tests adding a new interface. |
| 337 | * |
| 338 | * We verify that the synchronizer records the correct state and that the |
| 339 | * correct intent is withdrawn from the IntentService. |
| 340 | */ |
| 341 | @Test |
| 342 | public void testAddInterface() { |
| 343 | // Add a route first |
| 344 | testRouteAddToNoVlan(); |
| 345 | |
| 346 | // Create the new expected intent |
| 347 | MultiPointToSinglePointIntent addedIntent = |
| 348 | createIntentToThreeSrcOneTwoFour(PREFIX1); |
| 349 | |
| 350 | reset(intentSynchronizer); |
| 351 | |
| 352 | intentSynchronizer.submit(eqExceptId(addedIntent)); |
| 353 | expectLastCall().once(); |
| 354 | |
| 355 | replay(intentSynchronizer); |
| 356 | |
| 357 | // Create the new interface and add notify it |
| 358 | Interface intf = new Interface("sw4-eth1", SW4_ETH1, |
| 359 | Collections.singletonList(IIP4), |
| 360 | MAC4, NO_VLAN); |
| 361 | InterfaceEvent intfEvent = |
| 362 | new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, intf); |
| 363 | |
| 364 | interfaceListener.event(intfEvent); |
| 365 | |
| 366 | verify(intentSynchronizer); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Tests removing an existing interface. |
| 371 | * |
| 372 | * We first push an intent with destination sw3 and source sw1 and sw2. We |
| 373 | * then remove the ingress interface on sw1. |
| 374 | * |
| 375 | * We verify that the synchronizer records the correct state and that the |
| 376 | * correct intent is withdrawn from the IntentService. |
| 377 | */ |
| 378 | @Test |
| 379 | public void testRemoveIngressInterface() { |
| 380 | // Add a route first |
| 381 | testRouteAddToNoVlan(); |
| 382 | |
| 383 | // Create the new expected intent |
| 384 | MultiPointToSinglePointIntent remainingIntent = |
| 385 | createIntentToThreeSrcTwo(PREFIX1); |
| 386 | |
| 387 | reset(intentSynchronizer); |
| 388 | |
| 389 | intentSynchronizer.submit(eqExceptId(remainingIntent)); |
| 390 | expectLastCall().once(); |
| 391 | |
| 392 | replay(intentSynchronizer); |
| 393 | |
| 394 | // Define the existing ingress interface and remove it |
| 395 | Interface intf = new Interface("sw1-eth1", SW1_ETH1, |
| 396 | Collections.singletonList(IIP1), |
| 397 | MAC1, VLAN10); |
| 398 | InterfaceEvent intfEvent = |
| 399 | new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, intf); |
| 400 | interfaceListener.event(intfEvent); |
| 401 | |
| 402 | verify(intentSynchronizer); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Tests removing an existing egress interface. |
| 407 | * |
| 408 | * We first push an intent with destination sw3 and source sw1 and sw2. We |
| 409 | * then remove the egress interface on sw3. |
| 410 | * |
| 411 | * We verify that the synchronizer records the correct state and that the |
| 412 | * correct intent is withdrawn from the IntentService. |
| 413 | */ |
| 414 | @Test |
| 415 | public void testRemoveEgressInterface() { |
| 416 | // Add a route first |
| 417 | testRouteAddToNoVlan(); |
| 418 | |
| 419 | // Create existing intent |
| 420 | MultiPointToSinglePointIntent removedIntent = |
| 421 | createIntentToThreeSrcOneTwo(PREFIX1); |
| 422 | |
| 423 | // Set up expectation |
| 424 | reset(intentSynchronizer); |
| 425 | // Setup the expected intents |
| 426 | intentSynchronizer.withdraw(eqExceptId(removedIntent)); |
| 427 | replay(intentSynchronizer); |
| 428 | |
| 429 | // Define the existing egress interface and remove it |
| 430 | Interface intf = new Interface("sw3-eth1", SW3_ETH1, |
| 431 | Collections.singletonList(IIP3), |
| 432 | MAC3, VlanId.NONE); |
| 433 | InterfaceEvent intfEvent = |
| 434 | new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, intf); |
| 435 | interfaceListener.event(intfEvent); |
| 436 | |
| 437 | verify(intentSynchronizer); |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Builds a MultiPointToSinglePointIntent with dest sw1 (VLAN Id) and src |
| 442 | * sw2, sw3. |
| 443 | */ |
| 444 | private MultiPointToSinglePointIntent createIntentToOne(IpPrefix ipPrefix) { |
| 445 | // Build the expected treatment |
| 446 | TrafficTreatment.Builder treatmentBuilder = |
| 447 | DefaultTrafficTreatment.builder(); |
| 448 | treatmentBuilder.setEthDst(MAC1); |
| 449 | |
| 450 | // Build the expected egress FilteredConnectPoint |
| 451 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 452 | selector.matchVlanId(VLAN10); |
| 453 | FilteredConnectPoint egressFilteredCP = |
| 454 | new FilteredConnectPoint(SW1_ETH1, selector.build()); |
| 455 | |
| 456 | // Build the expected selectors |
| 457 | Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet(); |
| 458 | |
| 459 | // Build the expected ingress FilteredConnectPoint for sw2 |
| 460 | selector = DefaultTrafficSelector.builder(); |
| 461 | selector.matchVlanId(VLAN20); |
| 462 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 463 | selector.matchIPDst(ipPrefix); |
| 464 | FilteredConnectPoint ingressFilteredCP = |
| 465 | new FilteredConnectPoint(SW2_ETH1, selector.build()); |
| 466 | ingressFilteredCPs.add(ingressFilteredCP); |
| 467 | |
| 468 | // Build the expected ingress FilteredConnectPoint for sw3 |
| 469 | selector = DefaultTrafficSelector.builder(); |
| 470 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 471 | selector.matchIPDst(ipPrefix); |
| 472 | ingressFilteredCP = new FilteredConnectPoint(SW3_ETH1, selector.build()); |
| 473 | ingressFilteredCPs.add(ingressFilteredCP); |
| 474 | |
| 475 | // Build the expected intent |
| 476 | MultiPointToSinglePointIntent intent = |
| 477 | MultiPointToSinglePointIntent.builder() |
| 478 | .appId(APPID) |
| 479 | .key(Key.of(ipPrefix.toString(), APPID)) |
| 480 | .filteredIngressPoints(ingressFilteredCPs) |
| 481 | .filteredEgressPoint(egressFilteredCP) |
| 482 | .treatment(treatmentBuilder.build()) |
| 483 | .constraints(SdnIpFib.CONSTRAINTS) |
| 484 | .build(); |
| 485 | |
| 486 | return intent; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Builds a MultiPointToSinglePointIntent with dest sw3 (no VLAN Id) and src |
| 491 | * sw1, sw2. |
| 492 | */ |
| 493 | private MultiPointToSinglePointIntent createIntentToThreeSrcOneTwo(IpPrefix ipPrefix) { |
| 494 | // Build the expected treatment |
| 495 | TrafficTreatment.Builder treatmentBuilder = |
| 496 | DefaultTrafficTreatment.builder(); |
| 497 | treatmentBuilder.setEthDst(MAC3); |
| 498 | |
| 499 | // Build the expected egress FilteredConnectPoint |
| 500 | FilteredConnectPoint egressFilteredCP = new FilteredConnectPoint(SW3_ETH1); |
| 501 | |
| 502 | // Build the expected selectors |
| 503 | Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet(); |
| 504 | |
| 505 | // Build the expected ingress FilteredConnectPoint for sw1 |
| 506 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 507 | selector.matchVlanId(VLAN10); |
| 508 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 509 | selector.matchIPDst(ipPrefix); |
| 510 | FilteredConnectPoint ingressFilteredCP = |
| 511 | new FilteredConnectPoint(SW1_ETH1, selector.build()); |
| 512 | ingressFilteredCPs.add(ingressFilteredCP); |
| 513 | |
| 514 | // Build the expected ingress FilteredConnectPoint for sw2 |
| 515 | selector = DefaultTrafficSelector.builder(); |
| 516 | selector.matchVlanId(VLAN20); |
| 517 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 518 | selector.matchIPDst(ipPrefix); |
| 519 | ingressFilteredCP = new FilteredConnectPoint(SW2_ETH1, selector.build()); |
| 520 | ingressFilteredCPs.add(ingressFilteredCP); |
| 521 | |
| 522 | // Build the expected intent |
| 523 | MultiPointToSinglePointIntent intent = |
| 524 | MultiPointToSinglePointIntent.builder() |
| 525 | .appId(APPID) |
| 526 | .key(Key.of(ipPrefix.toString(), APPID)) |
| 527 | .filteredIngressPoints(ingressFilteredCPs) |
| 528 | .filteredEgressPoint(egressFilteredCP) |
| 529 | .treatment(treatmentBuilder.build()) |
| 530 | .constraints(SdnIpFib.CONSTRAINTS) |
| 531 | .build(); |
| 532 | |
| 533 | return intent; |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * Builds a MultiPointToSinglePointIntent with dest sw3 (no VLAN Id) and src |
| 538 | * sw2. |
| 539 | */ |
| 540 | private MultiPointToSinglePointIntent createIntentToThreeSrcTwo(IpPrefix ipPrefix) { |
| 541 | // Build the expected treatment |
| 542 | TrafficTreatment.Builder treatmentBuilder = |
| 543 | DefaultTrafficTreatment.builder(); |
| 544 | treatmentBuilder.setEthDst(MAC3); |
| 545 | |
| 546 | // Build the expected egress FilteredConnectPoint |
| 547 | FilteredConnectPoint egressFilteredCP = new FilteredConnectPoint(SW3_ETH1); |
| 548 | |
| 549 | // Build the expected ingress FilteredConnectPoint for sw2 |
| 550 | Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet(); |
| 551 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 552 | selector.matchVlanId(VLAN20); |
| 553 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 554 | selector.matchIPDst(ipPrefix); |
| 555 | FilteredConnectPoint ingressFilteredCP = |
| 556 | new FilteredConnectPoint(SW2_ETH1, selector.build()); |
| 557 | ingressFilteredCPs.add(ingressFilteredCP); |
| 558 | |
| 559 | // Build the expected intent |
| 560 | MultiPointToSinglePointIntent intent = |
| 561 | MultiPointToSinglePointIntent.builder() |
| 562 | .appId(APPID) |
| 563 | .key(Key.of(ipPrefix.toString(), APPID)) |
| 564 | .filteredIngressPoints(ingressFilteredCPs) |
| 565 | .filteredEgressPoint(egressFilteredCP) |
| 566 | .treatment(treatmentBuilder.build()) |
| 567 | .constraints(SdnIpFib.CONSTRAINTS) |
| 568 | .build(); |
| 569 | |
| 570 | return intent; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Builds a MultiPointToSinglePointIntent with dest sw3 (no VLAN Id) and src |
| 575 | * sw1, sw2, sw4. |
| 576 | */ |
| 577 | private MultiPointToSinglePointIntent createIntentToThreeSrcOneTwoFour(IpPrefix ipPrefix) { |
| 578 | // Build the expected treatment |
| 579 | TrafficTreatment.Builder treatmentBuilder = |
| 580 | DefaultTrafficTreatment.builder(); |
| 581 | treatmentBuilder.setEthDst(MAC3); |
| 582 | |
| 583 | // Build the expected egress FilteredConnectPoint |
| 584 | FilteredConnectPoint egressFilteredCP = new FilteredConnectPoint(SW3_ETH1); |
| 585 | |
| 586 | // Build the expected selectors |
| 587 | Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet(); |
| 588 | |
| 589 | // Build the expected ingress FilteredConnectPoint for sw1 |
| 590 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 591 | selector.matchVlanId(VLAN10); |
| 592 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 593 | selector.matchIPDst(ipPrefix); |
| 594 | FilteredConnectPoint ingressFilteredCP = |
| 595 | new FilteredConnectPoint(SW1_ETH1, selector.build()); |
| 596 | ingressFilteredCPs.add(ingressFilteredCP); |
| 597 | |
| 598 | // Build the expected ingress FilteredConnectPoint for sw2 |
| 599 | selector = DefaultTrafficSelector.builder(); |
| 600 | selector.matchVlanId(VLAN20); |
| 601 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 602 | selector.matchIPDst(ipPrefix); |
| 603 | ingressFilteredCP = new FilteredConnectPoint(SW2_ETH1, selector.build()); |
| 604 | ingressFilteredCPs.add(ingressFilteredCP); |
| 605 | |
| 606 | // Build the expected ingress FilteredConnectPoint for sw4 |
| 607 | selector = DefaultTrafficSelector.builder(); |
| 608 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 609 | selector.matchIPDst(ipPrefix); |
| 610 | ingressFilteredCP = new FilteredConnectPoint(SW4_ETH1, selector.build()); |
| 611 | ingressFilteredCPs.add(ingressFilteredCP); |
| 612 | |
| 613 | // Build the expected intent |
| 614 | MultiPointToSinglePointIntent intent = |
| 615 | MultiPointToSinglePointIntent.builder() |
| 616 | .appId(APPID) |
| 617 | .key(Key.of(ipPrefix.toString(), APPID)) |
| 618 | .filteredIngressPoints(ingressFilteredCPs) |
| 619 | .filteredEgressPoint(egressFilteredCP) |
| 620 | .treatment(treatmentBuilder.build()) |
| 621 | .constraints(SdnIpFib.CONSTRAINTS) |
| 622 | .build(); |
| 623 | |
| 624 | return intent; |
| 625 | } |
| 626 | |
Jonathan Hart | f2e7a34 | 2017-03-20 15:43:21 -0700 | [diff] [blame] | 627 | private static ResolvedRoute createRoute(IpPrefix prefix, IpAddress nextHop, |
Ray Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 628 | MacAddress nextHopMac) { |
Jonathan Hart | f2e7a34 | 2017-03-20 15:43:21 -0700 | [diff] [blame] | 629 | return new ResolvedRoute( |
Ray Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 630 | new Route(Route.Source.UNDEFINED, prefix, nextHop), nextHopMac); |
Jonathan Hart | f2e7a34 | 2017-03-20 15:43:21 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 633 | private class TestCoreService extends CoreServiceAdapter { |
| 634 | @Override |
| 635 | public ApplicationId getAppId(String name) { |
| 636 | return APPID; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | private class TestRouteService extends RouteServiceAdapter { |
| 641 | @Override |
| 642 | public void addListener(RouteListener routeListener) { |
| 643 | SdnIpFibTest.this.routeListener = routeListener; |
| 644 | } |
| 645 | } |
| 646 | |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 647 | private class TestNetworkConfigService extends NetworkConfigServiceAdapter { |
| 648 | /** |
| 649 | * Returns an empty BGP network configuration to be able to correctly |
| 650 | * return the encapsulation parameter when needed. |
| 651 | * |
| 652 | * @return an empty BGP network configuration object |
| 653 | */ |
| 654 | @Override |
| 655 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 656 | ApplicationId appId = |
| 657 | new TestApplicationId(SdnIp.SDN_IP_APP); |
| 658 | |
| 659 | ObjectMapper mapper = new ObjectMapper(); |
| 660 | ConfigApplyDelegate delegate = new MockCfgDelegate(); |
| 661 | JsonNode emptyTree = new ObjectMapper().createObjectNode(); |
| 662 | |
| 663 | SdnIpConfig sdnIpConfig = new SdnIpConfig(); |
| 664 | |
| 665 | sdnIpConfig.init(appId, "sdnip-test", emptyTree, mapper, delegate); |
| 666 | |
| 667 | return (C) sdnIpConfig; |
| 668 | } |
| 669 | } |
| 670 | |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 671 | private class InterfaceServiceDelegate extends InterfaceServiceAdapter { |
| 672 | @Override |
| 673 | public void addListener(InterfaceListener listener) { |
| 674 | SdnIpFibTest.this.interfaceListener = listener; |
| 675 | } |
| 676 | } |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 677 | |
| 678 | private class MockCfgDelegate implements ConfigApplyDelegate { |
| 679 | @Override |
| 680 | public void onApply(@SuppressWarnings("rawtypes") Config config) { |
| 681 | config.apply(); |
| 682 | } |
| 683 | } |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 684 | } |