blob: 3ebc858658e138ae0f0a4ff0e170d41b63cbd2fc [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();
248 Intent.bindIdGenerator(idGenerator);
249
Luca Prete9c2ee072016-02-16 11:00:44 -0800250 applicationService = createMock(ApplicationService.class);
251
nosignal5fd282e2016-09-16 16:11:40 -0700252 configService = createMock(NetworkConfigService.class);
253
Luca Prete9c2ee072016-02-16 11:00:44 -0800254 coreService = createMock(CoreService.class);
255 expect(coreService.registerApplication(APP_NAME))
256 .andReturn(APPID);
257 replay(coreService);
258
259 hostsAvailable = Sets.newHashSet();
260 hostService = new TestHostService(hostsAvailable);
261
262 intentService = new TestIntentService();
263
264 TestIntentSynchronizer intentSynchronizer =
265 new TestIntentSynchronizer(intentService);
266
267 interfaceService = createMock(InterfaceService.class);
Luca Pretea8854822016-04-26 16:30:55 -0700268 interfaceService.addListener(anyObject(InterfaceListener.class));
269 expectLastCall().anyTimes();
Luca Prete092e8952016-10-26 16:25:56 +0200270 addIfaceConfig();
Luca Prete9c2ee072016-02-16 11:00:44 -0800271
Luca Prete092e8952016-10-26 16:25:56 +0200272 interfacesByVpls.put(VPLS1, V100H1);
273 interfacesByVpls.put(VPLS1, V200H1);
274 interfacesByVpls.put(VPLS1, V300H1);
275 interfacesByVpls.put(VPLS2, V100H2);
276 interfacesByVpls.put(VPLS2, V200H2);
277 interfacesByVpls.put(VPLS2, V300H2);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100278 interfacesByVpls.put(VPLS3, VNONEH1);
279 interfacesByVpls.put(VPLS3, VNONEH2);
280 interfacesByVpls.put(VPLS4, V400H1);
281 interfacesByVpls.put(VPLS4, VNONEH3);
nosignal5fd282e2016-09-16 16:11:40 -0700282
Luca Prete092e8952016-10-26 16:25:56 +0200283 Map<String, EncapsulationType> encapByVpls = new HashMap<>();
284 encapByVpls.put(VPLS1, VLAN);
285 encapByVpls.put(VPLS2, NONE);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100286 encapByVpls.put(VPLS3, NONE);
287 encapByVpls.put(VPLS4, NONE);
Luca Prete092e8952016-10-26 16:25:56 +0200288
289 vplsConfigService = new TestVplsConfigService(interfacesByVpls, encapByVpls);
nosignal5fd282e2016-09-16 16:11:40 -0700290
Luca Prete9c2ee072016-02-16 11:00:44 -0800291 vpls = new Vpls();
292 vpls.applicationService = applicationService;
293 vpls.coreService = coreService;
294 vpls.hostService = hostService;
nosignal5fd282e2016-09-16 16:11:40 -0700295 vpls.vplsConfigService = vplsConfigService;
Luca Prete9c2ee072016-02-16 11:00:44 -0800296 vpls.intentService = intentService;
297 vpls.interfaceService = interfaceService;
nosignal5fd282e2016-09-16 16:11:40 -0700298 vpls.configService = configService;
Luca Prete9c2ee072016-02-16 11:00:44 -0800299 vpls.intentSynchronizer = intentSynchronizer;
Yi Tseng28767f02016-09-13 04:27:20 -0700300
Luca Prete9c2ee072016-02-16 11:00:44 -0800301 }
302
Ray Milkey6e968eb2016-06-29 09:37:27 -0700303 @After
304 public void tearDown() {
305 Intent.unbindIdGenerator(idGenerator);
306 }
307
Luca Prete9c2ee072016-02-16 11:00:44 -0800308 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100309 * Creates the interface configuration:
310 * On devices 1 and 2 is configured an interface on port 1 with vlan 100.
311 * On device 3 is configured an interface on port 3 with no vlan.
312 * On devices 3 and 4 is configured an interface on port 1 with vlan 200.
313 * On device 4 is an interface configured on port 2 with vlan 400.
314 * On device 5 are configured two interfaces on port 1 and 2 with no vlan.
315 * On device 5 and 6 is configured an interface on port 1 with vlan 300.
Luca Prete9c2ee072016-02-16 11:00:44 -0800316 */
Luca Prete092e8952016-10-26 16:25:56 +0200317 private void addIfaceConfig() {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100318 Set<Interface> interfaces = ImmutableSet.copyOf(AVAILABLE_INTERFACES);
Luca Prete092e8952016-10-26 16:25:56 +0200319 Set<Interface> vlanOneSet = ImmutableSet.of(V100H1, V100H2);
320 Set<Interface> vlanTwoSet = ImmutableSet.of(V200H1, V200H2);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100321 Set<Interface> vlanThreeSet = ImmutableSet.of(VNONEH1, VNONEH2);
322 Set<Interface> vlanFourSet = ImmutableSet.of(V400H1, VNONEH3);
Luca Prete9c2ee072016-02-16 11:00:44 -0800323
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100324 AVAILABLE_INTERFACES.forEach(intf -> {
nosignal5fd282e2016-09-16 16:11:40 -0700325 expect(interfaceService.getInterfacesByPort(intf.connectPoint()))
Luca Prete9c2ee072016-02-16 11:00:44 -0800326 .andReturn(Sets.newHashSet(intf)).anyTimes();
nosignal5fd282e2016-09-16 16:11:40 -0700327 });
328 expect(interfaceService.getInterfacesByVlan(VLAN100))
Luca Prete9c2ee072016-02-16 11:00:44 -0800329 .andReturn(vlanOneSet).anyTimes();
nosignal5fd282e2016-09-16 16:11:40 -0700330 expect(interfaceService.getInterfacesByVlan(VLAN200))
Luca Prete9c2ee072016-02-16 11:00:44 -0800331 .andReturn(vlanTwoSet).anyTimes();
nosignal5fd282e2016-09-16 16:11:40 -0700332 expect(interfaceService.getInterfacesByVlan(VLAN300))
333 .andReturn(vlanThreeSet).anyTimes();
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100334 expect(interfaceService.getInterfacesByVlan(VLAN400))
335 .andReturn(vlanFourSet).anyTimes();
336 expect(interfaceService.getInterfacesByVlan(VlanId.NONE))
337 .andReturn(vlanFourSet).anyTimes();
Luca Prete9c2ee072016-02-16 11:00:44 -0800338 expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
339
340 replay(interfaceService);
341 }
342
343 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100344 * Seven ports are configured with VLANs, while three ports are not. No hosts are
345 * registered by the HostService.
346 *
347 * The first three ports have an interface configured on VPLS 1,
348 * the other three on VPLS 2. Two ports are defined for VPLS 3, while
349 * the two remaining ports are configured on VPLS 4.
350 *
351 * The number of intents expected is 10: three for VPLS 1, three for VPLS 2,
352 * two for VPLS 3, two for VPLS 4. Eight MP2SP intents.
353 * Checks if the number of intents submitted to the intent framework is
354 * equal to the number of intents expected and if all intents are equivalent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800355 */
356 @Test
Luca Prete092e8952016-10-26 16:25:56 +0200357 public void activateNoHosts() {
Luca Prete9c2ee072016-02-16 11:00:44 -0800358 vpls.activate();
359
Yi Tseng28767f02016-09-13 04:27:20 -0700360 List<Intent> expectedIntents = Lists.newArrayList();
nosignal5fd282e2016-09-16 16:11:40 -0700361 Set<FilteredConnectPoint> fcPoints;
362
Luca Prete092e8952016-10-26 16:25:56 +0200363 fcPoints = buildFCPoints(ImmutableSet.of(V100H1, V200H1, V300H1));
364 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS1, VLAN));
nosignal5fd282e2016-09-16 16:11:40 -0700365
Luca Prete092e8952016-10-26 16:25:56 +0200366 fcPoints = buildFCPoints(ImmutableSet.of(V100H2, V200H2, V300H2));
367 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS2, NONE));
Luca Prete9c2ee072016-02-16 11:00:44 -0800368
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100369 fcPoints = buildFCPoints(ImmutableSet.of(VNONEH1, VNONEH2));
370 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS3, NONE));
371
372 fcPoints = buildFCPoints(ImmutableSet.of(V400H1, VNONEH3));
373 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS4, NONE));
374
Luca Prete9c2ee072016-02-16 11:00:44 -0800375 checkIntents(expectedIntents);
376 }
377
378 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100379 * Ten ports are configured with VLANs and ten hosts are registered by the
380 * HostService.
381 *
382 * The first three ports have an interface configured on VPLS 1,
383 * the other three on VPLS 2, two on VPLS3 and two on VPLS4.
384 *
385 * The number of intents expected is twenty: six
386 * for VPLS 1, six for VPLS 2. four for VPLS 3, four for VPLS 4.
387 * That is ten sp2mp intents, ten mp2sp intents. For VPLS 1
nosignal5fd282e2016-09-16 16:11:40 -0700388 * IPs are added to demonstrate this doesn't influence the number of intents
389 * created. Checks if the number of intents submitted to the intent
390 * framework is equal to the number of intents expected and if all intents
391 * are equivalent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800392 */
393 @Test
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100394 public void tenInterfacesConfiguredHostsPresent() {
395 hostsAvailable.addAll(AVAILABLE_HOSTS);
Luca Prete9c2ee072016-02-16 11:00:44 -0800396
397 vpls.activate();
398
Yi Tseng28767f02016-09-13 04:27:20 -0700399 List<Intent> expectedIntents = Lists.newArrayList();
nosignal5fd282e2016-09-16 16:11:40 -0700400 Set<FilteredConnectPoint> fcPoints;
401 Set<Host> hosts;
402
Luca Prete092e8952016-10-26 16:25:56 +0200403 fcPoints = buildFCPoints(ImmutableSet.of(V100H1, V200H1, V300H1));
404 hosts = ImmutableSet.of(V100HOST1, V200HOST1, V300HOST1);
405 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS1, VLAN));
406 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS1, VLAN));
nosignal5fd282e2016-09-16 16:11:40 -0700407
Luca Prete092e8952016-10-26 16:25:56 +0200408 fcPoints = buildFCPoints(ImmutableSet.of(V100H2, V200H2, V300H2));
409 hosts = ImmutableSet.of(V100HOST2, V200HOST2, V300HOST2);
410 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS2, NONE));
411 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS2, NONE));
Luca Prete9c2ee072016-02-16 11:00:44 -0800412
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100413 fcPoints = buildFCPoints(ImmutableSet.of(VNONEH1, VNONEH2));
414 hosts = ImmutableSet.of(VNONEHOST1, VNONEHOST2);
415 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS3, NONE));
416 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS3, NONE));
417
418 fcPoints = buildFCPoints(ImmutableSet.of(V400H1, VNONEH3));
419 hosts = ImmutableSet.of(V400HOST1, VNONEHOST3);
420 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS4, NONE));
421 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS4, NONE));
422
Luca Prete9c2ee072016-02-16 11:00:44 -0800423 checkIntents(expectedIntents);
424 }
425
426 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100427 * Ten ports are configured; seven have VLANs and three do not.
428 * Initially, no hosts are registered by the HostService.
429 *
430 * The first three ports have an interface configured on
431 * VPLS 1, three have an interface configured on VPLS 2, two have an
432 * interface configured on VPLS 3 and two have an interface configured
433 * on VPLS 4, three have an interface configure. When the
nosignal5fd282e2016-09-16 16:11:40 -0700434 * module starts up, three hosts attached to device one, two and three -
435 * port 1, are registered by the HostService and events are sent to the
436 * application. sp2mp intents are created for all interfaces configured and
437 * mp2sp intents are created only for the hosts attached.
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100438 *
439 * The number of intents expected is seventeen: six for VPLS 1,
440 * three for VPLS 2, four for VPLS3 and four for VPLS4.
441 * Ten sp2mp intents, seven mp2sp intents. IPs are added on the first two
nosignal5fd282e2016-09-16 16:11:40 -0700442 * hosts only to demonstrate this doesn't influence the number of intents
Luca Prete9c2ee072016-02-16 11:00:44 -0800443 * created.
nosignal5fd282e2016-09-16 16:11:40 -0700444 * An additional host is added on device seven - port 1, to demonstrate that
445 * the application does not generate intents, even if the interface uses the
446 * same VLAN Id of the other interfaces configured for the specifc VPLS.
447 * Checks if the number of intents submitted to the intent framework is equal
448 * to the number of intents expected and if all intents are equivalent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800449 */
450 @Test
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100451 public void tenInterfacesThreeHostEventsSameVpls() {
Luca Prete9c2ee072016-02-16 11:00:44 -0800452 vpls.activate();
453
nosignal5fd282e2016-09-16 16:11:40 -0700454 List<Intent> expectedIntents = Lists.newArrayList();
455 Set<FilteredConnectPoint> fcPoints;
456 Set<Host> hosts;
457
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100458 hostsAvailable.addAll(Sets.newHashSet(V100HOST1, V200HOST1,
459 V300HOST1, V300HOST3,
460 VNONEHOST1, VNONEHOST2,
461 V400HOST1, VNONEHOST3));
Luca Prete9c2ee072016-02-16 11:00:44 -0800462 hostsAvailable.forEach(host ->
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100463 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host)));
Luca Prete9c2ee072016-02-16 11:00:44 -0800464
Luca Prete092e8952016-10-26 16:25:56 +0200465 fcPoints = buildFCPoints(ImmutableSet.of(V100H1, V200H1, V300H1));
466 hosts = ImmutableSet.of(V100HOST1, V200HOST1, V300HOST1);
467 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS1, VLAN));
468 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS1, VLAN));
nosignal5fd282e2016-09-16 16:11:40 -0700469
Luca Prete092e8952016-10-26 16:25:56 +0200470 fcPoints = buildFCPoints(ImmutableSet.of(V100H2, V200H2, V300H2));
471 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS2, NONE));
Luca Prete9c2ee072016-02-16 11:00:44 -0800472
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100473 fcPoints = buildFCPoints(ImmutableSet.of(VNONEH1, VNONEH2));
474 hosts = ImmutableSet.of(VNONEHOST1, VNONEHOST2);
475 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS3, NONE));
476 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS3, NONE));
477
478 fcPoints = buildFCPoints(ImmutableSet.of(V400H1, VNONEH3));
479 hosts = ImmutableSet.of(V400HOST1, VNONEHOST3);
480 expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS4, NONE));
481 expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS4, NONE));
482
Luca Prete9c2ee072016-02-16 11:00:44 -0800483 checkIntents(expectedIntents);
484 }
485
486 /**
nosignal5fd282e2016-09-16 16:11:40 -0700487 * Generates a list of the expected sp2mp intents for a VPLS.
488 *
489 * @param fcPoints the filtered connect point
490 * @param name the name of the VPLS
Luca Prete092e8952016-10-26 16:25:56 +0200491 * @param encap the encapsulation type
nosignal5fd282e2016-09-16 16:11:40 -0700492 * @return the list of expected sp2mp intents for the given VPLS
Luca Prete9c2ee072016-02-16 11:00:44 -0800493 */
nosignal5fd282e2016-09-16 16:11:40 -0700494 private List<SinglePointToMultiPointIntent>
Luca Prete092e8952016-10-26 16:25:56 +0200495 generateVplsBrc(Set<FilteredConnectPoint> fcPoints, String name, EncapsulationType encap) {
nosignal5fd282e2016-09-16 16:11:40 -0700496 List<SinglePointToMultiPointIntent> intents = Lists.newArrayList();
Luca Prete9c2ee072016-02-16 11:00:44 -0800497
nosignal5fd282e2016-09-16 16:11:40 -0700498 fcPoints.forEach(point -> {
499 Set<FilteredConnectPoint> otherPoints =
500 fcPoints.stream()
501 .filter(fcp -> !fcp.equals(point))
502 .collect(Collectors.toSet());
Luca Prete9c2ee072016-02-16 11:00:44 -0800503
nosignal5fd282e2016-09-16 16:11:40 -0700504 Key brckey = buildKey(PREFIX_BROADCAST,
Luca Prete13465642017-01-04 10:31:48 -0800505 point.connectPoint(),
506 name,
507 MacAddress.BROADCAST);
nosignal5fd282e2016-09-16 16:11:40 -0700508
Luca Prete092e8952016-10-26 16:25:56 +0200509 intents.add(buildBrcIntent(brckey, point, otherPoints, encap));
Luca Prete9c2ee072016-02-16 11:00:44 -0800510 });
511
nosignal5fd282e2016-09-16 16:11:40 -0700512 return intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800513 }
514
515 /**
nosignal5fd282e2016-09-16 16:11:40 -0700516 * Generates a list of expected mp2sp intents for a given VPLS.
517 *
518 * @param fcPoints the filtered connect point
519 * @param hosts the hosts
520 * @param name the name of the VPLS
Luca Prete092e8952016-10-26 16:25:56 +0200521 * @param encap the encapsulation type
nosignal5fd282e2016-09-16 16:11:40 -0700522 * @return the list of expected mp2sp intents for the given VPLS
523 */
524 private List<MultiPointToSinglePointIntent>
Luca Prete092e8952016-10-26 16:25:56 +0200525 generateVplsUni(Set<FilteredConnectPoint> fcPoints, Set<Host> hosts,
526 String name, EncapsulationType encap) {
nosignal5fd282e2016-09-16 16:11:40 -0700527 List<MultiPointToSinglePointIntent> intents = Lists.newArrayList();
528
529 hosts.forEach(host -> {
530 FilteredConnectPoint hostPoint = getHostPoint(host, fcPoints);
531
532 Set<FilteredConnectPoint> otherPoints =
533 fcPoints.stream()
534 .filter(fcp -> !fcp.equals(hostPoint))
535 .collect(Collectors.toSet());
536
537 Key uniKey = buildKey(PREFIX_UNICAST,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100538 host.location(), name, host.mac());
nosignal5fd282e2016-09-16 16:11:40 -0700539
Luca Prete092e8952016-10-26 16:25:56 +0200540 intents.add(buildUniIntent(uniKey, otherPoints, hostPoint, host, encap));
nosignal5fd282e2016-09-16 16:11:40 -0700541 });
542
543 return intents;
544 }
545
546 /**
547 * Checks if the number of intents submitted to the intent framework is equal
548 * to the number of intents expected and if all intents are equivalent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800549 *
550 * @param intents the list of intents expected
551 */
552 private void checkIntents(List<Intent> intents) {
Luca Prete092e8952016-10-26 16:25:56 +0200553 assertEquals("The number of intents submitted differs from the number" +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100554 " of intents expected. ",
Luca Prete092e8952016-10-26 16:25:56 +0200555 intents.size(), intentService.getIntentCount());
Luca Prete9c2ee072016-02-16 11:00:44 -0800556
557 for (Intent intentOne : intents) {
558 boolean found = false;
559 for (Intent intentTwo : intentService.getIntents()) {
560 if (intentOne.key().equals(intentTwo.key())) {
561 found = true;
Luca Prete092e8952016-10-26 16:25:56 +0200562 assertTrue(format("The intent submitted is different from" +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100563 " the intent expected. %s %s",
Luca Prete092e8952016-10-26 16:25:56 +0200564 intentOne, intentTwo),
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100565 IntentUtils.intentsAreEqual(intentOne, intentTwo));
Luca Prete9c2ee072016-02-16 11:00:44 -0800566 break;
567 }
568 }
Luca Prete092e8952016-10-26 16:25:56 +0200569 assertTrue("The intent submitted is not equal to any of the expected" +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100570 " intents. ", found);
Luca Prete9c2ee072016-02-16 11:00:44 -0800571 }
572 }
573
574 /**
nosignal5fd282e2016-09-16 16:11:40 -0700575 * Builds a broadcast intent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800576 *
nosignal5fd282e2016-09-16 16:11:40 -0700577 * @param key the key to identify the intent
578 * @param src the ingress connect point
579 * @param dsts the egress connect points
580 * @return the generated single-point to multi-point intent
Luca Prete9c2ee072016-02-16 11:00:44 -0800581 */
582 private SinglePointToMultiPointIntent buildBrcIntent(Key key,
Luca Prete092e8952016-10-26 16:25:56 +0200583 FilteredConnectPoint src,
584 Set<FilteredConnectPoint> dsts,
585 EncapsulationType encap) {
586 SinglePointToMultiPointIntent.Builder intentBuilder;
Luca Prete9c2ee072016-02-16 11:00:44 -0800587
Luca Prete9c2ee072016-02-16 11:00:44 -0800588 TrafficSelector selector = DefaultTrafficSelector.builder()
589 .matchEthDst(MacAddress.BROADCAST)
Luca Prete9c2ee072016-02-16 11:00:44 -0800590 .build();
591
Luca Prete092e8952016-10-26 16:25:56 +0200592 intentBuilder = SinglePointToMultiPointIntent.builder()
Luca Prete9c2ee072016-02-16 11:00:44 -0800593 .appId(APPID)
594 .key(key)
595 .selector(selector)
nosignal5fd282e2016-09-16 16:11:40 -0700596 .filteredIngressPoint(src)
597 .filteredEgressPoints(dsts)
Luca Prete13465642017-01-04 10:31:48 -0800598 .constraints(PARTIAL_FAILURE_CONSTRAINT)
Luca Prete092e8952016-10-26 16:25:56 +0200599 .priority(PRIORITY_OFFSET);
600
Luca Prete13465642017-01-04 10:31:48 -0800601 setEncap(intentBuilder, PARTIAL_FAILURE_CONSTRAINT, encap);
Luca Prete092e8952016-10-26 16:25:56 +0200602
603 return intentBuilder.build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800604 }
605
606 /**
nosignal5fd282e2016-09-16 16:11:40 -0700607 * Builds a unicast intent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800608 *
nosignal5fd282e2016-09-16 16:11:40 -0700609 * @param key the key to identify the intent
610 * @param srcs the ingress connect points
611 * @param dst the egress connect point
612 * @param host the destination Host
613 * @return the generated multi-point to single-point intent
Luca Prete9c2ee072016-02-16 11:00:44 -0800614 */
615 private MultiPointToSinglePointIntent buildUniIntent(Key key,
Luca Prete092e8952016-10-26 16:25:56 +0200616 Set<FilteredConnectPoint> srcs,
617 FilteredConnectPoint dst,
618 Host host,
619 EncapsulationType encap) {
620 MultiPointToSinglePointIntent.Builder intentBuilder;
621
nosignal5fd282e2016-09-16 16:11:40 -0700622 TrafficSelector selector = DefaultTrafficSelector.builder()
623 .matchEthDst(host.mac())
624 .build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800625
Luca Prete092e8952016-10-26 16:25:56 +0200626 intentBuilder = MultiPointToSinglePointIntent.builder()
Luca Prete9c2ee072016-02-16 11:00:44 -0800627 .appId(APPID)
628 .key(key)
629 .selector(selector)
nosignal5fd282e2016-09-16 16:11:40 -0700630 .filteredIngressPoints(srcs)
631 .filteredEgressPoint(dst)
Luca Prete13465642017-01-04 10:31:48 -0800632 .constraints(PARTIAL_FAILURE_CONSTRAINT)
Luca Prete092e8952016-10-26 16:25:56 +0200633 .priority(PRIORITY_OFFSET);
nosignal5fd282e2016-09-16 16:11:40 -0700634
Luca Prete13465642017-01-04 10:31:48 -0800635 setEncap(intentBuilder, PARTIAL_FAILURE_CONSTRAINT, encap);
Luca Prete092e8952016-10-26 16:25:56 +0200636
637 return intentBuilder.build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800638 }
639
640 /**
nosignal5fd282e2016-09-16 16:11:40 -0700641 * Returns the filtered connect point associated to a given host.
Luca Prete9c2ee072016-02-16 11:00:44 -0800642 *
nosignal5fd282e2016-09-16 16:11:40 -0700643 * @param host the target host
644 * @param fcps the filtered connected points
645 * @return the filtered connect point associated to the given host; null
646 * otherwise
647 */
648 private FilteredConnectPoint getHostPoint(Host host,
649 Set<FilteredConnectPoint> fcps) {
650 return fcps.stream()
651 .filter(fcp -> fcp.connectPoint().equals(host.location()))
652 .filter(fcp -> {
653 VlanIdCriterion vlanCriterion =
654 (VlanIdCriterion) fcp.trafficSelector().
655 getCriterion(Criterion.Type.VLAN_VID);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100656 return vlanCriterion == null ||
nosignal5fd282e2016-09-16 16:11:40 -0700657 vlanCriterion.vlanId().equals(host.vlan());
658 })
659 .findFirst()
660 .orElse(null);
661 }
662
663 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100664 * Computes a set of filtered connect points from a list of given interfaces.
nosignal5fd282e2016-09-16 16:11:40 -0700665 *
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100666 * @param interfaces the interfaces to compute
nosignal5fd282e2016-09-16 16:11:40 -0700667 * @return the set of filtered connect points
668 */
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100669 private Set<FilteredConnectPoint> buildFCPoints(Collection<Interface> interfaces) {
670 // Build all filtered connected points in the VPLS
nosignal5fd282e2016-09-16 16:11:40 -0700671 return interfaces
672 .stream()
673 .map(intf -> {
674 TrafficSelector.Builder selectorBuilder =
675 DefaultTrafficSelector.builder();
nosignal5fd282e2016-09-16 16:11:40 -0700676 if (!intf.vlan().equals(VlanId.NONE)) {
677 selectorBuilder.matchVlanId(intf.vlan());
678 }
nosignal5fd282e2016-09-16 16:11:40 -0700679 return new FilteredConnectPoint(intf.connectPoint(),
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100680 selectorBuilder.build());
nosignal5fd282e2016-09-16 16:11:40 -0700681 })
682 .collect(Collectors.toSet());
683 }
684
685 /**
686 * Builds an intent Key either for a single-point to multi-point or
687 * multi-point to single-point intent, based on a prefix that defines
688 * the intent type, the connection point representing the source or the
Luca Prete092e8952016-10-26 16:25:56 +0200689 * destination and the VLAN Id representing the VPLS.
nosignal5fd282e2016-09-16 16:11:40 -0700690 *
691 * @param prefix the key prefix
692 * @param cPoint the ingress/egress connect point
Luca Prete092e8952016-10-26 16:25:56 +0200693 * @param vplsName the VPLS name
nosignal5fd282e2016-09-16 16:11:40 -0700694 * @param hostMac the ingress/egress MAC address
695 * @return the key to identify the intent
696 */
697 private Key buildKey(String prefix,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100698 ConnectPoint cPoint,
699 String vplsName,
700 MacAddress hostMac) {
Luca Prete092e8952016-10-26 16:25:56 +0200701 String keyString = vplsName +
nosignal5fd282e2016-09-16 16:11:40 -0700702 DASH +
703 prefix +
704 DASH +
705 cPoint.deviceId() +
706 DASH +
707 cPoint.port() +
708 DASH +
709 hostMac;
710
711 return Key.of(keyString, APPID);
712 }
713
714 /**
715 * Returns the device Id of the ith device.
716 *
717 * @param i the device to get the Id of
718 * @return the device Id
Luca Prete9c2ee072016-02-16 11:00:44 -0800719 */
720 private static DeviceId getDeviceId(int i) {
721 return DeviceId.deviceId("" + i);
722 }
723
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100724 private static MacAddress getMac(int n) {
725 return MacAddress.valueOf(String.format("00:00:00:00:00:%s", n));
726 }
727
Luca Prete9c2ee072016-02-16 11:00:44 -0800728 private static HostLocation getLocation(int i) {
729 return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L);
730 }
731
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100732 private static HostLocation getLocation(int d, int p) {
733 return new HostLocation(new ConnectPoint(getDeviceId(d),
734 PortNumber.portNumber(p)), 123L);
735 }
736
Luca Prete9c2ee072016-02-16 11:00:44 -0800737 /**
nosignal5fd282e2016-09-16 16:11:40 -0700738 * Represents a fake IntentService class that allows to store and retrieve
739 * intents without implementing the IntentService logic.
Luca Prete9c2ee072016-02-16 11:00:44 -0800740 */
741 private class TestIntentService extends IntentServiceAdapter {
742
Luca Pretec21c6e62016-09-22 14:52:21 -0700743 private Map<Key, Intent> intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800744
745 public TestIntentService() {
Luca Pretec21c6e62016-09-22 14:52:21 -0700746 intents = Maps.newHashMap();
Luca Prete9c2ee072016-02-16 11:00:44 -0800747 }
748
749 @Override
750 public void submit(Intent intent) {
Luca Pretec21c6e62016-09-22 14:52:21 -0700751 intents.put(intent.key(), intent);
Luca Prete9c2ee072016-02-16 11:00:44 -0800752 }
753
754 @Override
755 public long getIntentCount() {
756 return intents.size();
757 }
758
759 @Override
760 public Iterable<Intent> getIntents() {
Luca Pretec21c6e62016-09-22 14:52:21 -0700761 return intents.values();
Luca Prete9c2ee072016-02-16 11:00:44 -0800762 }
763
764 @Override
765 public Intent getIntent(Key intentKey) {
Luca Pretec21c6e62016-09-22 14:52:21 -0700766 for (Intent intent : intents.values()) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800767 if (intent.key().equals(intentKey)) {
768 return intent;
769 }
770 }
771 return null;
772 }
773 }
774
775 /**
776 * Represents a fake HostService class which allows to add hosts manually
777 * in each test, when needed.
778 */
779 private class TestHostService extends HostServiceAdapter {
780
781 private Set<Host> hosts;
782
783 public TestHostService(Set<Host> hosts) {
784 this.hosts = hosts;
785 }
786
787 @Override
788 public void addListener(HostListener listener) {
789 VplsTest.this.hostListener = listener;
790 }
791
792 @Override
793 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
794 return hosts.stream()
nosignal5fd282e2016-09-16 16:11:40 -0700795 .filter(h -> h.location().equals(connectPoint))
Luca Prete9c2ee072016-02-16 11:00:44 -0800796 .collect(Collectors.toSet());
797 }
798
799 }
800
nosignal5fd282e2016-09-16 16:11:40 -0700801 /**
802 * Represents a fake IdGenerator class for intents.
803 */
Luca Prete9c2ee072016-02-16 11:00:44 -0800804 private static class TestIdGenerator implements IdGenerator {
805
806 private final AtomicLong id = new AtomicLong(0);
807
808 @Override
809 public long getNewId() {
810 return id.getAndIncrement();
811 }
812
813 }
814
815 /**
816 * Test IntentSynchronizer that passes all intents straight through to the
817 * intent service.
818 */
Jonathan Hart470ed4f2017-01-31 16:52:28 -0800819 private class TestIntentSynchronizer implements IntentSynchronizationService {
Luca Prete9c2ee072016-02-16 11:00:44 -0800820
821 private final IntentService intentService;
822
823 /**
nosignal5fd282e2016-09-16 16:11:40 -0700824 * Creates a new intent test synchronizer.
Luca Prete9c2ee072016-02-16 11:00:44 -0800825 *
826 * @param intentService intent service
827 */
828 public TestIntentSynchronizer(IntentService intentService) {
829 this.intentService = intentService;
830 }
831
832 @Override
833 public void submit(Intent intent) {
834 intentService.submit(intent);
835 }
836
837 @Override
838 public void withdraw(Intent intent) {
839 intentService.withdraw(intent);
840 }
841
842 @Override
Luca Prete2705d662016-04-29 15:30:23 -0700843 public void removeIntentsByAppId(ApplicationId applicationId) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800844 }
845 }
846
nosignal5fd282e2016-09-16 16:11:40 -0700847 /**
848 * Represents a fake VplsConfigService class which is needed for testing.
849 */
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100850 private class TestVplsConfigService extends VplsConfigServiceAdapter {
nosignal5fd282e2016-09-16 16:11:40 -0700851
Luca Prete092e8952016-10-26 16:25:56 +0200852 private final SetMultimap<String, Interface> ifacesByVplsName;
853 private final Map<String, EncapsulationType> encapsByVplsName;
854
nosignal5fd282e2016-09-16 16:11:40 -0700855 private Set<String> vplsAffectByApi = new HashSet<>();
856
Luca Prete092e8952016-10-26 16:25:56 +0200857 TestVplsConfigService(SetMultimap<String, Interface> ifacesByVplsName,
858 Map<String, EncapsulationType> encapsByVplsName) {
859 this.ifacesByVplsName = ifacesByVplsName;
860 this.encapsByVplsName = encapsByVplsName;
nosignal5fd282e2016-09-16 16:11:40 -0700861 }
862
863 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200864 public void addVpls(String vplsName, Set<String> ifaceNames, String encap) {
865 if (!ifacesByVplsName.containsKey(vplsName)) {
866 ifaceNames.forEach(ifaceName -> {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100867 AVAILABLE_INTERFACES.forEach(iface -> {
Luca Prete092e8952016-10-26 16:25:56 +0200868 if (iface.name().equals(ifaceName)) {
869 ifacesByVplsName.put(vplsName, iface);
nosignal5fd282e2016-09-16 16:11:40 -0700870 }
871 });
872 });
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100873 }
874 if (!ifacesByVplsName.containsKey(vplsName)) {
Luca Prete092e8952016-10-26 16:25:56 +0200875 encapsByVplsName.put(vplsName, valueOf(encap));
nosignal5fd282e2016-09-16 16:11:40 -0700876 }
877 }
878
879 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200880 public void removeVpls(String vplsName) {
881 if (ifacesByVplsName.containsKey(vplsName)) {
882 ifacesByVplsName.removeAll(vplsName);
nosignal5fd282e2016-09-16 16:11:40 -0700883 }
884 }
885
886 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200887 public void addIface(String vplsName, String iface) {
888 if (!ifacesByVplsName.containsKey(vplsName)) {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100889 AVAILABLE_INTERFACES.forEach(intf -> {
nosignal5fd282e2016-09-16 16:11:40 -0700890 if (intf.name().equals(iface)) {
Luca Prete092e8952016-10-26 16:25:56 +0200891 ifacesByVplsName.put(vplsName, intf);
nosignal5fd282e2016-09-16 16:11:40 -0700892 }
893 });
894 }
895 }
896
897 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200898 public void setEncap(String vplsName, String encap) {
899 encapsByVplsName.put(vplsName, EncapsulationType.enumFromString(encap));
900 }
901
902 @Override
903 public void removeIface(String iface) {
904 SetMultimap<String, Interface> search = HashMultimap.create(ifacesByVplsName);
nosignal5fd282e2016-09-16 16:11:40 -0700905 search.entries().forEach(e -> {
906 if (e.getValue().name().equals(iface)) {
Luca Prete092e8952016-10-26 16:25:56 +0200907 ifacesByVplsName.remove(e.getKey(), iface);
nosignal5fd282e2016-09-16 16:11:40 -0700908 }
909 });
910 }
911
912 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200913 public void cleanVplsConfig() {
914 ifacesByVplsName.clear();
nosignal5fd282e2016-09-16 16:11:40 -0700915 }
916
917 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200918 public EncapsulationType encap(String vplsName) {
919 EncapsulationType encap = null;
920 if (encapsByVplsName.containsKey(vplsName)) {
921 encap = encapsByVplsName.get(vplsName);
922 }
923 return encap;
924 }
925
926 @Override
927 public Set<String> vplsAffectedByApi() {
nosignal5fd282e2016-09-16 16:11:40 -0700928 Set<String> vplsNames = ImmutableSet.copyOf(vplsAffectByApi);
929
930 vplsAffectByApi.clear();
931
932 return vplsNames;
933 }
934
935 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200936 public Set<Interface> allIfaces() {
937 return ifacesByVplsName.values()
nosignal5fd282e2016-09-16 16:11:40 -0700938 .stream()
939 .collect(Collectors.toSet());
940 }
941
942 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200943 public Set<Interface> ifaces(String name) {
944 return ifacesByVplsName.get(name)
nosignal5fd282e2016-09-16 16:11:40 -0700945 .stream()
946 .collect(Collectors.toSet());
947 }
948
949 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200950 public Set<String> vplsNames() {
951 return ifacesByVplsName.keySet();
nosignal5fd282e2016-09-16 16:11:40 -0700952 }
953
954 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200955 public Set<String> vplsNamesOld() {
956 return ifacesByVplsName.keySet();
957 }
958
959 public SetMultimap<String, Interface> ifacesByVplsName() {
960 return ImmutableSetMultimap.copyOf(ifacesByVplsName);
nosignal5fd282e2016-09-16 16:11:40 -0700961 }
962
963 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200964 public SetMultimap<String, Interface> ifacesByVplsName(VlanId vlan,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100965 ConnectPoint connectPoint) {
Luca Prete092e8952016-10-26 16:25:56 +0200966 String vplsName =
967 ifacesByVplsName.entries().stream()
nosignal5fd282e2016-09-16 16:11:40 -0700968 .filter(e -> e.getValue().connectPoint().equals(connectPoint))
969 .filter(e -> e.getValue().vlan().equals(vlan))
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100970 .map(Map.Entry::getKey)
nosignal5fd282e2016-09-16 16:11:40 -0700971 .findFirst()
972 .orElse(null);
973 SetMultimap<String, Interface> result = HashMultimap.create();
Luca Prete092e8952016-10-26 16:25:56 +0200974 if (vplsName != null && ifacesByVplsName.containsKey(vplsName)) {
975 ifacesByVplsName.get(vplsName)
976 .forEach(intf -> result.put(vplsName, intf));
nosignal5fd282e2016-09-16 16:11:40 -0700977 return result;
978 }
979 return null;
980 }
nosignal5fd282e2016-09-16 16:11:40 -0700981 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800982}