Search in sources :

Example 1 with MessageHeader

use of org.finra.herd.model.dto.MessageHeader in project herd by FINRAOS.

the class DefaultNotificationMessageBuilder method buildBusinessObjectFormatVersionChangeMessages.

@Override
public List<NotificationMessage> buildBusinessObjectFormatVersionChangeMessages(BusinessObjectFormatKey businessObjectFormatKey, String oldBusinessObjectFormatVersion) {
    // Create a result list.
    List<NotificationMessage> notificationMessages = new ArrayList<>();
    // Get notification message definitions.
    NotificationMessageDefinitions notificationMessageDefinitions = configurationDaoHelper.getXmlClobPropertyAndUnmarshallToObject(NotificationMessageDefinitions.class, ConfigurationValue.HERD_NOTIFICATION_BUSINESS_OBJECT_FORMAT_VERSION_CHANGE_MESSAGE_DEFINITIONS.getKey());
    // Continue processing if notification message definitions are configured.
    if (notificationMessageDefinitions != null && CollectionUtils.isNotEmpty(notificationMessageDefinitions.getNotificationMessageDefinitions())) {
        // Create a context map of values that can be used when building the message.
        Map<String, Object> velocityContextMap = getBusinessObjectFormatVersionChangeMessageVelocityContextMap(businessObjectFormatKey, oldBusinessObjectFormatVersion);
        // Generate notification message for each notification message definition.
        for (NotificationMessageDefinition notificationMessageDefinition : notificationMessageDefinitions.getNotificationMessageDefinitions()) {
            // Validate the notification message type.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageType())) {
                throw new IllegalStateException(String.format("Notification message type must be specified. Please update \"%s\" configuration entry.", ConfigurationValue.HERD_NOTIFICATION_BUSINESS_OBJECT_FORMAT_VERSION_CHANGE_MESSAGE_DEFINITIONS.getKey()));
            }
            // Validate the notification message destination.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageDestination())) {
                throw new IllegalStateException(String.format("Notification message destination must be specified. Please update \"%s\" configuration entry.", ConfigurationValue.HERD_NOTIFICATION_BUSINESS_OBJECT_FORMAT_VERSION_CHANGE_MESSAGE_DEFINITIONS.getKey()));
            }
            // Evaluate the template to generate the message text.
            String messageText = evaluateVelocityTemplate(notificationMessageDefinition.getMessageVelocityTemplate(), velocityContextMap, "businessObjectFormatVersionChangeEvent");
            // Build a list of optional message headers.
            List<MessageHeader> messageHeaders = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(notificationMessageDefinition.getMessageHeaderDefinitions())) {
                for (MessageHeaderDefinition messageHeaderDefinition : notificationMessageDefinition.getMessageHeaderDefinitions()) {
                    messageHeaders.add(new MessageHeader(messageHeaderDefinition.getKey(), evaluateVelocityTemplate(messageHeaderDefinition.getValueVelocityTemplate(), velocityContextMap, String.format("businessObjectFormatVersionChangeEvent_messageHeader_%s", messageHeaderDefinition.getKey()))));
                }
            }
            // Create a notification message and add it to the result list.
            notificationMessages.add(new NotificationMessage(notificationMessageDefinition.getMessageType(), notificationMessageDefinition.getMessageDestination(), messageText, messageHeaders));
        }
    }
    // Return the results.
    return notificationMessages;
}
Also used : NotificationMessageDefinition(org.finra.herd.model.api.xml.NotificationMessageDefinition) NotificationMessageDefinitions(org.finra.herd.model.api.xml.NotificationMessageDefinitions) MessageHeaderDefinition(org.finra.herd.model.api.xml.MessageHeaderDefinition) NotificationMessage(org.finra.herd.model.dto.NotificationMessage) ArrayList(java.util.ArrayList) MessageHeader(org.finra.herd.model.dto.MessageHeader)

Example 2 with MessageHeader

use of org.finra.herd.model.dto.MessageHeader in project herd by FINRAOS.

the class DefaultNotificationMessageBuilder method buildStorageUnitStatusChangeMessages.

