2019-10-02 00:25:26 +01:00
|
|
|
"""
|
|
|
|
Incorporate grpc into python tkinter GUI
|
|
|
|
"""
|
|
|
|
import logging
|
2019-10-11 01:02:28 +01:00
|
|
|
import os
|
2019-10-19 00:42:00 +01:00
|
|
|
from collections import OrderedDict
|
2019-10-02 00:25:26 +01:00
|
|
|
|
|
|
|
from core.api.grpc import client, core_pb2
|
2019-10-19 00:42:00 +01:00
|
|
|
from coretk.linkinfo import Throughput
|
|
|
|
from coretk.querysessiondrawing import SessionTable
|
|
|
|
from coretk.wirelessconnection import WirelessConnection
|
2019-10-02 00:25:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CoreGrpc:
|
2019-10-19 00:42:00 +01:00
|
|
|
def __init__(self, app, sid=None):
|
2019-10-02 00:25:26 +01:00
|
|
|
"""
|
|
|
|
Create a CoreGrpc instance
|
|
|
|
"""
|
|
|
|
self.core = client.CoreGrpcClient()
|
2019-10-19 00:42:00 +01:00
|
|
|
self.session_id = sid
|
2019-10-22 00:33:18 +01:00
|
|
|
|
|
|
|
self.node_ids = []
|
|
|
|
|
2019-10-19 00:42:00 +01:00
|
|
|
self.master = app.master
|
2019-10-11 01:02:28 +01:00
|
|
|
|
|
|
|
# self.set_up()
|
2019-10-04 00:50:49 +01:00
|
|
|
self.interface_helper = None
|
2019-10-19 00:42:00 +01:00
|
|
|
self.throughput_draw = Throughput(app.canvas, self)
|
|
|
|
self.wireless_draw = WirelessConnection(app.canvas, self)
|
2019-10-02 00:25:26 +01:00
|
|
|
|
|
|
|
def log_event(self, event):
|
|
|
|
logging.info("event: %s", event)
|
2019-10-19 00:42:00 +01:00
|
|
|
if event.link_event is not None:
|
|
|
|
self.wireless_draw.hangle_link_event(event.link_event)
|
|
|
|
|
|
|
|
def log_throughput(self, event):
|
|
|
|
interface_throughputs = event.interface_throughputs
|
2019-10-25 00:50:24 +01:00
|
|
|
for i in interface_throughputs:
|
|
|
|
print("")
|
|
|
|
return
|
2019-10-22 00:33:18 +01:00
|
|
|
throughputs_belong_to_session = []
|
|
|
|
for if_tp in interface_throughputs:
|
|
|
|
if if_tp.node_id in self.node_ids:
|
|
|
|
throughputs_belong_to_session.append(if_tp)
|
2019-10-19 00:42:00 +01:00
|
|
|
# bridge_throughputs = event.bridge_throughputs
|
2019-10-22 00:33:18 +01:00
|
|
|
self.throughput_draw.process_grpc_throughput_event(
|
|
|
|
throughputs_belong_to_session
|
|
|
|
)
|
2019-10-02 00:25:26 +01:00
|
|
|
|
2019-10-04 00:50:49 +01:00
|
|
|
def create_new_session(self):
|
2019-10-02 00:25:26 +01:00
|
|
|
"""
|
2019-10-04 00:50:49 +01:00
|
|
|
Create a new session
|
2019-10-02 00:25:26 +01:00
|
|
|
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
response = self.core.create_session()
|
|
|
|
logging.info("created session: %s", response)
|
|
|
|
|
|
|
|
# handle events session may broadcast
|
|
|
|
self.session_id = response.session_id
|
2019-10-25 00:50:24 +01:00
|
|
|
self.master.title("CORE Session ID " + str(self.session_id))
|
2019-10-02 00:25:26 +01:00
|
|
|
self.core.events(self.session_id, self.log_event)
|
2019-10-25 00:50:24 +01:00
|
|
|
# self.core.throughputs(self.log_throughput)
|
2019-10-11 01:02:28 +01:00
|
|
|
|
2019-10-04 00:50:49 +01:00
|
|
|
def query_existing_sessions(self, sessions):
|
|
|
|
"""
|
|
|
|
Query for existing sessions and prompt to join one
|
|
|
|
|
|
|
|
:param repeated core_pb2.SessionSummary sessions: summaries of all the existing sessions
|
|
|
|
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2019-10-19 00:42:00 +01:00
|
|
|
SessionTable(self, self.master)
|
|
|
|
|
|
|
|
def delete_session(self, custom_sid=None):
|
|
|
|
if custom_sid is None:
|
|
|
|
sid = self.session_id
|
|
|
|
else:
|
|
|
|
sid = custom_sid
|
|
|
|
response = self.core.delete_session(sid)
|
2019-10-05 00:52:07 +01:00
|
|
|
logging.info("Deleted session result: %s", response)
|
|
|
|
|
2019-10-19 00:42:00 +01:00
|
|
|
def terminate_session(self, custom_sid=None):
|
|
|
|
if custom_sid is None:
|
|
|
|
sid = self.session_id
|
|
|
|
else:
|
|
|
|
sid = custom_sid
|
|
|
|
s = self.core.get_session(sid).session
|
|
|
|
# delete links and nodes from running session
|
|
|
|
if s.state == core_pb2.SessionState.RUNTIME:
|
|
|
|
self.set_session_state("datacollect", sid)
|
|
|
|
self.delete_links(sid)
|
|
|
|
self.delete_nodes(sid)
|
|
|
|
self.delete_session(sid)
|
|
|
|
|
2019-10-04 00:50:49 +01:00
|
|
|
def set_up(self):
|
|
|
|
"""
|
2019-10-11 01:02:28 +01:00
|
|
|
Query sessions, if there exist any, prompt whether to join one
|
2019-10-04 00:50:49 +01:00
|
|
|
|
2019-10-11 01:02:28 +01:00
|
|
|
:return: existing sessions
|
2019-10-04 00:50:49 +01:00
|
|
|
"""
|
|
|
|
self.core.connect()
|
|
|
|
|
|
|
|
response = self.core.get_sessions()
|
2019-10-19 00:42:00 +01:00
|
|
|
# logging.info("coregrpc.py: all sessions: %s", response)
|
2019-10-04 00:50:49 +01:00
|
|
|
|
|
|
|
# if there are no sessions, create a new session, else join a session
|
|
|
|
sessions = response.sessions
|
|
|
|
|
|
|
|
if len(sessions) == 0:
|
|
|
|
self.create_new_session()
|
|
|
|
else:
|
2019-10-11 01:02:28 +01:00
|
|
|
|
2019-10-04 00:50:49 +01:00
|
|
|
self.query_existing_sessions(sessions)
|
|
|
|
|
2019-10-05 00:52:07 +01:00
|
|
|
def get_session_state(self):
|
|
|
|
response = self.core.get_session(self.session_id)
|
2019-10-19 00:42:00 +01:00
|
|
|
# logging.info("get session: %s", response)
|
2019-10-05 00:52:07 +01:00
|
|
|
return response.session.state
|
|
|
|
|
2019-10-19 00:42:00 +01:00
|
|
|
def set_session_state(self, state, custom_session_id=None):
|
2019-10-05 00:52:07 +01:00
|
|
|
"""
|
|
|
|
Set session state
|
|
|
|
|
|
|
|
:param str state: session state to set
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2019-10-19 00:42:00 +01:00
|
|
|
if custom_session_id is None:
|
|
|
|
sid = self.session_id
|
|
|
|
else:
|
|
|
|
sid = custom_session_id
|
|
|
|
|
2019-10-05 00:52:07 +01:00
|
|
|
if state == "configuration":
|
|
|
|
response = self.core.set_session_state(
|
2019-10-19 00:42:00 +01:00
|
|
|
sid, core_pb2.SessionState.CONFIGURATION
|
2019-10-05 00:52:07 +01:00
|
|
|
)
|
|
|
|
elif state == "instantiation":
|
|
|
|
response = self.core.set_session_state(
|
2019-10-19 00:42:00 +01:00
|
|
|
sid, core_pb2.SessionState.INSTANTIATION
|
2019-10-05 00:52:07 +01:00
|
|
|
)
|
|
|
|
elif state == "datacollect":
|
|
|
|
response = self.core.set_session_state(
|
2019-10-19 00:42:00 +01:00
|
|
|
sid, core_pb2.SessionState.DATACOLLECT
|
2019-10-05 00:52:07 +01:00
|
|
|
)
|
|
|
|
elif state == "shutdown":
|
2019-10-19 00:42:00 +01:00
|
|
|
response = self.core.set_session_state(sid, core_pb2.SessionState.SHUTDOWN)
|
2019-10-05 00:52:07 +01:00
|
|
|
elif state == "runtime":
|
2019-10-19 00:42:00 +01:00
|
|
|
response = self.core.set_session_state(sid, core_pb2.SessionState.RUNTIME)
|
2019-10-05 00:52:07 +01:00
|
|
|
elif state == "definition":
|
|
|
|
response = self.core.set_session_state(
|
2019-10-19 00:42:00 +01:00
|
|
|
sid, core_pb2.SessionState.DEFINITION
|
2019-10-05 00:52:07 +01:00
|
|
|
)
|
|
|
|
elif state == "none":
|
2019-10-19 00:42:00 +01:00
|
|
|
response = self.core.set_session_state(sid, core_pb2.SessionState.NONE)
|
2019-10-05 00:52:07 +01:00
|
|
|
else:
|
|
|
|
logging.error("coregrpc.py: set_session_state: INVALID STATE")
|
2019-10-02 00:25:26 +01:00
|
|
|
|
2019-10-04 00:50:49 +01:00
|
|
|
logging.info("set session state: %s", response)
|
|
|
|
|
2019-10-05 00:52:07 +01:00
|
|
|
def add_node(self, node_type, model, x, y, name, node_id):
|
2019-10-04 00:50:49 +01:00
|
|
|
position = core_pb2.Position(x=x, y=y)
|
2019-10-05 00:52:07 +01:00
|
|
|
node = core_pb2.Node(id=node_id, type=node_type, position=position, model=model)
|
2019-10-22 00:33:18 +01:00
|
|
|
self.node_ids.append(node_id)
|
2019-10-02 00:25:26 +01:00
|
|
|
response = self.core.add_node(self.session_id, node)
|
2019-10-04 00:50:49 +01:00
|
|
|
logging.info("created node: %s", response)
|
2019-10-19 00:42:00 +01:00
|
|
|
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)
|
2019-10-02 00:25:26 +01:00
|
|
|
return response.node_id
|
|
|
|
|
2019-10-11 01:02:28 +01:00
|
|
|
def edit_node(self, node_id, x, y):
|
2019-10-02 00:25:26 +01:00
|
|
|
position = core_pb2.Position(x=x, y=y)
|
2019-10-11 01:02:28 +01:00
|
|
|
response = self.core.edit_node(self.session_id, node_id, position)
|
2019-10-02 00:25:26 +01:00
|
|
|
logging.info("updated node id %s: %s", node_id, response)
|
2019-10-19 00:42:00 +01:00
|
|
|
# self.core.events(self.session_id, self.log_event)
|
2019-10-02 00:25:26 +01:00
|
|
|
|
2019-10-19 00:42:00 +01:00
|
|
|
def delete_nodes(self, delete_session=None):
|
|
|
|
if delete_session is None:
|
|
|
|
sid = self.session_id
|
|
|
|
else:
|
|
|
|
sid = delete_session
|
|
|
|
for node in self.core.get_session(sid).session.nodes:
|
2019-10-05 00:52:07 +01:00
|
|
|
response = self.core.delete_node(self.session_id, node.id)
|
2019-10-19 00:42:00 +01:00
|
|
|
logging.info("delete nodes %s", response)
|
|
|
|
|
|
|
|
def delete_links(self, delete_session=None):
|
2019-10-29 16:04:16 +00:00
|
|
|
# sid = None
|
2019-10-19 00:42:00 +01:00
|
|
|
if delete_session is None:
|
|
|
|
sid = self.session_id
|
|
|
|
else:
|
|
|
|
sid = delete_session
|
2019-10-05 00:52:07 +01:00
|
|
|
|
2019-10-19 00:42:00 +01:00
|
|
|
for link in self.core.get_session(sid).session.links:
|
2019-10-05 00:52:07 +01:00
|
|
|
response = self.core.delete_link(
|
|
|
|
self.session_id,
|
|
|
|
link.node_one_id,
|
|
|
|
link.node_two_id,
|
|
|
|
link.interface_one.id,
|
|
|
|
link.interface_two.id,
|
|
|
|
)
|
2019-10-19 00:42:00 +01:00
|
|
|
logging.info("delete links %s", response)
|
2019-10-05 00:52:07 +01:00
|
|
|
|
2019-10-11 01:02:28 +01:00
|
|
|
def add_link(self, id1, id2, type1, type2, edge):
|
2019-10-04 00:50:49 +01:00
|
|
|
"""
|
|
|
|
Grpc client request add link
|
|
|
|
|
|
|
|
:param int session_id: session id
|
|
|
|
:param int id1: node 1 core id
|
|
|
|
:param core_pb2.NodeType type1: node 1 core node type
|
|
|
|
:param int id2: node 2 core id
|
|
|
|
:param core_pb2.NodeType type2: node 2 core node type
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2019-10-11 01:02:28 +01:00
|
|
|
if1 = None
|
|
|
|
if2 = None
|
2019-10-04 00:50:49 +01:00
|
|
|
if type1 == core_pb2.NodeType.DEFAULT:
|
2019-10-11 01:02:28 +01:00
|
|
|
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)
|
|
|
|
|
2019-10-04 00:50:49 +01:00
|
|
|
if type2 == core_pb2.NodeType.DEFAULT:
|
2019-10-11 01:02:28 +01:00
|
|
|
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)
|
2019-10-04 00:50:49 +01:00
|
|
|
logging.info("created link: %s", response)
|
|
|
|
|
2019-10-29 16:04:16 +00:00
|
|
|
# self.core.get_node_links(self.session_id, id1)
|
2019-10-22 21:17:47 +01:00
|
|
|
|
2019-10-19 00:42:00 +01:00
|
|
|
# def get_session(self):
|
|
|
|
# response = self.core.get_session(self.session_id)
|
|
|
|
# nodes = response.session.nodes
|
|
|
|
# for node in nodes:
|
|
|
|
# r = self.core.get_node_links(self.session_id, node.id)
|
|
|
|
# logging.info(r)
|
|
|
|
|
2019-10-11 01:02:28 +01:00
|
|
|
def launch_terminal(self, node_id):
|
|
|
|
response = self.core.get_node_terminal(self.session_id, node_id)
|
|
|
|
logging.info("get terminal %s", response.terminal)
|
|
|
|
os.system("xterm -e %s &" % response.terminal)
|
|
|
|
|
|
|
|
def save_xml(self, file_path):
|
|
|
|
"""
|
|
|
|
Save core session as to an xml file
|
|
|
|
|
|
|
|
:param str file_path: file path that user pick
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
response = self.core.save_xml(self.session_id, file_path)
|
|
|
|
logging.info("coregrpc.py save xml %s", response)
|
2019-10-19 00:42:00 +01:00
|
|
|
self.core.events(self.session_id, self.log_event)
|
2019-10-11 01:02:28 +01:00
|
|
|
|
|
|
|
def open_xml(self, file_path):
|
|
|
|
"""
|
|
|
|
Open core xml
|
|
|
|
|
|
|
|
:param str file_path: file to open
|
|
|
|
:return: session id
|
|
|
|
"""
|
|
|
|
response = self.core.open_xml(file_path)
|
2019-10-19 00:42:00 +01:00
|
|
|
self.session_id = response.session_id
|
2019-10-29 16:04:16 +00:00
|
|
|
logging.debug("coreprgc.py open_xml(): %s", response.result)
|
2019-10-11 01:02:28 +01:00
|
|
|
|
2019-10-02 00:25:26 +01:00
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Clean ups when done using grpc
|
|
|
|
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
logging.debug("Close grpc")
|
|
|
|
self.core.close()
|