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
_type = NodeTypes(node_proto.type)
options = NodeOptions(name=node_proto.name, model=node_proto.model)
options.icon = node_proto.icon
options.opaque = node_proto.opaque
options.image = node_proto.image
options.services = node_proto.services
options.config_services = node_proto.config_services
options = NodeOptions(
name=node_proto.name,
model=node_proto.model,
icon=node_proto.icon,
opaque=node_proto.opaque,
image=node_proto.image,
services=node_proto.services,
config_services=node_proto.config_services,
)
if node_proto.emane:
options.emane = node_proto.emane
if node_proto.server:
options.server = node_proto.server
position = node_proto.position
options.set_position(position.x, position.y)
if node_proto.HasField("geo"):

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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