diff --git a/corefx/src/main/java/com/core/data/NodeType.java b/corefx/src/main/java/com/core/data/NodeType.java index 4af7dc3d..3b070799 100644 --- a/corefx/src/main/java/com/core/data/NodeType.java +++ b/corefx/src/main/java/com/core/data/NodeType.java @@ -73,11 +73,9 @@ public class NodeType { return ID_LOOKUP.values().stream() .filter(nodeType -> { boolean sameType = nodeType.getValue() == type; - boolean sameModel; - if (model != null) { + boolean sameModel = true; + if (!model.isEmpty()) { sameModel = model.equals(nodeType.getModel()); - } else { - sameModel = nodeType.getModel() == null; } return sameType && sameModel; }) diff --git a/corefx/src/main/java/com/core/graph/CoreAddresses.java b/corefx/src/main/java/com/core/graph/CoreAddresses.java index fdd872c4..70ab5e06 100644 --- a/corefx/src/main/java/com/core/graph/CoreAddresses.java +++ b/corefx/src/main/java/com/core/graph/CoreAddresses.java @@ -6,7 +6,9 @@ import inet.ipaddr.IPAddressString; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.beans.IndexedPropertyDescriptor; import java.util.Comparator; +import java.util.HashSet; import java.util.Queue; import java.util.Set; import java.util.concurrent.ArrayBlockingQueue; @@ -16,8 +18,14 @@ 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().toPrefixBlock(); - private AtomicBoolean firstSubnet = new AtomicBoolean(true); private Queue deleted = new LinkedBlockingQueue<>(); + private Set usedSubnets = new HashSet<>(); + + public void usedAddress(IPAddress address) { + logger.info("adding used address: {} - {}", address, address.toPrefixBlock()); + usedSubnets.add(address.toPrefixBlock()); + logger.info("used subnets: {}", usedSubnets); + } public void reuseSubnet(IPAddress subnet) { deleted.add(subnet); @@ -25,16 +33,18 @@ public class CoreAddresses { public IPAddress nextSubnet() { logger.info("getting next subnet: {}", currentSubnet); - if (!firstSubnet.getAndSet(false)) { - IPAddress deletedSubnet = deleted.poll(); - if (deletedSubnet != null) { - currentSubnet = deletedSubnet; - } else { - currentSubnet = currentSubnet.incrementBoundary(1).toPrefixBlock(); - } + // skip existing subnets, when loaded from file + while (usedSubnets.contains(currentSubnet)) { + currentSubnet = currentSubnet.incrementBoundary(1).toPrefixBlock(); } - logger.info("getting updated boundary: {}", currentSubnet); - return currentSubnet; + + // re-use any deleted subnets + IPAddress next = deleted.poll(); + if (next == null) { + next = currentSubnet; + currentSubnet = currentSubnet.incrementBoundary(1).toPrefixBlock(); + } + return next; } public IPAddress findSubnet(Set interfaces) { @@ -58,7 +68,7 @@ public class CoreAddresses { public void reset() { deleted.clear(); - firstSubnet.set(true); + usedSubnets.clear(); currentSubnet = new IPAddressString("10.0.0.0/24").getAddress().toPrefixBlock(); } diff --git a/corefx/src/main/java/com/core/graph/NetworkGraph.java b/corefx/src/main/java/com/core/graph/NetworkGraph.java index 8dfe4e84..da3389ef 100644 --- a/corefx/src/main/java/com/core/graph/NetworkGraph.java +++ b/corefx/src/main/java/com/core/graph/NetworkGraph.java @@ -286,6 +286,13 @@ public class NetworkGraph { private void handleEdgeAdded(GraphEvent.Edge edgeEvent) { CoreLink link = edgeEvent.getEdge(); if (link.isLoaded()) { + // load addresses to avoid duplication + if (link.getInterfaceOne().getIp4() != null) { + coreAddresses.usedAddress(link.getInterfaceOne().getIp4()); + } + if (link.getInterfaceTwo().getIp4() != null) { + coreAddresses.usedAddress(link.getInterfaceTwo().getIp4()); + } return; } Pair endpoints = graph.getEndpoints(link); @@ -508,7 +515,7 @@ public class NetworkGraph { } private boolean isWirelessNode(CoreNode node) { - return node.getType() == NodeType.EMANE || node.getType() == NodeType.WLAN; + return node != null && (node.getType() == NodeType.EMANE || node.getType() == NodeType.WLAN); } private boolean checkForWirelessNode(CoreNode nodeOne, CoreNode nodeTwo) { diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index 72e390ff..b2c26f42 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -17,6 +17,7 @@ from core.emulator.emudata import NodeOptions, InterfaceData, LinkOptions from core.emulator.enumerations import NodeTypes, EventTypes, LinkTypes from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility from core.nodes import nodeutils +from core.nodes.base import CoreNetworkBase from core.nodes.ipaddress import MacAddress from core.services.coreservices import ServiceManager @@ -73,18 +74,24 @@ def convert_link(session, link_data): interface_one = None if link_data.interface1_id is not None: node = session.get_node(link_data.node1_id) - interface = node.netif(link_data.interface1_id) + interface_name = None + if not isinstance(node, CoreNetworkBase): + interface = node.netif(link_data.interface1_id) + interface_name = interface.name interface_one = core_pb2.Interface( - id=link_data.interface1_id, name=interface.name, mac=convert_value(link_data.interface1_mac), + id=link_data.interface1_id, name=interface_name, mac=convert_value(link_data.interface1_mac), ip4=convert_value(link_data.interface1_ip4), ip4mask=link_data.interface1_ip4_mask, ip6=convert_value(link_data.interface1_ip6), ip6mask=link_data.interface1_ip6_mask) interface_two = None if link_data.interface2_id is not None: node = session.get_node(link_data.node2_id) - interface = node.netif(link_data.interface2_id) + interface_name = None + if not isinstance(node, CoreNetworkBase): + interface = node.netif(link_data.interface2_id) + interface_name = interface.name interface_two = core_pb2.Interface( - id=link_data.interface2_id, name=interface.name, mac=convert_value(link_data.interface2_mac), + id=link_data.interface2_id, name=interface_name, mac=convert_value(link_data.interface2_mac), ip4=convert_value(link_data.interface2_ip4), ip4mask=link_data.interface2_ip4_mask, ip6=convert_value(link_data.interface2_ip6), ip6mask=link_data.interface2_ip6_mask)