blob: 716b331621f6c5713b9cad06f46561d853581621 [file] [log] [blame]
Luca Prete9c2ee072016-02-16 11:00:44 -08001/*
Yi Tsengf4e13e32017-03-30 15:38:39 -07002 * Copyright 2017-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 */
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;
Luca Prete9c2ee072016-02-16 11:00:44 -080036import org.onosproject.core.IdGenerator;
37import org.onosproject.incubator.net.intf.Interface;
Luca Pretea8854822016-04-26 16:30:55 -070038import org.onosproject.incubator.net.intf.InterfaceListener;
Luca Prete9c2ee072016-02-16 11:00:44 -080039import org.onosproject.incubator.net.intf.InterfaceService;
40import 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 Prete9c2ee072016-02-16 11:00:44 -080044import org.onosproject.net.Host;
45import org.onosproject.net.HostId;
46import org.onosproject.net.HostLocation;
47import org.onosproject.net.PortNumber;
Yi Tsengf4e13e32017-03-30 15:38:39 -070048import org.onosproject.net.config.Config;
49import org.onosproject.net.config.ConfigApplyDelegate;
50import org.onosproject.net.config.NetworkConfigEvent;
51import org.onosproject.net.config.NetworkConfigListener;
52import org.onosproject.net.config.NetworkConfigServiceAdapter;
Luca Prete9c2ee072016-02-16 11:00:44 -080053import org.onosproject.net.host.HostEvent;
54import org.onosproject.net.host.HostListener;
Luca Prete9c2ee072016-02-16 11:00:44 -080055import org.onosproject.net.host.HostServiceAdapter;
56import 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;
79import java.util.concurrent.atomic.AtomicLong;
80import java.util.stream.Collectors;
81
Luca Prete9c2ee072016-02-16 11:00:44 -080082/**
Yi Tsengf4e13e32017-03-30 15:38:39 -070083 * Class provides data for VPLS testing.
Luca Prete9c2ee072016-02-16 11:00:44 -080084 */
Yi Tsengf4e13e32017-03-30 15:38:39 -070085public abstract class VplsTest {
86 protected static final String APP_NAME = "org.onosproject.vpls";
87 protected static final ApplicationId APPID = TestApplicationId.create(APP_NAME);
88 protected static final String DASH = "-";
89 protected static final int PRIORITY_OFFSET = 1000;
90 protected static final String VPLS1 = "vpls1";
91 protected static final String VPLS2 = "vpls2";
92 protected static final String VPLS3 = "vpls3";
93 protected static final String VPLS4 = "vpls4";
Luca Prete9c2ee072016-02-16 11:00:44 -080094
Yi Tsengf4e13e32017-03-30 15:38:39 -070095 protected static final PortNumber P1 = PortNumber.portNumber(1);
96 protected static final PortNumber P2 = PortNumber.portNumber(2);
Luca Prete9c2ee072016-02-16 11:00:44 -080097
Yi Tsengf4e13e32017-03-30 15:38:39 -070098 protected static final DeviceId DID1 = getDeviceId(1);
99 protected static final DeviceId DID2 = getDeviceId(2);
100 protected static final DeviceId DID3 = getDeviceId(3);
101 protected static final DeviceId DID4 = getDeviceId(4);
102 protected static final DeviceId DID5 = getDeviceId(5);
103 protected static final DeviceId DID6 = getDeviceId(6);
Luca Prete9c2ee072016-02-16 11:00:44 -0800104
Yi Tsengf4e13e32017-03-30 15:38:39 -0700105 protected static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
106 protected static final ConnectPoint CP2 = new ConnectPoint(DID2, P1);
107 protected static final ConnectPoint CP3 = new ConnectPoint(DID3, P1);
108 protected static final ConnectPoint CP4 = new ConnectPoint(DID4, P1);
109 protected static final ConnectPoint CP5 = new ConnectPoint(DID5, P1);
110 protected static final ConnectPoint CP6 = new ConnectPoint(DID6, P1);
111 protected static final ConnectPoint CP7 = new ConnectPoint(DID4, P2);
112 protected static final ConnectPoint CP8 = new ConnectPoint(DID3, P2);
113 protected static final ConnectPoint CP9 = new ConnectPoint(DID5, P1);
114 protected static final ConnectPoint CP10 = new ConnectPoint(DID5, P2);
Luca Prete9c2ee072016-02-16 11:00:44 -0800115
Yi Tsengf4e13e32017-03-30 15:38:39 -0700116 protected static final VlanId VLAN100 = VlanId.vlanId((short) 100);
117 protected static final VlanId VLAN200 = VlanId.vlanId((short) 200);
118 protected static final VlanId VLAN300 = VlanId.vlanId((short) 300);
119 protected static final VlanId VLAN400 = VlanId.vlanId((short) 400);
120 protected static final VlanId VLAN_NONE = VlanId.NONE;
Luca Prete9c2ee072016-02-16 11:00:44 -0800121
Yi Tsengf4e13e32017-03-30 15:38:39 -0700122 protected static final MacAddress MAC1 = getMac(1);
123 protected static final MacAddress MAC2 = getMac(2);
124 protected static final MacAddress MAC3 = getMac(3);
125 protected static final MacAddress MAC4 = getMac(4);
126 protected static final MacAddress MAC5 = getMac(5);
127 protected static final MacAddress MAC6 = getMac(6);
128 protected static final MacAddress MAC7 = getMac(7);
129 protected static final MacAddress MAC8 = getMac(8);
130 protected static final MacAddress MAC9 = getMac(9);
131 protected static final MacAddress MAC10 = getMac(10);
132 protected static final MacAddress MAC11 = getMac(11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800133
Yi Tsengf4e13e32017-03-30 15:38:39 -0700134 protected static final Ip4Address IP1 = Ip4Address.valueOf("192.168.1.1");
135 protected static final Ip4Address IP2 = Ip4Address.valueOf("192.168.1.2");
nosignal5fd282e2016-09-16 16:11:40 -0700136
Yi Tsengf4e13e32017-03-30 15:38:39 -0700137 protected static final HostId HID1 = HostId.hostId(MAC1, VLAN100);
138 protected static final HostId HID2 = HostId.hostId(MAC2, VLAN100);
139 protected static final HostId HID3 = HostId.hostId(MAC3, VLAN200);
140 protected static final HostId HID4 = HostId.hostId(MAC4, VLAN200);
141 protected static final HostId HID5 = HostId.hostId(MAC5, VLAN300);
142 protected static final HostId HID6 = HostId.hostId(MAC6, VLAN300);
143 protected static final HostId HID7 = HostId.hostId(MAC7, VLAN300);
144 protected static final HostId HID8 = HostId.hostId(MAC8, VLAN400);
145 protected static final HostId HID9 = HostId.hostId(MAC9);
146 protected static final HostId HID10 = HostId.hostId(MAC10);
147 protected static final HostId HID11 = HostId.hostId(MAC11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800148
Yi Tsengf4e13e32017-03-30 15:38:39 -0700149 protected static final ProviderId PID = new ProviderId("of", "foo");
Luca Prete9c2ee072016-02-16 11:00:44 -0800150
Yi Tsengf4e13e32017-03-30 15:38:39 -0700151 protected static final NodeId NODE_ID_1 = new NodeId("Node1");
152 protected static final NodeId NODE_ID_2 = new NodeId("Node2");
Luca Prete9c2ee072016-02-16 11:00:44 -0800153
Yi Tsengf4e13e32017-03-30 15:38:39 -0700154 protected static IdGenerator idGenerator;
155
156 protected static final Interface V100H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700157 new Interface("v100h1", CP1, null, null, VLAN100);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700158 protected static final Interface V100H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700159 new Interface("v100h2", CP2, null, null, VLAN100);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700160 protected static final Interface V200H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700161 new Interface("v200h1", CP3, null, null, VLAN200);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700162 protected static final Interface V200H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700163 new Interface("v200h2", CP4, null, null, VLAN200);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700164 protected static final Interface V300H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700165 new Interface("v300h1", CP5, null, null, VLAN300);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700166 protected static final Interface V300H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700167 new Interface("v300h2", CP6, null, null, VLAN300);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700168 protected static final Interface V400H1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100169 new Interface("v400h1", CP7, null, null, VLAN400);
170
Yi Tsengf4e13e32017-03-30 15:38:39 -0700171 protected static final Interface VNONEH1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100172 new Interface("vNoneh1", CP8, null, null, VLAN_NONE);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700173 protected static final Interface VNONEH2 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100174 new Interface("vNoneh2", CP9, null, null, VLAN_NONE);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700175 protected static final Interface VNONEH3 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100176 new Interface("vNoneh3", CP10, null, null, VLAN_NONE);
nosignal5fd282e2016-09-16 16:11:40 -0700177
Yi Tsengf4e13e32017-03-30 15:38:39 -0700178 protected static final Host V100HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700179 new DefaultHost(PID, HID1, MAC1, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100180 getLocation(1), Collections.singleton(IP1));
Yi Tsengf4e13e32017-03-30 15:38:39 -0700181 protected static final Host V100HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700182 new DefaultHost(PID, HID2, MAC2, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100183 getLocation(2), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700184 protected static final Host V200HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700185 new DefaultHost(PID, HID3, MAC3, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100186 getLocation(3), Collections.singleton(IP2));
Yi Tsengf4e13e32017-03-30 15:38:39 -0700187 protected static final Host V200HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700188 new DefaultHost(PID, HID4, MAC4, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100189 getLocation(4), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700190 protected static final Host V300HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700191 new DefaultHost(PID, HID5, MAC5, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100192 getLocation(5), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700193 protected static final Host V300HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700194 new DefaultHost(PID, HID6, MAC6, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100195 getLocation(6), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700196 protected static final Host V300HOST3 =
nosignal5fd282e2016-09-16 16:11:40 -0700197 new DefaultHost(PID, HID7, MAC7, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100198 getLocation(7), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700199 protected static final Host V400HOST1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100200 new DefaultHost(PID, HID8, MAC8, VLAN400,
201 getLocation(4, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700202
Yi Tsengf4e13e32017-03-30 15:38:39 -0700203 protected static final Host VNONEHOST1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100204 new DefaultHost(PID, HID9, MAC9, VlanId.NONE,
205 getLocation(3, 2), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700206 protected static final Host VNONEHOST2 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100207 new DefaultHost(PID, HID10, MAC10, VlanId.NONE,
208 getLocation(5, 1), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700209 protected static final Host VNONEHOST3 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100210 new DefaultHost(PID, HID11, MAC11, VlanId.NONE,
211 getLocation(5, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700212
Yi Tsengf4e13e32017-03-30 15:38:39 -0700213 protected static final Set<Interface> AVAILABLE_INTERFACES =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100214 ImmutableSet.of(V100H1, V100H2, V200H1, V200H2, V300H1, V300H2,
Yi Tsengf4e13e32017-03-30 15:38:39 -0700215 V400H1, VNONEH1, VNONEH2, VNONEH3);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100216
Yi Tsengf4e13e32017-03-30 15:38:39 -0700217 protected static final Set<Host> AVAILABLE_HOSTS =
Luca Prete092e8952016-10-26 16:25:56 +0200218 ImmutableSet.of(V100HOST1, V100HOST2, V200HOST1,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100219 V200HOST2, V300HOST1, V300HOST2, V300HOST3,
220 VNONEHOST1, VNONEHOST2,
221 V400HOST1, VNONEHOST3);
222
nosignal5fd282e2016-09-16 16:11:40 -0700223
224 /**
225 * Returns the device Id of the ith device.
226 *
227 * @param i the device to get the Id of
228 * @return the device Id
Luca Prete9c2ee072016-02-16 11:00:44 -0800229 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700230 protected static DeviceId getDeviceId(int i) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800231 return DeviceId.deviceId("" + i);
232 }
233
Yi Tsengf4e13e32017-03-30 15:38:39 -0700234 /**
235 * Generates a mac address by given number.
236 *
237 * @param n the number to generate mac address
238 * @return the mac address
239 */
240 protected static MacAddress getMac(int n) {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100241 return MacAddress.valueOf(String.format("00:00:00:00:00:%s", n));
242 }
243
Yi Tsengf4e13e32017-03-30 15:38:39 -0700244 /**
245 * Generates a host location by given device number.
246 *
247 * @param i the given number
248 * @return the host location
249 */
250 protected static HostLocation getLocation(int i) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800251 return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L);
252 }
253
Yi Tsengf4e13e32017-03-30 15:38:39 -0700254 /**
255 * Generates host location by given device number and port number.
256 *
257 * @param d the device number
258 * @param p the port number
259 * @return the host location
260 */
261 protected static HostLocation getLocation(int d, int p) {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100262 return new HostLocation(new ConnectPoint(getDeviceId(d),
263 PortNumber.portNumber(p)), 123L);
264 }
265
Luca Prete9c2ee072016-02-16 11:00:44 -0800266 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700267 * Test core service; For generate test application ID.
Luca Prete9c2ee072016-02-16 11:00:44 -0800268 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700269 public class TestCoreService extends CoreServiceAdapter {
270 @Override
271 public ApplicationId registerApplication(String name) {
272 return TestApplicationId.create(name);
273 }
274 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800275
Yi Tsengf4e13e32017-03-30 15:38:39 -0700276 /**
277 * Test intent service.
278 * Always install or withdraw success for any Intents.
279 */
280 public class TestIntentService extends IntentServiceAdapter {
281 IntentListener listener;
282 List<IntentData> intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800283
284 public TestIntentService() {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700285 intents = Lists.newArrayList();
Luca Prete9c2ee072016-02-16 11:00:44 -0800286 }
287
288 @Override
289 public void submit(Intent intent) {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700290 intents.add(new IntentData(intent, IntentState.INSTALLED, new WallClockTimestamp()));
291 if (listener != null) {
292 IntentEvent.getEvent(IntentState.INSTALLED, intent).ifPresent(listener::event);
293
294 }
295 }
296
297 @Override
298 public void withdraw(Intent intent) {
299 intents.forEach(intentData -> {
300 if (intentData.intent().key().equals(intent.key())) {
301 intentData.setState(IntentState.WITHDRAWN);
302
303 if (listener != null) {
304 IntentEvent.getEvent(IntentState.WITHDRAWN, intent).ifPresent(listener::event);
305 }
306 }
307 });
308 }
309
310 @Override
311 public Iterable<Intent> getIntents() {
312 return intents.stream()
313 .map(IntentData::intent)
314 .collect(Collectors.toList());
Luca Prete9c2ee072016-02-16 11:00:44 -0800315 }
316
317 @Override
318 public long getIntentCount() {
319 return intents.size();
320 }
321
322 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700323 public void addListener(IntentListener listener) {
324 this.listener = listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800325 }
326
327 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700328 public void removeListener(IntentListener listener) {
329 this.listener = null;
Luca Prete9c2ee072016-02-16 11:00:44 -0800330 }
331 }
332
333 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700334 * Test leadership service.
Luca Prete9c2ee072016-02-16 11:00:44 -0800335 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700336 public class TestLeadershipService extends LeadershipServiceAdapter {
337 LeadershipEventListener listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800338
Yi Tsengf4e13e32017-03-30 15:38:39 -0700339 @Override
340 public void addListener(LeadershipEventListener listener) {
341 this.listener = listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800342 }
343
344 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700345 public void removeListener(LeadershipEventListener listener) {
346 this.listener = null;
347 }
348
349 /**
350 * Sends the leadership event to the listener.
351 *
352 * @param event the Intent event
353 */
354 public void sendEvent(LeadershipEvent event) {
355 if (listener != null && listener.isRelevant(event)) {
356 listener.event(event);
357 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800358 }
359
360 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700361 public NodeId getLeader(String path) {
362 return NODE_ID_1;
363 }
364 }
365
366 /**
367 * Test interface service; contains all interfaces which already generated.
368 */
369 public class TestInterfaceService implements InterfaceService {
370
371 @Override
372 public void addListener(InterfaceListener listener) {
373 }
374
375 @Override
376 public void removeListener(InterfaceListener listener) {
377 }
378
379 @Override
380 public Set<Interface> getInterfaces() {
381 return AVAILABLE_INTERFACES;
382 }
383
384 @Override
385 public Interface getInterfaceByName(ConnectPoint connectPoint,
386 String name) {
387 return AVAILABLE_INTERFACES.stream()
388 .filter(intf -> intf.name().equals(name))
389 .findFirst()
390 .orElse(null);
391 }
392
393 @Override
394 public Set<Interface> getInterfacesByPort(ConnectPoint port) {
395 return AVAILABLE_INTERFACES.stream()
396 .filter(intf -> intf.connectPoint().equals(port))
Luca Prete9c2ee072016-02-16 11:00:44 -0800397 .collect(Collectors.toSet());
398 }
399
Yi Tsengf4e13e32017-03-30 15:38:39 -0700400 @Override
401 public Set<Interface> getInterfacesByIp(IpAddress ip) {
402 return AVAILABLE_INTERFACES.stream()
403 .filter(intf -> intf.ipAddressesList().contains(ip))
404 .collect(Collectors.toSet());
405 }
406
407 @Override
408 public Set<Interface> getInterfacesByVlan(VlanId vlan) {
409 return AVAILABLE_INTERFACES.stream()
410 .filter(intf -> intf.vlan().equals(vlan))
411 .collect(Collectors.toSet());
412 }
413
414 @Override
415 public Interface getMatchingInterface(IpAddress ip) {
416 return AVAILABLE_INTERFACES.stream()
417 .filter(intf -> intf.ipAddressesList().contains(ip))
418 .findFirst()
419 .orElse(null);
420 }
421
422 @Override
423 public Set<Interface> getMatchingInterfaces(IpAddress ip) {
424 return AVAILABLE_INTERFACES.stream()
425 .filter(intf -> intf.ipAddressesList().contains(ip))
426 .collect(Collectors.toSet());
427 }
428 }
429
430 /**
431 * Test VPLS store.
432 */
433 public class TestVplsStore extends VplsStoreAdapter {
434 /**
435 * Clears the store.
436 */
437 public void clear() {
438 vplsDataMap.clear();
439 }
440
441 /**
442 * Gets the store delegate.
443 *
444 * @return the store delegate
445 */
446 public StoreDelegate<VplsStoreEvent> delegate() {
447 return this.delegate;
448 }
449 }
450
451 /**
452 * Test VPLS.
453 * Provides basic VPLS functionality and stores VPLS information.
454 */
455 public class TestVpls implements Vpls {
456 public Map<String, VplsData> testData;
457
458 public TestVpls() {
459 testData = Maps.newHashMap();
460 }
461
462 public void initSampleData() {
463 testData.clear();
464 VplsData vplsData = VplsData.of(VPLS1);
465 vplsData.addInterfaces(ImmutableSet.of(V100H1, V100H2));
466 vplsData.state(VplsData.VplsState.ADDED);
467 testData.put(VPLS1, vplsData);
468
469 vplsData = VplsData.of(VPLS2);
470 vplsData.addInterfaces(ImmutableSet.of(V200H1, V200H2));
471 vplsData.state(VplsData.VplsState.ADDED);
472 testData.put(VPLS2, vplsData);
473 }
474
475 @Override
476 public VplsData createVpls(String vplsName, EncapsulationType encapsulationType) {
477 VplsData vplsData = VplsData.of(vplsName, encapsulationType);
478 testData.put(vplsName, vplsData);
479 return vplsData;
480 }
481
482 @Override
483 public VplsData removeVpls(VplsData vplsData) {
484 if (!testData.containsKey(vplsData.name())) {
485 return null;
486 }
487
488 testData.remove(vplsData.name());
489 return vplsData;
490 }
491
492 @Override
493 public void addInterfaces(VplsData vplsData, Collection<Interface> interfaces) {
494 vplsData.addInterfaces(interfaces);
495 testData.put(vplsData.name(), vplsData);
496 }
497
498 @Override
499 public void addInterface(VplsData vplsData, Interface iface) {
500 vplsData.addInterface(iface);
501 testData.put(vplsData.name(), vplsData);
502 }
503
504 @Override
505 public void setEncapsulationType(VplsData vplsData, EncapsulationType encapsulationType) {
506 vplsData.encapsulationType(encapsulationType);
507 testData.put(vplsData.name(), vplsData);
508 }
509
510 @Override
511 public VplsData getVpls(String vplsName) {
512 return testData.get(vplsName);
513 }
514
515 @Override
516 public Collection<VplsData> getAllVpls() {
517 return testData.values();
518 }
519
520 @Override
521 public Collection<Interface> removeInterfaces(VplsData vplsData, Collection<Interface> interfaces) {
522 vplsData.removeInterfaces(interfaces);
523 testData.put(vplsData.name(), vplsData);
524 return interfaces;
525 }
526
527 @Override
528 public Interface removeInterface(VplsData vplsData, Interface iface) {
529 vplsData.removeInterface(iface);
530 testData.put(vplsData.name(), vplsData);
531 return iface;
532 }
533
534 @Override
535 public void removeAllVpls() {
536 testData.clear();
537 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800538 }
539
nosignal5fd282e2016-09-16 16:11:40 -0700540 /**
541 * Represents a fake IdGenerator class for intents.
542 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700543 protected static class TestIdGenerator implements IdGenerator {
Luca Prete9c2ee072016-02-16 11:00:44 -0800544 private final AtomicLong id = new AtomicLong(0);
545
546 @Override
547 public long getNewId() {
548 return id.getAndIncrement();
549 }
550
551 }
552
553 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700554 * Test host service; contains all hosts which already generated.
555 *
Luca Prete9c2ee072016-02-16 11:00:44 -0800556 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700557 public class TestHostService extends HostServiceAdapter {
Luca Prete9c2ee072016-02-16 11:00:44 -0800558
Yi Tsengf4e13e32017-03-30 15:38:39 -0700559 private HostListener listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800560
Yi Tsengf4e13e32017-03-30 15:38:39 -0700561 @Override
562 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
563 return AVAILABLE_HOSTS.stream()
564 .filter(host -> host.location().equals(connectPoint))
565 .collect(Collectors.toSet());
Luca Prete9c2ee072016-02-16 11:00:44 -0800566 }
567
568 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700569 public Set<Host> getHostsByMac(MacAddress mac) {
570 return AVAILABLE_HOSTS.stream()
571 .filter(host -> host.mac().equals(mac))
572 .collect(Collectors.toSet());
Luca Prete9c2ee072016-02-16 11:00:44 -0800573 }
574
575 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700576 public Iterable<Host> getHosts() {
577 return AVAILABLE_HOSTS;
Luca Prete9c2ee072016-02-16 11:00:44 -0800578 }
579
580 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700581 public Set<Host> getHostsByVlan(VlanId vlanId) {
582 return AVAILABLE_HOSTS.stream()
583 .filter(host -> host.vlan().equals(vlanId))
584 .collect(Collectors.toSet());
585 }
586
587 @Override
588 public int getHostCount() {
589 return AVAILABLE_HOSTS.size();
590 }
591
592 @Override
593 public Host getHost(HostId hostId) {
594 return AVAILABLE_HOSTS.stream()
595 .filter(host -> host.id().equals(hostId))
596 .findFirst()
597 .orElse(null);
598 }
599
600 @Override
601 public void addListener(HostListener listener) {
602 this.listener = listener;
603 }
604
605 public void postHostEvent(HostEvent hostEvent) {
606 this.listener.event(hostEvent);
Luca Prete9c2ee072016-02-16 11:00:44 -0800607 }
608 }
609
nosignal5fd282e2016-09-16 16:11:40 -0700610 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700611 * Test network configuration service.
nosignal5fd282e2016-09-16 16:11:40 -0700612 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700613 public class TestConfigService extends NetworkConfigServiceAdapter {
614 public static final String EMPTY_JSON_TREE = "{}";
615 NetworkConfigListener listener;
616 VplsAppConfig vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700617
Yi Tsengf4e13e32017-03-30 15:38:39 -0700618 @Override
619 public void addListener(NetworkConfigListener listener) {
620 this.listener = listener;
nosignal5fd282e2016-09-16 16:11:40 -0700621 }
622
623 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700624 public void removeListener(NetworkConfigListener listener) {
625 this.listener = null;
626 }
627
628 /**
629 * Sends network config event to listener.
630 *
631 * @param event the network config event
632 */
633 public void sendEvent(NetworkConfigEvent event) {
634 if (listener != null) {
635 listener.event(event);
nosignal5fd282e2016-09-16 16:11:40 -0700636 }
637 }
638
Yi Tsengf4e13e32017-03-30 15:38:39 -0700639 /**
640 * Constructs test config service.
641 * Generates an VPLS configuration with sample VPLS configs.
642 */
643 public TestConfigService() {
644 vplsAppConfig = new VplsAppConfig();
645 final ObjectMapper mapper = new ObjectMapper();
646 final ConfigApplyDelegate delegate = new VplsAppConfigTest.MockCfgDelegate();
647 JsonNode tree = null;
648 try {
649 tree = new ObjectMapper().readTree(EMPTY_JSON_TREE);
650 } catch (IOException e) {
651 e.printStackTrace();
nosignal5fd282e2016-09-16 16:11:40 -0700652 }
Yi Tsengf4e13e32017-03-30 15:38:39 -0700653 vplsAppConfig.init(APPID, APP_NAME, tree, mapper, delegate);
654 VplsConfig vplsConfig = new VplsConfig(VPLS1,
655 ImmutableSet.of(V100H1.name(), V100H2.name()),
656 EncapsulationType.NONE);
657 vplsAppConfig.addVpls(vplsConfig);
658 vplsConfig = new VplsConfig(VPLS2,
659 ImmutableSet.of(V200H1.name(), V200H2.name()),
660 EncapsulationType.VLAN);
661 vplsAppConfig.addVpls(vplsConfig);
662
663 }
664
665 /**
666 * Overrides VPLS config to the config service.
667 *
668 * @param vplsAppConfig the new VPLS config
669 */
670 public void setConfig(VplsAppConfig vplsAppConfig) {
671 this.vplsAppConfig = vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700672 }
673
674 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700675 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
676 return (C) vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700677 }
678
679 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700680 public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
681 return (C) vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700682 }
nosignal5fd282e2016-09-16 16:11:40 -0700683 }
Yi Tsengf4e13e32017-03-30 15:38:39 -0700684
Luca Prete9c2ee072016-02-16 11:00:44 -0800685}