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);
}
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);
}
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);
}
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);
}
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());
}
Aggregations