updates to use message flags enum directly

This commit is contained in:
Blake Harnden 2020-03-12 23:12:17 -07:00
parent 5cdfd8d8b9
commit a7790185d4
19 changed files with 65 additions and 60 deletions

View file

@ -87,7 +87,7 @@ def handle_link_event(event: LinkData) -> core_pb2.LinkEvent:
interface_two=interface_two,
options=options,
)
return core_pb2.LinkEvent(message_type=event.message_type, link=link)
return core_pb2.LinkEvent(message_type=event.message_type.value, link=link)
def handle_session_event(event: EventData) -> core_pb2.SessionEvent:
@ -158,7 +158,7 @@ def handle_file_event(event: FileData) -> core_pb2.FileEvent:
:return: file event
"""
return core_pb2.FileEvent(
message_type=event.message_type,
message_type=event.message_type.value,
node_id=event.node,
name=event.name,
mode=event.mode,

View file

@ -229,7 +229,7 @@ def get_links(session: Session, node: NodeBase):
:return: [core.api.grpc.core_pb2.Link]
"""
links = []
for link_data in node.all_link_data(0):
for link_data in node.all_link_data():
link = convert_link(session, link_data)
links.append(link)
return links

View file

@ -722,7 +722,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
if request.source:
source = request.source
if not has_geo:
node_data = node.data(0, source=source)
node_data = node.data(source=source)
session.broadcast_node(node_data)
except CoreError:
result = False
@ -1496,9 +1496,9 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
if emane_one.id == emane_two.id:
if request.linked:
flag = MessageFlags.ADD.value
flag = MessageFlags.ADD
else:
flag = MessageFlags.DELETE.value
flag = MessageFlags.DELETE
link = LinkData(
message_type=flag,
link_type=LinkTypes.WIRELESS,

View file

@ -265,7 +265,7 @@ class CoreHandler(socketserver.BaseRequestHandler):
(FileTlvs.COMPRESSED_DATA, file_data.compressed_data),
],
)
message = coreapi.CoreFileMessage.pack(file_data.message_type, tlv_data)
message = coreapi.CoreFileMessage.pack(file_data.message_type.value, tlv_data)
try:
self.sendall(message)
@ -380,7 +380,7 @@ class CoreHandler(socketserver.BaseRequestHandler):
],
)
message = coreapi.CoreLinkMessage.pack(link_data.message_type, tlv_data)
message = coreapi.CoreLinkMessage.pack(link_data.message_type.value, tlv_data)
try:
self.sendall(message)
@ -1841,11 +1841,11 @@ class CoreHandler(socketserver.BaseRequestHandler):
with self.session._nodes_lock:
for node_id in self.session.nodes:
node = self.session.nodes[node_id]
node_data = node.data(message_type=MessageFlags.ADD.value)
node_data = node.data(message_type=MessageFlags.ADD)
if node_data:
nodes_data.append(node_data)
node_links = node.all_link_data(flags=MessageFlags.ADD.value)
node_links = node.all_link_data(flags=MessageFlags.ADD)
for link_data in node_links:
links_data.append(link_data)
@ -1913,7 +1913,7 @@ class CoreHandler(socketserver.BaseRequestHandler):
for file_name, config_data in self.session.services.all_files(service):
file_data = FileData(
message_type=MessageFlags.ADD.value,
message_type=MessageFlags.ADD,
node=node_id,
name=str(file_name),
type=opaque,
@ -1927,7 +1927,7 @@ class CoreHandler(socketserver.BaseRequestHandler):
for state in sorted(self.session._hooks.keys()):
for file_name, config_data in self.session._hooks[state]:
file_data = FileData(
message_type=MessageFlags.ADD.value,
message_type=MessageFlags.ADD,
name=str(file_name),
type=f"hook:{state}",
data=str(config_data),

View file

@ -38,7 +38,7 @@ def convert_node(node_data):
(NodeTlvs.OPAQUE, node_data.opaque),
],
)
return coreapi.CoreNodeMessage.pack(node_data.message_type, tlv_data)
return coreapi.CoreNodeMessage.pack(node_data.message_type.value, tlv_data)
def convert_config(config_data):

View file

@ -806,7 +806,7 @@ class EmaneManager(ModelManager):
# don"t use node.setposition(x,y,z) which generates an event
node.position.set(x, y, z)
node_data = node.data(message_type=0, lat=lat, lon=lon, alt=alt)
node_data = node.data(lat=lat, lon=lon, alt=alt)
self.session.broadcast_node(node_data)
return True

View file

