Search in sources :

Example 1 with Iec61850Connection

use of com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850DaRtuDeviceService method getData.

@Override
public void getData(final DaDeviceRequest deviceRequest, final DeviceResponseHandler deviceResponseHandler, final DaRtuDeviceRequestMessageProcessor messageProcessor) throws JMSException {
    try {
        final String serverName = this.getServerName(deviceRequest);
        final ServerModel serverModel = this.connectAndRetrieveServerModel(deviceRequest, serverName);
        final ClientAssociation clientAssociation = this.iec61850DeviceConnectionService.getClientAssociation(deviceRequest.getDeviceIdentification());
        final Serializable dataResponse = this.handleGetData(new DeviceConnection(new Iec61850Connection(new Iec61850ClientAssociation(clientAssociation, null), serverModel), deviceRequest.getDeviceIdentification(), deviceRequest.getOrganisationIdentification(), serverName), deviceRequest, messageProcessor);
        final DaDeviceResponse deviceResponse = new DaDeviceResponse(deviceRequest.getOrganisationIdentification(), deviceRequest.getDeviceIdentification(), deviceRequest.getCorrelationUid(), DeviceMessageStatus.OK, dataResponse);
        deviceResponseHandler.handleResponse(deviceResponse);
    } catch (final ConnectionFailureException se) {
        LOGGER.error("Could not connect to device after all retries", se);
        final EmptyDeviceResponse deviceResponse = new EmptyDeviceResponse(deviceRequest.getOrganisationIdentification(), deviceRequest.getDeviceIdentification(), deviceRequest.getCorrelationUid(), DeviceMessageStatus.FAILURE);
        deviceResponseHandler.handleConnectionFailure(se, deviceResponse);
    } catch (final Exception e) {
        LOGGER.error("Unexpected exception during Get Data", e);
        final EmptyDeviceResponse deviceResponse = new EmptyDeviceResponse(deviceRequest.getOrganisationIdentification(), deviceRequest.getDeviceIdentification(), deviceRequest.getCorrelationUid(), DeviceMessageStatus.FAILURE);
        deviceResponseHandler.handleException(e, deviceResponse);
    }
}
Also used : DaDeviceResponse(com.alliander.osgp.adapter.protocol.iec61850.device.da.rtu.DaDeviceResponse) Serializable(java.io.Serializable) ServerModel(org.openmuc.openiec61850.ServerModel) Iec61850Connection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection) Iec61850ClientAssociation(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850ClientAssociation) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) DeviceConnection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.DeviceConnection) Iec61850ClientAssociation(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850ClientAssociation) ClientAssociation(org.openmuc.openiec61850.ClientAssociation) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) JMSException(javax.jms.JMSException) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) EmptyDeviceResponse(com.alliander.osgp.adapter.protocol.iec61850.device.ssld.responses.EmptyDeviceResponse)

Example 2 with Iec61850Connection

use of com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850DeviceConnectionService method connect.