@Override
public List<NotificationMessage> buildStorageUnitStatusChangeMessages(BusinessObjectDataKey businessObjectDataKey, String storageName, String newStorageUnitStatus, String oldStorageUnitStatus) {
    // Create a result list.
    List<NotificationMessage> notificationMessages = new ArrayList<>();
    // Get notification message definitions.
    NotificationMessageDefinitions notificationMessageDefinitions = configurationDaoHelper.getXmlClobPropertyAndUnmarshallToObject(NotificationMessageDefinitions.class, ConfigurationValue.HERD_NOTIFICATION_STORAGE_UNIT_STATUS_CHANGE_MESSAGE_DEFINITIONS.getKey());
    // Continue processing if notification message definitions are configured.
    if (notificationMessageDefinitions != null && CollectionUtils.isNotEmpty(notificationMessageDefinitions.getNotificationMessageDefinitions())) {
        // Create a context map of values that can be used when building the message.
        Map<String, Object> velocityContextMap = getStorageUnitStatusChangeMessageVelocityContextMap(businessObjectDataKey, storageName, newStorageUnitStatus, oldStorageUnitStatus);
        // Generate notification message for each notification message definition.
        for (NotificationMessageDefinition notificationMessageDefinition : notificationMessageDefinitions.getNotificationMessageDefinitions()) {
            // Validate the notification message type.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageType())) {
                throw new IllegalStateException(String.format("Notification message type must be specified. Please update \"%s\" configuration entry.", ConfigurationValue.HERD_NOTIFICATION_STORAGE_UNIT_STATUS_CHANGE_MESSAGE_DEFINITIONS.getKey()));
            }
            // Validate the notification message destination.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageDestination())) {
                throw new IllegalStateException(String.format("Notification message destination must be specified. Please update \"%s\" configuration entry.", ConfigurationValue.HERD_NOTIFICATION_STORAGE_UNIT_STATUS_CHANGE_MESSAGE_DEFINITIONS.getKey()));
            }
            // Evaluate the template to generate the message text.
            String messageText = evaluateVelocityTemplate(notificationMessageDefinition.getMessageVelocityTemplate(), velocityContextMap, "storageUnitStatusChangeEvent");
            // Build a list of optional message headers.
            List<MessageHeader> messageHeaders = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(notificationMessageDefinition.getMessageHeaderDefinitions())) {
                for (MessageHeaderDefinition messageHeaderDefinition : notificationMessageDefinition.getMessageHeaderDefinitions()) {
                    messageHeaders.add(new MessageHeader(messageHeaderDefinition.getKey(), evaluateVelocityTemplate(messageHeaderDefinition.getValueVelocityTemplate(), velocityContextMap, String.format("storageUnitStatusChangeEvent_messageHeader_%s", messageHeaderDefinition.getKey()))));
                }
            }
            // Create a notification message and add it to the result list.
            notificationMessages.add(new NotificationMessage(notificationMessageDefinition.getMessageType(), notificationMessageDefinition.getMessageDestination(), messageText, messageHeaders));
        }
    }
    // Return the results.
    return notificationMessages;
}
Also used : NotificationMessageDefinition(org.finra.herd.model.api.xml.NotificationMessageDefinition) NotificationMessageDefinitions(org.finra.herd.model.api.xml.NotificationMessageDefinitions) MessageHeaderDefinition(org.finra.herd.model.api.xml.MessageHeaderDefinition) NotificationMessage(org.finra.herd.model.dto.NotificationMessage) ArrayList(java.util.ArrayList) MessageHeader(org.finra.herd.model.dto.MessageHeader)

Example 3 with MessageHeader

use of org.finra.herd.model.dto.MessageHeader in project herd by FINRAOS.

the class NotificationMessagePublishingServiceImpl method publishOldestNotificationMessageFromDatabaseQueueImpl.

/**
 * Publishes and removes from the database queue the oldest notification message.
 *
 * @return true if notification message was successfully published and false otherwise
 */
@SuppressWarnings("unchecked")
protected boolean publishOldestNotificationMessageFromDatabaseQueueImpl() {
    // Initialize the result flag to false.
    boolean result = false;
    // Retrieve the oldest notification message from the database queue, unless the queue is empty.
    NotificationMessageEntity notificationMessageEntity = notificationMessageDao.getOldestNotificationMessage();
    // If message is retrieved, publish and remove it from the queue.
    if (notificationMessageEntity != null) {
        // Get the message headers from the entity.
        List<MessageHeader> messageHeaders = null;
        if (StringUtils.isNotBlank(notificationMessageEntity.getMessageHeaders())) {
            try {
                messageHeaders = jsonHelper.unmarshallJsonToListOfObjects(MessageHeader.class, notificationMessageEntity.getMessageHeaders());
            } catch (IOException e) {
                throw new IllegalStateException(String.format("Failed to unmarshall notification message headers. " + "messageId=%d messageType=%s messageDestination=%s messageText=%s messageHeaders=%s", notificationMessageEntity.getId(), notificationMessageEntity.getMessageType().getCode(), notificationMessageEntity.getMessageDestination(), notificationMessageEntity.getMessageText(), notificationMessageEntity.getMessageHeaders()), e);
            }
        }
        // Publish notification message.
        publishNotificationMessageImpl(new NotificationMessage(notificationMessageEntity.getMessageType().getCode(), notificationMessageEntity.getMessageDestination(), notificationMessageEntity.getMessageText(), messageHeaders));
        // Delete this message from the queue.
        notificationMessageDao.delete(notificationMessageEntity);
        // Set the result flag to true.
        result = true;
    }
    return result;
}
Also used : NotificationMessage(org.finra.herd.model.dto.NotificationMessage) NotificationMessageEntity(org.finra.herd.model.jpa.NotificationMessageEntity) MessageHeader(org.finra.herd.model.dto.MessageHeader) IOException(java.io.IOException)

