2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2017-05-03 17:30:49 +01:00
|
|
|
Common support for configurable CORE objects.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
|
2019-02-16 17:50:19 +00:00
|
|
|
import logging
|
2018-06-06 22:51:45 +01:00
|
|
|
from collections import OrderedDict
|
2021-03-31 19:13:40 +01:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Type, Union
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
from core.emane.nodes import EmaneNet
|
|
|
|
from core.emulator.enumerations import ConfigDataTypes
|
2021-03-31 19:13:40 +01:00
|
|
|
from core.errors import CoreConfigError
|
2020-01-13 18:06:18 +00:00
|
|
|
from core.nodes.network import WlanNode
|
|
|
|
|
2021-04-22 05:09:35 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from core.location.mobility import WirelessModel
|
|
|
|
|
2020-01-16 19:17:08 +00:00
|
|
|
WirelessModelType = Type[WirelessModel]
|
|
|
|
|
2021-03-31 19:13:40 +01:00
|
|
|
_BOOL_OPTIONS: Set[str] = {"0", "1"}
|
2020-01-13 18:06:18 +00:00
|
|
|
|
2021-03-31 19:13:40 +01:00
|
|
|
|
|
|
|
@dataclass
|
2020-01-13 18:06:18 +00:00
|
|
|
class ConfigGroup:
|
|
|
|
"""
|
|
|
|
Defines configuration group tabs used for display by ConfigurationOptions.
|
|
|
|
"""
|
|
|
|
|
2021-03-31 19:13:40 +01:00
|
|
|
name: str
|
|
|
|
start: int
|
|
|
|
stop: int
|
2020-01-13 18:06:18 +00:00
|
|
|
|
|
|
|
|
2021-03-31 19:13:40 +01:00
|
|
|
@dataclass
|
2020-01-13 18:06:18 +00:00
|
|
|
class Configuration:
|
|
|
|
"""
|
2021-11-16 00:40:30 +00:00
|
|
|
Represents a configuration option.
|
2020-01-13 18:06:18 +00:00
|
|
|
"""
|
|
|
|
|
2021-03-31 19:13:40 +01:00
|
|
|
id: str
|
|
|
|
type: ConfigDataTypes
|
|
|
|
label: str = None
|
|
|
|
default: str = ""
|
|
|
|
options: List[str] = field(default_factory=list)
|
2022-04-15 00:31:14 +01:00
|
|
|
group: str = "Configuration"
|
2021-03-31 19:13:40 +01:00
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
|
|
|
self.label = self.label if self.label else self.id
|
|
|
|
if self.type == ConfigDataTypes.BOOL:
|
|
|
|
if self.default and self.default not in _BOOL_OPTIONS:
|
|
|
|
raise CoreConfigError(
|
|
|
|
f"{self.id} bool value must be one of: {_BOOL_OPTIONS}: "
|
|
|
|
f"{self.default}"
|
|
|
|
)
|
|
|
|
elif self.type == ConfigDataTypes.FLOAT:
|
|
|
|
if self.default:
|
|
|
|
try:
|
|
|
|
float(self.default)
|
|
|
|
except ValueError:
|
|
|
|
raise CoreConfigError(
|
|
|
|
f"{self.id} is not a valid float: {self.default}"
|
|
|
|
)
|
|
|
|
elif self.type != ConfigDataTypes.STRING:
|
|
|
|
if self.default:
|
|
|
|
try:
|
|
|
|
int(self.default)
|
|
|
|
except ValueError:
|
|
|
|
raise CoreConfigError(
|
|
|
|
f"{self.id} is not a valid int: {self.default}"
|
|
|
|
)
|
2020-01-13 18:06:18 +00:00
|
|
|
|
|
|
|
|
2021-11-16 00:40:30 +00:00
|
|
|
@dataclass
|
|
|
|
class ConfigBool(Configuration):
|
|
|
|
"""
|
|
|
|
Represents a boolean configuration option.
|
|
|
|
"""
|
|
|
|
|
|
|
|
type: ConfigDataTypes = ConfigDataTypes.BOOL
|
2022-04-15 00:31:14 +01:00
|
|
|
value: bool = False
|
2021-11-16 00:40:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ConfigFloat(Configuration):
|
|
|
|
"""
|
|
|
|
Represents a float configuration option.
|
|
|
|
"""
|
|
|
|
|
|
|
|
type: ConfigDataTypes = ConfigDataTypes.FLOAT
|
2022-04-15 00:31:14 +01:00
|
|
|
value: float = 0.0
|
2021-11-16 00:40:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ConfigInt(Configuration):
|
|
|
|
"""
|
|
|
|
Represents an integer configuration option.
|
|
|
|
"""
|
|
|
|
|
|
|
|
type: ConfigDataTypes = ConfigDataTypes.INT32
|
2022-04-15 00:31:14 +01:00
|
|
|
value: int = 0
|
2021-11-16 00:40:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ConfigString(Configuration):
|
|
|
|
"""
|
|
|
|
Represents a string configuration option.
|
|
|
|
"""
|
|
|
|
|
|
|
|
type: ConfigDataTypes = ConfigDataTypes.STRING
|
2022-04-15 00:31:14 +01:00
|
|
|
value: str = ""
|
2021-11-16 00:40:30 +00:00
|
|
|
|
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
class ConfigurableOptions:
|
|
|
|
"""
|
|
|
|
Provides a base for defining configuration options within CORE.
|
|
|
|
"""
|
|
|
|
|
2020-06-12 18:05:49 +01:00
|
|
|
name: Optional[str] = None
|
|
|
|
options: List[Configuration] = []
|
2020-01-13 18:06:18 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def configurations(cls) -> List[Configuration]:
|
|
|
|
"""
|
|
|
|
Provides the configurations for this class.
|
|
|
|
|
|
|
|
:return: configurations
|
|
|
|
"""
|
|
|
|
return cls.options
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def config_groups(cls) -> List[ConfigGroup]:
|
|
|
|
"""
|
|
|
|
Defines how configurations are grouped.
|
|
|
|
|
|
|
|
:return: configuration group definition
|
|
|
|
"""
|
|
|
|
return [ConfigGroup("Options", 1, len(cls.configurations()))]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def default_values(cls) -> Dict[str, str]:
|
|
|
|
"""
|
|
|
|
Provides an ordered mapping of configuration keys to default values.
|
|
|
|
|
|
|
|
:return: ordered configuration mapping default values
|
|
|
|
"""
|
|
|
|
return OrderedDict(
|
|
|
|
[(config.id, config.default) for config in cls.configurations()]
|
|
|
|
)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2019-10-23 17:31:07 +01:00
|
|
|
class ConfigurableManager:
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
2020-01-13 18:06:18 +00:00
|
|
|
Provides convenience methods for storing and retrieving configuration options for
|
|
|
|
nodes.
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
2019-09-10 23:10:24 +01:00
|
|
|
|
2020-06-12 18:05:49 +01:00
|
|
|
_default_node: int = -1
|
|
|
|
_default_type: int = _default_node
|
2018-06-13 19:59:50 +01:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def __init__(self) -> None:
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
|
|
|
Creates a ConfigurableManager object.
|
|
|
|
"""
|
2018-06-13 20:39:50 +01:00
|
|
|
self.node_configurations = {}
|
2018-06-13 19:59:50 +01:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def nodes(self) -> List[int]:
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
|
|
|
Retrieves the ids of all node configurations known by this manager.
|
|
|
|
|
|
|
|
:return: list of node ids
|
|
|
|
"""
|
2019-05-06 05:23:43 +01:00
|
|
|
return [x for x in self.node_configurations if x != self._default_node]
|
2018-06-13 19:59:50 +01:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def config_reset(self, node_id: int = None) -> None:
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
|
|
|
Clears all configurations or configuration for a specific node.
|
|
|
|
|
2020-06-12 16:54:06 +01:00
|
|
|
:param node_id: node id to clear configurations for, default is None and clears
|
|
|
|
all configurations
|
2018-06-13 20:32:01 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2018-06-13 19:59:50 +01:00
|
|
|
if not node_id:
|
2018-06-13 20:39:50 +01:00
|
|
|
self.node_configurations.clear()
|
|
|
|
elif node_id in self.node_configurations:
|
|
|
|
self.node_configurations.pop(node_id)
|
2018-06-13 19:59:50 +01:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def set_config(
|
|
|
|
self,
|
|
|
|
_id: str,
|
|
|
|
value: str,
|
|
|
|
node_id: int = _default_node,
|
|
|
|
config_type: str = _default_type,
|
|
|
|
) -> None:
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
|
|
|
Set a specific configuration value for a node and configuration type.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param _id: configuration key
|
|
|
|
:param value: configuration value
|
|
|
|
:param node_id: node id to store configuration for
|
|
|
|
:param config_type: configuration type to store configuration for
|
2018-06-13 20:32:01 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2018-07-12 05:34:21 +01:00
|
|
|
node_configs = self.node_configurations.setdefault(node_id, OrderedDict())
|
|
|
|
node_type_configs = node_configs.setdefault(config_type, OrderedDict())
|
|
|
|
node_type_configs[_id] = value
|
2018-06-13 19:59:50 +01:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def set_configs(
|
|
|
|
self,
|
|
|
|
config: Dict[str, str],
|
|
|
|
node_id: int = _default_node,
|
|
|
|
config_type: str = _default_type,
|
|
|
|
) -> None:
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
|
|
|
Set configurations for a node and configuration type.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param config: configurations to set
|
|
|
|
:param node_id: node id to store configuration for
|
|
|
|
:param config_type: configuration type to store configuration for
|
2018-06-13 20:32:01 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug(
|
2019-09-10 23:10:24 +01:00
|
|
|
"setting config for node(%s) type(%s): %s", node_id, config_type, config
|
|
|
|
)
|
2018-07-12 05:34:21 +01:00
|
|
|
node_configs = self.node_configurations.setdefault(node_id, OrderedDict())
|
2018-06-13 19:59:50 +01:00
|
|
|
node_configs[config_type] = config
|
|
|
|
|
2019-09-10 23:10:24 +01:00
|
|
|
def get_config(
|
2020-01-13 18:06:18 +00:00
|
|
|
self,
|
|
|
|
_id: str,
|
|
|
|
node_id: int = _default_node,
|
|
|
|
config_type: str = _default_type,
|
|
|
|
default: str = None,
|
|
|
|
) -> str:
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
|
|
|
Retrieves a specific configuration for a node and configuration type.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param _id: specific configuration to retrieve
|
|
|
|
:param node_id: node id to store configuration for
|
|
|
|
:param config_type: configuration type to store configuration for
|
2018-07-12 05:34:21 +01:00
|
|
|
:param default: default value to return when value is not found
|
2018-06-13 20:32:01 +01:00
|
|
|
:return: configuration value
|
|
|
|
"""
|
2018-07-12 05:34:21 +01:00
|
|
|
result = default
|
|
|
|
node_type_configs = self.get_configs(node_id, config_type)
|
|
|
|
if node_type_configs:
|
|
|
|
result = node_type_configs.get(_id, default)
|
|
|
|
return result
|
2018-06-13 19:59:50 +01:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def get_configs(
|
|
|
|
self, node_id: int = _default_node, config_type: str = _default_type
|
2020-09-02 00:19:01 +01:00
|
|
|
) -> Optional[Dict[str, str]]:
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
|
|
|
Retrieve configurations for a node and configuration type.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node_id: node id to store configuration for
|
|
|
|
:param config_type: configuration type to store configuration for
|
2018-06-13 20:32:01 +01:00
|
|
|
:return: configurations
|
|
|
|
"""
|
2018-07-12 05:34:21 +01:00
|
|
|
result = None
|
|
|
|
node_configs = self.node_configurations.get(node_id)
|
|
|
|
if node_configs:
|
|
|
|
result = node_configs.get(config_type)
|
|
|
|
return result
|
2018-06-13 19:59:50 +01:00
|
|
|
|
2020-06-12 16:54:06 +01:00
|
|
|
def get_all_configs(self, node_id: int = _default_node) -> Dict[str, Any]:
|
2018-06-13 20:32:01 +01:00
|
|
|
"""
|
|
|
|
Retrieve all current configuration types for a node.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node_id: node id to retrieve configurations for
|
2018-06-13 20:32:01 +01:00
|
|
|
:return: all configuration types for a node
|
|
|
|
"""
|
2018-07-12 05:34:21 +01:00
|
|
|
return self.node_configurations.get(node_id)
|
2018-06-13 19:59:50 +01:00
|
|
|
|
|
|
|
|
2018-06-14 00:17:47 +01:00
|
|
|
class ModelManager(ConfigurableManager):
|
2018-07-06 22:58:59 +01:00
|
|
|
"""
|
|
|
|
Helps handle setting models for nodes and managing their model configurations.
|
|
|
|
"""
|
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def __init__(self) -> None:
|
2018-07-06 22:58:59 +01:00
|
|
|
"""
|
|
|
|
Creates a ModelManager object.
|
|
|
|
"""
|
2019-10-23 17:51:52 +01:00
|
|
|
super().__init__()
|
2020-06-12 18:05:49 +01:00
|
|
|
self.models: Dict[str, Any] = {}
|
|
|
|
self.node_models: Dict[int, str] = {}
|
2018-06-14 00:17:47 +01:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def set_model_config(
|
|
|
|
self, node_id: int, model_name: str, config: Dict[str, str] = None
|
|
|
|
) -> None:
|
2018-06-14 00:17:47 +01:00
|
|
|
"""
|
|
|
|
Set configuration data for a model.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node_id: node id to set model configuration for
|
|
|
|
:param model_name: model to set configuration for
|
|
|
|
:param config: configuration data to set for model
|
2018-06-14 00:17:47 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
# get model class to configure
|
|
|
|
model_class = self.models.get(model_name)
|
|
|
|
if not model_class:
|
2019-10-18 18:33:31 +01:00
|
|
|
raise ValueError(f"{model_name} is an invalid model")
|
2018-06-14 00:17:47 +01:00
|
|
|
|
|
|
|
# retrieve default values
|
2018-07-12 05:34:21 +01:00
|
|
|
model_config = self.get_model_config(node_id, model_name)
|
2018-06-14 00:17:47 +01:00
|
|
|
if not config:
|
|
|
|
config = {}
|
2019-05-06 05:23:43 +01:00
|
|
|
for key in config:
|
|
|
|
value = config[key]
|
2018-07-12 05:34:21 +01:00
|
|
|
model_config[key] = value
|
2018-06-14 00:17:47 +01:00
|
|
|
|
|
|
|
# set as node model for startup
|
|
|
|
self.node_models[node_id] = model_name
|
|
|
|
|
|
|
|
# set configuration
|
2018-07-12 05:34:21 +01:00
|
|
|
self.set_configs(model_config, node_id=node_id, config_type=model_name)
|
2018-06-14 00:17:47 +01:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def get_model_config(self, node_id: int, model_name: str) -> Dict[str, str]:
|
2018-06-14 00:17:47 +01:00
|
|
|
"""
|
2019-09-13 19:07:04 +01:00
|
|
|
Retrieve configuration data for a model.
|
2018-06-14 00:17:47 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node_id: node id to set model configuration for
|
|
|
|
:param model_name: model to set configuration for
|
2018-06-14 00:17:47 +01:00
|
|
|
:return: current model configuration for node
|
|
|
|
"""
|
|
|
|
# get model class to configure
|
|
|
|
model_class = self.models.get(model_name)
|
|
|
|
if not model_class:
|
2019-10-18 18:33:31 +01:00
|
|
|
raise ValueError(f"{model_name} is an invalid model")
|
2018-06-14 00:17:47 +01:00
|
|
|
|
|
|
|
config = self.get_configs(node_id=node_id, config_type=model_name)
|
|
|
|
if not config:
|
|
|
|
# set default values, when not already set
|
|
|
|
config = model_class.default_values()
|
|
|
|
self.set_configs(config, node_id=node_id, config_type=model_name)
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def set_model(
|
|
|
|
self,
|
|
|
|
node: Union[WlanNode, EmaneNet],
|
2020-01-16 19:17:08 +00:00
|
|
|
model_class: "WirelessModelType",
|
2020-01-13 18:06:18 +00:00
|
|
|
config: Dict[str, str] = None,
|
|
|
|
) -> None:
|
2018-07-06 22:58:59 +01:00
|
|
|
"""
|
|
|
|
Set model and model configuration for node.
|
|
|
|
|
|
|
|
:param node: node to set model for
|
|
|
|
:param model_class: model class to set for node
|
2020-01-16 19:00:57 +00:00
|
|
|
:param config: model configuration, None for default configuration
|
2018-07-06 22:58:59 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug(
|
2019-09-11 23:05:05 +01:00
|
|
|
"setting model(%s) for node(%s): %s", model_class.name, node.id, config
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2019-04-27 06:07:51 +01:00
|
|
|
self.set_model_config(node.id, model_class.name, config)
|
|
|
|
config = self.get_model_config(node.id, model_class.name)
|
2018-06-14 00:17:47 +01:00
|
|
|
node.setmodel(model_class, config)
|
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def get_models(
|
|
|
|
self, node: Union[WlanNode, EmaneNet]
|
|
|
|
) -> List[Tuple[Type, Dict[str, str]]]:
|
2018-06-14 00:17:47 +01:00
|
|
|
"""
|
|
|
|
Return a list of model classes and values for a net if one has been
|
|
|
|
configured. This is invoked when exporting a session to XML.
|
|
|
|
|
|
|
|
:param node: network node to get models for
|
|
|
|
:return: list of model and values tuples for the network node
|
|
|
|
"""
|
2019-04-27 06:07:51 +01:00
|
|
|
all_configs = self.get_all_configs(node.id)
|
2018-07-12 05:34:21 +01:00
|
|
|
if not all_configs:
|
|
|
|
all_configs = {}
|
2018-06-14 00:17:47 +01:00
|
|
|
|
2018-07-12 05:34:21 +01:00
|
|
|
models = []
|
2019-05-06 05:23:43 +01:00
|
|
|
for model_name in all_configs:
|
|
|
|
config = all_configs[model_name]
|
2018-06-29 00:30:55 +01:00
|
|
|
if model_name == ModelManager._default_node:
|
|
|
|
continue
|
2018-06-14 00:17:47 +01:00
|
|
|
model_class = self.models[model_name]
|
|
|
|
models.append((model_class, config))
|
|
|
|
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("models for node(%s): %s", node.id, models)
|
2018-06-14 00:17:47 +01:00
|
|
|
return models
|