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