Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [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.vpls; |
| 17 | |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 19 | import java.util.HashSet; |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 20 | import java.util.List; |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 21 | import java.util.Map; |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 22 | import java.util.Set; |
| 23 | import java.util.concurrent.atomic.AtomicLong; |
| 24 | import java.util.stream.Collectors; |
| 25 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 26 | import com.google.common.collect.Sets; |
| 27 | import com.google.common.collect.ImmutableSet; |
| 28 | import com.google.common.collect.SetMultimap; |
| 29 | import com.google.common.collect.HashMultimap; |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 30 | import com.google.common.collect.Maps; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 31 | import com.google.common.collect.Lists; |
| 32 | import com.google.common.collect.ImmutableSetMultimap; |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 33 | import org.junit.After; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 34 | import org.junit.Before; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 35 | import org.junit.Test; |
| 36 | import org.onlab.packet.Ip4Address; |
| 37 | import org.onlab.packet.MacAddress; |
| 38 | import org.onlab.packet.VlanId; |
| 39 | import org.onosproject.TestApplicationId; |
| 40 | import org.onosproject.app.ApplicationService; |
| 41 | import org.onosproject.core.ApplicationId; |
| 42 | import org.onosproject.core.CoreService; |
| 43 | import org.onosproject.core.IdGenerator; |
| 44 | import org.onosproject.incubator.net.intf.Interface; |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 45 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 46 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 47 | import org.onosproject.net.ConnectPoint; |
| 48 | import org.onosproject.net.DefaultHost; |
| 49 | import org.onosproject.net.DeviceId; |
| 50 | import org.onosproject.net.Host; |
| 51 | import org.onosproject.net.HostId; |
| 52 | import org.onosproject.net.HostLocation; |
| 53 | import org.onosproject.net.PortNumber; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 54 | import org.onosproject.net.FilteredConnectPoint; |
| 55 | import org.onosproject.net.config.NetworkConfigService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 56 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 57 | import org.onosproject.net.flow.TrafficSelector; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 58 | import org.onosproject.net.flow.criteria.Criterion; |
| 59 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 60 | import org.onosproject.net.host.HostEvent; |
| 61 | import org.onosproject.net.host.HostListener; |
| 62 | import org.onosproject.net.host.HostService; |
| 63 | import org.onosproject.net.host.HostServiceAdapter; |
| 64 | import org.onosproject.net.intent.Intent; |
| 65 | import org.onosproject.net.intent.IntentService; |
| 66 | import org.onosproject.net.intent.IntentServiceAdapter; |
| 67 | import org.onosproject.net.intent.IntentUtils; |
| 68 | import org.onosproject.net.intent.Key; |
| 69 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 70 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
| 71 | import org.onosproject.net.provider.ProviderId; |
| 72 | import org.onosproject.routing.IntentSynchronizationAdminService; |
| 73 | import org.onosproject.routing.IntentSynchronizationService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 74 | import org.onosproject.vpls.config.VplsConfigurationService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 75 | |
| 76 | import static java.lang.String.format; |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 77 | import static org.easymock.EasyMock.anyObject; |
| 78 | import static org.easymock.EasyMock.createMock; |
| 79 | import static org.easymock.EasyMock.expect; |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 80 | import static org.easymock.EasyMock.expectLastCall; |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 81 | import static org.easymock.EasyMock.replay; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 82 | import static org.junit.Assert.assertEquals; |
| 83 | import static org.junit.Assert.assertTrue; |
| 84 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 85 | import static org.onosproject.vpls.IntentInstaller.PREFIX_BROADCAST; |
| 86 | import static org.onosproject.vpls.IntentInstaller.PREFIX_UNICAST; |
| 87 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Tests for the {@link Vpls} class. |
| 90 | */ |
| 91 | public class VplsTest { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 92 | private static final String APP_NAME = "org.onosproject.vpls"; |
| 93 | private static final ApplicationId APPID = TestApplicationId.create(APP_NAME); |
| 94 | private static final String DASH = "-"; |
| 95 | private static final int PRIORITY_OFFSET = 1000; |
| 96 | private static final String NET1 = "net1"; |
| 97 | private static final String NET2 = "net2"; |
| 98 | private static final String COMPARE = "Comparing %s to %s"; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 99 | |
| 100 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 101 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 102 | private static final DeviceId DID1 = getDeviceId(1); |
| 103 | private static final DeviceId DID2 = getDeviceId(2); |
| 104 | private static final DeviceId DID3 = getDeviceId(3); |
| 105 | private static final DeviceId DID4 = getDeviceId(4); |
| 106 | private static final DeviceId DID5 = getDeviceId(5); |
| 107 | private static final DeviceId DID6 = getDeviceId(6); |
| 108 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 109 | private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1); |
| 110 | private static final ConnectPoint CP2 = new ConnectPoint(DID2, P1); |
| 111 | private static final ConnectPoint CP3 = new ConnectPoint(DID3, P1); |
| 112 | private static final ConnectPoint CP4 = new ConnectPoint(DID4, P1); |
| 113 | private static final ConnectPoint CP5 = new ConnectPoint(DID5, P1); |
| 114 | private static final ConnectPoint CP6 = new ConnectPoint(DID6, P1); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 115 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 116 | private static final VlanId VLAN100 = VlanId.vlanId((short) 100); |
| 117 | private static final VlanId VLAN200 = VlanId.vlanId((short) 200); |
| 118 | private static final VlanId VLAN300 = VlanId.vlanId((short) 300); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 119 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 120 | private static final MacAddress MAC1 = |
| 121 | MacAddress.valueOf("00:00:00:00:00:01"); |
| 122 | private static final MacAddress MAC2 = |
| 123 | MacAddress.valueOf("00:00:00:00:00:02"); |
| 124 | private static final MacAddress MAC3 = |
| 125 | MacAddress.valueOf("00:00:00:00:00:03"); |
| 126 | private static final MacAddress MAC4 = |
| 127 | MacAddress.valueOf("00:00:00:00:00:04"); |
| 128 | private static final MacAddress MAC5 = |
| 129 | MacAddress.valueOf("00:00:00:00:00:05"); |
| 130 | private static final MacAddress MAC6 = |
| 131 | MacAddress.valueOf("00:00:00:00:00:06"); |
| 132 | private static final MacAddress MAC7 = |
| 133 | MacAddress.valueOf("00:00:00:00:00:07"); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 134 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 135 | private static final Ip4Address IP1 = Ip4Address.valueOf("192.168.1.1"); |
| 136 | private static final Ip4Address IP2 = Ip4Address.valueOf("192.168.1.2"); |
| 137 | |
| 138 | private static final HostId HID1 = HostId.hostId(MAC1, VLAN100); |
| 139 | private static final HostId HID2 = HostId.hostId(MAC2, VLAN100); |
| 140 | private static final HostId HID3 = HostId.hostId(MAC3, VLAN200); |
| 141 | private static final HostId HID4 = HostId.hostId(MAC4, VLAN200); |
| 142 | private static final HostId HID5 = HostId.hostId(MAC5, VLAN300); |
| 143 | private static final HostId HID6 = HostId.hostId(MAC6, VLAN300); |
| 144 | private static final HostId HID7 = HostId.hostId(MAC7, VLAN300); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 145 | |
| 146 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 147 | |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 148 | private static IdGenerator idGenerator; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 149 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 150 | private final Interface v100h1 = |
| 151 | new Interface("v100h1", CP1, null, null, VLAN100); |
| 152 | private final Interface v100h2 = |
| 153 | new Interface("v100h2", CP2, null, null, VLAN100); |
| 154 | private final Interface v200h1 = |
| 155 | new Interface("v200h1", CP3, null, null, VLAN200); |
| 156 | private final Interface v200h2 = |
| 157 | new Interface("v200h2", CP4, null, null, VLAN200); |
| 158 | private final Interface v300h1 = |
| 159 | new Interface("v300h1", CP5, null, null, VLAN300); |
| 160 | private final Interface v300h2 = |
| 161 | new Interface("v300h2", CP6, null, null, VLAN300); |
| 162 | |
| 163 | private final Host v100host1 = |
| 164 | new DefaultHost(PID, HID1, MAC1, VLAN100, |
| 165 | getLocation(1), Collections.singleton(IP1)); |
| 166 | private final Host v100host2 = |
| 167 | new DefaultHost(PID, HID2, MAC2, VLAN100, |
| 168 | getLocation(2), Sets.newHashSet()); |
| 169 | private final Host v200host1 = |
| 170 | new DefaultHost(PID, HID3, MAC3, VLAN200, |
| 171 | getLocation(3), Collections.singleton(IP2)); |
| 172 | private final Host v200host2 = |
| 173 | new DefaultHost(PID, HID4, MAC4, VLAN200, |
| 174 | getLocation(4), Sets.newHashSet()); |
| 175 | private final Host v300host1 = |
| 176 | new DefaultHost(PID, HID5, MAC5, VLAN300, |
| 177 | getLocation(5), Sets.newHashSet()); |
| 178 | private final Host v300host2 = |
| 179 | new DefaultHost(PID, HID6, MAC6, VLAN300, |
| 180 | getLocation(6), Sets.newHashSet()); |
| 181 | private final Host v300host3 = |
| 182 | new DefaultHost(PID, HID7, MAC7, VLAN300, |
| 183 | getLocation(7), Sets.newHashSet()); |
| 184 | |
| 185 | private final Set<Interface> avaliableInterfaces = |
| 186 | ImmutableSet.of(v100h1, v100h2, v200h1, v200h2, v300h1, v300h2); |
| 187 | |
| 188 | private final Set<Host> avaliableHosts = |
| 189 | ImmutableSet.of(v100host1, v100host2, v200host1, |
| 190 | v200host2, v300host1, v300host2, v300host3); |
| 191 | |
| 192 | private ApplicationService applicationService; |
| 193 | private CoreService coreService; |
| 194 | private HostListener hostListener; |
| 195 | private NetworkConfigService configService; |
| 196 | private Set<Host> hostsAvailable; |
| 197 | private HostService hostService; |
| 198 | private IntentService intentService; |
| 199 | private InterfaceService interfaceService; |
| 200 | private VplsConfigurationService vplsConfigService; |
| 201 | private Vpls vpls; |
| 202 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 203 | @Before |
| 204 | public void setUp() throws Exception { |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 205 | idGenerator = new TestIdGenerator(); |
| 206 | Intent.bindIdGenerator(idGenerator); |
| 207 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 208 | applicationService = createMock(ApplicationService.class); |
| 209 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 210 | configService = createMock(NetworkConfigService.class); |
| 211 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 212 | coreService = createMock(CoreService.class); |
| 213 | expect(coreService.registerApplication(APP_NAME)) |
| 214 | .andReturn(APPID); |
| 215 | replay(coreService); |
| 216 | |
| 217 | hostsAvailable = Sets.newHashSet(); |
| 218 | hostService = new TestHostService(hostsAvailable); |
| 219 | |
| 220 | intentService = new TestIntentService(); |
| 221 | |
| 222 | TestIntentSynchronizer intentSynchronizer = |
| 223 | new TestIntentSynchronizer(intentService); |
| 224 | |
| 225 | interfaceService = createMock(InterfaceService.class); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 226 | interfaceService.addListener(anyObject(InterfaceListener.class)); |
| 227 | expectLastCall().anyTimes(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 228 | addIntfConfig(); |
| 229 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 230 | SetMultimap<String, Interface> vplsNetworks = |
| 231 | HashMultimap.create(); |
| 232 | vplsNetworks.put(NET1, v100h1); |
| 233 | vplsNetworks.put(NET1, v200h1); |
| 234 | vplsNetworks.put(NET1, v300h1); |
| 235 | vplsNetworks.put(NET2, v100h2); |
| 236 | vplsNetworks.put(NET2, v200h2); |
| 237 | vplsNetworks.put(NET2, v300h2); |
| 238 | |
| 239 | vplsConfigService = new TestVplsConfigService(vplsNetworks); |
| 240 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 241 | vpls = new Vpls(); |
| 242 | vpls.applicationService = applicationService; |
| 243 | vpls.coreService = coreService; |
| 244 | vpls.hostService = hostService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 245 | vpls.vplsConfigService = vplsConfigService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 246 | vpls.intentService = intentService; |
| 247 | vpls.interfaceService = interfaceService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 248 | vpls.configService = configService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 249 | vpls.intentSynchronizer = intentSynchronizer; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 250 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 253 | @After |
| 254 | public void tearDown() { |
| 255 | Intent.unbindIdGenerator(idGenerator); |
| 256 | } |
| 257 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 258 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 259 | * Creates the interface configuration. On devices 1 and 2 is configured |
| 260 | * an interface on port 1 with vlan 100. On devices 3 and 4 is configured |
| 261 | * an interface on port 1 with vlan 200. On device 5 and 6 is configured |
| 262 | * an interface on port 1 with vlan 300. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 263 | */ |
| 264 | private void addIntfConfig() { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 265 | Set<Interface> interfaces = ImmutableSet.copyOf(avaliableInterfaces); |
| 266 | Set<Interface> vlanOneSet = ImmutableSet.of(v100h1, v100h2); |
| 267 | Set<Interface> vlanTwoSet = ImmutableSet.of(v200h1, v200h2); |
| 268 | Set<Interface> vlanThreeSet = ImmutableSet.of(v300h1, v300h2); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 269 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 270 | avaliableInterfaces.forEach(intf -> { |
| 271 | expect(interfaceService.getInterfacesByPort(intf.connectPoint())) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 272 | .andReturn(Sets.newHashSet(intf)).anyTimes(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 273 | }); |
| 274 | expect(interfaceService.getInterfacesByVlan(VLAN100)) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 275 | .andReturn(vlanOneSet).anyTimes(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 276 | expect(interfaceService.getInterfacesByVlan(VLAN200)) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 277 | .andReturn(vlanTwoSet).anyTimes(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 278 | expect(interfaceService.getInterfacesByVlan(VLAN300)) |
| 279 | .andReturn(vlanThreeSet).anyTimes(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 280 | expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes(); |
| 281 | |
| 282 | replay(interfaceService); |
| 283 | } |
| 284 | |
| 285 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 286 | * Six ports are configured with VLANs but no hosts are registered by the |
| 287 | * HostService. The first three ports have an interface configured on NET1, |
| 288 | * the other three on NET2. The number of intents expected is six: three for |
| 289 | * NET1, three for NET2. Six mp2sp intents. Checks if the number of intents |
| 290 | * submitted to the intent framework is equal to the number of intents |
| 291 | * expected and if all intents are equivalent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 292 | */ |
| 293 | @Test |
| 294 | public void testActivateNoHosts() { |
| 295 | vpls.activate(); |
| 296 | |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 297 | List<Intent> expectedIntents = Lists.newArrayList(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 298 | Set<FilteredConnectPoint> fcPoints; |
| 299 | |
| 300 | fcPoints = buildFCPoints(ImmutableSet.of(v100h1, v200h1, v300h1)); |
| 301 | expectedIntents.addAll(generateVplsBrc(fcPoints, NET1)); |
| 302 | |
| 303 | fcPoints = buildFCPoints(ImmutableSet.of(v100h2, v200h2, v300h2)); |
| 304 | expectedIntents.addAll(generateVplsBrc(fcPoints, NET2)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 305 | |
| 306 | checkIntents(expectedIntents); |
| 307 | } |
| 308 | |
| 309 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 310 | * Six ports are configured with VLANs and six hosts are registered by the |
| 311 | * HostService. The first three ports have an interface configured on NET1, |
| 312 | * the other three on NET2. The number of intents expected is twelve: six |
| 313 | * for VLAN1, six for NET2. six sp2mp intents, six mp2sp intents. For NET1 |
| 314 | * IPs are added to demonstrate this doesn't influence the number of intents |
| 315 | * created. Checks if the number of intents submitted to the intent |
| 316 | * framework is equal to the number of intents expected and if all intents |
| 317 | * are equivalent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 318 | */ |
| 319 | @Test |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 320 | public void testSixInterfacesConfiguredHostsPresent() { |
| 321 | hostsAvailable.addAll(avaliableHosts); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 322 | |
| 323 | vpls.activate(); |
| 324 | |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 325 | List<Intent> expectedIntents = Lists.newArrayList(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 326 | Set<FilteredConnectPoint> fcPoints; |
| 327 | Set<Host> hosts; |
| 328 | |
| 329 | fcPoints = buildFCPoints(ImmutableSet.of(v100h1, v200h1, v300h1)); |
| 330 | hosts = ImmutableSet.of(v100host1, v200host1, v300host1); |
| 331 | expectedIntents.addAll(generateVplsBrc(fcPoints, NET1)); |
| 332 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, NET1)); |
| 333 | |
| 334 | fcPoints = buildFCPoints(ImmutableSet.of(v100h2, v200h2, v300h2)); |
| 335 | hosts = ImmutableSet.of(v100host2, v200host2, v300host2); |
| 336 | expectedIntents.addAll(generateVplsBrc(fcPoints, NET2)); |
| 337 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, NET2)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 338 | |
| 339 | checkIntents(expectedIntents); |
| 340 | } |
| 341 | |
| 342 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 343 | * Six ports are configured with VLANs and initially no hosts are registered |
| 344 | * by the HostService. The first three ports have an interface configured on |
| 345 | * NET1, the other three have an interface configured on NET2. When the |
| 346 | * module starts up, three hosts attached to device one, two and three - |
| 347 | * port 1, are registered by the HostService and events are sent to the |
| 348 | * application. sp2mp intents are created for all interfaces configured and |
| 349 | * mp2sp intents are created only for the hosts attached. |
| 350 | * The number of intents expected is nine: six for NET1, three for NET2. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 351 | * Six sp2mp intents, three mp2sp intents. IPs are added on the first two |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 352 | * hosts only to demonstrate this doesn't influence the number of intents |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 353 | * created. |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 354 | * An additional host is added on device seven - port 1, to demonstrate that |
| 355 | * the application does not generate intents, even if the interface uses the |
| 356 | * same VLAN Id of the other interfaces configured for the specifc VPLS. |
| 357 | * Checks if the number of intents submitted to the intent framework is equal |
| 358 | * to the number of intents expected and if all intents are equivalent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 359 | */ |
| 360 | @Test |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 361 | public void testSixInterfacesThreeHostEventsSameVpls() { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 362 | vpls.activate(); |
| 363 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 364 | List<Intent> expectedIntents = Lists.newArrayList(); |
| 365 | Set<FilteredConnectPoint> fcPoints; |
| 366 | Set<Host> hosts; |
| 367 | |
| 368 | hostsAvailable.addAll(Sets.newHashSet(v100host1, v200host1, v300host1, v300host3)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 369 | |
| 370 | hostsAvailable.forEach(host -> |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 371 | hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host))); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 372 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 373 | fcPoints = buildFCPoints(ImmutableSet.of(v100h1, v200h1, v300h1)); |
| 374 | hosts = ImmutableSet.of(v100host1, v200host1, v300host1); |
| 375 | expectedIntents.addAll(generateVplsBrc(fcPoints, NET1)); |
| 376 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, NET1)); |
| 377 | |
| 378 | fcPoints = buildFCPoints(ImmutableSet.of(v100h2, v200h2, v300h2)); |
| 379 | expectedIntents.addAll(generateVplsBrc(fcPoints, NET2)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 380 | |
| 381 | checkIntents(expectedIntents); |
| 382 | } |
| 383 | |
| 384 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 385 | * Generates a list of the expected sp2mp intents for a VPLS. |
| 386 | * |
| 387 | * @param fcPoints the filtered connect point |
| 388 | * @param name the name of the VPLS |
| 389 | * @return the list of expected sp2mp intents for the given VPLS |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 390 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 391 | private List<SinglePointToMultiPointIntent> |
| 392 | generateVplsBrc(Set<FilteredConnectPoint> fcPoints, String name) { |
| 393 | List<SinglePointToMultiPointIntent> intents = Lists.newArrayList(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 394 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 395 | fcPoints.forEach(point -> { |
| 396 | Set<FilteredConnectPoint> otherPoints = |
| 397 | fcPoints.stream() |
| 398 | .filter(fcp -> !fcp.equals(point)) |
| 399 | .collect(Collectors.toSet()); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 400 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 401 | Key brckey = buildKey(PREFIX_BROADCAST, |
| 402 | point.connectPoint(), name, MacAddress.BROADCAST); |
| 403 | |
| 404 | intents.add(buildBrcIntent(brckey, point, otherPoints)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 405 | }); |
| 406 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 407 | return intents; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 411 | * Generates a list of expected mp2sp intents for a given VPLS. |
| 412 | * |
| 413 | * @param fcPoints the filtered connect point |
| 414 | * @param hosts the hosts |
| 415 | * @param name the name of the VPLS |
| 416 | * @return the list of expected mp2sp intents for the given VPLS |
| 417 | */ |
| 418 | private List<MultiPointToSinglePointIntent> |
| 419 | generateVplsUni(Set<FilteredConnectPoint> fcPoints, Set<Host> hosts, String name) { |
| 420 | List<MultiPointToSinglePointIntent> intents = Lists.newArrayList(); |
| 421 | |
| 422 | hosts.forEach(host -> { |
| 423 | FilteredConnectPoint hostPoint = getHostPoint(host, fcPoints); |
| 424 | |
| 425 | Set<FilteredConnectPoint> otherPoints = |
| 426 | fcPoints.stream() |
| 427 | .filter(fcp -> !fcp.equals(hostPoint)) |
| 428 | .collect(Collectors.toSet()); |
| 429 | |
| 430 | Key uniKey = buildKey(PREFIX_UNICAST, |
| 431 | host.location(), name, host.mac()); |
| 432 | |
| 433 | intents.add(buildUniIntent(uniKey, otherPoints, hostPoint, host)); |
| 434 | }); |
| 435 | |
| 436 | return intents; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Checks if the number of intents submitted to the intent framework is equal |
| 441 | * to the number of intents expected and if all intents are equivalent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 442 | * |
| 443 | * @param intents the list of intents expected |
| 444 | */ |
| 445 | private void checkIntents(List<Intent> intents) { |
| 446 | assertEquals(intents.size(), intentService.getIntentCount()); |
| 447 | |
| 448 | for (Intent intentOne : intents) { |
| 449 | boolean found = false; |
| 450 | for (Intent intentTwo : intentService.getIntents()) { |
| 451 | if (intentOne.key().equals(intentTwo.key())) { |
| 452 | found = true; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 453 | assertTrue(format(COMPARE, intentOne, intentTwo), |
| 454 | IntentUtils.intentsAreEqual(intentOne, intentTwo)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | } |
| 458 | assertTrue(found); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 463 | * Builds a broadcast intent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 464 | * |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 465 | * @param key the key to identify the intent |
| 466 | * @param src the ingress connect point |
| 467 | * @param dsts the egress connect points |
| 468 | * @return the generated single-point to multi-point intent |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 469 | */ |
| 470 | private SinglePointToMultiPointIntent buildBrcIntent(Key key, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 471 | FilteredConnectPoint src, |
| 472 | Set<FilteredConnectPoint> dsts) { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 473 | SinglePointToMultiPointIntent intent; |
| 474 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 475 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 476 | .matchEthDst(MacAddress.BROADCAST) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 477 | .build(); |
| 478 | |
| 479 | intent = SinglePointToMultiPointIntent.builder() |
| 480 | .appId(APPID) |
| 481 | .key(key) |
| 482 | .selector(selector) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 483 | .filteredIngressPoint(src) |
| 484 | .filteredEgressPoints(dsts) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 485 | .priority(PRIORITY_OFFSET) |
| 486 | .build(); |
| 487 | return intent; |
| 488 | } |
| 489 | |
| 490 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 491 | * Builds a unicast intent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 492 | * |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 493 | * @param key the key to identify the intent |
| 494 | * @param srcs the ingress connect points |
| 495 | * @param dst the egress connect point |
| 496 | * @param host the destination Host |
| 497 | * @return the generated multi-point to single-point intent |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 498 | */ |
| 499 | private MultiPointToSinglePointIntent buildUniIntent(Key key, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 500 | Set<FilteredConnectPoint> srcs, |
| 501 | FilteredConnectPoint dst, |
| 502 | Host host) { |
| 503 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 504 | .matchEthDst(host.mac()) |
| 505 | .build(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 506 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 507 | return MultiPointToSinglePointIntent.builder() |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 508 | .appId(APPID) |
| 509 | .key(key) |
| 510 | .selector(selector) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 511 | .filteredIngressPoints(srcs) |
| 512 | .filteredEgressPoint(dst) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 513 | .priority(PRIORITY_OFFSET) |
| 514 | .build(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 515 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 519 | * Returns the filtered connect point associated to a given host. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 520 | * |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 521 | * @param host the target host |
| 522 | * @param fcps the filtered connected points |
| 523 | * @return the filtered connect point associated to the given host; null |
| 524 | * otherwise |
| 525 | */ |
| 526 | private FilteredConnectPoint getHostPoint(Host host, |
| 527 | Set<FilteredConnectPoint> fcps) { |
| 528 | return fcps.stream() |
| 529 | .filter(fcp -> fcp.connectPoint().equals(host.location())) |
| 530 | .filter(fcp -> { |
| 531 | VlanIdCriterion vlanCriterion = |
| 532 | (VlanIdCriterion) fcp.trafficSelector(). |
| 533 | getCriterion(Criterion.Type.VLAN_VID); |
| 534 | |
| 535 | return vlanCriterion != null && |
| 536 | vlanCriterion.vlanId().equals(host.vlan()); |
| 537 | }) |
| 538 | .findFirst() |
| 539 | .orElse(null); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Computes a set of filtered connect points from given interfaces. |
| 544 | * |
| 545 | * @param interfaces the interfaces belonging to the VPLS |
| 546 | * @return the set of filtered connect points |
| 547 | */ |
| 548 | private Set<FilteredConnectPoint> buildFCPoints(Set<Interface> interfaces) { |
| 549 | // Build all filtered connected point in the network |
| 550 | return interfaces |
| 551 | .stream() |
| 552 | .map(intf -> { |
| 553 | TrafficSelector.Builder selectorBuilder = |
| 554 | DefaultTrafficSelector.builder(); |
| 555 | |
| 556 | if (!intf.vlan().equals(VlanId.NONE)) { |
| 557 | selectorBuilder.matchVlanId(intf.vlan()); |
| 558 | } |
| 559 | |
| 560 | return new FilteredConnectPoint(intf.connectPoint(), |
| 561 | selectorBuilder.build()); |
| 562 | }) |
| 563 | .collect(Collectors.toSet()); |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Builds an intent Key either for a single-point to multi-point or |
| 568 | * multi-point to single-point intent, based on a prefix that defines |
| 569 | * the intent type, the connection point representing the source or the |
| 570 | * destination and the VLAN Id representing the network. |
| 571 | * |
| 572 | * @param prefix the key prefix |
| 573 | * @param cPoint the ingress/egress connect point |
| 574 | * @param networkName the VPLS name |
| 575 | * @param hostMac the ingress/egress MAC address |
| 576 | * @return the key to identify the intent |
| 577 | */ |
| 578 | private Key buildKey(String prefix, |
| 579 | ConnectPoint cPoint, |
| 580 | String networkName, |
| 581 | MacAddress hostMac) { |
| 582 | String keyString = networkName + |
| 583 | DASH + |
| 584 | prefix + |
| 585 | DASH + |
| 586 | cPoint.deviceId() + |
| 587 | DASH + |
| 588 | cPoint.port() + |
| 589 | DASH + |
| 590 | hostMac; |
| 591 | |
| 592 | return Key.of(keyString, APPID); |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Returns the device Id of the ith device. |
| 597 | * |
| 598 | * @param i the device to get the Id of |
| 599 | * @return the device Id |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 600 | */ |
| 601 | private static DeviceId getDeviceId(int i) { |
| 602 | return DeviceId.deviceId("" + i); |
| 603 | } |
| 604 | |
| 605 | private static HostLocation getLocation(int i) { |
| 606 | return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L); |
| 607 | } |
| 608 | |
| 609 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 610 | * Represents a fake IntentService class that allows to store and retrieve |
| 611 | * intents without implementing the IntentService logic. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 612 | */ |
| 613 | private class TestIntentService extends IntentServiceAdapter { |
| 614 | |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 615 | private Map<Key, Intent> intents; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 616 | |
| 617 | public TestIntentService() { |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 618 | intents = Maps.newHashMap(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public void submit(Intent intent) { |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 623 | intents.put(intent.key(), intent); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | @Override |
| 627 | public long getIntentCount() { |
| 628 | return intents.size(); |
| 629 | } |
| 630 | |
| 631 | @Override |
| 632 | public Iterable<Intent> getIntents() { |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 633 | return intents.values(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | @Override |
| 637 | public Intent getIntent(Key intentKey) { |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 638 | for (Intent intent : intents.values()) { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 639 | if (intent.key().equals(intentKey)) { |
| 640 | return intent; |
| 641 | } |
| 642 | } |
| 643 | return null; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Represents a fake HostService class which allows to add hosts manually |
| 649 | * in each test, when needed. |
| 650 | */ |
| 651 | private class TestHostService extends HostServiceAdapter { |
| 652 | |
| 653 | private Set<Host> hosts; |
| 654 | |
| 655 | public TestHostService(Set<Host> hosts) { |
| 656 | this.hosts = hosts; |
| 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public void addListener(HostListener listener) { |
| 661 | VplsTest.this.hostListener = listener; |
| 662 | } |
| 663 | |
| 664 | @Override |
| 665 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
| 666 | return hosts.stream() |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 667 | .filter(h -> h.location().equals(connectPoint)) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 668 | .collect(Collectors.toSet()); |
| 669 | } |
| 670 | |
| 671 | } |
| 672 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 673 | /** |
| 674 | * Represents a fake IdGenerator class for intents. |
| 675 | */ |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 676 | private static class TestIdGenerator implements IdGenerator { |
| 677 | |
| 678 | private final AtomicLong id = new AtomicLong(0); |
| 679 | |
| 680 | @Override |
| 681 | public long getNewId() { |
| 682 | return id.getAndIncrement(); |
| 683 | } |
| 684 | |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Test IntentSynchronizer that passes all intents straight through to the |
| 689 | * intent service. |
| 690 | */ |
| 691 | private class TestIntentSynchronizer implements IntentSynchronizationService, |
| 692 | IntentSynchronizationAdminService { |
| 693 | |
| 694 | private final IntentService intentService; |
| 695 | |
| 696 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 697 | * Creates a new intent test synchronizer. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 698 | * |
| 699 | * @param intentService intent service |
| 700 | */ |
| 701 | public TestIntentSynchronizer(IntentService intentService) { |
| 702 | this.intentService = intentService; |
| 703 | } |
| 704 | |
| 705 | @Override |
| 706 | public void submit(Intent intent) { |
| 707 | intentService.submit(intent); |
| 708 | } |
| 709 | |
| 710 | @Override |
| 711 | public void withdraw(Intent intent) { |
| 712 | intentService.withdraw(intent); |
| 713 | } |
| 714 | |
| 715 | @Override |
| 716 | public void modifyPrimary(boolean isPrimary) { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | @Override |
| 720 | public void removeIntents() { |
Luca Prete | 2705d66 | 2016-04-29 15:30:23 -0700 | [diff] [blame] | 721 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 722 | |
Luca Prete | 2705d66 | 2016-04-29 15:30:23 -0700 | [diff] [blame] | 723 | @Override |
| 724 | public void removeIntentsByAppId(ApplicationId applicationId) { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 728 | /** |
| 729 | * Represents a fake VplsConfigService class which is needed for testing. |
| 730 | */ |
| 731 | private class TestVplsConfigService implements VplsConfigurationService { |
| 732 | |
| 733 | private final SetMultimap<String, Interface> vplsNetworks; |
| 734 | private Set<String> vplsAffectByApi = new HashSet<>(); |
| 735 | |
| 736 | TestVplsConfigService(SetMultimap<String, Interface> vplsNetworks) { |
| 737 | this.vplsNetworks = vplsNetworks; |
| 738 | } |
| 739 | |
| 740 | @Override |
| 741 | public void addVpls(String name, Set<String> ifaces) { |
| 742 | if (!vplsNetworks.containsKey(name)) { |
| 743 | ifaces.forEach(iface -> { |
| 744 | avaliableInterfaces.forEach(intf -> { |
| 745 | if (intf.name().equals(iface)) { |
| 746 | vplsNetworks.put(name, intf); |
| 747 | } |
| 748 | }); |
| 749 | }); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | @Override |
| 754 | public void removeVpls(String name) { |
| 755 | if (vplsNetworks.containsKey(name)) { |
| 756 | vplsNetworks.removeAll(name); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | @Override |
| 761 | public void addInterfaceToVpls(String name, String iface) { |
| 762 | if (!vplsNetworks.containsKey(name)) { |
| 763 | avaliableInterfaces.forEach(intf -> { |
| 764 | if (intf.name().equals(iface)) { |
| 765 | vplsNetworks.put(name, intf); |
| 766 | } |
| 767 | }); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | @Override |
| 772 | public void removeInterfaceFromVpls(String iface) { |
| 773 | SetMultimap<String, Interface> search = HashMultimap.create(vplsNetworks); |
| 774 | search.entries().forEach(e -> { |
| 775 | if (e.getValue().name().equals(iface)) { |
| 776 | vplsNetworks.remove(e.getKey(), iface); |
| 777 | } |
| 778 | }); |
| 779 | } |
| 780 | |
| 781 | @Override |
| 782 | public void cleanVpls() { |
| 783 | vplsNetworks.clear(); |
| 784 | } |
| 785 | |
| 786 | @Override |
| 787 | public Set<String> getVplsAffectedByApi() { |
| 788 | Set<String> vplsNames = ImmutableSet.copyOf(vplsAffectByApi); |
| 789 | |
| 790 | vplsAffectByApi.clear(); |
| 791 | |
| 792 | return vplsNames; |
| 793 | } |
| 794 | |
| 795 | @Override |
| 796 | public Set<Interface> getAllInterfaces() { |
| 797 | return vplsNetworks.values() |
| 798 | .stream() |
| 799 | .collect(Collectors.toSet()); |
| 800 | } |
| 801 | |
| 802 | @Override |
| 803 | public Set<Interface> getVplsInterfaces(String name) { |
| 804 | return vplsNetworks.get(name) |
| 805 | .stream() |
| 806 | .collect(Collectors.toSet()); |
| 807 | } |
| 808 | |
| 809 | @Override |
| 810 | public Set<String> getAllVpls() { |
| 811 | return vplsNetworks.keySet(); |
| 812 | } |
| 813 | |
| 814 | @Override |
| 815 | public Set<String> getOldVpls() { |
| 816 | return vplsNetworks.keySet(); |
| 817 | } |
| 818 | |
| 819 | @Override |
| 820 | public SetMultimap<String, Interface> getVplsNetworks() { |
| 821 | return ImmutableSetMultimap.copyOf(vplsNetworks); |
| 822 | } |
| 823 | |
| 824 | @Override |
| 825 | public SetMultimap<String, Interface> getVplsNetwork(VlanId vlan, |
| 826 | ConnectPoint connectPoint) { |
| 827 | String vplsNetworkName = |
| 828 | vplsNetworks.entries().stream() |
| 829 | .filter(e -> e.getValue().connectPoint().equals(connectPoint)) |
| 830 | .filter(e -> e.getValue().vlan().equals(vlan)) |
| 831 | .map(e -> e.getKey()) |
| 832 | .findFirst() |
| 833 | .orElse(null); |
| 834 | SetMultimap<String, Interface> result = HashMultimap.create(); |
| 835 | if (vplsNetworkName != null && vplsNetworks.containsKey(vplsNetworkName)) { |
| 836 | vplsNetworks.get(vplsNetworkName) |
| 837 | .forEach(intf -> result.put(vplsNetworkName, intf)); |
| 838 | return result; |
| 839 | } |
| 840 | return null; |
| 841 | } |
| 842 | |
| 843 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 844 | } |