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