16 package org.openkilda.floodlight.switchmanager;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import java.util.HashSet;
27 import java.util.concurrent.ConcurrentHashMap;
33 private static final Logger logger = LoggerFactory.getLogger(
MeterPool.class);
36 private static final Integer MIN_METER_ID = 200;
37 private static final Integer MAX_METER_ID = 4095;
38 private final Map<SwitchId, ResourcePool> switchMeterPool =
new ConcurrentHashMap<>();
39 private final Map<String, Set<Integer>> flowMeterPool =
new ConcurrentHashMap<>();
42 return flowMeterPool.get(flowId);
47 return pool == null ? null : pool.
dumpPool();
58 public synchronized Integer
allocate(
final SwitchId switchId,
final String flowId, Integer meterId) {
60 Set<Integer> flowPool = getFlowPool(flowId);
62 Integer allocatedMeterId = switchPool.
allocate(meterId);
63 if (allocatedMeterId == null) {
64 logger.warn(
"Meter pool already have record for meter id {}", meterId);
65 allocatedMeterId = meterId;
67 flowPool.add(allocatedMeterId);
69 return allocatedMeterId;
77 Set<Integer> flowPool = getFlowPool(flowId);
79 Integer meterId = switchPool.
allocate();
80 flowPool.add(meterId);
90 if (switchPool == null) {
91 logger.error(
"Could not deallocate meter: no such switch {}", switchId);
95 Set<Integer> flowPool = flowMeterPool.remove(flowId);
96 if (flowPool == null) {
97 logger.error(
"Could not deallocate meter: no such flow id={}", flowId);
101 Integer meterId = null;
103 for (Integer meter : flowPool) {
113 ResourcePool switchPool = switchMeterPool.get(switchId);
114 if (switchPool == null) {
115 switchPool =
new ResourcePool(MIN_METER_ID, MAX_METER_ID);
116 switchMeterPool.put(switchId, switchPool);
122 private Set<Integer> getFlowPool(
final String flowId) {
123 return flowMeterPool.computeIfAbsent(flowId, k ->
new HashSet<>());
synchronized Set< Integer > getMetersBySwitch(final SwitchId switchId)
synchronized Integer deallocate(final SwitchId switchId, final String flowId)
synchronized Integer allocate(final SwitchId switchId, final String flowId)
Integer deallocate(final Integer resourceId)
synchronized Integer allocate(final SwitchId switchId, final String flowId, Integer meterId)
Set< Integer > dumpPool()
synchronized Set< Integer > getMetersByFlow(final String flowId)