daemon: updated NodeOptions to leverage dataclass

This commit is contained in:
Blake Harnden 2020-06-09 10:45:18 -07:00
parent b5e53e573a
commit 7d2034df71
7 changed files with 39 additions and 55 deletions

View file

@ -31,17 +31,19 @@ def add_node_data(node_proto: core_pb2.Node) -> Tuple[NodeTypes, int, NodeOption
""" """
_id = node_proto.id _id = node_proto.id
_type = NodeTypes(node_proto.type) _type = NodeTypes(node_proto.type)
options = NodeOptions(name=node_proto.name, model=node_proto.model) options = NodeOptions(
options.icon = node_proto.icon name=node_proto.name,
options.opaque = node_proto.opaque model=node_proto.model,
options.image = node_proto.image icon=node_proto.icon,
options.services = node_proto.services opaque=node_proto.opaque,
options.config_services = node_proto.config_services image=node_proto.image,
services=node_proto.services,
config_services=node_proto.config_services,
)
if node_proto.emane: if node_proto.emane:
options.emane = node_proto.emane options.emane = node_proto.emane
if node_proto.server: if node_proto.server:
options.server = node_proto.server options.server = node_proto.server
position = node_proto.position position = node_proto.position
options.set_position(position.x, position.y) options.set_position(position.x, position.y)
if node_proto.HasField("geo"): if node_proto.HasField("geo"):

View file

@ -743,8 +743,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
logging.debug("edit node: %s", request) logging.debug("edit node: %s", request)
session = self.get_session(request.session_id, context) session = self.get_session(request.session_id, context)
node = self.get_node(session, request.node_id, context, NodeBase) node = self.get_node(session, request.node_id, context, NodeBase)
options = NodeOptions() options = NodeOptions(icon=request.icon)
options.icon = request.icon
if request.HasField("position"): if request.HasField("position"):
x = request.position.x x = request.position.x
y = request.position.y y = request.position.y

View file

@ -1,5 +1,5 @@
from dataclasses import dataclass from dataclasses import dataclass, field
from typing import List, Union from typing import List, Optional, Union
import netaddr import netaddr
@ -37,36 +37,28 @@ def link_config(
) )
@dataclass
class NodeOptions: class NodeOptions:
""" """
Options for creating and updating nodes within core. Options for creating and updating nodes within core.
""" """
def __init__(self, name: str = None, model: str = "PC", image: str = None) -> None: name: str = None
""" model: Optional[str] = "PC"
Create a NodeOptions object. canvas: int = None
icon: str = None
:param name: name of node, defaults to node class name postfix with its id opaque: str = None
:param model: defines services for default and physical nodes, defaults to services: List[str] = field(default_factory=list)
"router" config_services: List[str] = field(default_factory=list)
:param image: image to use for docker nodes x: float = None
""" y: float = None
self.name = name lat: float = None
self.model = model lon: float = None
self.canvas = None alt: float = None
self.icon = None emulation_id: int = None
self.opaque = None server: str = None
self.services = [] image: str = None
self.config_services = [] emane: str = None
self.x = None
self.y = None
self.lat = None
self.lon = None
self.alt = None
self.emulation_id = None
self.server = None
self.image = image
self.emane = None
def set_position(self, x: float, y: float) -> None: def set_position(self, x: float, y: float) -> None:
""" """

View file

@ -832,8 +832,7 @@ class CoreXmlReader:
icon = device_element.get("icon") icon = device_element.get("icon")
clazz = device_element.get("class") clazz = device_element.get("class")
image = device_element.get("image") image = device_element.get("image")
options = NodeOptions(name, model, image) options = NodeOptions(name=name, model=model, image=image, icon=icon)
options.icon = icon
node_type = NodeTypes.DEFAULT node_type = NodeTypes.DEFAULT
if clazz == "docker": if clazz == "docker":
@ -874,8 +873,7 @@ class CoreXmlReader:
node_type = NodeTypes[network_element.get("type")] node_type = NodeTypes[network_element.get("type")]
_class = self.session.get_node_class(node_type) _class = self.session.get_node_class(node_type)
icon = network_element.get("icon") icon = network_element.get("icon")
options = NodeOptions(name) options = NodeOptions(name=name, icon=icon)
options.icon = icon
position_element = network_element.find("position") position_element = network_element.find("position")
if position_element is not None: if position_element is not None:

View file

@ -12,8 +12,7 @@ class TestDistributed:
# when # when
session.distributed.add_server(server_name, host) session.distributed.add_server(server_name, host)
options = NodeOptions() options = NodeOptions(server=server_name)
options.server = server_name
node = session.add_node(CoreNode, options=options) node = session.add_node(CoreNode, options=options)
session.instantiate() session.instantiate()
@ -30,8 +29,7 @@ class TestDistributed:
# when # when
session.distributed.add_server(server_name, host) session.distributed.add_server(server_name, host)
options = NodeOptions() options = NodeOptions(server=server_name)
options.server = server_name
node = session.add_node(HubNode, options=options) node = session.add_node(HubNode, options=options)
session.instantiate() session.instantiate()

View file

@ -710,8 +710,7 @@ class TestGrpc:
client = CoreGrpcClient() client = CoreGrpcClient()
session = grpc_server.coreemu.create_session() session = grpc_server.coreemu.create_session()
session.set_location(47.57917, -122.13232, 2.00000, 1.0) session.set_location(47.57917, -122.13232, 2.00000, 1.0)
options = NodeOptions() options = NodeOptions(emane=EmaneIeee80211abgModel.name)
options.emane = EmaneIeee80211abgModel.name
emane_network = session.add_node(EmaneNet, options=options) emane_network = session.add_node(EmaneNet, options=options)
session.emane.set_model(emane_network, EmaneIeee80211abgModel) session.emane.set_model(emane_network, EmaneIeee80211abgModel)
config_key = "platform_id_start" config_key = "platform_id_start"
@ -737,8 +736,7 @@ class TestGrpc:
client = CoreGrpcClient() client = CoreGrpcClient()
session = grpc_server.coreemu.create_session() session = grpc_server.coreemu.create_session()
session.set_location(47.57917, -122.13232, 2.00000, 1.0) session.set_location(47.57917, -122.13232, 2.00000, 1.0)
options = NodeOptions() options = NodeOptions(emane=EmaneIeee80211abgModel.name)
options.emane = EmaneIeee80211abgModel.name
emane_network = session.add_node(EmaneNet, options=options) emane_network = session.add_node(EmaneNet, options=options)
session.emane.set_model(emane_network, EmaneIeee80211abgModel) session.emane.set_model(emane_network, EmaneIeee80211abgModel)
config_key = "bandwidth" config_key = "bandwidth"
@ -765,8 +763,7 @@ class TestGrpc:
client = CoreGrpcClient() client = CoreGrpcClient()
session = grpc_server.coreemu.create_session() session = grpc_server.coreemu.create_session()
session.set_location(47.57917, -122.13232, 2.00000, 1.0) session.set_location(47.57917, -122.13232, 2.00000, 1.0)
options = NodeOptions() options = NodeOptions(emane=EmaneIeee80211abgModel.name)
options.emane = EmaneIeee80211abgModel.name
emane_network = session.add_node(EmaneNet, options=options) emane_network = session.add_node(EmaneNet, options=options)
session.emane.set_model(emane_network, EmaneIeee80211abgModel) session.emane.set_model(emane_network, EmaneIeee80211abgModel)

View file

@ -136,8 +136,7 @@ coreemu = CoreEmu()
session = coreemu.create_session() session = coreemu.create_session()
# create node with custom services # create node with custom services
options = NodeOptions() options = NodeOptions(services=["ServiceName"])
options.services = ["ServiceName"]
node = session.add_node(options=options) node = session.add_node(options=options)
# set custom file data # set custom file data
@ -157,7 +156,6 @@ options = NodeOptions()
options.set_position(80, 50) options.set_position(80, 50)
emane_network = session.add_node(EmaneNet, options=options) emane_network = session.add_node(EmaneNet, options=options)
# set custom emane model config # set custom emane model config defaults
config = {} session.emane.set_model(emane_network, EmaneIeee80211abgModel)
session.emane.set_model(emane_network, EmaneIeee80211abgModel, config)
``` ```