<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~~~~~~licensing~~~~~~
  bridge-profile-manager
  ==========
  Copyright (C) 2020 - 2021 EmDev LLC
  ==========
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  ~~~~~~/licensing~~~~~~
  -->

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <cm:property-placeholder persistent-id="ru.entaxy.esb" update-strategy="reload">
        <cm:default-properties>
            <cm:property name="is.restore.from.db" value="true"/>
        </cm:default-properties>
    </cm:property-placeholder>

    <reference id="pooledConnectionFactory" interface="javax.jms.ConnectionFactory"/>
    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="pooledConnectionFactory"/>
    </bean>

    <bean id="bridgeProfileManager"
          class="ru.entaxy.esb.system.management.bridge.profile.manager.BridgeProfileManagerImpl">
        <property name="systemService" ref="systemService"/>
        <property name="bridgeService" ref="bridgeService"/>
        <property name="bridgeProfileMapper" ref="bridgeProfileMapper"/>
        <property name="bridgeProfileSender" ref="bridgeProfileSender"/>
    </bean>

    <bean id="bridgeProfileRecipientManager"
          class="ru.entaxy.esb.system.management.bridge.profile.manager.recipient.BridgeProfileRecipientManagerImpl"
          init-method="restore">
        <property name="isRestoreFromDB" value="${is.restore.from.db}"/>

        <property name="systemService" ref="systemService"/>
        <property name="bundleManager" ref="bundleManager"/>
        <property name="bridgeProfileMapper" ref="bridgeProfileMapper"/>
    </bean>

    <service ref="bridgeProfileManager"
             interface="ru.entaxy.esb.system.management.bridge.profile.manager.BridgeProfileManager"/>

    <bean id="bridgeProfileMapper"
          class="ru.entaxy.esb.system.management.bridge.profile.manager.mapper.BridgeProfileMapperImpl"/>

    <bean id="bridgeProfileSender"
          class="ru.entaxy.esb.system.management.bridge.profile.manager.sender.BridgeProfileSenderImpl">
          <property name="context" ref="system-management-service"/>
    </bean>

    <reference id="bridgeService" interface="ru.entaxy.esb.system.management.bridge.profile.manager.service.BridgeService"
               timeout="30000"
               availability="optional"/>

    <reference id="systemService"
               interface="ru.entaxy.esb.system.jpa.SystemService"
               timeout="30000"
               availability="mandatory"/>

    <reference id="bundleManager"
               interface="ru.entaxy.esb.system.management.bundle.manager.BundleManager"
               timeout="30000"
               availability="mandatory"/>

    <camelContext id="system-management-service" xmlns="http://camel.apache.org/schema/blueprint">

        <route id="direct-profile-controller-route">
            <from uri="direct-vm:send-message"/>
            <setHeader name="NTX_ExchangePattern">
                <simple>InOut</simple>
            </setHeader>
            <toD uri="jms:queue:entaxy.local.out.${headers.NTX_Bridge}?exchangePattern=InOut"/>
        </route>

        <route id="profile-controller-route">
            <from uri="jms:queue:entaxy.profile.controller.route"/>
            <choice>
                <when>
                    <simple>${headers.NTX_CommandBridgeProfileManager} == "create"</simple>
                    <to uri="bean:bridgeProfileRecipientManager?
                            method=installBridgeProfile(${body}, ${headers.NTX_Origin})"/>
                </when>
                <when>
                    <simple>${headers.NTX_CommandBridgeProfileManager} == "start"</simple>
                    <to uri="bean:bridgeProfileRecipientManager?
                            method=startBridgeProfile(${body}, ${headers.NTX_Origin})"/>
                </when>
                <when>
                    <simple>${headers.NTX_CommandBridgeProfileManager} == "stop"</simple>
                    <to uri="bean:bridgeProfileRecipientManager?
                            method=stopBridgeProfile(${body}, ${headers.NTX_Origin})"/>
                </when>
                <when>
                    <simple>${headers.NTX_CommandBridgeProfileManager} == "uninstall"</simple>
                    <to uri="bean:bridgeProfileRecipientManager?
                            method=uninstallBridgeProfile(${body})"/>
                </when>
                <when>
                    <simple>${headers.NTX_CommandBridgeProfileManager} == "get"</simple>
                    <to uri="bean:bridgeProfileRecipientManager?
                            method=getBridgeProfile(${body})"/>
                </when>
            </choice>
        </route>
    </camelContext>
</blueprint>
