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 = MibDbExternal.DEVICE_PATH + MibDbExternal.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 = MibClassData(class_id=class_id) root_path = MibDbExternal.CLASSES_PATH.format(device_id) self._root_proxy.add(root_path, data) return self._core.get_proxy(path)
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 = [MibAttributeData(name=k, value=self._attribute_to_string(device_id, class_id, k, v)) for k, v in attributes.items()] class_data = MibClassData(class_id=class_id, instances=[MibInstanceData(instance_id=instance_id, created=now, modified=now, attributes=attrs)]) self._root_proxy.add(MibDbExternal.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
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 = [MibAttributeData(name=k, # value=self._attribute_to_string(device_id, # class_id, # k, # v)) for k, v in attributes.items()] attrs = [] for k, v in attributes.items(): if k == 'serial_number': vendor_id = str(v[0:4]) vendor_specific = v[4:] vendor_specific = str(vendor_specific.encode('hex')) str_value = vendor_id + vendor_specific attrs.append(MibAttributeData(name=k, value=str_value)) else: str_value = self._attribute_to_string(device_id, class_id, k, v) attrs.append(MibAttributeData(name=k, value=str_value)) class_data = MibClassData(class_id=class_id, instances=[ MibInstanceData(instance_id=instance_id, created=now, modified=now, attributes=attrs) ]) self._root_proxy.add(MibDbExternal.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
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 = MibDbExternal.DEVICE_PATH + MibDbExternal.CLASS_PATH path = fmt.format(device_id, class_id) try: return self._core.get_proxy(path) except KeyError: if not create: # This can occur right after a MIB Reset if the ONU publishes AVCs right away # and during the MIB audit resync for ONU created MEs in response to an OLT # created ME. Fail since for these test cases they occur during a verification # 'query' and not the ME creation during resync. Calling code should handle # they exception if it is expected to occur on occasion. self.log.debug('class-proxy-does-not-exist', device_id=device_id, class_id=class_id) raise # Create class data = MibClassData(class_id=class_id) root_path = MibDbExternal.CLASSES_PATH.format(device_id) self._root_proxy.add(root_path, data) return self._core.get_proxy(path)