16 package org.openkilda.pce.cache;
25 import com.google.common.annotations.VisibleForTesting;
26 import com.google.common.base.MoreObjects;
27 import com.google.common.graph.EndpointPair;
28 import com.google.common.graph.MutableNetwork;
29 import com.google.common.graph.NetworkBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.util.HashSet;
36 import java.util.concurrent.ConcurrentHashMap;
37 import java.util.stream.Collectors;
43 private static final Logger logger = LoggerFactory.getLogger(
NetworkCache.class);
48 private final MutableNetwork<SwitchInfoData, IslInfoData> network = NetworkBuilder
50 .allowsSelfLoops(
false)
51 .allowsParallelEdges(
true)
57 private final Map<SwitchId, SwitchInfoData> switchPool =
new ConcurrentHashMap<>();
62 private final Map<String, IslInfoData> islPool =
new ConcurrentHashMap<>();
70 public void load(Set<SwitchInfoData> switches, Set<IslInfoData> isls) {
71 logger.debug(
"Switches: {}", switches);
72 switches.forEach(this::createSwitch);
74 logger.debug(
"Isls: {}", isls);
75 isls.forEach(this::createIsl);
86 logger.debug(
"Get all isls by source switch {}", switchId);
90 return network.outEdges(startNode);
101 logger.debug(
"Get all isls by destination switch {}", switchId);
105 return network.inEdges(endNode);
116 logger.debug(
"Get all isls incident switch {}", switchId);
120 return network.incidentEdges(
node);
130 logger.debug(
"Get all switches in {} state", state);
132 return network.nodes().stream()
133 .filter(sw -> sw.getState() == state)
134 .collect(Collectors.toSet());
144 logger.debug(
"Get all switches connected to {} controller", controller);
146 return network.nodes().stream()
147 .filter(sw -> sw.getController().equals(controller))
148 .collect(Collectors.toSet());
159 logger.debug(
"Get all switches directly connected to {} switch ", switchId);
163 return network.adjacentNodes(
node);
170 islPool.values().forEach(network::removeEdge);
173 switchPool.values().forEach(network::removeNode);
185 logger.debug(
"Get {} switch", switchId);
190 String.format(
"SimpleSwitch %s not found", switchId));
204 SwitchId switchId = newSwitch.getSwitchId();
206 logger.debug(
"Create {} switch with {} parameters", switchId, newSwitch);
209 if (oldSwitch != null) {
211 String.format(
"SimpleSwitch %s already exists", switchId));
214 newSwitch.setCreatedInCacheNow();
216 network.addNode(newSwitch);
217 switchPool.put(switchId, newSwitch);
230 SwitchId switchId = newSwitch.getSwitchId();
231 logger.debug(
"Update {} switch with {} parameters", switchId, newSwitch);
234 if (oldSwitch == null) {
236 String.format(
"SimpleSwitch %s not found", switchId));
239 newSwitch.copyTimeTag(oldSwitch);
240 newSwitch.setUpdatedInCacheNow();
242 network.removeNode(oldSwitch);
243 network.addNode(newSwitch);
244 switchPool.put(switchId, newSwitch);
257 logger.debug(
"Create Or Update {} switch with {} parameters", newSwitch);
259 if (newSwitch == null) {
260 throw new IllegalArgumentException(
"SimpleSwitch can't be null in createOrUpdateSwitch");
278 logger.debug(
"Delete {} switch", switchId);
283 String.format(
"SimpleSwitch %s not found", switchId));
286 network.removeNode(
node);
297 logger.debug(
"Get all switches");
299 return new HashSet<>(network.nodes());
309 logger.debug(
"Is switch {} in cache", switchId);
311 return switchPool.containsKey(switchId);
322 SwitchState switchState = switchPool.get(switchId).getState();
338 logger.debug(
"Get {} isl", islId);
343 String.format(
"SimpleIsl %s not found", islId));
346 return islPool.get(islId);
357 String islId = isl.getId();
358 logger.debug(
"Create {} isl with {} parameters", islId, isl);
360 isl.setCreatedInCacheNow();
362 EndpointPair<SwitchInfoData>
nodes = getIslSwitches(isl);
363 network.addEdge(
nodes.source(),
nodes.target(), isl);
365 return islPool.put(islId, isl);
376 String islId = isl.getId();
377 logger.debug(
"Update {} isl with {} parameters", islId, isl);
380 network.removeEdge(oldIsl);
383 isl.setUpdatedInCacheNow();
385 EndpointPair<SwitchInfoData>
nodes = getIslSwitches(isl);
386 network.addEdge(
nodes.source(),
nodes.target(), isl);
388 return islPool.put(islId, isl);
399 logger.debug(
"Create or Update {} isl with {} parameters", isl);
402 throw new IllegalArgumentException(
"ISL can't be null in createOrUpdateIsl");
420 logger.debug(
"Delete {} isl", islId);
425 String.format(
"SimpleIsl %s not found", islId));
428 network.removeEdge(isl);
439 logger.debug(
"Get all isls");
441 return new HashSet<>(network.edges());
451 logger.debug(
"Is isl {} in cache", islId);
453 return islPool.containsKey(islId);
462 MutableNetwork<SwitchInfoData, IslInfoData> getNetwork() {
473 private EndpointPair<SwitchInfoData> getIslSwitches(
IslInfoData isl)
throws CacheException {
474 SwitchId srcSwitch = isl.getPath().get(0).getSwitchId();
475 if (srcSwitch == null) {
476 throw new CacheException(ErrorType.PARAMETERS_INVALID,
"Can not get isl nodes",
477 "Source switch not specified");
480 SwitchInfoData startNode =
getSwitch(srcSwitch);
482 SwitchId dstSwitch = isl.getPath().get(1).getSwitchId();
483 if (dstSwitch == null) {
484 throw new CacheException(ErrorType.PARAMETERS_INVALID,
"Can not get isl nodes",
485 "Destination switch not specified");
488 SwitchInfoData endNode =
getSwitch(dstSwitch);
490 return EndpointPair.ordered(startNode, endNode);
495 return MoreObjects.toStringHelper(
this)
496 .add(
"switches", switchPool)
497 .add(
"isls", islPool)
IslInfoData deleteIsl(String islId)
IslInfoData updateIsl(IslInfoData isl)
Set< SwitchInfoData > getControllerSwitches(String controller)
Set< SwitchInfoData > getStateSwitches(SwitchState state)
boolean cacheContainsIsl(String islId)
SwitchInfoData updateSwitch(SwitchInfoData newSwitch)
Set< IslInfoData > getIslsBySource(SwitchId switchId)
IslInfoData createOrUpdateIsl(IslInfoData isl)
IslInfoData createIsl(IslInfoData isl)
void copyTimeTag(CacheTimeTag src)
Set< SwitchInfoData > getDirectlyConnectedSwitches(SwitchId switchId)
Set< IslInfoData > getIslsByDestination(SwitchId switchId)
SwitchInfoData deleteSwitch(SwitchId switchId)
SwitchInfoData createSwitch(SwitchInfoData newSwitch)
Set< IslInfoData > getIslsBySwitch(SwitchId switchId)
boolean switchIsOperable(SwitchId switchId)
Set< SwitchInfoData > dumpSwitches()
void load(Set< SwitchInfoData > switches, Set< IslInfoData > isls)
IslInfoData getIsl(String islId)
SwitchInfoData getSwitch(SwitchId switchId)
SwitchInfoData createOrUpdateSwitch(SwitchInfoData newSwitch)
boolean cacheContainsSwitch(SwitchId switchId)
Set< IslInfoData > dumpIsls()