blob: 70cfa231fce05e1183dcef79a39497d87a939c31 [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,
88 (short) 3, "aa:bb:cc:dd:ee:ff", "XXX-NASID", "10.10.10.10").checkEquals(entries.get(0)));
89 assertTrue(SubscriberAndDeviceInformationBuilder.build("2", (short) 4, (short) 4, "1/1/2", (short) 129,
90 (short) 4, "aa:bb:cc:dd:ee:ff", "YYY-NASID", "1.1.1.1").checkEquals(entries.get(1)));
91 assertTrue(SubscriberAndDeviceInformationBuilder.build("cc:dd:ee:ff:aa:bb", (short) -1, (short) -1, null,
Amit Ghosh38b232a2017-07-23 15:11:56 +010092 (short) -1,
Amit Ghoshc29c7a92017-08-01 09:59:13 +010093 (short) -1, "cc:dd:ee:ff:aa:bb", "CCC-NASID", "12.12.12.12").checkEquals(entries.get(2)));
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070094
95 }
96
97 // Mocks live here
98
99 private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
100
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100101 public static SubscriberAndDeviceInformationBuilder build(String id, short cTag, short sTag, String nasPortId,
Amit Ghosh38b232a2017-07-23 15:11:56 +0100102 short port, short slot, String mac, String nasId, String ipAddress) {
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100103 //SubscriberAndDeviceInformation info = new SubscriberAndDeviceInformation();
104 SubscriberAndDeviceInformationBuilder info = new SubscriberAndDeviceInformationBuilder();
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700105 info.setId(id);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100106 if (cTag != -1) {
107 info.setCTag(VlanId.vlanId(cTag));
108 }
109 if (sTag != -1) {
110 info.setSTag(VlanId.vlanId(sTag));
111 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700112 info.setNasPortId(nasPortId);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100113 if (port != -1) {
114 info.setPort(port);
115 }
116 if (slot != -1) {
117 info.setSlot(slot);
118 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700119 info.setHardwareIdentifier(MacAddress.valueOf(mac));
Amit Ghosh38b232a2017-07-23 15:11:56 +0100120 info.setIPAddress(Ip4Address.valueOf(ipAddress));
121 info.setNasId(nasId);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700122 return info;
123 }
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100124
125 public boolean checkEquals(SubscriberAndDeviceInformation other) {
126 if (other == null) {
127 return false;
128 }
129 if (this.cTag() == null) {
130 if (other.cTag() != null) {
131 return false;
132 }
133 } else if (!this.cTag().equals(other.cTag())) {
134 return false;
135 }
136 if (this.hardwareIdentifier() == null) {
137 if (other.hardwareIdentifier() != null) {
138 return false;
139 }
140 } else if (!this.hardwareIdentifier().equals(other.hardwareIdentifier())) {
141 return false;
142 }
143 if (this.id() == null) {
144 if (other.id() != null) {
145 return false;
146 }
147 } else if (!this.id().equals(other.id())) {
148 return false;
149 }
150 if (this.nasPortId() == null) {
151 if (other.nasPortId() != null) {
152 return false;
153 }
154 } else if (!this.nasPortId().equals(other.nasPortId())) {
155 return false;
156 }
157 if (this.nasId() == null) {
158 if (other.nasId() != null) {
159 return false;
160 }
161 } else if (!this.nasId().equals(other.nasId())) {
162 return false;
163 }
164 if (this.ipAddress() == null) {
165 if (other.ipAddress() != null) {
166 return false;
167 }
168 } else if (!this.ipAddress().equals(other.ipAddress())) {
169 return false;
170 }
171 if (this.port() != other.port()) {
172 return false;
173 }
174 if (this.sTag() == null) {
175 if (other.sTag() != null) {
176 return false;
177 }
178 } else if (!this.sTag().equals(other.sTag())) {
179 return false;
180 }
181 if (this.slot() != other.slot()) {
182 return false;
183 }
184 return true;
185 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700186 }
187
188 /**
189 * Mocks an ONOS configuration delegate to allow JSON based configuration to
190 * be tested.
191 */
192 private static final class MockConfigDelegate implements ConfigApplyDelegate {
193 @Override
194 public void onApply(@SuppressWarnings("rawtypes") Config config) {
195 config.apply();
196 }
197 }
198
199 /**
200 * Mocks an instance of {@link ApplicationId} so that the application
201 * component under test can query and use its application ID.
202 */
203 private static final class MockApplicationId implements ApplicationId {
204
205 private final short id;
206 private final String name;
207
208 public MockApplicationId(short id, String name) {
209 this.id = id;
210 this.name = name;
211 }
212
213 @Override
214 public short id() {
215 return id;
216 }
217
218 @Override
219 public String name() {
220 return name;
221 }
222 }
223
224 /**
225 * Mocks the core services of ONOS so that the application under test can
226 * register and query application IDs.
227 */
228 private static final class MockCoreService extends CoreServiceAdapter {
229
230 private List<ApplicationId> idList = new ArrayList<ApplicationId>();
231 private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
232
233 /*
234 * (non-Javadoc)
235 *
236 * @see
237 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
238 */
239 @Override
240 public ApplicationId getAppId(Short id) {
241 if (id >= idList.size()) {
242 return null;
243 }
244 return idList.get(id);
245 }
246
247 /*
248 * (non-Javadoc)
249 *
250 * @see
251 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
252 */
253 @Override
254 public ApplicationId getAppId(String name) {
255 return idMap.get(name);
256 }
257
258 /*
259 * (non-Javadoc)
260 *
261 * @see
262 * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
263 * .String)
264 */
265 @Override
266 public ApplicationId registerApplication(String name) {
267 ApplicationId appId = idMap.get(name);
268 if (appId == null) {
269 appId = new MockApplicationId((short) idList.size(), name);
270 idList.add(appId);
271 idMap.put(name, appId);
272 }
273 return appId;
274 }
275
276 }
277
278 /**
279 * Mocks the ONOS network configuration registry so that the application
280 * under test can access a JSON defined configuration.
281 */
282 static final class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
283 private final SadisConfig config;
284
285 public MockNetworkConfigRegistry(final SadisConfig config) {
286 this.config = config;
287 }
288
289 @SuppressWarnings("unchecked")
290 @Override
291 public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
292 return (C) this.config;
293 }
294 }
295}