refactored how getters for configurations worked, to avoid setting defaults and causing undesireable states

This commit is contained in:
Blake J. Harnden 2018-07-11 21:34:21 -07:00
parent b03662dbeb
commit 3d59a68299
6 changed files with 46 additions and 47 deletions

View file

@ -152,16 +152,6 @@ class ConfigurableManager(object):
""" """
return [node_id for node_id in self.node_configurations.iterkeys() if node_id != self._default_node] return [node_id for node_id in self.node_configurations.iterkeys() if node_id != self._default_node]
def has_configs(self, node_id):
"""
Checks if this manager contains a configuration for the node id.
:param int node_id: node id to check for a configuration
:return: True if a node configuration exists, False otherwise
:rtype: bool
"""
return node_id in self.node_configurations
def config_reset(self, node_id=None): def config_reset(self, node_id=None):
""" """
Clears all configurations or configuration for a specific node. Clears all configurations or configuration for a specific node.
@ -186,8 +176,9 @@ class ConfigurableManager(object):
:return: nothing :return: nothing
""" """
logger.debug("setting config for node(%s) type(%s): %s=%s", node_id, config_type, _id, value) logger.debug("setting config for node(%s) type(%s): %s=%s", node_id, config_type, _id, value)
node_type_map = self.get_configs(node_id, config_type) node_configs = self.node_configurations.setdefault(node_id, OrderedDict())
node_type_map[_id] = value node_type_configs = node_configs.setdefault(config_type, OrderedDict())
node_type_configs[_id] = value
def set_configs(self, config, node_id=_default_node, config_type=_default_type): def set_configs(self, config, node_id=_default_node, config_type=_default_type):
""" """
@ -199,9 +190,7 @@ class ConfigurableManager(object):
:return: nothing :return: nothing
""" """
logger.debug("setting config for node(%s) type(%s): %s", node_id, config_type, config) logger.debug("setting config for node(%s) type(%s): %s", node_id, config_type, config)
node_configs = self.get_all_configs(node_id) node_configs = self.node_configurations.setdefault(node_id, OrderedDict())
if config_type in node_configs:
node_configs.pop(config_type)
node_configs[config_type] = config node_configs[config_type] = config
def get_config(self, _id, node_id=_default_node, config_type=_default_type, default=None): def get_config(self, _id, node_id=_default_node, config_type=_default_type, default=None):
@ -211,13 +200,16 @@ class ConfigurableManager(object):
:param str _id: specific configuration to retrieve :param str _id: specific configuration to retrieve
:param int node_id: node id to store configuration for :param int node_id: node id to store configuration for
:param str config_type: configuration type to store configuration for :param str config_type: configuration type to store configuration for
:param default: :param default: default value to return when value is not found
:return: configuration value :return: configuration value
:rtype str :rtype str
""" """
logger.debug("getting config for node(%s) type(%s): %s", node_id, config_type, _id) logger.debug("getting config for node(%s) type(%s): %s", node_id, config_type, _id)
node_type_map = self.get_configs(node_id, config_type) result = default
return node_type_map.get(_id, default) node_type_configs = self.get_configs(node_id, config_type)
if node_type_configs:
result = node_type_configs.get(_id, default)
return result
def get_configs(self, node_id=_default_node, config_type=_default_type): def get_configs(self, node_id=_default_node, config_type=_default_type):
""" """
@ -229,8 +221,11 @@ class ConfigurableManager(object):
:rtype: dict :rtype: dict
""" """
logger.debug("getting configs for node(%s) type(%s)", node_id, config_type) logger.debug("getting configs for node(%s) type(%s)", node_id, config_type)
node_map = self.get_all_configs(node_id) result = None
return node_map.setdefault(config_type, {}) node_configs = self.node_configurations.get(node_id)
if node_configs:
result = node_configs.get(config_type)
return result
def get_all_configs(self, node_id=_default_node): def get_all_configs(self, node_id=_default_node):
""" """
@ -241,7 +236,7 @@ class ConfigurableManager(object):
:rtype: dict :rtype: dict
""" """
logger.debug("getting all configs for node(%s)", node_id) logger.debug("getting all configs for node(%s)", node_id)
return self.node_configurations.setdefault(node_id, OrderedDict()) return self.node_configurations.get(node_id)
class ConfigGroup(object): class ConfigGroup(object):
@ -331,17 +326,17 @@ class ModelManager(ConfigurableManager):
raise ValueError("%s is an invalid model" % model_name) raise ValueError("%s is an invalid model" % model_name)
# retrieve default values # retrieve default values
node_config = self.get_model_config(node_id, model_name) model_config = self.get_model_config(node_id, model_name)
if not config: if not config:
config = {} config = {}
for key, value in config.iteritems(): for key, value in config.iteritems():
node_config[key] = value model_config[key] = value
# set as node model for startup # set as node model for startup
self.node_models[node_id] = model_name self.node_models[node_id] = model_name
# set configuration # set configuration
self.set_configs(node_config, node_id=node_id, config_type=model_name) self.set_configs(model_config, node_id=node_id, config_type=model_name)
def get_model_config(self, node_id, model_name): def get_model_config(self, node_id, model_name):
""" """
@ -388,16 +383,15 @@ class ModelManager(ConfigurableManager):
:return: list of model and values tuples for the network node :return: list of model and values tuples for the network node
:rtype: list :rtype: list
""" """
models = [] all_configs = self.get_all_configs(node.objid)
all_configs = {} if not all_configs:
if self.has_configs(node_id=node.objid): all_configs = {}
all_configs = self.get_all_configs(node_id=node.objid)
for model_name in all_configs.iterkeys(): models = []
for model_name, config in all_configs.iteritems():
if model_name == ModelManager._default_node: if model_name == ModelManager._default_node:
continue continue
model_class = self.models[model_name] model_class = self.models[model_name]
config = self.get_configs(node_id=node.objid, config_type=model_name)
models.append((model_class, config)) models.append((model_class, config))
logger.debug("models for node(%s): %s", node.objid, models) logger.debug("models for node(%s): %s", node.objid, models)

