From 814d35d7dda1e2a774ab45663d4a503dec93d148 Mon Sep 17 00:00:00 2001 From: bharnden <32446120+bharnden@users.noreply.github.com> Date: Fri, 15 Nov 2019 12:39:08 -0800 Subject: [PATCH] removed coreclient edge and updated code to save and use link protobuf data structures --- coretk/coretk/canvasaction.py | 15 +--- coretk/coretk/coreclient.py | 119 +++++++++------------------- coretk/coretk/graph.py | 46 +++++------ coretk/coretk/interface.py | 50 ------------ coretk/coretk/linkinfo.py | 2 - coretk/coretk/toolbar.py | 1 + coretk/coretk/wirelessconnection.py | 2 +- 7 files changed, 63 insertions(+), 172 deletions(-) diff --git a/coretk/coretk/canvasaction.py b/coretk/coretk/canvasaction.py index 8f217833..5b9c08c3 100644 --- a/coretk/coretk/canvasaction.py +++ b/coretk/coretk/canvasaction.py @@ -6,13 +6,6 @@ from coretk.dialogs.emaneconfig import EmaneConfiguration from coretk.dialogs.nodeconfig import NodeConfigDialog from coretk.dialogs.wlanconfig import WlanConfigDialog -# TODO, finish classifying node types -NODE_TO_TYPE = { - "router": core_pb2.NodeType.DEFAULT, - "wlan": core_pb2.NodeType.WIRELESS_LAN, - "emane": core_pb2.NodeType.EMANE, -} - class CanvasAction: def __init__(self, master, canvas): @@ -21,13 +14,13 @@ class CanvasAction: self.node_to_show_config = None def display_configuration(self, canvas_node): - pb_type = NODE_TO_TYPE[canvas_node.node_type] + node_type = canvas_node.core_node.type self.node_to_show_config = canvas_node - if pb_type == core_pb2.NodeType.DEFAULT: + if node_type == core_pb2.NodeType.DEFAULT: self.display_node_configuration() - elif pb_type == core_pb2.NodeType.WIRELESS_LAN: + elif node_type == core_pb2.NodeType.WIRELESS_LAN: self.display_wlan_configuration(canvas_node) - elif pb_type == core_pb2.NodeType.EMANE: + elif node_type == core_pb2.NodeType.EMANE: self.display_emane_configuration() def display_node_configuration(self): diff --git a/coretk/coretk/coreclient.py b/coretk/coretk/coreclient.py index d80f6e5c..5dd0a621 100644 --- a/coretk/coretk/coreclient.py +++ b/coretk/coretk/coreclient.py @@ -8,7 +8,7 @@ from core.api.grpc import client, core_pb2 from coretk.dialogs.sessions import SessionsDialog from coretk.emaneodelnodeconfig import EmaneModelNodeConfig from coretk.images import NODE_WIDTH, Images -from coretk.interface import Interface, InterfaceManager +from coretk.interface import InterfaceManager from coretk.mobilitynodeconfig import MobilityNodeConfig from coretk.servicenodeconfig import ServiceNodeConfig from coretk.wlannodeconfig import WlanNodeConfig @@ -28,25 +28,6 @@ OBSERVERS = { } -class Edge: - def __init__(self, session_id, node_id_1, node_type_1, node_id_2, node_type_2): - """ - Create an instance of an edge - :param int session_id: session id - :param int node_id_1: node 1 id - :param int node_type_1: node 1 type - :param core_pb2.NodeType node_id_2: node 2 id - :param core_pb2.NodeType node_type_2: node 2 type - """ - self.session_id = session_id - self.id1 = node_id_1 - self.id2 = node_id_2 - self.type1 = node_type_1 - self.type2 = node_type_2 - self.interface_1 = None - self.interface_2 = None - - class CoreServer: def __init__(self, name, address, port): self.name = name @@ -92,7 +73,7 @@ class CoreClient: self.canvas_nodes = {} self.interface_to_edge = {} self.state = None - self.edges = {} + self.links = {} self.hooks = {} self.id = 1 self.reusable = [] @@ -157,7 +138,7 @@ class CoreClient: self.reusable.clear() self.preexisting.clear() self.canvas_nodes.clear() - self.edges.clear() + self.links.clear() self.hooks.clear() self.wlanconfig_management.configurations.clear() self.mobilityconfig_management.configurations.clear() @@ -212,7 +193,7 @@ class CoreClient: self.reusable.append(i) # draw session - self.app.canvas.canvas_reset_and_redraw(session) + self.app.canvas.reset_and_redraw(session) # draw tool bar appropritate with session state if self.is_runtime(): @@ -317,7 +298,7 @@ class CoreClient: def start_session(self): nodes = [x.core_node for x in self.canvas_nodes.values()] - links = self.get_links_proto() + links = list(self.links.values()) wlan_configs = self.get_wlan_configs_proto() mobility_configs = self.get_mobility_configs_proto() emane_model_configs = self.get_emane_model_configs_proto() @@ -389,7 +370,7 @@ class CoreClient: def create_nodes_and_links(self): node_protos = [x.core_node for x in self.canvas_nodes.values()] - link_protos = self.get_links_proto() + link_protos = list(self.links.values()) self.client.set_session_state(self.session_id, core_pb2.SessionState.DEFINITION) for node_proto in node_protos: response = self.client.add_node(self.session_id, node_proto) @@ -496,11 +477,15 @@ class CoreClient: node_interface_pairs = [] for i in edge_tokens: try: - e = self.edges.pop(i) - if e.interface_1 is not None: - node_interface_pairs.append(tuple([e.id1, e.interface_1.id])) - if e.interface_2 is not None: - node_interface_pairs.append(tuple([e.id2, e.interface_2.id])) + link = self.links.pop(i) + if link.interface_one is not None: + node_interface_pairs.append( + (link.node_one_id, link.interface_one.id) + ) + if link.interface_two is not None: + node_interface_pairs.append( + (link.node_two_id, link.interface_two.id) + ) except KeyError: logging.error("coreclient.py invalid edge token ") @@ -525,49 +510,31 @@ class CoreClient: if tuple([i, None]) in self.emaneconfig_management.configurations: self.emaneconfig_management.configurations.pop(tuple([i, None])) - def create_interface(self, node_type, gui_interface): - """ - create a protobuf interface given the interface object stored by the programmer - - :param core_bp2.NodeType node_type: node type - :param coretk.interface.Interface gui_interface: the programmer's interface - :rtype: core_bp2.Interface - :return: protobuf interface object - """ - if node_type != core_pb2.NodeType.DEFAULT: - return None - else: - interface = core_pb2.Interface( - id=gui_interface.id, - name=gui_interface.name, - mac=gui_interface.mac, - ip4=gui_interface.ipv4, - ip4mask=gui_interface.ip4prefix, - ) - logging.debug("create interface: %s", interface) - return interface - - def create_edge_interface(self, canvas_node): + def create_interface(self, canvas_node): interface = None core_node = canvas_node.core_node if self.is_model_node(core_node.model): ifid = len(canvas_node.interfaces) name = f"eth{ifid}" - interface = Interface( - name=name, ifid=ifid, ipv4=str(self.interfaces_manager.get_address()) + interface = core_pb2.Interface( + id=ifid, + name=name, + ip4=str(self.interfaces_manager.get_address()), + ip4mask=24, ) canvas_node.interfaces.append(interface) logging.debug( - "create node(%s) interface IP: %s, name: %s", + "create node(%s) interface IPv4: %s, name: %s", core_node.name, - interface.ipv4, + interface.ip4, interface.name, ) return interface - def create_edge(self, token, canvas_node_one, canvas_node_two): + def create_link(self, token, canvas_node_one, canvas_node_two): """ - Add an edge to grpc manager + Create core link for a pair of canvas nodes, with token referencing + the canvas edge. :param tuple(int, int) token: edge's identification in the canvas :param canvas_node_one: canvas node one @@ -580,14 +547,15 @@ class CoreClient: # create interfaces self.interfaces_manager.new_subnet() - interface_one = self.create_edge_interface(canvas_node_one) + interface_one = self.create_interface(canvas_node_one) if interface_one is not None: self.interface_to_edge[(node_one.id, interface_one.id)] = token - interface_two = self.create_edge_interface(canvas_node_two) + interface_two = self.create_interface(canvas_node_two) if interface_two is not None: self.interface_to_edge[(node_two.id, interface_two.id)] = token # emane setup + # TODO: determine if this is needed if ( node_one.type == core_pb2.NodeType.EMANE and node_two.type == core_pb2.NodeType.DEFAULT @@ -605,28 +573,15 @@ class CoreClient: node_two.node_id, node_one.node_id, interface_one.id ) - edge = Edge( - self.session_id, node_one.id, node_one.type, node_two.id, node_two.type + link = core_pb2.Link( + type=core_pb2.LinkType.WIRED, + node_one_id=node_one.id, + node_two_id=node_two.id, + interface_one=interface_one, + interface_two=interface_two, ) - edge.interface_1 = interface_one - edge.interface_2 = interface_two - self.edges[token] = edge - return edge - - def get_links_proto(self): - links = [] - for edge in self.edges.values(): - interface_one = self.create_interface(edge.type1, edge.interface_1) - interface_two = self.create_interface(edge.type2, edge.interface_2) - link = core_pb2.Link( - node_one_id=edge.id1, - node_two_id=edge.id2, - type=core_pb2.LinkType.WIRED, - interface_one=interface_one, - interface_two=interface_two, - ) - links.append(link) - return links + self.links[token] = link + return link def get_wlan_configs_proto(self): configs = [] diff --git a/coretk/coretk/graph.py b/coretk/coretk/graph.py index 1fcafac1..c1be859d 100644 --- a/coretk/coretk/graph.py +++ b/coretk/coretk/graph.py @@ -9,7 +9,6 @@ from coretk.canvasaction import CanvasAction from coretk.canvastooltip import CanvasTooltip from coretk.graph_helper import GraphHelper, WlanAntennaManager from coretk.images import Images -from coretk.interface import Interface from coretk.linkinfo import LinkInfo, Throughput from coretk.nodedelete import CanvasComponentManagement from coretk.wirelessconnection import WirelessConnection @@ -84,7 +83,7 @@ class CanvasGraph(tk.Canvas): self.node_context.add_command(label="Hide") self.node_context.add_command(label="Services") - def canvas_reset_and_redraw(self, session): + def reset_and_redraw(self, session): """ Reset the private variables CanvasGraph object, redraw nodes given the new grpc client. @@ -183,22 +182,23 @@ class CanvasGraph(tk.Canvas): canvas_node_one.edges.add(edge) canvas_node_two.edges.add(edge) self.edges[edge.token] = edge - self.core.create_edge(edge.token, canvas_node_one, canvas_node_two) + self.core.links[edge.token] = link self.helper.redraw_antenna(link, canvas_node_one, canvas_node_two) # TODO add back the link info to grpc manager also redraw - grpc_if1 = link.interface_one - grpc_if2 = link.interface_two + # TODO will include throughput and ipv6 in the future + interface_one = link.interface_one + interface_two = link.interface_two ip4_src = None ip4_dst = None ip6_src = None ip6_dst = None - if grpc_if1 is not None: - ip4_src = grpc_if1.ip4 - ip6_src = grpc_if1.ip6 - if grpc_if2 is not None: - ip4_dst = grpc_if2.ip4 - ip6_dst = grpc_if2.ip6 + if interface_one is not None: + ip4_src = interface_one.ip4 + ip6_src = interface_one.ip6 + if interface_two is not None: + ip4_dst = interface_two.ip4 + ip6_dst = interface_two.ip6 edge.link_info = LinkInfo( canvas=self, edge=edge, @@ -207,14 +207,8 @@ class CanvasGraph(tk.Canvas): ip4_dst=ip4_dst, ip6_dst=ip6_dst, ) - - # TODO will include throughput and ipv6 in the future - if1 = Interface(grpc_if1.name, grpc_if1.ip4, ifid=grpc_if1.id) - if2 = Interface(grpc_if2.name, grpc_if2.ip4, ifid=grpc_if2.id) - self.core.edges[edge.token].interface_1 = if1 - self.core.edges[edge.token].interface_2 = if2 - canvas_node_one.interfaces.append(if1) - canvas_node_two.interfaces.append(if2) + canvas_node_one.interfaces.append(interface_one) + canvas_node_two.interfaces.append(interface_two) # raise the nodes so they on top of the links self.tag_raise("node") @@ -316,17 +310,17 @@ class CanvasGraph(tk.Canvas): node_src.edges.add(edge) node_dst = self.nodes[edge.dst] node_dst.edges.add(edge) - core_edge = self.core.create_edge(edge.token, node_src, node_dst) + link = self.core.create_link(edge.token, node_src, node_dst) # draw link info on the edge - if1 = core_edge.interface_1 - if2 = core_edge.interface_2 ip4_and_prefix_1 = None ip4_and_prefix_2 = None - if if1 is not None: - ip4_and_prefix_1 = if1.ip4_and_prefix - if if2 is not None: - ip4_and_prefix_2 = if2.ip4_and_prefix + if link.HasField("interface_one"): + if1 = link.interface_one + ip4_and_prefix_1 = f"{if1.ip4}/{if1.ip4mask}" + if link.HasField("interface_two"): + if2 = link.interface_two + ip4_and_prefix_2 = f"{if2.ip4}/{if2.ip4mask}" edge.link_info = LinkInfo( self, edge, diff --git a/coretk/coretk/interface.py b/coretk/coretk/interface.py index 5df26e26..b0fcd346 100644 --- a/coretk/coretk/interface.py +++ b/coretk/coretk/interface.py @@ -1,35 +1,4 @@ import ipaddress -import random - - -class Interface: - def __init__(self, name, ipv4, ifid=None): - """ - Create an interface instance - - :param str name: interface name - :param str ip4: IPv4 - :param str mac: MAC address - :param int ifid: interface id - """ - self.name = name - self.ipv4 = ipv4 - self.ip4prefix = 24 - self.ip4_and_prefix = ipv4 + "/" + str(self.ip4prefix) - self.mac = self.random_mac_address() - self.id = ifid - - def random_mac_address(self): - """ - create a random MAC address for an interface - - :return: nothing - """ - return "02:00:00:%02x:%02x:%02x" % ( - random.randint(0, 255), - random.randint(0, 255), - random.randint(0, 225), - ) class SubnetAddresses: @@ -46,18 +15,13 @@ class SubnetAddresses: class InterfaceManager: def __init__(self): - # self.prefix = None self.core_subnets = list( ipaddress.ip_network("10.0.0.0/12").subnets(prefixlen_diff=12) ) self.subnet_index = 0 self.address_index = 0 - - # self.network = ipaddress.ip_network("10.0.0.0/24") - # self.addresses = list(self.network.hosts()) self.network = None self.addresses = None - # self.start_interface_manager() def start_interface_manager(self): self.subnet_index = 0 @@ -72,24 +36,10 @@ class InterfaceManager: :return: """ - # i = self.index - # self.address_index = self.index + 1 - # return self.addresses[i] ipaddr = self.addresses[self.address_index] self.address_index = self.address_index + 1 return ipaddr def new_subnet(self): self.network = self.core_subnets[self.subnet_index] - # self.subnet_index = self.subnet_index + 1 self.addresses = list(self.network.hosts()) - # self.address_index = 0 - - # def new_subnet(self): - # """ - # retrieve a new subnet - # :return: - # """ - # if self.prefix is None: - # self.prefix = - # self.addresses = list(ipaddress.ip_network("10.0.0.0/24").hosts()) diff --git a/coretk/coretk/linkinfo.py b/coretk/coretk/linkinfo.py index 29430deb..1e0363cf 100644 --- a/coretk/coretk/linkinfo.py +++ b/coretk/coretk/linkinfo.py @@ -4,8 +4,6 @@ Link information, such as IPv4, IPv6 and throughput drawn in the canvas import logging import math -WIRELESS_DEF = ["mdr", "wlan"] - class LinkInfo: def __init__(self, canvas, edge, ip4_src, ip6_src, ip4_dst, ip6_dst): diff --git a/coretk/coretk/toolbar.py b/coretk/coretk/toolbar.py index a049c321..6017cbb1 100644 --- a/coretk/coretk/toolbar.py +++ b/coretk/coretk/toolbar.py @@ -375,6 +375,7 @@ class Toolbar(ttk.Frame): """ logging.debug("Click on STOP button ") self.app.core.stop_session() + self.app.canvas.delete("wireless") self.design_frame.tkraise() def update_annotation(self, image): diff --git a/coretk/coretk/wirelessconnection.py b/coretk/coretk/wirelessconnection.py index b864ce38..e8d03126 100644 --- a/coretk/coretk/wirelessconnection.py +++ b/coretk/coretk/wirelessconnection.py @@ -19,7 +19,7 @@ class WirelessConnection: x1, y1 = self.canvas.coords(canvas_node_one.id) x2, y2 = self.canvas.coords(canvas_node_two.id) wlan_canvas_id = self.canvas.create_line( - x1, y1, x2, y2, fill="#009933", tags="wlan", width=1.5 + x1, y1, x2, y2, fill="#009933", tags="wireless", width=1.5 ) self.map[key] = wlan_canvas_id canvas_node_one.wlans.append(wlan_canvas_id)