2019-03-21 05:11:09 +00:00
|
|
|
import atexit
|
2019-03-16 20:48:25 +00:00
|
|
|
import logging
|
2019-02-24 16:15:33 +00:00
|
|
|
import os
|
2019-03-02 21:44:27 +00:00
|
|
|
import tempfile
|
2019-02-18 07:41:30 +00:00
|
|
|
import time
|
2019-03-19 05:32:01 +00:00
|
|
|
from Queue import Queue, Empty
|
2019-02-18 07:41:30 +00:00
|
|
|
|
|
|
|
import grpc
|
2019-03-16 20:48:25 +00:00
|
|
|
from concurrent import futures
|
2019-02-18 07:41:30 +00:00
|
|
|
|
2019-03-16 20:48:25 +00:00
|
|
|
from core.emulator.emudata import NodeOptions, InterfaceData, LinkOptions
|
2019-03-26 00:16:54 +00:00
|
|
|
from core.enumerations import NodeTypes, EventTypes, LinkTypes
|
2019-03-23 00:07:54 +00:00
|
|
|
from core.grpc import core_pb2
|
|
|
|
from core.grpc import core_pb2_grpc
|
2019-02-19 06:54:14 +00:00
|
|
|
from core.misc import nodeutils
|
2019-04-06 05:25:46 +01:00
|
|
|
from core.misc.ipaddress import MacAddress
|
2019-03-17 22:28:51 +00:00
|
|
|
from core.mobility import BasicRangeModel, Ns2ScriptedMobility
|
2019-03-26 00:16:54 +00:00
|
|
|
from core.service import ServiceManager
|
2019-02-18 07:41:30 +00:00
|
|
|
|
|
|
|
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
|
|
|
|
|
|
|
|
|
2019-02-20 07:32:04 +00:00
|
|
|
def convert_value(value):
|
2019-03-23 00:07:54 +00:00
|
|
|
if value is not None:
|
|
|
|
value = str(value)
|
|
|
|
return value
|
2019-02-20 07:32:04 +00:00
|
|
|
|
|
|
|
|
2019-03-02 06:14:16 +00:00
|
|
|
def get_config_groups(config, configurable_options):
|
|
|
|
groups = []
|
|
|
|
config_options = []
|
|
|
|
|
|
|
|
for configuration in configurable_options.configurations():
|
|
|
|
value = config[configuration.id]
|
|
|
|
config_option = core_pb2.ConfigOption()
|
|
|
|
config_option.label = configuration.label
|
|
|
|
config_option.name = configuration.id
|
|
|
|
config_option.value = value
|
|
|
|
config_option.type = configuration.type.value
|
|
|
|
config_option.select.extend(configuration.options)
|
|
|
|
config_options.append(config_option)
|
|
|
|
|
|
|
|
for config_group in configurable_options.config_groups():
|
|
|
|
start = config_group.start - 1
|
|
|
|
stop = config_group.stop
|
2019-03-23 04:06:10 +00:00
|
|
|
options = config_options[start: stop]
|
|
|
|
config_group_proto = core_pb2.ConfigGroup(name=config_group.name, options=options)
|
2019-03-02 06:14:16 +00:00
|
|
|
groups.append(config_group_proto)
|
|
|
|
|
|
|
|
return groups
|
|
|
|
|
|
|
|
|
2019-03-23 04:06:10 +00:00
|
|
|
def get_links(session, node):
|
|
|
|
links = []
|
|
|
|
for link_data in node.all_link_data(0):
|
|
|
|
link = convert_link(session, link_data)
|
|
|
|
links.append(link)
|
|
|
|
return links
|
|
|
|
|
|
|
|
|
2019-03-29 04:47:30 +00:00
|
|
|
def get_emane_model_id(_id, interface):
|
|
|
|
if interface >= 0:
|
|
|
|
return _id * 1000 + interface
|
|
|
|
else:
|
|
|
|
return _id
|
|
|
|
|
|
|
|
|
2019-03-23 04:06:10 +00:00
|
|
|
def convert_link(session, link_data):
|
2019-03-22 21:32:39 +00:00
|
|
|
interface_one = None
|
2019-02-20 07:32:04 +00:00
|
|
|
if link_data.interface1_id is not None:
|
|
|
|
node = session.get_object(link_data.node1_id)
|
|
|
|
interface = node.netif(link_data.interface1_id)
|
2019-03-22 21:32:39 +00:00
|
|
|
interface_one = core_pb2.Interface(
|
|
|
|
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)
|
2019-02-20 07:32:04 +00:00
|
|
|
|
2019-03-22 21:32:39 +00:00
|
|
|
interface_two = None
|
2019-02-20 07:32:04 +00:00
|
|
|
if link_data.interface2_id is not None:
|
|
|
|
node = session.get_object(link_data.node2_id)
|
|
|
|
interface = node.netif(link_data.interface2_id)
|
2019-03-22 21:32:39 +00:00
|
|
|
interface_two = core_pb2.Interface(
|
|
|
|
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,
|
2019-03-22 22:11:40 +00:00
|
|
|
ip6=convert_value(link_data.interface2_ip6), ip6mask=link_data.interface2_ip6_mask)
|
2019-02-20 07:32:04 +00:00
|
|
|
|
2019-03-22 21:32:39 +00:00
|
|
|
options = core_pb2.LinkOptions(
|
2019-02-20 07:32:04 +00:00
|
|
|
opaque=link_data.opaque,
|
|
|
|
jitter=link_data.jitter,
|
|
|
|
key=link_data.key,
|
|
|
|
mburst=link_data.mburst,
|
|
|
|
mer=link_data.mer,
|
|
|
|
per=link_data.per,
|
|
|
|
bandwidth=link_data.bandwidth,
|
|
|
|
burst=link_data.burst,
|
|
|
|
delay=link_data.delay,
|
|
|
|
dup=link_data.dup,
|
|
|
|
unidirectional=link_data.unidirectional
|
|
|
|
)
|
|
|
|
|
2019-03-23 04:06:10 +00:00
|
|
|
return core_pb2.Link(
|
2019-03-22 21:32:39 +00:00
|
|
|
type=link_data.link_type, node_one=link_data.node1_id, node_two=link_data.node2_id,
|
|
|
|
interface_one=interface_one, interface_two=interface_two, options=options
|
|
|
|
)
|
|
|
|
|
2019-02-20 07:32:04 +00:00
|
|
|
|
2019-03-22 05:56:33 +00:00
|
|
|
class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
|
2019-02-19 06:54:14 +00:00
|
|
|
def __init__(self, coreemu):
|
2019-03-22 05:56:33 +00:00
|
|
|
super(CoreGrpcServer, self).__init__()
|
2019-02-19 06:54:14 +00:00
|
|
|
self.coreemu = coreemu
|
2019-03-21 05:11:09 +00:00
|
|
|
self.running = True
|
2019-03-22 06:29:26 +00:00
|
|
|
self.server = None
|
2019-03-21 05:11:09 +00:00
|
|
|
atexit.register(self._exit_handler)
|
|
|
|
|
|
|
|
def _exit_handler(self):
|
|
|
|
logging.debug("catching exit, stop running")
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
def _is_running(self, context):
|
|
|
|
return self.running and context.is_active()
|
|
|
|
|
|
|
|
def _cancel_stream(self, context):
|
|
|
|
context.abort(grpc.StatusCode.CANCELLED, "server stopping")
|
2019-02-18 07:41:30 +00:00
|
|
|
|
2019-03-22 05:56:33 +00:00
|
|
|
def listen(self, address="[::]:50051"):
|
|
|
|
logging.info("starting grpc api: %s", address)
|
2019-03-22 06:29:26 +00:00
|
|
|
self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
|
|
|
core_pb2_grpc.add_CoreApiServicer_to_server(self, self.server)
|
|
|
|
self.server.add_insecure_port(address)
|
|
|
|
self.server.start()
|
2019-03-22 05:56:33 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
time.sleep(_ONE_DAY_IN_SECONDS)
|
|
|
|
except KeyboardInterrupt:
|
2019-03-22 06:29:26 +00:00
|
|
|
self.server.stop(None)
|
2019-03-22 05:56:33 +00:00
|
|
|
|
2019-03-17 22:53:29 +00:00
|
|
|
def get_session(self, _id, context):
|
2019-03-17 22:28:51 +00:00
|
|
|
session = self.coreemu.sessions.get(_id)
|
|
|
|
if not session:
|
2019-03-26 05:00:08 +00:00
|
|
|
context.abort(grpc.StatusCode.NOT_FOUND, "session {} not found".format(_id))
|
2019-03-17 22:28:51 +00:00
|
|
|
return session
|
|
|
|
|
2019-03-17 22:53:29 +00:00
|
|
|
def get_node(self, session, _id, context):
|
2019-03-26 05:00:08 +00:00
|
|
|
try:
|
|
|
|
return session.get_object(_id)
|
|
|
|
except KeyError:
|
|
|
|
context.abort(grpc.StatusCode.NOT_FOUND, "node {} not found".format(_id))
|
2019-03-17 22:28:51 +00:00
|
|
|
|
2019-02-24 15:44:41 +00:00
|
|
|
def CreateSession(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("create session: %s", request)
|
2019-03-22 20:45:53 +00:00
|
|
|
session = self.coreemu.create_session(request.id)
|
2019-02-24 15:44:41 +00:00
|
|
|
session.set_state(EventTypes.DEFINITION_STATE)
|
|
|
|
session.location.setrefgeo(47.57917, -122.13232, 2.0)
|
|
|
|
session.location.refscale = 150000.0
|
2019-04-08 18:39:36 +01:00
|
|
|
return core_pb2.CreateSessionResponse(id=session.id, state=session.state)
|
2019-02-24 15:44:41 +00:00
|
|
|
|
2019-02-24 16:15:33 +00:00
|
|
|
def DeleteSession(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("delete session: %s", request)
|
2019-03-22 21:32:39 +00:00
|
|
|
result = self.coreemu.delete_session(request.id)
|
|
|
|
return core_pb2.DeleteSessionResponse(result=result)
|
2019-02-24 16:15:33 +00:00
|
|
|
|
2019-02-18 07:41:30 +00:00
|
|
|
def GetSessions(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get sessions: %s", request)
|
2019-03-23 00:07:54 +00:00
|
|
|
sessions = []
|
2019-02-19 06:54:14 +00:00
|
|
|
for session_id in self.coreemu.sessions:
|
|
|
|
session = self.coreemu.sessions[session_id]
|
2019-03-23 00:07:54 +00:00
|
|
|
session_summary = core_pb2.SessionSummary(
|
|
|
|
id=session_id, state=session.state, nodes=session.get_node_count())
|
|
|
|
sessions.append(session_summary)
|
|
|
|
return core_pb2.GetSessionsResponse(sessions=sessions)
|
2019-02-19 06:54:14 +00:00
|
|
|
|
2019-02-21 05:19:35 +00:00
|
|
|
def GetSessionLocation(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get session location: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.id, context)
|
2019-02-21 05:19:35 +00:00
|
|
|
x, y, z = session.location.refxyz
|
|
|
|
lat, lon, alt = session.location.refgeo
|
2019-03-22 21:32:39 +00:00
|
|
|
position = core_pb2.Position(x=x, y=y, z=z, lat=lat, lon=lon, alt=alt)
|
|
|
|
return core_pb2.GetSessionLocationResponse(position=position, scale=session.location.refscale)
|
2019-02-21 05:19:35 +00:00
|
|
|
|
2019-02-24 15:44:41 +00:00
|
|
|
def SetSessionLocation(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set session location: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.id, context)
|
2019-02-24 15:44:41 +00:00
|
|
|
session.location.refxyz = (request.position.x, request.position.y, request.position.z)
|
|
|
|
session.location.setrefgeo(request.position.lat, request.position.lon, request.position.alt)
|
|
|
|
session.location.refscale = request.scale
|
2019-03-22 21:32:39 +00:00
|
|
|
return core_pb2.SetSessionLocationResponse(result=True)
|
2019-02-24 15:44:41 +00:00
|
|
|
|
2019-02-24 16:15:33 +00:00
|
|
|
def SetSessionState(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set session state: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.id, context)
|
2019-02-24 16:15:33 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
state = EventTypes(request.state)
|
|
|
|
session.set_state(state)
|
|
|
|
|
|
|
|
if state == EventTypes.INSTANTIATION_STATE:
|
|
|
|
if not os.path.exists(session.session_dir):
|
|
|
|
os.mkdir(session.session_dir)
|
|
|
|
session.instantiate()
|
|
|
|
elif state == EventTypes.SHUTDOWN_STATE:
|
|
|
|
session.shutdown()
|
|
|
|
elif state == EventTypes.DATACOLLECT_STATE:
|
|
|
|
session.data_collect()
|
|
|
|
elif state == EventTypes.DEFINITION_STATE:
|
|
|
|
session.clear()
|
|
|
|
|
2019-03-22 21:32:39 +00:00
|
|
|
result = True
|
2019-02-24 16:15:33 +00:00
|
|
|
except KeyError:
|
2019-03-22 21:32:39 +00:00
|
|
|
result = False
|
2019-02-24 16:15:33 +00:00
|
|
|
|
2019-03-22 21:32:39 +00:00
|
|
|
return core_pb2.SetSessionStateResponse(result=result)
|
2019-02-24 16:15:33 +00:00
|
|
|
|
2019-02-21 05:19:35 +00:00
|
|
|
def GetSessionOptions(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get session options: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.id, context)
|
2019-02-21 05:19:35 +00:00
|
|
|
config = session.options.get_configs()
|
2019-03-02 21:44:27 +00:00
|
|
|
defaults = session.options.default_values()
|
|
|
|
defaults.update(config)
|
|
|
|
groups = get_config_groups(defaults, session.options)
|
2019-03-22 21:32:39 +00:00
|
|
|
return core_pb2.GetSessionOptionsResponse(groups=groups)
|
2019-02-21 05:19:35 +00:00
|
|
|
|
2019-03-17 04:40:15 +00:00
|
|
|
def SetSessionOptions(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set session options: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.id, context)
|
2019-03-24 21:13:04 +00:00
|
|
|
config = session.options.get_configs()
|
|
|
|
config.update(request.config)
|
2019-03-22 21:32:39 +00:00
|
|
|
return core_pb2.SetSessionOptionsResponse(result=True)
|
2019-03-17 04:40:15 +00:00
|
|
|
|
2019-02-19 06:54:14 +00:00
|
|
|
def GetSession(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get session: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.id, context)
|
2019-02-24 16:15:33 +00:00
|
|
|
|
2019-03-23 04:06:10 +00:00
|
|
|
links = []
|
|
|
|
nodes = []
|
2019-02-19 06:54:14 +00:00
|
|
|
for node_id in session.objects:
|
|
|
|
node = session.objects[node_id]
|
|
|
|
if not isinstance(node.objid, int):
|
|
|
|
continue
|
|
|
|
|
2019-03-22 21:32:39 +00:00
|
|
|
node_type = nodeutils.get_node_type(node.__class__).value
|
2019-02-19 06:54:14 +00:00
|
|
|
model = getattr(node, "type", None)
|
2019-03-22 21:32:39 +00:00
|
|
|
position = core_pb2.Position(x=node.position.x, y=node.position.y, z=node.position.z)
|
2019-02-19 06:54:14 +00:00
|
|
|
|
|
|
|
services = getattr(node, "services", [])
|
|
|
|
if services is None:
|
|
|
|
services = []
|
|
|
|
services = [x.name for x in services]
|
|
|
|
|
|
|
|
emane_model = None
|
|
|
|
if nodeutils.is_node(node, NodeTypes.EMANE):
|
|
|
|
emane_model = node.model.name
|
2019-03-22 21:32:39 +00:00
|
|
|
|
2019-03-23 04:06:10 +00:00
|
|
|
node_proto = core_pb2.Node(
|
2019-03-22 22:11:40 +00:00
|
|
|
id=node.objid, name=node.name, emane=emane_model, model=model,
|
|
|
|
type=node_type, position=position, services=services)
|
2019-03-23 04:06:10 +00:00
|
|
|
nodes.append(node_proto)
|
2019-02-19 06:54:14 +00:00
|
|
|
|
2019-03-23 04:06:10 +00:00
|
|
|
node_links = get_links(session, node)
|
|
|
|
links.extend(node_links)
|
2019-02-20 07:32:04 +00:00
|
|
|
|
2019-03-23 04:06:10 +00:00
|
|
|
session_proto = core_pb2.Session(state=session.state, nodes=nodes, links=links)
|
2019-03-22 21:32:39 +00:00
|
|
|
return core_pb2.GetSessionResponse(session=session_proto)
|
2019-02-18 07:41:30 +00:00
|
|
|
|
2019-03-16 20:48:25 +00:00
|
|
|
def NodeEvents(self, request, context):
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.id, context)
|
2019-03-16 20:48:25 +00:00
|
|
|
queue = Queue()
|
2019-04-05 04:52:20 +01:00
|
|
|
session.node_handlers.append(queue.put)
|
2019-03-16 20:48:25 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
while self._is_running(context):
|
2019-03-19 05:32:01 +00:00
|
|
|
try:
|
|
|
|
node = queue.get(timeout=1)
|
2019-03-22 23:16:30 +00:00
|
|
|
position = core_pb2.Position(x=node.x_position, y=node.y_position)
|
2019-03-19 05:32:01 +00:00
|
|
|
services = node.services or ""
|
2019-03-22 23:16:30 +00:00
|
|
|
services = services.split("|")
|
|
|
|
node_proto = core_pb2.Node(
|
|
|
|
id=node.id, name=node.name, model=node.model, position=position, services=services)
|
|
|
|
node_event = core_pb2.NodeEvent(node=node_proto)
|
2019-03-19 05:32:01 +00:00
|
|
|
yield node_event
|
|
|
|
except Empty:
|
|
|
|
continue
|
2019-03-16 20:48:25 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
self._cancel_stream(context)
|
|
|
|
|
2019-03-19 04:46:27 +00:00
|
|
|
def LinkEvents(self, request, context):
|
|
|
|
session = self.get_session(request.id, context)
|
|
|
|
queue = Queue()
|
2019-04-05 04:52:20 +01:00
|
|
|
session.link_handlers.append(queue.put)
|
2019-03-19 04:46:27 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
while self._is_running(context):
|
2019-03-19 05:32:01 +00:00
|
|
|
try:
|
|
|
|
event = queue.get(timeout=1)
|
2019-03-22 23:16:30 +00:00
|
|
|
interface_one = None
|
2019-03-19 05:32:01 +00:00
|
|
|
if event.interface1_id is not None:
|
2019-03-22 23:16:30 +00:00
|
|
|
interface_one = core_pb2.Interface(
|
|
|
|
id=event.interface1_id, name=event.interface1_name, mac=convert_value(event.interface1_mac),
|
|
|
|
ip4=convert_value(event.interface1_ip4), ip4mask=event.interface1_ip4_mask,
|
|
|
|
ip6=convert_value(event.interface1_ip6), ip6mask=event.interface1_ip6_mask)
|
2019-03-19 05:32:01 +00:00
|
|
|
|
2019-03-22 23:16:30 +00:00
|
|
|
interface_two = None
|
2019-03-19 05:32:01 +00:00
|
|
|
if event.interface2_id is not None:
|
2019-03-22 23:16:30 +00:00
|
|
|
interface_two = core_pb2.Interface(
|
|
|
|
id=event.interface2_id, name=event.interface2_name, mac=convert_value(event.interface2_mac),
|
|
|
|
ip4=convert_value(event.interface2_ip4), ip4mask=event.interface2_ip4_mask,
|
|
|
|
ip6=convert_value(event.interface2_ip6), ip6mask=event.interface2_ip6_mask)
|
|
|
|
|
|
|
|
options = core_pb2.LinkOptions(
|
2019-03-19 05:32:01 +00:00
|
|
|
opaque=event.opaque,
|
|
|
|
jitter=event.jitter,
|
|
|
|
key=event.key,
|
|
|
|
mburst=event.mburst,
|
|
|
|
mer=event.mer,
|
|
|
|
per=event.per,
|
|
|
|
bandwidth=event.bandwidth,
|
|
|
|
burst=event.burst,
|
|
|
|
delay=event.delay,
|
|
|
|
dup=event.dup,
|
|
|
|
unidirectional=event.unidirectional
|
2019-03-19 04:46:27 +00:00
|
|
|
)
|
2019-03-22 23:16:30 +00:00
|
|
|
link = core_pb2.Link(
|
|
|
|
type=event.link_type, node_one=event.node1_id, node_two=event.node2_id,
|
|
|
|
interface_one=interface_one, interface_two=interface_two, options=options)
|
|
|
|
link_event = core_pb2.LinkEvent(message_type=event.message_type, link=link)
|
2019-03-19 05:32:01 +00:00
|
|
|
yield link_event
|
|
|
|
except Empty:
|
|
|
|
continue
|
2019-03-19 04:46:27 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
self._cancel_stream(context)
|
|
|
|
|
2019-03-16 20:48:25 +00:00
|
|
|
def SessionEvents(self, request, context):
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.id, context)
|
2019-03-16 20:48:25 +00:00
|
|
|
queue = Queue()
|
2019-04-05 04:52:20 +01:00
|
|
|
session.event_handlers.append(queue.put)
|
2019-03-16 20:48:25 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
while self._is_running(context):
|
2019-03-19 05:32:01 +00:00
|
|
|
try:
|
|
|
|
event = queue.get(timeout=1)
|
|
|
|
event_time = event.time
|
|
|
|
if event_time is not None:
|
|
|
|
event_time = float(event_time)
|
2019-03-22 23:16:30 +00:00
|
|
|
session_event = core_pb2.SessionEvent(
|
2019-03-19 05:32:01 +00:00
|
|
|
node=event.node,
|
|
|
|
event=event.event_type,
|
|
|
|
name=event.name,
|
|
|
|
data=event.data,
|
|
|
|
time=event_time,
|
2019-04-08 18:39:36 +01:00
|
|
|
session=session.id
|
2019-03-19 05:32:01 +00:00
|
|
|
)
|
|
|
|
yield session_event
|
|
|
|
except Empty:
|
|
|
|
continue
|
2019-03-16 20:48:25 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
self._cancel_stream(context)
|
|
|
|
|
2019-03-18 06:29:38 +00:00
|
|
|
def ConfigEvents(self, request, context):
|
|
|
|
session = self.get_session(request.id, context)
|
|
|
|
queue = Queue()
|
2019-04-05 04:52:20 +01:00
|
|
|
session.config_handlers.append(queue.put)
|
2019-03-18 06:29:38 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
while self._is_running(context):
|
2019-03-19 05:32:01 +00:00
|
|
|
try:
|
|
|
|
event = queue.get(timeout=1)
|
2019-03-22 23:16:30 +00:00
|
|
|
config_event = core_pb2.ConfigEvent(
|
2019-03-19 05:32:01 +00:00
|
|
|
message_type=event.message_type,
|
|
|
|
node=event.node,
|
|
|
|
object=event.object,
|
|
|
|
type=event.type,
|
|
|
|
captions=event.captions,
|
|
|
|
bitmap=event.bitmap,
|
|
|
|
data_values=event.data_values,
|
|
|
|
possible_values=event.possible_values,
|
|
|
|
groups=event.groups,
|
|
|
|
session=event.session,
|
|
|
|
interface=event.interface_number,
|
|
|
|
network_id=event.network_id,
|
2019-03-22 23:16:30 +00:00
|
|
|
opaque=event.opaque,
|
|
|
|
data_types=event.data_types
|
2019-03-19 05:32:01 +00:00
|
|
|
)
|
|
|
|
yield config_event
|
|
|
|
except Empty:
|
|
|
|
continue
|
2019-03-18 06:29:38 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
self._cancel_stream(context)
|
|
|
|
|
2019-03-18 06:29:38 +00:00
|
|
|
def ExceptionEvents(self, request, context):
|
|
|
|
session = self.get_session(request.id, context)
|
|
|
|
queue = Queue()
|
2019-04-05 04:52:20 +01:00
|
|
|
session.exception_handlers.append(queue.put)
|
2019-03-18 06:29:38 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
while self._is_running(context):
|
2019-03-19 05:32:01 +00:00
|
|
|
try:
|
|
|
|
event = queue.get(timeout=1)
|
2019-03-22 23:16:30 +00:00
|
|
|
exception_event = core_pb2.ExceptionEvent(
|
2019-03-19 05:32:01 +00:00
|
|
|
node=event.node,
|
2019-03-20 04:26:06 +00:00
|
|
|
session=int(event.session),
|
|
|
|
level=event.level.value,
|
2019-03-19 05:32:01 +00:00
|
|
|
source=event.source,
|
2019-03-20 04:26:06 +00:00
|
|
|
date=event.date,
|
2019-03-19 05:32:01 +00:00
|
|
|
text=event.text,
|
|
|
|
opaque=event.opaque
|
|
|
|
)
|
|
|
|
yield exception_event
|
|
|
|
except Empty:
|
|
|
|
continue
|
2019-03-18 06:29:38 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
self._cancel_stream(context)
|
|
|
|
|
2019-03-18 06:29:38 +00:00
|
|
|
def FileEvents(self, request, context):
|
|
|
|
session = self.get_session(request.id, context)
|
|
|
|
queue = Queue()
|
2019-04-05 04:52:20 +01:00
|
|
|
session.file_handlers.append(queue.put)
|
2019-03-18 06:29:38 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
while self._is_running(context):
|
2019-03-19 05:32:01 +00:00
|
|
|
try:
|
|
|
|
event = queue.get(timeout=1)
|
2019-03-22 23:16:30 +00:00
|
|
|
file_event = core_pb2.FileEvent(
|
2019-03-19 05:32:01 +00:00
|
|
|
message_type=event.message_type,
|
|
|
|
node=event.node,
|
|
|
|
name=event.name,
|
|
|
|
mode=event.mode,
|
|
|
|
number=event.number,
|
|
|
|
type=event.type,
|
|
|
|
source=event.source,
|
|
|
|
session=event.session,
|
|
|
|
data=event.data,
|
|
|
|
compressed_data=event.compressed_data
|
|
|
|
)
|
|
|
|
yield file_event
|
|
|
|
except Empty:
|
|
|
|
continue
|
2019-03-18 06:29:38 +00:00
|
|
|
|
2019-03-21 05:11:09 +00:00
|
|
|
self._cancel_stream(context)
|
|
|
|
|
2019-03-24 20:14:39 +00:00
|
|
|
def AddNode(self, request, context):
|
|
|
|
logging.debug("add node: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-02-26 06:45:57 +00:00
|
|
|
|
2019-03-29 04:47:30 +00:00
|
|
|
node_proto = request.node
|
|
|
|
node_id = node_proto.id
|
|
|
|
node_type = node_proto.type
|
2019-02-26 06:45:57 +00:00
|
|
|
if node_type is None:
|
|
|
|
node_type = NodeTypes.DEFAULT.value
|
|
|
|
node_type = NodeTypes(node_type)
|
|
|
|
|
2019-03-29 04:47:30 +00:00
|
|
|
node_options = NodeOptions(name=node_proto.name, model=node_proto.model)
|
|
|
|
node_options.icon = node_proto.icon
|
|
|
|
node_options.opaque = node_proto.opaque
|
|
|
|
node_options.services = node_proto.services
|
2019-02-26 06:45:57 +00:00
|
|
|
|
2019-03-29 04:47:30 +00:00
|
|
|
position = node_proto.position
|
2019-02-26 06:45:57 +00:00
|
|
|
node_options.set_position(position.x, position.y)
|
|
|
|
node_options.set_location(position.lat, position.lon, position.alt)
|
|
|
|
node = session.add_node(_type=node_type, _id=node_id, node_options=node_options)
|
|
|
|
|
|
|
|
# configure emane if provided
|
2019-03-29 04:47:30 +00:00
|
|
|
emane_model = node_proto.emane
|
2019-02-26 06:45:57 +00:00
|
|
|
if emane_model:
|
|
|
|
session.emane.set_model_config(node_id, emane_model)
|
|
|
|
|
2019-03-24 20:14:39 +00:00
|
|
|
return core_pb2.AddNodeResponse(id=node.objid)
|
2019-02-26 06:45:57 +00:00
|
|
|
|
2019-02-26 22:34:52 +00:00
|
|
|
def GetNode(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get node: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
|
|
|
node = self.get_node(session, request.id, context)
|
2019-02-26 22:34:52 +00:00
|
|
|
|
2019-03-22 23:16:30 +00:00
|
|
|
interfaces = []
|
2019-02-26 22:34:52 +00:00
|
|
|
for interface_id, interface in node._netif.iteritems():
|
|
|
|
net_id = None
|
|
|
|
if interface.net:
|
|
|
|
net_id = interface.net.objid
|
2019-03-22 23:16:30 +00:00
|
|
|
interface_proto = core_pb2.Interface(
|
|
|
|
id=interface_id, netid=net_id, name=interface.name, mac=str(interface.hwaddr),
|
|
|
|
mtu=interface.mtu, flowid=interface.flow_id)
|
|
|
|
interfaces.append(interface_proto)
|
2019-02-26 22:34:52 +00:00
|
|
|
|
|
|
|
emane_model = None
|
|
|
|
if nodeutils.is_node(node, NodeTypes.EMANE):
|
|
|
|
emane_model = node.model.name
|
|
|
|
|
|
|
|
services = [x.name for x in getattr(node, "services", [])]
|
2019-03-22 23:16:30 +00:00
|
|
|
position = core_pb2.Position(x=node.position.x, y=node.position.y, z=node.position.z)
|
|
|
|
node_type = nodeutils.get_node_type(node.__class__).value
|
|
|
|
node = core_pb2.Node(
|
2019-03-23 04:20:37 +00:00
|
|
|
id=node.objid, name=node.name, type=node_type, emane=emane_model, model=node.type, position=position,
|
|
|
|
services=services)
|
2019-03-22 23:16:30 +00:00
|
|
|
|
|
|
|
return core_pb2.GetNodeResponse(node=node, interfaces=interfaces)
|
2019-02-26 22:34:52 +00:00
|
|
|
|
2019-02-26 06:45:57 +00:00
|
|
|
def EditNode(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("edit node: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-02-26 06:45:57 +00:00
|
|
|
node_id = request.id
|
|
|
|
node_options = NodeOptions()
|
|
|
|
x = request.position.x
|
|
|
|
y = request.position.y
|
|
|
|
node_options.set_position(x, y)
|
|
|
|
lat = request.position.lat
|
|
|
|
lon = request.position.lon
|
|
|
|
alt = request.position.alt
|
|
|
|
node_options.set_location(lat, lon, alt)
|
|
|
|
result = session.update_node(node_id, node_options)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.EditNodeResponse(result=result)
|
2019-02-26 06:45:57 +00:00
|
|
|
|
2019-02-26 22:34:52 +00:00
|
|
|
def DeleteNode(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("delete node: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-23 00:07:54 +00:00
|
|
|
result = session.delete_node(request.id)
|
|
|
|
return core_pb2.DeleteNodeResponse(result=result)
|
2019-02-26 22:34:52 +00:00
|
|
|
|
2019-03-02 06:14:16 +00:00
|
|
|
def GetNodeLinks(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get node links: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
|
|
|
node = self.get_node(session, request.id, context)
|
2019-03-23 04:06:10 +00:00
|
|
|
links = get_links(session, node)
|
|
|
|
return core_pb2.GetNodeLinksResponse(links=links)
|
2019-03-02 06:14:16 +00:00
|
|
|
|
2019-03-24 20:14:39 +00:00
|
|
|
def AddLink(self, request, context):
|
|
|
|
logging.debug("add link: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-26 05:00:08 +00:00
|
|
|
|
|
|
|
# validate node exist
|
|
|
|
self.get_node(session, request.link.node_one, context)
|
|
|
|
self.get_node(session, request.link.node_two, context)
|
2019-02-26 22:34:52 +00:00
|
|
|
node_one = request.link.node_one
|
|
|
|
node_two = request.link.node_two
|
|
|
|
|
|
|
|
interface_one = None
|
|
|
|
interface_one_data = request.link.interface_one
|
|
|
|
if interface_one_data:
|
|
|
|
name = interface_one_data.name
|
|
|
|
if name == "":
|
|
|
|
name = None
|
|
|
|
mac = interface_one_data.mac
|
|
|
|
if mac == "":
|
|
|
|
mac = None
|
2019-04-06 05:25:46 +01:00
|
|
|
else:
|
|
|
|
mac = MacAddress.from_string(mac)
|
2019-02-26 22:34:52 +00:00
|
|
|
interface_one = InterfaceData(
|
|
|
|
_id=interface_one_data.id,
|
|
|
|
name=name,
|
|
|
|
mac=mac,
|
|
|
|
ip4=interface_one_data.ip4,
|
|
|
|
ip4_mask=interface_one_data.ip4mask,
|
|
|
|
ip6=interface_one_data.ip6,
|
|
|
|
ip6_mask=interface_one_data.ip6mask,
|
|
|
|
)
|
|
|
|
|
|
|
|
interface_two = None
|
|
|
|
interface_two_data = request.link.interface_two
|
|
|
|
if interface_two_data:
|
|
|
|
name = interface_two_data.name
|
|
|
|
if name == "":
|
|
|
|
name = None
|
|
|
|
mac = interface_two_data.mac
|
|
|
|
if mac == "":
|
|
|
|
mac = None
|
2019-04-06 05:25:46 +01:00
|
|
|
else:
|
|
|
|
mac = MacAddress.from_string(mac)
|
2019-02-26 22:34:52 +00:00
|
|
|
interface_two = InterfaceData(
|
|
|
|
_id=interface_two_data.id,
|
|
|
|
name=name,
|
|
|
|
mac=mac,
|
|
|
|
ip4=interface_two_data.ip4,
|
|
|
|
ip4_mask=interface_two_data.ip4mask,
|
|
|
|
ip6=interface_two_data.ip6,
|
|
|
|
ip6_mask=interface_two_data.ip6mask,
|
|
|
|
)
|
|
|
|
|
|
|
|
link_type = None
|
|
|
|
link_type_value = request.link.type
|
|
|
|
if link_type_value is not None:
|
|
|
|
link_type = LinkTypes(link_type_value)
|
|
|
|
|
|
|
|
options_data = request.link.options
|
|
|
|
link_options = LinkOptions(_type=link_type)
|
|
|
|
if options_data:
|
|
|
|
link_options.delay = options_data.delay
|
|
|
|
link_options.bandwidth = options_data.bandwidth
|
|
|
|
link_options.per = options_data.per
|
|
|
|
link_options.dup = options_data.dup
|
|
|
|
link_options.jitter = options_data.jitter
|
|
|
|
link_options.mer = options_data.mer
|
|
|
|
link_options.burst = options_data.burst
|
|
|
|
link_options.mburst = options_data.mburst
|
|
|
|
link_options.unidirectional = options_data.unidirectional
|
|
|
|
link_options.key = options_data.key
|
|
|
|
link_options.opaque = options_data.opaque
|
|
|
|
|
|
|
|
session.add_link(node_one, node_two, interface_one, interface_two, link_options=link_options)
|
2019-03-24 20:14:39 +00:00
|
|
|
return core_pb2.AddLinkResponse(result=True)
|
2019-02-26 22:34:52 +00:00
|
|
|
|
|
|
|
def EditLink(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("edit link: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-02-26 22:34:52 +00:00
|
|
|
node_one = request.node_one
|
|
|
|
node_two = request.node_two
|
|
|
|
interface_one_id = request.interface_one
|
|
|
|
interface_two_id = request.interface_two
|
|
|
|
options_data = request.options
|
|
|
|
link_options = LinkOptions()
|
|
|
|
link_options.delay = options_data.delay
|
|
|
|
link_options.bandwidth = options_data.bandwidth
|
|
|
|
link_options.per = options_data.per
|
|
|
|
link_options.dup = options_data.dup
|
|
|
|
link_options.jitter = options_data.jitter
|
|
|
|
link_options.mer = options_data.mer
|
|
|
|
link_options.burst = options_data.burst
|
|
|
|
link_options.mburst = options_data.mburst
|
|
|
|
link_options.unidirectional = options_data.unidirectional
|
|
|
|
link_options.key = options_data.key
|
|
|
|
link_options.opaque = options_data.opaque
|
|
|
|
session.update_link(node_one, node_two, interface_one_id, interface_two_id, link_options)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.EditLinkResponse(result=True)
|
2019-02-26 22:34:52 +00:00
|
|
|
|
|
|
|
def DeleteLink(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("delete link: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-02-26 22:34:52 +00:00
|
|
|
node_one = request.node_one
|
|
|
|
node_two = request.node_two
|
|
|
|
interface_one = request.interface_one
|
|
|
|
interface_two = request.interface_two
|
|
|
|
session.delete_link(node_one, node_two, interface_one, interface_two)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.DeleteLinkResponse(result=True)
|
2019-02-26 22:34:52 +00:00
|
|
|
|
2019-03-16 19:33:24 +00:00
|
|
|
def GetHooks(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get hooks: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-23 00:07:54 +00:00
|
|
|
hooks = []
|
2019-03-16 19:33:24 +00:00
|
|
|
for state, state_hooks in session._hooks.iteritems():
|
|
|
|
for file_name, file_data in state_hooks:
|
2019-03-23 00:07:54 +00:00
|
|
|
hook = core_pb2.Hook(state=state, file=file_name, data=file_data)
|
|
|
|
hooks.append(hook)
|
|
|
|
return core_pb2.GetHooksResponse(hooks=hooks)
|
2019-03-16 19:33:24 +00:00
|
|
|
|
|
|
|
def AddHook(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("add hook: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-16 19:33:24 +00:00
|
|
|
hook = request.hook
|
|
|
|
session.add_hook(hook.state, hook.file, None, hook.data)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.AddHookResponse(result=True)
|
2019-03-16 19:33:24 +00:00
|
|
|
|
2019-03-17 22:28:51 +00:00
|
|
|
def GetMobilityConfigs(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get mobility configs: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-25 19:59:07 +00:00
|
|
|
response = core_pb2.GetMobilityConfigsResponse()
|
2019-03-17 22:28:51 +00:00
|
|
|
for node_id, model_config in session.mobility.node_configurations.iteritems():
|
|
|
|
if node_id == -1:
|
|
|
|
continue
|
|
|
|
for model_name in model_config.iterkeys():
|
|
|
|
if model_name != Ns2ScriptedMobility.name:
|
|
|
|
continue
|
|
|
|
config = session.mobility.get_model_config(node_id, model_name)
|
|
|
|
groups = get_config_groups(config, Ns2ScriptedMobility)
|
2019-03-25 19:59:07 +00:00
|
|
|
response.configs[node_id].groups.extend(groups)
|
|
|
|
return response
|
2019-03-17 22:28:51 +00:00
|
|
|
|
|
|
|
def GetMobilityConfig(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get mobility config: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-17 22:28:51 +00:00
|
|
|
config = session.mobility.get_model_config(request.id, Ns2ScriptedMobility.name)
|
|
|
|
groups = get_config_groups(config, Ns2ScriptedMobility)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.GetMobilityConfigResponse(groups=groups)
|
2019-03-17 22:28:51 +00:00
|
|
|
|
|
|
|
def SetMobilityConfig(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set mobility config: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-17 22:28:51 +00:00
|
|
|
session.mobility.set_model_config(request.id, Ns2ScriptedMobility.name, request.config)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.SetMobilityConfigResponse(result=True)
|
2019-03-17 22:28:51 +00:00
|
|
|
|
|
|
|
def MobilityAction(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("mobility action: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
|
|
|
node = self.get_node(session, request.id, context)
|
2019-03-23 00:07:54 +00:00
|
|
|
result = True
|
2019-03-17 22:28:51 +00:00
|
|
|
if request.action == core_pb2.MOBILITY_START:
|
|
|
|
node.mobility.start()
|
|
|
|
elif request.action == core_pb2.MOBILITY_PAUSE:
|
|
|
|
node.mobility.pause()
|
|
|
|
elif request.action == core_pb2.MOBILITY_STOP:
|
|
|
|
node.mobility.stop(move_initial=True)
|
|
|
|
else:
|
2019-03-23 00:07:54 +00:00
|
|
|
result = False
|
|
|
|
return core_pb2.MobilityActionResponse(result=result)
|
2019-03-17 22:28:51 +00:00
|
|
|
|
2019-03-02 06:14:16 +00:00
|
|
|
def GetServices(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get services: %s", request)
|
2019-03-23 00:07:54 +00:00
|
|
|
services = []
|
2019-03-02 06:14:16 +00:00
|
|
|
for service in ServiceManager.services.itervalues():
|
2019-03-23 00:07:54 +00:00
|
|
|
service_proto = core_pb2.Service(group=service.group, name=service.name)
|
|
|
|
services.append(service_proto)
|
|
|
|
return core_pb2.GetServicesResponse(services=services)
|
2019-03-02 06:14:16 +00:00
|
|
|
|
2019-03-15 05:33:10 +00:00
|
|
|
def GetServiceDefaults(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get service defaults: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-23 00:07:54 +00:00
|
|
|
all_service_defaults = []
|
2019-03-15 05:33:10 +00:00
|
|
|
for node_type in session.services.default_services:
|
|
|
|
services = session.services.default_services[node_type]
|
2019-03-23 00:07:54 +00:00
|
|
|
service_defaults = core_pb2.ServiceDefaults(node_type=node_type, services=services)
|
|
|
|
all_service_defaults.append(service_defaults)
|
|
|
|
return core_pb2.GetServiceDefaultsResponse(defaults=all_service_defaults)
|
2019-03-15 05:33:10 +00:00
|
|
|
|
|
|
|
def SetServiceDefaults(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set service defaults: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-15 05:33:10 +00:00
|
|
|
session.services.default_services.clear()
|
|
|
|
for service_defaults in request.defaults:
|
|
|
|
session.services.default_services[service_defaults.node_type] = service_defaults.services
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.SetServiceDefaultsResponse(result=True)
|
2019-03-15 05:33:10 +00:00
|
|
|
|
|
|
|
def GetNodeService(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get node service: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-17 04:40:15 +00:00
|
|
|
service = session.services.get_service(request.id, request.service, default_service=True)
|
2019-03-23 00:07:54 +00:00
|
|
|
service_proto = core_pb2.NodeServiceData(
|
|
|
|
executables=service.executables,
|
|
|
|
dependencies=service.dependencies,
|
|
|
|
dirs=service.dirs,
|
|
|
|
configs=service.configs,
|
|
|
|
startup=service.startup,
|
|
|
|
validate=service.validate,
|
|
|
|
validation_mode=service.validation_mode.value,
|
|
|
|
validation_timer=service.validation_timer,
|
|
|
|
shutdown=service.shutdown,
|
|
|
|
meta=service.meta
|
|
|
|
)
|
|
|
|
return core_pb2.GetNodeServiceResponse(service=service_proto)
|
2019-03-15 05:33:10 +00:00
|
|
|
|
2019-03-17 03:53:26 +00:00
|
|
|
def GetNodeServiceFile(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get node service file: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
|
|
|
node = self.get_node(session, request.id, context)
|
2019-03-17 03:53:26 +00:00
|
|
|
service = None
|
|
|
|
for current_service in node.services:
|
|
|
|
if current_service.name == request.service:
|
|
|
|
service = current_service
|
|
|
|
break
|
|
|
|
if not service:
|
2019-03-23 00:07:54 +00:00
|
|
|
context.abort(grpc.StatusCode.NOT_FOUND, "service not found")
|
2019-03-17 03:53:26 +00:00
|
|
|
file_data = session.services.get_service_file(node, request.service, request.file)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.GetNodeServiceFileResponse(data=file_data.data)
|
2019-03-17 03:53:26 +00:00
|
|
|
|
2019-03-17 04:40:15 +00:00
|
|
|
def SetNodeService(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set node service: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-17 04:40:15 +00:00
|
|
|
session.services.set_service(request.id, request.service)
|
|
|
|
service = session.services.get_service(request.id, request.service)
|
|
|
|
service.startup = tuple(request.startup)
|
|
|
|
service.validate = tuple(request.validate)
|
|
|
|
service.shutdown = tuple(request.shutdown)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.SetNodeServiceResponse(result=True)
|
2019-03-17 04:40:15 +00:00
|
|
|
|
|
|
|
def SetNodeServiceFile(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set node service file: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-17 04:40:15 +00:00
|
|
|
session.services.set_service_file(request.id, request.service, request.file, request.data)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.SetNodeServiceFileResponse(result=True)
|
2019-03-17 04:40:15 +00:00
|
|
|
|
2019-03-17 03:53:26 +00:00
|
|
|
def ServiceAction(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("service action: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
|
|
|
node = self.get_node(session, request.id, context)
|
2019-03-17 03:53:26 +00:00
|
|
|
service = None
|
|
|
|
for current_service in node.services:
|
|
|
|
if current_service.name == request.service:
|
|
|
|
service = current_service
|
|
|
|
break
|
|
|
|
|
|
|
|
if not service:
|
2019-03-23 00:07:54 +00:00
|
|
|
context.abort(grpc.StatusCode.NOT_FOUND, "service not found")
|
2019-03-17 03:53:26 +00:00
|
|
|
|
|
|
|
status = -1
|
2019-03-25 21:03:04 +00:00
|
|
|
if request.action == core_pb2.SERVICE_START:
|
2019-03-17 03:53:26 +00:00
|
|
|
status = session.services.startup_service(node, service, wait=True)
|
2019-03-25 21:03:04 +00:00
|
|
|
elif request.action == core_pb2.SERVICE_STOP:
|
2019-03-17 03:53:26 +00:00
|
|
|
status = session.services.stop_service(node, service)
|
2019-03-25 21:03:04 +00:00
|
|
|
elif request.action == core_pb2.SERVICE_RESTART:
|
2019-03-17 03:53:26 +00:00
|
|
|
status = session.services.stop_service(node, service)
|
|
|
|
if not status:
|
|
|
|
status = session.services.startup_service(node, service, wait=True)
|
2019-03-25 21:03:04 +00:00
|
|
|
elif request.action == core_pb2.SERVICE_VALIDATE:
|
2019-03-17 03:53:26 +00:00
|
|
|
status = session.services.validate_service(node, service)
|
|
|
|
|
2019-03-23 00:07:54 +00:00
|
|
|
result = False
|
2019-03-17 03:53:26 +00:00
|
|
|
if not status:
|
2019-03-23 00:07:54 +00:00
|
|
|
result = True
|
2019-03-17 03:53:26 +00:00
|
|
|
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.ServiceActionResponse(result=result)
|
2019-03-17 03:53:26 +00:00
|
|
|
|
2019-03-16 19:48:37 +00:00
|
|
|
def GetWlanConfig(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get wlan config: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-17 04:40:15 +00:00
|
|
|
config = session.mobility.get_model_config(request.id, BasicRangeModel.name)
|
2019-03-16 19:48:37 +00:00
|
|
|
groups = get_config_groups(config, BasicRangeModel)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.GetWlanConfigResponse(groups=groups)
|
2019-03-16 19:48:37 +00:00
|
|
|
|
|
|
|
def SetWlanConfig(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set wlan config: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-17 04:40:15 +00:00
|
|
|
session.mobility.set_model_config(request.id, BasicRangeModel.name, request.config)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.SetWlanConfigResponse(result=True)
|
2019-03-16 19:48:37 +00:00
|
|
|
|
2019-03-02 06:14:16 +00:00
|
|
|
def GetEmaneConfig(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get emane config: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-02 06:14:16 +00:00
|
|
|
config = session.emane.get_configs()
|
|
|
|
groups = get_config_groups(config, session.emane.emane_config)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.GetEmaneConfigResponse(groups=groups)
|
2019-03-02 06:14:16 +00:00
|
|
|
|
2019-03-16 05:03:37 +00:00
|
|
|
def SetEmaneConfig(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set emane config: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-24 21:13:04 +00:00
|
|
|
config = session.emane.get_configs()
|
|
|
|
config.update(request.config)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.SetEmaneConfigResponse(result=True)
|
2019-03-16 05:03:37 +00:00
|
|
|
|
2019-03-16 04:38:29 +00:00
|
|
|
def GetEmaneModels(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get emane models: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-16 04:38:29 +00:00
|
|
|
models = []
|
|
|
|
for model in session.emane.models.keys():
|
|
|
|
if len(model.split("_")) != 2:
|
|
|
|
continue
|
|
|
|
models.append(model)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.GetEmaneModelsResponse(models=models)
|
2019-03-16 04:38:29 +00:00
|
|
|
|
|
|
|
def GetEmaneModelConfig(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get emane model config: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-16 04:38:29 +00:00
|
|
|
model = session.emane.models[request.model]
|
2019-03-29 04:47:30 +00:00
|
|
|
_id = get_emane_model_id(request.id, request.interface)
|
|
|
|
config = session.emane.get_model_config(_id, request.model)
|
2019-03-16 04:38:29 +00:00
|
|
|
groups = get_config_groups(config, model)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.GetEmaneModelConfigResponse(groups=groups)
|
2019-03-16 04:38:29 +00:00
|
|
|
|
2019-03-16 05:03:37 +00:00
|
|
|
def SetEmaneModelConfig(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("set emane model config: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-29 04:47:30 +00:00
|
|
|
_id = get_emane_model_id(request.id, request.interface)
|
|
|
|
session.emane.set_model_config(_id, request.model, request.config)
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.SetEmaneModelConfigResponse(result=True)
|
2019-03-16 05:03:37 +00:00
|
|
|
|
2019-03-16 04:38:29 +00:00
|
|
|
def GetEmaneModelConfigs(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("get emane model configs: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-16 04:38:29 +00:00
|
|
|
response = core_pb2.GetEmaneModelConfigsResponse()
|
|
|
|
for node_id, model_config in session.emane.node_configurations.iteritems():
|
|
|
|
if node_id == -1:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for model_name in model_config.iterkeys():
|
|
|
|
model = session.emane.models[model_name]
|
|
|
|
config = session.emane.get_model_config(node_id, model_name)
|
|
|
|
config_groups = get_config_groups(config, model)
|
|
|
|
node_configurations = response.configs[node_id]
|
|
|
|
node_configurations.model = model_name
|
|
|
|
node_configurations.groups.extend(config_groups)
|
|
|
|
return response
|
|
|
|
|
2019-03-02 21:44:27 +00:00
|
|
|
def SaveXml(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("save xml: %s", request)
|
2019-03-17 22:53:29 +00:00
|
|
|
session = self.get_session(request.session, context)
|
2019-03-02 21:44:27 +00:00
|
|
|
|
|
|
|
_, temp_path = tempfile.mkstemp()
|
|
|
|
session.save_xml(temp_path)
|
|
|
|
|
|
|
|
with open(temp_path, "rb") as xml_file:
|
|
|
|
data = xml_file.read()
|
|
|
|
|
2019-03-23 00:07:54 +00:00
|
|
|
return core_pb2.SaveXmlResponse(data=data)
|
2019-03-02 21:44:27 +00:00
|
|
|
|
|
|
|
def OpenXml(self, request, context):
|
2019-03-20 04:43:11 +00:00
|
|
|
logging.debug("open xml: %s", request)
|
2019-03-02 21:44:27 +00:00
|
|
|
session = self.coreemu.create_session()
|
|
|
|
session.set_state(EventTypes.CONFIGURATION_STATE)
|
|
|
|
|
|
|
|
_, temp_path = tempfile.mkstemp()
|
|
|
|
with open(temp_path, "wb") as xml_file:
|
|
|
|
xml_file.write(request.data)
|
|
|
|
|
|
|
|
try:
|
|
|
|
session.open_xml(temp_path, start=True)
|
2019-04-08 18:39:36 +01:00
|
|
|
return core_pb2.OpenXmlResponse(session=session.id, result=True)
|
2019-03-23 00:07:54 +00:00
|
|
|
except IOError:
|
2019-03-02 21:44:27 +00:00
|
|
|
logging.exception("error opening session file")
|
2019-04-08 18:39:36 +01:00
|
|
|
self.coreemu.delete_session(session.id)
|
2019-03-23 00:07:54 +00:00
|
|
|
context.abort(grpc.StatusCode.INVALID_ARGUMENT, "invalid xml file")
|