class NotificationBase(base.IronicObject): """Base class for versioned notifications. Subclasses must define the "payload" field, which must be a subclass of NotificationPayloadBase. """ # Version 1.0: Initial version VERSION = '1.0' fields = { 'level': fields.NotificationLevelField(), 'event_type': fields.ObjectField('EventType'), 'publisher': fields.ObjectField('NotificationPublisher') } # NOTE(mariojv) This may be a candidate for something oslo.messaging # implements instead of in ironic. def _should_notify(self): """Determine whether the notification should be sent. A notification is sent when the level of the notification is greater than or equal to the level specified in the configuration, in the increasing order of DEBUG, INFO, WARNING, ERROR, CRITICAL. :return: True if notification should be sent, False otherwise. """ if CONF.notification_level is None: return False return (NOTIFY_LEVELS[self.level] >= NOTIFY_LEVELS[CONF.notification_level]) def emit(self, context): """Send the notification. :raises NotificationPayloadError :raises oslo_versionedobjects.exceptions.MessageDeliveryFailure """ if not self._should_notify(): return if not self.payload.populated: raise exception.NotificationPayloadError( class_name=self.__class__.__name__) # NOTE(mariojv) By default, oslo_versionedobjects includes a list # of "changed fields" for the object in the output of # obj_to_primitive. This is unneeded since every field of the # object will look changed, since each payload is a newly created # object, so we drop the changes. self.payload.obj_reset_changes() event_type = self.event_type.to_event_type_field() publisher_id = '%s.%s' % (self.publisher.service, self.publisher.host) payload = self.payload.obj_to_primitive() notifier = rpc.get_versioned_notifier(publisher_id) notify = getattr(notifier, self.level) notify(context, event_type=event_type, payload=payload)
def setUp(self): super(TestNotificationLevelField, self).setUp() self.field = fields.NotificationLevelField()