grpc updates to use proto values from client, rather than trying to squeeze in existing data types
This commit is contained in:
parent
cb8630bb03
commit
5161e4812a
4 changed files with 52 additions and 70 deletions
|
@ -169,12 +169,12 @@ class CoreGrpcClient(object):
|
|||
Set session state.
|
||||
|
||||
:param int _id: id of session
|
||||
:param EventTypes state: session state to transition to
|
||||
:param core_pb2.SessionState state: session state to transition to
|
||||
:return: response with result of success or failure
|
||||
:rtype: core_pb2.SetSessionStateResponse
|
||||
:raises grpc.RpcError: when session doesn't exist
|
||||
"""
|
||||
request = core_pb2.SetSessionStateRequest(id=_id, state=state.value)
|
||||
request = core_pb2.SetSessionStateRequest(id=_id, state=state)
|
||||
return self.stub.SetSessionState(request)
|
||||
|
||||
def node_events(self, _id, handler):
|
||||
|
@ -255,28 +255,17 @@ class CoreGrpcClient(object):
|
|||
stream = self.stub.FileEvents(request)
|
||||
start_streamer(stream, handler)
|
||||
|
||||
def add_node(self, session, _type=NodeTypes.DEFAULT, _id=None, node_options=None, emane=None):
|
||||
def add_node(self, session, node):
|
||||
"""
|
||||
Add node to session.
|
||||
|
||||
:param int session: session id
|
||||
:param NodeTypes _type: type of node to create
|
||||
:param int _id: id for node, defaults to None, which will generate an id
|
||||
:param NodeOptions node_options: options for node including position, services, and model
|
||||
:param str emane: emane model, if an emane node
|
||||
:param core_pb2.Node: node to add
|
||||
:return: response with node id
|
||||
:rtype: core_pb2.AddNodeResponse
|
||||
:raises grpc.RpcError: when session doesn't exist
|
||||
"""
|
||||
if not node_options:
|
||||
node_options = NodeOptions()
|
||||
position = core_pb2.Position(
|
||||
x=node_options.x, y=node_options.y,
|
||||
lat=node_options.lat, lon=node_options.lon, alt=node_options.alt)
|
||||
request = core_pb2.AddNodeRequest(
|
||||
session=session, type=_type.value, name=node_options.name,
|
||||
model=node_options.model, icon=node_options.icon, services=node_options.services,
|
||||
opaque=node_options.opaque, emane=emane, position=position)
|
||||
request = core_pb2.AddNodeRequest(session=session, node=node)
|
||||
return self.stub.AddNode(request)
|
||||
|
||||
def get_node(self, session, _id):
|
||||
|
@ -446,7 +435,7 @@ class CoreGrpcClient(object):
|
|||
return self.stub.GetHooks(request)
|
||||
|
||||
def add_hook(self, session, state, file_name, file_data):
|
||||
hook = core_pb2.Hook(state=state.value, file=file_name, data=file_data)
|
||||
hook = core_pb2.Hook(state=state, file=file_name, data=file_data)
|
||||
request = core_pb2.AddHookRequest(session=session, hook=hook)
|
||||
return self.stub.AddHook(request)
|
||||
|
||||
|
@ -525,16 +514,13 @@ class CoreGrpcClient(object):
|
|||
request = core_pb2.GetEmaneModelsRequest(session=session)
|
||||
return self.stub.GetEmaneModels(request)
|
||||
|
||||
def get_emane_model_config(self, session, _id, model, interface_id=None):
|
||||
if interface_id is not None:
|
||||
_id = _id * 1000 + interface_id
|
||||
request = core_pb2.GetEmaneModelConfigRequest(session=session, id=_id, model=model)
|
||||
def get_emane_model_config(self, session, _id, model, interface_id=-1):
|
||||
request = core_pb2.GetEmaneModelConfigRequest(session=session, id=_id, model=model, interface=interface_id)
|
||||
return self.stub.GetEmaneModelConfig(request)
|
||||
|
||||
def set_emane_model_config(self, session, _id, model, config, interface_id=None):
|
||||
if interface_id is not None:
|
||||
_id = _id * 1000 + interface_id
|
||||
request = core_pb2.SetEmaneModelConfigRequest(session=session, id=_id, model=model, config=config)
|
||||
def set_emane_model_config(self, session, _id, model, config, interface_id=-1):
|
||||
request = core_pb2.SetEmaneModelConfigRequest(
|
||||
session=session, id=_id, model=model, config=config, interface=interface_id)
|
||||
return self.stub.SetEmaneModelConfig(request)
|
||||
|
||||
def get_emane_model_configs(self, session):
|
||||
|
@ -593,7 +579,7 @@ def main():
|
|||
print("created session: {}".format(session_data))
|
||||
print("default services: {}".format(client.get_service_defaults(session_data.id)))
|
||||
print("emane models: {}".format(client.get_emane_models(session_data.id)))
|
||||
print("add hook: {}".format(client.add_hook(session_data.id, EventTypes.RUNTIME_STATE, "test", "echo hello")))
|
||||
print("add hook: {}".format(client.add_hook(session_data.id, core_pb2.STATE_RUNTIME, "test", "echo hello")))
|
||||
print("hooks: {}".format(client.get_hooks(session_data.id)))
|
||||
|
||||
response = client.get_sessions()
|
||||
|
@ -618,10 +604,11 @@ def main():
|
|||
print("get location: {}".format(client.get_session_location(session_data.id)))
|
||||
|
||||
# change session state
|
||||
print("set session state: {}".format(client.set_session_state(session_data.id, EventTypes.CONFIGURATION_STATE)))
|
||||
print("set session state: {}".format(client.set_session_state(session_data.id, core_pb2.STATE_CONFIGURATION)))
|
||||
|
||||
# create switch node
|
||||
response = client.add_node(session_data.id, _type=NodeTypes.SWITCH)
|
||||
switch = core_pb2.Node(type=core_pb2.NODE_SWITCH)
|
||||
response = client.add_node(session_data.id, switch)
|
||||
print("created switch: {}".format(response))
|
||||
switch_id = response.id
|
||||
|
||||
|
@ -629,7 +616,8 @@ def main():
|
|||
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
|
||||
|
||||
for _ in xrange(2):
|
||||
response = client.add_node(session_data.id)
|
||||
node = core_pb2.Node()
|
||||
response = client.add_node(session_data.id, node)
|
||||
print("created node: {}".format(response))
|
||||
node_id = response.id
|
||||
node_options = NodeOptions()
|
||||
|
@ -657,7 +645,7 @@ def main():
|
|||
print("get node links: {}".format(client.get_node_links(session_data.id, node_id)))
|
||||
|
||||
# change session state
|
||||
print("set session state: {}".format(client.set_session_state(session_data.id, EventTypes.INSTANTIATION_STATE)))
|
||||
print("set session state: {}".format(client.set_session_state(session_data.id, core_pb2.STATE_INSTANTIATION)))
|
||||
# import pdb; pdb.set_trace()
|
||||
|
||||
# get session
|
||||
|
|
|
@ -57,6 +57,13 @@ def get_links(session, node):
|
|||
return links
|
||||
|
||||
|
||||
def get_emane_model_id(_id, interface):
|
||||
if interface >= 0:
|
||||
return _id * 1000 + interface
|
||||
else:
|
||||
return _id
|
||||
|
||||
|
||||
def convert_link(session, link_data):
|
||||
interface_one = None
|
||||
if link_data.interface1_id is not None:
|
||||
|
@ -427,24 +434,25 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
|
|||
logging.debug("add node: %s", request)
|
||||
session = self.get_session(request.session, context)
|
||||
|
||||
node_id = request.id
|
||||
node_type = request.type
|
||||
node_proto = request.node
|
||||
node_id = node_proto.id
|
||||
node_type = node_proto.type
|
||||
if node_type is None:
|
||||
node_type = NodeTypes.DEFAULT.value
|
||||
node_type = NodeTypes(node_type)
|
||||
|
||||
node_options = NodeOptions(name=request.name, model=request.model)
|
||||
node_options.icon = request.icon
|
||||
node_options.opaque = request.opaque
|
||||
node_options.services = request.services
|
||||
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
|
||||
|
||||
position = request.position
|
||||
position = node_proto.position
|
||||
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
|
||||
emane_model = request.emane
|
||||
emane_model = node_proto.emane
|
||||
if emane_model:
|
||||
session.emane.set_model_config(node_id, emane_model)
|
||||
|
||||
|
@ -816,14 +824,16 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
|
|||
logging.debug("get emane model config: %s", request)
|
||||
session = self.get_session(request.session, context)
|
||||
model = session.emane.models[request.model]
|
||||
config = session.emane.get_model_config(request.id, request.model)
|
||||
_id = get_emane_model_id(request.id, request.interface)
|
||||
config = session.emane.get_model_config(_id, request.model)
|
||||
groups = get_config_groups(config, model)
|
||||
return core_pb2.GetEmaneModelConfigResponse(groups=groups)
|
||||
|
||||
def SetEmaneModelConfig(self, request, context):
|
||||
logging.debug("set emane model config: %s", request)
|
||||
session = self.get_session(request.session, context)
|
||||
session.emane.set_model_config(request.id, request.model, request.config)
|
||||
_id = get_emane_model_id(request.id, request.interface)
|
||||
session.emane.set_model_config(_id, request.model, request.config)
|
||||
return core_pb2.SetEmaneModelConfigResponse(result=True)
|
||||
|
||||
def GetEmaneModelConfigs(self, request, context):
|
||||
|
|
|
@ -280,15 +280,7 @@ message FileEvent {
|
|||
|
||||
message AddNodeRequest {
|
||||
int32 session = 1;
|
||||
int32 id = 2;
|
||||
NodeType type = 3;
|
||||
string name = 4;
|
||||
string model = 5;
|
||||
string icon = 6;
|
||||
repeated string services = 7;
|
||||
Position position = 8;
|
||||
string emane = 9;
|
||||
string opaque = 10;
|
||||
Node node = 2;
|
||||
}
|
||||
|
||||
message AddNodeResponse {
|
||||
|
@ -553,7 +545,8 @@ message GetEmaneModelsResponse {
|
|||
message GetEmaneModelConfigRequest {
|
||||
int32 session = 1;
|
||||
int32 id = 2;
|
||||
string model = 3;
|
||||
int32 interface = 3;
|
||||
string model = 4;
|
||||
}
|
||||
|
||||
message GetEmaneModelConfigResponse {
|
||||
|
@ -563,8 +556,9 @@ message GetEmaneModelConfigResponse {
|
|||
message SetEmaneModelConfigRequest {
|
||||
int32 session = 1;
|
||||
int32 id = 2;
|
||||
string model = 3;
|
||||
map<string, string> config = 4;
|
||||
int32 interface = 3;
|
||||
string model = 4;
|
||||
map<string, string> config = 5;
|
||||
}
|
||||
|
||||
message SetEmaneModelConfigResponse {
|
||||
|
@ -724,11 +718,13 @@ message SessionSummary {
|
|||
message Node {
|
||||
int32 id = 1;
|
||||
string name = 2;
|
||||
int32 type = 3;
|
||||
NodeType type = 3;
|
||||
string model = 4;
|
||||
Position position = 5;
|
||||
repeated string services = 6;
|
||||
string emane = 7;
|
||||
string icon = 8;
|
||||
string opaque = 9;
|
||||
}
|
||||
|
||||
message Link {
|
||||
|
|
|
@ -14,19 +14,6 @@ from core.grpc import core_pb2
|
|||
from core.grpc.client import CoreGrpcClient
|
||||
from core.mobility import BasicRangeModel, Ns2ScriptedMobility
|
||||
|
||||
MODELS = [
|
||||
"router",
|
||||
"host",
|
||||
"PC",
|
||||
"mdr",
|
||||
]
|
||||
|
||||
NET_TYPES = [
|
||||
NodeTypes.SWITCH,
|
||||
NodeTypes.HUB,
|
||||
NodeTypes.WIRELESS_LAN
|
||||
]
|
||||
|
||||
|
||||
class TestGrpc:
|
||||
@pytest.mark.parametrize("session_id", [None, 6013])
|
||||
|
@ -178,11 +165,11 @@ class TestGrpc:
|
|||
|
||||
# then
|
||||
with client.context_connect():
|
||||
response = client.set_session_state(session.session_id, EventTypes.DEFINITION_STATE)
|
||||
response = client.set_session_state(session.session_id, core_pb2.STATE_DEFINITION)
|
||||
|
||||
# then
|
||||
assert response.result is True
|
||||
assert session.state == EventTypes.DEFINITION_STATE.value
|
||||
assert session.state == core_pb2.STATE_DEFINITION
|
||||
|
||||
def test_add_node(self, grpc_server):
|
||||
# given
|
||||
|
@ -191,7 +178,8 @@ class TestGrpc:
|
|||
|
||||
# then
|
||||
with client.context_connect():
|
||||
response = client.add_node(session.session_id)
|
||||
node = core_pb2.Node()
|
||||
response = client.add_node(session.session_id, node)
|
||||
|
||||
# then
|
||||
assert response.id is not None
|
||||
|
@ -281,7 +269,7 @@ class TestGrpc:
|
|||
file_name = "test"
|
||||
file_data = "echo hello"
|
||||
with client.context_connect():
|
||||
response = client.add_hook(session.session_id, EventTypes.RUNTIME_STATE, file_name, file_data)
|
||||
response = client.add_hook(session.session_id, core_pb2.STATE_RUNTIME, file_name, file_data)
|
||||
|
||||
# then
|
||||
assert response.result is True
|
||||
|
|
Loading…
Reference in a new issue