From 22e92111d04378da8c5a7ac9381341d5dd46ee94 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Mon, 15 Nov 2021 16:40:30 -0800 Subject: [PATCH] daemon: updates to provide config types for configurable values, without the need to specify the enumerated type --- daemon/core/config.py | 38 +++++- .../securityservices/services.py | 45 ++----- daemon/core/emane/emanemodel.py | 13 +- daemon/core/emane/models/bypass.py | 6 +- daemon/core/emane/models/tdma.py | 6 +- daemon/core/emulator/sessionconfig.py | 111 +++++------------- daemon/core/location/mobility.py | 100 ++++------------ daemon/tests/test_conf.py | 8 +- daemon/tests/test_config_services.py | 14 +-- docs/configservices.md | 14 +-- 10 files changed, 115 insertions(+), 240 deletions(-) diff --git a/daemon/core/config.py b/daemon/core/config.py index 7d04947e..b705e8b6 100644 --- a/daemon/core/config.py +++ b/daemon/core/config.py @@ -36,7 +36,7 @@ class ConfigGroup: @dataclass class Configuration: """ - Represents a configuration options. + Represents a configuration option. """ id: str @@ -71,6 +71,42 @@ class Configuration: ) +@dataclass +class ConfigBool(Configuration): + """ + Represents a boolean configuration option. + """ + + type: ConfigDataTypes = ConfigDataTypes.BOOL + + +@dataclass +class ConfigFloat(Configuration): + """ + Represents a float configuration option. + """ + + type: ConfigDataTypes = ConfigDataTypes.FLOAT + + +@dataclass +class ConfigInt(Configuration): + """ + Represents an integer configuration option. + """ + + type: ConfigDataTypes = ConfigDataTypes.INT32 + + +@dataclass +class ConfigString(Configuration): + """ + Represents a string configuration option. + """ + + type: ConfigDataTypes = ConfigDataTypes.STRING + + class ConfigurableOptions: """ Provides a base for defining configuration options within CORE. diff --git a/daemon/core/configservices/securityservices/services.py b/daemon/core/configservices/securityservices/services.py index 9da27010..e866617c 100644 --- a/daemon/core/configservices/securityservices/services.py +++ b/daemon/core/configservices/securityservices/services.py @@ -1,8 +1,7 @@ from typing import Any, Dict, List -from core.config import Configuration +from core.config import ConfigString, Configuration from core.configservice.base import ConfigService, ConfigServiceMode -from core.emulator.enumerations import ConfigDataTypes GROUP_NAME: str = "Security" @@ -19,24 +18,9 @@ class VpnClient(ConfigService): shutdown: List[str] = ["killall openvpn"] validation_mode: ConfigServiceMode = ConfigServiceMode.BLOCKING default_configs: List[Configuration] = [ - Configuration( - id="keydir", - type=ConfigDataTypes.STRING, - label="Key Dir", - default="/etc/core/keys", - ), - Configuration( - id="keyname", - type=ConfigDataTypes.STRING, - label="Key Name", - default="client1", - ), - Configuration( - id="server", - type=ConfigDataTypes.STRING, - label="Server", - default="10.0.2.10", - ), + ConfigString(id="keydir", label="Key Dir", default="/etc/core/keys"), + ConfigString(id="keyname", label="Key Name", default="client1"), + ConfigString(id="server", label="Server", default="10.0.2.10"), ] modes: Dict[str, Dict[str, str]] = {} @@ -53,24 +37,9 @@ class VpnServer(ConfigService): shutdown: List[str] = ["killall openvpn"] validation_mode: ConfigServiceMode = ConfigServiceMode.BLOCKING default_configs: List[Configuration] = [ - Configuration( - id="keydir", - type=ConfigDataTypes.STRING, - label="Key Dir", - default="/etc/core/keys", - ), - Configuration( - id="keyname", - type=ConfigDataTypes.STRING, - label="Key Name", - default="server", - ), - Configuration( - id="subnet", - type=ConfigDataTypes.STRING, - label="Subnet", - default="10.0.200.0", - ), + ConfigString(id="keydir", label="Key Dir", default="/etc/core/keys"), + ConfigString(id="keyname", label="Key Name", default="server"), + ConfigString(id="subnet", label="Subnet", default="10.0.200.0"), ] modes: Dict[str, Dict[str, str]] = {} diff --git a/daemon/core/emane/emanemodel.py b/daemon/core/emane/emanemodel.py index 21fcccb3..cc5b0f4d 100644 --- a/daemon/core/emane/emanemodel.py +++ b/daemon/core/emane/emanemodel.py @@ -5,10 +5,9 @@ import logging from pathlib import Path from typing import Dict, List, Optional, Set -from core.config import ConfigGroup, Configuration +from core.config import ConfigBool, ConfigGroup, ConfigString, Configuration from core.emane import emanemanifest from core.emulator.data import LinkOptions -from core.emulator.enumerations import ConfigDataTypes from core.errors import CoreError from core.location.mobility import WirelessModel from core.nodes.interface import CoreInterface @@ -55,13 +54,9 @@ class EmaneModel(WirelessModel): # support for external configurations external_config: List[Configuration] = [ - Configuration("external", ConfigDataTypes.BOOL, default="0"), - Configuration( - "platformendpoint", ConfigDataTypes.STRING, default="127.0.0.1:40001" - ), - Configuration( - "transportendpoint", ConfigDataTypes.STRING, default="127.0.0.1:50002" - ), + ConfigBool(id="external", default="0"), + ConfigString(id="platformendpoint", default="127.0.0.1:40001"), + ConfigString(id="transportendpoint", default="127.0.0.1:50002"), ] config_ignore: Set[str] = set() diff --git a/daemon/core/emane/models/bypass.py b/daemon/core/emane/models/bypass.py index 67b7707d..25841114 100644 --- a/daemon/core/emane/models/bypass.py +++ b/daemon/core/emane/models/bypass.py @@ -4,9 +4,8 @@ EMANE Bypass model for CORE from pathlib import Path from typing import List, Set -from core.config import Configuration +from core.config import ConfigBool, Configuration from core.emane import emanemodel -from core.emulator.enumerations import ConfigDataTypes class EmaneBypassModel(emanemodel.EmaneModel): @@ -18,9 +17,8 @@ class EmaneBypassModel(emanemodel.EmaneModel): # mac definitions mac_library: str = "bypassmaclayer" mac_config: List[Configuration] = [ - Configuration( + ConfigBool( id="none", - type=ConfigDataTypes.BOOL, default="0", label="There are no parameters for the bypass model.", ) diff --git a/daemon/core/emane/models/tdma.py b/daemon/core/emane/models/tdma.py index c23e3d73..c6ac631b 100644 --- a/daemon/core/emane/models/tdma.py +++ b/daemon/core/emane/models/tdma.py @@ -7,10 +7,9 @@ from pathlib import Path from typing import Set from core import constants, utils -from core.config import Configuration +from core.config import ConfigString from core.emane import emanemodel from core.emane.nodes import EmaneNet -from core.emulator.enumerations import ConfigDataTypes from core.nodes.interface import CoreInterface logger = logging.getLogger(__name__) @@ -38,9 +37,8 @@ class EmaneTdmaModel(emanemodel.EmaneModel): / "share/emane/xml/models/mac/tdmaeventscheduler/tdmabasemodelpcr.xml" ) super().load(emane_prefix) - config_item = Configuration( + config_item = ConfigString( id=cls.schedule_name, - type=ConfigDataTypes.STRING, default=str(cls.default_schedule), label="TDMA schedule file (core)", ) diff --git a/daemon/core/emulator/sessionconfig.py b/daemon/core/emulator/sessionconfig.py index f40161cb..893f71c4 100644 --- a/daemon/core/emulator/sessionconfig.py +++ b/daemon/core/emulator/sessionconfig.py @@ -1,7 +1,14 @@ from typing import Any, List -from core.config import ConfigurableManager, ConfigurableOptions, Configuration -from core.emulator.enumerations import ConfigDataTypes, RegisterTlvs +from core.config import ( + ConfigBool, + ConfigInt, + ConfigString, + ConfigurableManager, + ConfigurableOptions, + Configuration, +) +from core.emulator.enumerations import RegisterTlvs from core.plugins.sdt import Sdt @@ -12,89 +19,27 @@ class SessionConfig(ConfigurableManager, ConfigurableOptions): name: str = "session" options: List[Configuration] = [ - Configuration( - id="controlnet", type=ConfigDataTypes.STRING, label="Control Network" + ConfigString(id="controlnet", label="Control Network"), + ConfigString(id="controlnet0", label="Control Network 0"), + ConfigString(id="controlnet1", label="Control Network 1"), + ConfigString(id="controlnet2", label="Control Network 2"), + ConfigString(id="controlnet3", label="Control Network 3"), + ConfigString(id="controlnet_updown_script", label="Control Network Script"), + ConfigBool(id="enablerj45", default="1", label="Enable RJ45s"), + ConfigBool(id="preservedir", default="0", label="Preserve session dir"), + ConfigBool(id="enablesdt", default="0", label="Enable SDT3D output"), + ConfigString(id="sdturl", default=Sdt.DEFAULT_SDT_URL, label="SDT3D URL"), + ConfigBool(id="ovs", default="0", label="Enable OVS"), + ConfigInt(id="platform_id_start", default="1", label="EMANE Platform ID Start"), + ConfigInt(id="nem_id_start", default="1", label="EMANE NEM ID Start"), + ConfigBool(id="link_enabled", default="1", label="EMANE Links?"), + ConfigInt( + id="loss_threshold", default="30", label="EMANE Link Loss Threshold (%)" ), - Configuration( - id="controlnet0", type=ConfigDataTypes.STRING, label="Control Network 0" - ), - Configuration( - id="controlnet1", type=ConfigDataTypes.STRING, label="Control Network 1" - ), - Configuration( - id="controlnet2", type=ConfigDataTypes.STRING, label="Control Network 2" - ), - Configuration( - id="controlnet3", type=ConfigDataTypes.STRING, label="Control Network 3" - ), - Configuration( - id="controlnet_updown_script", - type=ConfigDataTypes.STRING, - label="Control Network Script", - ), - Configuration( - id="enablerj45", - type=ConfigDataTypes.BOOL, - default="1", - label="Enable RJ45s", - ), - Configuration( - id="preservedir", - type=ConfigDataTypes.BOOL, - default="0", - label="Preserve session dir", - ), - Configuration( - id="enablesdt", - type=ConfigDataTypes.BOOL, - default="0", - label="Enable SDT3D output", - ), - Configuration( - id="sdturl", - type=ConfigDataTypes.STRING, - default=Sdt.DEFAULT_SDT_URL, - label="SDT3D URL", - ), - Configuration( - id="ovs", type=ConfigDataTypes.BOOL, default="0", label="Enable OVS" - ), - Configuration( - id="platform_id_start", - type=ConfigDataTypes.INT32, - default="1", - label="EMANE Platform ID Start", - ), - Configuration( - id="nem_id_start", - type=ConfigDataTypes.INT32, - default="1", - label="EMANE NEM ID Start", - ), - Configuration( - id="link_enabled", - type=ConfigDataTypes.BOOL, - default="1", - label="EMANE Links?", - ), - Configuration( - id="loss_threshold", - type=ConfigDataTypes.INT32, - default="30", - label="EMANE Link Loss Threshold (%)", - ), - Configuration( - id="link_interval", - type=ConfigDataTypes.INT32, - default="1", - label="EMANE Link Check Interval (sec)", - ), - Configuration( - id="link_timeout", - type=ConfigDataTypes.INT32, - default="4", - label="EMANE Link Timeout (sec)", + ConfigInt( + id="link_interval", default="1", label="EMANE Link Check Interval (sec)" ), + ConfigInt(id="link_timeout", default="4", label="EMANE Link Timeout (sec)"), ] config_type: RegisterTlvs = RegisterTlvs.UTILITY diff --git a/daemon/core/location/mobility.py b/daemon/core/location/mobility.py index ad7fc821..a92eae3d 100644 --- a/daemon/core/location/mobility.py +++ b/daemon/core/location/mobility.py @@ -12,16 +12,18 @@ from pathlib import Path from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Union from core import utils -from core.config import ConfigGroup, ConfigurableOptions, Configuration, ModelManager +from core.config import ( + ConfigBool, + ConfigGroup, + ConfigInt, + ConfigString, + ConfigurableOptions, + Configuration, + ModelManager, +) from core.emane.nodes import EmaneNet from core.emulator.data import EventData, LinkData, LinkOptions -from core.emulator.enumerations import ( - ConfigDataTypes, - EventTypes, - LinkTypes, - MessageFlags, - RegisterTlvs, -) +from core.emulator.enumerations import EventTypes, LinkTypes, MessageFlags, RegisterTlvs from core.errors import CoreError from core.executables import BASH from core.nodes.base import CoreNode @@ -274,39 +276,12 @@ class BasicRangeModel(WirelessModel): name: str = "basic_range" options: List[Configuration] = [ - Configuration( - id="range", - type=ConfigDataTypes.UINT32, - default="275", - label="wireless range (pixels)", - ), - Configuration( - id="bandwidth", - type=ConfigDataTypes.UINT64, - default="54000000", - label="bandwidth (bps)", - ), - Configuration( - id="jitter", - type=ConfigDataTypes.UINT64, - default="0", - label="transmission jitter (usec)", - ), - Configuration( - id="delay", - type=ConfigDataTypes.UINT64, - default="5000", - label="transmission delay (usec)", - ), - Configuration( - id="error", type=ConfigDataTypes.STRING, default="0", label="loss (%)" - ), - Configuration( - id="promiscuous", - type=ConfigDataTypes.BOOL, - default="0", - label="promiscuous mode", - ), + ConfigInt(id="range", default="275", label="wireless range (pixels)"), + ConfigInt(id="bandwidth", default="54000000", label="bandwidth (bps)"), + ConfigInt(id="jitter", default="0", label="transmission jitter (usec)"), + ConfigInt(id="delay", default="5000", label="transmission delay (usec)"), + ConfigString(id="error", default="0", label="loss (%)"), + ConfigBool(id="promiscuous", default="0", label="promiscuous mode"), ] @classmethod @@ -887,41 +862,14 @@ class Ns2ScriptedMobility(WayPointMobility): name: str = "ns2script" options: List[Configuration] = [ - Configuration( - id="file", type=ConfigDataTypes.STRING, label="mobility script file" - ), - Configuration( - id="refresh_ms", - type=ConfigDataTypes.UINT32, - default="50", - label="refresh time (ms)", - ), - Configuration(id="loop", type=ConfigDataTypes.BOOL, default="1", label="loop"), - Configuration( - id="autostart", - type=ConfigDataTypes.STRING, - label="auto-start seconds (0.0 for runtime)", - ), - Configuration( - id="map", - type=ConfigDataTypes.STRING, - label="node mapping (optional, e.g. 0:1,1:2,2:3)", - ), - Configuration( - id="script_start", - type=ConfigDataTypes.STRING, - label="script file to run upon start", - ), - Configuration( - id="script_pause", - type=ConfigDataTypes.STRING, - label="script file to run upon pause", - ), - Configuration( - id="script_stop", - type=ConfigDataTypes.STRING, - label="script file to run upon stop", - ), + ConfigString(id="file", label="mobility script file"), + ConfigInt(id="refresh_ms", default="50", label="refresh time (ms)"), + ConfigBool(id="loop", default="1", label="loop"), + ConfigString(id="autostart", label="auto-start seconds (0.0 for runtime)"), + ConfigString(id="map", label="node mapping (optional, e.g. 0:1,1:2,2:3)"), + ConfigString(id="script_start", label="script file to run upon start"), + ConfigString(id="script_pause", label="script file to run upon pause"), + ConfigString(id="script_stop", label="script file to run upon stop"), ] @classmethod diff --git a/daemon/tests/test_conf.py b/daemon/tests/test_conf.py index df16fb22..2c74841d 100644 --- a/daemon/tests/test_conf.py +++ b/daemon/tests/test_conf.py @@ -1,13 +1,12 @@ import pytest from core.config import ( + ConfigString, ConfigurableManager, ConfigurableOptions, - Configuration, ModelManager, ) from core.emane.models.ieee80211abg import EmaneIeee80211abgModel -from core.emulator.enumerations import ConfigDataTypes from core.emulator.session import Session from core.location.mobility import BasicRangeModel from core.nodes.network import WlanNode @@ -16,10 +15,7 @@ from core.nodes.network import WlanNode class TestConfigurableOptions(ConfigurableOptions): name1 = "value1" name2 = "value2" - options = [ - Configuration(id=name1, type=ConfigDataTypes.STRING, label=name1), - Configuration(id=name2, type=ConfigDataTypes.STRING, label=name2), - ] + options = [ConfigString(id=name1, label=name1), ConfigString(id=name2, label=name2)] class TestConf: diff --git a/daemon/tests/test_config_services.py b/daemon/tests/test_config_services.py index 598450c1..876b7f32 100644 --- a/daemon/tests/test_config_services.py +++ b/daemon/tests/test_config_services.py @@ -3,13 +3,12 @@ from unittest import mock import pytest -from core.config import Configuration +from core.config import ConfigBool, ConfigString from core.configservice.base import ( ConfigService, ConfigServiceBootError, ConfigServiceMode, ) -from core.emulator.enumerations import ConfigDataTypes from core.errors import CoreCommandError, CoreError TEMPLATE_TEXT = "echo hello" @@ -27,13 +26,10 @@ class MyService(ConfigService): shutdown = [f"pkill {files[0]}"] validation_mode = ConfigServiceMode.BLOCKING default_configs = [ - Configuration(id="value1", type=ConfigDataTypes.STRING, label="Text"), - Configuration(id="value2", type=ConfigDataTypes.BOOL, label="Boolean"), - Configuration( - id="value3", - type=ConfigDataTypes.STRING, - label="Multiple Choice", - options=["value1", "value2", "value3"], + ConfigString(id="value1", label="Text"), + ConfigBool(id="value2", label="Boolean"), + ConfigString( + id="value3", label="Multiple Choice", options=["value1", "value2", "value3"] ), ] modes = { diff --git a/docs/configservices.md b/docs/configservices.md index 93718f3f..42cf1478 100644 --- a/docs/configservices.md +++ b/docs/configservices.md @@ -118,9 +118,8 @@ running the shell files generated. ```python from typing import Dict, List -from core.config import Configuration +from core.config import ConfigString, ConfigBool, Configuration from core.configservice.base import ConfigService, ConfigServiceMode, ShadowDir -from core.emulator.enumerations import ConfigDataTypes # class that subclasses ConfigService class ExampleService(ConfigService): @@ -152,14 +151,9 @@ class ExampleService(ConfigService): validation_mode: ConfigServiceMode = ConfigServiceMode.BLOCKING # configurable values that this service can use, for file generation default_configs: List[Configuration] = [ - Configuration(id="value1", type=ConfigDataTypes.STRING, label="Text"), - Configuration(id="value2", type=ConfigDataTypes.BOOL, label="Boolean"), - Configuration( - id="value3", - type=ConfigDataTypes.STRING, - label="Multiple Choice", - options=["value1", "value2", "value3"], - ), + ConfigString(id="value1", label="Text"), + ConfigBool(id="value2", label="Boolean"), + ConfigString(id="value3", label="Multiple Choice", options=["value1", "value2", "value3"]), ] # sets of values to set for the configuration defined above, can be used to # provide convenient sets of values to typically use