merged grpcmanager with coregrpc

This commit is contained in:
bharnden 2019-11-01 13:15:45 -07:00
parent 5369694797
commit 1f230146a6
7 changed files with 368 additions and 486 deletions

View file

@ -6,8 +6,54 @@ import os
from collections import OrderedDict
from core.api.grpc import client, core_pb2
from coretk.coretocanvas import CoreToCanvasMapping
from coretk.dialogs.sessions import SessionsDialog
from coretk.grpcmanagement import GrpcManager
from coretk.interface import Interface, InterfaceManager
from coretk.wlannodeconfig import WlanNodeConfig
link_layer_nodes = ["switch", "hub", "wlan", "rj45", "tunnel"]
network_layer_nodes = ["router", "host", "PC", "mdr", "prouter", "OVS"]
class Node:
def __init__(self, session_id, node_id, node_type, model, x, y, name):
"""
Create an instance of a node
:param int session_id: session id
:param int node_id: node id
:param core_pb2.NodeType node_type: node type
:param int x: x coordinate
:param int y: coordinate
:param str name: node name
"""
self.session_id = session_id
self.node_id = node_id
self.type = node_type
self.x = x
self.y = y
self.model = model
self.name = name
self.interfaces = []
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 CoreGrpc:
@ -21,7 +67,16 @@ class CoreGrpc:
self.app = app
self.master = app.master
self.interface_helper = None
self.manager = GrpcManager(self)
# data for managing the current session
self.nodes = {}
self.edges = {}
self.id = 1
self.reusable = []
self.preexisting = set()
self.interfaces_manager = InterfaceManager()
self.core_mapping = CoreToCanvasMapping()
self.wlanconfig_management = WlanNodeConfig()
def handle_events(self, event):
logging.info("event: %s", event)
@ -37,7 +92,6 @@ class CoreGrpc:
for if_tp in interface_throughputs:
if if_tp.node_id in self.node_ids:
throughputs_belong_to_session.append(if_tp)
# bridge_throughputs = event.bridge_throughputs
self.throughput_draw.process_grpc_throughput_event(
throughputs_belong_to_session
)
@ -47,21 +101,24 @@ class CoreGrpc:
self.session_id = session_id
response = self.core.get_session(self.session_id)
logging.info("joining session(%s): %s", self.session_id, response)
self.core.events(self.session_id, self.app.core_grpc.handle_events)
self.core.events(self.session_id, self.handle_events)
# set title to session
self.master.title(f"CORE Session({self.session_id})")
# determine next node id and reusable nodes
session = response.session
self.manager.reusable.clear()
self.manager.preexisting.clear()
self.reusable.clear()
self.preexisting.clear()
max_id = 1
for node in session.nodes:
if node.id > max_id:
max_id = node.id
self.manager.preexisting.add(node.id)
self.manager.id = max_id
for i in range(1, self.manager.id):
if i not in self.manager.preexisting:
self.manager.reusable.append(i)
self.preexisting.add(node.id)
self.id = max_id
for i in range(1, self.id):
if i not in self.preexisting:
self.reusable.append(i)
# draw session
self.app.canvas.canvas_reset_and_redraw(session)
@ -74,12 +131,7 @@ class CoreGrpc:
"""
response = self.core.create_session()
logging.info("created session: %s", response)
# handle events session may broadcast
self.session_id = response.session_id
self.master.title("CORE Session ID " + str(self.session_id))
self.core.events(self.session_id, self.handle_events)
# self.core.throughputs(self.log_throughput)
self.join_session(response.session_id)
def delete_session(self, custom_sid=None):
if custom_sid is None:
@ -164,28 +216,10 @@ class CoreGrpc:
logging.info("set session state: %s", response)
def add_node(self, node_type, model, x, y, name, node_id):
position = core_pb2.Position(x=x, y=y)
node = core_pb2.Node(id=node_id, type=node_type, position=position, model=model)
self.node_ids.append(node_id)
response = self.core.add_node(self.session_id, node)
logging.info("created node: %s", response)
if node_type == core_pb2.NodeType.WIRELESS_LAN:
d = OrderedDict()
d["basic_range"] = "275"
d["bandwidth"] = "54000000"
d["jitter"] = "0"
d["delay"] = "20000"
d["error"] = "0"
r = self.core.set_wlan_config(self.session_id, node_id, d)
logging.debug("set wlan config %s", r)
return response.node_id
def edit_node(self, node_id, x, y):
position = core_pb2.Position(x=x, y=y)
response = self.core.edit_node(self.session_id, node_id, position)
logging.info("updated node id %s: %s", node_id, response)
# self.core.events(self.session_id, self.log_event)
def delete_nodes(self, delete_session=None):
if delete_session is None:
@ -213,29 +247,6 @@ class CoreGrpc:
)
logging.info("delete links %s", response)
def create_interface(self, node_type, gui_interface):
"""
create a protobuf interface given the interface object stored by the programmer
:param core_bp2.NodeType type: node type
:param coretk.interface.Interface gui_interface: the programmer's interface object
: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 1 %s", interface)
return interface
# TODO add location, hooks, emane_config, etc...
def start_session(
self,
@ -274,29 +285,6 @@ class CoreGrpc:
"""
if1 = self.create_interface(type1, edge.interface_1)
if2 = self.create_interface(type2, edge.interface_2)
# if type1 == core_pb2.NodeType.DEFAULT:
# interface = edge.interface_1
# if1 = core_pb2.Interface(
# id=interface.id,
# name=interface.name,
# mac=interface.mac,
# ip4=interface.ipv4,
# ip4mask=interface.ip4prefix,
# )
# logging.debug("create interface 1 %s", if1)
# # interface1 = self.interface_helper.create_interface(id1, 0)
#
# if type2 == core_pb2.NodeType.DEFAULT:
# interface = edge.interface_2
# if2 = core_pb2.Interface(
# id=interface.id,
# name=interface.name,
# mac=interface.mac,
# ip4=interface.ipv4,
# ip4mask=interface.ip4prefix,
# )
# logging.debug("create interface 2: %s", if2)
response = self.core.add_link(self.session_id, id1, id2, if1, if2)
logging.info("created link: %s", response)
@ -335,3 +323,279 @@ class CoreGrpc:
"""
logging.debug("Close grpc")
self.core.close()
def peek_id(self):
"""
Peek the next id to be used
:return: nothing
"""
if len(self.reusable) == 0:
return self.id
else:
return self.reusable[0]
def get_id(self):
"""
Get the next node id as well as update id status and reusable ids
:rtype: int
:return: the next id to be used
"""
if len(self.reusable) == 0:
new_id = self.id
self.id = self.id + 1
return new_id
else:
return self.reusable.pop(0)
def add_node(self, node_type, model, x, y, name, node_id):
position = core_pb2.Position(x=x, y=y)
node = core_pb2.Node(id=node_id, type=node_type, position=position, model=model)
self.node_ids.append(node_id)
response = self.core.add_node(self.session_id, node)
logging.info("created node: %s", response)
if node_type == core_pb2.NodeType.WIRELESS_LAN:
d = OrderedDict()
d["basic_range"] = "275"
d["bandwidth"] = "54000000"
d["jitter"] = "0"
d["delay"] = "20000"
d["error"] = "0"
r = self.core.set_wlan_config(self.session_id, node_id, d)
logging.debug("set wlan config %s", r)
return response.node_id
def add_graph_node(self, session_id, canvas_id, x, y, name):
"""
Add node, with information filled in, to grpc manager
:param int session_id: session id
:param int canvas_id: node's canvas id
:param int x: x coord
:param int y: y coord
:param str name: node type
:return: nothing
"""
node_type = None
node_model = None
if name in link_layer_nodes:
if name == "switch":
node_type = core_pb2.NodeType.SWITCH
elif name == "hub":
node_type = core_pb2.NodeType.HUB
elif name == "wlan":
node_type = core_pb2.NodeType.WIRELESS_LAN
elif name == "rj45":
node_type = core_pb2.NodeType.RJ45
elif name == "tunnel":
node_type = core_pb2.NodeType.TUNNEL
elif name in network_layer_nodes:
node_type = core_pb2.NodeType.DEFAULT
node_model = name
else:
logging.error("grpcmanagemeny.py INVALID node name")
nid = self.get_id()
create_node = Node(session_id, nid, node_type, node_model, x, y, name)
# set default configuration for wireless node
self.wlanconfig_management.set_default_config(node_type, nid)
self.nodes[canvas_id] = create_node
self.core_mapping.map_core_id_to_canvas_id(nid, canvas_id)
# self.core_id_to_canvas_id[nid] = canvas_id
logging.debug(
"Adding node to GrpcManager.. Session id: %s, Coords: (%s, %s), Name: %s",
session_id,
x,
y,
name,
)
def add_preexisting_node(self, canvas_node, session_id, core_node, name):
"""
Add preexisting nodes to grpc manager
:param str name: node_type
:param core_pb2.Node core_node: core node grpc message
:param coretk.graph.CanvasNode canvas_node: canvas node
:param int session_id: session id
:return: nothing
"""
# update the next available id
core_id = core_node.id
if self.id is None or core_id >= self.id:
self.id = core_id + 1
self.preexisting.add(core_id)
n = Node(
session_id,
core_id,
core_node.type,
core_node.model,
canvas_node.x_coord,
canvas_node.y_coord,
name,
)
self.nodes[canvas_node.id] = n
def update_node_location(self, canvas_id, new_x, new_y):
"""
update node
:param int canvas_id: canvas id of that node
:param int new_x: new x coord
:param int new_y: new y coord
:return: nothing
"""
self.nodes[canvas_id].x = new_x
self.nodes[canvas_id].y = new_y
def update_reusable_id(self):
"""
Update available id for reuse
:return: nothing
"""
if len(self.preexisting) > 0:
for i in range(1, self.id):
if i not in self.preexisting:
self.reusable.append(i)
self.preexisting.clear()
logging.debug("Next id: %s, Reusable: %s", self.id, self.reusable)
def delete_node(self, canvas_id):
"""
Delete a node from the session
:param int canvas_id: node's id in the canvas
:return: thing
"""
try:
self.nodes.pop(canvas_id)
self.reusable.append(canvas_id)
self.reusable.sort()
except KeyError:
logging.error("grpcmanagement.py INVALID NODE CANVAS ID")
def create_interface(self, node_type, gui_interface):
"""
create a protobuf interface given the interface object stored by the programmer
:param core_bp2.NodeType type: node type
:param coretk.interface.Interface gui_interface: the programmer's interface object
: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, edge, src_canvas_id, dst_canvas_id):
"""
Create the interface for the two end of an edge, add a copy to node's interfaces
:param coretk.grpcmanagement.Edge edge: edge to add interfaces to
:param int src_canvas_id: canvas id for the source node
:param int dst_canvas_id: canvas id for the destination node
:return: nothing
"""
src_interface = None
dst_interface = None
print("create interface")
self.interfaces_manager.new_subnet()
src_node = self.nodes[src_canvas_id]
if src_node.model in network_layer_nodes:
ifid = len(src_node.interfaces)
name = "eth" + str(ifid)
src_interface = Interface(
name=name, ifid=ifid, ipv4=str(self.interfaces_manager.get_address())
)
self.nodes[src_canvas_id].interfaces.append(src_interface)
logging.debug(
"Create source interface 1... IP: %s, name: %s",
src_interface.ipv4,
src_interface.name,
)
dst_node = self.nodes[dst_canvas_id]
if dst_node.model in network_layer_nodes:
ifid = len(dst_node.interfaces)
name = "eth" + str(ifid)
dst_interface = Interface(
name=name, ifid=ifid, ipv4=str(self.interfaces_manager.get_address())
)
self.nodes[dst_canvas_id].interfaces.append(dst_interface)
logging.debug(
"Create destination interface... IP: %s, name: %s",
dst_interface.ipv4,
dst_interface.name,
)
edge.interface_1 = src_interface
edge.interface_2 = dst_interface
return src_interface, dst_interface
def add_edge(self, session_id, token, canvas_id_1, canvas_id_2):
"""
Add an edge to grpc manager
:param int session_id: core session id
:param tuple(int, int) token: edge's identification in the canvas
:param int canvas_id_1: canvas id of source node
:param int canvas_id_2: canvas_id of destination node
:return: nothing
"""
if canvas_id_1 in self.nodes and canvas_id_2 in self.nodes:
edge = Edge(
session_id,
self.nodes[canvas_id_1].node_id,
self.nodes[canvas_id_1].type,
self.nodes[canvas_id_2].node_id,
self.nodes[canvas_id_2].type,
)
self.edges[token] = edge
src_interface, dst_interface = self.create_edge_interface(
edge, canvas_id_1, canvas_id_2
)
node_one_id = self.nodes[canvas_id_1].node_id
node_two_id = self.nodes[canvas_id_2].node_id
# provide a way to get an edge from a core node and an interface id
if src_interface is not None:
self.core_mapping.map_node_and_interface_to_canvas_edge(
node_one_id, src_interface.id, token
)
logging.debug(
"map node id %s, interface_id %s to edge token %s",
node_one_id,
src_interface.id,
token,
)
if dst_interface is not None:
self.core_mapping.map_node_and_interface_to_canvas_edge(
node_two_id, dst_interface.id, token
)
logging.debug(
"map node id %s, interface_id %s to edge token %s",
node_two_id,
dst_interface.id,
token,
)
logging.debug("Adding edge to grpc manager...")
else:
logging.error("grpcmanagement.py INVALID CANVAS NODE ID")

View file

@ -8,7 +8,6 @@ class CoreToCanvasMapping:
def __init__(self):
self.core_id_to_canvas_id = {}
self.core_node_and_interface_to_canvas_edge = {}
# self.edge_id_to_canvas_token = {}
def map_node_and_interface_to_canvas_edge(self, nid, iid, edge_token):
self.core_node_and_interface_to_canvas_edge[tuple([nid, iid])] = edge_token
@ -33,13 +32,3 @@ class CoreToCanvasMapping:
else:
logging.debug("invalid key")
return None
# def add_mapping(self, core_id, canvas_id):
# if core_id not in self.core_id_to_canvas_id:
# self.core_id_to_canvas_id[core_id] = canvas_id
# else:
# logging.error("key already mapped")
#
# def delete_mapping(self, core_id):
# result = self.core_id_to_canvas_id.pop(core_id, None)
# return result

View file

@ -15,8 +15,8 @@ class CoreToolbarHelp:
:return: nothing
"""
nodes = []
manager = self.app.core_grpc.manager
for node in manager.nodes.values():
core = self.app.core_grpc
for node in core.nodes.values():
pos = core_pb2.Position(x=int(node.x), y=int(node.y))
n = core_pb2.Node(
id=node.node_id, type=node.type, position=pos, model=node.model
@ -32,8 +32,8 @@ class CoreToolbarHelp:
:return: list of protobuf links
"""
links = []
manager = self.app.core_grpc.manager
for edge in manager.edges.values():
core = self.app.core_grpc
for edge in core.edges.values():
interface_one = self.app.core_grpc.create_interface(
edge.type1, edge.interface_1
)
@ -56,61 +56,20 @@ class CoreToolbarHelp:
interface_two=interface_two,
)
links.append(link)
# self.id1 = edge.id1
# self.id2 = edge.id2
# self.type = link_type
# self.if1 = interface_one
# self.if2 = interface_two
return links
def get_wlan_configuration_list(self):
configs = []
manager = self.app.core_grpc.manager
manager_configs = manager.wlanconfig_management.configurations
core = self.app.core_grpc
manager_configs = core.wlanconfig_management.configurations
for key in manager_configs:
cnf = core_pb2.WlanConfig(node_id=key, config=manager_configs[key])
configs.append(cnf)
return configs
def gui_start_session(self):
# list(core_pb2.Node)
nodes = self.get_node_list()
# list(core_bp2.Link)
links = self.get_link_list()
# print(links[0])
wlan_configs = self.get_wlan_configuration_list()
# print(wlan_configs)
self.app.core_grpc.start_session(nodes, links, wlan_configs=wlan_configs)
# self.core_grpc.core.add_link(self.core_grpc.session_id, self.id1, self.id2, self.if1, self.if2)
# res = self.core_grpc.core.get_wlan_config(self.core_grpc.session_id, 1)
# res = self.core_grpc.core.get_session(self.core_grpc.session_id).session
# print(res)
# res = self.core_grpc.core.get_wlan_config(self.core_grpc.session_id, 1)
# print(res)
# def add_nodes(self):
# """
# add the nodes stored in grpc manager
# :return: nothing
# """
# grpc_manager = self.application.canvas.grpc_manager
# for node in grpc_manager.nodes.values():
# self.application.core_grpc.add_node(
# node.type, node.model, int(node.x), int(node.y), node.name, node.node_id
# )
#
# def add_edges(self):
# """
# add the edges stored in grpc manager
# :return:
# """
# grpc_manager = self.application.canvas.grpc_manager
# for edge in grpc_manager.edges.values():
# self.application.core_grpc.add_link(
# edge.id1, edge.id2, edge.type1, edge.type2, edge
# )

View file

@ -147,7 +147,7 @@ class CanvasGraph(tk.Canvas):
core_id_to_canvas_id[node.id] = n.id
# store the node in grpc manager
self.core_grpc.manager.add_preexisting_node(n, session.id, node, name)
self.core_grpc.add_preexisting_node(n, session.id, node, name)
# draw existing links
for link in session.links:
@ -179,7 +179,7 @@ class CanvasGraph(tk.Canvas):
n1.edges.add(e)
n2.edges.add(e)
self.edges[e.token] = e
self.core_grpc.manager.add_edge(session.id, e.token, n1.id, n2.id)
self.core_grpc.add_edge(session.id, e.token, n1.id, n2.id)
self.helper.redraw_antenna(link, n1, n2)
@ -208,12 +208,12 @@ class CanvasGraph(tk.Canvas):
# 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_grpc.manager.edges[e.token].interface_1 = if1
self.core_grpc.manager.edges[e.token].interface_2 = if2
self.core_grpc.manager.nodes[
self.core_grpc.edges[e.token].interface_1 = if1
self.core_grpc.edges[e.token].interface_2 = if2
self.core_grpc.nodes[
core_id_to_canvas_id[link.node_one_id]
].interfaces.append(if1)
self.core_grpc.manager.nodes[
self.core_grpc.nodes[
core_id_to_canvas_id[link.node_two_id]
].interfaces.append(if2)
@ -318,13 +318,13 @@ class CanvasGraph(tk.Canvas):
node_dst = self.nodes[edge.dst]
node_dst.edges.add(edge)
self.core_grpc.manager.add_edge(
self.core_grpc.add_edge(
self.core_grpc.session_id, edge.token, node_src.id, node_dst.id
)
# draw link info on the edge
if1 = self.core_grpc.manager.edges[edge.token].interface_1
if2 = self.core_grpc.manager.edges[edge.token].interface_2
if1 = self.core_grpc.edges[edge.token].interface_1
if2 = self.core_grpc.edges[edge.token].interface_2
ip4_and_prefix_1 = None
ip4_and_prefix_2 = None
if if1 is not None:
@ -390,10 +390,10 @@ class CanvasGraph(tk.Canvas):
image=image,
node_type=node_name,
canvas=self,
core_id=self.core_grpc.manager.peek_id(),
core_id=self.core_grpc.peek_id(),
)
self.nodes[node.id] = node
self.core_grpc.manager.add_node(
self.core_grpc.add_graph_node(
self.core_grpc.session_id, node.id, x, y, node_name
)
return node
@ -485,7 +485,7 @@ class CanvasNode:
self.moving = None
def double_click(self, event):
node_id = self.canvas.core_grpc.manager.nodes[self.id].node_id
node_id = self.canvas.core_grpc.nodes[self.id].node_id
state = self.canvas.core_grpc.get_session_state()
if state == core_pb2.SessionState.RUNTIME:
self.canvas.core_grpc.launch_terminal(node_id)
@ -511,9 +511,7 @@ class CanvasNode:
def click_release(self, event):
logging.debug(f"click release {self.name}: {event}")
self.update_coords()
self.canvas.core_grpc.manager.update_node_location(
self.id, self.x_coord, self.y_coord
)
self.canvas.core_grpc.update_node_location(self.id, self.x_coord, self.y_coord)
self.moving = None
def motion(self, event):

View file

@ -1,328 +0,0 @@
"""
Manage useful informations about the nodes, edges and configuration
that can be useful for grpc, acts like a session class
"""
import logging
from core.api.grpc import core_pb2
from coretk.coretocanvas import CoreToCanvasMapping
from coretk.interface import Interface, InterfaceManager
from coretk.wlannodeconfig import WlanNodeConfig
link_layer_nodes = ["switch", "hub", "wlan", "rj45", "tunnel"]
network_layer_nodes = ["router", "host", "PC", "mdr", "prouter", "OVS"]
class Node:
def __init__(self, session_id, node_id, node_type, model, x, y, name):
"""
Create an instance of a node
:param int session_id: session id
:param int node_id: node id
:param core_pb2.NodeType node_type: node type
:param int x: x coordinate
:param int y: coordinate
:param str name: node name
"""
self.session_id = session_id
self.node_id = node_id
self.type = node_type
self.x = x
self.y = y
self.model = model
self.name = name
self.interfaces = []
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 GrpcManager:
def __init__(self, grpc):
self.nodes = {}
self.edges = {}
self.id = 1
# A list of id for re-use, keep in increasing order
self.reusable = []
self.preexisting = set()
self.core_grpc = grpc
# self.update_preexisting_ids()
# self.core_id_to_canvas_id = {}
self.interfaces_manager = InterfaceManager()
# map tuple(core_node_id, interface_id) to and edge
# self.node_id_and_interface_to_edge_token = {}
self.core_mapping = CoreToCanvasMapping()
self.wlanconfig_management = WlanNodeConfig()
def update_preexisting_ids(self):
"""
get preexisting node ids
:return:
"""
max_id = 0
client = self.core_grpc.core
sessions = client.get_sessions().sessions
for session_summary in sessions:
session = client.get_session(session_summary.id).session
for node in session.nodes:
if node.id > max_id:
max_id = node.id
self.preexisting.append(node.id)
self.id = max_id + 1
self.update_reusable_id()
def peek_id(self):
"""
Peek the next id to be used
:return: nothing
"""
if len(self.reusable) == 0:
return self.id
else:
return self.reusable[0]
def get_id(self):
"""
Get the next node id as well as update id status and reusable ids
:rtype: int
:return: the next id to be used
"""
if len(self.reusable) == 0:
new_id = self.id
self.id = self.id + 1
return new_id
else:
return self.reusable.pop(0)
def add_node(self, session_id, canvas_id, x, y, name):
"""
Add node, with information filled in, to grpc manager
:param int session_id: session id
:param int canvas_id: node's canvas id
:param int x: x coord
:param int y: y coord
:param str name: node type
:return: nothing
"""
node_type = None
node_model = None
if name in link_layer_nodes:
if name == "switch":
node_type = core_pb2.NodeType.SWITCH
elif name == "hub":
node_type = core_pb2.NodeType.HUB
elif name == "wlan":
node_type = core_pb2.NodeType.WIRELESS_LAN
elif name == "rj45":
node_type = core_pb2.NodeType.RJ45
elif name == "tunnel":
node_type = core_pb2.NodeType.TUNNEL
elif name in network_layer_nodes:
node_type = core_pb2.NodeType.DEFAULT
node_model = name
else:
logging.error("grpcmanagemeny.py INVALID node name")
nid = self.get_id()
create_node = Node(session_id, nid, node_type, node_model, x, y, name)
# set default configuration for wireless node
self.wlanconfig_management.set_default_config(node_type, nid)
self.nodes[canvas_id] = create_node
self.core_mapping.map_core_id_to_canvas_id(nid, canvas_id)
# self.core_id_to_canvas_id[nid] = canvas_id
logging.debug(
"Adding node to GrpcManager.. Session id: %s, Coords: (%s, %s), Name: %s",
session_id,
x,
y,
name,
)
def add_preexisting_node(self, canvas_node, session_id, core_node, name):
"""
Add preexisting nodes to grpc manager
:param str name: node_type
:param core_pb2.Node core_node: core node grpc message
:param coretk.graph.CanvasNode canvas_node: canvas node
:param int session_id: session id
:return: nothing
"""
# update the next available id
core_id = core_node.id
if self.id is None or core_id >= self.id:
self.id = core_id + 1
self.preexisting.add(core_id)
n = Node(
session_id,
core_id,
core_node.type,
core_node.model,
canvas_node.x_coord,
canvas_node.y_coord,
name,
)
self.nodes[canvas_node.id] = n
def update_node_location(self, canvas_id, new_x, new_y):
"""
update node
:param int canvas_id: canvas id of that node
:param int new_x: new x coord
:param int new_y: new y coord
:return: nothing
"""
self.nodes[canvas_id].x = new_x
self.nodes[canvas_id].y = new_y
def update_reusable_id(self):
"""
Update available id for reuse
:return: nothing
"""
if len(self.preexisting) > 0:
for i in range(1, self.id):
if i not in self.preexisting:
self.reusable.append(i)
self.preexisting.clear()
logging.debug("Next id: %s, Reusable: %s", self.id, self.reusable)
def delete_node(self, canvas_id):
"""
Delete a node from the session
:param int canvas_id: node's id in the canvas
:return: thing
"""
try:
self.nodes.pop(canvas_id)
self.reusable.append(canvas_id)
self.reusable.sort()
except KeyError:
logging.error("grpcmanagement.py INVALID NODE CANVAS ID")
def create_interface(self, edge, src_canvas_id, dst_canvas_id):
"""
Create the interface for the two end of an edge, add a copy to node's interfaces
:param coretk.grpcmanagement.Edge edge: edge to add interfaces to
:param int src_canvas_id: canvas id for the source node
:param int dst_canvas_id: canvas id for the destination node
:return: nothing
"""
src_interface = None
dst_interface = None
print("create interface")
self.interfaces_manager.new_subnet()
src_node = self.nodes[src_canvas_id]
if src_node.model in network_layer_nodes:
ifid = len(src_node.interfaces)
name = "eth" + str(ifid)
src_interface = Interface(
name=name, ifid=ifid, ipv4=str(self.interfaces_manager.get_address())
)
self.nodes[src_canvas_id].interfaces.append(src_interface)
logging.debug(
"Create source interface 1... IP: %s, name: %s",
src_interface.ipv4,
src_interface.name,
)
dst_node = self.nodes[dst_canvas_id]
if dst_node.model in network_layer_nodes:
ifid = len(dst_node.interfaces)
name = "eth" + str(ifid)
dst_interface = Interface(
name=name, ifid=ifid, ipv4=str(self.interfaces_manager.get_address())
)
self.nodes[dst_canvas_id].interfaces.append(dst_interface)
logging.debug(
"Create destination interface... IP: %s, name: %s",
dst_interface.ipv4,
dst_interface.name,
)
edge.interface_1 = src_interface
edge.interface_2 = dst_interface
return src_interface, dst_interface
def add_edge(self, session_id, token, canvas_id_1, canvas_id_2):
"""
Add an edge to grpc manager
:param int session_id: core session id
:param tuple(int, int) token: edge's identification in the canvas
:param int canvas_id_1: canvas id of source node
:param int canvas_id_2: canvas_id of destination node
:return: nothing
"""
if canvas_id_1 in self.nodes and canvas_id_2 in self.nodes:
edge = Edge(
session_id,
self.nodes[canvas_id_1].node_id,
self.nodes[canvas_id_1].type,
self.nodes[canvas_id_2].node_id,
self.nodes[canvas_id_2].type,
)
self.edges[token] = edge
src_interface, dst_interface = self.create_interface(
edge, canvas_id_1, canvas_id_2
)
node_one_id = self.nodes[canvas_id_1].node_id
node_two_id = self.nodes[canvas_id_2].node_id
# provide a way to get an edge from a core node and an interface id
if src_interface is not None:
# self.node_id_and_interface_to_edge_token[tuple([node_one_id, src_interface.id])] = token
self.core_mapping.map_node_and_interface_to_canvas_edge(
node_one_id, src_interface.id, token
)
logging.debug(
"map node id %s, interface_id %s to edge token %s",
node_one_id,
src_interface.id,
token,
)
if dst_interface is not None:
# self.node_id_and_interface_to_edge_token[tuple([node_two_id, dst_interface.id])] = token
self.core_mapping.map_node_and_interface_to_canvas_edge(
node_two_id, dst_interface.id, token
)
logging.debug(
"map node id %s, interface_id %s to edge token %s",
node_two_id,
dst_interface.id,
token,
)
logging.debug("Adding edge to grpc manager...")
else:
logging.error("grpcmanagement.py INVALID CANVAS NODE ID")

View file

@ -130,7 +130,7 @@ class Throughput:
iid = t.interface_id
tp = t.throughput
# token = self.grpc_manager.node_id_and_interface_to_edge_token[nid, iid]
token = self.core_grpc.manager.core_mapping.get_token_from_node_and_interface(
token = self.core_grpc.core_mapping.get_token_from_node_and_interface(
nid, iid
)
print(token)

View file

@ -5,10 +5,10 @@ from core.api.grpc import core_pb2
class WirelessConnection:
def __init__(self, canvas, grpc):
def __init__(self, canvas, core_grpc):
self.canvas = canvas
self.core_grpc = grpc
self.core_mapping = grpc.manager.core_mapping
self.core_grpc = core_grpc
self.core_mapping = core_grpc.core_mapping
# map a (node_one_id, node_two_id) to a wlan canvas id
self.map = {}