grpc added mobility get configs, get config, set config, and action

This commit is contained in:
bharnden 2019-03-17 15:28:51 -07:00
parent 9185c6b8eb
commit a07b0c8919
3 changed files with 183 additions and 124 deletions

View file

@ -257,6 +257,31 @@ class CoreApiClient(object):
request.hook.data = file_data request.hook.data = file_data
return self.stub.AddHook(request) return self.stub.AddHook(request)
def get_mobility_configs(self, session):
request = core_pb2.GetMobilityConfigsRequest()
request.session = session
return self.stub.GetMobilityConfigs(request)
def get_mobility_config(self, session, _id):
request = core_pb2.GetMobilityConfigRequest()
request.session = session
request.id = _id
return self.stub.GetMobilityConfig(request)
def set_mobility_config(self, session, _id, config):
request = core_pb2.SetMobilityConfigRequest()
request.session = session
request.id = _id
request.config.update(config)
return self.stub.SetMobilityConfig(request)
def mobility_action(self, session, _id, action):
request = core_pb2.MobilityActionRequest()
request.session = session
request.id = _id
request.action = action
return self.stub.MobilityAction(request)
def get_services(self): def get_services(self):
request = core_pb2.GetServicesRequest() request = core_pb2.GetServicesRequest()
return self.stub.GetServices(request) return self.stub.GetServices(request)

View file

