<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You 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.
  -->
<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xmlns:camel="http://camel.apache.org/schema/blueprint"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
                               http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd
                               http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <cm:property-placeholder persistent-id="org.apache.unomi.router" update-strategy="reload">
        <cm:default-properties>
            <cm:property name="router.config.type" value="nobroker"/>
            <cm:property name="config.allowedEndpoints" value="file,ftp"/>
            <cm:property name="kafka.host" value="localhost"/>
            <cm:property name="kafka.port" value="9092"/>
            <cm:property name="kafka.import.topic" value="import-deposit"/>
            <cm:property name="kafka.export.topic" value="export-deposit"/>
            <cm:property name="kafka.import.groupId" value="unomi-import-group"/>
            <cm:property name="kafka.export.groupId" value="unomi-export-group"/>
            <cm:property name="kafka.consumerCount" value="10"/>
            <cm:property name="kafka.autoCommit" value="true"/>
            <cm:property name="import.oneshot.uploadDir" value="/tmp/oneshot_import_configs/"/>
            <cm:property name="executionsHistory.size" value="5"/>
            <cm:property name="executions.error.report.size" value="200"/>
        </cm:default-properties>
    </cm:property-placeholder>

    <bean id="unomiStorageProcessor" class="org.apache.unomi.router.core.processor.UnomiStorageProcessor">
        <property name="profileImportService" ref="profileImportService"/>
        <property name="segmentService" ref="segmentService"/>
    </bean>

    <bean id="importRouteCompletionProcessor" class="org.apache.unomi.router.core.processor.ImportRouteCompletionProcessor">
        <property name="importConfigurationService" ref="importConfigurationService"/>
        <property name="executionsHistorySize" value="${executionsHistory.size}"/>
        <property name="execErrReportSize" value="${executions.error.report.size}"/>
    </bean>

    <bean id="exportRouteCompletionProcessor" class="org.apache.unomi.router.core.processor.ExportRouteCompletionProcessor">
        <property name="exportConfigurationService" ref="exportConfigurationService"/>
        <property name="executionsHistorySize" value="${executionsHistory.size}"/>
    </bean>

    <bean id="importConfigByFileNameProcessor" class="org.apache.unomi.router.core.processor.ImportConfigByFileNameProcessor">
        <property name="importConfigurationService" ref="importConfigurationService"/>
    </bean>

    <bean id="jacksonDataFormat" class="org.apache.camel.component.jackson.JacksonDataFormat">
        <property name="unmarshalType" value="org.apache.unomi.router.api.ProfileToImport"/>
    </bean>

    <bean id="jacksonDataFormatImportConfig" class="org.apache.camel.model.dataformat.JsonDataFormat">
        <property name="unmarshalType" value="org.apache.unomi.router.api.ImportConfiguration"/>
        <property name="library" value="Jackson"/>
    </bean>

    <bean id="jacksonDataFormatExportConfig" class="org.apache.camel.model.dataformat.JsonDataFormat">
        <property name="unmarshalType" value="org.apache.unomi.router.api.ExportConfiguration"/>
        <property name="library" value="Jackson"/>
    </bean>

    <bean id="jacksonDataFormatImportLineError" class="org.apache.camel.model.dataformat.JsonDataFormat">
        <property name="unmarshalType" value="org.apache.unomi.router.api.ImportLineError"/>
        <property name="library" value="Jackson"/>
    </bean>


    <bean id="camelContext" class="org.apache.unomi.router.core.context.RouterCamelContext"
          init-method="initCamelContext" destroy-method="preDestroy">
        <property name="configType" value="${router.config.type}"/>
        <property name="allowedEndpoints" value="${config.allowedEndpoints}"/>
        <property name="uploadDir" value="${import.oneshot.uploadDir}"/>
        <property name="execHistorySize" value="${executionsHistory.size}"/>
        <property name="execErrReportSize" value="${executions.error.report.size}"/>
        <property name="bundleContext" ref="blueprintBundleContext"/>
        <property name="jacksonDataFormat" ref="jacksonDataFormat"/>
        <property name="kafkaProps">
            <map>
                <entry key="kafkaHost" value="${kafka.host}"/>
                <entry key="kafkaPort" value="${kafka.port}"/>
                <entry key="kafkaImportTopic" value="${kafka.import.topic}"/>
                <entry key="kafkaExportTopic" value="${kafka.export.topic}"/>
                <entry key="kafkaImportGroupId" value="${kafka.import.groupId}"/>
                <entry key="kafkaExportGroupId" value="${kafka.export.groupId}"/>
                <entry key="kafkaConsumerCount" value="${kafka.consumerCount}"/>
                <entry key="kafkaAutoCommit" value="${kafka.autoCommit}"/>
            </map>
        </property>
        <property name="unomiStorageProcessor" ref="unomiStorageProcessor"/>
        <property name="importRouteCompletionProcessor" ref="importRouteCompletionProcessor"/>
        <property name="exportRouteCompletionProcessor" ref="exportRouteCompletionProcessor"/>
        <property name="importConfigByFileNameProcessor" ref="importConfigByFileNameProcessor"/>
        <property name="configSharingService" ref="configSharingService" />
        <property name="exportConfigurationService" ref="exportConfigurationService"/>
        <property name="importConfigurationService" ref="importConfigurationService"/>
        <property name="persistenceService" ref="persistenceService"/>
        <property name="profileExportService" ref="profileExportService"/>
        <property name="profileService" ref="profileService"/>
        <property name="clusterService" ref="clusterService" />
    </bean>

    <bean id="collectProfileBean" class="org.apache.unomi.router.core.bean.CollectProfileBean">
        <property name="persistenceService" ref="persistenceService"/>
    </bean>

    <bean id="updateCamelRouteEventHandler" class="org.apache.unomi.router.core.event.UpdateCamelRouteEventHandler">
        <property name="configurationAdmin" ref="osgiConfigurationAdmin"/>
        <property name="clusterManager" ref="karafCellarClusterManager"/>
        <property name="groupManager" ref="karafCellarGroupManager"/>
        <property name="routerCamelContext" ref="camelContext"/>
    </bean>
    <service ref="updateCamelRouteEventHandler" interface="org.apache.karaf.cellar.core.event.EventHandler">
        <service-properties>
            <entry key="managed" value="true"/>
        </service-properties>
    </service>

    <reference id="configSharingService" interface="org.apache.unomi.api.services.ConfigSharingService" />
    <reference id="profileImportService" interface="org.apache.unomi.router.api.services.ProfileImportService"/>
    <reference id="profileExportService" interface="org.apache.unomi.router.api.services.ProfileExportService"/>
    <reference id="profileService" interface="org.apache.unomi.api.services.ProfileService"/>
    <reference id="persistenceService" interface="org.apache.unomi.persistence.spi.PersistenceService"/>
    <reference id="segmentService" interface="org.apache.unomi.api.services.SegmentService"/>
    <reference id="importConfigurationService" interface="org.apache.unomi.router.api.services.ImportExportConfigurationService" filter="(configDiscriminator=IMPORT)"/>
    <reference id="exportConfigurationService" interface="org.apache.unomi.router.api.services.ImportExportConfigurationService" filter="(configDiscriminator=EXPORT)"/>
    <reference id="clusterService" interface="org.apache.unomi.api.services.ClusterService" />
    <reference id="karafCellarGroupManager" interface="org.apache.karaf.cellar.core.GroupManager" />
    <reference id="osgiConfigurationAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"/>
    <reference id="karafCellarClusterManager" interface="org.apache.karaf.cellar.core.ClusterManager" />

</blueprint>