public synchronized DeviceConnection connect(final String ipAddress, final String deviceIdentification, final String organisationIdentification, final IED ied, final String serverName, final String logicalDevice, final boolean cacheConnection) throws ConnectionFailureException {
    // an usable for the given deviceIdentification.
    try {
        if (cacheConnection && this.testIfConnectionIsCachedAndAlive(deviceIdentification, ied, serverName, logicalDevice)) {
            return new DeviceConnection(this.fetchIec61850Connection(deviceIdentification), deviceIdentification, organisationIdentification, serverName);
        }
    } catch (final ProtocolAdapterException e) {
        this.logProtocolAdapterException(deviceIdentification, e);
    }
    if (StringUtils.isEmpty(ipAddress)) {
        throw new ConnectionFailureException("Ip address is null");
    }
    final InetAddress inetAddress = this.convertIpAddress(ipAddress);
    // Connect to obtain ClientAssociation and ServerModel.
    LOGGER.info("Trying to connect to deviceIdentification: {} at IP address {} using response time-out: {}", deviceIdentification, ipAddress, this.responseTimeout);
    final DateTime startTime = DateTime.now();
    // Create instance of appropriate event listener.
    Iec61850ClientBaseEventListener eventListener = null;
    try {
        eventListener = Iec61850ClientEventListenerFactory.getInstance().getEventListener(ied, deviceIdentification, this.deviceManagementService);
    } catch (final ProtocolAdapterException e) {
        this.logProtocolAdapterException(deviceIdentification, e);
    }
    final Iec61850Device iec61850Device = this.iec61850DeviceRepository.findByDeviceIdentification(deviceIdentification);
    final int port = this.determinePortForIec61850Device(ied, iec61850Device);
    // Try to connect and receive the ClientAssociation.
    final Iec61850ClientAssociation iec61850ClientAssociation = this.iec61850Client.connect(deviceIdentification, inetAddress, eventListener, port);
    final ClientAssociation clientAssociation = iec61850ClientAssociation.getClientAssociation();
    // Set response time-out.
    clientAssociation.setResponseTimeout(this.responseTimeout);
    // Read the ServerModel, either from the device or from a SCL file.
    ServerModel serverModel;
    try {
        serverModel = this.readServerModel(clientAssociation, deviceIdentification, iec61850Device);
    } catch (final ProtocolAdapterException e) {
        LOGGER.error("ProtocolAdapterException: unable to read ServerModel for deviceIdentification " + deviceIdentification, e);
        throw new ConnectionFailureException(e.getMessage(), e);
    }
    // Cache the connection.
    final Iec61850Connection iec61850Connection = new Iec61850Connection(iec61850ClientAssociation, serverModel, startTime);
    if (cacheConnection) {
        this.cacheIec61850Connection(deviceIdentification, iec61850Connection);
    }
    final DateTime endTime = DateTime.now();
    LOGGER.info("Connected to device: {}, fetched server model. Start time: {}, end time: {}, total time in milliseconds: {}", deviceIdentification, startTime, endTime, endTime.minus(startTime.getMillis()).getMillis());
    return new DeviceConnection(iec61850Connection, deviceIdentification, organisationIdentification, serverName);
}
Also used : Iec61850ClientAssociation(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850ClientAssociation) ServerModel(org.openmuc.openiec61850.ServerModel) Iec61850Connection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection) Iec61850Device(com.alliander.osgp.adapter.protocol.iec61850.domain.entities.Iec61850Device) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) DeviceConnection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.DeviceConnection) Iec61850ClientAssociation(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850ClientAssociation) ClientAssociation(org.openmuc.openiec61850.ClientAssociation) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) Iec61850ClientBaseEventListener(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.reporting.Iec61850ClientBaseEventListener) InetAddress(java.net.InetAddress) DateTime(org.joda.time.DateTime)

Example 3 with Iec61850Connection

use of com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850DeviceConnectionService method readNodeDataValues.

public void readNodeDataValues(final String deviceIdentification, final FcModelNode fcModelNode) throws NodeReadException {
    final Iec61850Connection iec61850Connection = this.fetchIec61850Connection(deviceIdentification);
    if (iec61850Connection == null) {
        return;
    }
    final ClientAssociation clientAssociation = iec61850Connection.getClientAssociation();
    this.iec61850Client.readNodeDataValues(clientAssociation, fcModelNode);
}
Also used : Iec61850Connection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection) Iec61850ClientAssociation(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850ClientAssociation) ClientAssociation(org.openmuc.openiec61850.ClientAssociation)

Example 4 with Iec61850Connection

use of com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850DeviceConnectionService method testIfConnectionIsCachedAndAlive.

private boolean testIfConnectionIsCachedAndAlive(final String deviceIdentification, final IED ied, final String serverName, final String logicalDevice) throws ProtocolAdapterException {
    try {
        LOGGER.info("Trying to find connection in cache for deviceIdentification: {}", deviceIdentification);
        final Iec61850Connection iec61850Connection = this.fetchIec61850Connection(deviceIdentification);
        if (iec61850Connection != null) {
            // Already connected, check if connection is still usable.
            LOGGER.info("Connection found for deviceIdentification: {}", deviceIdentification);
            // requires manual reads of remote data.
            if (ied != null && logicalDevice != null) {
                final String description = this.getActualServerName(ied, serverName);
                LOGGER.info("Testing if connection is alive using {}{}/{}.{} for deviceIdentification: {}", description, logicalDevice, LogicalNode.LOGICAL_NODE_ZERO.getDescription(), DataAttribute.NAME_PLATE.getDescription(), deviceIdentification);
                final FcModelNode modelNode = this.getModelNode(logicalDevice, iec61850Connection, description);
                this.iec61850Client.readNodeDataValues(iec61850Connection.getClientAssociation(), modelNode);
            } else {
                // Read all data values, which is much slower, but requires
                // no manual reads of remote data.
                LOGGER.info("Testing if connection is alive using readAllDataValues() for deviceIdentification: {}", deviceIdentification);
                this.iec61850Client.readAllDataValues(iec61850Connection.getClientAssociation());
            }
            LOGGER.info("Connection is still active for deviceIdentification: {}", deviceIdentification);
            return true;
        }
    } catch (final NodeReadException e) {
        LOGGER.error("Connection is no longer active, removing connection from cache for deviceIdentification: " + deviceIdentification, e);
        this.removeIec61850Connection(deviceIdentification);
    }
    return false;
}
Also used : Iec61850Connection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection) NodeReadException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeReadException) FcModelNode(org.openmuc.openiec61850.FcModelNode)