@ -12,7 +12,7 @@ import core_pb2_grpc
from core.emulator.emudata import NodeOptions, InterfaceData, LinkOptions from core.emulator.emudata import NodeOptions, InterfaceData, LinkOptions
from core.enumerations import NodeTypes, EventTypes, LinkTypes from core.enumerations import NodeTypes, EventTypes, LinkTypes
from core.misc import nodeutils from core.misc import nodeutils
from core.mobility import BasicRangeModel from core.mobility import BasicRangeModel, Ns2ScriptedMobility
from core.service import ServiceManager from core.service import ServiceManager
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
@ -110,6 +110,18 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
super(CoreApiServer, self).__init__() super(CoreApiServer, self).__init__()
self.coreemu = coreemu self.coreemu = coreemu
def get_session(self, _id):
session = self.coreemu.sessions.get(_id)
if not session:
raise Exception("no session found")
return session
def get_node(self, session, _id):
node = session.get_object(_id)
if not node:
raise Exception("no node found")
return node
def CreateSession(self, request, context): def CreateSession(self, request, context):
session = self.coreemu.create_session() session = self.coreemu.create_session()
session.set_state(EventTypes.DEFINITION_STATE) session.set_state(EventTypes.DEFINITION_STATE)
@ -139,7 +151,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def GetSessionLocation(self, request, context): def GetSessionLocation(self, request, context):
session = self.coreemu.sessions.get(request.id) session = self.get_session(request.id)
x, y, z = session.location.refxyz x, y, z = session.location.refxyz
lat, lon, alt = session.location.refgeo lat, lon, alt = session.location.refgeo
response = core_pb2.GetSessionLocationResponse() response = core_pb2.GetSessionLocationResponse()
@ -156,7 +168,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def SetSessionLocation(self, request, context): def SetSessionLocation(self, request, context):
session = self.coreemu.sessions.get(request.id) session = self.get_session(request.id)
session.location.refxyz = (request.position.x, request.position.y, request.position.z) 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.setrefgeo(request.position.lat, request.position.lon, request.position.alt)
@ -168,7 +180,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
def SetSessionState(self, request, context): def SetSessionState(self, request, context):
response = core_pb2.SetSessionStateResponse() response = core_pb2.SetSessionStateResponse()
session = self.coreemu.sessions.get(request.id) session = self.get_session(request.id)
try: try:
state = EventTypes(request.state) state = EventTypes(request.state)
@ -193,7 +205,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def GetSessionOptions(self, request, context): def GetSessionOptions(self, request, context):
session = self.coreemu.sessions.get(request.id) session = self.get_session(request.id)
config = session.options.get_configs() config = session.options.get_configs()
defaults = session.options.default_values() defaults = session.options.default_values()
@ -206,20 +218,14 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def SetSessionOptions(self, request, context): def SetSessionOptions(self, request, context):
session = self.coreemu.sessions.get(request.id) session = self.get_session(request.id)
if not session:
raise Exception("no session found")
session.options.set_configs(request.config) session.options.set_configs(request.config)
response = core_pb2.SetSessionOptionsResponse() response = core_pb2.SetSessionOptionsResponse()
response.result = True response.result = True
return response return response
def GetSession(self, request, context): def GetSession(self, request, context):
session = self.coreemu.sessions.get(request.id) session = self.get_session(request.id)
if not session:
raise Exception("no session found")
response = core_pb2.GetSessionResponse() response = core_pb2.GetSessionResponse()
response.state = session.state response.state = session.state
@ -264,11 +270,8 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def NodeEvents(self, request, context): def NodeEvents(self, request, context):
session = self.coreemu.sessions.get(request.id) session = self.get_session(request.id)
if not session:
raise Exception("no session found")
queue = Queue() queue = Queue()
session.node_handlers.append(lambda x: queue.put(x)) session.node_handlers.append(lambda x: queue.put(x))
while True: while True:
@ -290,11 +293,8 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
yield node_event yield node_event
def SessionEvents(self, request, context): def SessionEvents(self, request, context):
session = self.coreemu.sessions.get(request.id) session = self.get_session(request.id)
if not session:
raise Exception("no session found")
queue = Queue() queue = Queue()
session.event_handlers.append(lambda x: queue.put(x)) session.event_handlers.append(lambda x: queue.put(x))
while True: while True:
@ -315,9 +315,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
yield session_event yield session_event
def CreateNode(self, request, context): def CreateNode(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
node_id = request.id node_id = request.id
node_type = request.type node_type = request.type
@ -346,13 +344,8 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def GetNode(self, request, context): def GetNode(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session: node = self.get_node(session, request.id)
raise Exception("no session found")
node = session.get_object(request.id)
if not node:
raise Exception("no node found")
response = core_pb2.GetNodeResponse() response = core_pb2.GetNodeResponse()
for interface_id, interface in node._netif.iteritems(): for interface_id, interface in node._netif.iteritems():
@ -392,9 +385,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def EditNode(self, request, context): def EditNode(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
node_id = request.id node_id = request.id
node_options = NodeOptions() node_options = NodeOptions()
@ -414,23 +405,15 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
def DeleteNode(self, request, context): def DeleteNode(self, request, context):
logging.info("delete node: %s", request) logging.info("delete node: %s", request)
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
response = core_pb2.DeleteNodeResponse() response = core_pb2.DeleteNodeResponse()
response.result = session.delete_node(request.id) response.result = session.delete_node(request.id)
return response return response
def GetNodeLinks(self, request, context): def GetNodeLinks(self, request, context):
logging.info("get node links: %s", request) logging.info("get node links: %s", request)
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session: node = self.get_node(session, request.id)
raise Exception("no session found")
node = session.get_object(request.id)
if not node:
raise Exception("no node found")
response = core_pb2.GetNodeLinksResponse() response = core_pb2.GetNodeLinksResponse()
links_data = node.all_link_data(0) links_data = node.all_link_data(0)
for link_data in links_data: for link_data in links_data:
@ -440,12 +423,8 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def CreateLink(self, request, context): def CreateLink(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
logging.info("adding link: %s", request) logging.info("adding link: %s", request)
node_one = request.link.node_one node_one = request.link.node_one
node_two = request.link.node_two node_two = request.link.node_two
@ -515,9 +494,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
def EditLink(self, request, context): def EditLink(self, request, context):
logging.info("edit link: %s", request) logging.info("edit link: %s", request)
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
node_one = request.node_one node_one = request.node_one
node_two = request.node_two node_two = request.node_two
@ -546,9 +523,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
def DeleteLink(self, request, context): def DeleteLink(self, request, context):
logging.info("delete link: %s", request) logging.info("delete link: %s", request)
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
node_one = request.node_one node_one = request.node_one
node_two = request.node_two node_two = request.node_two
@ -561,9 +536,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def GetHooks(self, request, context): def GetHooks(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
response = core_pb2.GetHooksResponse() response = core_pb2.GetHooksResponse()
for state, state_hooks in session._hooks.iteritems(): for state, state_hooks in session._hooks.iteritems():
@ -576,9 +549,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def AddHook(self, request, context): def AddHook(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
hook = request.hook hook = request.hook
session.add_hook(hook.state, hook.file, None, hook.data) session.add_hook(hook.state, hook.file, None, hook.data)
@ -586,6 +557,56 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
response.result = True response.result = True
return response return response
def GetMobilityConfigs(self, request, context):
session = self.get_session(request.session)
response = core_pb2.GetMobilityConfigsResponse()
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)
mobility_config = response.configs[node_id]
mobility_config.groups.extend(groups)
return response
def GetMobilityConfig(self, request, context):
session = self.get_session(request.session)
config = session.mobility.get_model_config(request.id, Ns2ScriptedMobility.name)
groups = get_config_groups(config, Ns2ScriptedMobility)
response = core_pb2.GetMobilityConfigResponse()
response.groups.extend(groups)
return response
def SetMobilityConfig(self, request, context):
session = self.get_session(request.session)
session.mobility.set_model_config(request.id, Ns2ScriptedMobility.name, request.config)
response = core_pb2.SetMobilityConfigResponse()
response.result = True
return response
def MobilityAction(self, request, context):
session = self.get_session(request.session)
node = self.get_node(session, request.id)
response = core_pb2.MobilityActionResponse()
response.result = True
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:
response.result = False
return response
def GetServices(self, request, context): def GetServices(self, request, context):
response = core_pb2.GetServicesResponse() response = core_pb2.GetServicesResponse()
for service in ServiceManager.services.itervalues(): for service in ServiceManager.services.itervalues():
@ -595,9 +616,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def GetServiceDefaults(self, request, context): def GetServiceDefaults(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
response = core_pb2.GetServiceDefaultsResponse() response = core_pb2.GetServiceDefaultsResponse()
for node_type in session.services.default_services: for node_type in session.services.default_services:
@ -608,10 +627,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def SetServiceDefaults(self, request, context): def SetServiceDefaults(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
session.services.default_services.clear() session.services.default_services.clear()
for service_defaults in request.defaults: for service_defaults in request.defaults:
session.services.default_services[service_defaults.node_type] = service_defaults.services session.services.default_services[service_defaults.node_type] = service_defaults.services
@ -621,9 +637,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def GetNodeService(self, request, context): def GetNodeService(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
service = session.services.get_service(request.id, request.service, default_service=True) service = session.services.get_service(request.id, request.service, default_service=True)
response = core_pb2.GetNodeServiceResponse() response = core_pb2.GetNodeServiceResponse()
@ -641,12 +655,8 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def GetNodeServiceFile(self, request, context): def GetNodeServiceFile(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session: node = self.get_node(session, request.id)
raise Exception("no session found")
node = session.get_object(request.id)
if not node:
raise Exception("no node found")
service = None service = None
for current_service in node.services: for current_service in node.services:
@ -662,9 +672,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def SetNodeService(self, request, context): def SetNodeService(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
# guarantee custom service exists # guarantee custom service exists
session.services.set_service(request.id, request.service) session.services.set_service(request.id, request.service)
@ -681,23 +689,15 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def SetNodeServiceFile(self, request, context): def SetNodeServiceFile(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
session.services.set_service_file(request.id, request.service, request.file, request.data) session.services.set_service_file(request.id, request.service, request.file, request.data)
response = core_pb2.SetNodeServiceFileResponse() response = core_pb2.SetNodeServiceFileResponse()
response.result = True response.result = True
return response return response
def ServiceAction(self, request, context): def ServiceAction(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session: node = self.get_node(session, request.id)
raise Exception("no session found")
node = session.get_object(request.id)
if not node:
raise Exception("no node found")
service = None service = None
for current_service in node.services: for current_service in node.services:
@ -729,10 +729,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def GetWlanConfig(self, request, context): def GetWlanConfig(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
config = session.mobility.get_model_config(request.id, BasicRangeModel.name) config = session.mobility.get_model_config(request.id, BasicRangeModel.name)
groups = get_config_groups(config, BasicRangeModel) groups = get_config_groups(config, BasicRangeModel)
response = core_pb2.GetWlanConfigResponse() response = core_pb2.GetWlanConfigResponse()
@ -740,19 +737,14 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def SetWlanConfig(self, request, context): def SetWlanConfig(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
session.mobility.set_model_config(request.id, BasicRangeModel.name, request.config) session.mobility.set_model_config(request.id, BasicRangeModel.name, request.config)
response = core_pb2.SetWlanConfigResponse() response = core_pb2.SetWlanConfigResponse()
response.result = True response.result = True
return response return response
def GetEmaneConfig(self, request, context): def GetEmaneConfig(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
config = session.emane.get_configs() config = session.emane.get_configs()
groups = get_config_groups(config, session.emane.emane_config) groups = get_config_groups(config, session.emane.emane_config)
response = core_pb2.GetEmaneConfigResponse() response = core_pb2.GetEmaneConfigResponse()
@ -760,19 +752,14 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def SetEmaneConfig(self, request, context): def SetEmaneConfig(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
session.emane.set_configs(request.config) session.emane.set_configs(request.config)
response = core_pb2.SetEmaneConfigResponse() response = core_pb2.SetEmaneConfigResponse()
response.result = True response.result = True
return response return response
def GetEmaneModels(self, request, context): def GetEmaneModels(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
models = [] models = []
for model in session.emane.models.keys(): for model in session.emane.models.keys():
@ -785,10 +772,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def GetEmaneModelConfig(self, request, context): def GetEmaneModelConfig(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
model = session.emane.models[request.model] model = session.emane.models[request.model]
config = session.emane.get_model_config(request.id, request.model) config = session.emane.get_model_config(request.id, request.model)
groups = get_config_groups(config, model) groups = get_config_groups(config, model)
@ -797,19 +781,14 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def SetEmaneModelConfig(self, request, context): def SetEmaneModelConfig(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
session.emane.set_model_config(request.id, request.model, request.config) session.emane.set_model_config(request.id, request.model, request.config)
response = core_pb2.SetEmaneModelConfigResponse() response = core_pb2.SetEmaneModelConfigResponse()
response.result = True response.result = True
return response return response
def GetEmaneModelConfigs(self, request, context): def GetEmaneModelConfigs(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
response = core_pb2.GetEmaneModelConfigsResponse() response = core_pb2.GetEmaneModelConfigsResponse()
for node_id, model_config in session.emane.node_configurations.iteritems(): for node_id, model_config in session.emane.node_configurations.iteritems():
@ -828,9 +807,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response return response
def SaveXml(self, request, context): def SaveXml(self, request, context):
session = self.coreemu.sessions.get(request.session) session = self.get_session(request.session)
if not session:
raise Exception("no session found")
_, temp_path = tempfile.mkstemp() _, temp_path = tempfile.mkstemp()
session.save_xml(temp_path) session.save_xml(temp_path)

View file

@ -55,6 +55,17 @@ service CoreApi {
rpc AddHook (AddHookRequest) returns (AddHookResponse) { rpc AddHook (AddHookRequest) returns (AddHookResponse) {
} }
// mobility rpc
rpc GetMobilityConfigs (GetMobilityConfigsRequest) returns (GetMobilityConfigsResponse) {
}
rpc GetMobilityConfig (GetMobilityConfigRequest) returns (GetMobilityConfigResponse) {
}
rpc SetMobilityConfig (SetMobilityConfigRequest) returns (SetMobilityConfigResponse) {
}
rpc MobilityAction (MobilityActionRequest) returns (MobilityActionResponse) {
}
// service rpc // service rpc
rpc GetServices (GetServicesRequest) returns (GetServicesResponse) { rpc GetServices (GetServicesRequest) returns (GetServicesResponse) {
} }
@ -307,6 +318,46 @@ message AddHookResponse {
bool result = 1; bool result = 1;
} }
message GetMobilityConfigsRequest {
int32 session = 1;
}
message GetMobilityConfigsResponse {
message MobilityConfig {
repeated ConfigGroup groups = 1;
}
map<int32, MobilityConfig> configs = 1;
}
message GetMobilityConfigRequest {
int32 session = 1;
int32 id = 2;
}
message GetMobilityConfigResponse {
repeated ConfigGroup groups = 1;
}
message SetMobilityConfigRequest {
int32 session = 1;
int32 id = 2;
map<string, string> config = 3;
}
message SetMobilityConfigResponse {
bool result = 1;
}
message MobilityActionRequest {
int32 session = 1;
int32 id = 2;
MobilityAction action = 3;
}
message MobilityActionResponse {
bool result = 1;
}
message GetServicesRequest { message GetServicesRequest {
} }
@ -524,6 +575,12 @@ enum ServiceAction {
VALIDATE = 3; VALIDATE = 3;
} }
enum MobilityAction {
MOBILITY_START = 0;
MOBILITY_PAUSE = 1;
MOBILITY_STOP = 2;
}
message Hook { message Hook {
SessionState state = 1; SessionState state = 1;
string file = 2; string file = 2;