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 | |
Andrea Campanella | af39b4c | 2020-05-13 14:07:44 +0200 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Maps; |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.cfg.ComponentConfigService; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 29 | import org.onosproject.net.meter.Band; |
| 30 | import org.onosproject.net.meter.DefaultBand; |
| 31 | import org.onosproject.net.meter.DefaultMeterRequest; |
| 32 | import org.onosproject.net.meter.Meter; |
| 33 | import org.onosproject.net.meter.MeterContext; |
| 34 | import org.onosproject.net.meter.MeterEvent; |
| 35 | import org.onosproject.net.meter.MeterFailReason; |
| 36 | import org.onosproject.net.meter.MeterId; |
| 37 | import org.onosproject.net.meter.MeterKey; |
| 38 | import org.onosproject.net.meter.MeterListener; |
| 39 | import org.onosproject.net.meter.MeterRequest; |
| 40 | import org.onosproject.net.meter.MeterService; |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 41 | import org.onosproject.store.serializers.KryoNamespaces; |
| 42 | import org.onosproject.store.service.ConsistentMultimap; |
| 43 | import org.onosproject.store.service.Serializer; |
| 44 | import org.onosproject.store.service.StorageService; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 45 | import org.opencord.olt.internalapi.AccessDeviceMeterService; |
| 46 | import org.opencord.sadis.BandwidthProfileInformation; |
| 47 | import org.osgi.service.component.ComponentContext; |
| 48 | import org.osgi.service.component.annotations.Activate; |
| 49 | import org.osgi.service.component.annotations.Component; |
| 50 | import org.osgi.service.component.annotations.Deactivate; |
| 51 | import org.osgi.service.component.annotations.Modified; |
| 52 | import org.osgi.service.component.annotations.Reference; |
| 53 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 54 | import org.slf4j.Logger; |
| 55 | |
| 56 | import java.util.ArrayList; |
| 57 | import java.util.Collection; |
| 58 | import java.util.Dictionary; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 59 | import java.util.List; |
| 60 | import java.util.Map; |
| 61 | import java.util.Optional; |
| 62 | import java.util.Properties; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 63 | import java.util.concurrent.CompletableFuture; |
| 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.concurrent.Executors; |
| 66 | import java.util.concurrent.atomic.AtomicInteger; |
| 67 | import java.util.concurrent.atomic.AtomicReference; |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 68 | import java.util.stream.Collectors; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 69 | |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 70 | import static java.util.stream.Collectors.collectingAndThen; |
| 71 | import static java.util.stream.Collectors.groupingBy; |
| 72 | import static java.util.stream.Collectors.mapping; |
| 73 | import static java.util.stream.Collectors.toSet; |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 74 | import static org.onlab.util.Tools.groupedThreads; |
| 75 | import static org.opencord.olt.impl.OsgiPropertyConstants.DELETE_METERS; |
| 76 | import static org.opencord.olt.impl.OsgiPropertyConstants.DELETE_METERS_DEFAULT; |
| 77 | import static org.slf4j.LoggerFactory.getLogger; |
| 78 | |
| 79 | /** |
| 80 | * Provisions Meters on access devices. |
| 81 | */ |
| 82 | @Component(immediate = true, property = { |
| 83 | DELETE_METERS + ":Boolean=" + DELETE_METERS_DEFAULT, |
| 84 | }) |
| 85 | public class OltMeterService implements AccessDeviceMeterService { |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 88 | protected MeterService meterService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 91 | protected CoreService coreService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 94 | protected ComponentConfigService componentConfigService; |
| 95 | |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 97 | protected StorageService storageService; |
| 98 | |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 99 | protected boolean deleteMeters = true; |
| 100 | |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 101 | ConsistentMultimap<String, MeterKey> bpInfoToMeter; |
| 102 | |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 103 | private ApplicationId appId; |
| 104 | private static final String APP_NAME = "org.opencord.olt"; |
| 105 | |
| 106 | private final MeterListener meterListener = new InternalMeterListener(); |
| 107 | |
| 108 | private final Logger log = getLogger(getClass()); |
| 109 | |
| 110 | protected ExecutorService eventExecutor; |
| 111 | |
| 112 | @Activate |
| 113 | public void activate(ComponentContext context) { |
| 114 | eventExecutor = Executors.newFixedThreadPool(5, groupedThreads("onos/olt", |
| 115 | "events-%d", log)); |
| 116 | appId = coreService.registerApplication(APP_NAME); |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 117 | modified(context); |
| 118 | |
| 119 | KryoNamespace serializer = KryoNamespace.newBuilder() |
| 120 | .register(KryoNamespaces.API) |
| 121 | .register(MeterKey.class) |
| 122 | .build(); |
| 123 | |
| 124 | bpInfoToMeter = storageService.<String, MeterKey>consistentMultimapBuilder() |
| 125 | .withName("volt-bp-info-to-meter") |
| 126 | .withSerializer(Serializer.using(serializer)) |
| 127 | .withApplicationId(appId) |
| 128 | .build(); |
| 129 | |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 130 | meterService.addListener(meterListener); |
| 131 | componentConfigService.registerProperties(getClass()); |
| 132 | log.info("Olt Meter service started"); |
| 133 | } |
| 134 | |
| 135 | @Deactivate |
| 136 | public void deactivate() { |
| 137 | meterService.removeListener(meterListener); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | @Modified |
| 142 | public void modified(ComponentContext context) { |
| 143 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 144 | |
| 145 | Boolean d = Tools.isPropertyEnabled(properties, "deleteMeters"); |
| 146 | if (d != null) { |
| 147 | deleteMeters = d; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public ImmutableMap<String, Collection<MeterKey>> getBpMeterMappings() { |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 153 | return bpInfoToMeter.stream() |
| 154 | .collect(collectingAndThen( |
| 155 | groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())), |
| 156 | ImmutableMap::copyOf)); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 159 | void addMeterIdToBpMapping(DeviceId deviceId, MeterId meterId, String bandwidthProfile) { |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 160 | bpInfoToMeter.put(bandwidthProfile, MeterKey.key(deviceId, meterId)); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public MeterId getMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfile) { |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 165 | if (bpInfoToMeter.get(bandwidthProfile).value().isEmpty()) { |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 166 | log.warn("Bandwidth Profile '{}' is not currently mapped to a meter", |
| 167 | bandwidthProfile); |
| 168 | return null; |
| 169 | } |
| 170 | |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 171 | Optional<? extends MeterKey> meterKeyForDevice = bpInfoToMeter.get(bandwidthProfile).value() |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 172 | .stream() |
| 173 | .filter(meterKey -> meterKey.deviceId().equals(deviceId)) |
| 174 | .findFirst(); |
| 175 | if (meterKeyForDevice.isPresent()) { |
| 176 | log.debug("Found meter {} for bandwidth profile {}", |
| 177 | meterKeyForDevice.get().meterId(), bandwidthProfile); |
| 178 | return meterKeyForDevice.get().meterId(); |
| 179 | } else { |
| 180 | log.warn("Bandwidth profile '{}' is not currently mapped to a meter", |
| 181 | bandwidthProfile); |
| 182 | return null; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public ImmutableSet<MeterKey> getProgMeters() { |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 188 | return bpInfoToMeter.stream() |
| 189 | .map(Map.Entry::getValue) |
| 190 | .collect(ImmutableSet.toImmutableSet()); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public MeterId createMeter(DeviceId deviceId, BandwidthProfileInformation bpInfo, |
| 195 | CompletableFuture<Object> meterFuture) { |
| 196 | if (bpInfo == null) { |
| 197 | log.warn("Requested bandwidth profile information is NULL"); |
| 198 | meterFuture.complete(ObjectiveError.BADPARAMS); |
| 199 | return null; |
| 200 | } |
| 201 | |
| 202 | MeterId meterId = getMeterIdFromBpMapping(deviceId, bpInfo.id()); |
| 203 | if (meterId != null) { |
| 204 | log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id()); |
| 205 | meterFuture.complete(null); |
| 206 | return meterId; |
| 207 | } |
| 208 | |
| 209 | List<Band> meterBands = createMeterBands(bpInfo); |
| 210 | |
| 211 | final AtomicReference<MeterId> meterIdRef = new AtomicReference<>(); |
| 212 | MeterRequest meterRequest = DefaultMeterRequest.builder() |
| 213 | .withBands(meterBands) |
| 214 | .withUnit(Meter.Unit.KB_PER_SEC) |
| 215 | .withContext(new MeterContext() { |
| 216 | @Override |
| 217 | public void onSuccess(MeterRequest op) { |
| 218 | meterFuture.complete(null); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void onError(MeterRequest op, MeterFailReason reason) { |
| 223 | bpInfoToMeter.remove(bpInfo.id(), |
| 224 | MeterKey.key(deviceId, meterIdRef.get())); |
| 225 | meterFuture.complete(reason); |
| 226 | } |
| 227 | }) |
| 228 | .forDevice(deviceId) |
| 229 | .fromApp(appId) |
| 230 | .burst() |
| 231 | .add(); |
| 232 | |
| 233 | Meter meter = meterService.submit(meterRequest); |
| 234 | meterIdRef.set(meter.id()); |
| 235 | addMeterIdToBpMapping(deviceId, meterIdRef.get(), bpInfo.id()); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 236 | log.info("Meter is created. Meter Id {}", meter.id()); |
| 237 | return meter.id(); |
| 238 | } |
| 239 | |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 240 | @Override |
| 241 | public void clearMeters(DeviceId deviceId) { |
| 242 | List<Map.Entry<String, MeterKey>> meters = bpInfoToMeter.stream() |
| 243 | .filter(e -> e.getValue().deviceId().equals(deviceId)) |
| 244 | .collect(Collectors.toList()); |
| 245 | |
| 246 | meters.forEach(e -> bpInfoToMeter.remove(e.getKey(), e.getValue())); |
Andrea Campanella | af39b4c | 2020-05-13 14:07:44 +0200 | [diff] [blame] | 247 | List<Meter> metersToRemove = ImmutableList.copyOf(meterService.getMeters(deviceId)); |
| 248 | metersToRemove.forEach(meter -> { |
| 249 | MeterRequest mq = DefaultMeterRequest.builder().fromApp(appId) |
| 250 | .forDevice(deviceId).withBands(meter.bands()) |
| 251 | .withUnit(meter.unit()).remove(); |
| 252 | meterService.withdraw(mq, meter.id()); |
| 253 | }); |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 256 | private List<Band> createMeterBands(BandwidthProfileInformation bpInfo) { |
| 257 | List<Band> meterBands = new ArrayList<>(); |
| 258 | |
| 259 | meterBands.add(createMeterBand(bpInfo.committedInformationRate(), bpInfo.committedBurstSize())); |
| 260 | meterBands.add(createMeterBand(bpInfo.exceededInformationRate(), bpInfo.exceededBurstSize())); |
| 261 | meterBands.add(createMeterBand(bpInfo.assuredInformationRate(), 0L)); |
| 262 | |
| 263 | return meterBands; |
| 264 | } |
| 265 | |
| 266 | private Band createMeterBand(long rate, Long burst) { |
| 267 | return DefaultBand.builder() |
| 268 | .withRate(rate) //already Kbps |
| 269 | .burstSize(burst) // already Kbits |
| 270 | .ofType(Band.Type.DROP) // no matter |
| 271 | .build(); |
| 272 | } |
| 273 | |
| 274 | private class InternalMeterListener implements MeterListener { |
| 275 | |
| 276 | Map<MeterKey, AtomicInteger> pendingRemoveMeters = Maps.newConcurrentMap(); |
| 277 | |
| 278 | @Override |
| 279 | public void event(MeterEvent meterEvent) { |
| 280 | eventExecutor.execute(() -> { |
| 281 | Meter meter = meterEvent.subject(); |
| 282 | if (meter == null) { |
| 283 | log.error("Meter in event {} is null", meterEvent); |
| 284 | return; |
| 285 | } |
| 286 | MeterKey key = MeterKey.key(meter.deviceId(), meter.id()); |
| 287 | if (deleteMeters && MeterEvent.Type.METER_REFERENCE_COUNT_ZERO.equals(meterEvent.type())) { |
| 288 | log.info("Zero Count Meter Event is received. Meter is {}", meter.id()); |
| 289 | incrementMeterCount(key); |
| 290 | |
| 291 | if (appId.equals(meter.appId()) && pendingRemoveMeters.get(key).get() == 3) { |
| 292 | log.info("Deleting unreferenced, no longer programmed Meter {}", meter.id()); |
| 293 | deleteMeter(meter.deviceId(), meter.id()); |
| 294 | } |
| 295 | } |
| 296 | if (MeterEvent.Type.METER_REMOVED.equals(meterEvent.type())) { |
| 297 | log.info("Meter Removed Event is received for {}", meter.id()); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 298 | pendingRemoveMeters.remove(key); |
| 299 | removeMeterFromBpMapping(key); |
| 300 | } |
| 301 | }); |
| 302 | } |
| 303 | |
| 304 | private void incrementMeterCount(MeterKey key) { |
| 305 | if (key == null) { |
| 306 | return; |
| 307 | } |
| 308 | pendingRemoveMeters.compute(key, |
| 309 | (k, v) -> { |
| 310 | if (v == null) { |
| 311 | return new AtomicInteger(1); |
| 312 | } |
| 313 | v.addAndGet(1); |
| 314 | return v; |
| 315 | }); |
| 316 | } |
| 317 | |
| 318 | private void deleteMeter(DeviceId deviceId, MeterId meterId) { |
| 319 | Meter meter = meterService.getMeter(deviceId, meterId); |
| 320 | if (meter != null) { |
| 321 | MeterRequest meterRequest = DefaultMeterRequest.builder() |
| 322 | .withBands(meter.bands()) |
| 323 | .withUnit(meter.unit()) |
| 324 | .forDevice(deviceId) |
| 325 | .fromApp(appId) |
| 326 | .burst() |
| 327 | .remove(); |
| 328 | |
| 329 | meterService.withdraw(meterRequest, meterId); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | private void removeMeterFromBpMapping(MeterKey meterKey) { |
Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 334 | List<Map.Entry<String, MeterKey>> meters = bpInfoToMeter.stream() |
| 335 | .filter(e -> e.getValue().equals(meterKey)) |
| 336 | .collect(Collectors.toList()); |
| 337 | |
| 338 | meters.forEach(e -> bpInfoToMeter.remove(e.getKey(), e.getValue())); |
Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |