diff --git a/corefx/src/main/java/com/core/graph/CoreAddresses.java b/corefx/src/main/java/com/core/graph/CoreAddresses.java index dc1dcc5d..fdd872c4 100644 --- a/corefx/src/main/java/com/core/graph/CoreAddresses.java +++ b/corefx/src/main/java/com/core/graph/CoreAddresses.java @@ -7,18 +7,31 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.util.Comparator; +import java.util.Queue; import java.util.Set; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; public class CoreAddresses { private static final Logger logger = LogManager.getLogger(); - private IPAddress currentSubnet = new IPAddressString("10.0.0.0/24").getAddress(); + private IPAddress currentSubnet = new IPAddressString("10.0.0.0/24").getAddress().toPrefixBlock(); private AtomicBoolean firstSubnet = new AtomicBoolean(true); + private Queue deleted = new LinkedBlockingQueue<>(); + + public void reuseSubnet(IPAddress subnet) { + deleted.add(subnet); + } public IPAddress nextSubnet() { logger.info("getting next subnet: {}", currentSubnet); if (!firstSubnet.getAndSet(false)) { - currentSubnet = currentSubnet.incrementBoundary(1).toPrefixBlock(); + IPAddress deletedSubnet = deleted.poll(); + if (deletedSubnet != null) { + currentSubnet = deletedSubnet; + } else { + currentSubnet = currentSubnet.incrementBoundary(1).toPrefixBlock(); + } } logger.info("getting updated boundary: {}", currentSubnet); return currentSubnet; @@ -43,6 +56,12 @@ public class CoreAddresses { .orElseGet(() -> currentSubnet); } + public void reset() { + deleted.clear(); + firstSubnet.set(true); + currentSubnet = new IPAddressString("10.0.0.0/24").getAddress().toPrefixBlock(); + } + public static void main(String... args) { IPAddress addresses = new IPAddressString("10.0.0.0/16").getAddress(); System.out.println(String.format("address: %s", addresses.increment(257))); diff --git a/corefx/src/main/java/com/core/graph/NetworkGraph.java b/corefx/src/main/java/com/core/graph/NetworkGraph.java index 5e7f7259..8dfe4e84 100644 --- a/corefx/src/main/java/com/core/graph/NetworkGraph.java +++ b/corefx/src/main/java/com/core/graph/NetworkGraph.java @@ -248,6 +248,7 @@ public class NetworkGraph { } nodeMap.clear(); graphViewer.repaint(); + coreAddresses.reset(); } public void updatePositions() { @@ -417,6 +418,38 @@ public class NetworkGraph { private void handleEdgeRemoved(GraphEvent.Edge edgeEvent) { CoreLink link = edgeEvent.getEdge(); logger.info("removed edge: {}", link); + CoreNode nodeOne = getVertex(link.getNodeOne()); + CoreInterface interfaceOne = link.getInterfaceOne(); + CoreNode nodeTwo = getVertex(link.getNodeTwo()); + CoreInterface interfaceTwo = link.getInterfaceTwo(); + boolean nodeOneIsDefault = isNode(nodeOne); + boolean nodeTwoIsDefault = isNode(nodeTwo); + + // check what we are unlinking + Set interfaces; + IPAddress subnet = null; + if (nodeOneIsDefault && nodeTwoIsDefault) { + subnet = interfaceOne.getIp4().toPrefixBlock(); + logger.info("unlinking node to node reuse subnet: {}", subnet); + } else if (nodeOneIsDefault) { + interfaces = getNetworkInterfaces(nodeTwo, new HashSet<>()); + if (interfaces.isEmpty()) { + subnet = interfaceOne.getIp4().toPrefixBlock(); + logger.info("unlinking node one from network reuse subnet: {}", subnet); + } + } else if (nodeTwoIsDefault) { + interfaces = getNetworkInterfaces(nodeOne, new HashSet<>()); + if (interfaces.isEmpty()) { + subnet = interfaceTwo.getIp4().toPrefixBlock(); + logger.info("unlinking node two from network reuse subnet: {}", subnet); + } + } else { + logger.info("nothing to do when unlinking networks"); + } + + if (subnet != null) { + coreAddresses.reuseSubnet(subnet); + } } private void handleVertexAdded(GraphEvent.Vertex vertexEvent) {