use of org.opensmartgridplatform.shared.exceptionhandling.OsgpException 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.exceptionhandling.OsgpException in project open-smart-grid-platform by OSGP.
the class DlmsConnectionMessageProcessor method createAndHandleConnectionForDevice.
public void createAndHandleConnectionForDevice(final DlmsDevice device, final MessageMetadata messageMetadata, final Consumer<DlmsConnectionManager> taskForConnectionManager) throws OsgpException {
Permit permit = null;
if (this.throttlingClientConfig.clientEnabled()) {
permit = this.throttlingClientConfig.throttlingClient().requestPermitUsingNetworkSegmentIfIdsAreAvailable(messageMetadata.getBaseTransceiverStationId(), messageMetadata.getCellId());
} else {
this.throttlingService.openConnection();
}
final DlmsMessageListener dlmsMessageListener = this.createMessageListenerForDeviceConnection(device, messageMetadata);
try {
this.dlmsConnectionHelper.createAndHandleConnectionForDevice(messageMetadata, device, dlmsMessageListener, permit, taskForConnectionManager);
} catch (final ConnectionTaskException e) {
/*
* Exceptions thrown by the tasks working with the DlmsConnectionManager are wrapped in
* ConnectionTaskException in the ThrowingConsumer.
*/
throw e.getOsgpException();
} catch (final Exception e) {
/*
* Throttling permit is released in this catch block to make sure that it is made available
* again when there were problems setting up the connection and only then (exceptions thrown
* by the tasks working with the DlmsConnectionManager are wrapped as ConnectionTaskException
* and are caught in the previous catch block, so they don't end up here).
*
* If there are no problems in setting up the connection, releasing the throttling permit is
* the responsibility of the tasks for the DlmsConnectionManager, as seen in
* DeviceRequestMessageProcessor.processMessageTasks(), where
* this.doConnectionPostProcessing() is called in a finally block.
*/
if (this.throttlingClientConfig.clientEnabled()) {
this.throttlingClientConfig.throttlingClient().releasePermit(permit);
} else {
this.throttlingService.closeConnection();
}
throw e;
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.OsgpException 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.exceptionhandling.OsgpException in project open-smart-grid-platform by OSGP.
the class TariffSwitchingGetStatusRequestMessageProcessor method handleGetStatusDeviceResponse.
private void handleGetStatusDeviceResponse(final DeviceResponse deviceResponse, final ResponseMessageSender responseMessageSender, final String domain, final String domainVersion, final String messageType, final int retryCount) {
ResponseMessageResultType result = ResponseMessageResultType.OK;
OsgpException osgpException = null;
DeviceStatusDto status = null;
try {
final GetStatusDeviceResponse response = (GetStatusDeviceResponse) deviceResponse;
status = response.getDeviceStatus();
} catch (final Exception e) {
LOGGER.error("Device Response Exception", e);
result = ResponseMessageResultType.NOT_OK;
osgpException = new TechnicalException(ComponentType.UNKNOWN, "Exception occurred while getting device status", e);
}
final MessageMetadata messageMetadata = MessageMetadataFactory.from(deviceResponse, messageType).builder().withDomain(domain).withDomainVersion(domainVersion).withRetryCount(retryCount).build();
final ProtocolResponseMessage responseMessage = ProtocolResponseMessage.newBuilder().messageMetadata(messageMetadata).result(result).osgpException(osgpException).dataObject(status).build();
responseMessageSender.send(responseMessage);
}
use of org.opensmartgridplatform.shared.exceptionhandling.OsgpException in project open-smart-grid-platform by OSGP.
the class CommonGetFirmwareRequestMessageProcessor method handleGetFirmwareVersionDeviceResponse.
private void handleGetFirmwareVersionDeviceResponse(final DeviceResponse deviceResponse, final ResponseMessageSender responseMessageSender, final String domain, final String domainVersion, final String messageType, final int retryCount) {
ResponseMessageResultType result = ResponseMessageResultType.OK;
final String firmwareVersion;
OsgpException osgpException = null;
final List<FirmwareVersionDto> firmwareVersions = new ArrayList<>();
try {
final GetFirmwareVersionDeviceResponse response = (GetFirmwareVersionDeviceResponse) deviceResponse;
firmwareVersion = response.getFirmwareVersion();
firmwareVersions.add(new FirmwareVersionDto(FirmwareModuleType.FUNCTIONAL, firmwareVersion));
} catch (final Exception e) {
LOGGER.error("Device Response Exception", e);
result = ResponseMessageResultType.NOT_OK;
osgpException = new TechnicalException(ComponentType.UNKNOWN, "Exception occurred while getting device firmware version", e);
}
final ProtocolResponseMessage responseMessage = ProtocolResponseMessage.newBuilder().messageMetadata(MessageMetadataFactory.from(deviceResponse, messageType).builder().withDomain(domain).withDomainVersion(domainVersion).withRetryCount(retryCount).build()).result(result).osgpException(osgpException).dataObject((Serializable) firmwareVersions).build();
responseMessageSender.send(responseMessage);
}
Aggregations