Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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.olt.impl; |
| 17 | |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Multiset; |
Tunahan Sezen | f0843b9 | 2021-04-30 07:13:16 +0000 | [diff] [blame^] | 21 | import org.onlab.packet.ChassisId; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.core.DefaultApplicationId; |
Tunahan Sezen | f0843b9 | 2021-04-30 07:13:16 +0000 | [diff] [blame^] | 25 | import org.onosproject.net.DefaultDevice; |
| 26 | import org.onosproject.net.Device; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.meter.MeterId; |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 29 | import org.onosproject.store.service.AsyncConsistentMultimap; |
| 30 | import org.onosproject.store.service.ConsistentMultimap; |
| 31 | import org.onosproject.store.service.ConsistentMultimapBuilder; |
| 32 | import org.onosproject.store.service.MultimapEventListener; |
| 33 | import org.onosproject.store.service.TestConsistentMultimap; |
| 34 | import org.onosproject.store.service.Versioned; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 35 | import org.opencord.sadis.BandwidthProfileInformation; |
| 36 | import org.opencord.sadis.BaseInformationService; |
| 37 | import org.opencord.sadis.SadisService; |
| 38 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 39 | |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 40 | import java.util.AbstractMap; |
| 41 | import java.util.Collection; |
| 42 | import java.util.Iterator; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 43 | import java.util.Map; |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 44 | import java.util.Set; |
| 45 | import java.util.concurrent.Executor; |
| 46 | import java.util.concurrent.atomic.AtomicLong; |
| 47 | import java.util.stream.Collectors; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 48 | |
| 49 | public class TestBase { |
| 50 | |
| 51 | protected static final String CLIENT_NAS_PORT_ID = "PON 1/1"; |
| 52 | protected static final String CLIENT_CIRCUIT_ID = "CIR-PON 1/1"; |
| 53 | protected static final String OLT_DEV_ID = "of:00000000000000aa"; |
| 54 | protected static final DeviceId DEVICE_ID_1 = DeviceId.deviceId(OLT_DEV_ID); |
| 55 | protected MeterId usMeterId = MeterId.meterId(1); |
| 56 | protected MeterId dsMeterId = MeterId.meterId(2); |
| 57 | protected String usBpId = "HSIA-US"; |
| 58 | protected String dsBpId = "HSIA-DS"; |
| 59 | protected DefaultApplicationId appId = new DefaultApplicationId(1, "OltServices"); |
| 60 | |
Tunahan Sezen | f0843b9 | 2021-04-30 07:13:16 +0000 | [diff] [blame^] | 61 | protected static Device olt = new DefaultDevice(null, DeviceId.deviceId(OLT_DEV_ID), Device.Type.SWITCH, |
| 62 | "VOLTHA Project", "open_pon", "open_pon", "BBSIM_OLT_1", new ChassisId("a0a0a0a0a01")); |
| 63 | |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 64 | Map<String, BandwidthProfileInformation> bpInformation = Maps.newConcurrentMap(); |
| 65 | |
| 66 | protected void addBandwidthProfile(String id) { |
| 67 | BandwidthProfileInformation bpInfo = new BandwidthProfileInformation(); |
Gamze Abaka | f46ab43 | 2021-03-03 10:51:17 +0000 | [diff] [blame] | 68 | bpInfo.setGuaranteedInformationRate(0); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 69 | bpInfo.setCommittedInformationRate(10000); |
| 70 | bpInfo.setCommittedBurstSize(1000L); |
| 71 | bpInfo.setExceededBurstSize(2000L); |
| 72 | bpInfo.setExceededInformationRate(20000); |
| 73 | bpInformation.put(id, bpInfo); |
| 74 | } |
| 75 | |
| 76 | protected class MockSadisService implements SadisService { |
| 77 | |
| 78 | @Override |
| 79 | public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() { |
| 80 | return new MockSubService(); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() { |
| 85 | return new MockBpService(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private class MockBpService implements BaseInformationService<BandwidthProfileInformation> { |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 90 | @Override |
Ilayda Ozdemir | 90a9362 | 2021-02-25 09:40:58 +0000 | [diff] [blame] | 91 | public void clearLocalData() { |
| 92 | |
| 93 | } |
| 94 | |
| 95 | @Override |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 96 | public void invalidateAll() { |
| 97 | |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void invalidateId(String id) { |
| 102 | |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public BandwidthProfileInformation get(String id) { |
| 107 | return bpInformation.get(id); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public BandwidthProfileInformation getfromCache(String id) { |
| 112 | return null; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> { |
| 117 | MockSubscriberAndDeviceInformation sub = |
| 118 | new MockSubscriberAndDeviceInformation(CLIENT_NAS_PORT_ID, |
| 119 | CLIENT_NAS_PORT_ID, CLIENT_CIRCUIT_ID, null, null); |
| 120 | |
| 121 | @Override |
| 122 | public SubscriberAndDeviceInformation get(String id) { |
| 123 | return sub; |
| 124 | } |
| 125 | |
| 126 | @Override |
Ilayda Ozdemir | 90a9362 | 2021-02-25 09:40:58 +0000 | [diff] [blame] | 127 | public void clearLocalData() { |
| 128 | |
| 129 | } |
| 130 | |
| 131 | @Override |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 132 | public void invalidateAll() { |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void invalidateId(String id) { |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public SubscriberAndDeviceInformation getfromCache(String id) { |
| 141 | return null; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation { |
| 146 | |
| 147 | MockSubscriberAndDeviceInformation(String id, String nasPortId, |
| 148 | String circuitId, MacAddress hardId, |
| 149 | Ip4Address ipAddress) { |
| 150 | this.setHardwareIdentifier(hardId); |
| 151 | this.setId(id); |
| 152 | this.setIPAddress(ipAddress); |
| 153 | this.setNasPortId(nasPortId); |
| 154 | this.setCircuitId(circuitId); |
| 155 | } |
| 156 | } |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 157 | |
| 158 | class MockConsistentMultimap<K, V> implements ConsistentMultimap<K, V> { |
| 159 | private HashMultimap<K, Versioned<V>> innermap; |
| 160 | private AtomicLong counter = new AtomicLong(); |
| 161 | |
| 162 | public MockConsistentMultimap() { |
| 163 | this.innermap = HashMultimap.create(); |
| 164 | } |
| 165 | |
| 166 | private Versioned<V> version(V v) { |
| 167 | return new Versioned<>(v, counter.incrementAndGet(), System.currentTimeMillis()); |
| 168 | } |
| 169 | |
| 170 | private Versioned<Collection<? extends V>> versionCollection(Collection<? extends V> collection) { |
| 171 | return new Versioned<>(collection, counter.incrementAndGet(), System.currentTimeMillis()); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public int size() { |
| 176 | return innermap.size(); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public boolean isEmpty() { |
| 181 | return innermap.isEmpty(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public boolean containsKey(K key) { |
| 186 | return innermap.containsKey(key); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public boolean containsValue(V value) { |
| 191 | return innermap.containsValue(value); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public boolean containsEntry(K key, V value) { |
| 196 | return innermap.containsEntry(key, value); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public boolean put(K key, V value) { |
| 201 | return innermap.put(key, version(value)); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public Versioned<Collection<? extends V>> putAndGet(K key, V value) { |
| 206 | innermap.put(key, version(value)); |
| 207 | return (Versioned<Collection<? extends V>>) innermap.get(key); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public boolean remove(K key, V value) { |
| 212 | return innermap.remove(key, value); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Versioned<Collection<? extends V>> removeAndGet(K key, V value) { |
| 217 | innermap.remove(key, value); |
| 218 | return (Versioned<Collection<? extends V>>) innermap.get(key); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public boolean removeAll(K key, Collection<? extends V> values) { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public Versioned<Collection<? extends V>> removeAll(K key) { |
| 228 | return null; |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public boolean putAll(K key, Collection<? extends V> values) { |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Versioned<Collection<? extends V>> replaceValues(K key, Collection<V> values) { |
| 238 | return null; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void clear() { |
| 243 | innermap.clear(); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public Versioned<Collection<? extends V>> get(K key) { |
| 248 | Collection<? extends V> values = innermap.get(key).stream() |
| 249 | .map(v -> v.value()) |
| 250 | .collect(Collectors.toList()); |
| 251 | return versionCollection(values); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public Set<K> keySet() { |
| 256 | return innermap.keySet(); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public Multiset<K> keys() { |
| 261 | return innermap.keys(); |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public Multiset<V> values() { |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public Collection<Map.Entry<K, V>> entries() { |
| 271 | return null; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public Iterator<Map.Entry<K, V>> iterator() { |
| 276 | return new ConsistentMultimapIterator(innermap.entries().iterator()); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public Map<K, Collection<V>> asMap() { |
| 281 | return null; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public void addListener(MultimapEventListener<K, V> listener, Executor executor) { |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void removeListener(MultimapEventListener<K, V> listener) { |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public String name() { |
| 294 | return "mock multimap"; |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public Type primitiveType() { |
| 299 | return null; |
| 300 | } |
| 301 | |
| 302 | private class ConsistentMultimapIterator implements Iterator<Map.Entry<K, V>> { |
| 303 | |
| 304 | private final Iterator<Map.Entry<K, Versioned<V>>> it; |
| 305 | |
| 306 | public ConsistentMultimapIterator(Iterator<Map.Entry<K, Versioned<V>>> it) { |
| 307 | this.it = it; |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public boolean hasNext() { |
| 312 | return it.hasNext(); |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public Map.Entry<K, V> next() { |
| 317 | Map.Entry<K, Versioned<V>> e = it.next(); |
| 318 | return new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue().value()); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | } |
| 323 | |
| 324 | public static TestConsistentMultimap.Builder builder() { |
| 325 | return new TestConsistentMultimap.Builder(); |
| 326 | } |
| 327 | |
| 328 | public static class Builder<K, V> extends ConsistentMultimapBuilder<K, V> { |
| 329 | |
| 330 | @Override |
| 331 | public AsyncConsistentMultimap<K, V> buildMultimap() { |
| 332 | return null; |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public ConsistentMultimap<K, V> build() { |
| 337 | return new TestConsistentMultimap<K, V>(); |
| 338 | } |
| 339 | } |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 340 | } |