Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.opencord.sadis.impl; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 27 | |
Matteo Scandolo | e4f4b63 | 2020-01-07 23:54:35 +0000 | [diff] [blame] | 28 | import org.opencord.sadis.BaseConfig; |
Matteo Scandolo | e4f4b63 | 2020-01-07 23:54:35 +0000 | [diff] [blame] | 29 | import org.opencord.sadis.BaseInformation; |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 30 | import org.opencord.sadis.BaseInformationService; |
| 31 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Set of tests of the SADIS ONOS application component. |
| 35 | */ |
| 36 | public class SubscriberAndDeviceManagerTest extends BaseSadis { |
| 37 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 38 | @Before |
| 39 | public void setUp() throws Exception { |
| 40 | config = new SubscriberAndDeviceInformationConfig(); |
| 41 | super.setUp("/LocalSubConfig.json", SubscriberAndDeviceInformationConfig.class); |
| 42 | } |
| 43 | |
| 44 | @After |
| 45 | public void tearDown() { |
| 46 | super.tearDown(); |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void testConfiguration() { |
| 51 | SubscriberAndDeviceInformationConfig config = sadis.cfgService.getConfig(null, |
| 52 | SubscriberAndDeviceInformationConfig.class); |
| 53 | checkConfigInfo(50, "PT1m", config); |
| 54 | checkEntriesForSubscriberAndAccessDevice(config); |
| 55 | } |
| 56 | |
| 57 | private void checkEntriesForSubscriberAndAccessDevice(BaseConfig config) { |
| 58 | List<SubscriberAndDeviceInformation> entries = config.getEntries(); |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame^] | 59 | assertEquals(4, entries.size()); |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 60 | assertTrue(checkEquality(entry1, entries.get(0))); |
| 61 | assertTrue(checkEquality(entry2, entries.get(1))); |
| 62 | assertTrue(checkEquality(entry5, entries.get(2))); |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame^] | 63 | assertTrue(checkEquality(entry6, entries.get(3))); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testLocalMode() throws Exception { |
| 68 | |
| 69 | BaseInformationService<SubscriberAndDeviceInformation> subscriberService = sadis.getSubscriberInfoService(); |
| 70 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 71 | checkGetForExisting(ID1, entry1, subscriberService); |
| 72 | checkGetForExisting(ID2, entry2, subscriberService); |
| 73 | checkGetForExisting(ID5, entry5, subscriberService); |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame^] | 74 | checkGetForExisting(ID6, entry6, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 75 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 76 | invalidateId(ID1, subscriberService); |
| 77 | checkFromBoth(ID1, entry1, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 78 | |
| 79 | invalidateAll(subscriberService); |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 80 | checkFromBoth(ID2, entry2, subscriberService); |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame^] | 81 | checkFromBoth(ID6, entry6, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | private void checkGetForNonExist(String id, BaseInformationService service) { |
| 86 | BaseInformation entry = service.get(id); |
| 87 | assertNull(entry); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testRemoteMode() throws Exception { |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 92 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 93 | BaseInformationService<SubscriberAndDeviceInformation> subscriberService = sadis.getSubscriberInfoService(); |
| 94 | config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate); |
| 95 | configListener.event(event); |
| 96 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 97 | checkGetForExisting(ID3, entry3, subscriberService); |
| 98 | checkGetForExisting(ID4, entry4, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 99 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 100 | invalidateId(ID3, subscriberService); |
| 101 | checkFromBoth(ID3, entry3, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 102 | |
| 103 | invalidateAll(subscriberService); |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 104 | checkFromBoth(ID4, entry4, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testModeSwitch() throws Exception { |
| 109 | BaseInformationService<SubscriberAndDeviceInformation> service = sadis.getSubscriberInfoService(); |
| 110 | config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate); |
| 111 | configListener.event(event); |
| 112 | |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 113 | service.clearLocalData(); |
| 114 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 115 | checkGetForExisting(ID3, null, service); |
| 116 | checkGetForNonExist(ID1, service); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 117 | |
| 118 | config.init(subject, "sadis-local-mode-test", node("/LocalSubConfig.json"), mapper, delegate); |
| 119 | configListener.event(event); |
| 120 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 121 | checkGetForExisting(ID1, null, service); |
| 122 | checkGetForNonExist(ID3, service); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 125 | // test the hybrid mode (both local and remote data in the config) |
| 126 | // ids 1 and 2 are local, others are remote |
| 127 | @Test |
| 128 | public void testHybridMode() throws Exception { |
| 129 | BaseInformationService<SubscriberAndDeviceInformation> subscriberService = sadis.getSubscriberInfoService(); |
| 130 | config.init(subject, "sadis-hybrid-mode-test", node("/HybridSubConfig.json"), mapper, delegate); |
| 131 | configListener.event(event); |
| 132 | |
| 133 | // check that I can fetch from remote |
| 134 | checkGetForExisting(ID3, entry3, subscriberService); |
| 135 | checkGetForExisting(ID4, entry4, subscriberService); |
| 136 | |
| 137 | // check that I can fetch from local |
| 138 | checkGetForExisting(ID1, entry1, subscriberService); |
| 139 | checkGetForExisting(ID2, entry2, subscriberService); |
| 140 | } |
| 141 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 142 | public boolean checkEquality(BaseInformation localEntry, BaseInformation entry) { |
| 143 | SubscriberAndDeviceInformation sub = (SubscriberAndDeviceInformation) localEntry; |
| 144 | SubscriberAndDeviceInformation other = (SubscriberAndDeviceInformation) localEntry; |
| 145 | |
| 146 | if (other == null) { |
| 147 | return false; |
| 148 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 149 | if (sub.hardwareIdentifier() == null) { |
| 150 | if (other.hardwareIdentifier() != null) { |
| 151 | return false; |
| 152 | } |
| 153 | } else if (!sub.hardwareIdentifier().equals(other.hardwareIdentifier())) { |
| 154 | return false; |
| 155 | } |
| 156 | if (sub.id() == null) { |
| 157 | if (other.id() != null) { |
| 158 | return false; |
| 159 | } |
| 160 | } else if (!sub.id().equals(other.id())) { |
| 161 | return false; |
| 162 | } |
| 163 | if (sub.nasPortId() == null) { |
| 164 | if (other.nasPortId() != null) { |
| 165 | return false; |
| 166 | } |
| 167 | } else if (!sub.nasPortId().equals(other.nasPortId())) { |
| 168 | return false; |
| 169 | } |
| 170 | if (sub.nasId() == null) { |
| 171 | if (other.nasId() != null) { |
| 172 | return false; |
| 173 | } |
| 174 | } else if (!sub.nasId().equals(other.nasId())) { |
| 175 | return false; |
| 176 | } |
| 177 | if (sub.ipAddress() == null) { |
| 178 | if (other.ipAddress() != null) { |
| 179 | return false; |
| 180 | } |
| 181 | } else if (!sub.ipAddress().equals(other.ipAddress())) { |
| 182 | return false; |
| 183 | } |
| 184 | if (sub.uplinkPort() != other.uplinkPort()) { |
| 185 | return false; |
| 186 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 187 | if (sub.slot() != other.slot()) { |
| 188 | return false; |
| 189 | } |
| 190 | if (sub.circuitId() == null) { |
| 191 | if (other.circuitId() != null) { |
| 192 | return false; |
| 193 | } |
| 194 | } else if (!sub.circuitId().equals(other.circuitId())) { |
| 195 | return false; |
| 196 | } |
| 197 | if (sub.remoteId() == null) { |
| 198 | if (other.remoteId() != null) { |
| 199 | return false; |
| 200 | } |
| 201 | } else if (!sub.remoteId().equals(other.remoteId())) { |
| 202 | return false; |
| 203 | } |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 204 | if (sub.uniTagList() == null) { |
| 205 | if (other.uniTagList() != null) { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 206 | return false; |
| 207 | } |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 208 | } else if (!sub.uniTagList().equals(other.uniTagList())) { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 213 | } |