<?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:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           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">

    <cm:property-placeholder persistent-id="org.apache.unomi.persistence.elasticsearch"
                             update-strategy="reload" placeholder-prefix="${es.">
        <cm:default-properties>
            <cm:property name="cluster.name" value="contextElasticSearch"/>
            <cm:property name="elasticSearchAddresses" value="localhost:9200"/>
            <cm:property name="index.prefix" value="context"/>
            <cm:property name="numberOfShards" value="5"/>
            <cm:property name="numberOfReplicas" value="0"/>
            <cm:property name="indexMappingTotalFieldsLimit" value="1000"/>
            <cm:property name="indexMaxDocValueFieldsSearch" value="1000"/>
            <cm:property name="monthlyIndex.numberOfShards" value="3"/>
            <cm:property name="monthlyIndex.numberOfReplicas" value="0"/>
            <cm:property name="monthlyIndex.indexMappingTotalFieldsLimit" value="1000"/>
            <cm:property name="monthlyIndex.indexMaxDocValueFieldsSearch" value="1000"/>
            <cm:property name="defaultQueryLimit" value="10"/>

            <cm:property name="bulkProcessor.concurrentRequests" value="1" />
            <cm:property name="bulkProcessor.bulkActions" value="1000" />
            <cm:property name="bulkProcessor.bulkSize" value="5MB" />
            <cm:property name="bulkProcessor.flushInterval" value="5s" />
            <cm:property name="bulkProcessor.backoffPolicy" value="exponential" />

            <cm:property name="minimalElasticSearchVersion" value="7.0.0" />
            <cm:property name="maximalElasticSearchVersion" value="8.0.0" />

            <cm:property name="aggregateQueryBucketSize" value="5000" />
            <cm:property name="clientSocketTimeout" value="" />
            <cm:property name="aggQueryMaxResponseSizeHttp" value="" />
            <cm:property name="aggQueryThrowOnMissingDocs" value="false" />
            <cm:property name="itemClassesToCache" value="" />
            <cm:property name="useBatchingForSave" value="false" />

            <cm:property name="username" value="" />
            <cm:property name="password" value="" />
            <cm:property name="sslEnable" value="false" />
            <cm:property name="sslTrustAllCertificates" value="false" />
        </cm:default-properties>
    </cm:property-placeholder>

    <reference id="metricsService" interface="org.apache.unomi.metrics.MetricsService" />
    <reference id="hazelcastInstance" interface="com.hazelcast.core.HazelcastInstance" />
    <reference id="scriptExecutor" interface="org.apache.unomi.scripting.ScriptExecutor" />

    <service id="elasticSearchPersistenceService" ref="elasticSearchPersistenceServiceImpl">
        <interfaces>
            <value>org.apache.unomi.persistence.spi.PersistenceService</value>
            <value>org.osgi.framework.SynchronousBundleListener</value>
        </interfaces>
    </service>

    <bean id="conditionESQueryBuilderDispatcher"
          class="org.apache.unomi.persistence.elasticsearch.conditions.ConditionESQueryBuilderDispatcher">
        <property name="scriptExecutor" ref="scriptExecutor" />
    </bean>

    <bean id="conditionEvaluatorDispatcherImpl"
          class="org.apache.unomi.persistence.elasticsearch.conditions.ConditionEvaluatorDispatcher">
        <property name="metricsService" ref="metricsService" />
        <property name="scriptExecutor" ref="scriptExecutor" />
    </bean>

    <bean id="elasticSearchPersistenceServiceImpl"
          class="org.apache.unomi.persistence.elasticsearch.ElasticSearchPersistenceServiceImpl"
          init-method="start"
          destroy-method="stop">
        <property name="bundleContext" ref="blueprintBundleContext"/>
        <property name="conditionEvaluatorDispatcher" ref="conditionEvaluatorDispatcherImpl"/>
        <property name="conditionESQueryBuilderDispatcher" ref="conditionESQueryBuilderDispatcher"/>
        <property name="clusterName" value="${es.cluster.name}"/>
        <property name="indexPrefix" value="${es.index.prefix}"/>
        <property name="monthlyIndexNumberOfShards" value="${es.monthlyIndex.numberOfShards}"/>
        <property name="monthlyIndexNumberOfReplicas" value="${es.monthlyIndex.numberOfReplicas}"/>
        <property name="monthlyIndexMappingTotalFieldsLimit" value="${es.monthlyIndex.indexMappingTotalFieldsLimit}"/>
        <property name="monthlyIndexMaxDocValueFieldsSearch" value="${es.monthlyIndex.indexMaxDocValueFieldsSearch}"/>
        <property name="numberOfShards" value="${es.numberOfShards}"/>
        <property name="numberOfReplicas" value="${es.numberOfReplicas}"/>
        <property name="indexMappingTotalFieldsLimit" value="${es.indexMappingTotalFieldsLimit}"/>
        <property name="indexMaxDocValueFieldsSearch" value="${es.indexMaxDocValueFieldsSearch}"/>
        <property name="elasticSearchAddresses" value="${es.elasticSearchAddresses}"/>
        <property name="fatalIllegalStateErrors" value="${es.fatalIllegalStateErrors}"/>
        <property name="defaultQueryLimit" value="${es.defaultQueryLimit}"/>
        <property name="itemsMonthlyIndexedOverride" value="${es.monthlyIndex.itemsMonthlyIndexedOverride}" />
        <property name="routingByType">
            <map>
            </map>
        </property>
        <property name="bulkProcessorConcurrentRequests" value="${es.bulkProcessor.concurrentRequests}" />
        <property name="bulkProcessorBulkActions" value="${es.bulkProcessor.bulkActions}" />
        <property name="bulkProcessorBulkSize" value="${es.bulkProcessor.bulkSize}" />
        <property name="bulkProcessorFlushInterval" value="${es.bulkProcessor.flushInterval}" />
        <property name="bulkProcessorBackoffPolicy" value="${es.bulkProcessor.backoffPolicy}" />

        <property name="minimalElasticSearchVersion" value="${es.minimalElasticSearchVersion}" />
        <property name="maximalElasticSearchVersion" value="${es.maximalElasticSearchVersion}" />

        <property name="aggregateQueryBucketSize" value="${es.aggregateQueryBucketSize}" />
        <property name="aggQueryMaxResponseSizeHttp" value="${es.aggQueryMaxResponseSizeHttp}" />
        <property name="aggQueryThrowOnMissingDocs" value="${es.aggQueryThrowOnMissingDocs}" />
        <property name="clientSocketTimeout" value="${es.clientSocketTimeout}" />

        <property name="metricsService" ref="metricsService" />
        <property name="hazelcastInstance" ref="hazelcastInstance" />
        <property name="itemClassesToCache" value="${es.itemClassesToCache}" />
        <property name="useBatchingForSave" value="${es.useBatchingForSave}" />

        <property name="username" value="${es.username}" />
        <property name="password" value="${es.password}" />
        <property name="sslEnable" value="${es.sslEnable}" />
        <property name="sslTrustAllCertificates" value="${es.sslTrustAllCertificates}" />
    </bean>

    <!-- We use a listener here because using the list directly for listening to proxies coming from the same bundle didn't seem to work -->
    <reference-list id="conditionEvaluators"
                    interface="org.apache.unomi.persistence.elasticsearch.conditions.ConditionEvaluator"
                    availability="optional">
        <reference-listener
                bind-method="bindConditionEvaluator" unbind-method="unbindConditionEvaluator" ref="elasticSearchPersistenceServiceImpl"/>
    </reference-list>

    <reference-list id="conditionESQueryBuilders"
                    interface="org.apache.unomi.persistence.elasticsearch.conditions.ConditionESQueryBuilder"
        availability="optional">
        <reference-listener
                bind-method="bindConditionESQueryBuilder" unbind-method="unbindConditionESQueryBuilder" ref="elasticSearchPersistenceServiceImpl"/>
    </reference-list>

</blueprint>
