corefx changes to support similarly bumping subnet linking to be the same as previous gui

This commit is contained in:
Blake J. Harnden 2019-06-14 17:59:14 -07:00
parent 778021c553
commit 9b485c8209
2 changed files with 48 additions and 90 deletions

View file

@ -8,80 +8,43 @@ import org.apache.logging.log4j.Logger;
import java.util.Comparator;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
public class CoreAddresses {
private static final Logger logger = LogManager.getLogger();
private IPAddress defaultSubnet = new IPAddressString("10.0.0.0/24").getAddress();
private IPAddress currentSubnet = new IPAddressString("10.0.0.0/24").getAddress();
private AtomicBoolean firstSubnet = new AtomicBoolean(true);
public IPAddress nextSubnet() {
logger.info("getting next subnet: {}", currentSubnet);
if (!firstSubnet.getAndSet(false)) {
currentSubnet = currentSubnet.incrementBoundary(1).toPrefixBlock();
}
logger.info("getting updated boundary: {}", currentSubnet);
return currentSubnet;
}
public IPAddress findSubnet(Set<CoreInterface> interfaces) {
IPAddress subnet;
logger.info("finding subnet from interfaces: {}", interfaces);
if (interfaces.isEmpty()) {
subnet = nextSubnet();
} else {
IPAddress maxAddress = getMaxAddress(interfaces);
subnet = maxAddress.toPrefixBlock();
}
return subnet;
}
private IPAddress getMaxAddress(Set<CoreInterface> interfaces) {
return interfaces.stream()
.map(CoreInterface::getIp4)
.max(Comparator.comparingInt(x -> x.toIPv4().intValue()))
.orElseGet(() -> defaultSubnet);
}
public IPAddress getSubnet(Set<CoreInterface> nodeOneInterfaces, Set<CoreInterface> nodeTwoInterfaces,
boolean nodeOneIsNetwork, boolean nodeTwoIsNetwork) {
IPAddress nodeOneMax = getMaxAddress(nodeOneInterfaces);
IPAddress nodeTwoMax = getMaxAddress(nodeTwoInterfaces);
logger.info("node one max: {}, node two max: {}", nodeOneMax, nodeTwoMax);
logger.info("max one compared to two: {} - {}",
nodeOneMax.toIPv4().intValue(), nodeTwoMax.toIPv4().intValue());
boolean shouldBump;
boolean isDefault;
IPAddress subnet;
if (nodeOneMax.toIPv4().intValue() > nodeTwoMax.toIPv4().intValue()) {
subnet = nodeOneMax;
isDefault = nodeOneMax == defaultSubnet;
shouldBump = !nodeOneIsNetwork && !isDefault;
} else {
subnet = nodeTwoMax;
isDefault = nodeTwoMax == defaultSubnet;
shouldBump = !nodeTwoIsNetwork && !isDefault;
}
logger.info("found max address: {} - {}", isDefault, subnet);
if (!isDefault) {
subnet = subnet.toPrefixBlock();
}
if (shouldBump) {
logger.info("incrementing subnet for host to host");
subnet = subnet.incrementBoundary(1);
}
logger.info("found subnet: {}", subnet);
return subnet;
.orElseGet(() -> currentSubnet);
}
public static void main(String... args) {
IPAddress addresses = new IPAddressString("10.0.100.1/24").getAddress();
IPAddress addresses3 = new IPAddressString("10.0.0.2/24").getAddress();
IPAddress addresses1 = new IPAddressString("10.0.1.0/24").getAddress();
IPAddress addresses2 = new IPAddressString("10.0.2.0/24").getAddress();
System.out.println(String.format("compare to greater: %s", addresses.compareTo(addresses3)));
System.out.println(String.format("compare to greater: %s", addresses3.compareTo(addresses1)));
System.out.println(String.format("compare to greater: %s", addresses.toInetAddress()));
IPAddress address = addresses.increment(1);
IPAddress address1 = addresses1.increment(1);
IPAddress address2 = addresses2.increment(1);
System.out.println(String.format("divisions: %s", address.toPrefixBlock()));
System.out.println(String.format("divisions: %s", address1.toPrefixBlock()));
System.out.println(String.format("divisions: %s", address2.toPrefixBlock()));
System.out.println(String.format("compares: %s", address1.compareTo(address2)));
System.out.println(String.format("compares: %s", address1.compareTo(address)));
System.out.println(String.format("compares: %s", address2.getSection(2, 3)));
System.out.println(String.format("compares: %s", address2.getSegment(2)));
System.out.println(String.format("address: %s", address2));
IPAddress prefixBlock = address1.toPrefixBlock();
System.out.println(String.format("prefix block: %s", prefixBlock));
IPAddress update = new IPAddressString("0.0.1.0").getAddress();
IPAddress nextAddress = prefixBlock.incrementBoundary(1);
// nextAddress.setPrefixLength(prefixBlock.getPrefixLength(), true);
System.out.println(String.format("prefix block update: %s", nextAddress));
IPAddress addresses = new IPAddressString("10.0.0.0/16").getAddress();
System.out.println(String.format("address: %s", addresses.increment(257)));
}
}