View file

@ -120,17 +120,14 @@ class EmaneManager(ModelManager):
key += interface.netindex key += interface.netindex
# try retrieve interface specific configuration, avoid getting defaults # try retrieve interface specific configuration, avoid getting defaults
config = {} config = self.get_configs(node_id=key, config_type=model_name)
if self.has_configs(key):
config = self.get_configs(key)
# otherwise retrieve the interfaces node configuration, avoid using defaults # otherwise retrieve the interfaces node configuration, avoid using defaults
if not config and self.has_configs(interface.node.objid): if not config:
config = self.get_configs(node_id=interface.node.objid, config_type=model_name) config = self.get_configs(node_id=interface.node.objid, config_type=model_name)
# get non interface config, when none found # get non interface config, when none found
# if not config and interface.transport_type == "raw": if not config:
if not config and self.has_configs(node_id):
# with EMANE 0.9.2+, we need an extra NEM XML from # with EMANE 0.9.2+, we need an extra NEM XML from
# model.buildnemxmlfiles(), so defaults are returned here # model.buildnemxmlfiles(), so defaults are returned here
config = self.get_configs(node_id=node_id, config_type=model_name) config = self.get_configs(node_id=node_id, config_type=model_name)

View file

@ -73,8 +73,7 @@ class MobilityManager(ModelManager):
continue continue
for model_name in self.models.iterkeys(): for model_name in self.models.iterkeys():
all_configs = self.get_all_configs(node_id) config = self.get_configs(node_id, model_name)
config = all_configs.get(model_name)
if not config: if not config:
continue continue
model_class = self.models[model_name] model_class = self.models[model_name]

View file

