blob: e1b5c1737a37025ddddd2feab22de052a1c5eb5c [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;
Luca Prete9c2ee072016-02-16 11:00:44 -080036import org.onosproject.incubator.net.intf.Interface;
Luca Pretea8854822016-04-26 16:30:55 -070037import org.onosproject.incubator.net.intf.InterfaceListener;
Luca Prete9c2ee072016-02-16 11:00:44 -080038import org.onosproject.incubator.net.intf.InterfaceService;
39import 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;
55import org.onosproject.net.intent.Intent;
Yi Tsengf4e13e32017-03-30 15:38:39 -070056import org.onosproject.net.intent.IntentData;
57import org.onosproject.net.intent.IntentEvent;
58import org.onosproject.net.intent.IntentListener;
Luca Prete9c2ee072016-02-16 11:00:44 -080059import org.onosproject.net.intent.IntentServiceAdapter;
Yi Tsengf4e13e32017-03-30 15:38:39 -070060import org.onosproject.net.intent.IntentState;
Luca Prete9c2ee072016-02-16 11:00:44 -080061import org.onosproject.net.provider.ProviderId;
Yi Tsengf4e13e32017-03-30 15:38:39 -070062import org.onosproject.store.StoreDelegate;
63import org.onosproject.store.service.WallClockTimestamp;
64import org.onosproject.vpls.api.Vpls;
65import org.onosproject.vpls.api.VplsData;
66import org.onosproject.vpls.config.VplsAppConfig;
67import org.onosproject.vpls.config.VplsAppConfigTest;
68import org.onosproject.vpls.config.VplsConfig;
69import org.onosproject.vpls.store.VplsStoreAdapter;
70import org.onosproject.vpls.store.VplsStoreEvent;
Luca Prete9c2ee072016-02-16 11:00:44 -080071
Yi Tsengf4e13e32017-03-30 15:38:39 -070072import java.io.IOException;
Luca Prete13465642017-01-04 10:31:48 -080073import java.util.Collection;
74import java.util.Collections;
Luca Prete13465642017-01-04 10:31:48 -080075import java.util.List;
76import java.util.Map;
77import java.util.Set;
Luca Prete13465642017-01-04 10:31:48 -080078import java.util.stream.Collectors;
79
Luca Prete9c2ee072016-02-16 11:00:44 -080080/**
Yi Tsengf4e13e32017-03-30 15:38:39 -070081 * Class provides data for VPLS testing.
Luca Prete9c2ee072016-02-16 11:00:44 -080082 */
Yi Tsengf4e13e32017-03-30 15:38:39 -070083public abstract class VplsTest {
84 protected static final String APP_NAME = "org.onosproject.vpls";
85 protected static final ApplicationId APPID = TestApplicationId.create(APP_NAME);
86 protected static final String DASH = "-";
87 protected static final int PRIORITY_OFFSET = 1000;
88 protected static final String VPLS1 = "vpls1";
89 protected static final String VPLS2 = "vpls2";
90 protected static final String VPLS3 = "vpls3";
91 protected static final String VPLS4 = "vpls4";
Luca Prete9c2ee072016-02-16 11:00:44 -080092
Yi Tsengf4e13e32017-03-30 15:38:39 -070093 protected static final PortNumber P1 = PortNumber.portNumber(1);
94 protected static final PortNumber P2 = PortNumber.portNumber(2);
Luca Prete9c2ee072016-02-16 11:00:44 -080095
Yi Tsengf4e13e32017-03-30 15:38:39 -070096 protected static final DeviceId DID1 = getDeviceId(1);
97 protected static final DeviceId DID2 = getDeviceId(2);
98 protected static final DeviceId DID3 = getDeviceId(3);
99 protected static final DeviceId DID4 = getDeviceId(4);
100 protected static final DeviceId DID5 = getDeviceId(5);
101 protected static final DeviceId DID6 = getDeviceId(6);
Luca Prete9c2ee072016-02-16 11:00:44 -0800102
Yi Tsengf4e13e32017-03-30 15:38:39 -0700103 protected static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
104 protected static final ConnectPoint CP2 = new ConnectPoint(DID2, P1);
105 protected static final ConnectPoint CP3 = new ConnectPoint(DID3, P1);
106 protected static final ConnectPoint CP4 = new ConnectPoint(DID4, P1);
107 protected static final ConnectPoint CP5 = new ConnectPoint(DID5, P1);
108 protected static final ConnectPoint CP6 = new ConnectPoint(DID6, P1);
109 protected static final ConnectPoint CP7 = new ConnectPoint(DID4, P2);
110 protected static final ConnectPoint CP8 = new ConnectPoint(DID3, P2);
111 protected static final ConnectPoint CP9 = new ConnectPoint(DID5, P1);
112 protected static final ConnectPoint CP10 = new ConnectPoint(DID5, P2);
Luca Prete9c2ee072016-02-16 11:00:44 -0800113
Yi Tsengf4e13e32017-03-30 15:38:39 -0700114 protected static final VlanId VLAN100 = VlanId.vlanId((short) 100);
115 protected static final VlanId VLAN200 = VlanId.vlanId((short) 200);
116 protected static final VlanId VLAN300 = VlanId.vlanId((short) 300);
117 protected static final VlanId VLAN400 = VlanId.vlanId((short) 400);
118 protected static final VlanId VLAN_NONE = VlanId.NONE;
Luca Prete9c2ee072016-02-16 11:00:44 -0800119
Yi Tsengf4e13e32017-03-30 15:38:39 -0700120 protected static final MacAddress MAC1 = getMac(1);
121 protected static final MacAddress MAC2 = getMac(2);
122 protected static final MacAddress MAC3 = getMac(3);
123 protected static final MacAddress MAC4 = getMac(4);
124 protected static final MacAddress MAC5 = getMac(5);
125 protected static final MacAddress MAC6 = getMac(6);
126 protected static final MacAddress MAC7 = getMac(7);
127 protected static final MacAddress MAC8 = getMac(8);
128 protected static final MacAddress MAC9 = getMac(9);
129 protected static final MacAddress MAC10 = getMac(10);
130 protected static final MacAddress MAC11 = getMac(11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800131
Yi Tsengf4e13e32017-03-30 15:38:39 -0700132 protected static final Ip4Address IP1 = Ip4Address.valueOf("192.168.1.1");
133 protected static final Ip4Address IP2 = Ip4Address.valueOf("192.168.1.2");
nosignal5fd282e2016-09-16 16:11:40 -0700134
Yi Tsengf4e13e32017-03-30 15:38:39 -0700135 protected static final HostId HID1 = HostId.hostId(MAC1, VLAN100);
136 protected static final HostId HID2 = HostId.hostId(MAC2, VLAN100);
137 protected static final HostId HID3 = HostId.hostId(MAC3, VLAN200);
138 protected static final HostId HID4 = HostId.hostId(MAC4, VLAN200);
139 protected static final HostId HID5 = HostId.hostId(MAC5, VLAN300);
140 protected static final HostId HID6 = HostId.hostId(MAC6, VLAN300);
141 protected static final HostId HID7 = HostId.hostId(MAC7, VLAN300);
142 protected static final HostId HID8 = HostId.hostId(MAC8, VLAN400);
143 protected static final HostId HID9 = HostId.hostId(MAC9);
144 protected static final HostId HID10 = HostId.hostId(MAC10);
145 protected static final HostId HID11 = HostId.hostId(MAC11);
Luca Prete9c2ee072016-02-16 11:00:44 -0800146
Yi Tsengf4e13e32017-03-30 15:38:39 -0700147 protected static final ProviderId PID = new ProviderId("of", "foo");
Luca Prete9c2ee072016-02-16 11:00:44 -0800148
Yi Tsengf4e13e32017-03-30 15:38:39 -0700149 protected static final NodeId NODE_ID_1 = new NodeId("Node1");
150 protected static final NodeId NODE_ID_2 = new NodeId("Node2");
Luca Prete9c2ee072016-02-16 11:00:44 -0800151
Yi Tsengf4e13e32017-03-30 15:38:39 -0700152 protected static final Interface V100H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700153 new Interface("v100h1", CP1, null, null, VLAN100);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700154 protected static final Interface V100H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700155 new Interface("v100h2", CP2, null, null, VLAN100);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700156 protected static final Interface V200H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700157 new Interface("v200h1", CP3, null, null, VLAN200);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700158 protected static final Interface V200H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700159 new Interface("v200h2", CP4, null, null, VLAN200);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700160 protected static final Interface V300H1 =
nosignal5fd282e2016-09-16 16:11:40 -0700161 new Interface("v300h1", CP5, null, null, VLAN300);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700162 protected static final Interface V300H2 =
nosignal5fd282e2016-09-16 16:11:40 -0700163 new Interface("v300h2", CP6, null, null, VLAN300);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700164 protected static final Interface V400H1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100165 new Interface("v400h1", CP7, null, null, VLAN400);
166
Yi Tsengf4e13e32017-03-30 15:38:39 -0700167 protected static final Interface VNONEH1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100168 new Interface("vNoneh1", CP8, null, null, VLAN_NONE);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700169 protected static final Interface VNONEH2 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100170 new Interface("vNoneh2", CP9, null, null, VLAN_NONE);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700171 protected static final Interface VNONEH3 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100172 new Interface("vNoneh3", CP10, null, null, VLAN_NONE);
nosignal5fd282e2016-09-16 16:11:40 -0700173
Yi Tsengf4e13e32017-03-30 15:38:39 -0700174 protected static final Host V100HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700175 new DefaultHost(PID, HID1, MAC1, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100176 getLocation(1), Collections.singleton(IP1));
Yi Tsengf4e13e32017-03-30 15:38:39 -0700177 protected static final Host V100HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700178 new DefaultHost(PID, HID2, MAC2, VLAN100,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100179 getLocation(2), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700180 protected static final Host V200HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700181 new DefaultHost(PID, HID3, MAC3, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100182 getLocation(3), Collections.singleton(IP2));
Yi Tsengf4e13e32017-03-30 15:38:39 -0700183 protected static final Host V200HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700184 new DefaultHost(PID, HID4, MAC4, VLAN200,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100185 getLocation(4), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700186 protected static final Host V300HOST1 =
nosignal5fd282e2016-09-16 16:11:40 -0700187 new DefaultHost(PID, HID5, MAC5, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100188 getLocation(5), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700189 protected static final Host V300HOST2 =
nosignal5fd282e2016-09-16 16:11:40 -0700190 new DefaultHost(PID, HID6, MAC6, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100191 getLocation(6), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700192 protected static final Host V300HOST3 =
nosignal5fd282e2016-09-16 16:11:40 -0700193 new DefaultHost(PID, HID7, MAC7, VLAN300,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100194 getLocation(7), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700195 protected static final Host V400HOST1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100196 new DefaultHost(PID, HID8, MAC8, VLAN400,
197 getLocation(4, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700198
Yi Tsengf4e13e32017-03-30 15:38:39 -0700199 protected static final Host VNONEHOST1 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100200 new DefaultHost(PID, HID9, MAC9, VlanId.NONE,
201 getLocation(3, 2), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700202 protected static final Host VNONEHOST2 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100203 new DefaultHost(PID, HID10, MAC10, VlanId.NONE,
204 getLocation(5, 1), Sets.newHashSet());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700205 protected static final Host VNONEHOST3 =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100206 new DefaultHost(PID, HID11, MAC11, VlanId.NONE,
207 getLocation(5, 2), Sets.newHashSet());
nosignal5fd282e2016-09-16 16:11:40 -0700208
Yi Tsengf4e13e32017-03-30 15:38:39 -0700209 protected static final Set<Interface> AVAILABLE_INTERFACES =
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100210 ImmutableSet.of(V100H1, V100H2, V200H1, V200H2, V300H1, V300H2,
Yi Tsengf4e13e32017-03-30 15:38:39 -0700211 V400H1, VNONEH1, VNONEH2, VNONEH3);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100212
Yi Tsengf4e13e32017-03-30 15:38:39 -0700213 protected static final Set<Host> AVAILABLE_HOSTS =
Luca Prete092e8952016-10-26 16:25:56 +0200214 ImmutableSet.of(V100HOST1, V100HOST2, V200HOST1,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100215 V200HOST2, V300HOST1, V300HOST2, V300HOST3,
216 VNONEHOST1, VNONEHOST2,
217 V400HOST1, VNONEHOST3);
218
nosignal5fd282e2016-09-16 16:11:40 -0700219
220 /**
221 * Returns the device Id of the ith device.
222 *
223 * @param i the device to get the Id of
224 * @return the device Id
Luca Prete9c2ee072016-02-16 11:00:44 -0800225 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700226 protected static DeviceId getDeviceId(int i) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800227 return DeviceId.deviceId("" + i);
228 }
229
Yi Tsengf4e13e32017-03-30 15:38:39 -0700230 /**
231 * Generates a mac address by given number.
232 *
233 * @param n the number to generate mac address
234 * @return the mac address
235 */
236 protected static MacAddress getMac(int n) {
Dominika Molenda52cc3a92017-06-19 17:08:35 +0200237 return MacAddress.valueOf(String.format("00:00:00:00:00:%02x", n));
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100238 }
239
Yi Tsengf4e13e32017-03-30 15:38:39 -0700240 /**
241 * Generates a host location by given device number.
242 *
243 * @param i the given number
244 * @return the host location
245 */
246 protected static HostLocation getLocation(int i) {
Luca Prete9c2ee072016-02-16 11:00:44 -0800247 return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L);
248 }
249
Yi Tsengf4e13e32017-03-30 15:38:39 -0700250 /**
251 * Generates host location by given device number and port number.
252 *
253 * @param d the device number
254 * @param p the port number
255 * @return the host location
256 */
257 protected static HostLocation getLocation(int d, int p) {
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100258 return new HostLocation(new ConnectPoint(getDeviceId(d),
259 PortNumber.portNumber(p)), 123L);
260 }
261
Luca Prete9c2ee072016-02-16 11:00:44 -0800262 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700263 * Test core service; For generate test application ID.
Luca Prete9c2ee072016-02-16 11:00:44 -0800264 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700265 public class TestCoreService extends CoreServiceAdapter {
266 @Override
267 public ApplicationId registerApplication(String name) {
268 return TestApplicationId.create(name);
269 }
270 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800271
Yi Tsengf4e13e32017-03-30 15:38:39 -0700272 /**
273 * Test intent service.
274 * Always install or withdraw success for any Intents.
275 */
276 public class TestIntentService extends IntentServiceAdapter {
277 IntentListener listener;
278 List<IntentData> intents;
Luca Prete9c2ee072016-02-16 11:00:44 -0800279
280 public TestIntentService() {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700281 intents = Lists.newArrayList();
Luca Prete9c2ee072016-02-16 11:00:44 -0800282 }
283
284 @Override
285 public void submit(Intent intent) {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700286 intents.add(new IntentData(intent, IntentState.INSTALLED, new WallClockTimestamp()));
287 if (listener != null) {
288 IntentEvent.getEvent(IntentState.INSTALLED, intent).ifPresent(listener::event);
289
290 }
291 }
292
293 @Override
294 public void withdraw(Intent intent) {
295 intents.forEach(intentData -> {
296 if (intentData.intent().key().equals(intent.key())) {
297 intentData.setState(IntentState.WITHDRAWN);
298
299 if (listener != null) {
300 IntentEvent.getEvent(IntentState.WITHDRAWN, intent).ifPresent(listener::event);
301 }
302 }
303 });
304 }
305
306 @Override
307 public Iterable<Intent> getIntents() {
308 return intents.stream()
309 .map(IntentData::intent)
310 .collect(Collectors.toList());
Luca Prete9c2ee072016-02-16 11:00:44 -0800311 }
312
313 @Override
314 public long getIntentCount() {
315 return intents.size();
316 }
317
318 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700319 public void addListener(IntentListener listener) {
320 this.listener = listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800321 }
322
323 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700324 public void removeListener(IntentListener listener) {
325 this.listener = null;
Luca Prete9c2ee072016-02-16 11:00:44 -0800326 }
327 }
328
329 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700330 * Test leadership service.
Luca Prete9c2ee072016-02-16 11:00:44 -0800331 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700332 public class TestLeadershipService extends LeadershipServiceAdapter {
333 LeadershipEventListener listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800334
Yi Tsengf4e13e32017-03-30 15:38:39 -0700335 @Override
336 public void addListener(LeadershipEventListener listener) {
337 this.listener = listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800338 }
339
340 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700341 public void removeListener(LeadershipEventListener listener) {
342 this.listener = null;
343 }
344
345 /**
346 * Sends the leadership event to the listener.
347 *
348 * @param event the Intent event
349 */
350 public void sendEvent(LeadershipEvent event) {
351 if (listener != null && listener.isRelevant(event)) {
352 listener.event(event);
353 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800354 }
355
356 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700357 public NodeId getLeader(String path) {
358 return NODE_ID_1;
359 }
360 }
361
362 /**
363 * Test interface service; contains all interfaces which already generated.
364 */
365 public class TestInterfaceService implements InterfaceService {
366
367 @Override
368 public void addListener(InterfaceListener listener) {
369 }
370
371 @Override
372 public void removeListener(InterfaceListener listener) {
373 }
374
375 @Override
376 public Set<Interface> getInterfaces() {
377 return AVAILABLE_INTERFACES;
378 }
379
380 @Override
381 public Interface getInterfaceByName(ConnectPoint connectPoint,
382 String name) {
383 return AVAILABLE_INTERFACES.stream()
384 .filter(intf -> intf.name().equals(name))
385 .findFirst()
386 .orElse(null);
387 }
388
389 @Override
390 public Set<Interface> getInterfacesByPort(ConnectPoint port) {
391 return AVAILABLE_INTERFACES.stream()
392 .filter(intf -> intf.connectPoint().equals(port))
Luca Prete9c2ee072016-02-16 11:00:44 -0800393 .collect(Collectors.toSet());
394 }
395
Yi Tsengf4e13e32017-03-30 15:38:39 -0700396 @Override
397 public Set<Interface> getInterfacesByIp(IpAddress ip) {
398 return AVAILABLE_INTERFACES.stream()
399 .filter(intf -> intf.ipAddressesList().contains(ip))
400 .collect(Collectors.toSet());
401 }
402
403 @Override
404 public Set<Interface> getInterfacesByVlan(VlanId vlan) {
405 return AVAILABLE_INTERFACES.stream()
406 .filter(intf -> intf.vlan().equals(vlan))
407 .collect(Collectors.toSet());
408 }
409
410 @Override
411 public Interface getMatchingInterface(IpAddress ip) {
412 return AVAILABLE_INTERFACES.stream()
413 .filter(intf -> intf.ipAddressesList().contains(ip))
414 .findFirst()
415 .orElse(null);
416 }
417
418 @Override
419 public Set<Interface> getMatchingInterfaces(IpAddress ip) {
420 return AVAILABLE_INTERFACES.stream()
421 .filter(intf -> intf.ipAddressesList().contains(ip))
422 .collect(Collectors.toSet());
423 }
424 }
425
426 /**
427 * Test VPLS store.
428 */
429 public class TestVplsStore extends VplsStoreAdapter {
430 /**
431 * Clears the store.
432 */
433 public void clear() {
434 vplsDataMap.clear();
435 }
436
437 /**
438 * Gets the store delegate.
439 *
440 * @return the store delegate
441 */
442 public StoreDelegate<VplsStoreEvent> delegate() {
443 return this.delegate;
444 }
445 }
446
447 /**
448 * Test VPLS.
449 * Provides basic VPLS functionality and stores VPLS information.
450 */
451 public class TestVpls implements Vpls {
452 public Map<String, VplsData> testData;
453
454 public TestVpls() {
455 testData = Maps.newHashMap();
456 }
457
458 public void initSampleData() {
459 testData.clear();
460 VplsData vplsData = VplsData.of(VPLS1);
461 vplsData.addInterfaces(ImmutableSet.of(V100H1, V100H2));
462 vplsData.state(VplsData.VplsState.ADDED);
463 testData.put(VPLS1, vplsData);
464
465 vplsData = VplsData.of(VPLS2);
466 vplsData.addInterfaces(ImmutableSet.of(V200H1, V200H2));
467 vplsData.state(VplsData.VplsState.ADDED);
468 testData.put(VPLS2, vplsData);
469 }
470
471 @Override
472 public VplsData createVpls(String vplsName, EncapsulationType encapsulationType) {
473 VplsData vplsData = VplsData.of(vplsName, encapsulationType);
Yi Tseng3069c612017-05-26 17:09:43 -0700474 vplsData.state(VplsData.VplsState.ADDED);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700475 testData.put(vplsName, vplsData);
476 return vplsData;
477 }
478
479 @Override
480 public VplsData removeVpls(VplsData vplsData) {
481 if (!testData.containsKey(vplsData.name())) {
482 return null;
483 }
484
485 testData.remove(vplsData.name());
486 return vplsData;
487 }
488
489 @Override
490 public void addInterfaces(VplsData vplsData, Collection<Interface> interfaces) {
491 vplsData.addInterfaces(interfaces);
492 testData.put(vplsData.name(), vplsData);
493 }
494
495 @Override
496 public void addInterface(VplsData vplsData, Interface iface) {
497 vplsData.addInterface(iface);
498 testData.put(vplsData.name(), vplsData);
499 }
500
501 @Override
502 public void setEncapsulationType(VplsData vplsData, EncapsulationType encapsulationType) {
503 vplsData.encapsulationType(encapsulationType);
504 testData.put(vplsData.name(), vplsData);
505 }
506
507 @Override
508 public VplsData getVpls(String vplsName) {
509 return testData.get(vplsName);
510 }
511
512 @Override
513 public Collection<VplsData> getAllVpls() {
514 return testData.values();
515 }
516
517 @Override
518 public Collection<Interface> removeInterfaces(VplsData vplsData, Collection<Interface> interfaces) {
519 vplsData.removeInterfaces(interfaces);
520 testData.put(vplsData.name(), vplsData);
521 return interfaces;
522 }
523
524 @Override
525 public Interface removeInterface(VplsData vplsData, Interface iface) {
526 vplsData.removeInterface(iface);
527 testData.put(vplsData.name(), vplsData);
528 return iface;
529 }
530
531 @Override
532 public void removeAllVpls() {
533 testData.clear();
534 }
Luca Prete9c2ee072016-02-16 11:00:44 -0800535 }
536
nosignal5fd282e2016-09-16 16:11:40 -0700537 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700538 * Test host service; contains all hosts which already generated.
539 *
Luca Prete9c2ee072016-02-16 11:00:44 -0800540 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700541 public class TestHostService extends HostServiceAdapter {
Luca Prete9c2ee072016-02-16 11:00:44 -0800542
Yi Tsengf4e13e32017-03-30 15:38:39 -0700543 private HostListener listener;
Luca Prete9c2ee072016-02-16 11:00:44 -0800544
Yi Tsengf4e13e32017-03-30 15:38:39 -0700545 @Override
546 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
547 return AVAILABLE_HOSTS.stream()
548 .filter(host -> host.location().equals(connectPoint))
549 .collect(Collectors.toSet());
Luca Prete9c2ee072016-02-16 11:00:44 -0800550 }
551
552 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700553 public Set<Host> getHostsByMac(MacAddress mac) {
554 return AVAILABLE_HOSTS.stream()
555 .filter(host -> host.mac().equals(mac))
556 .collect(Collectors.toSet());
Luca Prete9c2ee072016-02-16 11:00:44 -0800557 }
558
559 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700560 public Iterable<Host> getHosts() {
561 return AVAILABLE_HOSTS;
Luca Prete9c2ee072016-02-16 11:00:44 -0800562 }
563
564 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700565 public Set<Host> getHostsByVlan(VlanId vlanId) {
566 return AVAILABLE_HOSTS.stream()
567 .filter(host -> host.vlan().equals(vlanId))
568 .collect(Collectors.toSet());
569 }
570
571 @Override
572 public int getHostCount() {
573 return AVAILABLE_HOSTS.size();
574 }
575
576 @Override
577 public Host getHost(HostId hostId) {
578 return AVAILABLE_HOSTS.stream()
579 .filter(host -> host.id().equals(hostId))
580 .findFirst()
581 .orElse(null);
582 }
583
584 @Override
585 public void addListener(HostListener listener) {
586 this.listener = listener;
587 }
588
589 public void postHostEvent(HostEvent hostEvent) {
590 this.listener.event(hostEvent);
Luca Prete9c2ee072016-02-16 11:00:44 -0800591 }
592 }
593
nosignal5fd282e2016-09-16 16:11:40 -0700594 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700595 * Test network configuration service.
nosignal5fd282e2016-09-16 16:11:40 -0700596 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700597 public class TestConfigService extends NetworkConfigServiceAdapter {
598 public static final String EMPTY_JSON_TREE = "{}";
599 NetworkConfigListener listener;
600 VplsAppConfig vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700601
Yi Tsengf4e13e32017-03-30 15:38:39 -0700602 @Override
603 public void addListener(NetworkConfigListener listener) {
604 this.listener = listener;
nosignal5fd282e2016-09-16 16:11:40 -0700605 }
606
607 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700608 public void removeListener(NetworkConfigListener listener) {
609 this.listener = null;
610 }
611
612 /**
613 * Sends network config event to listener.
614 *
615 * @param event the network config event
616 */
617 public void sendEvent(NetworkConfigEvent event) {
618 if (listener != null) {
619 listener.event(event);
nosignal5fd282e2016-09-16 16:11:40 -0700620 }
621 }
622
Yi Tsengf4e13e32017-03-30 15:38:39 -0700623 /**
624 * Constructs test config service.
625 * Generates an VPLS configuration with sample VPLS configs.
626 */
627 public TestConfigService() {
628 vplsAppConfig = new VplsAppConfig();
629 final ObjectMapper mapper = new ObjectMapper();
630 final ConfigApplyDelegate delegate = new VplsAppConfigTest.MockCfgDelegate();
631 JsonNode tree = null;
632 try {
633 tree = new ObjectMapper().readTree(EMPTY_JSON_TREE);
634 } catch (IOException e) {
635 e.printStackTrace();
nosignal5fd282e2016-09-16 16:11:40 -0700636 }
Yi Tsengf4e13e32017-03-30 15:38:39 -0700637 vplsAppConfig.init(APPID, APP_NAME, tree, mapper, delegate);
638 VplsConfig vplsConfig = new VplsConfig(VPLS1,
639 ImmutableSet.of(V100H1.name(), V100H2.name()),
640 EncapsulationType.NONE);
641 vplsAppConfig.addVpls(vplsConfig);
642 vplsConfig = new VplsConfig(VPLS2,
643 ImmutableSet.of(V200H1.name(), V200H2.name()),
644 EncapsulationType.VLAN);
645 vplsAppConfig.addVpls(vplsConfig);
646
647 }
648
649 /**
650 * Overrides VPLS config to the config service.
651 *
652 * @param vplsAppConfig the new VPLS config
653 */
654 public void setConfig(VplsAppConfig vplsAppConfig) {
655 this.vplsAppConfig = vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700656 }
657
658 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700659 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
660 return (C) vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700661 }
662
663 @Override
Yi Tsengf4e13e32017-03-30 15:38:39 -0700664 public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
665 return (C) vplsAppConfig;
nosignal5fd282e2016-09-16 16:11:40 -0700666 }
nosignal5fd282e2016-09-16 16:11:40 -0700667 }
Yi Tsengf4e13e32017-03-30 15:38:39 -0700668
Luca Prete9c2ee072016-02-16 11:00:44 -0800669}