View file

@ -75,9 +75,8 @@ public class NetworkGraph {
graphViewer.setBackground(Color.WHITE);
graphViewer.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.S);
RenderContext<CoreNode, CoreLink> renderContext = graphViewer.getRenderContext();
// node render properties
RenderContext<CoreNode, CoreLink> renderContext = graphViewer.getRenderContext();
renderContext.setVertexLabelTransformer(CoreNode::getName);
renderContext.setVertexLabelRenderer(nodeLabelRenderer);
renderContext.setVertexShapeTransformer(node -> {
@ -288,44 +287,39 @@ public class NetworkGraph {
if (link.isLoaded()) {
return;
}
Pair<CoreNode> endpoints = graph.getEndpoints(link);
CoreNode nodeOne = endpoints.getFirst();
CoreNode nodeTwo = endpoints.getSecond();
boolean nodeOneIsDefault = isNode(nodeOne);
boolean nodeTwoIsDefault = isNode(nodeTwo);
// check if a node being linked is a network node
Set<CoreInterface> nodeOneInterfaces;
boolean nodeOneIsNetwork = false;
if (isNode(nodeOne)) {
nodeOneInterfaces = getNodeInterfaces(nodeOne);
// check what we are linking together
IPAddress subnet = null;
Set<CoreInterface> interfaces;
if (nodeOneIsDefault && nodeTwoIsDefault) {
subnet = coreAddresses.nextSubnet();
logger.info("linking node to node using subnet: {}", subnet);
} else if (nodeOneIsDefault) {
interfaces = getNetworkInterfaces(nodeTwo, new HashSet<>());
subnet = coreAddresses.findSubnet(interfaces);
logger.info("linking node one to network using subnet: {}", subnet);
} else if (nodeTwoIsDefault) {
interfaces = getNetworkInterfaces(nodeOne, new HashSet<>());
subnet = coreAddresses.findSubnet(interfaces);
logger.info("linking node two to network using subnet: {}", subnet);
} else {
nodeOneInterfaces = getNetworkInterfaces(nodeOne, new HashSet<>());
nodeOneIsNetwork = true;
logger.info("subnet not needed for linking networks together");
}
Set<CoreInterface> nodeTwoInterfaces;
boolean nodeTwoIsNetwork = false;
if (isNode(nodeTwo)) {
nodeTwoInterfaces = getNodeInterfaces(nodeTwo);
} else {
nodeTwoInterfaces = getNetworkInterfaces(nodeTwo, new HashSet<>());
nodeTwoIsNetwork = true;
}
// create interfaces for nodes
IPAddress subnet = coreAddresses.getSubnet(nodeOneInterfaces, nodeTwoInterfaces,
nodeOneIsNetwork, nodeTwoIsNetwork);
link.setNodeOne(nodeOne.getId());
if (isNode(nodeOne)) {
if (nodeOneIsDefault) {
int interfaceOneId = nextInterfaceId(nodeOne);
CoreInterface interfaceOne = createInterface(nodeOne, interfaceOneId, subnet);
link.setInterfaceOne(interfaceOne);
}
link.setNodeTwo(nodeTwo.getId());
if (isNode(nodeTwo)) {
if (nodeTwoIsDefault) {
int interfaceTwoId = nextInterfaceId(nodeTwo);
CoreInterface interfaceTwo = createInterface(nodeTwo, interfaceTwoId, subnet);
link.setInterfaceTwo(interfaceTwo);
@ -414,6 +408,7 @@ public class NetworkGraph {
coreInterface.setId(interfaceId);
coreInterface.setName(String.format("eth%s", interfaceId));
IPAddress address = subnet.increment(node.getId());
logger.info("creating interface for node({}): {}", node.getId(), address);
coreInterface.setIp4(address);
coreInterface.setIp6(address.toIPv6());
return coreInterface;