blob: 178e1a85f6e14d89ff9f114350ab84717e77208f [file] [log] [blame]
Luca Prete9c2ee072016-02-16 11:00:44 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Luca Prete9c2ee072016-02-16 11:00:44 -08003 *
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 */
16package org.onosproject.vpls;
17
Ray Milkey6e968eb2016-06-29 09:37:27 -070018import java.util.Collections;
nosignal5fd282e2016-09-16 16:11:40 -070019import java.util.HashSet;
Ray Milkey6e968eb2016-06-29 09:37:27 -070020import java.util.List;
Luca Pretec21c6e62016-09-22 14:52:21 -070021import java.util.Map;
Ray Milkey6e968eb2016-06-29 09:37:27 -070022import java.util.Set;
23import java.util.concurrent.atomic.AtomicLong;
24import java.util.stream.Collectors;
25
nosignal5fd282e2016-09-16 16:11:40 -070026import com.google.common.collect.Sets;
27import com.google.common.collect.ImmutableSet;
28import com.google.common.collect.SetMultimap;
29import com.google.common.collect.HashMultimap;
Luca Pretec21c6e62016-09-22 14:52:21 -070030import com.google.common.collect.Maps;
nosignal5fd282e2016-09-16 16:11:40 -070031import com.google.common.collect.Lists;
32import com.google.common.collect.ImmutableSetMultimap;
Ray Milkey6e968eb2016-06-29 09:37:27 -070033import org.junit.After;
Luca Prete9c2ee072016-02-16 11:00:44 -080034import org.junit.Before;
Luca Prete9c2ee072016-02-16 11:00:44 -080035import org.junit.Test;
36import org.onlab.packet.Ip4Address;
37import org.onlab.packet.MacAddress;
38import org.onlab.packet.VlanId;
39import org.onosproject.TestApplicationId;
40import org.onosproject.app.ApplicationService;
41import org.onosproject.core.ApplicationId;
42import org.onosproject.core.CoreService;
43import org.onosproject.core.IdGenerator;
44import org.onosproject.incubator.net.intf.Interface;
Luca Pretea8854822016-04-26 16:30:55 -070045import org.onosproject.incubator.net.intf.InterfaceListener;
Luca Prete9c2ee072016-02-16 11:00:44 -080046import org.onosproject.incubator.net.intf.InterfaceService;
47import org.onosproject.net.ConnectPoint;
48import org.onosproject.net.DefaultHost;
49import org.onosproject.net.DeviceId;
50import org.onosproject.net.Host;
51import org.onosproject.net.HostId;
52import org.onosproject.net.HostLocation;
53import org.onosproject.net.PortNumber;
nosignal5fd282e2016-09-16 16:11:40 -070054import org.onosproject.net.FilteredConnectPoint;
55import org.onosproject.net.config.NetworkConfigService;
Luca Prete9c2ee072016-02-16 11:00:44 -080056import org.onosproject.net.flow.DefaultTrafficSelector;
Luca Prete9c2ee072016-02-16 11:00:44 -080057import org.onosproject.net.flow.TrafficSelector;
nosignal5fd282e2016-09-16 16:11:40 -070058import org.onosproject.net.flow.criteria.Criterion;
59import org.onosproject.net.flow.criteria.VlanIdCriterion;
Luca Prete9c2ee072016-02-16 11:00:44 -080060import org.onosproject.net.host.HostEvent;
61import org.onosproject.net.host.HostListener;
62import org.onosproject.net.host.HostService;
63import org.onosproject.net.host.HostServiceAdapter;
64import org.onosproject.net.intent.Intent;
65import org.onosproject.net.intent.IntentService;
66import org.onosproject.net.intent.IntentServiceAdapter;
67import org.onosproject.net.intent.IntentUtils;
68import org.onosproject.net.intent.Key;
69import org.onosproject.net.intent.MultiPointToSinglePointIntent;
70import org.onosproject.net.intent.SinglePointToMultiPointIntent;
71import org.onosproject.net.provider.ProviderId;
72import org.onosproject.routing.IntentSynchronizationAdminService;
73import org.onosproject.routing.IntentSynchronizationService;
nosignal5fd282e2016-09-16 16:11:40 -070074import org.onosproject.vpls.config.VplsConfigurationService;
Luca Prete9c2ee072016-02-16 11:00:44 -080075
76import static java.lang.String.format;
Ray Milkey6e968eb2016-06-29 09:37:27 -070077import static org.easymock.EasyMock.anyObject;
78import static org.easymock.EasyMock.createMock;
79import static org.easymock.EasyMock.expect;
Luca Pretea8854822016-04-26 16:30:55 -070080import static org.easymock.EasyMock.expectLastCall;
Ray Milkey6e968eb2016-06-29 09:37:27 -070081import static org.easymock.EasyMock.replay;
Luca Prete9c2ee072016-02-16 11:00:44 -080082import static org.junit.Assert.assertEquals;
83import static org.junit.Assert.assertTrue;
84
nosignal5fd282e2016-09-16 16:11:40 -070085import static org.onosproject.vpls.IntentInstaller.PREFIX_BROADCAST;
86import static org.onosproject.vpls.IntentInstaller.PREFIX_UNICAST;
87
Luca Prete9c2ee072016-02-16 11:00:44 -080088/**
89 * Tests for the {@link Vpls} class.
90 */
91public class VplsTest {
nosignal5fd282e2016-09-16 16:11:40 -070092 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 Prete9c2ee072016-02-16 11:00:44 -080099
100 private static final PortNumber P1 = PortNumber.portNumber(1);
101
Luca Prete9c2ee072016-02-16 11:00:44 -0800102 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
nosignal5fd282e2016-09-16 16:11:40 -0700109 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 Prete9c2ee072016-02-16 11:00:44 -0800115
nosignal5fd282e2016-09-16 16:11:40 -0700116 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 Prete9c2ee072016-02-16 11:00:44 -0800119
nosignal5fd282e2016-09-16 16:11:40 -0700120 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 Prete9c2ee072016-02-16 11:00:44 -0800134
nosignal5fd282e2016-09-16 16:11:40 -0700135 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 Prete9c2ee072016-02-16 11:00:44 -0800145
146 private static final ProviderId PID = new ProviderId("of", "foo");
147
Ray Milkey6e968eb2016-06-29 09:37:27 -0700148 private static IdGenerator idGenerator;
Luca Prete9c2ee072016-02-16 11:00:44 -0800149
nosignal5fd282e2016-09-16 16:11:40 -0700150 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 Prete9c2ee072016-02-16 11:00:44 -0800203 @Before
204 public void setUp() throws Exception {
Ray Milkey6e968eb2016-06-29 09:37:27 -0700205 idGenerator = new TestIdGenerator();
206 Intent.bindIdGenerator(idGenerator);
207
Luca Prete9c2ee072016-02-16 11:00:44 -0800208 applicationService = createMock(ApplicationService.class);
209
nosignal5fd282e2016-09-16 16:11:40 -0700210 configService = createMock(NetworkConfigService.class);
211
Luca Prete9c2ee072016-02-16 11:00:44 -0800212 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 Pretea8854822016-04-26 16:30:55 -0700226 interfaceService.addListener(anyObject(InterfaceListener.class));
227 expectLastCall().anyTimes();
Luca Prete9c2ee072016-02-16 11:00:44 -0800228 addIntfConfig();
229
nosignal5fd282e2016-09-16 16:11:40 -0700230 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 Prete9c2ee072016-02-16 11:00:44 -0800241 vpls = new Vpls();
242 vpls.applicationService = applicationService;
243 vpls.coreService = coreService;
244 vpls.hostService = hostService;
nosignal5fd282e2016-09-16 16:11:40 -0700245 vpls.vplsConfigService = vplsConfigService;
Luca Prete9c2ee072016-02-16 11:00:44 -0800246 vpls.intentService = intentService;
247 vpls.interfaceService = interfaceService;
nosignal5fd282e2016-09-16 16:11:40 -0700248 vpls.configService = configService;
Luca Prete9c2ee072016-02-16 11:00:44 -0800249 vpls.intentSynchronizer = intentSynchronizer;
Yi Tseng28767f02016-09-13 04:27:20 -0700250
Luca Prete9c2ee072016-02-16 11:00:44 -0800251 }
252
Ray Milkey6e968eb2016-06-29 09:37:27 -0700253 @After
254 public void tearDown() {
255 Intent.unbindIdGenerator(idGenerator);
256 }
257
Luca Prete9c2ee072016-02-16 11:00:44 -0800258 /**
nosignal5fd282e2016-09-16 16:11:40 -0700259 * 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 Prete9c2ee072016-02-16 11:00:44 -0800263 */
264 private void addIntfConfig() {
nosignal5fd282e2016-09-16 16:11:40 -0700265 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 Prete9c2ee072016-02-16 11:00:44 -0800269
nosignal5fd282e2016-09-16 16:11:40 -0700270 avaliableInterfaces.forEach(intf -> {
271 expect(interfaceService.getInterfacesByPort(intf.connectPoint()))
Luca Prete9c2ee072016-02-16 11:00:44 -0800272 .andReturn(Sets.newHashSet(intf)).anyTimes();
nosignal5fd282e2016-09-16 16:11:40 -0700273 });
274 expect(interfaceService.getInterfacesByVlan(VLAN100))
Luca Prete9c2ee072016-02-16 11:00:44 -0800275 .andReturn(vlanOneSet).anyTimes();
nosignal5fd282e2016-09-16 16:11:40 -0700276 expect(interfaceService.getInterfacesByVlan(VLAN200))
Luca Prete9c2ee072016-02-16 11:00:44 -0800277 .andReturn(vlanTwoSet).anyTimes();
nosignal5fd282e2016-09-16 16:11:40 -0700278 expect(interfaceService.getInterfacesByVlan(VLAN300))
279 .andReturn(vlanThreeSet).anyTimes();
Luca Prete9c2ee072016-02-16 11:00:44 -0800280 expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
281
282 replay(interfaceService);
283 }
284
285 /**
nosignal5fd282e2016-09-16 16:11:40 -0700286 * 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 Prete9c2ee072016-02-16 11:00:44 -0800292 */
293 @Test
294 public void testActivateNoHosts() {
295 vpls.activate();
296
Yi Tseng28767f02016-09-13 04:27:20 -0700297 List<Intent> expectedIntents = Lists.newArrayList();
nosignal5fd282e2016-09-16 16:11:40 -0700298 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 Prete9c2ee072016-02-16 11:00:44 -0800305
306 checkIntents(expectedIntents);
307 }
308
309 /**
nosignal5fd282e2016-09-16 16:11:40 -0700310 * 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 Prete9c2ee072016-02-16 11:00:44 -0800318 */
319 @Test
nosignal5fd282e2016-09-16 16:11:40 -0700320 public void testSixInterfacesConfiguredHostsPresent() {
321 hostsAvailable.addAll(avaliableHosts);
Luca Prete9c2ee072016-02-16 11:00:44 -0800322
323 vpls.activate();
324
Yi Tseng28767f02016-09-13 04:27:20 -0700325 List<Intent> expectedIntents = Lists.newArrayList();
nosignal5fd282e2016-09-16 16:11:40 -0700326 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 Prete9c2ee072016-02-16 11:00:44 -0800338
339 checkIntents(expectedIntents);
340 }
341
342 /**
nosignal5fd282e2016-09-16 16:11:40 -0700343 * 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 Prete9c2ee072016-02-16 11:00:44 -0800351 * Six sp2mp intents, three mp2sp intents. IPs are added on the first two
nosignal5fd282e2016-09-16 16:11:40 -0700352 * hosts only to demonstrate this doesn't influence the number of intents
Luca Prete9c2ee072016-02-16 11:00:44 -0800353 * created.
nosignal5fd282e2016-09-16 16:11:40 -0700354 * 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 Prete9c2ee072016-02-16 11:00:44 -0800359 */
360 @Test
nosignal5fd282e2016-09-16 16:11:40 -0700361 public void testSixInterfacesThreeHostEventsSameVpls() {
Luca Prete9c2ee072016-02-16 11:00:44 -0800362 vpls.activate();
363
nosignal5fd282e2016-09-16 16:11:40 -0700364 List<Intent> expectedIntents = Lists.newArrayList();
365 Set<FilteredConnectPoint> fcPoints;
366 Set<Host> hosts;
367
368 hostsAvailable.addAll(Sets.newHashSet(v100host1, v200host1, v300host1, v300host3));
Luca Prete9c2ee072016-02-16 11:00:44 -0800369
370 hostsAvailable.forEach(host ->
nosignal5fd282e2016-09-16 16:11:40 -0700371 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host)));
Luca Prete9c2ee072016-02-16 11:00:44 -0800372
nosignal5fd282e2016-09-16 16:11:40 -0700373 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 Prete9c2ee072016-02-16 11:00:44 -0800380
381 checkIntents(expectedIntents);
382 }
383
384 /**
nosignal5fd282e2016-09-16 16:11:40 -0700385 * 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 Prete9c2ee072016-02-16 11:00:44 -0800390 */
nosignal5fd282e2016-09-16 16:11:40 -0700391 private List<SinglePointToMultiPointIntent>
392 generateVplsBrc(Set<FilteredConnectPoint> fcPoints, String name) {
393 List<SinglePointToMultiPointIntent> intents = Lists.newArrayList();
Luca Prete9c2ee072016-02-16 11:00:44 -0800394
nosignal5fd282e2016-09-16 16:11:40 -0700395 fcPoints.forEach(point -> {
396 Set<FilteredConnectPoint> otherPoints =
397 fcPoints.stream()
398 .filter(fcp -> !fcp.equals(point))
399 .collect(Collectors.toSet());
Luca Prete9c2ee072016-02-16 11:00:44 -0800400
nosignal5fd282e2016-09-16 16:11:40 -0700401 Key brckey = buildKey(PREFIX_BROADCAST,
402 point.connectPoint(), name, MacAddress.BROADCAST);
403
404 intents.add(buildBrcIntent(brckey, point, otherPoints));
Luca Prete9c2ee072016-02-16 11:00:44 -0800405 });
406
nosignal5fd282e2016-09-16 16:11:40 -0700407 return intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800408 }
409
410 /**
nosignal5fd282e2016-09-16 16:11:40 -0700411 * 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 Prete9c2ee072016-02-16 11:00:44 -0800442 *
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;
nosignal5fd282e2016-09-16 16:11:40 -0700453 assertTrue(format(COMPARE, intentOne, intentTwo),
454 IntentUtils.intentsAreEqual(intentOne, intentTwo));
Luca Prete9c2ee072016-02-16 11:00:44 -0800455 break;
456 }
457 }
458 assertTrue(found);
459 }
460 }
461
462 /**
nosignal5fd282e2016-09-16 16:11:40 -0700463 * Builds a broadcast intent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800464 *
nosignal5fd282e2016-09-16 16:11:40 -0700465 * @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 Prete9c2ee072016-02-16 11:00:44 -0800469 */
470 private SinglePointToMultiPointIntent buildBrcIntent(Key key,
nosignal5fd282e2016-09-16 16:11:40 -0700471 FilteredConnectPoint src,
472 Set<FilteredConnectPoint> dsts) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800473 SinglePointToMultiPointIntent intent;
474
Luca Prete9c2ee072016-02-16 11:00:44 -0800475 TrafficSelector selector = DefaultTrafficSelector.builder()
476 .matchEthDst(MacAddress.BROADCAST)
Luca Prete9c2ee072016-02-16 11:00:44 -0800477 .build();
478
479 intent = SinglePointToMultiPointIntent.builder()
480 .appId(APPID)
481 .key(key)
482 .selector(selector)
nosignal5fd282e2016-09-16 16:11:40 -0700483 .filteredIngressPoint(src)
484 .filteredEgressPoints(dsts)
Luca Prete9c2ee072016-02-16 11:00:44 -0800485 .priority(PRIORITY_OFFSET)
486 .build();
487 return intent;
488 }
489
490 /**
nosignal5fd282e2016-09-16 16:11:40 -0700491 * Builds a unicast intent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800492 *
nosignal5fd282e2016-09-16 16:11:40 -0700493 * @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 Prete9c2ee072016-02-16 11:00:44 -0800498 */
499 private MultiPointToSinglePointIntent buildUniIntent(Key key,
nosignal5fd282e2016-09-16 16:11:40 -0700500 Set<FilteredConnectPoint> srcs,
501 FilteredConnectPoint dst,
502 Host host) {
503 TrafficSelector selector = DefaultTrafficSelector.builder()
504 .matchEthDst(host.mac())
505 .build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800506
nosignal5fd282e2016-09-16 16:11:40 -0700507 return MultiPointToSinglePointIntent.builder()
Luca Prete9c2ee072016-02-16 11:00:44 -0800508 .appId(APPID)
509 .key(key)
510 .selector(selector)
nosignal5fd282e2016-09-16 16:11:40 -0700511 .filteredIngressPoints(srcs)
512 .filteredEgressPoint(dst)
Luca Prete9c2ee072016-02-16 11:00:44 -0800513 .priority(PRIORITY_OFFSET)
514 .build();
nosignal5fd282e2016-09-16 16:11:40 -0700515
Luca Prete9c2ee072016-02-16 11:00:44 -0800516 }
517
518 /**
nosignal5fd282e2016-09-16 16:11:40 -0700519 * Returns the filtered connect point associated to a given host.
Luca Prete9c2ee072016-02-16 11:00:44 -0800520 *
nosignal5fd282e2016-09-16 16:11:40 -0700521 * @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 Prete9c2ee072016-02-16 11:00:44 -0800600 */
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 /**
nosignal5fd282e2016-09-16 16:11:40 -0700610 * Represents a fake IntentService class that allows to store and retrieve
611 * intents without implementing the IntentService logic.
Luca Prete9c2ee072016-02-16 11:00:44 -0800612 */
613 private class TestIntentService extends IntentServiceAdapter {
614
Luca Pretec21c6e62016-09-22 14:52:21 -0700615 private Map<Key, Intent> intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800616
617 public TestIntentService() {
Luca Pretec21c6e62016-09-22 14:52:21 -0700618 intents = Maps.newHashMap();
Luca Prete9c2ee072016-02-16 11:00:44 -0800619 }
620
621 @Override
622 public void submit(Intent intent) {
Luca Pretec21c6e62016-09-22 14:52:21 -0700623 intents.put(intent.key(), intent);
Luca Prete9c2ee072016-02-16 11:00:44 -0800624 }
625
626 @Override
627 public long getIntentCount() {
628 return intents.size();
629 }
630
631 @Override
632 public Iterable<Intent> getIntents() {
Luca Pretec21c6e62016-09-22 14:52:21 -0700633 return intents.values();
Luca Prete9c2ee072016-02-16 11:00:44 -0800634 }
635
636 @Override
637 public Intent getIntent(Key intentKey) {
Luca Pretec21c6e62016-09-22 14:52:21 -0700638 for (Intent intent : intents.values()) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800639 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()
nosignal5fd282e2016-09-16 16:11:40 -0700667 .filter(h -> h.location().equals(connectPoint))
Luca Prete9c2ee072016-02-16 11:00:44 -0800668 .collect(Collectors.toSet());
669 }
670
671 }
672
nosignal5fd282e2016-09-16 16:11:40 -0700673 /**
674 * Represents a fake IdGenerator class for intents.
675 */
Luca Prete9c2ee072016-02-16 11:00:44 -0800676 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 /**
nosignal5fd282e2016-09-16 16:11:40 -0700697 * Creates a new intent test synchronizer.
Luca Prete9c2ee072016-02-16 11:00:44 -0800698 *
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 Prete9c2ee072016-02-16 11:00:44 -0800717 }
718
719 @Override
720 public void removeIntents() {
Luca Prete2705d662016-04-29 15:30:23 -0700721 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800722
Luca Prete2705d662016-04-29 15:30:23 -0700723 @Override
724 public void removeIntentsByAppId(ApplicationId applicationId) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800725 }
726 }
727
nosignal5fd282e2016-09-16 16:11:40 -0700728 /**
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 Prete9c2ee072016-02-16 11:00:44 -0800844}