blob: 6dfcea5977f1e5180a1ae03bb6b7c35ec99a9cd7 [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
nosignal5fd282e2016-09-16 16:11:40 -070018import com.google.common.collect.HashMultimap;
Luca Prete13465642017-01-04 10:31:48 -080019import com.google.common.collect.ImmutableSet;
nosignal5fd282e2016-09-16 16:11:40 -070020import com.google.common.collect.ImmutableSetMultimap;
Luca Prete13465642017-01-04 10:31:48 -080021import com.google.common.collect.Lists;
22import com.google.common.collect.Maps;
23import com.google.common.collect.SetMultimap;
24import com.google.common.collect.Sets;
Ray Milkey6e968eb2016-06-29 09:37:27 -070025import org.junit.After;
Luca Prete9c2ee072016-02-16 11:00:44 -080026import org.junit.Before;
Luca Prete9c2ee072016-02-16 11:00:44 -080027import org.junit.Test;
28import org.onlab.packet.Ip4Address;
29import org.onlab.packet.MacAddress;
30import org.onlab.packet.VlanId;
31import org.onosproject.TestApplicationId;
32import org.onosproject.app.ApplicationService;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
35import org.onosproject.core.IdGenerator;
36import org.onosproject.incubator.net.intf.Interface;
Luca Pretea8854822016-04-26 16:30:55 -070037import org.onosproject.incubator.net.intf.InterfaceListener;
Luca Prete9c2ee072016-02-16 11:00:44 -080038import org.onosproject.incubator.net.intf.InterfaceService;
Jonathan Hart470ed4f2017-01-31 16:52:28 -080039import org.onosproject.intentsync.IntentSynchronizationService;
Luca Prete9c2ee072016-02-16 11:00:44 -080040import org.onosproject.net.ConnectPoint;
41import org.onosproject.net.DefaultHost;
42import org.onosproject.net.DeviceId;
Luca Prete092e8952016-10-26 16:25:56 +020043import org.onosproject.net.EncapsulationType;
Luca Prete13465642017-01-04 10:31:48 -080044import org.onosproject.net.FilteredConnectPoint;
Luca Prete9c2ee072016-02-16 11:00:44 -080045import org.onosproject.net.Host;
46import org.onosproject.net.HostId;
47import org.onosproject.net.HostLocation;
48import org.onosproject.net.PortNumber;
nosignal5fd282e2016-09-16 16:11:40 -070049import org.onosproject.net.config.NetworkConfigService;
Luca Prete9c2ee072016-02-16 11:00:44 -080050import org.onosproject.net.flow.DefaultTrafficSelector;
Luca Prete9c2ee072016-02-16 11:00:44 -080051import org.onosproject.net.flow.TrafficSelector;
nosignal5fd282e2016-09-16 16:11:40 -070052import org.onosproject.net.flow.criteria.Criterion;
53import org.onosproject.net.flow.criteria.VlanIdCriterion;
Luca Prete9c2ee072016-02-16 11:00:44 -080054import org.onosproject.net.host.HostEvent;
55import org.onosproject.net.host.HostListener;
56import org.onosproject.net.host.HostService;
57import org.onosproject.net.host.HostServiceAdapter;
58import org.onosproject.net.intent.Intent;
59import org.onosproject.net.intent.IntentService;
60import org.onosproject.net.intent.IntentServiceAdapter;
61import org.onosproject.net.intent.IntentUtils;
62import org.onosproject.net.intent.Key;
63import org.onosproject.net.intent.MultiPointToSinglePointIntent;
64import org.onosproject.net.intent.SinglePointToMultiPointIntent;
65import org.onosproject.net.provider.ProviderId;
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +010066import org.onosproject.vpls.config.VplsConfigService;
Luca Prete9c2ee072016-02-16 11:00:44 -080067
Luca Prete13465642017-01-04 10:31:48 -080068import java.util.Collection;
69import java.util.Collections;
70import java.util.HashMap;
71import java.util.HashSet;
72import java.util.List;
73import java.util.Map;
74import java.util.Set;
75import java.util.concurrent.atomic.AtomicLong;
76import java.util.stream.Collectors;
77
Luca Prete9c2ee072016-02-16 11:00:44 -080078import static java.lang.String.format;
Jonathan Hart470ed4f2017-01-31 16:52:28 -080079import static org.easymock.EasyMock.anyObject;
80import static org.easymock.EasyMock.createMock;
81import static org.easymock.EasyMock.expect;
82import static org.easymock.EasyMock.expectLastCall;
83import static org.easymock.EasyMock.replay;
Luca Prete9c2ee072016-02-16 11:00:44 -080084import static org.junit.Assert.assertEquals;
85import static org.junit.Assert.assertTrue;
Jonathan Hart470ed4f2017-01-31 16:52:28 -080086import static org.onosproject.net.EncapsulationType.NONE;
87import static org.onosproject.net.EncapsulationType.VLAN;
88import static org.onosproject.net.EncapsulationType.valueOf;
89import static org.onosproject.vpls.IntentInstaller.PARTIAL_FAILURE_CONSTRAINT;
90import static org.onosproject.vpls.IntentInstaller.PREFIX_BROADCAST;
91import static org.onosproject.vpls.IntentInstaller.PREFIX_UNICAST;
92import static org.onosproject.vpls.IntentInstaller.setEncap;
nosignal5fd282e2016-09-16 16:11:40 -070093
Luca Prete9c2ee072016-02-16 11:00:44 -080094/**
95 * Tests for the {@link Vpls} class.
96 */
97public class VplsTest {
nosignal5fd282e2016-09-16 16:11:40 -070098 private static final String APP_NAME = "org.onosproject.vpls";
99 private static final ApplicationId APPID = TestApplicationId.create(APP_NAME);
100 private static final String DASH = "-";
101 private static final int PRIORITY_OFFSET = 1000;
Luca Prete092e8952016-10-26 16:25:56 +0200102 private static final String VPLS1 = "vpls1";
103 private static final String VPLS2 = "vpls2";
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100104 private static final String VPLS3 = "vpls3";
105 private static final String VPLS4 = "vpls4";
Luca Prete9c2ee072016-02-16 11:00:44 -0800106
107 private static final PortNumber P1 = PortNumber.portNumber(1);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100108 private static final PortNumber P2 = PortNumber.portNumber(2);
Luca Prete9c2ee072016-02-16 11:00:44 -0800109
Luca Prete9c2ee072016-02-16 11:00:44 -0800110 private static final DeviceId DID1 = getDeviceId(1);
111 private static final DeviceId DID2 = getDeviceId(2);
112 private static final DeviceId DID3 = getDeviceId(3);
113 private static final DeviceId DID4 = getDeviceId(4);
114 private static final DeviceId DID5 = getDeviceId(5);
115 private static final DeviceId DID6 = getDeviceId(6);
116
nosignal5fd282e2016-09-16 16:11:40 -0700117 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
118 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P1);
119 private static final ConnectPoint CP3 = new ConnectPoint(DID3, P1);
120 private static final ConnectPoint CP4 = new ConnectPoint(DID4, P1);
121 private static final ConnectPoint CP5 = new ConnectPoint(DID5, P1);
122 private static final ConnectPoint CP6 = new ConnectPoint(DID6, P1);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100123 private static final ConnectPoint CP7 = new ConnectPoint(DID4, P2);
124 private static final ConnectPoint CP8 = new ConnectPoint(DID3, P2);
125 private static final ConnectPoint CP9 = new ConnectPoint(DID5, P1);
126 private static final ConnectPoint CP10 = new ConnectPoint(DID5, P2);
Luca Prete9c2ee072016-02-16 11:00:44 -0800127
nosignal5fd282e2016-09-16 16:11:40 -0700128 private static final VlanId VLAN100 = VlanId.vlanId((short) 100);
129 private static final VlanId VLAN200 = VlanId.vlanId((short) 200);
130 private static final VlanId VLAN300 = VlanId.vlanId((short) 300);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100131 private static final VlanId VLAN400 = VlanId.vlanId((short) 400);
132 private static final VlanId VLAN_NONE = VlanId.NONE;
Luca Prete9c2ee072016-02-16 11:00:44 -0800133
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100134 private static final MacAddress MAC1 = getMac(1);
135 private static final MacAddress MAC2 = getMac(2);
136 private static final MacAddress MAC3 = getMac(3);
137 private static final MacAddress MAC4 = getMac(4);
138 private static final MacAddress MAC5 = getMac(5);
139 private static final MacAddress MAC6 = getMac(6);
140 private static final MacAddress MAC7 = getMac(7);
141 private static final MacAddress MAC8 = getMac(8);
142 private static final MacAddress MAC9 = getMac(9);
143 private static final MacAddress MAC10 = getMac(10);
144 private static final MacAddress MAC11 = getMac(11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800145
nosignal5fd282e2016-09-16 16:11:40 -0700146 private static final Ip4Address IP1 = Ip4Address.valueOf("192.168.1.1");
147 private static final Ip4Address IP2 = Ip4Address.valueOf("192.168.1.2");
148
149 private static final HostId HID1 = HostId.hostId(MAC1, VLAN100);
150 private static final HostId HID2 = HostId.hostId(MAC2, VLAN100);
151 private static final HostId HID3 = HostId.hostId(MAC3, VLAN200);
152 private static final HostId HID4 = HostId.hostId(MAC4, VLAN200);
153 private static final HostId HID5 = HostId.hostId(MAC5, VLAN300);
154 private static final HostId HID6 = HostId.hostId(MAC6, VLAN300);
155 private static final HostId HID7 = HostId.hostId(MAC7, VLAN300);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100156 private static final HostId HID8 = HostId.hostId(MAC8, VLAN400);
157 private static final HostId HID9 = HostId.hostId(MAC9);
158 private static final HostId HID10 = HostId.hostId(MAC10);
159 private static final HostId HID11 = HostId.hostId(MAC11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800160
161 private static final ProviderId PID = new ProviderId("of", "foo");
162
Ray Milkey6e968eb2016-06-29 09:37:27 -0700163 private static IdGenerator idGenerator;
Luca Prete9c2ee072016-02-16 11:00:44 -0800164
Luca Prete092e8952016-10-26 16:25:56 +0200165 private static final Interface V100H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700166 new Interface("v100h1", CP1, null, null, VLAN100);
Luca Prete092e8952016-10-26 16:25:56 +0200167 private static final Interface V100H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700168 new Interface("v100h2", CP2, null, null, VLAN100);
Luca Prete092e8952016-10-26 16:25:56 +0200169 private static final Interface V200H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700170 new Interface("v200h1", CP3, null, null, VLAN200);
Luca Prete092e8952016-10-26 16:25:56 +0200171 private static final Interface V200H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700172 new Interface("v200h2", CP4, null, null, VLAN200);
Luca Prete092e8952016-10-26 16:25:56 +0200173 private static final Interface V300H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700174 new Interface("v300h1", CP5, null, null, VLAN300);
Luca Prete092e8952016-10-26 16:25:56 +0200175 private static final Interface V300H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700176 new Interface("v300h2", CP6, null, null, VLAN300);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100177 private static final Interface V400H1 =
178 new Interface("v400h1", CP7, null, null, VLAN400);
179
180 private static final Interface VNONEH1 =
181 new Interface("vNoneh1", CP8, null, null, VLAN_NONE);
182 private static final Interface VNONEH2 =
183 new Interface("vNoneh2", CP9, null, null, VLAN_NONE);
184 private static final Interface VNONEH3 =
185 new Interface("vNoneh3", CP10, null, null, VLAN_NONE);
nosignal5fd282e2016-09-16 16:11:40 -0700186
Luca Prete092e8952016-10-26 16:25:56 +0200187 private static final Host V100HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700188 new DefaultHost(PID, HID1, MAC1, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100189 getLocation(1), Collections.singleton(IP1));
Luca Prete092e8952016-10-26 16:25:56 +0200190 private static final Host V100HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700191 new DefaultHost(PID, HID2, MAC2, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100192 getLocation(2), Sets.newHashSet());
Luca Prete092e8952016-10-26 16:25:56 +0200193 private static final Host V200HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700194 new DefaultHost(PID, HID3, MAC3, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100195 getLocation(3), Collections.singleton(IP2));
Luca Prete092e8952016-10-26 16:25:56 +0200196 private static final Host V200HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700197 new DefaultHost(PID, HID4, MAC4, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100198 getLocation(4), Sets.newHashSet());
Luca Prete092e8952016-10-26 16:25:56 +0200199 private static final Host V300HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700200 new DefaultHost(PID, HID5, MAC5, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100201 getLocation(5), Sets.newHashSet());
Luca Prete092e8952016-10-26 16:25:56 +0200202 private static final Host V300HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700203 new DefaultHost(PID, HID6, MAC6, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100204 getLocation(6), Sets.newHashSet());
Luca Prete092e8952016-10-26 16:25:56 +0200205 private static final Host V300HOST3 =
nosignal5fd282e2016-09-16 16:11:40 -0700206 new DefaultHost(PID, HID7, MAC7, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100207 getLocation(7), Sets.newHashSet());
208 private static final Host V400HOST1 =
209 new DefaultHost(PID, HID8, MAC8, VLAN400,
210 getLocation(4, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700211
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100212 private static final Host VNONEHOST1 =
213 new DefaultHost(PID, HID9, MAC9, VlanId.NONE,
214 getLocation(3, 2), Sets.newHashSet());
215 private static final Host VNONEHOST2 =
216 new DefaultHost(PID, HID10, MAC10, VlanId.NONE,
217 getLocation(5, 1), Sets.newHashSet());
218 private static final Host VNONEHOST3 =
219 new DefaultHost(PID, HID11, MAC11, VlanId.NONE,
220 getLocation(5, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700221
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100222 private static final Set<Interface> AVAILABLE_INTERFACES =
223 ImmutableSet.of(V100H1, V100H2, V200H1, V200H2, V300H1, V300H2,
224 V400H1, VNONEH1, VNONEH2);
225
226 private static final Set<Host> AVAILABLE_HOSTS =
Luca Prete092e8952016-10-26 16:25:56 +0200227 ImmutableSet.of(V100HOST1, V100HOST2, V200HOST1,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100228 V200HOST2, V300HOST1, V300HOST2, V300HOST3,
229 VNONEHOST1, VNONEHOST2,
230 V400HOST1, VNONEHOST3);
231
232 private SetMultimap<String, Interface> interfacesByVpls = HashMultimap.create();
nosignal5fd282e2016-09-16 16:11:40 -0700233
234 private ApplicationService applicationService;
235 private CoreService coreService;
236 private HostListener hostListener;
237 private NetworkConfigService configService;
238 private Set<Host> hostsAvailable;
239 private HostService hostService;
240 private IntentService intentService;
241 private InterfaceService interfaceService;
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100242 private VplsConfigService vplsConfigService;
nosignal5fd282e2016-09-16 16:11:40 -0700243 private Vpls vpls;
244
Luca Prete9c2ee072016-02-16 11:00:44 -0800245 @Before
246 public void setUp() throws Exception {
Ray Milkey6e968eb2016-06-29 09:37:27 -0700247 idGenerator = new TestIdGenerator();
Thomas Vachuska23235962017-02-03 11:44:15 -0800248 Intent.unbindIdGenerator(idGenerator);
Ray Milkey6e968eb2016-06-29 09:37:27 -0700249 Intent.bindIdGenerator(idGenerator);
250
Luca Prete9c2ee072016-02-16 11:00:44 -0800251 applicationService = createMock(ApplicationService.class);
252
nosignal5fd282e2016-09-16 16:11:40 -0700253 configService = createMock(NetworkConfigService.class);
254
Luca Prete9c2ee072016-02-16 11:00:44 -0800255 coreService = createMock(CoreService.class);
256 expect(coreService.registerApplication(APP_NAME))
257 .andReturn(APPID);
258 replay(coreService);
259
260 hostsAvailable = Sets.newHashSet();
261 hostService = new TestHostService(hostsAvailable);
262
263 intentService = new TestIntentService();
264
265 TestIntentSynchronizer intentSynchronizer =
266 new TestIntentSynchronizer(intentService);
267
268 interfaceService = createMock(InterfaceService.class);
Luca Pretea8854822016-04-26 16:30:55 -0700269 interfaceService.addListener(anyObject(InterfaceListener.class));
270 expectLastCall().anyTimes();
Luca Prete092e8952016-10-26 16:25:56 +0200271 addIfaceConfig();
Luca Prete9c2ee072016-02-16 11:00:44 -0800272
Luca Prete092e8952016-10-26 16:25:56 +0200273 interfacesByVpls.put(VPLS1, V100H1);
274 interfacesByVpls.put(VPLS1, V200H1);
275 interfacesByVpls.put(VPLS1, V300H1);
276 interfacesByVpls.put(VPLS2, V100H2);
277 interfacesByVpls.put(VPLS2, V200H2);
278 interfacesByVpls.put(VPLS2, V300H2);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100279 interfacesByVpls.put(VPLS3, VNONEH1);
280 interfacesByVpls.put(VPLS3, VNONEH2);
281 interfacesByVpls.put(VPLS4, V400H1);
282 interfacesByVpls.put(VPLS4, VNONEH3);
nosignal5fd282e2016-09-16 16:11:40 -0700283
Luca Prete092e8952016-10-26 16:25:56 +0200284 Map<String, EncapsulationType> encapByVpls = new HashMap<>();
285 encapByVpls.put(VPLS1, VLAN);
286 encapByVpls.put(VPLS2, NONE);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100287 encapByVpls.put(VPLS3, NONE);
288 encapByVpls.put(VPLS4, NONE);
Luca Prete092e8952016-10-26 16:25:56 +0200289
290 vplsConfigService = new TestVplsConfigService(interfacesByVpls, encapByVpls);
nosignal5fd282e2016-09-16 16:11:40 -0700291
Luca Prete9c2ee072016-02-16 11:00:44 -0800292 vpls = new Vpls();
293 vpls.applicationService = applicationService;
294 vpls.coreService = coreService;
295 vpls.hostService = hostService;
nosignal5fd282e2016-09-16 16:11:40 -0700296 vpls.vplsConfigService = vplsConfigService;
Luca Prete9c2ee072016-02-16 11:00:44 -0800297 vpls.intentService = intentService;
298 vpls.interfaceService = interfaceService;
nosignal5fd282e2016-09-16 16:11:40 -0700299 vpls.configService = configService;
Luca Prete9c2ee072016-02-16 11:00:44 -0800300 vpls.intentSynchronizer = intentSynchronizer;
Yi Tseng28767f02016-09-13 04:27:20 -0700301
Luca Prete9c2ee072016-02-16 11:00:44 -0800302 }
303
Ray Milkey6e968eb2016-06-29 09:37:27 -0700304 @After
305 public void tearDown() {
306 Intent.unbindIdGenerator(idGenerator);
307 }
308
Luca Prete9c2ee072016-02-16 11:00:44 -0800309 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100310 * Creates the interface configuration:
311 * On devices 1 and 2 is configured an interface on port 1 with vlan 100.
312 * On device 3 is configured an interface on port 3 with no vlan.
313 * On devices 3 and 4 is configured an interface on port 1 with vlan 200.
314 * On device 4 is an interface configured on port 2 with vlan 400.
315 * On device 5 are configured two interfaces on port 1 and 2 with no vlan.
316 * On device 5 and 6 is configured an interface on port 1 with vlan 300.
Luca Prete9c2ee072016-02-16 11:00:44 -0800317 */
Luca Prete092e8952016-10-26 16:25:56 +0200318 private void addIfaceConfig() {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100319 Set<Interface> interfaces = ImmutableSet.copyOf(AVAILABLE_INTERFACES);
Luca Prete092e8952016-10-26 16:25:56 +0200320 Set<Interface> vlanOneSet = ImmutableSet.of(V100H1, V100H2);
321 Set<Interface> vlanTwoSet = ImmutableSet.of(V200H1, V200H2);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100322 Set<Interface> vlanThreeSet = ImmutableSet.of(VNONEH1, VNONEH2);
323 Set<Interface> vlanFourSet = ImmutableSet.of(V400H1, VNONEH3);
Luca Prete9c2ee072016-02-16 11:00:44 -0800324
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100325 AVAILABLE_INTERFACES.forEach(intf -> {
nosignal5fd282e2016-09-16 16:11:40 -0700326 expect(interfaceService.getInterfacesByPort(intf.connectPoint()))
Luca Prete9c2ee072016-02-16 11:00:44 -0800327 .andReturn(Sets.newHashSet(intf)).anyTimes();
nosignal5fd282e2016-09-16 16:11:40 -0700328 });
329 expect(interfaceService.getInterfacesByVlan(VLAN100))
Luca Prete9c2ee072016-02-16 11:00:44 -0800330 .andReturn(vlanOneSet).anyTimes();
nosignal5fd282e2016-09-16 16:11:40 -0700331 expect(interfaceService.getInterfacesByVlan(VLAN200))
Luca Prete9c2ee072016-02-16 11:00:44 -0800332 .andReturn(vlanTwoSet).anyTimes();
nosignal5fd282e2016-09-16 16:11:40 -0700333 expect(interfaceService.getInterfacesByVlan(VLAN300))
334 .andReturn(vlanThreeSet).anyTimes();
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100335 expect(interfaceService.getInterfacesByVlan(VLAN400))
336 .andReturn(vlanFourSet).anyTimes();
337 expect(interfaceService.getInterfacesByVlan(VlanId.NONE))
338 .andReturn(vlanFourSet).anyTimes();
Luca Prete9c2ee072016-02-16 11:00:44 -0800339 expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
340
341 replay(interfaceService);
342 }
343
344 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100345 * Seven ports are configured with VLANs, while three ports are not. No hosts are
346 * registered by the HostService.
347 *
348 * The first three ports have an interface configured on VPLS 1,
349 * the other three on VPLS 2. Two ports are defined for VPLS 3, while
350 * the two remaining ports are configured on VPLS 4.
351 *
352 * The number of intents expected is 10: three for VPLS 1, three for VPLS 2,
353 * two for VPLS 3, two for VPLS 4. Eight MP2SP intents.
354 * Checks if the number of intents submitted to the intent framework is
355 * equal to the number of intents expected and if all intents are equivalent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800356 */
357 @Test
Luca Prete092e8952016-10-26 16:25:56 +0200358 public void activateNoHosts() {
Luca Prete9c2ee072016-02-16 11:00:44 -0800359 vpls.activate();
360
Yi Tseng28767f02016-09-13 04:27:20 -0700361 List<Intent> expectedIntents = Lists.newArrayList();
nosignal5fd282e2016-09-16 16:11:40 -0700362 Set<FilteredConnectPoint> fcPoints;
363
Luca Prete092e8952016-10-26 16:25:56 +0200364 fcPoints = buildFCPoints(ImmutableSet.of(V100H1, V200H1, V300H1));
365 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS1, VLAN));
nosignal5fd282e2016-09-16 16:11:40 -0700366
Luca Prete092e8952016-10-26 16:25:56 +0200367 fcPoints = buildFCPoints(ImmutableSet.of(V100H2, V200H2, V300H2));
368 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS2, NONE));
Luca Prete9c2ee072016-02-16 11:00:44 -0800369
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100370 fcPoints = buildFCPoints(ImmutableSet.of(VNONEH1, VNONEH2));
371 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS3, NONE));
372
373 fcPoints = buildFCPoints(ImmutableSet.of(V400H1, VNONEH3));
374 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS4, NONE));
375
Luca Prete9c2ee072016-02-16 11:00:44 -0800376 checkIntents(expectedIntents);
377 }
378
379 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100380 * Ten ports are configured with VLANs and ten hosts are registered by the
381 * HostService.
382 *
383 * The first three ports have an interface configured on VPLS 1,
384 * the other three on VPLS 2, two on VPLS3 and two on VPLS4.
385 *
386 * The number of intents expected is twenty: six
387 * for VPLS 1, six for VPLS 2. four for VPLS 3, four for VPLS 4.
388 * That is ten sp2mp intents, ten mp2sp intents. For VPLS 1
nosignal5fd282e2016-09-16 16:11:40 -0700389 * IPs are added to demonstrate this doesn't influence the number of intents
390 * created. Checks if the number of intents submitted to the intent
391 * framework is equal to the number of intents expected and if all intents
392 * are equivalent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800393 */
394 @Test
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100395 public void tenInterfacesConfiguredHostsPresent() {
396 hostsAvailable.addAll(AVAILABLE_HOSTS);
Luca Prete9c2ee072016-02-16 11:00:44 -0800397
398 vpls.activate();
399
Yi Tseng28767f02016-09-13 04:27:20 -0700400 List<Intent> expectedIntents = Lists.newArrayList();
nosignal5fd282e2016-09-16 16:11:40 -0700401 Set<FilteredConnectPoint> fcPoints;
402 Set<Host> hosts;
403
Luca Prete092e8952016-10-26 16:25:56 +0200404 fcPoints = buildFCPoints(ImmutableSet.of(V100H1, V200H1, V300H1));
405 hosts = ImmutableSet.of(V100HOST1, V200HOST1, V300HOST1);
406 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS1, VLAN));
407 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS1, VLAN));
nosignal5fd282e2016-09-16 16:11:40 -0700408
Luca Prete092e8952016-10-26 16:25:56 +0200409 fcPoints = buildFCPoints(ImmutableSet.of(V100H2, V200H2, V300H2));
410 hosts = ImmutableSet.of(V100HOST2, V200HOST2, V300HOST2);
411 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS2, NONE));
412 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS2, NONE));
Luca Prete9c2ee072016-02-16 11:00:44 -0800413
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100414 fcPoints = buildFCPoints(ImmutableSet.of(VNONEH1, VNONEH2));
415 hosts = ImmutableSet.of(VNONEHOST1, VNONEHOST2);
416 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS3, NONE));
417 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS3, NONE));
418
419 fcPoints = buildFCPoints(ImmutableSet.of(V400H1, VNONEH3));
420 hosts = ImmutableSet.of(V400HOST1, VNONEHOST3);
421 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS4, NONE));
422 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS4, NONE));
423
Luca Prete9c2ee072016-02-16 11:00:44 -0800424 checkIntents(expectedIntents);
425 }
426
427 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100428 * Ten ports are configured; seven have VLANs and three do not.
429 * Initially, no hosts are registered by the HostService.
430 *
431 * The first three ports have an interface configured on
432 * VPLS 1, three have an interface configured on VPLS 2, two have an
433 * interface configured on VPLS 3 and two have an interface configured
434 * on VPLS 4, three have an interface configure. When the
nosignal5fd282e2016-09-16 16:11:40 -0700435 * module starts up, three hosts attached to device one, two and three -
436 * port 1, are registered by the HostService and events are sent to the
437 * application. sp2mp intents are created for all interfaces configured and
438 * mp2sp intents are created only for the hosts attached.
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100439 *
440 * The number of intents expected is seventeen: six for VPLS 1,
441 * three for VPLS 2, four for VPLS3 and four for VPLS4.
442 * Ten sp2mp intents, seven mp2sp intents. IPs are added on the first two
nosignal5fd282e2016-09-16 16:11:40 -0700443 * hosts only to demonstrate this doesn't influence the number of intents
Luca Prete9c2ee072016-02-16 11:00:44 -0800444 * created.
nosignal5fd282e2016-09-16 16:11:40 -0700445 * An additional host is added on device seven - port 1, to demonstrate that
446 * the application does not generate intents, even if the interface uses the
447 * same VLAN Id of the other interfaces configured for the specifc VPLS.
448 * Checks if the number of intents submitted to the intent framework is equal
449 * to the number of intents expected and if all intents are equivalent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800450 */
451 @Test
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100452 public void tenInterfacesThreeHostEventsSameVpls() {
Luca Prete9c2ee072016-02-16 11:00:44 -0800453 vpls.activate();
454
nosignal5fd282e2016-09-16 16:11:40 -0700455 List<Intent> expectedIntents = Lists.newArrayList();
456 Set<FilteredConnectPoint> fcPoints;
457 Set<Host> hosts;
458
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100459 hostsAvailable.addAll(Sets.newHashSet(V100HOST1, V200HOST1,
460 V300HOST1, V300HOST3,
461 VNONEHOST1, VNONEHOST2,
462 V400HOST1, VNONEHOST3));
Luca Prete9c2ee072016-02-16 11:00:44 -0800463 hostsAvailable.forEach(host ->
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100464 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host)));
Luca Prete9c2ee072016-02-16 11:00:44 -0800465
Luca Prete092e8952016-10-26 16:25:56 +0200466 fcPoints = buildFCPoints(ImmutableSet.of(V100H1, V200H1, V300H1));
467 hosts = ImmutableSet.of(V100HOST1, V200HOST1, V300HOST1);
468 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS1, VLAN));
469 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS1, VLAN));
nosignal5fd282e2016-09-16 16:11:40 -0700470
Luca Prete092e8952016-10-26 16:25:56 +0200471 fcPoints = buildFCPoints(ImmutableSet.of(V100H2, V200H2, V300H2));
472 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS2, NONE));
Luca Prete9c2ee072016-02-16 11:00:44 -0800473
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100474 fcPoints = buildFCPoints(ImmutableSet.of(VNONEH1, VNONEH2));
475 hosts = ImmutableSet.of(VNONEHOST1, VNONEHOST2);
476 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS3, NONE));
477 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS3, NONE));
478
479 fcPoints = buildFCPoints(ImmutableSet.of(V400H1, VNONEH3));
480 hosts = ImmutableSet.of(V400HOST1, VNONEHOST3);
481 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS4, NONE));
482 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS4, NONE));
483
Luca Prete9c2ee072016-02-16 11:00:44 -0800484 checkIntents(expectedIntents);
485 }
486
487 /**
nosignal5fd282e2016-09-16 16:11:40 -0700488 * Generates a list of the expected sp2mp intents for a VPLS.
489 *
490 * @param fcPoints the filtered connect point
491 * @param name the name of the VPLS
Luca Prete092e8952016-10-26 16:25:56 +0200492 * @param encap the encapsulation type
nosignal5fd282e2016-09-16 16:11:40 -0700493 * @return the list of expected sp2mp intents for the given VPLS
Luca Prete9c2ee072016-02-16 11:00:44 -0800494 */
nosignal5fd282e2016-09-16 16:11:40 -0700495 private List<SinglePointToMultiPointIntent>
Luca Prete092e8952016-10-26 16:25:56 +0200496 generateVplsBrc(Set<FilteredConnectPoint> fcPoints, String name, EncapsulationType encap) {
nosignal5fd282e2016-09-16 16:11:40 -0700497 List<SinglePointToMultiPointIntent> intents = Lists.newArrayList();
Luca Prete9c2ee072016-02-16 11:00:44 -0800498
nosignal5fd282e2016-09-16 16:11:40 -0700499 fcPoints.forEach(point -> {
500 Set<FilteredConnectPoint> otherPoints =
501 fcPoints.stream()
502 .filter(fcp -> !fcp.equals(point))
503 .collect(Collectors.toSet());
Luca Prete9c2ee072016-02-16 11:00:44 -0800504
nosignal5fd282e2016-09-16 16:11:40 -0700505 Key brckey = buildKey(PREFIX_BROADCAST,
Luca Prete13465642017-01-04 10:31:48 -0800506 point.connectPoint(),
507 name,
508 MacAddress.BROADCAST);
nosignal5fd282e2016-09-16 16:11:40 -0700509
Luca Prete092e8952016-10-26 16:25:56 +0200510 intents.add(buildBrcIntent(brckey, point, otherPoints, encap));
Luca Prete9c2ee072016-02-16 11:00:44 -0800511 });
512
nosignal5fd282e2016-09-16 16:11:40 -0700513 return intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800514 }
515
516 /**
nosignal5fd282e2016-09-16 16:11:40 -0700517 * Generates a list of expected mp2sp intents for a given VPLS.
518 *
519 * @param fcPoints the filtered connect point
520 * @param hosts the hosts
521 * @param name the name of the VPLS
Luca Prete092e8952016-10-26 16:25:56 +0200522 * @param encap the encapsulation type
nosignal5fd282e2016-09-16 16:11:40 -0700523 * @return the list of expected mp2sp intents for the given VPLS
524 */
525 private List<MultiPointToSinglePointIntent>
Luca Prete092e8952016-10-26 16:25:56 +0200526 generateVplsUni(Set<FilteredConnectPoint> fcPoints, Set<Host> hosts,
527 String name, EncapsulationType encap) {
nosignal5fd282e2016-09-16 16:11:40 -0700528 List<MultiPointToSinglePointIntent> intents = Lists.newArrayList();
529
530 hosts.forEach(host -> {
531 FilteredConnectPoint hostPoint = getHostPoint(host, fcPoints);
532
533 Set<FilteredConnectPoint> otherPoints =
534 fcPoints.stream()
535 .filter(fcp -> !fcp.equals(hostPoint))
536 .collect(Collectors.toSet());
537
538 Key uniKey = buildKey(PREFIX_UNICAST,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100539 host.location(), name, host.mac());
nosignal5fd282e2016-09-16 16:11:40 -0700540
Luca Prete092e8952016-10-26 16:25:56 +0200541 intents.add(buildUniIntent(uniKey, otherPoints, hostPoint, host, encap));
nosignal5fd282e2016-09-16 16:11:40 -0700542 });
543
544 return intents;
545 }
546
547 /**
548 * Checks if the number of intents submitted to the intent framework is equal
549 * to the number of intents expected and if all intents are equivalent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800550 *
551 * @param intents the list of intents expected
552 */
553 private void checkIntents(List<Intent> intents) {
Luca Prete092e8952016-10-26 16:25:56 +0200554 assertEquals("The number of intents submitted differs from the number" +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100555 " of intents expected. ",
Luca Prete092e8952016-10-26 16:25:56 +0200556 intents.size(), intentService.getIntentCount());
Luca Prete9c2ee072016-02-16 11:00:44 -0800557
558 for (Intent intentOne : intents) {
559 boolean found = false;
560 for (Intent intentTwo : intentService.getIntents()) {
561 if (intentOne.key().equals(intentTwo.key())) {
562 found = true;
Luca Prete092e8952016-10-26 16:25:56 +0200563 assertTrue(format("The intent submitted is different from" +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100564 " the intent expected. %s %s",
Luca Prete092e8952016-10-26 16:25:56 +0200565 intentOne, intentTwo),
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100566 IntentUtils.intentsAreEqual(intentOne, intentTwo));
Luca Prete9c2ee072016-02-16 11:00:44 -0800567 break;
568 }
569 }
Luca Prete092e8952016-10-26 16:25:56 +0200570 assertTrue("The intent submitted is not equal to any of the expected" +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100571 " intents. ", found);
Luca Prete9c2ee072016-02-16 11:00:44 -0800572 }
573 }
574
575 /**
nosignal5fd282e2016-09-16 16:11:40 -0700576 * Builds a broadcast intent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800577 *
nosignal5fd282e2016-09-16 16:11:40 -0700578 * @param key the key to identify the intent
579 * @param src the ingress connect point
580 * @param dsts the egress connect points
581 * @return the generated single-point to multi-point intent
Luca Prete9c2ee072016-02-16 11:00:44 -0800582 */
583 private SinglePointToMultiPointIntent buildBrcIntent(Key key,
Luca Prete092e8952016-10-26 16:25:56 +0200584 FilteredConnectPoint src,
585 Set<FilteredConnectPoint> dsts,
586 EncapsulationType encap) {
587 SinglePointToMultiPointIntent.Builder intentBuilder;
Luca Prete9c2ee072016-02-16 11:00:44 -0800588
Luca Prete9c2ee072016-02-16 11:00:44 -0800589 TrafficSelector selector = DefaultTrafficSelector.builder()
590 .matchEthDst(MacAddress.BROADCAST)
Luca Prete9c2ee072016-02-16 11:00:44 -0800591 .build();
592
Luca Prete092e8952016-10-26 16:25:56 +0200593 intentBuilder = SinglePointToMultiPointIntent.builder()
Luca Prete9c2ee072016-02-16 11:00:44 -0800594 .appId(APPID)
595 .key(key)
596 .selector(selector)
nosignal5fd282e2016-09-16 16:11:40 -0700597 .filteredIngressPoint(src)
598 .filteredEgressPoints(dsts)
Luca Prete13465642017-01-04 10:31:48 -0800599 .constraints(PARTIAL_FAILURE_CONSTRAINT)
Luca Prete092e8952016-10-26 16:25:56 +0200600 .priority(PRIORITY_OFFSET);
601
Luca Prete13465642017-01-04 10:31:48 -0800602 setEncap(intentBuilder, PARTIAL_FAILURE_CONSTRAINT, encap);
Luca Prete092e8952016-10-26 16:25:56 +0200603
604 return intentBuilder.build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800605 }
606
607 /**
nosignal5fd282e2016-09-16 16:11:40 -0700608 * Builds a unicast intent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800609 *
nosignal5fd282e2016-09-16 16:11:40 -0700610 * @param key the key to identify the intent
611 * @param srcs the ingress connect points
612 * @param dst the egress connect point
613 * @param host the destination Host
614 * @return the generated multi-point to single-point intent
Luca Prete9c2ee072016-02-16 11:00:44 -0800615 */
616 private MultiPointToSinglePointIntent buildUniIntent(Key key,
Luca Prete092e8952016-10-26 16:25:56 +0200617 Set<FilteredConnectPoint> srcs,
618 FilteredConnectPoint dst,
619 Host host,
620 EncapsulationType encap) {
621 MultiPointToSinglePointIntent.Builder intentBuilder;
622
nosignal5fd282e2016-09-16 16:11:40 -0700623 TrafficSelector selector = DefaultTrafficSelector.builder()
624 .matchEthDst(host.mac())
625 .build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800626
Luca Prete092e8952016-10-26 16:25:56 +0200627 intentBuilder = MultiPointToSinglePointIntent.builder()
Luca Prete9c2ee072016-02-16 11:00:44 -0800628 .appId(APPID)
629 .key(key)
630 .selector(selector)
nosignal5fd282e2016-09-16 16:11:40 -0700631 .filteredIngressPoints(srcs)
632 .filteredEgressPoint(dst)
Luca Prete13465642017-01-04 10:31:48 -0800633 .constraints(PARTIAL_FAILURE_CONSTRAINT)
Luca Prete092e8952016-10-26 16:25:56 +0200634 .priority(PRIORITY_OFFSET);
nosignal5fd282e2016-09-16 16:11:40 -0700635
Luca Prete13465642017-01-04 10:31:48 -0800636 setEncap(intentBuilder, PARTIAL_FAILURE_CONSTRAINT, encap);
Luca Prete092e8952016-10-26 16:25:56 +0200637
638 return intentBuilder.build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800639 }
640
641 /**
nosignal5fd282e2016-09-16 16:11:40 -0700642 * Returns the filtered connect point associated to a given host.
Luca Prete9c2ee072016-02-16 11:00:44 -0800643 *
nosignal5fd282e2016-09-16 16:11:40 -0700644 * @param host the target host
645 * @param fcps the filtered connected points
646 * @return the filtered connect point associated to the given host; null
647 * otherwise
648 */
649 private FilteredConnectPoint getHostPoint(Host host,
650 Set<FilteredConnectPoint> fcps) {
651 return fcps.stream()
652 .filter(fcp -> fcp.connectPoint().equals(host.location()))
653 .filter(fcp -> {
654 VlanIdCriterion vlanCriterion =
655 (VlanIdCriterion) fcp.trafficSelector().
656 getCriterion(Criterion.Type.VLAN_VID);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100657 return vlanCriterion == null ||
nosignal5fd282e2016-09-16 16:11:40 -0700658 vlanCriterion.vlanId().equals(host.vlan());
659 })
660 .findFirst()
661 .orElse(null);
662 }
663
664 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100665 * Computes a set of filtered connect points from a list of given interfaces.
nosignal5fd282e2016-09-16 16:11:40 -0700666 *
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100667 * @param interfaces the interfaces to compute
nosignal5fd282e2016-09-16 16:11:40 -0700668 * @return the set of filtered connect points
669 */
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100670 private Set<FilteredConnectPoint> buildFCPoints(Collection<Interface> interfaces) {
671 // Build all filtered connected points in the VPLS
nosignal5fd282e2016-09-16 16:11:40 -0700672 return interfaces
673 .stream()
674 .map(intf -> {
675 TrafficSelector.Builder selectorBuilder =
676 DefaultTrafficSelector.builder();
nosignal5fd282e2016-09-16 16:11:40 -0700677 if (!intf.vlan().equals(VlanId.NONE)) {
678 selectorBuilder.matchVlanId(intf.vlan());
679 }
nosignal5fd282e2016-09-16 16:11:40 -0700680 return new FilteredConnectPoint(intf.connectPoint(),
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100681 selectorBuilder.build());
nosignal5fd282e2016-09-16 16:11:40 -0700682 })
683 .collect(Collectors.toSet());
684 }
685
686 /**
687 * Builds an intent Key either for a single-point to multi-point or
688 * multi-point to single-point intent, based on a prefix that defines
689 * the intent type, the connection point representing the source or the
Luca Prete092e8952016-10-26 16:25:56 +0200690 * destination and the VLAN Id representing the VPLS.
nosignal5fd282e2016-09-16 16:11:40 -0700691 *
692 * @param prefix the key prefix
693 * @param cPoint the ingress/egress connect point
Luca Prete092e8952016-10-26 16:25:56 +0200694 * @param vplsName the VPLS name
nosignal5fd282e2016-09-16 16:11:40 -0700695 * @param hostMac the ingress/egress MAC address
696 * @return the key to identify the intent
697 */
698 private Key buildKey(String prefix,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100699 ConnectPoint cPoint,
700 String vplsName,
701 MacAddress hostMac) {
Luca Prete092e8952016-10-26 16:25:56 +0200702 String keyString = vplsName +
nosignal5fd282e2016-09-16 16:11:40 -0700703 DASH +
704 prefix +
705 DASH +
706 cPoint.deviceId() +
707 DASH +
708 cPoint.port() +
709 DASH +
710 hostMac;
711
712 return Key.of(keyString, APPID);
713 }
714
715 /**
716 * Returns the device Id of the ith device.
717 *
718 * @param i the device to get the Id of
719 * @return the device Id
Luca Prete9c2ee072016-02-16 11:00:44 -0800720 */
721 private static DeviceId getDeviceId(int i) {
722 return DeviceId.deviceId("" + i);
723 }
724
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100725 private static MacAddress getMac(int n) {
726 return MacAddress.valueOf(String.format("00:00:00:00:00:%s", n));
727 }
728
Luca Prete9c2ee072016-02-16 11:00:44 -0800729 private static HostLocation getLocation(int i) {
730 return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L);
731 }
732
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100733 private static HostLocation getLocation(int d, int p) {
734 return new HostLocation(new ConnectPoint(getDeviceId(d),
735 PortNumber.portNumber(p)), 123L);
736 }
737
Luca Prete9c2ee072016-02-16 11:00:44 -0800738 /**
nosignal5fd282e2016-09-16 16:11:40 -0700739 * Represents a fake IntentService class that allows to store and retrieve
740 * intents without implementing the IntentService logic.
Luca Prete9c2ee072016-02-16 11:00:44 -0800741 */
742 private class TestIntentService extends IntentServiceAdapter {
743
Luca Pretec21c6e62016-09-22 14:52:21 -0700744 private Map<Key, Intent> intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800745
746 public TestIntentService() {
Luca Pretec21c6e62016-09-22 14:52:21 -0700747 intents = Maps.newHashMap();
Luca Prete9c2ee072016-02-16 11:00:44 -0800748 }
749
750 @Override
751 public void submit(Intent intent) {
Luca Pretec21c6e62016-09-22 14:52:21 -0700752 intents.put(intent.key(), intent);
Luca Prete9c2ee072016-02-16 11:00:44 -0800753 }
754
755 @Override
756 public long getIntentCount() {
757 return intents.size();
758 }
759
760 @Override
761 public Iterable<Intent> getIntents() {
Luca Pretec21c6e62016-09-22 14:52:21 -0700762 return intents.values();
Luca Prete9c2ee072016-02-16 11:00:44 -0800763 }
764
765 @Override
766 public Intent getIntent(Key intentKey) {
Luca Pretec21c6e62016-09-22 14:52:21 -0700767 for (Intent intent : intents.values()) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800768 if (intent.key().equals(intentKey)) {
769 return intent;
770 }
771 }
772 return null;
773 }
774 }
775
776 /**
777 * Represents a fake HostService class which allows to add hosts manually
778 * in each test, when needed.
779 */
780 private class TestHostService extends HostServiceAdapter {
781
782 private Set<Host> hosts;
783
784 public TestHostService(Set<Host> hosts) {
785 this.hosts = hosts;
786 }
787
788 @Override
789 public void addListener(HostListener listener) {
790 VplsTest.this.hostListener = listener;
791 }
792
793 @Override
794 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
795 return hosts.stream()
nosignal5fd282e2016-09-16 16:11:40 -0700796 .filter(h -> h.location().equals(connectPoint))
Luca Prete9c2ee072016-02-16 11:00:44 -0800797 .collect(Collectors.toSet());
798 }
799
800 }
801
nosignal5fd282e2016-09-16 16:11:40 -0700802 /**
803 * Represents a fake IdGenerator class for intents.
804 */
Luca Prete9c2ee072016-02-16 11:00:44 -0800805 private static class TestIdGenerator implements IdGenerator {
806
807 private final AtomicLong id = new AtomicLong(0);
808
809 @Override
810 public long getNewId() {
811 return id.getAndIncrement();
812 }
813
814 }
815
816 /**
817 * Test IntentSynchronizer that passes all intents straight through to the
818 * intent service.
819 */
Jonathan Hart470ed4f2017-01-31 16:52:28 -0800820 private class TestIntentSynchronizer implements IntentSynchronizationService {
Luca Prete9c2ee072016-02-16 11:00:44 -0800821
822 private final IntentService intentService;
823
824 /**
nosignal5fd282e2016-09-16 16:11:40 -0700825 * Creates a new intent test synchronizer.
Luca Prete9c2ee072016-02-16 11:00:44 -0800826 *
827 * @param intentService intent service
828 */
829 public TestIntentSynchronizer(IntentService intentService) {
830 this.intentService = intentService;
831 }
832
833 @Override
834 public void submit(Intent intent) {
835 intentService.submit(intent);
836 }
837
838 @Override
839 public void withdraw(Intent intent) {
840 intentService.withdraw(intent);
841 }
842
843 @Override
Luca Prete2705d662016-04-29 15:30:23 -0700844 public void removeIntentsByAppId(ApplicationId applicationId) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800845 }
846 }
847
nosignal5fd282e2016-09-16 16:11:40 -0700848 /**
849 * Represents a fake VplsConfigService class which is needed for testing.
850 */
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100851 private class TestVplsConfigService extends VplsConfigServiceAdapter {
nosignal5fd282e2016-09-16 16:11:40 -0700852
Luca Prete092e8952016-10-26 16:25:56 +0200853 private final SetMultimap<String, Interface> ifacesByVplsName;
854 private final Map<String, EncapsulationType> encapsByVplsName;
855
nosignal5fd282e2016-09-16 16:11:40 -0700856 private Set<String> vplsAffectByApi = new HashSet<>();
857
Luca Prete092e8952016-10-26 16:25:56 +0200858 TestVplsConfigService(SetMultimap<String, Interface> ifacesByVplsName,
859 Map<String, EncapsulationType> encapsByVplsName) {
860 this.ifacesByVplsName = ifacesByVplsName;
861 this.encapsByVplsName = encapsByVplsName;
nosignal5fd282e2016-09-16 16:11:40 -0700862 }
863
864 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200865 public void addVpls(String vplsName, Set<String> ifaceNames, String encap) {
866 if (!ifacesByVplsName.containsKey(vplsName)) {
867 ifaceNames.forEach(ifaceName -> {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100868 AVAILABLE_INTERFACES.forEach(iface -> {
Luca Prete092e8952016-10-26 16:25:56 +0200869 if (iface.name().equals(ifaceName)) {
870 ifacesByVplsName.put(vplsName, iface);
nosignal5fd282e2016-09-16 16:11:40 -0700871 }
872 });
873 });
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100874 }
875 if (!ifacesByVplsName.containsKey(vplsName)) {
Luca Prete092e8952016-10-26 16:25:56 +0200876 encapsByVplsName.put(vplsName, valueOf(encap));
nosignal5fd282e2016-09-16 16:11:40 -0700877 }
878 }
879
880 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200881 public void removeVpls(String vplsName) {
882 if (ifacesByVplsName.containsKey(vplsName)) {
883 ifacesByVplsName.removeAll(vplsName);
nosignal5fd282e2016-09-16 16:11:40 -0700884 }
885 }
886
887 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200888 public void addIface(String vplsName, String iface) {
889 if (!ifacesByVplsName.containsKey(vplsName)) {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100890 AVAILABLE_INTERFACES.forEach(intf -> {
nosignal5fd282e2016-09-16 16:11:40 -0700891 if (intf.name().equals(iface)) {
Luca Prete092e8952016-10-26 16:25:56 +0200892 ifacesByVplsName.put(vplsName, intf);
nosignal5fd282e2016-09-16 16:11:40 -0700893 }
894 });
895 }
896 }
897
898 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200899 public void setEncap(String vplsName, String encap) {
900 encapsByVplsName.put(vplsName, EncapsulationType.enumFromString(encap));
901 }
902
903 @Override
904 public void removeIface(String iface) {
905 SetMultimap<String, Interface> search = HashMultimap.create(ifacesByVplsName);
nosignal5fd282e2016-09-16 16:11:40 -0700906 search.entries().forEach(e -> {
907 if (e.getValue().name().equals(iface)) {
Luca Prete092e8952016-10-26 16:25:56 +0200908 ifacesByVplsName.remove(e.getKey(), iface);
nosignal5fd282e2016-09-16 16:11:40 -0700909 }
910 });
911 }
912
913 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200914 public void cleanVplsConfig() {
915 ifacesByVplsName.clear();
nosignal5fd282e2016-09-16 16:11:40 -0700916 }
917
918 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200919 public EncapsulationType encap(String vplsName) {
920 EncapsulationType encap = null;
921 if (encapsByVplsName.containsKey(vplsName)) {
922 encap = encapsByVplsName.get(vplsName);
923 }
924 return encap;
925 }
926
927 @Override
928 public Set<String> vplsAffectedByApi() {
nosignal5fd282e2016-09-16 16:11:40 -0700929 Set<String> vplsNames = ImmutableSet.copyOf(vplsAffectByApi);
930
931 vplsAffectByApi.clear();
932
933 return vplsNames;
934 }
935
936 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200937 public Set<Interface> allIfaces() {
938 return ifacesByVplsName.values()
nosignal5fd282e2016-09-16 16:11:40 -0700939 .stream()
940 .collect(Collectors.toSet());
941 }
942
943 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200944 public Set<Interface> ifaces(String name) {
945 return ifacesByVplsName.get(name)
nosignal5fd282e2016-09-16 16:11:40 -0700946 .stream()
947 .collect(Collectors.toSet());
948 }
949
950 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200951 public Set<String> vplsNames() {
952 return ifacesByVplsName.keySet();
nosignal5fd282e2016-09-16 16:11:40 -0700953 }
954
955 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200956 public Set<String> vplsNamesOld() {
957 return ifacesByVplsName.keySet();
958 }
959
960 public SetMultimap<String, Interface> ifacesByVplsName() {
961 return ImmutableSetMultimap.copyOf(ifacesByVplsName);
nosignal5fd282e2016-09-16 16:11:40 -0700962 }
963
964 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200965 public SetMultimap<String, Interface> ifacesByVplsName(VlanId vlan,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100966 ConnectPoint connectPoint) {
Luca Prete092e8952016-10-26 16:25:56 +0200967 String vplsName =
968 ifacesByVplsName.entries().stream()
nosignal5fd282e2016-09-16 16:11:40 -0700969 .filter(e -> e.getValue().connectPoint().equals(connectPoint))
970 .filter(e -> e.getValue().vlan().equals(vlan))
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100971 .map(Map.Entry::getKey)
nosignal5fd282e2016-09-16 16:11:40 -0700972 .findFirst()
973 .orElse(null);
974 SetMultimap<String, Interface> result = HashMultimap.create();
Luca Prete092e8952016-10-26 16:25:56 +0200975 if (vplsName != null && ifacesByVplsName.containsKey(vplsName)) {
976 ifacesByVplsName.get(vplsName)
977 .forEach(intf -> result.put(vplsName, intf));
nosignal5fd282e2016-09-16 16:11:40 -0700978 return result;
979 }
980 return null;
981 }
nosignal5fd282e2016-09-16 16:11:40 -0700982 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800983}