Search in sources :

Example 1 with RetryHeader

use of org.opensmartgridplatform.shared.infra.jms.RetryHeader in project open-smart-grid-platform by OSGP.

the class SigningService method doSignMessage.

private void doSignMessage(final UnsignedOslpEnvelopeDto unsignedOslpEnvelopeDto, final String correlationUid, final String deviceIdentification, final Destination replyToQueue) {
    final byte[] deviceId = unsignedOslpEnvelopeDto.getDeviceId();
    final byte[] sequenceNumber = unsignedOslpEnvelopeDto.getSequenceNumber();
    final Message payloadMessage = unsignedOslpEnvelopeDto.getPayloadMessage();
    final String organisationIdentification = unsignedOslpEnvelopeDto.getOrganisationIdentification();
    final int messagePriority = unsignedOslpEnvelopeDto.getMessagePriority();
    final boolean scheduled = unsignedOslpEnvelopeDto.isScheduled();
    final OslpEnvelope oslpEnvelope = new OslpEnvelope.Builder().withDeviceId(deviceId).withSequenceNumber(sequenceNumber).withPrimaryKey(this.privateKey).withSignature(this.signature).withProvider(this.signatureProvider).withPayloadMessage(payloadMessage).build();
    ResponseMessage responseMessage;
    if (oslpEnvelope == null) {
        LOGGER.error("Message for device: {} with correlationId: {} NOT SIGNED, sending error to protocol-adapter", deviceIdentification, correlationUid);
        responseMessage = ResponseMessage.newResponseMessageBuilder().withCorrelationUid(correlationUid).withOrganisationIdentification(organisationIdentification).withDeviceIdentification(deviceIdentification).withResult(ResponseMessageResultType.NOT_OK).withOsgpException(new OsgpException(ComponentType.UNKNOWN, "Failed to build signed OslpEnvelope", null)).withDataObject(unsignedOslpEnvelopeDto).withMessagePriority(messagePriority).withScheduled(scheduled).withRetryHeader(new RetryHeader()).build();
    } else {
        LOGGER.info("Message for device: {} with correlationId: {} signed, sending response to protocol-adapter", deviceIdentification, correlationUid);
        final SignedOslpEnvelopeDto signedOslpEnvelopeDto = new SignedOslpEnvelopeDto(oslpEnvelope, unsignedOslpEnvelopeDto);
        responseMessage = ResponseMessage.newResponseMessageBuilder().withCorrelationUid(correlationUid).withOrganisationIdentification(organisationIdentification).withDeviceIdentification(deviceIdentification).withResult(ResponseMessageResultType.OK).withDataObject(signedOslpEnvelopeDto).withMessagePriority(messagePriority).withScheduled(scheduled).withRetryHeader(new RetryHeader()).build();
    }
    this.signingServerResponseMessageSender.send(responseMessage, "SIGNING_RESPONSE", replyToQueue);
}
Also used : OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) ResponseMessage(org.opensmartgridplatform.shared.infra.jms.ResponseMessage) Message(org.opensmartgridplatform.oslp.Oslp.Message) SignedOslpEnvelopeDto(org.opensmartgridplatform.oslp.SignedOslpEnvelopeDto) ResponseMessage(org.opensmartgridplatform.shared.infra.jms.ResponseMessage) RetryHeader(org.opensmartgridplatform.shared.infra.jms.RetryHeader) OslpEnvelope(org.opensmartgridplatform.oslp.OslpEnvelope)

Example 2 with RetryHeader

use of org.opensmartgridplatform.shared.infra.jms.RetryHeader in project open-smart-grid-platform by OSGP.

the class DeviceResponseMessageServiceTest method testProcessScheduledMessageFailed.

/**
 * test processMessage with a scheduled task that failed
 */
@Test
public void testProcessScheduledMessageFailed() {
    final ResponseMessageResultType result = ResponseMessageResultType.NOT_OK;
    final Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);
    final Date scheduledRetryTime = calendar.getTime();
    // since the retryCount equals the maxRetries, the message should fail
    final RetryHeader retryHeader = new RetryHeader(1, 1, scheduledRetryTime);
    final ProtocolResponseMessage message = new ProtocolResponseMessage.Builder().messageMetadata(MESSAGE_METADATA.builder().withScheduled(true).build()).result(result).dataObject(DATA_OBJECT).retryHeader(retryHeader).build();
    final ScheduledTask scheduledTask = new ScheduledTask(MESSAGE_METADATA, DOMAIN, DOMAIN_VERSION, DATA_OBJECT, SCHEDULED_TIME);
    when(this.scheduledTaskService.findByCorrelationUid(anyString())).thenReturn(scheduledTask);
    this.deviceResponseMessageService.processMessage(message);
    // check if message is sent and task is deleted
    verify(this.domainResponseMessageSender).send(message);
    verify(this.scheduledTaskService).deleteScheduledTask(scheduledTask);
}
Also used : ProtocolResponseMessage(org.opensmartgridplatform.shared.infra.jms.ProtocolResponseMessage) Calendar(java.util.Calendar) ScheduledTask(org.opensmartgridplatform.domain.core.entities.ScheduledTask) RetryHeader(org.opensmartgridplatform.shared.infra.jms.RetryHeader) ResponseMessageResultType(org.opensmartgridplatform.shared.infra.jms.ResponseMessageResultType) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 3 with RetryHeader

use of org.opensmartgridplatform.shared.infra.jms.RetryHeader in project open-smart-grid-platform by OSGP.

the class DeviceResponseMessageServiceTest method testProcessScheduledMessageRetry.