@ -30,6 +30,7 @@ class MessageFlags(Enum):
CORE message flags.
"""
NONE = 0x00
ADD = 0x01
DELETE = 0x02
CRI = 0x04

View file

@ -220,7 +220,7 @@ class WirelessModel(ConfigurableOptions):
self.session = session
self.id = _id
def all_link_data(self, flags: int) -> List:
def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List:
"""
May be used if the model can populate the GUI with wireless (green)
link lines.
@ -484,7 +484,10 @@ class BasicRangeModel(WirelessModel):
return True
def create_link_data(
self, interface1: CoreInterface, interface2: CoreInterface, message_type: int
self,
interface1: CoreInterface,
interface2: CoreInterface,
message_type: MessageFlags,
) -> LinkData:
"""
Create a wireless link/unlink data message.
@ -514,14 +517,14 @@ class BasicRangeModel(WirelessModel):
:return: nothing
"""
if unlink:
message_type = MessageFlags.DELETE.value
message_type = MessageFlags.DELETE
else:
message_type = MessageFlags.ADD.value
message_type = MessageFlags.ADD
link_data = self.create_link_data(netif, netif2, message_type)
self.session.broadcast_link(link_data)
def all_link_data(self, flags: int) -> List[LinkData]:
def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]:
"""
Return a list of wireless link messages for when the GUI reconnects.
@ -816,7 +819,7 @@ class WayPointMobility(WirelessModel):
:return: nothing
"""
node.position.set(x, y, z)
node_data = node.data(message_type=0)
node_data = node.data()
self.session.broadcast_node(node_data)
def setendtime(self) -> None:

View file

@ -14,7 +14,7 @@ from core import utils
from core.configservice.dependencies import ConfigServiceDependencies
from core.constants import MOUNT_BIN, VNODED_BIN
from core.emulator.data import LinkData, NodeData
from core.emulator.enumerations import LinkTypes, NodeTypes
from core.emulator.enumerations import LinkTypes, MessageFlags, NodeTypes
from core.errors import CoreCommandError, CoreError
from core.nodes import client
from core.nodes.interface import CoreInterface, TunTap, Veth
@ -193,7 +193,7 @@ class NodeBase:
def data(
self,
message_type: int,
message_type: MessageFlags = MessageFlags.NONE,
lat: float = None,
lon: float = None,
alt: float = None,
@ -244,7 +244,7 @@ class NodeBase:
return node_data
def all_link_data(self, flags: int) -> List:
def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]:
"""
Build CORE Link data for this object. There is no default
method for PyCoreObjs as PyCoreNodes do not implement this but
@ -1069,7 +1069,7 @@ class CoreNetworkBase(NodeBase):
with self._linked_lock:
del self._linked[netif]
def all_link_data(self, flags: int) -> List[LinkData]:
def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]:
"""
Build link data objects for this network. Each link object describes a link
between this network and a node.

View file

@ -7,6 +7,7 @@ import time
from typing import TYPE_CHECKING, Callable, Dict, List, Tuple
from core import utils
from core.emulator.enumerations import MessageFlags
from core.errors import CoreCommandError
from core.nodes.netclient import get_net_client
@ -554,7 +555,7 @@ class GreTap(CoreInterface):
"""
return None
def all_link_data(self, flags: int) -> List:
def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List:
"""
Retrieve link data.

View file

@ -12,7 +12,7 @@ import netaddr
from core import utils
from core.constants import EBTABLES_BIN, TC_BIN
from core.emulator.data import LinkData, NodeData
from core.emulator.enumerations import LinkTypes, NodeTypes, RegisterTlvs
from core.emulator.enumerations import LinkTypes, MessageFlags, NodeTypes, RegisterTlvs
from core.errors import CoreCommandError, CoreError
from core.nodes.base import CoreNetworkBase
from core.nodes.interface import CoreInterface, GreTap, Veth
@ -848,7 +848,7 @@ class CtrlNet(CoreNetwork):
super().shutdown()
def all_link_data(self, flags: int) -> List[LinkData]:
def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]:
"""
Do not include CtrlNet in link messages describing this session.
@ -899,7 +899,7 @@ class PtpNet(CoreNetwork):
"""
return None
def all_link_data(self, flags: int) -> List[LinkData]:
def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]:
"""
Build CORE API TLVs for a point-to-point link. One Link message
describes this network.
@ -1122,7 +1122,7 @@ class WlanNode(CoreNetwork):
x, y, z = netif.node.position.get()
netif.poshook(netif, x, y, z)
def all_link_data(self, flags: int) -> List[LinkData]:
def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]:
"""
Retrieve all link data.

View file

