<?xml version="1.0" encoding="UTF-8"?>
<route>
    <from uri="direct:uniform-passive-service-operation-router"/>
    <choice>
        <when>
            <simple>${headers.operationName} == 'PutPackets'</simple>
            <log loggingLevel="DEBUG"
                 message="${headers.NTX_loggingKey} Called operation: ${headers.operationName} with message-type: ${header.message-type}"/>
            <log loggingLevel="DEBUG" message="${headers.NTX_loggingKey} Sending to connector: in"/>
            <log loggingLevel="TRACE" message="${headers.NTX_loggingKey} Body: ${body}"/>
            <to uri="bean:uniform-passive-service-in-connector-registry?method=inConnectorFind"/>
            <choice>
                <when>
                    <simple>${headers.NTX_targetConnector} != null</simple>
                    <setProperty name="NTX_TMP_targetConnector">
                        <simple>${headers.NTX_targetConnector}</simple>
                    </setProperty>
                    <removeHeader headerName="NTX_targetConnector"/>
                    <toD uri="${exchangeProperty.NTX_TMP_targetConnector}"/>
                </when>
                <otherwise>
                    <log message="${headers.NTX_loggingKey} Connector not found: service=uniform-service; profile=${headers[X-SystemName]}; direction=in"
                         loggingLevel="ERROR"/>
                    <throwException
                            exceptionType="ru.entaxy.platform.services.runtime.exceptions.ConnectorNotFound"
                            message="Connector not found: service=uniform-service; profile=${headers[X-SystemName]}; direction=in"/>
                </otherwise>
            </choice>
        </when>
        <when>
            <simple>${headers.operationName} == 'ConfirmGettingPackets'</simple>
            <log loggingLevel="DEBUG"
                 message="${headers.NTX_loggingKey} Called operation: ${headers.operationName}"/>
            <log loggingLevel="DEBUG" message="${headers.NTX_loggingKey} Sending to connector: out"/>
            <to uri="bean:uniform-passive-service-out-connector-registry?method=outConnectorFind"/>
            <choice>
                <when>
                    <simple>${headers.NTX_targetConnector} != null</simple>
                    <setProperty name="NTX_TMP_targetConnector">
                        <simple>${headers.NTX_targetConnector}</simple>
                    </setProperty>
                    <removeHeader headerName="NTX_targetConnector"/>
                    <toD uri="${exchangeProperty.NTX_TMP_targetConnector}"/>
                </when>
                <otherwise>
                    <log message="${headers.NTX_loggingKey} Connector not found: service=uniform-service; profile=${headers[X-SystemName]}; direction=out"
                         loggingLevel="ERROR"/>
                    <throwException
                            exceptionType="ru.entaxy.platform.services.runtime.exceptions.ConnectorNotFound"
                            message="Connector not found: service=uniform-service; profile=${headers[X-SystemName]}; direction=out"/>
                </otherwise>
            </choice>
        </when>
        <when>
            <simple>${headers.operationName} == 'GetPackets'</simple>
            <log loggingLevel="DEBUG"
                 message="${headers.NTX_loggingKey} Called operation: ${headers.operationName}"/>
            <log loggingLevel="DEBUG" message="${headers.NTX_loggingKey} Sending to connector: out"/>
            <to uri="bean:uniform-passive-service-out-connector-registry?method=outConnectorFind"/>
            <choice>
                <when>
                    <simple>${headers.NTX_targetConnector} != null</simple>
                    <setProperty name="NTX_TMP_targetConnector">
                        <simple>${headers.NTX_targetConnector}</simple>
                    </setProperty>
                    <removeHeader headerName="NTX_targetConnector"/>
                    <toD uri="${exchangeProperty.NTX_TMP_targetConnector}"/>
                </when>
                <otherwise>
                    <log message="${headers.NTX_loggingKey} Connector not found: service=uniform-service; profile=${headers[X-SystemName]}; direction=out"
                         loggingLevel="ERROR"/>
                    <throwException
                            exceptionType="ru.entaxy.platform.services.runtime.exceptions.ConnectorNotFound"
                            message="Connector not found: service=uniform-service; profile=${headers[X-SystemName]}; direction=out"/>
                </otherwise>
            </choice>
        </when>
        <otherwise>
            <log message="${headers.NTX_loggingKey} Uniform service operation ${header.operationName} isn't support"
                 loggingLevel="ERROR"/>
            <throwException exceptionType="java.lang.IllegalArgumentException"
                            message="Uniform service operation ${header.operationName} isn't support"/>
        </otherwise>
    </choice>
</route>