@ -23,11 +23,8 @@ class EmaneTransportService(CoreService):
for interface in node.netifs(sort=True): for interface in node.netifs(sort=True):
network_node = node.session.get_object(interface.net.objid) network_node = node.session.get_object(interface.net.objid)
if nodeutils.is_node(network_node, NodeTypes.EMANE): if nodeutils.is_node(network_node, NodeTypes.EMANE):
if not node.session.emane.has_configs(network_node.objid): config = node.session.emane.get_configs(network_node.objid, network_node.model.name)
continue if config and emanexml.is_external(config):
all_configs = node.session.emane.get_all_configs(network_node.objid)
config = all_configs.get(network_node.model.name)
if emanexml.is_external(config):
nem_id = network_node.getnemid(interface) nem_id = network_node.getnemid(interface)
command = "emanetransportd -r -l 0 -d ../transportdaemon%s.xml" % nem_id command = "emanetransportd -r -l 0 -d ../transportdaemon%s.xml" % nem_id
transport_commands.append(command) transport_commands.append(command)

View file

@ -496,11 +496,13 @@ class CoreXmlWriter(object):
self.scenario.append(hooks) self.scenario.append(hooks)
def write_session_options(self): def write_session_options(self):
# options
option_elements = etree.Element("session_options") option_elements = etree.Element("session_options")
# TODO: should we just save the current config regardless, since it may change?
options_config = self.session.options.get_configs() options_config = self.session.options.get_configs()
if not options_config:
return
for _id, default_value in self.session.options.default_values().iteritems(): for _id, default_value in self.session.options.default_values().iteritems():
# TODO: should we just save the current config regardless, since it may change?
value = options_config[_id] value = options_config[_id]
if value != default_value: if value != default_value:
add_configuration(option_elements, _id, value) add_configuration(option_elements, _id, value)
@ -511,7 +513,11 @@ class CoreXmlWriter(object):
def write_session_metadata(self): def write_session_metadata(self):
# metadata # metadata
metadata_elements = etree.Element("session_metadata") metadata_elements = etree.Element("session_metadata")
for _id, value in self.session.metadata.get_configs().iteritems(): config = self.session.metadata.get_configs()
if not config:
return
for _id, value in config.iteritems():
add_configuration(metadata_elements, _id, value) add_configuration(metadata_elements, _id, value)
if metadata_elements.getchildren(): if metadata_elements.getchildren():
@ -521,6 +527,9 @@ class CoreXmlWriter(object):
emane_configurations = etree.Element("emane_configurations") emane_configurations = etree.Element("emane_configurations")
for node_id in self.session.emane.nodes(): for node_id in self.session.emane.nodes():
all_configs = self.session.emane.get_all_configs(node_id) all_configs = self.session.emane.get_all_configs(node_id)
if not all_configs:
continue
for model_name, config in all_configs.iteritems(): for model_name, config in all_configs.iteritems():
logger.info("writing emane config node(%s) model(%s)", node_id, model_name) logger.info("writing emane config node(%s) model(%s)", node_id, model_name)
if model_name == -1: if model_name == -1:
@ -537,6 +546,9 @@ class CoreXmlWriter(object):
mobility_configurations = etree.Element("mobility_configurations") mobility_configurations = etree.Element("mobility_configurations")
for node_id in self.session.mobility.nodes(): for node_id in self.session.mobility.nodes():
all_configs = self.session.mobility.get_all_configs(node_id) all_configs = self.session.mobility.get_all_configs(node_id)
if not all_configs:
continue
for model_name, config in all_configs.iteritems(): for model_name, config in all_configs.iteritems():
logger.info("writing mobility config node(%s) model(%s)", node_id, model_name) logger.info("writing mobility config node(%s) model(%s)", node_id, model_name)
mobility_configuration = etree.SubElement(mobility_configurations, "mobility_configuration") mobility_configuration = etree.SubElement(mobility_configurations, "mobility_configuration")

View file

@ -85,7 +85,7 @@ class TestConf:
config_manager.config_reset(node_id) config_manager.config_reset(node_id)
# then # then
assert not config_manager.has_configs(node_id) assert not config_manager.get_configs(node_id=node_id)
assert config_manager.get_configs() assert config_manager.get_configs()
def test_configs_setget(self): def test_configs_setget(self):