fix p2p upstream link data not using enum, consolidated grpc logic for getting link protobufs

This commit is contained in:
Blake Harnden 2020-04-15 16:36:03 -07:00
parent e2490dee4a
commit 7e7bf8c7b7
5 changed files with 18 additions and 77 deletions

View file

@ -3,7 +3,7 @@ from queue import Empty, Queue
from typing import Iterable
from core.api.grpc import core_pb2
from core.api.grpc.grpcutils import convert_value
from core.api.grpc.grpcutils import convert_link
from core.emulator.data import (
ConfigData,
EventData,
@ -40,54 +40,7 @@ def handle_link_event(event: LinkData) -> core_pb2.LinkEvent:
:param event: link data
:return: link event that has message type and link information
"""
interface_one = None
if event.interface1_id is not None:
interface_one = core_pb2.Interface(
id=event.interface1_id,
name=event.interface1_name,
mac=convert_value(event.interface1_mac),
ip4=convert_value(event.interface1_ip4),
ip4mask=event.interface1_ip4_mask,
ip6=convert_value(event.interface1_ip6),
ip6mask=event.interface1_ip6_mask,
)
interface_two = None
if event.interface2_id is not None:
interface_two = core_pb2.Interface(
id=event.interface2_id,
name=event.interface2_name,
mac=convert_value(event.interface2_mac),
ip4=convert_value(event.interface2_ip4),
ip4mask=event.interface2_ip4_mask,
ip6=convert_value(event.interface2_ip6),
ip6mask=event.interface2_ip6_mask,
)
options = core_pb2.LinkOptions(
opaque=event.opaque,
jitter=event.jitter,
key=event.key,
mburst=event.mburst,
mer=event.mer,
per=event.per,
bandwidth=event.bandwidth,
burst=event.burst,
delay=event.delay,
dup=event.dup,
unidirectional=event.unidirectional,
)
link = core_pb2.Link(
type=event.link_type.value,
node_one_id=event.node1_id,
node_two_id=event.node2_id,
interface_one=interface_one,
interface_two=interface_two,
options=options,
network_id=event.network_id,
label=event.label,
color=event.color,
)
link = convert_link(event)
return core_pb2.LinkEvent(message_type=event.message_type.value, link=link)

View file

@ -13,7 +13,7 @@ from core.emulator.data import LinkData
from core.emulator.emudata import InterfaceData, LinkOptions, NodeOptions
from core.emulator.enumerations import LinkTypes, NodeTypes
from core.emulator.session import Session
from core.nodes.base import CoreNetworkBase, NodeBase
from core.nodes.base import NodeBase
from core.nodes.interface import CoreInterface
from core.services.coreservices import CoreService
@ -263,17 +263,16 @@ def get_node_proto(session: Session, node: NodeBase) -> core_pb2.Node:
)
def get_links(session: Session, node: NodeBase):
def get_links(node: NodeBase):
"""
Retrieve a list of links for grpc to use
Retrieve a list of links for grpc to use.
:param session: node's section
:param node: node to get links from
:return: [core.api.grpc.core_pb2.Link]
:return: protobuf links
"""
links = []
for link_data in node.all_link_data():
link = convert_link(session, link_data)
link = convert_link(link_data)
links.append(link)
return links
@ -307,48 +306,35 @@ def parse_emane_model_id(_id: int) -> Tuple[int, int]:
return node_id, interface
def convert_link(session: Session, link_data: LinkData) -> core_pb2.Link:
def convert_link(link_data: LinkData) -> core_pb2.Link:
"""
Convert link_data into core protobuf Link
Convert link_data into core protobuf link.
:param session:
:param link_data:
:param link_data: link to convert
:return: core protobuf Link
"""
interface_one = None
if link_data.interface1_id is not None:
node = session.get_node(link_data.node1_id)
interface_name = None
if not isinstance(node, CoreNetworkBase):
interface = node.netif(link_data.interface1_id)
interface_name = interface.name
interface_one = core_pb2.Interface(
id=link_data.interface1_id,
name=interface_name,
name=link_data.interface1_name,
mac=convert_value(link_data.interface1_mac),
ip4=convert_value(link_data.interface1_ip4),
ip4mask=link_data.interface1_ip4_mask,
ip6=convert_value(link_data.interface1_ip6),
ip6mask=link_data.interface1_ip6_mask,
)
interface_two = None
if link_data.interface2_id is not None:
node = session.get_node(link_data.node2_id)
interface_name = None
if not isinstance(node, CoreNetworkBase):
interface = node.netif(link_data.interface2_id)
interface_name = interface.name
interface_two = core_pb2.Interface(
id=link_data.interface2_id,
name=interface_name,
name=link_data.interface2_name,
mac=convert_value(link_data.interface2_mac),
ip4=convert_value(link_data.interface2_ip4),
ip4mask=link_data.interface2_ip4_mask,
ip6=convert_value(link_data.interface2_ip6),
ip6mask=link_data.interface2_ip6_mask,
)
options = core_pb2.LinkOptions(
opaque=link_data.opaque,
jitter=link_data.jitter,
@ -362,7 +348,6 @@ def convert_link(session: Session, link_data: LinkData) -> core_pb2.Link:
dup=link_data.dup,
unidirectional=link_data.unidirectional,
)
return core_pb2.Link(
type=link_data.link_type.value,
node_one_id=link_data.node1_id,

View file

@ -543,7 +543,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
continue
node_proto = grpcutils.get_node_proto(session, node)
nodes.append(node_proto)
node_links = get_links(session, node)
node_links = get_links(node)
links.extend(node_links)
session_proto = core_pb2.Session(
@ -788,7 +788,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
logging.debug("get node links: %s", request)
session = self.get_session(request.session_id, context)
node = self.get_node(session, request.node_id, context)
links = get_links(session, node)
links = get_links(node)
return core_pb2.GetNodeLinksResponse(links=links)
def AddLink(

View file

@ -1112,6 +1112,7 @@ class CoreNetworkBase(NodeBase):
link_type=self.linktype,
unidirectional=unidirectional,
interface2_id=linked_node.getifindex(netif),
interface2_name=netif.name,
interface2_mac=netif.hwaddr,
interface2_ip4=interface2_ip4,
interface2_ip4_mask=interface2_ip4_mask,

View file

@ -949,12 +949,14 @@ class PtpNet(CoreNetwork):
dup=if1.getparam("duplicate"),
jitter=if1.getparam("jitter"),
interface1_id=if1.node.getifindex(if1),
interface1_name=if1.name,
interface1_mac=if1.hwaddr,
interface1_ip4=interface1_ip4,
interface1_ip4_mask=interface1_ip4_mask,
interface1_ip6=interface1_ip6,
interface1_ip6_mask=interface1_ip6_mask,
interface2_id=if2.node.getifindex(if2),
interface2_name=if2.name,
interface2_mac=if2.hwaddr,
interface2_ip4=interface2_ip4,
interface2_ip4_mask=interface2_ip4_mask,
@ -968,7 +970,7 @@ class PtpNet(CoreNetwork):
# (swap if1 and if2)
if unidirectional:
link_data = LinkData(
message_type=0,
message_type=MessageFlags.NONE,
link_type=self.linktype,
node1_id=if2.node.id,
node2_id=if1.node.id,