David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 180c109 | 2017-08-03 22:46:14 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 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 | */ |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 16 | package org.opencord.sadis.impl; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 19 | import static org.junit.Assert.assertTrue; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 20 | |
| 21 | import java.io.InputStream; |
| 22 | import java.time.Duration; |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.HashMap; |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 31 | import org.onlab.packet.Ip4Address; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 32 | import org.onlab.packet.MacAddress; |
| 33 | import org.onlab.packet.VlanId; |
| 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreServiceAdapter; |
| 36 | import org.onosproject.net.config.Config; |
| 37 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 38 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 39 | |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 40 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 41 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 42 | import com.fasterxml.jackson.databind.JsonNode; |
| 43 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 44 | |
| 45 | /** |
| 46 | * Set of tests of the SADIS ONOS application component. |
| 47 | */ |
| 48 | public class SadisManagerTest { |
| 49 | |
| 50 | private SadisManager sadis; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() throws Exception { |
| 54 | this.sadis = new SadisManager(); |
| 55 | this.sadis.coreService = new MockCoreService(); |
| 56 | |
| 57 | final InputStream jsonStream = SadisManagerTest.class.getResourceAsStream("/config.json"); |
| 58 | |
| 59 | final ObjectMapper mapper = new ObjectMapper(); |
| 60 | final JsonNode testConfig = mapper.readTree(jsonStream); |
| 61 | final ConfigApplyDelegate delegate = new MockConfigDelegate(); |
| 62 | |
| 63 | final SadisConfig config = new SadisConfig(); |
| 64 | final ApplicationId subject = this.sadis.coreService.registerApplication("org.opencord.sadis"); |
| 65 | |
| 66 | config.init(subject, "sadis-test", testConfig, mapper, delegate); |
| 67 | |
| 68 | this.sadis.cfgService = new MockNetworkConfigRegistry(config); |
| 69 | this.sadis.activate(); |
| 70 | } |
| 71 | |
| 72 | @After |
| 73 | public void tearDown() { |
| 74 | this.sadis.deactivate(); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testConfiguration() { |
| 79 | SadisConfig config = sadis.cfgService.getConfig(null, SadisConfig.class); |
| 80 | assertEquals(true, config.getCacheEnabled()); |
| 81 | assertEquals(50, config.getCacheMaxSize()); |
| 82 | assertEquals(Duration.parse("PT1m"), config.getCacheTtl()); |
| 83 | List<SubscriberAndDeviceInformation> entries = config.getEntries(); |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 84 | assertEquals(3, entries.size()); |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 85 | assertTrue(SubscriberAndDeviceInformationBuilder.build("1", (short) 2, (short) 2, "1/1/2", (short) 125, |
| 86 | (short) 3, "aa:bb:cc:dd:ee:ff", "XXX-NASID", "10.10.10.10").checkEquals(entries.get(0))); |
| 87 | assertTrue(SubscriberAndDeviceInformationBuilder.build("2", (short) 4, (short) 4, "1/1/2", (short) 129, |
| 88 | (short) 4, "aa:bb:cc:dd:ee:ff", "YYY-NASID", "1.1.1.1").checkEquals(entries.get(1))); |
| 89 | assertTrue(SubscriberAndDeviceInformationBuilder.build("cc:dd:ee:ff:aa:bb", (short) -1, (short) -1, null, |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 90 | (short) -1, |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 91 | (short) -1, "cc:dd:ee:ff:aa:bb", "CCC-NASID", "12.12.12.12").checkEquals(entries.get(2))); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 92 | |
| 93 | } |
| 94 | |
| 95 | // Mocks live here |
| 96 | |
| 97 | private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation { |
| 98 | |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 99 | public static SubscriberAndDeviceInformationBuilder build(String id, short cTag, short sTag, String nasPortId, |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 100 | short port, short slot, String mac, String nasId, String ipAddress) { |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 101 | //SubscriberAndDeviceInformation info = new SubscriberAndDeviceInformation(); |
| 102 | SubscriberAndDeviceInformationBuilder info = new SubscriberAndDeviceInformationBuilder(); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 103 | info.setId(id); |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 104 | if (cTag != -1) { |
| 105 | info.setCTag(VlanId.vlanId(cTag)); |
| 106 | } |
| 107 | if (sTag != -1) { |
| 108 | info.setSTag(VlanId.vlanId(sTag)); |
| 109 | } |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 110 | info.setNasPortId(nasPortId); |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 111 | if (port != -1) { |
| 112 | info.setPort(port); |
| 113 | } |
| 114 | if (slot != -1) { |
| 115 | info.setSlot(slot); |
| 116 | } |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 117 | info.setHardwareIdentifier(MacAddress.valueOf(mac)); |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 118 | info.setIPAddress(Ip4Address.valueOf(ipAddress)); |
| 119 | info.setNasId(nasId); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 120 | return info; |
| 121 | } |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 122 | |
| 123 | public boolean checkEquals(SubscriberAndDeviceInformation other) { |
| 124 | if (other == null) { |
| 125 | return false; |
| 126 | } |
| 127 | if (this.cTag() == null) { |
| 128 | if (other.cTag() != null) { |
| 129 | return false; |
| 130 | } |
| 131 | } else if (!this.cTag().equals(other.cTag())) { |
| 132 | return false; |
| 133 | } |
| 134 | if (this.hardwareIdentifier() == null) { |
| 135 | if (other.hardwareIdentifier() != null) { |
| 136 | return false; |
| 137 | } |
| 138 | } else if (!this.hardwareIdentifier().equals(other.hardwareIdentifier())) { |
| 139 | return false; |
| 140 | } |
| 141 | if (this.id() == null) { |
| 142 | if (other.id() != null) { |
| 143 | return false; |
| 144 | } |
| 145 | } else if (!this.id().equals(other.id())) { |
| 146 | return false; |
| 147 | } |
| 148 | if (this.nasPortId() == null) { |
| 149 | if (other.nasPortId() != null) { |
| 150 | return false; |
| 151 | } |
| 152 | } else if (!this.nasPortId().equals(other.nasPortId())) { |
| 153 | return false; |
| 154 | } |
| 155 | if (this.nasId() == null) { |
| 156 | if (other.nasId() != null) { |
| 157 | return false; |
| 158 | } |
| 159 | } else if (!this.nasId().equals(other.nasId())) { |
| 160 | return false; |
| 161 | } |
| 162 | if (this.ipAddress() == null) { |
| 163 | if (other.ipAddress() != null) { |
| 164 | return false; |
| 165 | } |
| 166 | } else if (!this.ipAddress().equals(other.ipAddress())) { |
| 167 | return false; |
| 168 | } |
| 169 | if (this.port() != other.port()) { |
| 170 | return false; |
| 171 | } |
| 172 | if (this.sTag() == null) { |
| 173 | if (other.sTag() != null) { |
| 174 | return false; |
| 175 | } |
| 176 | } else if (!this.sTag().equals(other.sTag())) { |
| 177 | return false; |
| 178 | } |
| 179 | if (this.slot() != other.slot()) { |
| 180 | return false; |
| 181 | } |
| 182 | return true; |
| 183 | } |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Mocks an ONOS configuration delegate to allow JSON based configuration to |
| 188 | * be tested. |
| 189 | */ |
| 190 | private static final class MockConfigDelegate implements ConfigApplyDelegate { |
| 191 | @Override |
| 192 | public void onApply(@SuppressWarnings("rawtypes") Config config) { |
| 193 | config.apply(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Mocks an instance of {@link ApplicationId} so that the application |
| 199 | * component under test can query and use its application ID. |
| 200 | */ |
| 201 | private static final class MockApplicationId implements ApplicationId { |
| 202 | |
| 203 | private final short id; |
| 204 | private final String name; |
| 205 | |
| 206 | public MockApplicationId(short id, String name) { |
| 207 | this.id = id; |
| 208 | this.name = name; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public short id() { |
| 213 | return id; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public String name() { |
| 218 | return name; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Mocks the core services of ONOS so that the application under test can |
| 224 | * register and query application IDs. |
| 225 | */ |
| 226 | private static final class MockCoreService extends CoreServiceAdapter { |
| 227 | |
| 228 | private List<ApplicationId> idList = new ArrayList<ApplicationId>(); |
| 229 | private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>(); |
| 230 | |
| 231 | /* |
| 232 | * (non-Javadoc) |
| 233 | * |
| 234 | * @see |
| 235 | * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short) |
| 236 | */ |
| 237 | @Override |
| 238 | public ApplicationId getAppId(Short id) { |
| 239 | if (id >= idList.size()) { |
| 240 | return null; |
| 241 | } |
| 242 | return idList.get(id); |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * (non-Javadoc) |
| 247 | * |
| 248 | * @see |
| 249 | * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String) |
| 250 | */ |
| 251 | @Override |
| 252 | public ApplicationId getAppId(String name) { |
| 253 | return idMap.get(name); |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * (non-Javadoc) |
| 258 | * |
| 259 | * @see |
| 260 | * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang |
| 261 | * .String) |
| 262 | */ |
| 263 | @Override |
| 264 | public ApplicationId registerApplication(String name) { |
| 265 | ApplicationId appId = idMap.get(name); |
| 266 | if (appId == null) { |
| 267 | appId = new MockApplicationId((short) idList.size(), name); |
| 268 | idList.add(appId); |
| 269 | idMap.put(name, appId); |
| 270 | } |
| 271 | return appId; |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Mocks the ONOS network configuration registry so that the application |
| 278 | * under test can access a JSON defined configuration. |
| 279 | */ |
| 280 | static final class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
| 281 | private final SadisConfig config; |
| 282 | |
| 283 | public MockNetworkConfigRegistry(final SadisConfig config) { |
| 284 | this.config = config; |
| 285 | } |
| 286 | |
| 287 | @SuppressWarnings("unchecked") |
| 288 | @Override |
| 289 | public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) { |
| 290 | return (C) this.config; |
| 291 | } |
| 292 | } |
| 293 | } |