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

View file

@ -55,6 +55,17 @@ service CoreApi {
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
rpc GetServices (GetServicesRequest) returns (GetServicesResponse) {
}
@ -307,6 +318,46 @@ message AddHookResponse {
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 {
}
@ -524,6 +575,12 @@ enum ServiceAction {
VALIDATE = 3;
}
enum MobilityAction {
MOBILITY_START = 0;
MOBILITY_PAUSE = 1;
MOBILITY_STOP = 2;
}
message Hook {
SessionState state = 1;
string file = 2;