daemon: updates to refactor and clean up SessionConfig to contain its own logic

This commit is contained in:
Blake Harnden 2022-04-04 15:13:31 -07:00
parent 409b6809e6
commit 440c06c040
16 changed files with 128 additions and 119 deletions

View file

@ -125,9 +125,7 @@ class DistributedController:
self.session: "Session" = session
self.servers: Dict[str, DistributedServer] = OrderedDict()
self.tunnels: Dict[int, Tuple[GreTap, GreTap]] = {}
self.address: str = self.session.options.get_config(
"distributed_address", default=None
)
self.address: str = self.session.options.get("distributed_address")
def add_server(self, name: str, host: str) -> None:
"""
@ -188,7 +186,7 @@ class DistributedController:
:return: nothing
"""
mtu = self.session.options.get_config_int("mtu")
mtu = self.session.options.get_int("mtu")
for node_id in self.session.nodes:
node = self.session.nodes[node_id]
if not isinstance(node, CtrlNet) or node.serverintf is not None:
@ -210,7 +208,7 @@ class DistributedController:
raise CoreError(
"attempted to create gre tunnel for core link without a ptp network"
)
mtu = self.session.options.get_config_int("mtu")
mtu = self.session.options.get_int("mtu")
for server in self.servers.values():
self.create_gre_tunnel(core_link.ptp, server, mtu, True)

View file

