Example #1
0
    def _add_new_class(self, device_id, class_id, instance_id, attributes):
        """
        Create an entry for a new class in the external database

        :param device_id: (str) ONU Device ID
        :param class_id: (int) ME Class ID
        :param instance_id: (int) ME Entity ID
        :param attributes: (dict) Attribute dictionary

        :returns: (bool) True if the value was saved to the database. False if the
                         value was identical to the current instance
        """
        self.log.debug('add',
                       device_id=device_id,
                       class_id=class_id,
                       instance_id=instance_id,
                       attributes=attributes)

        now = self._time_to_string(datetime.utcnow())
        attrs = [
            AlarmAttributeData(name=k,
                               value=self._attribute_to_string(
                                   device_id, class_id, k, v))
            for k, v in attributes.items()
        ]
        class_data = AlarmClassData(class_id=class_id,
                                    instances=[
                                        AlarmInstanceData(
                                            instance_id=instance_id,
                                            created=now,
                                            modified=now,
                                            attributes=attrs)
                                    ])

        self._root_proxy.add(AlarmDbExternal.CLASSES_PATH.format(device_id),
                             class_data)
        self.log.debug('set-complete',
                       device_id=device_id,
                       class_id=class_id,
                       entity_id=instance_id,
                       attributes=attributes)
        return True
Example #2
0
    def _class_proxy(self, device_id, class_id, create=False):
        """
        Get a config proxy to a specific managed entity class
        :param device_id: (str) ONU Device ID
        :param class_id: (int) Class ID
        :param create: (bool) If true, create default instance (and class)
        :return: (ConfigProxy) Class configuration proxy

        :raises DatabaseStateError: If database is not started
        :raises KeyError: If Instance does not exist and 'create' is False
        """
        if not self._started:
            raise DatabaseStateError('The Database is not currently active')

        if not 0 <= class_id <= 0xFFFF:
            raise ValueError('class-id is 0..0xFFFF')

        fmt = AlarmDbExternal.DEVICE_PATH + AlarmDbExternal.CLASS_PATH
        path = fmt.format(device_id, class_id)

        try:
            return self._core.get_proxy(path)

        except KeyError:
            if not create:
                self.log.error('class-proxy-does-not-exist',
                               device_id=device_id,
                               class_id=class_id)
                raise

        # Create class
        data = AlarmClassData(class_id=class_id)
        root_path = AlarmDbExternal.CLASSES_PATH.format(device_id)
        self._root_proxy.add(root_path, data)

        return self._core.get_proxy(path)