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(); |
| 59 | assertEquals(3, 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))); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void testLocalMode() throws Exception { |
| 67 | |
| 68 | BaseInformationService<SubscriberAndDeviceInformation> subscriberService = sadis.getSubscriberInfoService(); |
| 69 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 70 | checkGetForExisting(ID1, entry1, subscriberService); |
| 71 | checkGetForExisting(ID2, entry2, subscriberService); |
| 72 | checkGetForExisting(ID5, entry5, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 73 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 74 | invalidateId(ID1, subscriberService); |
| 75 | checkFromBoth(ID1, entry1, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 76 | |
| 77 | invalidateAll(subscriberService); |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 78 | checkFromBoth(ID2, entry2, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | private void checkGetForNonExist(String id, BaseInformationService service) { |
| 83 | BaseInformation entry = service.get(id); |
| 84 | assertNull(entry); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testRemoteMode() throws Exception { |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 89 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 90 | BaseInformationService<SubscriberAndDeviceInformation> subscriberService = sadis.getSubscriberInfoService(); |
| 91 | config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate); |
| 92 | configListener.event(event); |
| 93 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 94 | checkGetForExisting(ID3, entry3, subscriberService); |
| 95 | checkGetForExisting(ID4, entry4, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 96 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 97 | invalidateId(ID3, subscriberService); |
| 98 | checkFromBoth(ID3, entry3, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 99 | |
| 100 | invalidateAll(subscriberService); |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 101 | checkFromBoth(ID4, entry4, subscriberService); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testModeSwitch() throws Exception { |
| 106 | BaseInformationService<SubscriberAndDeviceInformation> service = sadis.getSubscriberInfoService(); |
| 107 | config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate); |
| 108 | configListener.event(event); |
| 109 | |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 110 | service.clearLocalData(); |
| 111 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 112 | checkGetForExisting(ID3, null, service); |
| 113 | checkGetForNonExist(ID1, service); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 114 | |
| 115 | config.init(subject, "sadis-local-mode-test", node("/LocalSubConfig.json"), mapper, delegate); |
| 116 | configListener.event(event); |
| 117 | |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 118 | checkGetForExisting(ID1, null, service); |
| 119 | checkGetForNonExist(ID3, service); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 122 | // test the hybrid mode (both local and remote data in the config) |
| 123 | // ids 1 and 2 are local, others are remote |
| 124 | @Test |
| 125 | public void testHybridMode() throws Exception { |
| 126 | BaseInformationService<SubscriberAndDeviceInformation> subscriberService = sadis.getSubscriberInfoService(); |
| 127 | config.init(subject, "sadis-hybrid-mode-test", node("/HybridSubConfig.json"), mapper, delegate); |
| 128 | configListener.event(event); |
| 129 | |
| 130 | // check that I can fetch from remote |
| 131 | checkGetForExisting(ID3, entry3, subscriberService); |
| 132 | checkGetForExisting(ID4, entry4, subscriberService); |
| 133 | |
| 134 | // check that I can fetch from local |
| 135 | checkGetForExisting(ID1, entry1, subscriberService); |
| 136 | checkGetForExisting(ID2, entry2, subscriberService); |
| 137 | } |
| 138 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 139 | public boolean checkEquality(BaseInformation localEntry, BaseInformation entry) { |
| 140 | SubscriberAndDeviceInformation sub = (SubscriberAndDeviceInformation) localEntry; |
| 141 | SubscriberAndDeviceInformation other = (SubscriberAndDeviceInformation) localEntry; |
| 142 | |
| 143 | if (other == null) { |
| 144 | return false; |
| 145 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 146 | if (sub.hardwareIdentifier() == null) { |
| 147 | if (other.hardwareIdentifier() != null) { |
| 148 | return false; |
| 149 | } |
| 150 | } else if (!sub.hardwareIdentifier().equals(other.hardwareIdentifier())) { |
| 151 | return false; |
| 152 | } |
| 153 | if (sub.id() == null) { |
| 154 | if (other.id() != null) { |
| 155 | return false; |
| 156 | } |
| 157 | } else if (!sub.id().equals(other.id())) { |
| 158 | return false; |
| 159 | } |
| 160 | if (sub.nasPortId() == null) { |
| 161 | if (other.nasPortId() != null) { |
| 162 | return false; |
| 163 | } |
| 164 | } else if (!sub.nasPortId().equals(other.nasPortId())) { |
| 165 | return false; |
| 166 | } |
| 167 | if (sub.nasId() == null) { |
| 168 | if (other.nasId() != null) { |
| 169 | return false; |
| 170 | } |
| 171 | } else if (!sub.nasId().equals(other.nasId())) { |
| 172 | return false; |
| 173 | } |
| 174 | if (sub.ipAddress() == null) { |
| 175 | if (other.ipAddress() != null) { |
| 176 | return false; |
| 177 | } |
| 178 | } else if (!sub.ipAddress().equals(other.ipAddress())) { |
| 179 | return false; |
| 180 | } |
| 181 | if (sub.uplinkPort() != other.uplinkPort()) { |
| 182 | return false; |
| 183 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 184 | if (sub.slot() != other.slot()) { |
| 185 | return false; |
| 186 | } |
| 187 | if (sub.circuitId() == null) { |
| 188 | if (other.circuitId() != null) { |
| 189 | return false; |
| 190 | } |
| 191 | } else if (!sub.circuitId().equals(other.circuitId())) { |
| 192 | return false; |
| 193 | } |
| 194 | if (sub.remoteId() == null) { |
| 195 | if (other.remoteId() != null) { |
| 196 | return false; |
| 197 | } |
| 198 | } else if (!sub.remoteId().equals(other.remoteId())) { |
| 199 | return false; |
| 200 | } |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 201 | if (sub.uniTagList() == null) { |
| 202 | if (other.uniTagList() != null) { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 203 | return false; |
| 204 | } |
Matteo Scandolo | 198aef9 | 2020-01-08 00:43:17 +0000 | [diff] [blame] | 205 | } else if (!sub.uniTagList().equals(other.uniTagList())) { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | return true; |
| 209 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 210 | } |