@ -191,7 +191,7 @@ class Session:
return node_type
def use_ovs(self) -> bool:
return self.options.get_config("ovs") == "1"
return self.options.get_int("ovs") == 1
def linked(
self, node1_id: int, node2_id: int, iface1_id: int, iface2_id: int, linked: bool
@ -255,7 +255,7 @@ class Session:
"""
options = options if options else LinkOptions()
# set mtu
mtu = self.options.get_config_int("mtu") or DEFAULT_MTU
mtu = self.options.get_int("mtu") or DEFAULT_MTU
if iface1_data:
iface1_data.mtu = mtu
if iface2_data:
@ -489,7 +489,7 @@ class Session:
"""
# set node start based on current session state, override and check when rj45
start = self.state.should_start()
enable_rj45 = self.options.get_config("enablerj45") == "1"
enable_rj45 = self.options.get_int("enablerj45") == 1
if _class == Rj45Node and not enable_rj45:
start = False
@ -551,7 +551,7 @@ class Session:
node.add_config_service(service_class)
# set network mtu, if configured
mtu = self.options.get_config_int("mtu")
mtu = self.options.get_int("mtu")
if isinstance(node, CoreNetworkBase) and mtu > 0:
node.mtu = mtu
@ -714,7 +714,7 @@ class Session:
# shutdown sdt
self.sdt.shutdown()
# remove this sessions working directory
preserve = self.options.get_config("preservedir") == "1"
preserve = self.options.get_int("preservedir") == 1
if not preserve:
shutil.rmtree(self.directory, ignore_errors=True)
@ -1234,11 +1234,11 @@ class Session:
:return: control net prefix list
"""
p = self.options.get_config("controlnet")
p0 = self.options.get_config("controlnet0")
p1 = self.options.get_config("controlnet1")
p2 = self.options.get_config("controlnet2")
p3 = self.options.get_config("controlnet3")
p = self.options.get("controlnet")
p0 = self.options.get("controlnet0")
p1 = self.options.get("controlnet1")
p2 = self.options.get("controlnet2")
p3 = self.options.get("controlnet3")
if not p0 and p:
p0 = p
return [p0, p1, p2, p3]
@ -1249,12 +1249,12 @@ class Session:
:return: list of control net server interfaces
"""
d0 = self.options.get_config("controlnetif0")
d0 = self.options.get("controlnetif0")
if d0:
logger.error("controlnet0 cannot be assigned with a host interface")
d1 = self.options.get_config("controlnetif1")
d2 = self.options.get_config("controlnetif2")
d3 = self.options.get_config("controlnetif3")
d1 = self.options.get("controlnetif1")
d2 = self.options.get("controlnetif2")
d3 = self.options.get("controlnetif3")
return [None, d1, d2, d3]
def get_control_net_index(self, dev: str) -> int:
@ -1331,7 +1331,7 @@ class Session:
updown_script = None
if net_index == 0:
updown_script = self.options.get_config("controlnet_updown_script")
updown_script = self.options.get("controlnet_updown_script")
if not updown_script:
logger.debug("controlnet updown script not configured")
@ -1424,7 +1424,7 @@ class Session:
:param remove: flag to check if it should be removed
:return: nothing
"""
if not self.options.get_config_bool("update_etc_hosts", default=False):
if not self.options.get_bool("update_etc_hosts", False):
return
try:

View file

@ -1,23 +1,15 @@
from typing import Any, Dict, List
from typing import Dict, List, Optional
from core.config import (
ConfigBool,
ConfigInt,
ConfigString,
ConfigurableManager,
ConfigurableOptions,
Configuration,
)
from core.emulator.enumerations import RegisterTlvs
from core.config import ConfigBool, ConfigInt, ConfigString, Configuration
from core.errors import CoreError
from core.plugins.sdt import Sdt
class SessionConfig(ConfigurableManager, ConfigurableOptions):
class SessionConfig:
"""
Provides session configuration.
"""
name: str = "session"
options: List[Configuration] = [
ConfigString(id="controlnet", label="Control Network"),
ConfigString(id="controlnet0", label="Control Network 0"),
@ -42,37 +34,54 @@ class SessionConfig(ConfigurableManager, ConfigurableOptions):
ConfigInt(id="link_timeout", default="4", label="EMANE Link Timeout (sec)"),
ConfigInt(id="mtu", default="0", label="MTU for All Devices"),
]
config_type: RegisterTlvs = RegisterTlvs.UTILITY
def __init__(self, config: Dict[str, str] = None) -> None:
super().__init__()
self.set_configs(self.default_values())
if config:
for key, value in config.items():
self.set_config(key, value)
def get_config(
self,
_id: str,
node_id: int = ConfigurableManager._default_node,
config_type: str = ConfigurableManager._default_type,
default: Any = None,
) -> str:
"""
Retrieves a specific configuration for a node and configuration type.
Create a SessionConfig instance.
:param _id: specific configuration to retrieve
:param node_id: node id to store configuration for
:param config_type: configuration type to store configuration for
:param default: default value to return when value is not found
:return: configuration value
:param config: configuration to initialize with
"""
value = super().get_config(_id, node_id, config_type, default)
if value == "":
value = default
return value
self._config: Dict[str, str] = {x.id: x.default for x in self.options}
self._config.update(config or {})
def get_config_bool(self, name: str, default: Any = None) -> bool:
def update(self, config: Dict[str, str]) -> None:
"""
Update current configuration with provided values.
:param config: configuration to update with
:return: nothing
"""
self._config.update(config)
def set(self, name: str, value: str) -> None:
"""
Set a configuration value.
:param name: name of configuration to set
:param value: value to set
:return: nothing
"""
self._config[name] = value
def get(self, name: str, default: str = None) -> Optional[str]:
"""
Retrieve configuration value.
:param name: name of configuration to get
:param default: value to return as default
:return: return found configuration value or default
"""
return self._config.get(name, default)
def all(self) -> Dict[str, str]:
"""
Retrieve all configuration options.
:return: configuration value dict
"""
return self._config
def get_bool(self, name: str, default: bool = None) -> bool:
"""
Get configuration value as a boolean.
@ -80,12 +89,15 @@ class SessionConfig(ConfigurableManager, ConfigurableOptions):
:param default: default value if not found
:return: boolean for configuration value
"""
value = self.get_config(name)
value = self._config.get(name)
if value is None and default is None:
raise CoreError(f"missing session options for {name}")
if value is None:
return default
return value.lower() == "true"
else:
return value.lower() == "true"
def get_config_int(self, name: str, default: Any = None) -> int:
def get_int(self, name: str, default: int = None) -> int:
"""
Get configuration value as int.
@ -93,17 +105,10 @@ class SessionConfig(ConfigurableManager, ConfigurableOptions):
:param default: default value if not found
:return: int for configuration value
"""
value = self.get_config(name, default=default)
if value is not None:
value = int(value)
return value
def config_reset(self, node_id: int = None) -> None:
"""
Clear prior configuration files and reset to default values.
:param node_id: node id to store configuration for
:return: nothing
"""
super().config_reset(node_id)
self.set_configs(self.default_values())
value = self._config.get(name)
if value is None and default is None:
raise CoreError(f"missing session options for {name}")
if value is None:
return default
else:
return int(value)