blob: bc1c3f28227d5a3ef25e96aad3439ecd2fe71feb [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
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +010018import java.util.Collection;
Ray Milkey6e968eb2016-06-29 09:37:27 -070019import java.util.Collections;
Luca Prete092e8952016-10-26 16:25:56 +020020import java.util.HashMap;
nosignal5fd282e2016-09-16 16:11:40 -070021import java.util.HashSet;
Ray Milkey6e968eb2016-06-29 09:37:27 -070022import java.util.List;
Luca Pretec21c6e62016-09-22 14:52:21 -070023import java.util.Map;
Ray Milkey6e968eb2016-06-29 09:37:27 -070024import java.util.Set;
25import java.util.concurrent.atomic.AtomicLong;
26import java.util.stream.Collectors;
27
Luca Prete092e8952016-10-26 16:25:56 +020028import com.google.common.collect.ImmutableList;
nosignal5fd282e2016-09-16 16:11:40 -070029import com.google.common.collect.Sets;
30import com.google.common.collect.ImmutableSet;
31import com.google.common.collect.SetMultimap;
32import com.google.common.collect.HashMultimap;
Luca Pretec21c6e62016-09-22 14:52:21 -070033import com.google.common.collect.Maps;
nosignal5fd282e2016-09-16 16:11:40 -070034import com.google.common.collect.Lists;
35import com.google.common.collect.ImmutableSetMultimap;
Ray Milkey6e968eb2016-06-29 09:37:27 -070036import org.junit.After;
Luca Prete9c2ee072016-02-16 11:00:44 -080037import org.junit.Before;
Luca Prete9c2ee072016-02-16 11:00:44 -080038import org.junit.Test;
39import org.onlab.packet.Ip4Address;
40import org.onlab.packet.MacAddress;
41import org.onlab.packet.VlanId;
42import org.onosproject.TestApplicationId;
43import org.onosproject.app.ApplicationService;
44import org.onosproject.core.ApplicationId;
45import org.onosproject.core.CoreService;
46import org.onosproject.core.IdGenerator;
47import org.onosproject.incubator.net.intf.Interface;
Luca Pretea8854822016-04-26 16:30:55 -070048import org.onosproject.incubator.net.intf.InterfaceListener;
Luca Prete9c2ee072016-02-16 11:00:44 -080049import org.onosproject.incubator.net.intf.InterfaceService;
50import org.onosproject.net.ConnectPoint;
51import org.onosproject.net.DefaultHost;
52import org.onosproject.net.DeviceId;
Luca Prete092e8952016-10-26 16:25:56 +020053import org.onosproject.net.EncapsulationType;
Luca Prete9c2ee072016-02-16 11:00:44 -080054import org.onosproject.net.Host;
55import org.onosproject.net.HostId;
56import org.onosproject.net.HostLocation;
57import org.onosproject.net.PortNumber;
nosignal5fd282e2016-09-16 16:11:40 -070058import org.onosproject.net.FilteredConnectPoint;
59import org.onosproject.net.config.NetworkConfigService;
Luca Prete9c2ee072016-02-16 11:00:44 -080060import org.onosproject.net.flow.DefaultTrafficSelector;
Luca Prete9c2ee072016-02-16 11:00:44 -080061import org.onosproject.net.flow.TrafficSelector;
nosignal5fd282e2016-09-16 16:11:40 -070062import org.onosproject.net.flow.criteria.Criterion;
63import org.onosproject.net.flow.criteria.VlanIdCriterion;
Luca Prete9c2ee072016-02-16 11:00:44 -080064import org.onosproject.net.host.HostEvent;
65import org.onosproject.net.host.HostListener;
66import org.onosproject.net.host.HostService;
67import org.onosproject.net.host.HostServiceAdapter;
Luca Prete092e8952016-10-26 16:25:56 +020068import org.onosproject.net.intent.ConnectivityIntent;
Luca Prete9c2ee072016-02-16 11:00:44 -080069import org.onosproject.net.intent.Intent;
70import org.onosproject.net.intent.IntentService;
71import org.onosproject.net.intent.IntentServiceAdapter;
72import org.onosproject.net.intent.IntentUtils;
73import org.onosproject.net.intent.Key;
74import org.onosproject.net.intent.MultiPointToSinglePointIntent;
75import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Luca Prete092e8952016-10-26 16:25:56 +020076import org.onosproject.net.intent.constraint.EncapsulationConstraint;
Luca Prete9c2ee072016-02-16 11:00:44 -080077import org.onosproject.net.provider.ProviderId;
78import org.onosproject.routing.IntentSynchronizationAdminService;
79import org.onosproject.routing.IntentSynchronizationService;
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +010080import org.onosproject.vpls.config.VplsConfigService;
Luca Prete9c2ee072016-02-16 11:00:44 -080081
82import static java.lang.String.format;
Ray Milkey6e968eb2016-06-29 09:37:27 -070083import static org.easymock.EasyMock.anyObject;
84import static org.easymock.EasyMock.createMock;
85import static org.easymock.EasyMock.expect;
Luca Pretea8854822016-04-26 16:30:55 -070086import static org.easymock.EasyMock.expectLastCall;
Ray Milkey6e968eb2016-06-29 09:37:27 -070087import static org.easymock.EasyMock.replay;
Luca Prete9c2ee072016-02-16 11:00:44 -080088import static org.junit.Assert.assertEquals;
89import static org.junit.Assert.assertTrue;
90
Luca Prete092e8952016-10-26 16:25:56 +020091import static org.onosproject.net.EncapsulationType.*;
nosignal5fd282e2016-09-16 16:11:40 -070092import static org.onosproject.vpls.IntentInstaller.PREFIX_BROADCAST;
93import static org.onosproject.vpls.IntentInstaller.PREFIX_UNICAST;
94
Luca Prete9c2ee072016-02-16 11:00:44 -080095/**
96 * Tests for the {@link Vpls} class.
97 */
98public class VplsTest {
nosignal5fd282e2016-09-16 16:11:40 -070099 private static final String APP_NAME = "org.onosproject.vpls";
100 private static final ApplicationId APPID = TestApplicationId.create(APP_NAME);
101 private static final String DASH = "-";
102 private static final int PRIORITY_OFFSET = 1000;
Luca Prete092e8952016-10-26 16:25:56 +0200103 private static final String VPLS1 = "vpls1";
104 private static final String VPLS2 = "vpls2";
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100105 private static final String VPLS3 = "vpls3";
106 private static final String VPLS4 = "vpls4";
Luca Prete9c2ee072016-02-16 11:00:44 -0800107
108 private static final PortNumber P1 = PortNumber.portNumber(1);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100109 private static final PortNumber P2 = PortNumber.portNumber(2);
Luca Prete9c2ee072016-02-16 11:00:44 -0800110
Luca Prete9c2ee072016-02-16 11:00:44 -0800111 private static final DeviceId DID1 = getDeviceId(1);
112 private static final DeviceId DID2 = getDeviceId(2);
113 private static final DeviceId DID3 = getDeviceId(3);
114 private static final DeviceId DID4 = getDeviceId(4);
115 private static final DeviceId DID5 = getDeviceId(5);
116 private static final DeviceId DID6 = getDeviceId(6);
117
nosignal5fd282e2016-09-16 16:11:40 -0700118 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
119 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P1);
120 private static final ConnectPoint CP3 = new ConnectPoint(DID3, P1);
121 private static final ConnectPoint CP4 = new ConnectPoint(DID4, P1);
122 private static final ConnectPoint CP5 = new ConnectPoint(DID5, P1);
123 private static final ConnectPoint CP6 = new ConnectPoint(DID6, P1);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100124 private static final ConnectPoint CP7 = new ConnectPoint(DID4, P2);
125 private static final ConnectPoint CP8 = new ConnectPoint(DID3, P2);
126 private static final ConnectPoint CP9 = new ConnectPoint(DID5, P1);
127 private static final ConnectPoint CP10 = new ConnectPoint(DID5, P2);
Luca Prete9c2ee072016-02-16 11:00:44 -0800128
nosignal5fd282e2016-09-16 16:11:40 -0700129 private static final VlanId VLAN100 = VlanId.vlanId((short) 100);
130 private static final VlanId VLAN200 = VlanId.vlanId((short) 200);
131 private static final VlanId VLAN300 = VlanId.vlanId((short) 300);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100132 private static final VlanId VLAN400 = VlanId.vlanId((short) 400);
133 private static final VlanId VLAN_NONE = VlanId.NONE;
Luca Prete9c2ee072016-02-16 11:00:44 -0800134
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100135 private static final MacAddress MAC1 = getMac(1);
136 private static final MacAddress MAC2 = getMac(2);
137 private static final MacAddress MAC3 = getMac(3);
138 private static final MacAddress MAC4 = getMac(4);
139 private static final MacAddress MAC5 = getMac(5);
140 private static final MacAddress MAC6 = getMac(6);
141 private static final MacAddress MAC7 = getMac(7);
142 private static final MacAddress MAC8 = getMac(8);
143 private static final MacAddress MAC9 = getMac(9);
144 private static final MacAddress MAC10 = getMac(10);
145 private static final MacAddress MAC11 = getMac(11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800146
nosignal5fd282e2016-09-16 16:11:40 -0700147 private static final Ip4Address IP1 = Ip4Address.valueOf("192.168.1.1");
148 private static final Ip4Address IP2 = Ip4Address.valueOf("192.168.1.2");
149
150 private static final HostId HID1 = HostId.hostId(MAC1, VLAN100);
151 private static final HostId HID2 = HostId.hostId(MAC2, VLAN100);
152 private static final HostId HID3 = HostId.hostId(MAC3, VLAN200);
153 private static final HostId HID4 = HostId.hostId(MAC4, VLAN200);
154 private static final HostId HID5 = HostId.hostId(MAC5, VLAN300);
155 private static final HostId HID6 = HostId.hostId(MAC6, VLAN300);
156 private static final HostId HID7 = HostId.hostId(MAC7, VLAN300);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100157 private static final HostId HID8 = HostId.hostId(MAC8, VLAN400);
158 private static final HostId HID9 = HostId.hostId(MAC9);
159 private static final HostId HID10 = HostId.hostId(MAC10);
160 private static final HostId HID11 = HostId.hostId(MAC11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800161
162 private static final ProviderId PID = new ProviderId("of", "foo");
163
Ray Milkey6e968eb2016-06-29 09:37:27 -0700164 private static IdGenerator idGenerator;
Luca Prete9c2ee072016-02-16 11:00:44 -0800165
Luca Prete092e8952016-10-26 16:25:56 +0200166 private static final Interface V100H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700167 new Interface("v100h1", CP1, null, null, VLAN100);
Luca Prete092e8952016-10-26 16:25:56 +0200168 private static final Interface V100H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700169 new Interface("v100h2", CP2, null, null, VLAN100);
Luca Prete092e8952016-10-26 16:25:56 +0200170 private static final Interface V200H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700171 new Interface("v200h1", CP3, null, null, VLAN200);
Luca Prete092e8952016-10-26 16:25:56 +0200172 private static final Interface V200H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700173 new Interface("v200h2", CP4, null, null, VLAN200);
Luca Prete092e8952016-10-26 16:25:56 +0200174 private static final Interface V300H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700175 new Interface("v300h1", CP5, null, null, VLAN300);
Luca Prete092e8952016-10-26 16:25:56 +0200176 private static final Interface V300H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700177 new Interface("v300h2", CP6, null, null, VLAN300);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100178 private static final Interface V400H1 =
179 new Interface("v400h1", CP7, null, null, VLAN400);
180
181 private static final Interface VNONEH1 =
182 new Interface("vNoneh1", CP8, null, null, VLAN_NONE);
183 private static final Interface VNONEH2 =
184 new Interface("vNoneh2", CP9, null, null, VLAN_NONE);
185 private static final Interface VNONEH3 =
186 new Interface("vNoneh3", CP10, null, null, VLAN_NONE);
nosignal5fd282e2016-09-16 16:11:40 -0700187
Luca Prete092e8952016-10-26 16:25:56 +0200188 private static final Host V100HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700189 new DefaultHost(PID, HID1, MAC1, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100190 getLocation(1), Collections.singleton(IP1));
Luca Prete092e8952016-10-26 16:25:56 +0200191 private static final Host V100HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700192 new DefaultHost(PID, HID2, MAC2, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100193 getLocation(2), Sets.newHashSet());
Luca Prete092e8952016-10-26 16:25:56 +0200194 private static final Host V200HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700195 new DefaultHost(PID, HID3, MAC3, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100196 getLocation(3), Collections.singleton(IP2));
Luca Prete092e8952016-10-26 16:25:56 +0200197 private static final Host V200HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700198 new DefaultHost(PID, HID4, MAC4, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100199 getLocation(4), Sets.newHashSet());
Luca Prete092e8952016-10-26 16:25:56 +0200200 private static final Host V300HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700201 new DefaultHost(PID, HID5, MAC5, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100202 getLocation(5), Sets.newHashSet());
Luca Prete092e8952016-10-26 16:25:56 +0200203 private static final Host V300HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700204 new DefaultHost(PID, HID6, MAC6, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100205 getLocation(6), Sets.newHashSet());
Luca Prete092e8952016-10-26 16:25:56 +0200206 private static final Host V300HOST3 =
nosignal5fd282e2016-09-16 16:11:40 -0700207 new DefaultHost(PID, HID7, MAC7, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100208 getLocation(7), Sets.newHashSet());
209 private static final Host V400HOST1 =
210 new DefaultHost(PID, HID8, MAC8, VLAN400,
211 getLocation(4, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700212
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100213 private static final Host VNONEHOST1 =
214 new DefaultHost(PID, HID9, MAC9, VlanId.NONE,
215 getLocation(3, 2), Sets.newHashSet());
216 private static final Host VNONEHOST2 =
217 new DefaultHost(PID, HID10, MAC10, VlanId.NONE,
218 getLocation(5, 1), Sets.newHashSet());
219 private static final Host VNONEHOST3 =
220 new DefaultHost(PID, HID11, MAC11, VlanId.NONE,
221 getLocation(5, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700222
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100223 private static final Set<Interface> AVAILABLE_INTERFACES =
224 ImmutableSet.of(V100H1, V100H2, V200H1, V200H2, V300H1, V300H2,
225 V400H1, VNONEH1, VNONEH2);
226
227 private static final Set<Host> AVAILABLE_HOSTS =
Luca Prete092e8952016-10-26 16:25:56 +0200228 ImmutableSet.of(V100HOST1, V100HOST2, V200HOST1,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100229 V200HOST2, V300HOST1, V300HOST2, V300HOST3,
230 VNONEHOST1, VNONEHOST2,
231 V400HOST1, VNONEHOST3);
232
233 private SetMultimap<String, Interface> interfacesByVpls = HashMultimap.create();
nosignal5fd282e2016-09-16 16:11:40 -0700234
235 private ApplicationService applicationService;
236 private CoreService coreService;
237 private HostListener hostListener;
238 private NetworkConfigService configService;
239 private Set<Host> hostsAvailable;
240 private HostService hostService;
241 private IntentService intentService;
242 private InterfaceService interfaceService;
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100243 private VplsConfigService vplsConfigService;
nosignal5fd282e2016-09-16 16:11:40 -0700244 private Vpls vpls;
245
Luca Prete9c2ee072016-02-16 11:00:44 -0800246 @Before
247 public void setUp() throws Exception {
Ray Milkey6e968eb2016-06-29 09:37:27 -0700248 idGenerator = new TestIdGenerator();
249 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,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100506 point.connectPoint(), name, MacAddress.BROADCAST);
nosignal5fd282e2016-09-16 16:11:40 -0700507
Luca Prete092e8952016-10-26 16:25:56 +0200508 intents.add(buildBrcIntent(brckey, point, otherPoints, encap));
Luca Prete9c2ee072016-02-16 11:00:44 -0800509 });
510
nosignal5fd282e2016-09-16 16:11:40 -0700511 return intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800512 }
513
514 /**
nosignal5fd282e2016-09-16 16:11:40 -0700515 * Generates a list of expected mp2sp intents for a given VPLS.
516 *
517 * @param fcPoints the filtered connect point
518 * @param hosts the hosts
519 * @param name the name of the VPLS
Luca Prete092e8952016-10-26 16:25:56 +0200520 * @param encap the encapsulation type
nosignal5fd282e2016-09-16 16:11:40 -0700521 * @return the list of expected mp2sp intents for the given VPLS
522 */
523 private List<MultiPointToSinglePointIntent>
Luca Prete092e8952016-10-26 16:25:56 +0200524 generateVplsUni(Set<FilteredConnectPoint> fcPoints, Set<Host> hosts,
525 String name, EncapsulationType encap) {
nosignal5fd282e2016-09-16 16:11:40 -0700526 List<MultiPointToSinglePointIntent> intents = Lists.newArrayList();
527
528 hosts.forEach(host -> {
529 FilteredConnectPoint hostPoint = getHostPoint(host, fcPoints);
530
531 Set<FilteredConnectPoint> otherPoints =
532 fcPoints.stream()
533 .filter(fcp -> !fcp.equals(hostPoint))
534 .collect(Collectors.toSet());
535
536 Key uniKey = buildKey(PREFIX_UNICAST,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100537 host.location(), name, host.mac());
nosignal5fd282e2016-09-16 16:11:40 -0700538
Luca Prete092e8952016-10-26 16:25:56 +0200539 intents.add(buildUniIntent(uniKey, otherPoints, hostPoint, host, encap));
nosignal5fd282e2016-09-16 16:11:40 -0700540 });
541
542 return intents;
543 }
544
545 /**
546 * Checks if the number of intents submitted to the intent framework is equal
547 * to the number of intents expected and if all intents are equivalent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800548 *
549 * @param intents the list of intents expected
550 */
551 private void checkIntents(List<Intent> intents) {
Luca Prete092e8952016-10-26 16:25:56 +0200552 assertEquals("The number of intents submitted differs from the number" +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100553 " of intents expected. ",
Luca Prete092e8952016-10-26 16:25:56 +0200554 intents.size(), intentService.getIntentCount());
Luca Prete9c2ee072016-02-16 11:00:44 -0800555
556 for (Intent intentOne : intents) {
557 boolean found = false;
558 for (Intent intentTwo : intentService.getIntents()) {
559 if (intentOne.key().equals(intentTwo.key())) {
560 found = true;
Luca Prete092e8952016-10-26 16:25:56 +0200561 assertTrue(format("The intent submitted is different from" +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100562 " the intent expected. %s %s",
Luca Prete092e8952016-10-26 16:25:56 +0200563 intentOne, intentTwo),
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100564 IntentUtils.intentsAreEqual(intentOne, intentTwo));
Luca Prete9c2ee072016-02-16 11:00:44 -0800565 break;
566 }
567 }
Luca Prete092e8952016-10-26 16:25:56 +0200568 assertTrue("The intent submitted is not equal to any of the expected" +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100569 " intents. ", found);
Luca Prete9c2ee072016-02-16 11:00:44 -0800570 }
571 }
572
573 /**
nosignal5fd282e2016-09-16 16:11:40 -0700574 * Builds a broadcast intent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800575 *
nosignal5fd282e2016-09-16 16:11:40 -0700576 * @param key the key to identify the intent
577 * @param src the ingress connect point
578 * @param dsts the egress connect points
579 * @return the generated single-point to multi-point intent
Luca Prete9c2ee072016-02-16 11:00:44 -0800580 */
581 private SinglePointToMultiPointIntent buildBrcIntent(Key key,
Luca Prete092e8952016-10-26 16:25:56 +0200582 FilteredConnectPoint src,
583 Set<FilteredConnectPoint> dsts,
584 EncapsulationType encap) {
585 SinglePointToMultiPointIntent.Builder intentBuilder;
Luca Prete9c2ee072016-02-16 11:00:44 -0800586
Luca Prete9c2ee072016-02-16 11:00:44 -0800587 TrafficSelector selector = DefaultTrafficSelector.builder()
588 .matchEthDst(MacAddress.BROADCAST)
Luca Prete9c2ee072016-02-16 11:00:44 -0800589 .build();
590
Luca Prete092e8952016-10-26 16:25:56 +0200591 intentBuilder = SinglePointToMultiPointIntent.builder()
Luca Prete9c2ee072016-02-16 11:00:44 -0800592 .appId(APPID)
593 .key(key)
594 .selector(selector)
nosignal5fd282e2016-09-16 16:11:40 -0700595 .filteredIngressPoint(src)
596 .filteredEgressPoints(dsts)
Luca Prete092e8952016-10-26 16:25:56 +0200597 .priority(PRIORITY_OFFSET);
598
599 encap(intentBuilder, encap);
600
601 return intentBuilder.build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800602 }
603
604 /**
nosignal5fd282e2016-09-16 16:11:40 -0700605 * Builds a unicast intent.
Luca Prete9c2ee072016-02-16 11:00:44 -0800606 *
nosignal5fd282e2016-09-16 16:11:40 -0700607 * @param key the key to identify the intent
608 * @param srcs the ingress connect points
609 * @param dst the egress connect point
610 * @param host the destination Host
611 * @return the generated multi-point to single-point intent
Luca Prete9c2ee072016-02-16 11:00:44 -0800612 */
613 private MultiPointToSinglePointIntent buildUniIntent(Key key,
Luca Prete092e8952016-10-26 16:25:56 +0200614 Set<FilteredConnectPoint> srcs,
615 FilteredConnectPoint dst,
616 Host host,
617 EncapsulationType encap) {
618 MultiPointToSinglePointIntent.Builder intentBuilder;
619
nosignal5fd282e2016-09-16 16:11:40 -0700620 TrafficSelector selector = DefaultTrafficSelector.builder()
621 .matchEthDst(host.mac())
622 .build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800623
Luca Prete092e8952016-10-26 16:25:56 +0200624 intentBuilder = MultiPointToSinglePointIntent.builder()
Luca Prete9c2ee072016-02-16 11:00:44 -0800625 .appId(APPID)
626 .key(key)
627 .selector(selector)
nosignal5fd282e2016-09-16 16:11:40 -0700628 .filteredIngressPoints(srcs)
629 .filteredEgressPoint(dst)
Luca Prete092e8952016-10-26 16:25:56 +0200630 .priority(PRIORITY_OFFSET);
nosignal5fd282e2016-09-16 16:11:40 -0700631
Luca Prete092e8952016-10-26 16:25:56 +0200632 encap(intentBuilder, encap);
633
634 return intentBuilder.build();
Luca Prete9c2ee072016-02-16 11:00:44 -0800635 }
636
637 /**
nosignal5fd282e2016-09-16 16:11:40 -0700638 * Returns the filtered connect point associated to a given host.
Luca Prete9c2ee072016-02-16 11:00:44 -0800639 *
nosignal5fd282e2016-09-16 16:11:40 -0700640 * @param host the target host
641 * @param fcps the filtered connected points
642 * @return the filtered connect point associated to the given host; null
643 * otherwise
644 */
645 private FilteredConnectPoint getHostPoint(Host host,
646 Set<FilteredConnectPoint> fcps) {
647 return fcps.stream()
648 .filter(fcp -> fcp.connectPoint().equals(host.location()))
649 .filter(fcp -> {
650 VlanIdCriterion vlanCriterion =
651 (VlanIdCriterion) fcp.trafficSelector().
652 getCriterion(Criterion.Type.VLAN_VID);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100653 return vlanCriterion == null ||
nosignal5fd282e2016-09-16 16:11:40 -0700654 vlanCriterion.vlanId().equals(host.vlan());
655 })
656 .findFirst()
657 .orElse(null);
658 }
659
660 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100661 * Computes a set of filtered connect points from a list of given interfaces.
nosignal5fd282e2016-09-16 16:11:40 -0700662 *
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100663 * @param interfaces the interfaces to compute
nosignal5fd282e2016-09-16 16:11:40 -0700664 * @return the set of filtered connect points
665 */
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100666 private Set<FilteredConnectPoint> buildFCPoints(Collection<Interface> interfaces) {
667 // Build all filtered connected points in the VPLS
nosignal5fd282e2016-09-16 16:11:40 -0700668 return interfaces
669 .stream()
670 .map(intf -> {
671 TrafficSelector.Builder selectorBuilder =
672 DefaultTrafficSelector.builder();
nosignal5fd282e2016-09-16 16:11:40 -0700673 if (!intf.vlan().equals(VlanId.NONE)) {
674 selectorBuilder.matchVlanId(intf.vlan());
675 }
nosignal5fd282e2016-09-16 16:11:40 -0700676 return new FilteredConnectPoint(intf.connectPoint(),
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100677 selectorBuilder.build());
nosignal5fd282e2016-09-16 16:11:40 -0700678 })
679 .collect(Collectors.toSet());
680 }
681
682 /**
683 * Builds an intent Key either for a single-point to multi-point or
684 * multi-point to single-point intent, based on a prefix that defines
685 * the intent type, the connection point representing the source or the
Luca Prete092e8952016-10-26 16:25:56 +0200686 * destination and the VLAN Id representing the VPLS.
nosignal5fd282e2016-09-16 16:11:40 -0700687 *
688 * @param prefix the key prefix
689 * @param cPoint the ingress/egress connect point
Luca Prete092e8952016-10-26 16:25:56 +0200690 * @param vplsName the VPLS name
nosignal5fd282e2016-09-16 16:11:40 -0700691 * @param hostMac the ingress/egress MAC address
692 * @return the key to identify the intent
693 */
694 private Key buildKey(String prefix,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100695 ConnectPoint cPoint,
696 String vplsName,
697 MacAddress hostMac) {
Luca Prete092e8952016-10-26 16:25:56 +0200698 String keyString = vplsName +
nosignal5fd282e2016-09-16 16:11:40 -0700699 DASH +
700 prefix +
701 DASH +
702 cPoint.deviceId() +
703 DASH +
704 cPoint.port() +
705 DASH +
706 hostMac;
707
708 return Key.of(keyString, APPID);
709 }
710
711 /**
Luca Prete092e8952016-10-26 16:25:56 +0200712 * Adds an encapsulation constraint to the builder given, if encap is not
713 * equal to NONE.
714 *
715 * @param builder the intent builder
716 * @param encap the encapsulation type
717 */
718 private static void encap(ConnectivityIntent.Builder builder,
719 EncapsulationType encap) {
720 if (!encap.equals(NONE)) {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100721 builder.constraints(ImmutableList.of(
722 new EncapsulationConstraint(encap)));
Luca Prete092e8952016-10-26 16:25:56 +0200723 }
724 }
725
726 /**
nosignal5fd282e2016-09-16 16:11:40 -0700727 * Returns the device Id of the ith device.
728 *
729 * @param i the device to get the Id of
730 * @return the device Id
Luca Prete9c2ee072016-02-16 11:00:44 -0800731 */
732 private static DeviceId getDeviceId(int i) {
733 return DeviceId.deviceId("" + i);
734 }
735
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100736 private static MacAddress getMac(int n) {
737 return MacAddress.valueOf(String.format("00:00:00:00:00:%s", n));
738 }
739
Luca Prete9c2ee072016-02-16 11:00:44 -0800740 private static HostLocation getLocation(int i) {
741 return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L);
742 }
743
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100744 private static HostLocation getLocation(int d, int p) {
745 return new HostLocation(new ConnectPoint(getDeviceId(d),
746 PortNumber.portNumber(p)), 123L);
747 }
748
Luca Prete9c2ee072016-02-16 11:00:44 -0800749 /**
nosignal5fd282e2016-09-16 16:11:40 -0700750 * Represents a fake IntentService class that allows to store and retrieve
751 * intents without implementing the IntentService logic.
Luca Prete9c2ee072016-02-16 11:00:44 -0800752 */
753 private class TestIntentService extends IntentServiceAdapter {
754
Luca Pretec21c6e62016-09-22 14:52:21 -0700755 private Map<Key, Intent> intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800756
757 public TestIntentService() {
Luca Pretec21c6e62016-09-22 14:52:21 -0700758 intents = Maps.newHashMap();
Luca Prete9c2ee072016-02-16 11:00:44 -0800759 }
760
761 @Override
762 public void submit(Intent intent) {
Luca Pretec21c6e62016-09-22 14:52:21 -0700763 intents.put(intent.key(), intent);
Luca Prete9c2ee072016-02-16 11:00:44 -0800764 }
765
766 @Override
767 public long getIntentCount() {
768 return intents.size();
769 }
770
771 @Override
772 public Iterable<Intent> getIntents() {
Luca Pretec21c6e62016-09-22 14:52:21 -0700773 return intents.values();
Luca Prete9c2ee072016-02-16 11:00:44 -0800774 }
775
776 @Override
777 public Intent getIntent(Key intentKey) {
Luca Pretec21c6e62016-09-22 14:52:21 -0700778 for (Intent intent : intents.values()) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800779 if (intent.key().equals(intentKey)) {
780 return intent;
781 }
782 }
783 return null;
784 }
785 }
786
787 /**
788 * Represents a fake HostService class which allows to add hosts manually
789 * in each test, when needed.
790 */
791 private class TestHostService extends HostServiceAdapter {
792
793 private Set<Host> hosts;
794
795 public TestHostService(Set<Host> hosts) {
796 this.hosts = hosts;
797 }
798
799 @Override
800 public void addListener(HostListener listener) {
801 VplsTest.this.hostListener = listener;
802 }
803
804 @Override
805 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
806 return hosts.stream()
nosignal5fd282e2016-09-16 16:11:40 -0700807 .filter(h -> h.location().equals(connectPoint))
Luca Prete9c2ee072016-02-16 11:00:44 -0800808 .collect(Collectors.toSet());
809 }
810
811 }
812
nosignal5fd282e2016-09-16 16:11:40 -0700813 /**
814 * Represents a fake IdGenerator class for intents.
815 */
Luca Prete9c2ee072016-02-16 11:00:44 -0800816 private static class TestIdGenerator implements IdGenerator {
817
818 private final AtomicLong id = new AtomicLong(0);
819
820 @Override
821 public long getNewId() {
822 return id.getAndIncrement();
823 }
824
825 }
826
827 /**
828 * Test IntentSynchronizer that passes all intents straight through to the
829 * intent service.
830 */
831 private class TestIntentSynchronizer implements IntentSynchronizationService,
832 IntentSynchronizationAdminService {
833
834 private final IntentService intentService;
835
836 /**
nosignal5fd282e2016-09-16 16:11:40 -0700837 * Creates a new intent test synchronizer.
Luca Prete9c2ee072016-02-16 11:00:44 -0800838 *
839 * @param intentService intent service
840 */
841 public TestIntentSynchronizer(IntentService intentService) {
842 this.intentService = intentService;
843 }
844
845 @Override
846 public void submit(Intent intent) {
847 intentService.submit(intent);
848 }
849
850 @Override
851 public void withdraw(Intent intent) {
852 intentService.withdraw(intent);
853 }
854
855 @Override
856 public void modifyPrimary(boolean isPrimary) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800857 }
858
859 @Override
860 public void removeIntents() {
Luca Prete2705d662016-04-29 15:30:23 -0700861 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800862
Luca Prete2705d662016-04-29 15:30:23 -0700863 @Override
864 public void removeIntentsByAppId(ApplicationId applicationId) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800865 }
866 }
867
nosignal5fd282e2016-09-16 16:11:40 -0700868 /**
869 * Represents a fake VplsConfigService class which is needed for testing.
870 */
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100871 private class TestVplsConfigService extends VplsConfigServiceAdapter {
nosignal5fd282e2016-09-16 16:11:40 -0700872
Luca Prete092e8952016-10-26 16:25:56 +0200873 private final SetMultimap<String, Interface> ifacesByVplsName;
874 private final Map<String, EncapsulationType> encapsByVplsName;
875
nosignal5fd282e2016-09-16 16:11:40 -0700876 private Set<String> vplsAffectByApi = new HashSet<>();
877
Luca Prete092e8952016-10-26 16:25:56 +0200878 TestVplsConfigService(SetMultimap<String, Interface> ifacesByVplsName,
879 Map<String, EncapsulationType> encapsByVplsName) {
880 this.ifacesByVplsName = ifacesByVplsName;
881 this.encapsByVplsName = encapsByVplsName;
nosignal5fd282e2016-09-16 16:11:40 -0700882 }
883
884 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200885 public void addVpls(String vplsName, Set<String> ifaceNames, String encap) {
886 if (!ifacesByVplsName.containsKey(vplsName)) {
887 ifaceNames.forEach(ifaceName -> {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100888 AVAILABLE_INTERFACES.forEach(iface -> {
Luca Prete092e8952016-10-26 16:25:56 +0200889 if (iface.name().equals(ifaceName)) {
890 ifacesByVplsName.put(vplsName, iface);
nosignal5fd282e2016-09-16 16:11:40 -0700891 }
892 });
893 });
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100894 }
895 if (!ifacesByVplsName.containsKey(vplsName)) {
Luca Prete092e8952016-10-26 16:25:56 +0200896 encapsByVplsName.put(vplsName, valueOf(encap));
nosignal5fd282e2016-09-16 16:11:40 -0700897 }
898 }
899
900 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200901 public void removeVpls(String vplsName) {
902 if (ifacesByVplsName.containsKey(vplsName)) {
903 ifacesByVplsName.removeAll(vplsName);
nosignal5fd282e2016-09-16 16:11:40 -0700904 }
905 }
906
907 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200908 public void addIface(String vplsName, String iface) {
909 if (!ifacesByVplsName.containsKey(vplsName)) {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100910 AVAILABLE_INTERFACES.forEach(intf -> {
nosignal5fd282e2016-09-16 16:11:40 -0700911 if (intf.name().equals(iface)) {
Luca Prete092e8952016-10-26 16:25:56 +0200912 ifacesByVplsName.put(vplsName, intf);
nosignal5fd282e2016-09-16 16:11:40 -0700913 }
914 });
915 }
916 }
917
918 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200919 public void setEncap(String vplsName, String encap) {
920 encapsByVplsName.put(vplsName, EncapsulationType.enumFromString(encap));
921 }
922
923 @Override
924 public void removeIface(String iface) {
925 SetMultimap<String, Interface> search = HashMultimap.create(ifacesByVplsName);
nosignal5fd282e2016-09-16 16:11:40 -0700926 search.entries().forEach(e -> {
927 if (e.getValue().name().equals(iface)) {
Luca Prete092e8952016-10-26 16:25:56 +0200928 ifacesByVplsName.remove(e.getKey(), iface);
nosignal5fd282e2016-09-16 16:11:40 -0700929 }
930 });
931 }
932
933 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200934 public void cleanVplsConfig() {
935 ifacesByVplsName.clear();
nosignal5fd282e2016-09-16 16:11:40 -0700936 }
937
938 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200939 public EncapsulationType encap(String vplsName) {
940 EncapsulationType encap = null;
941 if (encapsByVplsName.containsKey(vplsName)) {
942 encap = encapsByVplsName.get(vplsName);
943 }
944 return encap;
945 }
946
947 @Override
948 public Set<String> vplsAffectedByApi() {
nosignal5fd282e2016-09-16 16:11:40 -0700949 Set<String> vplsNames = ImmutableSet.copyOf(vplsAffectByApi);
950
951 vplsAffectByApi.clear();
952
953 return vplsNames;
954 }
955
956 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200957 public Set<Interface> allIfaces() {
958 return ifacesByVplsName.values()
nosignal5fd282e2016-09-16 16:11:40 -0700959 .stream()
960 .collect(Collectors.toSet());
961 }
962
963 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200964 public Set<Interface> ifaces(String name) {
965 return ifacesByVplsName.get(name)
nosignal5fd282e2016-09-16 16:11:40 -0700966 .stream()
967 .collect(Collectors.toSet());
968 }
969
970 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200971 public Set<String> vplsNames() {
972 return ifacesByVplsName.keySet();
nosignal5fd282e2016-09-16 16:11:40 -0700973 }
974
975 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200976 public Set<String> vplsNamesOld() {
977 return ifacesByVplsName.keySet();
978 }
979
980 public SetMultimap<String, Interface> ifacesByVplsName() {
981 return ImmutableSetMultimap.copyOf(ifacesByVplsName);
nosignal5fd282e2016-09-16 16:11:40 -0700982 }
983
984 @Override
Luca Prete092e8952016-10-26 16:25:56 +0200985 public SetMultimap<String, Interface> ifacesByVplsName(VlanId vlan,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100986 ConnectPoint connectPoint) {
Luca Prete092e8952016-10-26 16:25:56 +0200987 String vplsName =
988 ifacesByVplsName.entries().stream()
nosignal5fd282e2016-09-16 16:11:40 -0700989 .filter(e -> e.getValue().connectPoint().equals(connectPoint))
990 .filter(e -> e.getValue().vlan().equals(vlan))
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100991 .map(Map.Entry::getKey)
nosignal5fd282e2016-09-16 16:11:40 -0700992 .findFirst()
993 .orElse(null);
994 SetMultimap<String, Interface> result = HashMultimap.create();
Luca Prete092e8952016-10-26 16:25:56 +0200995 if (vplsName != null && ifacesByVplsName.containsKey(vplsName)) {
996 ifacesByVplsName.get(vplsName)
997 .forEach(intf -> result.put(vplsName, intf));
nosignal5fd282e2016-09-16 16:11:40 -0700998 return result;
999 }
1000 return null;
1001 }
nosignal5fd282e2016-09-16 16:11:40 -07001002 }
Luca Prete9c2ee072016-02-16 11:00:44 -08001003}