blob: ea5f056ff3df7383956f9529c4f2ed862f50cd56 [file] [log] [blame]
Gamze Abaka1e5ccf52018-07-02 11:59:03 +00001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
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.opencord.sadis.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
Matteo Scandolo198aef92020-01-08 00:43:17 +000020import com.google.common.collect.Lists;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000024import org.onosproject.codec.impl.CodecManager;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreServiceAdapter;
27import org.onosproject.net.config.ConfigApplyDelegate;
28import org.onosproject.net.config.NetworkConfigRegistryAdapter;
29import org.onosproject.net.config.NetworkConfigListener;
30import org.onosproject.net.config.Config;
31import org.onosproject.net.config.NetworkConfigEvent;
32import org.opencord.sadis.BaseConfig;
Matteo Scandoloe4f4b632020-01-07 23:54:35 +000033import org.opencord.sadis.BaseInformation;
Matteo Scandolo198aef92020-01-08 00:43:17 +000034import org.opencord.sadis.BaseInformationService;
35import org.opencord.sadis.SubscriberAndDeviceInformation;
36import org.opencord.sadis.UniTagInformation;
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000037
38import java.io.InputStream;
39import java.time.Duration;
40import java.util.ArrayList;
41import java.util.HashMap;
42import java.util.List;
43import java.util.Map;
44
45import static org.junit.Assert.*;
46
47public abstract class BaseSadis {
48
49 protected SadisManager sadis;
50 protected ConfigApplyDelegate delegate;
51 protected ObjectMapper mapper;
52 protected ApplicationId subject;
53 protected BaseConfig config;
54 protected NetworkConfigEvent event;
55 protected static NetworkConfigListener configListener;
56
Matteo Scandolo198aef92020-01-08 00:43:17 +000057 private static final short UNI_TAG_MATCH_1 = 100;
58 private static final short UNI_TAG_MATCH_2 = 200;
59 private static final short C_TAG_1 = 2;
60 private static final short S_TAG_1 = 2;
61 private static final short S_TAG_2 = 3;
62 private static final int C_TAG_PRIORITY = 0;
63 private static final int S_TAG_PRIORITY = 1;
64 private static final int TECH_PROF_ID_1 = 64;
65 private static final int TECH_PROF_ID_2 = 65;
66
67 private static final String HSA = "HSA";
Gustavo Silva882b0552022-06-14 10:32:36 -030068 private static final String HSIA = "HSIA";
yasin sapli2c93ddb2021-06-11 17:46:26 +030069 private static final String HSA_OLT = "HSA_OLT";
Matteo Scandolo198aef92020-01-08 00:43:17 +000070 private static final String IPTV = "IPTV";
71 private static final String NAS_PORT_ID = "1/1/2";
72
73 private static final short PORT_1 = 125;
74 private static final short PORT_2 = 129;
75 private static final short PORT_3 = 130;
76 private static final short PORT_4 = 132;
77
78 private static final short SLOT_1 = 3;
79 private static final short SLOT_2 = 4;
80 private static final short SLOT_3 = 7;
81 private static final short SLOT_4 = 1;
82
83 private static final String MAC1 = "aa:bb:cc:dd:ee:ff";
84 private static final String MAC2 = "ff:aa:dd:cc:bb:ee";
85 private static final String MAC3 = "ff:cc:dd:aa:ee:bb";
86
87 private static final String NAS1 = "XXX-NASID";
88 private static final String NAS2 = "YYY-NASID";
89 private static final String NAS3 = "MNO-NASID";
90 private static final String NAS4 = "PQR-NASID";
91
92 private static final String CIRCUIT1 = "circuit123";
93 private static final String CIRCUIT2 = "circuit234";
94 private static final String CIRCUIT3 = "circuit567";
95 private static final String CIRCUIT4 = "circuit678";
96
97 private static final String REMOTE1 = "remote123";
98 private static final String REMOTE2 = "remote234";
99 private static final String REMOTE3 = "remote567";
100 private static final String REMOTE4 = "remote678";
101
102 private static final String IP1 = "10.10.10.10";
103 private static final String IP2 = "1.1.1.1";
104 private static final String IP3 = "30.30.30.30";
105 private static final String IP4 = "15.15.15.15";
106
107 protected static final String ID1 = "1";
108 protected static final String ID2 = "2";
109 protected static final String ID3 = "3";
110 protected static final String ID4 = "4";
111 protected static final String ID5 = "5";
yasin sapli2c93ddb2021-06-11 17:46:26 +0300112 protected static final String ID6 = "6";
Gustavo Silva882b0552022-06-14 10:32:36 -0300113 protected static final String EMPTY = "";
Matteo Scandolo198aef92020-01-08 00:43:17 +0000114
115 UniTagInformation ttService1 = new UniTagInformation.Builder()
116 .setUniTagMatch(VlanId.vlanId(UNI_TAG_MATCH_1))
117 .setPonCTag(VlanId.vlanId(C_TAG_1))
118 .setPonSTag(VlanId.vlanId(S_TAG_1))
119 .setUsPonCTagPriority(C_TAG_PRIORITY)
120 .setUsPonSTagPriority(S_TAG_PRIORITY)
121 .setDsPonCTagPriority(C_TAG_PRIORITY)
122 .setDsPonSTagPriority(S_TAG_PRIORITY)
123 .setTechnologyProfileId(TECH_PROF_ID_1)
124 .setUpstreamBandwidthProfile(HSA)
125 .setDownstreamBandwidthProfile(HSA)
Gustavo Silva882b0552022-06-14 10:32:36 -0300126 .setServiceName(HSIA)
127 .setConfiguredMacAddress(EMPTY)
128 .setUpstreamOltBandwidthProfile(HSA)
129 .setDownstreamOltBandwidthProfile(HSA)
Matteo Scandolo198aef92020-01-08 00:43:17 +0000130 .build();
131
132 UniTagInformation ttService2 = new UniTagInformation.Builder()
133 .setUniTagMatch(VlanId.vlanId(UNI_TAG_MATCH_2))
134 .setPonCTag(VlanId.vlanId(C_TAG_1))
135 .setPonSTag(VlanId.vlanId(S_TAG_2))
136 .setUsPonCTagPriority(C_TAG_PRIORITY)
137 .setUsPonSTagPriority(S_TAG_PRIORITY)
138 .setDsPonCTagPriority(C_TAG_PRIORITY)
139 .setDsPonSTagPriority((S_TAG_PRIORITY))
140 .setTechnologyProfileId(TECH_PROF_ID_2)
141 .setUpstreamBandwidthProfile(IPTV)
142 .setDownstreamBandwidthProfile(IPTV)
143 .setServiceName(IPTV)
144 .setIsIgmpRequired(true)
145 .setIsDhcpRequired(true)
146 .setEnableMacLearning(true)
147 .setConfiguredMacAddress(MAC2)
Gustavo Silva882b0552022-06-14 10:32:36 -0300148 .setUpstreamOltBandwidthProfile(IPTV)
149 .setDownstreamOltBandwidthProfile(IPTV)
Matteo Scandolo198aef92020-01-08 00:43:17 +0000150 .build();
151
yasin sapli2c93ddb2021-06-11 17:46:26 +0300152 UniTagInformation ttService3 = new UniTagInformation.Builder()
153 .setUniTagMatch(VlanId.vlanId(UNI_TAG_MATCH_1))
154 .setPonCTag(VlanId.vlanId(C_TAG_1))
155 .setPonSTag(VlanId.vlanId(S_TAG_1))
156 .setUsPonCTagPriority(C_TAG_PRIORITY)
157 .setUsPonSTagPriority(S_TAG_PRIORITY)
158 .setDsPonCTagPriority(C_TAG_PRIORITY)
159 .setDsPonSTagPriority(S_TAG_PRIORITY)
160 .setTechnologyProfileId(TECH_PROF_ID_1)
161 .setUpstreamBandwidthProfile(HSA)
162 .setDownstreamBandwidthProfile(HSA)
163 .setUpstreamOltBandwidthProfile(HSA_OLT)
164 .setDownstreamOltBandwidthProfile(HSA_OLT)
Gustavo Silva882b0552022-06-14 10:32:36 -0300165 .setServiceName(HSIA)
166 .setConfiguredMacAddress(EMPTY)
yasin sapli2c93ddb2021-06-11 17:46:26 +0300167 .build();
168
Matteo Scandolo198aef92020-01-08 00:43:17 +0000169 UniTagInformation attService1 = new UniTagInformation.Builder()
Gustavo Silva882b0552022-06-14 10:32:36 -0300170 .setServiceName(EMPTY)
Matteo Scandolo198aef92020-01-08 00:43:17 +0000171 .setPonCTag(VlanId.vlanId(C_TAG_1))
172 .setPonSTag(VlanId.vlanId(S_TAG_2))
173 .setTechnologyProfileId(TECH_PROF_ID_1)
174 .setUpstreamBandwidthProfile(HSA)
175 .setDownstreamBandwidthProfile(HSA)
Gustavo Silva882b0552022-06-14 10:32:36 -0300176 .setUniTagMatch(VlanId.vlanId(VlanId.NO_VID))
177 .setConfiguredMacAddress(EMPTY)
178 .setUpstreamOltBandwidthProfile(HSA)
179 .setDownstreamOltBandwidthProfile(HSA)
Matteo Scandolo198aef92020-01-08 00:43:17 +0000180 .build();
181
182 List<UniTagInformation> uniTagListForTT = Lists.newArrayList(ttService1);
183 List<UniTagInformation> uniTagList2ForTT = Lists.newArrayList(ttService1, ttService2);
yasin sapli2c93ddb2021-06-11 17:46:26 +0300184 List<UniTagInformation> uniTagList3ForTT = Lists.newArrayList(ttService3);
Matteo Scandolo198aef92020-01-08 00:43:17 +0000185 List<UniTagInformation> uniTagList3Att = Lists.newArrayList(attService1);
186
187 SubscriberAndDeviceInformationBuilder entry1 = SubscriberAndDeviceInformationBuilder.build(ID1, NAS_PORT_ID,
188 PORT_1, SLOT_1, MAC1, NAS1, IP1, CIRCUIT1, REMOTE1, uniTagListForTT);
189 SubscriberAndDeviceInformationBuilder entry2 = SubscriberAndDeviceInformationBuilder.build(ID2, NAS_PORT_ID,
190 PORT_2, SLOT_2, MAC1, NAS2, IP2, CIRCUIT2, REMOTE2, uniTagList2ForTT);
191 SubscriberAndDeviceInformationBuilder entry3 = SubscriberAndDeviceInformationBuilder.build(ID3, NAS_PORT_ID,
192 PORT_3, SLOT_3, MAC2, NAS3, IP3, CIRCUIT3, REMOTE3, uniTagListForTT);
193 SubscriberAndDeviceInformationBuilder entry4 = SubscriberAndDeviceInformationBuilder.build(ID4, NAS_PORT_ID,
194 PORT_4, SLOT_4, MAC3, NAS4, IP4, CIRCUIT4, REMOTE4, uniTagList2ForTT);
195 SubscriberAndDeviceInformationBuilder entry5 = SubscriberAndDeviceInformationBuilder.build(ID5, NAS_PORT_ID,
196 PORT_3, SLOT_3, MAC2, NAS3, IP3, CIRCUIT3, REMOTE3, uniTagList3Att);
yasin sapli2c93ddb2021-06-11 17:46:26 +0300197 SubscriberAndDeviceInformationBuilder entry6 = SubscriberAndDeviceInformationBuilder.build(ID6, NAS_PORT_ID,
198 PORT_3, SLOT_3, MAC2, NAS3, IP3, CIRCUIT3, REMOTE3, uniTagList3ForTT);
Matteo Scandolo198aef92020-01-08 00:43:17 +0000199
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000200 public void setUp(String localConfig, Class configClass) throws Exception {
201 sadis = new SadisManager();
202 sadis.coreService = new MockCoreService();
203 delegate = new MockConfigDelegate();
204 mapper = new ObjectMapper();
205 subject = sadis.coreService.registerApplication("org.opencord.sadis");
206
207 config.init(subject, "sadis-local-mode-test", node(localConfig), mapper, delegate);
208 sadis.cfgService = new MockNetworkConfigRegistry(subject, config);
209
210 event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, subject, configClass);
211
212 sadis.codecService = new CodecManager();
213 sadis.activate();
214 }
215
216 public void tearDown() {
217 this.sadis.deactivate();
218 }
219
220 protected JsonNode node(String jsonFile) throws Exception {
221 final InputStream jsonStream = BaseSadis.class.getResourceAsStream(jsonFile);
222 final JsonNode testConfig = mapper.readTree(jsonStream);
223 return testConfig;
224 }
225
226 protected void checkConfigInfo(int cacheMaxSize, String cacheTtl, BaseConfig config) {
227 assertEquals(cacheMaxSize, config.getCacheMaxSize());
228 assertEquals(Duration.parse(cacheTtl), config.getCacheTtl());
229 }
230
231 protected void invalidateId(String id, BaseInformationService service) {
232 service.invalidateId(id);
233 }
234
235 protected void invalidateAll(BaseInformationService service) {
236 service.invalidateAll();
237 }
238
239 protected void checkFromBoth(String id, BaseInformation localEntry, BaseInformationService service) {
240 BaseInformation entry = service.getfromCache(id);
241 assertNull(entry);
242 checkGetForExisting(id, localEntry, service);
243 }
244
245 abstract boolean checkEquality(BaseInformation localEntry, BaseInformation entry);
246
247 protected void checkGetForExisting(String id, BaseInformation localEntry, BaseInformationService service) {
248 BaseInformation entry = service.get(id);
249 assertNotNull(entry);
250 System.out.println(entry);
251 if (localEntry != null) {
252 assertTrue(checkEquality(localEntry, entry));
253 }
254 }
255
Matteo Scandolo198aef92020-01-08 00:43:17 +0000256 private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
257
258 public static SubscriberAndDeviceInformationBuilder build(String id, String nasPortId,
259 short port, short slot, String mac,
260 String nasId, String ipAddress, String circuitId,
261 String remoteId,
262 List<UniTagInformation> uniTagList) {
263
264 SubscriberAndDeviceInformationBuilder info = new SubscriberAndDeviceInformationBuilder();
265 info.setId(id);
266 if (slot != -1) {
267 info.setSlot(slot);
268 }
269 info.setNasPortId(nasPortId);
270 info.setUplinkPort(port);
271 info.setHardwareIdentifier(MacAddress.valueOf(mac));
272 info.setIPAddress(Ip4Address.valueOf(ipAddress));
273 info.setNasId(nasId);
274 info.setCircuitId(circuitId);
275 info.setRemoteId(remoteId);
276
277 info.setUniTagList(uniTagList);
278
279 return info;
280 }
281
282 }
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000283
284 /**
285 * Mocks an ONOS configuration delegate to allow JSON based configuration to
286 * be tested.
287 */
288 private static final class MockConfigDelegate implements ConfigApplyDelegate {
289 @Override
290 public void onApply(@SuppressWarnings("rawtypes") Config config) {
291 config.apply();
292 }
293 }
294
295 /**
296 * Mocks an instance of {@link ApplicationId} so that the application
297 * component under test can query and use its application ID.
298 */
299 private static final class MockApplicationId implements ApplicationId {
300
301 private final short id;
302 private final String name;
303
304 public MockApplicationId(short id, String name) {
305 this.id = id;
306 this.name = name;
307 }
308
309 @Override
310 public short id() {
311 return id;
312 }
313
314 @Override
315 public String name() {
316 return name;
317 }
318 }
319
320 /**
321 * Mocks the core services of ONOS so that the application under test can
322 * register and query application IDs.
323 */
324 private static final class MockCoreService extends CoreServiceAdapter {
325
326 private List<ApplicationId> idList = new ArrayList<ApplicationId>();
327 private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
328
329 /*
330 * (non-Javadoc)
331 *
332 * @see
333 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
334 */
335 @Override
336 public ApplicationId getAppId(Short id) {
337 if (id >= idList.size()) {
338 return null;
339 }
340 return idList.get(id);
341 }
342
343 /*
344 * (non-Javadoc)
345 *
346 * @see
347 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
348 */
349 @Override
350 public ApplicationId getAppId(String name) {
351 return idMap.get(name);
352 }
353
354 /*
355 * (non-Javadoc)
356 *
357 * @see
358 * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
359 * .String)
360 */
361 @Override
362 public ApplicationId registerApplication(String name) {
363 ApplicationId appId = idMap.get(name);
364 if (appId == null) {
365 appId = new MockApplicationId((short) idList.size(), name);
366 idList.add(appId);
367 idMap.put(name, appId);
368 }
369 return appId;
370 }
371
372 }
373
374
375 /**
376 * Mocks the ONOS network configuration registry so that the application
377 * under test can access a JSON defined configuration.
378 */
379 static final class MockNetworkConfigRegistry<S> extends NetworkConfigRegistryAdapter {
380 private final BaseConfig config;
381
382 public MockNetworkConfigRegistry(final S subject, final BaseConfig config) {
383 this.config = config;
384 }
385
Matteo Scandolo6c555cf2020-12-08 15:14:53 -0800386 @SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000387 @Override
388 public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
389 return (C) config;
390 }
391
392 @Override
393 public void addListener(NetworkConfigListener listener) {
394 configListener = listener;
395 }
396 }
397}