fixes handling of lifetimes in config, from days to seconds

This commit is contained in:
Ronald Steinke 2018-01-10 10:14:42 +01:00
parent 1242ba060d
commit d87a075786
2 changed files with 15 additions and 16 deletions

View File

@ -661,17 +661,13 @@ class OneM2MDefaultController(LoggerMixin):
except ParseError as e: except ParseError as e:
raise CSEValueError( raise CSEValueError(
"Illegal value for expirationTime: %s" % (e,)) "Illegal value for expirationTime: %s" % (e,))
if expiration_time < self.now + self.global_config[ if expiration_time < self.now + self.global_config["min_lifetime"]:
"min_lifetime"]:
self.logger.warn("expirationTime is too low. Adjusting") self.logger.warn("expirationTime is too low. Adjusting")
expiration_time = self.now + self.global_config[ expiration_time = self.now + self.global_config["min_lifetime"]
"min_lifetime"]
self.fields.append("expirationTime") self.fields.append("expirationTime")
elif expiration_time > self.now + self.global_config[ elif expiration_time > self.now + self.global_config["max_lifetime"]:
"max_lifetime"]:
self.logger.warn("expirationTime is too high. Adjusting") self.logger.warn("expirationTime is too high. Adjusting")
expiration_time = self.now + self.global_config[ expiration_time = self.now + self.global_config["max_lifetime"]
"max_lifetime"]
self.fields.append("expirationTime") self.fields.append("expirationTime")
values["expirationTime"] = expiration_time values["expirationTime"] = expiration_time
@ -693,8 +689,7 @@ class OneM2MDefaultController(LoggerMixin):
values[attribute.name] is not None) values[attribute.name] is not None)
# TODO(rkr): check mandatory attributes # TODO(rkr): check mandatory attributes
if not have_attr and attribute.mandatory: if not have_attr and attribute.mandatory:
raise CSEMissingValue("Missing attribute: %s" % raise CSEMissingValue("Missing attribute: %s" % (attribute.name,))
(attribute.name,))
if have_attr and attribute.accesstype == attribute.RO: if have_attr and attribute.accesstype == attribute.RO:
self._handle_ro_attribute(attribute) self._handle_ro_attribute(attribute)

View File

@ -3,18 +3,22 @@ from openmtc.configuration import (Configuration, BooleanOption, ListOption,
LowerCaseEnumOption, LogLevel, SimpleOption) LowerCaseEnumOption, LogLevel, SimpleOption)
def timedelta_in_seconds(sec):
return timedelta(seconds=sec)
class GlobalConfiguration(Configuration): class GlobalConfiguration(Configuration):
__name__ = "global configuration" __name__ = "global configuration"
__options__ = {"disable_forwarding": BooleanOption(default=False), __options__ = {"disable_forwarding": BooleanOption(default=False),
"default_lifetime": SimpleOption(type=int, "default_lifetime": SimpleOption(type=int,
default=timedelta(60 * 60), default=timedelta_in_seconds(60 * 60),
converter=timedelta), converter=timedelta_in_seconds),
"max_lifetime": SimpleOption(type=int, "max_lifetime": SimpleOption(type=int,
default=timedelta(60 * 60 * 24), default=timedelta_in_seconds(60 * 60 * 24),
converter=timedelta), converter=timedelta_in_seconds),
"min_lifetime": SimpleOption(type=int, "min_lifetime": SimpleOption(type=int,
default=timedelta(5), default=timedelta_in_seconds(5),
converter=timedelta), converter=timedelta_in_seconds),
"additional_host_names": ListOption(str), "additional_host_names": ListOption(str),
"require_auth": BooleanOption(default=False), "require_auth": BooleanOption(default=False),
"default_content_type": SimpleOption()} "default_content_type": SimpleOption()}