blob: cf0f2e67da1549ac1f2c4dd0a56fecd0a9892684 [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;
30import org.onlab.packet.MacAddress;
31import org.onlab.packet.VlanId;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreServiceAdapter;
34import org.onosproject.net.config.Config;
35import org.onosproject.net.config.ConfigApplyDelegate;
36import org.onosproject.net.config.NetworkConfigRegistryAdapter;
37
38import com.fasterxml.jackson.databind.JsonNode;
39import com.fasterxml.jackson.databind.ObjectMapper;
40
41/**
42 * Set of tests of the SADIS ONOS application component.
43 */
44public class SadisManagerTest {
45
46 private SadisManager sadis;
47
48 @Before
49 public void setUp() throws Exception {
50 this.sadis = new SadisManager();
51 this.sadis.coreService = new MockCoreService();
52
53 final InputStream jsonStream = SadisManagerTest.class.getResourceAsStream("/config.json");
54
55 final ObjectMapper mapper = new ObjectMapper();
56 final JsonNode testConfig = mapper.readTree(jsonStream);
57 final ConfigApplyDelegate delegate = new MockConfigDelegate();
58
59 final SadisConfig config = new SadisConfig();
60 final ApplicationId subject = this.sadis.coreService.registerApplication("org.opencord.sadis");
61
62 config.init(subject, "sadis-test", testConfig, mapper, delegate);
63
64 this.sadis.cfgService = new MockNetworkConfigRegistry(config);
65 this.sadis.activate();
66 }
67
68 @After
69 public void tearDown() {
70 this.sadis.deactivate();
71 }
72
73 @Test
74 public void testConfiguration() {
75 SadisConfig config = sadis.cfgService.getConfig(null, SadisConfig.class);
76 assertEquals(true, config.getCacheEnabled());
77 assertEquals(50, config.getCacheMaxSize());
78 assertEquals(Duration.parse("PT1m"), config.getCacheTtl());
79 List<SubscriberAndDeviceInformation> entries = config.getEntries();
80 assertEquals(2, entries.size());
81 assertEquals(SubscriberAndDeviceInformationBuilder.build("1", (short) 2, (short) 2, "1/1/2", (short) 125,
82 (short) 3, "aa:bb:cc:dd:ee:ff"), entries.get(0));
83 assertEquals(SubscriberAndDeviceInformationBuilder.build("2", (short) 4, (short) 4, "1/1/2", (short) 129,
84 (short) 4, "aa:bb:cc:dd:ee:ff"), entries.get(1));
85
86 }
87
88 // Mocks live here
89
90 private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
91
92 public static SubscriberAndDeviceInformation build(String id, short cTag, short sTag, String nasPortId,
93 short port, short slot, String mac) {
94 SubscriberAndDeviceInformation info = new SubscriberAndDeviceInformation();
95 info.setId(id);
96 info.setCTag(VlanId.vlanId(cTag));
97 info.setSTag(VlanId.vlanId(sTag));
98 info.setNasPortId(nasPortId);
99 info.setPort(port);
100 info.setSlot(slot);
101 info.setHardwareIdentifier(MacAddress.valueOf(mac));
102 return info;
103 }
104 }
105
106 /**
107 * Mocks an ONOS configuration delegate to allow JSON based configuration to
108 * be tested.
109 */
110 private static final class MockConfigDelegate implements ConfigApplyDelegate {
111 @Override
112 public void onApply(@SuppressWarnings("rawtypes") Config config) {
113 config.apply();
114 }
115 }
116
117 /**
118 * Mocks an instance of {@link ApplicationId} so that the application
119 * component under test can query and use its application ID.
120 */
121 private static final class MockApplicationId implements ApplicationId {
122
123 private final short id;
124 private final String name;
125
126 public MockApplicationId(short id, String name) {
127 this.id = id;
128 this.name = name;
129 }
130
131 @Override
132 public short id() {
133 return id;
134 }
135
136 @Override
137 public String name() {
138 return name;
139 }
140 }
141
142 /**
143 * Mocks the core services of ONOS so that the application under test can
144 * register and query application IDs.
145 */
146 private static final class MockCoreService extends CoreServiceAdapter {
147
148 private List<ApplicationId> idList = new ArrayList<ApplicationId>();
149 private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
150
151 /*
152 * (non-Javadoc)
153 *
154 * @see
155 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
156 */
157 @Override
158 public ApplicationId getAppId(Short id) {
159 if (id >= idList.size()) {
160 return null;
161 }
162 return idList.get(id);
163 }
164
165 /*
166 * (non-Javadoc)
167 *
168 * @see
169 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
170 */
171 @Override
172 public ApplicationId getAppId(String name) {
173 return idMap.get(name);
174 }
175
176 /*
177 * (non-Javadoc)
178 *
179 * @see
180 * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
181 * .String)
182 */
183 @Override
184 public ApplicationId registerApplication(String name) {
185 ApplicationId appId = idMap.get(name);
186 if (appId == null) {
187 appId = new MockApplicationId((short) idList.size(), name);
188 idList.add(appId);
189 idMap.put(name, appId);
190 }
191 return appId;
192 }
193
194 }
195
196 /**
197 * Mocks the ONOS network configuration registry so that the application
198 * under test can access a JSON defined configuration.
199 */
200 static final class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
201 private final SadisConfig config;
202
203 public MockNetworkConfigRegistry(final SadisConfig config) {
204 this.config = config;
205 }
206
207 @SuppressWarnings("unchecked")
208 @Override
209 public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
210 return (C) this.config;
211 }
212 }
213}