blob: eba613bc98c837c7a32fe90f14231f157f51246d [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";
yasin sapli2c93ddb2021-06-11 17:46:26 +030068 private static final String HSA_OLT = "HSA_OLT";
Matteo Scandolo198aef92020-01-08 00:43:17 +000069 private static final String IPTV = "IPTV";
70 private static final String NAS_PORT_ID = "1/1/2";
71
72 private static final short PORT_1 = 125;
73 private static final short PORT_2 = 129;
74 private static final short PORT_3 = 130;
75 private static final short PORT_4 = 132;
76
77 private static final short SLOT_1 = 3;
78 private static final short SLOT_2 = 4;
79 private static final short SLOT_3 = 7;
80 private static final short SLOT_4 = 1;
81
82 private static final String MAC1 = "aa:bb:cc:dd:ee:ff";
83 private static final String MAC2 = "ff:aa:dd:cc:bb:ee";
84 private static final String MAC3 = "ff:cc:dd:aa:ee:bb";
85
86 private static final String NAS1 = "XXX-NASID";
87 private static final String NAS2 = "YYY-NASID";
88 private static final String NAS3 = "MNO-NASID";
89 private static final String NAS4 = "PQR-NASID";
90
91 private static final String CIRCUIT1 = "circuit123";
92 private static final String CIRCUIT2 = "circuit234";
93 private static final String CIRCUIT3 = "circuit567";
94 private static final String CIRCUIT4 = "circuit678";
95
96 private static final String REMOTE1 = "remote123";
97 private static final String REMOTE2 = "remote234";
98 private static final String REMOTE3 = "remote567";
99 private static final String REMOTE4 = "remote678";
100
101 private static final String IP1 = "10.10.10.10";
102 private static final String IP2 = "1.1.1.1";
103 private static final String IP3 = "30.30.30.30";
104 private static final String IP4 = "15.15.15.15";
105
106 protected static final String ID1 = "1";
107 protected static final String ID2 = "2";
108 protected static final String ID3 = "3";
109 protected static final String ID4 = "4";
110 protected static final String ID5 = "5";
yasin sapli2c93ddb2021-06-11 17:46:26 +0300111 protected static final String ID6 = "6";
Matteo Scandolo198aef92020-01-08 00:43:17 +0000112
113 UniTagInformation ttService1 = new UniTagInformation.Builder()
114 .setUniTagMatch(VlanId.vlanId(UNI_TAG_MATCH_1))
115 .setPonCTag(VlanId.vlanId(C_TAG_1))
116 .setPonSTag(VlanId.vlanId(S_TAG_1))
117 .setUsPonCTagPriority(C_TAG_PRIORITY)
118 .setUsPonSTagPriority(S_TAG_PRIORITY)
119 .setDsPonCTagPriority(C_TAG_PRIORITY)
120 .setDsPonSTagPriority(S_TAG_PRIORITY)
121 .setTechnologyProfileId(TECH_PROF_ID_1)
122 .setUpstreamBandwidthProfile(HSA)
123 .setDownstreamBandwidthProfile(HSA)
124 .setServiceName(HSA)
125 .build();
126
127 UniTagInformation ttService2 = new UniTagInformation.Builder()
128 .setUniTagMatch(VlanId.vlanId(UNI_TAG_MATCH_2))
129 .setPonCTag(VlanId.vlanId(C_TAG_1))
130 .setPonSTag(VlanId.vlanId(S_TAG_2))
131 .setUsPonCTagPriority(C_TAG_PRIORITY)
132 .setUsPonSTagPriority(S_TAG_PRIORITY)
133 .setDsPonCTagPriority(C_TAG_PRIORITY)
134 .setDsPonSTagPriority((S_TAG_PRIORITY))
135 .setTechnologyProfileId(TECH_PROF_ID_2)
136 .setUpstreamBandwidthProfile(IPTV)
137 .setDownstreamBandwidthProfile(IPTV)
138 .setServiceName(IPTV)
139 .setIsIgmpRequired(true)
140 .setIsDhcpRequired(true)
141 .setEnableMacLearning(true)
142 .setConfiguredMacAddress(MAC2)
143 .build();
144
yasin sapli2c93ddb2021-06-11 17:46:26 +0300145 UniTagInformation ttService3 = new UniTagInformation.Builder()
146 .setUniTagMatch(VlanId.vlanId(UNI_TAG_MATCH_1))
147 .setPonCTag(VlanId.vlanId(C_TAG_1))
148 .setPonSTag(VlanId.vlanId(S_TAG_1))
149 .setUsPonCTagPriority(C_TAG_PRIORITY)
150 .setUsPonSTagPriority(S_TAG_PRIORITY)
151 .setDsPonCTagPriority(C_TAG_PRIORITY)
152 .setDsPonSTagPriority(S_TAG_PRIORITY)
153 .setTechnologyProfileId(TECH_PROF_ID_1)
154 .setUpstreamBandwidthProfile(HSA)
155 .setDownstreamBandwidthProfile(HSA)
156 .setUpstreamOltBandwidthProfile(HSA_OLT)
157 .setDownstreamOltBandwidthProfile(HSA_OLT)
158 .setServiceName(HSA)
159 .build();
160
Matteo Scandolo198aef92020-01-08 00:43:17 +0000161 UniTagInformation attService1 = new UniTagInformation.Builder()
162 .setPonCTag(VlanId.vlanId(C_TAG_1))
163 .setPonSTag(VlanId.vlanId(S_TAG_2))
164 .setTechnologyProfileId(TECH_PROF_ID_1)
165 .setUpstreamBandwidthProfile(HSA)
166 .setDownstreamBandwidthProfile(HSA)
167 .build();
168
169 List<UniTagInformation> uniTagListForTT = Lists.newArrayList(ttService1);
170 List<UniTagInformation> uniTagList2ForTT = Lists.newArrayList(ttService1, ttService2);
yasin sapli2c93ddb2021-06-11 17:46:26 +0300171 List<UniTagInformation> uniTagList3ForTT = Lists.newArrayList(ttService3);
Matteo Scandolo198aef92020-01-08 00:43:17 +0000172 List<UniTagInformation> uniTagList3Att = Lists.newArrayList(attService1);
173
174 SubscriberAndDeviceInformationBuilder entry1 = SubscriberAndDeviceInformationBuilder.build(ID1, NAS_PORT_ID,
175 PORT_1, SLOT_1, MAC1, NAS1, IP1, CIRCUIT1, REMOTE1, uniTagListForTT);
176 SubscriberAndDeviceInformationBuilder entry2 = SubscriberAndDeviceInformationBuilder.build(ID2, NAS_PORT_ID,
177 PORT_2, SLOT_2, MAC1, NAS2, IP2, CIRCUIT2, REMOTE2, uniTagList2ForTT);
178 SubscriberAndDeviceInformationBuilder entry3 = SubscriberAndDeviceInformationBuilder.build(ID3, NAS_PORT_ID,
179 PORT_3, SLOT_3, MAC2, NAS3, IP3, CIRCUIT3, REMOTE3, uniTagListForTT);
180 SubscriberAndDeviceInformationBuilder entry4 = SubscriberAndDeviceInformationBuilder.build(ID4, NAS_PORT_ID,
181 PORT_4, SLOT_4, MAC3, NAS4, IP4, CIRCUIT4, REMOTE4, uniTagList2ForTT);
182 SubscriberAndDeviceInformationBuilder entry5 = SubscriberAndDeviceInformationBuilder.build(ID5, NAS_PORT_ID,
183 PORT_3, SLOT_3, MAC2, NAS3, IP3, CIRCUIT3, REMOTE3, uniTagList3Att);
yasin sapli2c93ddb2021-06-11 17:46:26 +0300184 SubscriberAndDeviceInformationBuilder entry6 = SubscriberAndDeviceInformationBuilder.build(ID6, NAS_PORT_ID,
185 PORT_3, SLOT_3, MAC2, NAS3, IP3, CIRCUIT3, REMOTE3, uniTagList3ForTT);
Matteo Scandolo198aef92020-01-08 00:43:17 +0000186
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000187 public void setUp(String localConfig, Class configClass) throws Exception {
188 sadis = new SadisManager();
189 sadis.coreService = new MockCoreService();
190 delegate = new MockConfigDelegate();
191 mapper = new ObjectMapper();
192 subject = sadis.coreService.registerApplication("org.opencord.sadis");
193
194 config.init(subject, "sadis-local-mode-test", node(localConfig), mapper, delegate);
195 sadis.cfgService = new MockNetworkConfigRegistry(subject, config);
196
197 event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, subject, configClass);
198
199 sadis.codecService = new CodecManager();
200 sadis.activate();
201 }
202
203 public void tearDown() {
204 this.sadis.deactivate();
205 }
206
207 protected JsonNode node(String jsonFile) throws Exception {
208 final InputStream jsonStream = BaseSadis.class.getResourceAsStream(jsonFile);
209 final JsonNode testConfig = mapper.readTree(jsonStream);
210 return testConfig;
211 }
212
213 protected void checkConfigInfo(int cacheMaxSize, String cacheTtl, BaseConfig config) {
214 assertEquals(cacheMaxSize, config.getCacheMaxSize());
215 assertEquals(Duration.parse(cacheTtl), config.getCacheTtl());
216 }
217
218 protected void invalidateId(String id, BaseInformationService service) {
219 service.invalidateId(id);
220 }
221
222 protected void invalidateAll(BaseInformationService service) {
223 service.invalidateAll();
224 }
225
226 protected void checkFromBoth(String id, BaseInformation localEntry, BaseInformationService service) {
227 BaseInformation entry = service.getfromCache(id);
228 assertNull(entry);
229 checkGetForExisting(id, localEntry, service);
230 }
231
232 abstract boolean checkEquality(BaseInformation localEntry, BaseInformation entry);
233
234 protected void checkGetForExisting(String id, BaseInformation localEntry, BaseInformationService service) {
235 BaseInformation entry = service.get(id);
236 assertNotNull(entry);
237 System.out.println(entry);
238 if (localEntry != null) {
239 assertTrue(checkEquality(localEntry, entry));
240 }
241 }
242
Matteo Scandolo198aef92020-01-08 00:43:17 +0000243 private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
244
245 public static SubscriberAndDeviceInformationBuilder build(String id, String nasPortId,
246 short port, short slot, String mac,
247 String nasId, String ipAddress, String circuitId,
248 String remoteId,
249 List<UniTagInformation> uniTagList) {
250
251 SubscriberAndDeviceInformationBuilder info = new SubscriberAndDeviceInformationBuilder();
252 info.setId(id);
253 if (slot != -1) {
254 info.setSlot(slot);
255 }
256 info.setNasPortId(nasPortId);
257 info.setUplinkPort(port);
258 info.setHardwareIdentifier(MacAddress.valueOf(mac));
259 info.setIPAddress(Ip4Address.valueOf(ipAddress));
260 info.setNasId(nasId);
261 info.setCircuitId(circuitId);
262 info.setRemoteId(remoteId);
263
264 info.setUniTagList(uniTagList);
265
266 return info;
267 }
268
269 }
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000270
271 /**
272 * Mocks an ONOS configuration delegate to allow JSON based configuration to
273 * be tested.
274 */
275 private static final class MockConfigDelegate implements ConfigApplyDelegate {
276 @Override
277 public void onApply(@SuppressWarnings("rawtypes") Config config) {
278 config.apply();
279 }
280 }
281
282 /**
283 * Mocks an instance of {@link ApplicationId} so that the application
284 * component under test can query and use its application ID.
285 */
286 private static final class MockApplicationId implements ApplicationId {
287
288 private final short id;
289 private final String name;
290
291 public MockApplicationId(short id, String name) {
292 this.id = id;
293 this.name = name;
294 }
295
296 @Override
297 public short id() {
298 return id;
299 }
300
301 @Override
302 public String name() {
303 return name;
304 }
305 }
306
307 /**
308 * Mocks the core services of ONOS so that the application under test can
309 * register and query application IDs.
310 */
311 private static final class MockCoreService extends CoreServiceAdapter {
312
313 private List<ApplicationId> idList = new ArrayList<ApplicationId>();
314 private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
315
316 /*
317 * (non-Javadoc)
318 *
319 * @see
320 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
321 */
322 @Override
323 public ApplicationId getAppId(Short id) {
324 if (id >= idList.size()) {
325 return null;
326 }
327 return idList.get(id);
328 }
329
330 /*
331 * (non-Javadoc)
332 *
333 * @see
334 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
335 */
336 @Override
337 public ApplicationId getAppId(String name) {
338 return idMap.get(name);
339 }
340
341 /*
342 * (non-Javadoc)
343 *
344 * @see
345 * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
346 * .String)
347 */
348 @Override
349 public ApplicationId registerApplication(String name) {
350 ApplicationId appId = idMap.get(name);
351 if (appId == null) {
352 appId = new MockApplicationId((short) idList.size(), name);
353 idList.add(appId);
354 idMap.put(name, appId);
355 }
356 return appId;
357 }
358
359 }
360
361
362 /**
363 * Mocks the ONOS network configuration registry so that the application
364 * under test can access a JSON defined configuration.
365 */
366 static final class MockNetworkConfigRegistry<S> extends NetworkConfigRegistryAdapter {
367 private final BaseConfig config;
368
369 public MockNetworkConfigRegistry(final S subject, final BaseConfig config) {
370 this.config = config;
371 }
372
Matteo Scandolo6c555cf2020-12-08 15:14:53 -0800373 @SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000374 @Override
375 public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
376 return (C) config;
377 }
378
379 @Override
380 public void addListener(NetworkConfigListener listener) {
381 configListener = listener;
382 }
383 }
384}