Example 5 with Iec61850Connection

use of com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850RtuDeviceService method setData.

@Override
public void setData(final SetDataDeviceRequest deviceRequest, final DeviceResponseHandler deviceResponseHandler) throws JMSException {
    try {
        final String serverName = this.getServerName(deviceRequest);
        final ServerModel serverModel = this.connectAndRetrieveServerModel(deviceRequest, serverName);
        final ClientAssociation clientAssociation = this.iec61850DeviceConnectionService.getClientAssociation(deviceRequest.getDeviceIdentification());
        this.handleSetData(new DeviceConnection(new Iec61850Connection(new Iec61850ClientAssociation(clientAssociation, null), serverModel), deviceRequest.getDeviceIdentification(), deviceRequest.getOrganisationIdentification(), serverName), deviceRequest);
        final EmptyDeviceResponse deviceResponse = new EmptyDeviceResponse(deviceRequest.getOrganisationIdentification(), deviceRequest.getDeviceIdentification(), deviceRequest.getCorrelationUid(), DeviceMessageStatus.OK);
        deviceResponseHandler.handleResponse(deviceResponse);
    } catch (final ConnectionFailureException se) {
        LOGGER.error("Could not connect to device after all retries", se);
        final EmptyDeviceResponse deviceResponse = new EmptyDeviceResponse(deviceRequest.getOrganisationIdentification(), deviceRequest.getDeviceIdentification(), deviceRequest.getCorrelationUid(), DeviceMessageStatus.FAILURE);
        deviceResponseHandler.handleConnectionFailure(se, deviceResponse);
    } catch (final Exception e) {
        LOGGER.error("Unexpected exception during Set Data", e);
        final EmptyDeviceResponse deviceResponse = new EmptyDeviceResponse(deviceRequest.getOrganisationIdentification(), deviceRequest.getDeviceIdentification(), deviceRequest.getCorrelationUid(), DeviceMessageStatus.FAILURE);
        deviceResponseHandler.handleException(e, deviceResponse);
    }
}
Also used : ServerModel(org.openmuc.openiec61850.ServerModel) Iec61850Connection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection) Iec61850ClientAssociation(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850ClientAssociation) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) DeviceConnection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.DeviceConnection) Iec61850ClientAssociation(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850ClientAssociation) ClientAssociation(org.openmuc.openiec61850.ClientAssociation) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) JMSException(javax.jms.JMSException) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) EmptyDeviceResponse(com.alliander.osgp.adapter.protocol.iec61850.device.ssld.responses.EmptyDeviceResponse)

Aggregations

Iec61850Connection (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850Connection)8 Iec61850ClientAssociation (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.Iec61850ClientAssociation)6 ClientAssociation (org.openmuc.openiec61850.ClientAssociation)6 ProtocolAdapterException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException)5 ServerModel (org.openmuc.openiec61850.ServerModel)5 ConnectionFailureException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException)4 DeviceConnection (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.DeviceConnection)4 EmptyDeviceResponse (com.alliander.osgp.adapter.protocol.iec61850.device.ssld.responses.EmptyDeviceResponse)3 JMSException (javax.jms.JMSException)3 FcModelNode (org.openmuc.openiec61850.FcModelNode)2 DaDeviceResponse (com.alliander.osgp.adapter.protocol.iec61850.device.da.rtu.DaDeviceResponse)1 GetDataDeviceResponse (com.alliander.osgp.adapter.protocol.iec61850.device.ssld.responses.GetDataDeviceResponse)1 Iec61850Device (com.alliander.osgp.adapter.protocol.iec61850.domain.entities.Iec61850Device)1 NodeReadException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeReadException)1 Iec61850ClientBaseEventListener (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.reporting.Iec61850ClientBaseEventListener)1 GetDataResponseDto (com.alliander.osgp.dto.valueobjects.microgrids.GetDataResponseDto)1 Serializable (java.io.Serializable)1 InetAddress (java.net.InetAddress)1 DateTime (org.joda.time.DateTime)1