blob: 0ea58bb6bcec2d7485214270d0314790c53039b2 [file] [log] [blame]
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
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;
17
18import static org.junit.Assert.assertEquals;
19
20import java.io.InputStream;
21import java.time.Duration;
22import java.util.ArrayList;
23import java.util.HashMap;
24import java.util.List;
25import java.util.Map;
26
27import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
Amit Ghosh38b232a2017-07-23 15:11:56 +010030import org.onlab.packet.Ip4Address;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070031import org.onlab.packet.MacAddress;
32import org.onlab.packet.VlanId;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreServiceAdapter;
35import org.onosproject.net.config.Config;
36import org.onosproject.net.config.ConfigApplyDelegate;
37import org.onosproject.net.config.NetworkConfigRegistryAdapter;
38
39import com.fasterxml.jackson.databind.JsonNode;
40import com.fasterxml.jackson.databind.ObjectMapper;
41
42/**
43 * Set of tests of the SADIS ONOS application component.
44 */
45public class SadisManagerTest {
46
47 private SadisManager sadis;
48
49 @Before
50 public void setUp() throws Exception {
51 this.sadis = new SadisManager();
52 this.sadis.coreService = new MockCoreService();
53
54 final InputStream jsonStream = SadisManagerTest.class.getResourceAsStream("/config.json");
55
56 final ObjectMapper mapper = new ObjectMapper();
57 final JsonNode testConfig = mapper.readTree(jsonStream);
58 final ConfigApplyDelegate delegate = new MockConfigDelegate();
59
60 final SadisConfig config = new SadisConfig();
61 final ApplicationId subject = this.sadis.coreService.registerApplication("org.opencord.sadis");
62
63 config.init(subject, "sadis-test", testConfig, mapper, delegate);
64
65 this.sadis.cfgService = new MockNetworkConfigRegistry(config);
66 this.sadis.activate();
67 }
68
69 @After
70 public void tearDown() {
71 this.sadis.deactivate();
72 }
73
74 @Test
75 public void testConfiguration() {
76 SadisConfig config = sadis.cfgService.getConfig(null, SadisConfig.class);
77 assertEquals(true, config.getCacheEnabled());
78 assertEquals(50, config.getCacheMaxSize());
79 assertEquals(Duration.parse("PT1m"), config.getCacheTtl());
80 List<SubscriberAndDeviceInformation> entries = config.getEntries();
Amit Ghosh38b232a2017-07-23 15:11:56 +010081 assertEquals(3, entries.size());
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070082 assertEquals(SubscriberAndDeviceInformationBuilder.build("1", (short) 2, (short) 2, "1/1/2", (short) 125,
Amit Ghosh38b232a2017-07-23 15:11:56 +010083 (short) 3, "aa:bb:cc:dd:ee:ff", "XXX-NASID", "10.10.10.10"), entries.get(0));
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070084 assertEquals(SubscriberAndDeviceInformationBuilder.build("2", (short) 4, (short) 4, "1/1/2", (short) 129,
Amit Ghosh38b232a2017-07-23 15:11:56 +010085 (short) 4, "aa:bb:cc:dd:ee:ff", "YYY-NASID", "1.1.1.1"), entries.get(1));
86 assertEquals(SubscriberAndDeviceInformationBuilder.build("cc:dd:ee:ff:aa:bb", (short) -1, (short) -1, null,
87 (short) -1,
88 (short) -1, "cc:dd:ee:ff:aa:bb", "CCC-NASID", "12.12.12.12"), entries.get(2));
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070089
90 }
91
92 // Mocks live here
93
94 private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
95
96 public static SubscriberAndDeviceInformation build(String id, short cTag, short sTag, String nasPortId,
Amit Ghosh38b232a2017-07-23 15:11:56 +010097 short port, short slot, String mac, String nasId, String ipAddress) {
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070098 SubscriberAndDeviceInformation info = new SubscriberAndDeviceInformation();
99 info.setId(id);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100100 if (cTag != -1) {
101 info.setCTag(VlanId.vlanId(cTag));
102 }
103 if (sTag != -1) {
104 info.setSTag(VlanId.vlanId(sTag));
105 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700106 info.setNasPortId(nasPortId);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100107 if (port != -1) {
108 info.setPort(port);
109 }
110 if (slot != -1) {
111 info.setSlot(slot);
112 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700113 info.setHardwareIdentifier(MacAddress.valueOf(mac));
Amit Ghosh38b232a2017-07-23 15:11:56 +0100114 info.setIPAddress(Ip4Address.valueOf(ipAddress));
115 info.setNasId(nasId);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700116 return info;
117 }
118 }
119
120 /**
121 * Mocks an ONOS configuration delegate to allow JSON based configuration to
122 * be tested.
123 */
124 private static final class MockConfigDelegate implements ConfigApplyDelegate {
125 @Override
126 public void onApply(@SuppressWarnings("rawtypes") Config config) {
127 config.apply();
128 }
129 }
130
131 /**
132 * Mocks an instance of {@link ApplicationId} so that the application
133 * component under test can query and use its application ID.
134 */
135 private static final class MockApplicationId implements ApplicationId {
136
137 private final short id;
138 private final String name;
139
140 public MockApplicationId(short id, String name) {
141 this.id = id;
142 this.name = name;
143 }
144
145 @Override
146 public short id() {
147 return id;
148 }
149
150 @Override
151 public String name() {
152 return name;
153 }
154 }
155
156 /**
157 * Mocks the core services of ONOS so that the application under test can
158 * register and query application IDs.
159 */
160 private static final class MockCoreService extends CoreServiceAdapter {
161
162 private List<ApplicationId> idList = new ArrayList<ApplicationId>();
163 private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
164
165 /*
166 * (non-Javadoc)
167 *
168 * @see
169 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
170 */
171 @Override
172 public ApplicationId getAppId(Short id) {
173 if (id >= idList.size()) {
174 return null;
175 }
176 return idList.get(id);
177 }
178
179 /*
180 * (non-Javadoc)
181 *
182 * @see
183 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
184 */
185 @Override
186 public ApplicationId getAppId(String name) {
187 return idMap.get(name);
188 }
189
190 /*
191 * (non-Javadoc)
192 *
193 * @see
194 * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
195 * .String)
196 */
197 @Override
198 public ApplicationId registerApplication(String name) {
199 ApplicationId appId = idMap.get(name);
200 if (appId == null) {
201 appId = new MockApplicationId((short) idList.size(), name);
202 idList.add(appId);
203 idMap.put(name, appId);
204 }
205 return appId;
206 }
207
208 }
209
210 /**
211 * Mocks the ONOS network configuration registry so that the application
212 * under test can access a JSON defined configuration.
213 */
214 static final class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
215 private final SadisConfig config;
216
217 public MockNetworkConfigRegistry(final SadisConfig config) {
218 this.config = config;
219 }
220
221 @SuppressWarnings("unchecked")
222 @Override
223 public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
224 return (C) this.config;
225 }
226 }
227}