@ -210,7 +210,7 @@ class Sdt:
self.add_node(node)
for net in nets:
all_links = net.all_link_data(flags=MessageFlags.ADD.value)
all_links = net.all_link_data(flags=MessageFlags.ADD)
for link_data in all_links:
is_wireless = isinstance(net, (WlanNode, EmaneNet))
if is_wireless and link_data.node1_id == net.id:
@ -302,7 +302,7 @@ class Sdt:
return
# delete node
if node_data.message_type == MessageFlags.DELETE.value:
if node_data.message_type == MessageFlags.DELETE:
self.cmd(f"delete node,{node_data.id}")
else:
x = node_data.x_position
@ -375,9 +375,9 @@ class Sdt:
:param link_data: link data to handle
:return: nothing
"""
if link_data.message_type == MessageFlags.ADD.value:
if link_data.message_type == MessageFlags.ADD:
params = link_data_params(link_data)
self.add_link(*params)
elif link_data.message_type == MessageFlags.DELETE.value:
elif link_data.message_type == MessageFlags.DELETE:
params = link_data_params(link_data)
self.delete_link(*params[:2])

View file

@ -672,7 +672,7 @@ class CoreServices:
filetypestr = "service:%s" % service.name
return FileData(
message_type=MessageFlags.ADD.value,
message_type=MessageFlags.ADD,
node=node.id,
name=filename,
type=filetypestr,

View file

@ -476,7 +476,7 @@ class CoreXmlWriter:
self.write_device(node)
# add known links
links.extend(node.all_link_data(0))
links.extend(node.all_link_data())
return links

View file

@ -114,7 +114,7 @@ class TestCore:
session.instantiate()
# check link data gets generated
assert ptp_node.all_link_data(MessageFlags.ADD.value)
assert ptp_node.all_link_data(MessageFlags.ADD)
# check common nets exist between linked nodes
assert node_one.commonnets(node_two)

View file

@ -540,7 +540,7 @@ class TestGrpc:
session = grpc_server.coreemu.create_session()
switch = session.add_node(_type=NodeTypes.SWITCH)
node = session.add_node()
assert len(switch.all_link_data(0)) == 0
assert len(switch.all_link_data()) == 0
# then
interface = interface_helper.create_interface(node.id, 0)
@ -549,7 +549,7 @@ class TestGrpc:
# then
assert response.result is True
assert len(switch.all_link_data(0)) == 1
assert len(switch.all_link_data()) == 1
def test_add_link_exception(self, grpc_server, interface_helper):
# given
@ -572,7 +572,7 @@ class TestGrpc:
interface = ip_prefixes.create_interface(node)
session.add_link(node.id, switch.id, interface)
options = core_pb2.LinkOptions(bandwidth=30000)
link = switch.all_link_data(0)[0]
link = switch.all_link_data()[0]
assert options.bandwidth != link.bandwidth
# then
@ -583,7 +583,7 @@ class TestGrpc:
# then
assert response.result is True
link = switch.all_link_data(0)[0]
link = switch.all_link_data()[0]
assert options.bandwidth == link.bandwidth
def test_delete_link(self, grpc_server, ip_prefixes):
@ -986,7 +986,7 @@ class TestGrpc:
client = CoreGrpcClient()
session = grpc_server.coreemu.create_session()
node = session.add_node()
node_data = node.data(message_type=0)
node_data = node.data()
queue = Queue()
def handle_event(event_data):
@ -1011,7 +1011,7 @@ class TestGrpc:
node = session.add_node()
interface = ip_prefixes.create_interface(node)
session.add_link(node.id, wlan.id, interface)
link_data = wlan.all_link_data(0)[0]
link_data = wlan.all_link_data()[0]
queue = Queue()
def handle_event(event_data):

View file

@ -117,7 +117,7 @@ class TestGui:
coretlv.handle_message(message)
switch_node = coretlv.session.get_node(switch)
all_links = switch_node.all_link_data(0)
all_links = switch_node.all_link_data()
assert len(all_links) == 1
def test_link_add_net_to_node(self, coretlv):
@ -141,7 +141,7 @@ class TestGui:
coretlv.handle_message(message)
switch_node = coretlv.session.get_node(switch)
all_links = switch_node.all_link_data(0)
all_links = switch_node.all_link_data()
assert len(all_links) == 1
def test_link_add_node_to_node(self, coretlv):
@ -171,7 +171,7 @@ class TestGui:
all_links = []
for node_id in coretlv.session.nodes:
node = coretlv.session.nodes[node_id]
all_links += node.all_link_data(0)
all_links += node.all_link_data()
assert len(all_links) == 1
def test_link_update(self, coretlv):
@ -193,7 +193,7 @@ class TestGui:
)
coretlv.handle_message(message)
switch_node = coretlv.session.get_node(switch)
all_links = switch_node.all_link_data(0)
all_links = switch_node.all_link_data()
assert len(all_links) == 1
link = all_links[0]
assert link.bandwidth is None
@ -211,7 +211,7 @@ class TestGui:
coretlv.handle_message(message)
switch_node = coretlv.session.get_node(switch)
all_links = switch_node.all_link_data(0)
all_links = switch_node.all_link_data()
assert len(all_links) == 1
link = all_links[0]
assert link.bandwidth == bandwidth
@ -240,7 +240,7 @@ class TestGui:
all_links = []
for node_id in coretlv.session.nodes:
node = coretlv.session.nodes[node_id]
all_links += node.all_link_data(0)
all_links += node.all_link_data()
assert len(all_links) == 1
message = coreapi.CoreLinkMessage.create(
@ -257,7 +257,7 @@ class TestGui:
all_links = []
for node_id in coretlv.session.nodes:
node = coretlv.session.nodes[node_id]
all_links += node.all_link_data(0)
all_links += node.all_link_data()
assert len(all_links) == 0
def test_link_delete_node_to_net(self, coretlv):
@ -279,7 +279,7 @@ class TestGui:
)
coretlv.handle_message(message)
switch_node = coretlv.session.get_node(switch)
all_links = switch_node.all_link_data(0)
all_links = switch_node.all_link_data()
assert len(all_links) == 1
message = coreapi.CoreLinkMessage.create(
@ -293,7 +293,7 @@ class TestGui:
coretlv.handle_message(message)
switch_node = coretlv.session.get_node(switch)
all_links = switch_node.all_link_data(0)
all_links = switch_node.all_link_data()
assert len(all_links) == 0
def test_link_delete_net_to_node(self, coretlv):
@ -315,7 +315,7 @@ class TestGui:
)
coretlv.handle_message(message)
switch_node = coretlv.session.get_node(switch)
all_links = switch_node.all_link_data(0)
all_links = switch_node.all_link_data()
assert len(all_links) == 1
message = coreapi.CoreLinkMessage.create(
@ -329,7 +329,7 @@ class TestGui:
coretlv.handle_message(message)
switch_node = coretlv.session.get_node(switch)
all_links = switch_node.all_link_data(0)
all_links = switch_node.all_link_data()
assert len(all_links) == 0
def test_session_update(self, coretlv):

View file

@ -43,7 +43,7 @@ class TestLinks:
session.add_link(node_one.id, node_two.id, interface_one)
# then
assert node_two.all_link_data(0)
assert node_two.all_link_data()
assert node_one.netif(interface_one.id)
def test_net_to_node(self, session, ip_prefixes):
@ -56,7 +56,7 @@ class TestLinks:
session.add_link(node_one.id, node_two.id, interface_two=interface_two)
# then
assert node_one.all_link_data(0)
assert node_one.all_link_data()
assert node_two.netif(interface_two.id)
def test_net_to_net(self, session):
@ -68,7 +68,7 @@ class TestLinks:
session.add_link(node_one.id, node_two.id)
# then
assert node_one.all_link_data(0)
assert node_one.all_link_data()
def test_link_update(self, session, ip_prefixes):
# given

View file

@ -270,7 +270,7 @@ class TestXml:
switch_two = session.get_node(n2_id)
assert switch_one
assert switch_two
assert len(switch_one.all_link_data(0) + switch_two.all_link_data(0)) == 1
assert len(switch_one.all_link_data() + switch_two.all_link_data()) == 1
def test_link_options(self, session, tmpdir, ip_prefixes):
"""
@ -330,7 +330,7 @@ class TestXml:
links = []
for node_id in session.nodes:
node = session.nodes[node_id]
links += node.all_link_data(0)
links += node.all_link_data()
link = links[0]
assert link_options.per == link.per
assert link_options.bandwidth == link.bandwidth
@ -397,7 +397,7 @@ class TestXml:
links = []
for node_id in session.nodes:
node = session.nodes[node_id]
links += node.all_link_data(0)
links += node.all_link_data()
link = links[0]
assert link_options.per == link.per
assert link_options.bandwidth == link.bandwidth
@ -479,7 +479,7 @@ class TestXml:
links = []
for node_id in session.nodes:
node = session.nodes[node_id]
links += node.all_link_data(0)
links += node.all_link_data()
assert len(links) == 2
link_one = links[0]
link_two = links[1]