Example 4 with MessageHeader

use of org.finra.herd.model.dto.MessageHeader in project herd by FINRAOS.

the class NotificationMessagePublishingServiceTest method testNotificationPublishingServiceMethodsNewTransactionPropagation.

@Test
public void testNotificationPublishingServiceMethodsNewTransactionPropagation() {
    // Validate that notification message database queue is empty.
    assertNull(notificationMessageDao.getOldestNotificationMessage());
    // Add a notification message to the database queue.
    notificationMessagePublishingServiceImpl.addNotificationMessageToDatabaseQueue(new NotificationMessage(MessageTypeEntity.MessageEventTypes.SQS.name(), MESSAGE_DESTINATION, MESSAGE_TEXT, Collections.singletonList(new MessageHeader(KEY, VALUE))));
    // Validate that the database queue is not empty now.
    assertNotNull(notificationMessageDao.getOldestNotificationMessage());
    // Publish the notification message from the database queue.
    assertTrue(notificationMessagePublishingServiceImpl.publishOldestNotificationMessageFromDatabaseQueue());
    // Publish notification message directly - not from the database queue.
    notificationMessagePublishingServiceImpl.publishNotificationMessage(new NotificationMessage(MessageTypeEntity.MessageEventTypes.SQS.name(), MESSAGE_DESTINATION, MESSAGE_TEXT, Collections.singletonList(new MessageHeader(KEY, VALUE))));
}
Also used : NotificationMessage(org.finra.herd.model.dto.NotificationMessage) MessageHeader(org.finra.herd.model.dto.MessageHeader) Test(org.junit.Test)

Example 5 with MessageHeader

use of org.finra.herd.model.dto.MessageHeader in project herd by FINRAOS.

the class NotificationMessagePublishingServiceTest method testPublishNotificationMessageInvalidMessageType.

@Test
public void testPublishNotificationMessageInvalidMessageType() {
    // Try to publish notification message with an invalid message type.
    try {
        notificationMessagePublishingService.publishNotificationMessage(new NotificationMessage(I_DO_NOT_EXIST, MESSAGE_DESTINATION, MESSAGE_TEXT, Collections.singletonList(new MessageHeader(KEY, VALUE))));
        fail();
    } catch (IllegalStateException e) {
        assertEquals(String.format("Notification message type \"%s\" is not supported.", I_DO_NOT_EXIST), e.getMessage());
    }
}
Also used : NotificationMessage(org.finra.herd.model.dto.NotificationMessage) MessageHeader(org.finra.herd.model.dto.MessageHeader) Test(org.junit.Test)

Aggregations

MessageHeader (org.finra.herd.model.dto.MessageHeader)16 NotificationMessage (org.finra.herd.model.dto.NotificationMessage)13 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)4 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)4 MessageHeaderDefinition (org.finra.herd.model.api.xml.MessageHeaderDefinition)4 NotificationMessageDefinition (org.finra.herd.model.api.xml.NotificationMessageDefinition)4 NotificationMessageDefinitions (org.finra.herd.model.api.xml.NotificationMessageDefinitions)4 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)4 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AwsParamsDto (org.finra.herd.model.dto.AwsParamsDto)2 NotificationMessageEntity (org.finra.herd.model.jpa.NotificationMessageEntity)2 PublishResult (com.amazonaws.services.sns.model.PublishResult)1 SendMessageResult (com.amazonaws.services.sqs.model.SendMessageResult)1 IOException (java.io.IOException)1 LogLevel (org.finra.herd.core.helper.LogLevel)1 BusinessObjectDataInvalidateUnregisteredRequest (org.finra.herd.model.api.xml.BusinessObjectDataInvalidateUnregisteredRequest)1 BusinessObjectFormatKey (org.finra.herd.model.api.xml.BusinessObjectFormatKey)1 BusinessObjectFormatEntity (org.finra.herd.model.jpa.BusinessObjectFormatEntity)1 ConfigurationEntity (org.finra.herd.model.jpa.ConfigurationEntity)1