Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [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 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
| 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; |
| 31 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 32 | import org.onosproject.core.CoreServiceAdapter; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 33 | import org.onosproject.incubator.net.intf.Interface; |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 34 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 35 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 37 | import org.onosproject.incubator.net.intf.InterfaceServiceAdapter; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 38 | import org.onosproject.net.ConnectPoint; |
| 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.PortNumber; |
| 41 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 42 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 43 | import org.onosproject.net.flow.TrafficSelector; |
| 44 | import org.onosproject.net.flow.TrafficTreatment; |
| 45 | import org.onosproject.net.host.InterfaceIpAddress; |
| 46 | import org.onosproject.net.intent.AbstractIntentTest; |
| 47 | import org.onosproject.net.intent.Key; |
| 48 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 49 | import org.onosproject.routing.FibEntry; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 50 | import org.onosproject.routing.FibListener; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 51 | import org.onosproject.routing.FibUpdate; |
| 52 | import org.onosproject.routing.IntentSynchronizationService; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 53 | import org.onosproject.routing.RoutingServiceAdapter; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 54 | |
| 55 | import java.util.Collections; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 56 | import java.util.HashSet; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 57 | import java.util.List; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 58 | import java.util.Set; |
| 59 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 60 | import static org.easymock.EasyMock.anyObject; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 61 | import static org.easymock.EasyMock.createMock; |
| 62 | import static org.easymock.EasyMock.expect; |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 63 | import static org.easymock.EasyMock.expectLastCall; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 64 | import static org.easymock.EasyMock.replay; |
| 65 | import static org.easymock.EasyMock.reset; |
| 66 | import static org.easymock.EasyMock.verify; |
Jonathan Hart | 33b81f2 | 2016-02-05 09:25:50 -0800 | [diff] [blame] | 67 | import static org.onosproject.routing.TestIntentServiceHelper.eqExceptId; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Unit tests for SdnIpFib. |
| 71 | */ |
| 72 | public class SdnIpFibTest extends AbstractIntentTest { |
| 73 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 74 | private InterfaceService interfaceService; |
| 75 | |
| 76 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 77 | DeviceId.deviceId("of:0000000000000001"), |
| 78 | PortNumber.portNumber(1)); |
| 79 | |
| 80 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 81 | DeviceId.deviceId("of:0000000000000002"), |
| 82 | PortNumber.portNumber(1)); |
| 83 | |
| 84 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 85 | DeviceId.deviceId("of:0000000000000003"), |
| 86 | PortNumber.portNumber(1)); |
| 87 | |
| 88 | private static final ConnectPoint SW4_ETH1 = new ConnectPoint( |
| 89 | DeviceId.deviceId("of:0000000000000004"), |
| 90 | PortNumber.portNumber(1)); |
| 91 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 92 | private static final ConnectPoint SW5_ETH1 = new ConnectPoint( |
| 93 | DeviceId.deviceId("of:0000000000000005"), |
| 94 | PortNumber.portNumber(1)); |
| 95 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 96 | private SdnIpFib sdnipFib; |
| 97 | private IntentSynchronizationService intentSynchronizer; |
| 98 | private final Set<Interface> interfaces = Sets.newHashSet(); |
| 99 | |
| 100 | private static final ApplicationId APPID = TestApplicationId.create("SDNIP"); |
| 101 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 102 | private FibListener fibListener; |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 103 | private InterfaceListener interfaceListener; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 104 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 105 | @Before |
| 106 | public void setUp() throws Exception { |
| 107 | super.setUp(); |
| 108 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 109 | interfaceService = createMock(InterfaceService.class); |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 110 | interfaceService.addListener(anyObject(InterfaceListener.class)); |
| 111 | expectLastCall().andDelegateTo(new InterfaceServiceDelegate()); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 112 | |
| 113 | // These will set expectations on routingConfig and interfaceService |
| 114 | setUpInterfaceService(); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 115 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 116 | replay(interfaceService); |
| 117 | |
| 118 | intentSynchronizer = createMock(IntentSynchronizationService.class); |
| 119 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 120 | sdnipFib = new SdnIpFib(); |
| 121 | sdnipFib.routingService = new TestRoutingService(); |
| 122 | sdnipFib.coreService = new TestCoreService(); |
| 123 | sdnipFib.interfaceService = interfaceService; |
| 124 | sdnipFib.intentSynchronizer = intentSynchronizer; |
| 125 | |
| 126 | sdnipFib.activate(); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 130 | * Sets up InterfaceService. |
| 131 | */ |
| 132 | private void setUpInterfaceService() { |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 133 | List<InterfaceIpAddress> interfaceIpAddresses1 = Lists.newArrayList(); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 134 | interfaceIpAddresses1.add(new InterfaceIpAddress( |
| 135 | IpAddress.valueOf("192.168.10.101"), |
| 136 | IpPrefix.valueOf("192.168.10.0/24"))); |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 137 | Interface sw1Eth1 = new Interface("sw1-eth1", SW1_ETH1, |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 138 | interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"), |
| 139 | VlanId.NONE); |
| 140 | interfaces.add(sw1Eth1); |
| 141 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 142 | List<InterfaceIpAddress> interfaceIpAddresses2 = Lists.newArrayList(); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 143 | interfaceIpAddresses2.add( |
| 144 | new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"), |
| 145 | IpPrefix.valueOf("192.168.20.0/24"))); |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 146 | Interface sw2Eth1 = new Interface("sw2-eth1", SW2_ETH1, |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 147 | interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"), |
| 148 | VlanId.NONE); |
| 149 | interfaces.add(sw2Eth1); |
| 150 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 151 | List<InterfaceIpAddress> interfaceIpAddresses3 = Lists.newArrayList(); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 152 | interfaceIpAddresses3.add( |
| 153 | new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"), |
| 154 | IpPrefix.valueOf("192.168.30.0/24"))); |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 155 | Interface sw3Eth1 = new Interface("sw3-eth1", SW3_ETH1, |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 156 | interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"), |
| 157 | VlanId.NONE); |
| 158 | interfaces.add(sw3Eth1); |
| 159 | |
| 160 | InterfaceIpAddress interfaceIpAddress4 = |
| 161 | new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), |
| 162 | IpPrefix.valueOf("192.168.40.0/24")); |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 163 | Interface sw4Eth1 = new Interface("sw4-eth1", SW4_ETH1, |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 164 | Lists.newArrayList(interfaceIpAddress4), |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 165 | MacAddress.valueOf("00:00:00:00:00:04"), |
| 166 | VlanId.vlanId((short) 1)); |
| 167 | |
| 168 | expect(interfaceService.getInterfacesByPort(SW4_ETH1)).andReturn( |
| 169 | Collections.singleton(sw4Eth1)).anyTimes(); |
| 170 | expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.40.1"))) |
| 171 | .andReturn(sw4Eth1).anyTimes(); |
| 172 | |
| 173 | interfaces.add(sw4Eth1); |
| 174 | |
| 175 | expect(interfaceService.getInterfacesByPort(SW1_ETH1)).andReturn( |
| 176 | Collections.singleton(sw1Eth1)).anyTimes(); |
| 177 | expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.10.1"))) |
| 178 | .andReturn(sw1Eth1).anyTimes(); |
| 179 | expect(interfaceService.getInterfacesByPort(SW2_ETH1)).andReturn( |
| 180 | Collections.singleton(sw2Eth1)).anyTimes(); |
| 181 | expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.20.1"))) |
| 182 | .andReturn(sw2Eth1).anyTimes(); |
| 183 | expect(interfaceService.getInterfacesByPort(SW3_ETH1)).andReturn( |
| 184 | Collections.singleton(sw3Eth1)).anyTimes(); |
| 185 | expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.30.1"))) |
| 186 | .andReturn(sw3Eth1).anyTimes(); |
| 187 | expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes(); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Tests adding a FIB entry to the IntentSynchronizer. |
| 192 | * |
| 193 | * We verify that the synchronizer records the correct state and that the |
| 194 | * correct intent is submitted to the IntentService. |
| 195 | */ |
| 196 | @Test |
| 197 | public void testFibAdd() { |
| 198 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 199 | FibEntry fibEntry = new FibEntry(prefix, |
| 200 | Ip4Address.valueOf("192.168.10.1"), |
| 201 | MacAddress.valueOf("00:00:00:00:00:01")); |
| 202 | |
| 203 | // Construct a MultiPointToSinglePointIntent intent |
| 204 | TrafficSelector.Builder selectorBuilder = |
| 205 | DefaultTrafficSelector.builder(); |
| 206 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 207 | fibEntry.prefix()); |
| 208 | |
| 209 | TrafficTreatment.Builder treatmentBuilder = |
| 210 | DefaultTrafficTreatment.builder(); |
| 211 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 212 | |
| 213 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 214 | ingressPoints.add(SW2_ETH1); |
| 215 | ingressPoints.add(SW3_ETH1); |
| 216 | ingressPoints.add(SW4_ETH1); |
| 217 | |
| 218 | MultiPointToSinglePointIntent intent = |
| 219 | MultiPointToSinglePointIntent.builder() |
| 220 | .appId(APPID) |
| 221 | .key(Key.of(prefix.toString(), APPID)) |
| 222 | .selector(selectorBuilder.build()) |
| 223 | .treatment(treatmentBuilder.build()) |
| 224 | .ingressPoints(ingressPoints) |
| 225 | .egressPoint(SW1_ETH1) |
| 226 | .constraints(SdnIpFib.CONSTRAINTS) |
| 227 | .build(); |
| 228 | |
| 229 | // Setup the expected intents |
| 230 | intentSynchronizer.submit(eqExceptId(intent)); |
| 231 | replay(intentSynchronizer); |
| 232 | |
| 233 | // Send in the UPDATE FibUpdate |
| 234 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry); |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 235 | fibListener.update(Collections.singleton(fibUpdate), Collections.emptyList()); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 236 | |
| 237 | verify(intentSynchronizer); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Tests adding a FIB entry with to a next hop in a VLAN. |
| 242 | * |
| 243 | * We verify that the synchronizer records the correct state and that the |
| 244 | * correct intent is submitted to the IntentService. |
| 245 | */ |
| 246 | @Test |
| 247 | public void testFibAddWithVlan() { |
| 248 | IpPrefix prefix = Ip4Prefix.valueOf("3.3.3.0/24"); |
| 249 | FibEntry fibEntry = new FibEntry(prefix, |
| 250 | Ip4Address.valueOf("192.168.40.1"), |
| 251 | MacAddress.valueOf("00:00:00:00:00:04")); |
| 252 | |
| 253 | // Construct a MultiPointToSinglePointIntent intent |
| 254 | TrafficSelector.Builder selectorBuilder = |
| 255 | DefaultTrafficSelector.builder(); |
| 256 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 257 | .matchIPDst(fibEntry.prefix()) |
| 258 | .matchVlanId(VlanId.ANY); |
| 259 | |
| 260 | TrafficTreatment.Builder treatmentBuilder = |
| 261 | DefaultTrafficTreatment.builder(); |
| 262 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:04")) |
| 263 | .setVlanId(VlanId.vlanId((short) 1)); |
| 264 | |
| 265 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 266 | ingressPoints.add(SW1_ETH1); |
| 267 | ingressPoints.add(SW2_ETH1); |
| 268 | ingressPoints.add(SW3_ETH1); |
| 269 | |
| 270 | MultiPointToSinglePointIntent intent = |
| 271 | MultiPointToSinglePointIntent.builder() |
| 272 | .appId(APPID) |
| 273 | .key(Key.of(prefix.toString(), APPID)) |
| 274 | .selector(selectorBuilder.build()) |
| 275 | .treatment(treatmentBuilder.build()) |
| 276 | .ingressPoints(ingressPoints) |
| 277 | .egressPoint(SW4_ETH1) |
| 278 | .constraints(SdnIpFib.CONSTRAINTS) |
| 279 | .build(); |
| 280 | |
| 281 | // Setup the expected intents |
| 282 | intentSynchronizer.submit(eqExceptId(intent)); |
| 283 | |
| 284 | replay(intentSynchronizer); |
| 285 | |
| 286 | // Send in the UPDATE FibUpdate |
| 287 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, fibEntry); |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 288 | fibListener.update(Collections.singleton(fibUpdate), Collections.emptyList()); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 289 | |
| 290 | verify(intentSynchronizer); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Tests updating a FIB entry. |
| 295 | * |
| 296 | * We verify that the synchronizer records the correct state and that the |
| 297 | * correct intent is submitted to the IntentService. |
| 298 | */ |
| 299 | @Test |
| 300 | public void testFibUpdate() { |
| 301 | // Firstly add a route |
| 302 | testFibAdd(); |
| 303 | |
| 304 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 305 | |
| 306 | // Start to construct a new route entry and new intent |
| 307 | FibEntry fibEntryUpdate = new FibEntry(prefix, |
| 308 | Ip4Address.valueOf("192.168.20.1"), |
| 309 | MacAddress.valueOf("00:00:00:00:00:02")); |
| 310 | |
| 311 | // Construct a new MultiPointToSinglePointIntent intent |
| 312 | TrafficSelector.Builder selectorBuilderNew = |
| 313 | DefaultTrafficSelector.builder(); |
| 314 | selectorBuilderNew.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 315 | fibEntryUpdate.prefix()); |
| 316 | |
| 317 | TrafficTreatment.Builder treatmentBuilderNew = |
| 318 | DefaultTrafficTreatment.builder(); |
| 319 | treatmentBuilderNew.setEthDst(MacAddress.valueOf("00:00:00:00:00:02")); |
| 320 | |
| 321 | Set<ConnectPoint> ingressPointsNew = new HashSet<>(); |
| 322 | ingressPointsNew.add(SW1_ETH1); |
| 323 | ingressPointsNew.add(SW3_ETH1); |
| 324 | ingressPointsNew.add(SW4_ETH1); |
| 325 | |
| 326 | MultiPointToSinglePointIntent intentNew = |
| 327 | MultiPointToSinglePointIntent.builder() |
| 328 | .appId(APPID) |
| 329 | .key(Key.of(prefix.toString(), APPID)) |
| 330 | .selector(selectorBuilderNew.build()) |
| 331 | .treatment(treatmentBuilderNew.build()) |
| 332 | .ingressPoints(ingressPointsNew) |
| 333 | .egressPoint(SW2_ETH1) |
| 334 | .constraints(SdnIpFib.CONSTRAINTS) |
| 335 | .build(); |
| 336 | |
| 337 | // Set up test expectation |
| 338 | reset(intentSynchronizer); |
| 339 | |
| 340 | // Setup the expected intents |
| 341 | intentSynchronizer.submit(eqExceptId(intentNew)); |
| 342 | replay(intentSynchronizer); |
| 343 | |
| 344 | // Send in the UPDATE FibUpdate |
| 345 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.UPDATE, |
| 346 | fibEntryUpdate); |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 347 | fibListener.update(Collections.singletonList(fibUpdate), |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 348 | Collections.emptyList()); |
| 349 | |
| 350 | verify(intentSynchronizer); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Tests deleting a FIB entry. |
| 355 | * |
| 356 | * We verify that the synchronizer records the correct state and that the |
| 357 | * correct intent is withdrawn from the IntentService. |
| 358 | */ |
| 359 | @Test |
| 360 | public void testFibDelete() { |
| 361 | // Firstly add a route |
| 362 | testFibAdd(); |
| 363 | |
| 364 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 365 | |
| 366 | // Construct the existing route entry |
| 367 | FibEntry fibEntry = new FibEntry(prefix, null, null); |
| 368 | |
| 369 | // Construct the existing MultiPointToSinglePoint intent |
| 370 | TrafficSelector.Builder selectorBuilder = |
| 371 | DefaultTrafficSelector.builder(); |
| 372 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 373 | fibEntry.prefix()); |
| 374 | |
| 375 | TrafficTreatment.Builder treatmentBuilder = |
| 376 | DefaultTrafficTreatment.builder(); |
| 377 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 378 | |
| 379 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 380 | ingressPoints.add(SW2_ETH1); |
| 381 | ingressPoints.add(SW3_ETH1); |
| 382 | ingressPoints.add(SW4_ETH1); |
| 383 | |
| 384 | MultiPointToSinglePointIntent addedIntent = |
| 385 | MultiPointToSinglePointIntent.builder() |
| 386 | .appId(APPID) |
| 387 | .key(Key.of(prefix.toString(), APPID)) |
| 388 | .selector(selectorBuilder.build()) |
| 389 | .treatment(treatmentBuilder.build()) |
| 390 | .ingressPoints(ingressPoints) |
| 391 | .egressPoint(SW1_ETH1) |
| 392 | .constraints(SdnIpFib.CONSTRAINTS) |
| 393 | .build(); |
| 394 | |
| 395 | // Set up expectation |
| 396 | reset(intentSynchronizer); |
| 397 | // Setup the expected intents |
| 398 | intentSynchronizer.withdraw(eqExceptId(addedIntent)); |
| 399 | replay(intentSynchronizer); |
| 400 | |
| 401 | // Send in the DELETE FibUpdate |
| 402 | FibUpdate fibUpdate = new FibUpdate(FibUpdate.Type.DELETE, fibEntry); |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 403 | fibListener.update(Collections.emptyList(), Collections.singletonList(fibUpdate)); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 404 | |
| 405 | verify(intentSynchronizer); |
| 406 | } |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 407 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 408 | @Test |
| 409 | public void testAddInterface() { |
| 410 | testFibAdd(); |
| 411 | |
| 412 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 413 | |
| 414 | // Construct the existing MultiPointToSinglePoint intent |
| 415 | TrafficSelector.Builder selectorBuilder = |
| 416 | DefaultTrafficSelector.builder(); |
| 417 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(prefix); |
| 418 | |
| 419 | TrafficTreatment.Builder treatmentBuilder = |
| 420 | DefaultTrafficTreatment.builder(); |
| 421 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 422 | |
| 423 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 424 | ingressPoints.add(SW2_ETH1); |
| 425 | ingressPoints.add(SW3_ETH1); |
| 426 | ingressPoints.add(SW4_ETH1); |
| 427 | ingressPoints.add(SW5_ETH1); |
| 428 | |
| 429 | MultiPointToSinglePointIntent addedIntent = |
| 430 | MultiPointToSinglePointIntent.builder() |
| 431 | .appId(APPID) |
| 432 | .key(Key.of(prefix.toString(), APPID)) |
| 433 | .selector(selectorBuilder.build()) |
| 434 | .treatment(treatmentBuilder.build()) |
| 435 | .ingressPoints(ingressPoints) |
| 436 | .egressPoint(SW1_ETH1) |
| 437 | .constraints(SdnIpFib.CONSTRAINTS) |
| 438 | .build(); |
| 439 | |
| 440 | reset(intentSynchronizer); |
| 441 | |
| 442 | intentSynchronizer.submit(eqExceptId(addedIntent)); |
| 443 | expectLastCall().once(); |
| 444 | |
| 445 | replay(intentSynchronizer); |
| 446 | |
| 447 | Interface intf = new Interface("newintf", SW5_ETH1, |
| 448 | Collections.singletonList(InterfaceIpAddress.valueOf("192.168.50.101/24")), |
| 449 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE); |
| 450 | InterfaceEvent intfEvent = new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, intf); |
| 451 | interfaceListener.event(intfEvent); |
| 452 | |
| 453 | verify(intentSynchronizer); |
| 454 | } |
| 455 | |
| 456 | @Test |
| 457 | public void testRemoveInterface() { |
| 458 | testFibAdd(); |
| 459 | |
| 460 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 461 | |
| 462 | // Construct the existing MultiPointToSinglePoint intent |
| 463 | TrafficSelector.Builder selectorBuilder = |
| 464 | DefaultTrafficSelector.builder(); |
| 465 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(prefix); |
| 466 | |
| 467 | TrafficTreatment.Builder treatmentBuilder = |
| 468 | DefaultTrafficTreatment.builder(); |
| 469 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 470 | |
| 471 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 472 | ingressPoints.add(SW2_ETH1); |
| 473 | ingressPoints.add(SW3_ETH1); |
| 474 | |
| 475 | MultiPointToSinglePointIntent addedIntent = |
| 476 | MultiPointToSinglePointIntent.builder() |
| 477 | .appId(APPID) |
| 478 | .key(Key.of(prefix.toString(), APPID)) |
| 479 | .selector(selectorBuilder.build()) |
| 480 | .treatment(treatmentBuilder.build()) |
| 481 | .ingressPoints(ingressPoints) |
| 482 | .egressPoint(SW1_ETH1) |
| 483 | .constraints(SdnIpFib.CONSTRAINTS) |
| 484 | .build(); |
| 485 | |
| 486 | reset(intentSynchronizer); |
| 487 | |
| 488 | intentSynchronizer.submit(eqExceptId(addedIntent)); |
| 489 | expectLastCall().once(); |
| 490 | |
| 491 | replay(intentSynchronizer); |
| 492 | |
| 493 | Interface intf = new Interface("newintf", SW4_ETH1, |
| 494 | Collections.singletonList(InterfaceIpAddress.valueOf("192.168.50.101/24")), |
| 495 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE); |
| 496 | InterfaceEvent intfEvent = new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, intf); |
| 497 | interfaceListener.event(intfEvent); |
| 498 | |
| 499 | verify(intentSynchronizer); |
| 500 | } |
| 501 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 502 | private class TestCoreService extends CoreServiceAdapter { |
| 503 | @Override |
| 504 | public ApplicationId getAppId(String name) { |
| 505 | return APPID; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | private class TestRoutingService extends RoutingServiceAdapter { |
| 510 | |
| 511 | @Override |
| 512 | public void addFibListener(FibListener fibListener) { |
| 513 | SdnIpFibTest.this.fibListener = fibListener; |
| 514 | } |
| 515 | } |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 516 | |
| 517 | private class InterfaceServiceDelegate extends InterfaceServiceAdapter { |
| 518 | |
| 519 | @Override |
| 520 | public void addListener(InterfaceListener listener) { |
| 521 | SdnIpFibTest.this.interfaceListener = listener; |
| 522 | } |
| 523 | } |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 524 | } |