grpc added get node links, get services, get emane config

This commit is contained in:
bharnden 2019-03-01 22:14:16 -08:00
parent 460e5c04d0
commit 44f70d0c2e
3 changed files with 122 additions and 18 deletions

View file

@ -115,6 +115,12 @@ class CoreApiClient(object):
request.id = _id
return self.stub.DeleteNode(request)
def get_node_links(self, session, _id):
request = core_pb2.GetNodeLinksRequest()
request.session = session
request.id = _id
return self.stub.GetNodeLinks(request)
def create_link(self, session, node_one, node_two, interface_one=None, interface_two=None, link_options=None):
request = core_pb2.CreateLinkRequest()
request.session = session
@ -205,6 +211,15 @@ class CoreApiClient(object):
)
return self.stub.DeleteLink(request)
def get_services(self):
request = core_pb2.GetServicesRequest()
return self.stub.GetServices(request)
def get_emane_config(self, session):
request = core_pb2.GetEmaneConfigRequest()
request.session = session
return self.stub.GetEmaneConfig(request)
@contextmanager
def connect(self):
channel = grpc.insecure_channel(self.address)
@ -218,6 +233,8 @@ class CoreApiClient(object):
def main():
client = CoreApiClient()
with client.connect():
print("services: %s" % client.get_services())
# create session
response = client.create_session()
print("created session: %s" % response)
@ -228,6 +245,8 @@ def main():
if len(response.sessions) > 0:
session_data = response.sessions[0]
print("emane config: %s" % client.get_emane_config(session_data.id))
# set session location
response = client.set_session_location(
session_data.id,
@ -276,8 +295,11 @@ def main():
print("edit link: %s" % client.edit_link(
session_data.id, node_id, switch_id, link_options, interface_one=0))
print("get node links: %s" % client.get_node_links(session_data.id, node_id))
# change session state
print("set session state: %s" % client.set_session_state(session_data.id, EventTypes.INSTANTIATION_STATE))
# import pdb; pdb.set_trace()
# get session
print("get session: %s" % client.get_session(session_data.id))

View file

@ -12,6 +12,7 @@ import grpc
import core_pb2
import core_pb2_grpc
from core.misc import nodeutils
from core.service import ServiceManager
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
@ -30,6 +31,31 @@ def update_proto(obj, **kwargs):
setattr(obj, key, value)
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
config_group_proto = core_pb2.ConfigGroup()
config_group_proto.name = config_group.name
config_group_proto.options.extend(config_options[start: stop])
groups.append(config_group_proto)
return groups
def convert_link(session, link_data, link):
if link_data.interface1_id is not None:
node = session.get_object(link_data.node1_id)
@ -173,27 +199,12 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
def GetSessionOptions(self, request, context):
session = self.coreemu.sessions.get(request.id)
config = session.options.get_configs()
groups = get_config_groups(config, session.options)
response = core_pb2.SessionOptionsResponse()
config_options = []
for configuration in session.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 session.options.config_groups():
start = config_group.start - 1
stop = config_group.stop
config_group_proto = response.groups.add()
config_group_proto.name = config_group.name
config_group_proto.options.extend(config_options[start: stop])
response.groups.extend(groups)
return response
def GetSession(self, request, context):
@ -352,6 +363,23 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
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")
response = core_pb2.GetNodeLinksResponse()
links_data = node.all_link_data(0)
for link_data in links_data:
link = response.links.add()
convert_link(session, link_data, link)
return response
def CreateLink(self, request, context):
session = self.coreemu.sessions.get(request.session)
if not session:
@ -473,6 +501,24 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
response.result = True
return response
def GetServices(self, request, context):
response = core_pb2.GetServicesResponse()
for service in ServiceManager.services.itervalues():
service_proto = response.services.add()
service_proto.group = service.group
service_proto.name = service.name
return response
def GetEmaneConfig(self, request, context):
session = self.coreemu.sessions.get(request.session)
if not session:
raise Exception("no session found")
config = session.emane.get_configs()
groups = get_config_groups(config, session.emane.emane_config)
response = core_pb2.GetEmaneConfigResponse()
response.groups.extend(groups)
return response
def listen(coreemu, address="[::]:50051"):
logging.info("starting grpc api: %s", address)

View file

@ -27,12 +27,18 @@ service CoreApi {
}
rpc DeleteNode (DeleteNodeRequest) returns (DeleteNodeResponse) {
}
rpc GetNodeLinks (GetNodeLinksRequest) returns (GetNodeLinksResponse) {
}
rpc CreateLink (CreateLinkRequest) returns (CreateLinkResponse) {
}
rpc EditLink (EditLinkRequest) returns (EditLinkResponse) {
}
rpc DeleteLink (DeleteLinkRequest) returns (DeleteLinkResponse) {
}
rpc GetServices (GetServicesRequest) returns (GetServicesResponse) {
}
rpc GetEmaneConfig (GetEmaneConfigRequest) returns (GetEmaneConfigResponse) {
}
}
// rpc request/response messages
@ -151,6 +157,15 @@ message DeleteNodeResponse {
bool result = 1;
}
message GetNodeLinksRequest {
int32 session = 1;
int32 id = 2;
}
message GetNodeLinksResponse {
repeated Link links = 1;
}
message CreateLinkRequest {
int32 session = 1;
Link link = 2;
@ -185,6 +200,22 @@ message DeleteLinkResponse {
bool result = 1;
}
message GetServicesRequest {
}
message GetServicesResponse {
repeated Service services = 1;
}
message GetEmaneConfigRequest {
int32 session = 1;
}
message GetEmaneConfigResponse {
repeated ConfigGroup groups = 1;
}
// data structures for messages below
enum SessionState {
NONE = 0;
@ -213,6 +244,11 @@ enum NodeType {
EMANE_NET = 14;
}
message Service {
string group = 1;
string name = 2;
}
message ConfigGroup {
string name = 1;
repeated ConfigOption options = 2;