16 package org.openkilda.pce.cache;
23 import com.google.common.base.MoreObjects;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import java.util.Collections;
29 import java.util.Map.Entry;
31 import java.util.concurrent.ConcurrentHashMap;
32 import java.util.stream.Collectors;
56 static final int MIN_VLAN_ID = 2;
61 static final int MAX_VLAN_ID = 4094;
66 static final int MIN_METER_ID = 11;
73 static final int MAX_METER_ID = 2500;
78 static final int MAX_COOKIE = 128 * 1024;
83 static final int MIN_COOKIE = 1;
88 private static final Logger logger = LoggerFactory.getLogger(
ResourceCache.class);
93 private final Map<SwitchId, ResourcePool> meterPool =
new ConcurrentHashMap<>();
115 cookiePool.dumpPool().forEach(cookiePool::deallocate);
116 vlanPool.
dumpPool().forEach(vlanPool::deallocate);
126 return cookiePool.allocate();
137 return cookiePool.allocate();
139 cookiePool.allocate(cookie);
151 return cookiePool.deallocate(cookie);
195 return meterPool.computeIfAbsent(switchId, k ->
new ResourcePool(MIN_METER_ID, MAX_METER_ID)).allocate();
207 return meterPool.computeIfAbsent(switchId, k ->
new ResourcePool(MIN_METER_ID, MAX_METER_ID))
210 meterPool.computeIfAbsent(switchId, k ->
new ResourcePool(MIN_METER_ID, MAX_METER_ID))
225 return switchMeterPool != null ? switchMeterPool.
deallocate(meterId) : null;
235 ResourcePool switchMeterPool = meterPool.remove(switchId);
236 return switchMeterPool != null ? switchMeterPool.
dumpPool() : null;
245 return cookiePool.dumpPool();
264 return meterPool.containsKey(switchId) ? meterPool.get(switchId).dumpPool() : Collections.emptySet();
273 return meterPool.entrySet().stream()
274 .collect(Collectors.toMap(
276 e -> e.getValue().dumpPool())
288 if (flow.
left != null) {
290 if (!flow.
left.isOneSwitchFlow()) {
299 if (flow.
right != null) {
300 if (!flow.
right.isOneSwitchFlow()) {
321 if (flow.
right != null) {
332 return MoreObjects.toStringHelper(
this)
333 .add(
"meters", meterPool)
334 .add(
"cookies", cookiePool)
335 .add(
"vlans", vlanPool)
synchronized Set< Integer > deallocateMeterId(SwitchId switchId)
synchronized Integer allocateMeterId(SwitchId switchId, Integer meterId)
void deallocateFlow(ImmutablePair< Flow, Flow > flow)
Map< SwitchId, Set< Integer > > getAllMeterIds()
Integer allocateCookie(Integer cookie)
Integer deallocateCookie(Integer cookie)
void allocateFlow(ImmutablePair< Flow, Flow > flow)
Integer deallocate(final Integer resourceId)
static final long FLOW_COOKIE_VALUE_MASK
static final long REVERSE_FLOW_COOKIE_MASK
synchronized Integer allocateMeterId(SwitchId switchId)
Set< Integer > getAllMeterIds(SwitchId switchId)
Set< Integer > getAllVlanIds()
static final long FORWARD_FLOW_COOKIE_MASK
synchronized Integer deallocateMeterId(SwitchId switchId, Integer meterId)
Set< Integer > dumpPool()
Integer allocateVlanId(Integer vlanId)
Set< Integer > getAllCookies()
Integer deallocateVlanId(Integer vlanId)