blob: 6051f9206eab271ac4260642d8f768444cd9a5a7 [file] [log] [blame]
Luca Prete9c2ee072016-02-16 11:00:44 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
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 */
Yi Tsengf4e13e32017-03-30 15:38:39 -070016
Luca Prete9c2ee072016-02-16 11:00:44 -080017package org.onosproject.vpls;
18
Yi Tsengf4e13e32017-03-30 15:38:39 -070019import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
Luca Prete13465642017-01-04 10:31:48 -080021import com.google.common.collect.ImmutableSet;
Luca Prete13465642017-01-04 10:31:48 -080022import com.google.common.collect.Lists;
23import com.google.common.collect.Maps;
Luca Prete13465642017-01-04 10:31:48 -080024import com.google.common.collect.Sets;
Luca Prete9c2ee072016-02-16 11:00:44 -080025import org.onlab.packet.Ip4Address;
Yi Tsengf4e13e32017-03-30 15:38:39 -070026import org.onlab.packet.IpAddress;
Luca Prete9c2ee072016-02-16 11:00:44 -080027import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onosproject.TestApplicationId;
Yi Tsengf4e13e32017-03-30 15:38:39 -070030import org.onosproject.cluster.LeadershipEvent;
31import org.onosproject.cluster.LeadershipEventListener;
32import org.onosproject.cluster.LeadershipServiceAdapter;
33import org.onosproject.cluster.NodeId;
Luca Prete9c2ee072016-02-16 11:00:44 -080034import org.onosproject.core.ApplicationId;
Yi Tsengf4e13e32017-03-30 15:38:39 -070035import org.onosproject.core.CoreServiceAdapter;
Ray Milkeyfacf2862017-08-03 11:58:29 -070036import org.onosproject.net.intf.Interface;
37import org.onosproject.net.intf.InterfaceListener;
38import org.onosproject.net.intf.InterfaceService;
Luca Prete9c2ee072016-02-16 11:00:44 -080039import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.DefaultHost;
41import org.onosproject.net.DeviceId;
Luca Prete092e8952016-10-26 16:25:56 +020042import org.onosproject.net.EncapsulationType;
Luca Prete9c2ee072016-02-16 11:00:44 -080043import org.onosproject.net.Host;
44import org.onosproject.net.HostId;
45import org.onosproject.net.HostLocation;
46import org.onosproject.net.PortNumber;
Yi Tsengf4e13e32017-03-30 15:38:39 -070047import org.onosproject.net.config.Config;
48import org.onosproject.net.config.ConfigApplyDelegate;
49import org.onosproject.net.config.NetworkConfigEvent;
50import org.onosproject.net.config.NetworkConfigListener;
51import org.onosproject.net.config.NetworkConfigServiceAdapter;
Luca Prete9c2ee072016-02-16 11:00:44 -080052import org.onosproject.net.host.HostEvent;
53import org.onosproject.net.host.HostListener;
Luca Prete9c2ee072016-02-16 11:00:44 -080054import org.onosproject.net.host.HostServiceAdapter;
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -080055import org.onosproject.net.host.InterfaceIpAddress;
Luca Prete9c2ee072016-02-16 11:00:44 -080056import org.onosproject.net.intent.Intent;
Yi Tsengf4e13e32017-03-30 15:38:39 -070057import org.onosproject.net.intent.IntentData;
58import org.onosproject.net.intent.IntentEvent;
59import org.onosproject.net.intent.IntentListener;
Luca Prete9c2ee072016-02-16 11:00:44 -080060import org.onosproject.net.intent.IntentServiceAdapter;
Yi Tsengf4e13e32017-03-30 15:38:39 -070061import org.onosproject.net.intent.IntentState;
Luca Prete9c2ee072016-02-16 11:00:44 -080062import org.onosproject.net.provider.ProviderId;
Yi Tsengf4e13e32017-03-30 15:38:39 -070063import org.onosproject.store.StoreDelegate;
64import org.onosproject.store.service.WallClockTimestamp;
65import org.onosproject.vpls.api.Vpls;
66import org.onosproject.vpls.api.VplsData;
67import org.onosproject.vpls.config.VplsAppConfig;
68import org.onosproject.vpls.config.VplsAppConfigTest;
69import org.onosproject.vpls.config.VplsConfig;
70import org.onosproject.vpls.store.VplsStoreAdapter;
71import org.onosproject.vpls.store.VplsStoreEvent;
Luca Prete9c2ee072016-02-16 11:00:44 -080072
Yi Tsengf4e13e32017-03-30 15:38:39 -070073import java.io.IOException;
Luca Prete13465642017-01-04 10:31:48 -080074import java.util.Collection;
75import java.util.Collections;
Luca Prete13465642017-01-04 10:31:48 -080076import java.util.List;
77import java.util.Map;
78import java.util.Set;
Luca Prete13465642017-01-04 10:31:48 -080079import java.util.stream.Collectors;
80
Luca Prete9c2ee072016-02-16 11:00:44 -080081/**
Yi Tsengf4e13e32017-03-30 15:38:39 -070082 * Class provides data for VPLS testing.
Luca Prete9c2ee072016-02-16 11:00:44 -080083 */
Yi Tsengf4e13e32017-03-30 15:38:39 -070084public abstract class VplsTest {
85 protected static final String APP_NAME = "org.onosproject.vpls";
86 protected static final ApplicationId APPID = TestApplicationId.create(APP_NAME);
87 protected static final String DASH = "-";
88 protected static final int PRIORITY_OFFSET = 1000;
89 protected static final String VPLS1 = "vpls1";
90 protected static final String VPLS2 = "vpls2";
91 protected static final String VPLS3 = "vpls3";
92 protected static final String VPLS4 = "vpls4";
Luca Prete9c2ee072016-02-16 11:00:44 -080093
Yi Tsengf4e13e32017-03-30 15:38:39 -070094 protected static final PortNumber P1 = PortNumber.portNumber(1);
95 protected static final PortNumber P2 = PortNumber.portNumber(2);
Luca Prete9c2ee072016-02-16 11:00:44 -080096
Yi Tsengf4e13e32017-03-30 15:38:39 -070097 protected static final DeviceId DID1 = getDeviceId(1);
98 protected static final DeviceId DID2 = getDeviceId(2);
99 protected static final DeviceId DID3 = getDeviceId(3);
100 protected static final DeviceId DID4 = getDeviceId(4);
101 protected static final DeviceId DID5 = getDeviceId(5);
102 protected static final DeviceId DID6 = getDeviceId(6);
Luca Prete9c2ee072016-02-16 11:00:44 -0800103
Yi Tsengf4e13e32017-03-30 15:38:39 -0700104 protected static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
105 protected static final ConnectPoint CP2 = new ConnectPoint(DID2, P1);
106 protected static final ConnectPoint CP3 = new ConnectPoint(DID3, P1);
107 protected static final ConnectPoint CP4 = new ConnectPoint(DID4, P1);
108 protected static final ConnectPoint CP5 = new ConnectPoint(DID5, P1);
109 protected static final ConnectPoint CP6 = new ConnectPoint(DID6, P1);
110 protected static final ConnectPoint CP7 = new ConnectPoint(DID4, P2);
111 protected static final ConnectPoint CP8 = new ConnectPoint(DID3, P2);
112 protected static final ConnectPoint CP9 = new ConnectPoint(DID5, P1);
113 protected static final ConnectPoint CP10 = new ConnectPoint(DID5, P2);
Luca Prete9c2ee072016-02-16 11:00:44 -0800114
Yi Tsengf4e13e32017-03-30 15:38:39 -0700115 protected static final VlanId VLAN100 = VlanId.vlanId((short) 100);
116 protected static final VlanId VLAN200 = VlanId.vlanId((short) 200);
117 protected static final VlanId VLAN300 = VlanId.vlanId((short) 300);
118 protected static final VlanId VLAN400 = VlanId.vlanId((short) 400);
119 protected static final VlanId VLAN_NONE = VlanId.NONE;
Luca Prete9c2ee072016-02-16 11:00:44 -0800120
Yi Tsengf4e13e32017-03-30 15:38:39 -0700121 protected static final MacAddress MAC1 = getMac(1);
122 protected static final MacAddress MAC2 = getMac(2);
123 protected static final MacAddress MAC3 = getMac(3);
124 protected static final MacAddress MAC4 = getMac(4);
125 protected static final MacAddress MAC5 = getMac(5);
126 protected static final MacAddress MAC6 = getMac(6);
127 protected static final MacAddress MAC7 = getMac(7);
128 protected static final MacAddress MAC8 = getMac(8);
129 protected static final MacAddress MAC9 = getMac(9);
130 protected static final MacAddress MAC10 = getMac(10);
131 protected static final MacAddress MAC11 = getMac(11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800132
Yi Tsengf4e13e32017-03-30 15:38:39 -0700133 protected static final Ip4Address IP1 = Ip4Address.valueOf("192.168.1.1");
134 protected static final Ip4Address IP2 = Ip4Address.valueOf("192.168.1.2");
nosignal5fd282e2016-09-16 16:11:40 -0700135
Yi Tsengf4e13e32017-03-30 15:38:39 -0700136 protected static final HostId HID1 = HostId.hostId(MAC1, VLAN100);
137 protected static final HostId HID2 = HostId.hostId(MAC2, VLAN100);
138 protected static final HostId HID3 = HostId.hostId(MAC3, VLAN200);
139 protected static final HostId HID4 = HostId.hostId(MAC4, VLAN200);
140 protected static final HostId HID5 = HostId.hostId(MAC5, VLAN300);
141 protected static final HostId HID6 = HostId.hostId(MAC6, VLAN300);
142 protected static final HostId HID7 = HostId.hostId(MAC7, VLAN300);
143 protected static final HostId HID8 = HostId.hostId(MAC8, VLAN400);
144 protected static final HostId HID9 = HostId.hostId(MAC9);
145 protected static final HostId HID10 = HostId.hostId(MAC10);
146 protected static final HostId HID11 = HostId.hostId(MAC11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800147
Yi Tsengf4e13e32017-03-30 15:38:39 -0700148 protected static final ProviderId PID = new ProviderId("of", "foo");
Luca Prete9c2ee072016-02-16 11:00:44 -0800149
Yi Tsengf4e13e32017-03-30 15:38:39 -0700150 protected static final NodeId NODE_ID_1 = new NodeId("Node1");
151 protected static final NodeId NODE_ID_2 = new NodeId("Node2");
Luca Prete9c2ee072016-02-16 11:00:44 -0800152
Yi Tsengf4e13e32017-03-30 15:38:39 -0700153 protected static final Interface V100H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700154 new Interface("v100h1", CP1, null, null, VLAN100);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700155 protected static final Interface V100H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700156 new Interface("v100h2", CP2, null, null, VLAN100);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700157 protected static final Interface V200H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700158 new Interface("v200h1", CP3, null, null, VLAN200);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700159 protected static final Interface V200H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700160 new Interface("v200h2", CP4, null, null, VLAN200);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700161 protected static final Interface V300H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700162 new Interface("v300h1", CP5, null, null, VLAN300);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700163 protected static final Interface V300H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700164 new Interface("v300h2", CP6, null, null, VLAN300);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700165 protected static final Interface V400H1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100166 new Interface("v400h1", CP7, null, null, VLAN400);
167
Yi Tsengf4e13e32017-03-30 15:38:39 -0700168 protected static final Interface VNONEH1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100169 new Interface("vNoneh1", CP8, null, null, VLAN_NONE);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700170 protected static final Interface VNONEH2 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100171 new Interface("vNoneh2", CP9, null, null, VLAN_NONE);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700172 protected static final Interface VNONEH3 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100173 new Interface("vNoneh3", CP10, null, null, VLAN_NONE);
nosignal5fd282e2016-09-16 16:11:40 -0700174
Yi Tsengf4e13e32017-03-30 15:38:39 -0700175 protected static final Host V100HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700176 new DefaultHost(PID, HID1, MAC1, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100177 getLocation(1), Collections.singleton(IP1));
Yi Tsengf4e13e32017-03-30 15:38:39 -0700178 protected static final Host V100HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700179 new DefaultHost(PID, HID2, MAC2, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100180 getLocation(2), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700181 protected static final Host V200HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700182 new DefaultHost(PID, HID3, MAC3, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100183 getLocation(3), Collections.singleton(IP2));
Yi Tsengf4e13e32017-03-30 15:38:39 -0700184 protected static final Host V200HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700185 new DefaultHost(PID, HID4, MAC4, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100186 getLocation(4), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700187 protected static final Host V300HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700188 new DefaultHost(PID, HID5, MAC5, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100189 getLocation(5), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700190 protected static final Host V300HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700191 new DefaultHost(PID, HID6, MAC6, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100192 getLocation(6), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700193 protected static final Host V300HOST3 =
nosignal5fd282e2016-09-16 16:11:40 -0700194 new DefaultHost(PID, HID7, MAC7, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100195 getLocation(7), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700196 protected static final Host V400HOST1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100197 new DefaultHost(PID, HID8, MAC8, VLAN400,
198 getLocation(4, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700199
Yi Tsengf4e13e32017-03-30 15:38:39 -0700200 protected static final Host VNONEHOST1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100201 new DefaultHost(PID, HID9, MAC9, VlanId.NONE,
202 getLocation(3, 2), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700203 protected static final Host VNONEHOST2 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100204 new DefaultHost(PID, HID10, MAC10, VlanId.NONE,
205 getLocation(5, 1), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700206 protected static final Host VNONEHOST3 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100207 new DefaultHost(PID, HID11, MAC11, VlanId.NONE,
208 getLocation(5, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700209
Yi Tsengf4e13e32017-03-30 15:38:39 -0700210 protected static final Set<Interface> AVAILABLE_INTERFACES =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100211 ImmutableSet.of(V100H1, V100H2, V200H1, V200H2, V300H1, V300H2,
Yi Tsengf4e13e32017-03-30 15:38:39 -0700212 V400H1, VNONEH1, VNONEH2, VNONEH3);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100213
Yi Tsengf4e13e32017-03-30 15:38:39 -0700214 protected static final Set<Host> AVAILABLE_HOSTS =
Luca Prete092e8952016-10-26 16:25:56 +0200215 ImmutableSet.of(V100HOST1, V100HOST2, V200HOST1,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100216 V200HOST2, V300HOST1, V300HOST2, V300HOST3,
217 VNONEHOST1, VNONEHOST2,
218 V400HOST1, VNONEHOST3);
219
nosignal5fd282e2016-09-16 16:11:40 -0700220
221 /**
222 * Returns the device Id of the ith device.
223 *
224 * @param i the device to get the Id of
225 * @return the device Id
Luca Prete9c2ee072016-02-16 11:00:44 -0800226 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700227 protected static DeviceId getDeviceId(int i) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800228 return DeviceId.deviceId("" + i);
229 }
230
Yi Tsengf4e13e32017-03-30 15:38:39 -0700231 /**
232 * Generates a mac address by given number.
233 *
234 * @param n the number to generate mac address
235 * @return the mac address
236 */
237 protected static MacAddress getMac(int n) {
Dominika Molenda52cc3a92017-06-19 17:08:35 +0200238 return MacAddress.valueOf(String.format("00:00:00:00:00:%02x", n));
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100239 }
240
Yi Tsengf4e13e32017-03-30 15:38:39 -0700241 /**
242 * Generates a host location by given device number.
243 *
244 * @param i the given number
245 * @return the host location
246 */
247 protected static HostLocation getLocation(int i) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800248 return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L);
249 }
250
Yi Tsengf4e13e32017-03-30 15:38:39 -0700251 /**
252 * Generates host location by given device number and port number.
253 *
254 * @param d the device number
255 * @param p the port number
256 * @return the host location
257 */
258 protected static HostLocation getLocation(int d, int p) {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100259 return new HostLocation(new ConnectPoint(getDeviceId(d),
260 PortNumber.portNumber(p)), 123L);
261 }
262
Luca Prete9c2ee072016-02-16 11:00:44 -0800263 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700264 * Test core service; For generate test application ID.
Luca Prete9c2ee072016-02-16 11:00:44 -0800265 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700266 public class TestCoreService extends CoreServiceAdapter {
267 @Override
268 public ApplicationId registerApplication(String name) {
269 return TestApplicationId.create(name);
270 }
271 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800272
Yi Tsengf4e13e32017-03-30 15:38:39 -0700273 /**
274 * Test intent service.
275 * Always install or withdraw success for any Intents.
276 */
277 public class TestIntentService extends IntentServiceAdapter {
278 IntentListener listener;
279 List<IntentData> intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800280
281 public TestIntentService() {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700282 intents = Lists.newArrayList();
Luca Prete9c2ee072016-02-16 11:00:44 -0800283 }
284
285 @Override
286 public void submit(Intent intent) {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700287 intents.add(new IntentData(intent, IntentState.INSTALLED, new WallClockTimestamp()));
288 if (listener != null) {
289 IntentEvent.getEvent(IntentState.INSTALLED, intent).ifPresent(listener::event);
290
291 }
292 }
293
294 @Override
295 public void withdraw(Intent intent) {
296 intents.forEach(intentData -> {
297 if (intentData.intent().key().equals(intent.key())) {
298 intentData.setState(IntentState.WITHDRAWN);
299
300 if (listener != null) {
301 IntentEvent.getEvent(IntentState.WITHDRAWN, intent).ifPresent(listener::event);
302 }
303 }
304 });
305 }
306
307 @Override
308 public Iterable<Intent> getIntents() {
309 return intents.stream()
310 .map(IntentData::intent)
311 .collect(Collectors.toList());
Luca Prete9c2ee072016-02-16 11:00:44 -0800312 }
313
314 @Override
315 public long getIntentCount() {
316 return intents.size();
317 }
318
319 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700320 public void addListener(IntentListener listener) {
321 this.listener = listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800322 }
323
324 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700325 public void removeListener(IntentListener listener) {
326 this.listener = null;
Luca Prete9c2ee072016-02-16 11:00:44 -0800327 }
328 }
329
330 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700331 * Test leadership service.
Luca Prete9c2ee072016-02-16 11:00:44 -0800332 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700333 public class TestLeadershipService extends LeadershipServiceAdapter {
334 LeadershipEventListener listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800335
Yi Tsengf4e13e32017-03-30 15:38:39 -0700336 @Override
337 public void addListener(LeadershipEventListener listener) {
338 this.listener = listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800339 }
340
341 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700342 public void removeListener(LeadershipEventListener listener) {
343 this.listener = null;
344 }
345
346 /**
347 * Sends the leadership event to the listener.
348 *
349 * @param event the Intent event
350 */
351 public void sendEvent(LeadershipEvent event) {
352 if (listener != null && listener.isRelevant(event)) {
353 listener.event(event);
354 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800355 }
356
357 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700358 public NodeId getLeader(String path) {
359 return NODE_ID_1;
360 }
361 }
362
363 /**
364 * Test interface service; contains all interfaces which already generated.
365 */
366 public class TestInterfaceService implements InterfaceService {
367
368 @Override
369 public void addListener(InterfaceListener listener) {
370 }
371
372 @Override
373 public void removeListener(InterfaceListener listener) {
374 }
375
376 @Override
377 public Set<Interface> getInterfaces() {
378 return AVAILABLE_INTERFACES;
379 }
380
381 @Override
382 public Interface getInterfaceByName(ConnectPoint connectPoint,
383 String name) {
384 return AVAILABLE_INTERFACES.stream()
385 .filter(intf -> intf.name().equals(name))
386 .findFirst()
387 .orElse(null);
388 }
389
390 @Override
391 public Set<Interface> getInterfacesByPort(ConnectPoint port) {
392 return AVAILABLE_INTERFACES.stream()
393 .filter(intf -> intf.connectPoint().equals(port))
Luca Prete9c2ee072016-02-16 11:00:44 -0800394 .collect(Collectors.toSet());
395 }
396
Yi Tsengf4e13e32017-03-30 15:38:39 -0700397 @Override
398 public Set<Interface> getInterfacesByIp(IpAddress ip) {
399 return AVAILABLE_INTERFACES.stream()
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800400 .filter(intf -> intf.ipAddressesList().stream()
401 .map(InterfaceIpAddress::ipAddress)
402 .filter(ip::equals)
403 .findAny()
404 .isPresent())
Yi Tsengf4e13e32017-03-30 15:38:39 -0700405 .collect(Collectors.toSet());
406 }
407
408 @Override
409 public Set<Interface> getInterfacesByVlan(VlanId vlan) {
410 return AVAILABLE_INTERFACES.stream()
411 .filter(intf -> intf.vlan().equals(vlan))
412 .collect(Collectors.toSet());
413 }
414
415 @Override
416 public Interface getMatchingInterface(IpAddress ip) {
417 return AVAILABLE_INTERFACES.stream()
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800418 .filter(intf -> intf.ipAddressesList().stream()
419 .map(InterfaceIpAddress::ipAddress)
420 .filter(ip::equals)
421 .findAny()
422 .isPresent())
Yi Tsengf4e13e32017-03-30 15:38:39 -0700423 .findFirst()
424 .orElse(null);
425 }
426
427 @Override
428 public Set<Interface> getMatchingInterfaces(IpAddress ip) {
429 return AVAILABLE_INTERFACES.stream()
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800430 .filter(intf -> intf.ipAddressesList().stream()
431 .map(InterfaceIpAddress::ipAddress)
432 .filter(ip::equals)
433 .findAny()
434 .isPresent())
Yi Tsengf4e13e32017-03-30 15:38:39 -0700435 .collect(Collectors.toSet());
436 }
Saurav Das9a554292018-04-27 18:42:30 -0700437
438 @Override
439 public boolean isConfigured(ConnectPoint connectPoint) {
440 for (Interface intf : AVAILABLE_INTERFACES) {
441 if (!intf.connectPoint().equals(connectPoint)) {
442 continue;
443 }
444 if (!intf.ipAddressesList().isEmpty()
445 || intf.vlan() != VlanId.NONE
446 || intf.vlanNative() != VlanId.NONE
447 || intf.vlanUntagged() != VlanId.NONE
448 || !intf.vlanTagged().isEmpty()) {
449 return true;
450 }
451 }
452 return false;
453 }
Yi Tsengf4e13e32017-03-30 15:38:39 -0700454 }
455
456 /**
457 * Test VPLS store.
458 */
459 public class TestVplsStore extends VplsStoreAdapter {
460 /**
461 * Clears the store.
462 */
463 public void clear() {
464 vplsDataMap.clear();
465 }
466
467 /**
468 * Gets the store delegate.
469 *
470 * @return the store delegate
471 */
472 public StoreDelegate<VplsStoreEvent> delegate() {
473 return this.delegate;
474 }
475 }
476
477 /**
478 * Test VPLS.
479 * Provides basic VPLS functionality and stores VPLS information.
480 */
481 public class TestVpls implements Vpls {
482 public Map<String, VplsData> testData;
483
484 public TestVpls() {
485 testData = Maps.newHashMap();
486 }
487
488 public void initSampleData() {
489 testData.clear();
490 VplsData vplsData = VplsData.of(VPLS1);
491 vplsData.addInterfaces(ImmutableSet.of(V100H1, V100H2));
492 vplsData.state(VplsData.VplsState.ADDED);
493 testData.put(VPLS1, vplsData);
494
495 vplsData = VplsData.of(VPLS2);
496 vplsData.addInterfaces(ImmutableSet.of(V200H1, V200H2));
497 vplsData.state(VplsData.VplsState.ADDED);
498 testData.put(VPLS2, vplsData);
499 }
500
501 @Override
502 public VplsData createVpls(String vplsName, EncapsulationType encapsulationType) {
503 VplsData vplsData = VplsData.of(vplsName, encapsulationType);
Yi Tseng3069c612017-05-26 17:09:43 -0700504 vplsData.state(VplsData.VplsState.ADDED);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700505 testData.put(vplsName, vplsData);
506 return vplsData;
507 }
508
509 @Override
510 public VplsData removeVpls(VplsData vplsData) {
511 if (!testData.containsKey(vplsData.name())) {
512 return null;
513 }
514
515 testData.remove(vplsData.name());
516 return vplsData;
517 }
518
519 @Override
520 public void addInterfaces(VplsData vplsData, Collection<Interface> interfaces) {
521 vplsData.addInterfaces(interfaces);
522 testData.put(vplsData.name(), vplsData);
523 }
524
525 @Override
526 public void addInterface(VplsData vplsData, Interface iface) {
527 vplsData.addInterface(iface);
528 testData.put(vplsData.name(), vplsData);
529 }
530
531 @Override
532 public void setEncapsulationType(VplsData vplsData, EncapsulationType encapsulationType) {
533 vplsData.encapsulationType(encapsulationType);
534 testData.put(vplsData.name(), vplsData);
535 }
536
537 @Override
538 public VplsData getVpls(String vplsName) {
539 return testData.get(vplsName);
540 }
541
542 @Override
543 public Collection<VplsData> getAllVpls() {
544 return testData.values();
545 }
546
547 @Override
548 public Collection<Interface> removeInterfaces(VplsData vplsData, Collection<Interface> interfaces) {
549 vplsData.removeInterfaces(interfaces);
550 testData.put(vplsData.name(), vplsData);
551 return interfaces;
552 }
553
554 @Override
555 public Interface removeInterface(VplsData vplsData, Interface iface) {
556 vplsData.removeInterface(iface);
557 testData.put(vplsData.name(), vplsData);
558 return iface;
559 }
560
561 @Override
562 public void removeAllVpls() {
563 testData.clear();
564 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800565 }
566
nosignal5fd282e2016-09-16 16:11:40 -0700567 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700568 * Test host service; contains all hosts which already generated.
569 *
Luca Prete9c2ee072016-02-16 11:00:44 -0800570 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700571 public class TestHostService extends HostServiceAdapter {
Luca Prete9c2ee072016-02-16 11:00:44 -0800572
Yi Tsengf4e13e32017-03-30 15:38:39 -0700573 private HostListener listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800574
Yi Tsengf4e13e32017-03-30 15:38:39 -0700575 @Override
576 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
577 return AVAILABLE_HOSTS.stream()
578 .filter(host -> host.location().equals(connectPoint))
579 .collect(Collectors.toSet());
Luca Prete9c2ee072016-02-16 11:00:44 -0800580 }
581
582 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700583 public Set<Host> getHostsByMac(MacAddress mac) {
584 return AVAILABLE_HOSTS.stream()
585 .filter(host -> host.mac().equals(mac))
586 .collect(Collectors.toSet());
Luca Prete9c2ee072016-02-16 11:00:44 -0800587 }
588
589 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700590 public Iterable<Host> getHosts() {
591 return AVAILABLE_HOSTS;
Luca Prete9c2ee072016-02-16 11:00:44 -0800592 }
593
594 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700595 public Set<Host> getHostsByVlan(VlanId vlanId) {
596 return AVAILABLE_HOSTS.stream()
597 .filter(host -> host.vlan().equals(vlanId))
598 .collect(Collectors.toSet());
599 }
600
601 @Override
602 public int getHostCount() {
603 return AVAILABLE_HOSTS.size();
604 }
605
606 @Override
607 public Host getHost(HostId hostId) {
608 return AVAILABLE_HOSTS.stream()
609 .filter(host -> host.id().equals(hostId))
610 .findFirst()
611 .orElse(null);
612 }
613
614 @Override
615 public void addListener(HostListener listener) {
616 this.listener = listener;
617 }
618
619 public void postHostEvent(HostEvent hostEvent) {
620 this.listener.event(hostEvent);
Luca Prete9c2ee072016-02-16 11:00:44 -0800621 }
622 }
623
nosignal5fd282e2016-09-16 16:11:40 -0700624 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700625 * Test network configuration service.
nosignal5fd282e2016-09-16 16:11:40 -0700626 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700627 public class TestConfigService extends NetworkConfigServiceAdapter {
628 public static final String EMPTY_JSON_TREE = "{}";
629 NetworkConfigListener listener;
630 VplsAppConfig vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700631
Yi Tsengf4e13e32017-03-30 15:38:39 -0700632 @Override
633 public void addListener(NetworkConfigListener listener) {
634 this.listener = listener;
nosignal5fd282e2016-09-16 16:11:40 -0700635 }
636
637 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700638 public void removeListener(NetworkConfigListener listener) {
639 this.listener = null;
640 }
641
642 /**
643 * Sends network config event to listener.
644 *
645 * @param event the network config event
646 */
647 public void sendEvent(NetworkConfigEvent event) {
648 if (listener != null) {
649 listener.event(event);
nosignal5fd282e2016-09-16 16:11:40 -0700650 }
651 }
652
Yi Tsengf4e13e32017-03-30 15:38:39 -0700653 /**
654 * Constructs test config service.
655 * Generates an VPLS configuration with sample VPLS configs.
656 */
657 public TestConfigService() {
658 vplsAppConfig = new VplsAppConfig();
659 final ObjectMapper mapper = new ObjectMapper();
660 final ConfigApplyDelegate delegate = new VplsAppConfigTest.MockCfgDelegate();
661 JsonNode tree = null;
662 try {
663 tree = new ObjectMapper().readTree(EMPTY_JSON_TREE);
664 } catch (IOException e) {
665 e.printStackTrace();
nosignal5fd282e2016-09-16 16:11:40 -0700666 }
Yi Tsengf4e13e32017-03-30 15:38:39 -0700667 vplsAppConfig.init(APPID, APP_NAME, tree, mapper, delegate);
668 VplsConfig vplsConfig = new VplsConfig(VPLS1,
669 ImmutableSet.of(V100H1.name(), V100H2.name()),
670 EncapsulationType.NONE);
671 vplsAppConfig.addVpls(vplsConfig);
672 vplsConfig = new VplsConfig(VPLS2,
673 ImmutableSet.of(V200H1.name(), V200H2.name()),
674 EncapsulationType.VLAN);
675 vplsAppConfig.addVpls(vplsConfig);
676
677 }
678
679 /**
680 * Overrides VPLS config to the config service.
681 *
682 * @param vplsAppConfig the new VPLS config
683 */
684 public void setConfig(VplsAppConfig vplsAppConfig) {
685 this.vplsAppConfig = vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700686 }
687
688 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700689 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
690 return (C) vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700691 }
692
693 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700694 public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
695 return (C) vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700696 }
nosignal5fd282e2016-09-16 16:11:40 -0700697 }
Yi Tsengf4e13e32017-03-30 15:38:39 -0700698
Luca Prete9c2ee072016-02-16 11:00:44 -0800699}