blob: 3101f40a79902a4b1c5a9817860c0db5fd108054 [file] [log] [blame]
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07001/*
Brian O'Connor180c1092017-08-03 22:46:14 -07002 * Copyright 2017-present Open Networking Foundation
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07003 *
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 */
Amit Ghoshc29c7a92017-08-01 09:59:13 +010016package org.opencord.sadis.impl;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070017
18import static org.junit.Assert.assertEquals;
Amit Ghoshc29c7a92017-08-01 09:59:13 +010019import static org.junit.Assert.assertTrue;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070020
21import java.io.InputStream;
22import java.time.Duration;
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.List;
26import java.util.Map;
27
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
Amit Ghosh38b232a2017-07-23 15:11:56 +010031import org.onlab.packet.Ip4Address;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070032import org.onlab.packet.MacAddress;
33import org.onlab.packet.VlanId;
34import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreServiceAdapter;
36import org.onosproject.net.config.Config;
37import org.onosproject.net.config.ConfigApplyDelegate;
38import org.onosproject.net.config.NetworkConfigRegistryAdapter;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000039import org.onosproject.codec.impl.CodecManager;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070040
Amit Ghoshc29c7a92017-08-01 09:59:13 +010041import org.opencord.sadis.SubscriberAndDeviceInformation;
42
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070043import com.fasterxml.jackson.databind.JsonNode;
44import com.fasterxml.jackson.databind.ObjectMapper;
45
46/**
47 * Set of tests of the SADIS ONOS application component.
48 */
49public class SadisManagerTest {
50
51 private SadisManager sadis;
52
53 @Before
54 public void setUp() throws Exception {
55 this.sadis = new SadisManager();
56 this.sadis.coreService = new MockCoreService();
57
58 final InputStream jsonStream = SadisManagerTest.class.getResourceAsStream("/config.json");
59
60 final ObjectMapper mapper = new ObjectMapper();
61 final JsonNode testConfig = mapper.readTree(jsonStream);
62 final ConfigApplyDelegate delegate = new MockConfigDelegate();
63
64 final SadisConfig config = new SadisConfig();
65 final ApplicationId subject = this.sadis.coreService.registerApplication("org.opencord.sadis");
66
67 config.init(subject, "sadis-test", testConfig, mapper, delegate);
68
69 this.sadis.cfgService = new MockNetworkConfigRegistry(config);
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000070 this.sadis.codecService = new CodecManager();
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070071 this.sadis.activate();
72 }
73
74 @After
75 public void tearDown() {
76 this.sadis.deactivate();
77 }
78
79 @Test
80 public void testConfiguration() {
81 SadisConfig config = sadis.cfgService.getConfig(null, SadisConfig.class);
82 assertEquals(true, config.getCacheEnabled());
83 assertEquals(50, config.getCacheMaxSize());
84 assertEquals(Duration.parse("PT1m"), config.getCacheTtl());
85 List<SubscriberAndDeviceInformation> entries = config.getEntries();
Amit Ghosh38b232a2017-07-23 15:11:56 +010086 assertEquals(3, entries.size());
Amit Ghoshc29c7a92017-08-01 09:59:13 +010087 assertTrue(SubscriberAndDeviceInformationBuilder.build("1", (short) 2, (short) 2, "1/1/2", (short) 125,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -070088 (short) 3, "aa:bb:cc:dd:ee:ff", "XXX-NASID", "10.10.10.10", "circuit123", "remote123")
89 .checkEquals(entries.get(0)));
Amit Ghoshc29c7a92017-08-01 09:59:13 +010090 assertTrue(SubscriberAndDeviceInformationBuilder.build("2", (short) 4, (short) 4, "1/1/2", (short) 129,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -070091 (short) 4, "aa:bb:cc:dd:ee:ff", "YYY-NASID", "1.1.1.1", "circuit234", "remote234")
92 .checkEquals(entries.get(1)));
Amit Ghoshc29c7a92017-08-01 09:59:13 +010093 assertTrue(SubscriberAndDeviceInformationBuilder.build("cc:dd:ee:ff:aa:bb", (short) -1, (short) -1, null,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -070094 (short) -1, (short) -1, "cc:dd:ee:ff:aa:bb", "CCC-NASID", "12.12.12.12", "circuit345", "remote345")
95 .checkEquals(entries.get(2)));
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070096
97 }
98
99 // Mocks live here
100
101 private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
102
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100103 public static SubscriberAndDeviceInformationBuilder build(String id, short cTag, short sTag, String nasPortId,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700104 short port, short slot, String mac, String nasId, String ipAddress, String circuitId, String remoteId) {
105
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100106 SubscriberAndDeviceInformationBuilder info = new SubscriberAndDeviceInformationBuilder();
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700107 info.setId(id);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100108 if (cTag != -1) {
109 info.setCTag(VlanId.vlanId(cTag));
110 }
111 if (sTag != -1) {
112 info.setSTag(VlanId.vlanId(sTag));
113 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700114 info.setNasPortId(nasPortId);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100115 if (port != -1) {
116 info.setPort(port);
117 }
118 if (slot != -1) {
119 info.setSlot(slot);
120 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700121 info.setHardwareIdentifier(MacAddress.valueOf(mac));
Amit Ghosh38b232a2017-07-23 15:11:56 +0100122 info.setIPAddress(Ip4Address.valueOf(ipAddress));
123 info.setNasId(nasId);
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700124 info.setCircuitId(circuitId);
125 info.setRemoteId(remoteId);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700126 return info;
127 }
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100128
129 public boolean checkEquals(SubscriberAndDeviceInformation other) {
130 if (other == null) {
131 return false;
132 }
133 if (this.cTag() == null) {
134 if (other.cTag() != null) {
135 return false;
136 }
137 } else if (!this.cTag().equals(other.cTag())) {
138 return false;
139 }
140 if (this.hardwareIdentifier() == null) {
141 if (other.hardwareIdentifier() != null) {
142 return false;
143 }
144 } else if (!this.hardwareIdentifier().equals(other.hardwareIdentifier())) {
145 return false;
146 }
147 if (this.id() == null) {
148 if (other.id() != null) {
149 return false;
150 }
151 } else if (!this.id().equals(other.id())) {
152 return false;
153 }
154 if (this.nasPortId() == null) {
155 if (other.nasPortId() != null) {
156 return false;
157 }
158 } else if (!this.nasPortId().equals(other.nasPortId())) {
159 return false;
160 }
161 if (this.nasId() == null) {
162 if (other.nasId() != null) {
163 return false;
164 }
165 } else if (!this.nasId().equals(other.nasId())) {
166 return false;
167 }
168 if (this.ipAddress() == null) {
169 if (other.ipAddress() != null) {
170 return false;
171 }
172 } else if (!this.ipAddress().equals(other.ipAddress())) {
173 return false;
174 }
175 if (this.port() != other.port()) {
176 return false;
177 }
178 if (this.sTag() == null) {
179 if (other.sTag() != null) {
180 return false;
181 }
182 } else if (!this.sTag().equals(other.sTag())) {
183 return false;
184 }
185 if (this.slot() != other.slot()) {
186 return false;
187 }
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700188 if (this.circuitId() == null) {
189 if (other.circuitId() != null) {
190 return false;
191 }
192 } else if (!this.circuitId().equals(other.circuitId())) {
193 return false;
194 }
195 if (this.remoteId() == null) {
196 if (other.remoteId() != null) {
197 return false;
198 }
199 } else if (!this.remoteId().equals(other.remoteId())) {
200 return false;
201 }
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100202 return true;
203 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700204 }
205
206 /**
207 * Mocks an ONOS configuration delegate to allow JSON based configuration to
208 * be tested.
209 */
210 private static final class MockConfigDelegate implements ConfigApplyDelegate {
211 @Override
212 public void onApply(@SuppressWarnings("rawtypes") Config config) {
213 config.apply();
214 }
215 }
216
217 /**
218 * Mocks an instance of {@link ApplicationId} so that the application
219 * component under test can query and use its application ID.
220 */
221 private static final class MockApplicationId implements ApplicationId {
222
223 private final short id;
224 private final String name;
225
226 public MockApplicationId(short id, String name) {
227 this.id = id;
228 this.name = name;
229 }
230
231 @Override
232 public short id() {
233 return id;
234 }
235
236 @Override
237 public String name() {
238 return name;
239 }
240 }
241
242 /**
243 * Mocks the core services of ONOS so that the application under test can
244 * register and query application IDs.
245 */
246 private static final class MockCoreService extends CoreServiceAdapter {
247
248 private List<ApplicationId> idList = new ArrayList<ApplicationId>();
249 private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
250
251 /*
252 * (non-Javadoc)
253 *
254 * @see
255 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
256 */
257 @Override
258 public ApplicationId getAppId(Short id) {
259 if (id >= idList.size()) {
260 return null;
261 }
262 return idList.get(id);
263 }
264
265 /*
266 * (non-Javadoc)
267 *
268 * @see
269 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
270 */
271 @Override
272 public ApplicationId getAppId(String name) {
273 return idMap.get(name);
274 }
275
276 /*
277 * (non-Javadoc)
278 *
279 * @see
280 * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
281 * .String)
282 */
283 @Override
284 public ApplicationId registerApplication(String name) {
285 ApplicationId appId = idMap.get(name);
286 if (appId == null) {
287 appId = new MockApplicationId((short) idList.size(), name);
288 idList.add(appId);
289 idMap.put(name, appId);
290 }
291 return appId;
292 }
293
294 }
295
296 /**
297 * Mocks the ONOS network configuration registry so that the application
298 * under test can access a JSON defined configuration.
299 */
300 static final class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
301 private final SadisConfig config;
302
303 public MockNetworkConfigRegistry(final SadisConfig config) {
304 this.config = config;
305 }
306
307 @SuppressWarnings("unchecked")
308 @Override
309 public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
310 return (C) this.config;
311 }
312 }
313}