grpc: removed get_node_links, added links to get_node instead

This commit is contained in:
Blake Harnden 2021-05-02 20:58:24 -07:00
parent f271b0289e
commit 38e162aec5
7 changed files with 28 additions and 136 deletions

View file

@ -531,20 +531,6 @@ class CoreGrpcClient:
)
return self.stub.GetNodeTerminal(request)
def get_node_links(
self, session_id: int, node_id: int
) -> core_pb2.GetNodeLinksResponse:
"""
Get current links for a node.
:param session_id: session id
:param node_id: node id
:return: response with a list of links
:raises grpc.RpcError: when session or node doesn't exist
"""
request = core_pb2.GetNodeLinksRequest(session_id=session_id, node_id=node_id)
return self.stub.GetNodeLinks(request)
def add_link(
self,
session_id: int,

View file

@ -550,7 +550,7 @@ class CoreGrpcClient:
def get_node(
self, session_id: int, node_id: int
) -> Tuple[wrappers.Node, List[wrappers.Interface]]:
) -> Tuple[wrappers.Node, List[wrappers.Interface], List[wrappers.Link]]:
"""
Get node details.
@ -566,7 +566,11 @@ class CoreGrpcClient:
for iface_proto in response.ifaces:
iface = wrappers.Interface.from_proto(iface_proto)
ifaces.append(iface)
return node, ifaces
links = []
for link_proto in response.links:
link = wrappers.Link.from_proto(link_proto)
links.append(link)
return node, ifaces, links
def edit_node(
self,

View file

@ -647,7 +647,8 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
iface_proto = grpcutils.iface_to_proto(request.node_id, iface)
ifaces.append(iface_proto)
node_proto = grpcutils.get_node_proto(session, node)
return core_pb2.GetNodeResponse(node=node_proto, ifaces=ifaces)
links = get_links(node)
return core_pb2.GetNodeResponse(node=node_proto, ifaces=ifaces, links=links)
def MoveNodes(
self,
@ -778,22 +779,6 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
terminal = node.termcmdstring("/bin/bash")
return core_pb2.GetNodeTerminalResponse(terminal=terminal)
def GetNodeLinks(
self, request: core_pb2.GetNodeLinksRequest, context: ServicerContext
) -> core_pb2.GetNodeLinksResponse:
"""
Retrieve all links form a requested node
:param request: get-node-links request
:param context: context object
:return: get-node-links response
"""
logger.debug("get node links: %s", request)
session = self.get_session(request.session_id, context)
node = self.get_node(session, request.node_id, context, NodeBase)
links = get_links(node)
return core_pb2.GetNodeLinksResponse(links=links)
def AddLink(
self, request: core_pb2.AddLinkRequest, context: ServicerContext
) -> core_pb2.AddLinkResponse:

View file

@ -740,20 +740,22 @@ class Node:
@dataclass
class Session:
id: int
state: SessionState
nodes: Dict[int, Node]
links: List[Link]
dir: str
user: str
default_services: Dict[str, Set[str]]
location: SessionLocation
hooks: Dict[str, Hook]
emane_models: List[str]
emane_config: Dict[str, ConfigOption]
metadata: Dict[str, str]
file: Path
options: Dict[str, ConfigOption]
id: int = None
state: SessionState = SessionState.DEFINITION
nodes: Dict[int, Node] = field(default_factory=dict)
links: List[Link] = field(default_factory=list)
dir: str = None
user: str = None
default_services: Dict[str, Set[str]] = field(default_factory=dict)
location: SessionLocation = SessionLocation(
x=0.0, y=0.0, z=0.0, lat=47.57917, lon=-122.13232, alt=2.0, scale=150.0
)
hooks: Dict[str, Hook] = field(default_factory=dict)
emane_models: List[str] = field(default_factory=list)
emane_config: Dict[str, ConfigOption] = field(default_factory=dict)
metadata: Dict[str, str] = field(default_factory=dict)
file: Path = None
options: Dict[str, ConfigOption] = field(default_factory=dict)
def set_node(self, node: Node) -> None:
self.nodes[node.id] = node

View file

@ -57,8 +57,6 @@ service CoreApi {
}
// link rpc
rpc GetNodeLinks (GetNodeLinksRequest) returns (GetNodeLinksResponse) {
}
rpc AddLink (AddLinkRequest) returns (AddLinkResponse) {
}
rpc EditLink (EditLinkRequest) returns (EditLinkResponse) {
@ -378,6 +376,7 @@ message GetNodeRequest {
message GetNodeResponse {
Node node = 1;
repeated Interface ifaces = 2;
repeated Link links = 3;
}
message EditNodeRequest {
@ -438,15 +437,6 @@ message NodeCommandResponse {
int32 return_code = 2;
}
message GetNodeLinksRequest {
int32 session_id = 1;
int32 node_id = 2;
}
message GetNodeLinksResponse {
repeated Link links = 1;
}
message AddLinkRequest {
int32 session_id = 1;
Link link = 2;

View file

@ -265,19 +265,6 @@ class TestGrpc:
assert response.node_id is not None
assert session.get_node(response.node_id, CoreNode) is not None
def test_get_node(self, grpc_server: CoreGrpcServer):
# given
client = CoreGrpcClient()
session = grpc_server.coreemu.create_session()
node = session.add_node(CoreNode)
# then
with client.context_connect():
response = client.get_node(session.id, node.id)
# then
assert response.node.id == node.id
def test_edit_node(self, grpc_server: CoreGrpcServer):
# given
client = CoreGrpcClient()
@ -379,38 +366,6 @@ class TestGrpc:
assert response.result is True
assert response.session_id is not None
def test_get_node_links(self, grpc_server: CoreGrpcServer, ip_prefixes: IpPrefixes):
# given
client = CoreGrpcClient()
session = grpc_server.coreemu.create_session()
switch = session.add_node(SwitchNode)
node = session.add_node(CoreNode)
iface_data = ip_prefixes.create_iface(node)
session.add_link(node.id, switch.id, iface_data)
# then
with client.context_connect():
response = client.get_node_links(session.id, switch.id)
# then
assert len(response.links) == 1
def test_get_node_links_exception(
self, grpc_server: CoreGrpcServer, ip_prefixes: IpPrefixes
):
# given
client = CoreGrpcClient()
session = grpc_server.coreemu.create_session()
switch = session.add_node(SwitchNode)
node = session.add_node(CoreNode)
iface_data = ip_prefixes.create_iface(node)
session.add_link(node.id, switch.id, iface_data)
# then
with pytest.raises(grpc.RpcError):
with client.context_connect():
client.get_node_links(session.id, 3)
def test_add_link(self, grpc_server: CoreGrpcServer, iface_helper: InterfaceHelper):
# given
client = CoreGrpcClient()

View file

@ -316,10 +316,12 @@ class TestGrpcw:
# then
with client.context_connect():
get_node, ifaces = client.get_node(session.id, node.id)
get_node, ifaces, links = client.get_node(session.id, node.id)
# then
assert node.id == get_node.id
assert len(ifaces) == 0
assert len(links) == 0
def test_edit_node(self, grpc_server: CoreGrpcServer):
# given
@ -436,38 +438,6 @@ class TestGrpcw:
assert result is True
assert session_id is not None
def test_get_node_links(self, grpc_server: CoreGrpcServer, ip_prefixes: IpPrefixes):
# given
client = CoreGrpcClient()
session = grpc_server.coreemu.create_session()
switch = session.add_node(SwitchNode)
node = session.add_node(CoreNode)
iface_data = ip_prefixes.create_iface(node)
session.add_link(node.id, switch.id, iface_data)
# then
with client.context_connect():
links = client.get_node_links(session.id, switch.id)
# then
assert len(links) == 1
def test_get_node_links_exception(
self, grpc_server: CoreGrpcServer, ip_prefixes: IpPrefixes
):
# given
client = CoreGrpcClient()
session = grpc_server.coreemu.create_session()
switch = session.add_node(SwitchNode)
node = session.add_node(CoreNode)
iface_data = ip_prefixes.create_iface(node)
session.add_link(node.id, switch.id, iface_data)
# then
with pytest.raises(grpc.RpcError):
with client.context_connect():
client.get_node_links(session.id, 3)
def test_add_link(self, grpc_server: CoreGrpcServer):
# given
client = CoreGrpcClient()