private void testProcessScheduledMessageRetry(final String exceptionMessage, final String expectedMessage) {
    final ResponseMessageResultType result = ResponseMessageResultType.NOT_OK;
    final Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);
    final Date scheduledRetryTime = calendar.getTime();
    // since the retryCount is smaller than the maxRetries, the message
    // should be retried
    final RetryHeader retryHeader = new RetryHeader(0, 1, scheduledRetryTime);
    final OsgpException exception = new OsgpException(ComponentType.OSGP_CORE, exceptionMessage);
    final ProtocolResponseMessage message = new ProtocolResponseMessage.Builder().messageMetadata(MESSAGE_METADATA.builder().withScheduled(true).build()).result(result).dataObject(DATA_OBJECT).retryHeader(retryHeader).osgpException(exception).build();
    final ScheduledTask scheduledTask = new ScheduledTask(MESSAGE_METADATA, DOMAIN, DOMAIN_VERSION, DATA_OBJECT, SCHEDULED_TIME);
    when(this.scheduledTaskService.findByCorrelationUid(anyString())).thenReturn(scheduledTask);
    this.deviceResponseMessageService.processMessage(message);
    // check if the message is not send and the task is not deleted
    verify(this.domainResponseMessageSender, never()).send(message);
    verify(this.scheduledTaskService, never()).deleteScheduledTask(scheduledTask);
    verify(this.scheduledTaskService).saveScheduledTask(scheduledTask);
    // check if the scheduled time is updated to the message retry time
    assertThat(scheduledTask.getScheduledTime()).isEqualTo(new Timestamp(scheduledRetryTime.getTime()));
    assertThat(scheduledTask.getErrorLog()).contains(expectedMessage);
}
Also used : OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) ProtocolResponseMessage(org.opensmartgridplatform.shared.infra.jms.ProtocolResponseMessage) Calendar(java.util.Calendar) ScheduledTask(org.opensmartgridplatform.domain.core.entities.ScheduledTask) RetryHeader(org.opensmartgridplatform.shared.infra.jms.RetryHeader) ResponseMessageResultType(org.opensmartgridplatform.shared.infra.jms.ResponseMessageResultType) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 4 with RetryHeader

use of org.opensmartgridplatform.shared.infra.jms.RetryHeader in project open-smart-grid-platform by OSGP.

the class DlmsConnectionMessageProcessor method sendResponseMessage.

protected void sendResponseMessage(final MessageMetadata messageMetadata, final ResponseMessageResultType result, final Exception exception, final DeviceResponseMessageSender responseMessageSender, final Serializable responseObject) {
    OsgpException osgpException = null;
    if (exception != null) {
        osgpException = this.osgpExceptionConverter.ensureOsgpOrTechnicalException(exception);
    }
    final RetryHeader retryHeader;
    if (this.shouldRetry(result, exception, responseObject)) {
        retryHeader = this.retryHeaderFactory.createRetryHeader(messageMetadata.getRetryCount());
    } else {
        retryHeader = this.retryHeaderFactory.createEmptyRetryHeader();
    }
    final ProtocolResponseMessage responseMessage = new ProtocolResponseMessage.Builder().messageMetadata(messageMetadata).result(result).osgpException(osgpException).dataObject(responseObject).retryHeader(retryHeader).build();
    responseMessageSender.send(responseMessage);
}
Also used : OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) ProtocolResponseMessage(org.opensmartgridplatform.shared.infra.jms.ProtocolResponseMessage) RetryHeader(org.opensmartgridplatform.shared.infra.jms.RetryHeader)

Example 5 with RetryHeader

use of org.opensmartgridplatform.shared.infra.jms.RetryHeader in project open-smart-grid-platform by OSGP.

the class RetryHeaderFactory method createRetryHeader.

public RetryHeader createRetryHeader(final int retry) {
    final Calendar retryTime = Calendar.getInstance();
    retryTime.add(Calendar.MILLISECOND, this.calculateDelay(retry));
    LOGGER.info("Creating retry header for retryCount: {}, maximumRetries: {}, scheduledRetryTime: {}.", retry, this.maximumRetries, retryTime.getTime());
    return new RetryHeader(retry, this.maximumRetries, retryTime.getTime());
}
Also used : Calendar(java.util.Calendar) RetryHeader(org.opensmartgridplatform.shared.infra.jms.RetryHeader)

Aggregations

RetryHeader (org.opensmartgridplatform.shared.infra.jms.RetryHeader)6 Calendar (java.util.Calendar)3 OsgpException (org.opensmartgridplatform.shared.exceptionhandling.OsgpException)3 ProtocolResponseMessage (org.opensmartgridplatform.shared.infra.jms.ProtocolResponseMessage)3 Date (java.util.Date)2 Test (org.junit.jupiter.api.Test)2 ScheduledTask (org.opensmartgridplatform.domain.core.entities.ScheduledTask)2 ResponseMessage (org.opensmartgridplatform.shared.infra.jms.ResponseMessage)2 ResponseMessageResultType (org.opensmartgridplatform.shared.infra.jms.ResponseMessageResultType)2 Timestamp (java.sql.Timestamp)1 Message (org.opensmartgridplatform.oslp.Oslp.Message)1 OslpEnvelope (org.opensmartgridplatform.oslp.OslpEnvelope)1 SignedOslpEnvelopeDto (org.opensmartgridplatform.oslp.SignedOslpEnvelopeDto)1 MessageMetadata (org.opensmartgridplatform.shared.infra.jms.MessageMetadata)1