/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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.
 */
package io.gravitee.reporter.elastic.config;

import io.gravitee.reporter.elastic.model.HostAddress;
import io.gravitee.reporter.elastic.model.Protocol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;

import java.util.ArrayList;
import java.util.List;

/**
 * Elasticsearch client reporter configuration.
 *  
 * @author Loic DASSONVILLE (loic.dassonville at gmail.com)
 * @author David BRASSELY (brasseld at gmail.com)
 * @author GraviteeSource Team
 */
public class ElasticConfiguration {
	
	private static final String PORT_SEPARATOR = ":";

	@Autowired
	private Environment environment;
	
	/**
	 *  Client communication protocol. 
	 */
	@Value("${reporters.elastic.protocol:TRANSPORT}")
	private Protocol protocol;
	
	/**
	 * Cluster name. Used only for node protocol
	 */
	@Value("${reporters.elastic.cluster:elasticsearch}")
	private String clusterName;
	
	/**
	 * Prefix index name. 
	 */
	@Value("${reporters.elastic.index:gravitee}")
	private String indexName;
	
	/**
	 * Request actions max by bulk 
	 */
	@Value("${reporters.elastic.bulk.actions:5000}")
	private Integer bulkActions;
	
	/**
	 * Bulk flush interval in seconds
	 */
	@Value("${reporters.elastic.bulk.flush_interval:5}")
	private Long flushInterval;
	
	/**
	 * Accepted concurrent request
	 */
	@Value("${reporters.elastic.bulk.concurrent_requests:5}")
	private Integer concurrentRequests;

	/**
	 * Elasticsearch hosts
	 */
	private List<HostAddress> hostsAddresses;

	public Protocol getProtocol() {
		return protocol;
	}

	public String getClusterName() {
		return clusterName;
	}

	public List<HostAddress> getHostsAddresses() {
		if(hostsAddresses == null){
			hostsAddresses = initializeHostsAddresses();
		}
		return hostsAddresses;
	}

	public Integer getBulkActions() {
		return bulkActions;
	}

	public Long getFlushInterval() {
		return flushInterval;
	}

	public Integer getConcurrentRequests() {
		return concurrentRequests;
	}

	public String getIndexName() {
		return indexName;
	}

	private List<HostAddress> initializeHostsAddresses(){
		String key = String.format("reporters.elastic.hosts[%s]", 0);
		List<HostAddress> res = new ArrayList<>();
		
		while (environment.containsProperty(key)) {
			String serializedHost = environment.getProperty(key);
			
			if (serializedHost.contains(PORT_SEPARATOR)) {
				String[] hostParts = serializedHost.split(PORT_SEPARATOR);
				
				String hostname = hostParts[0].toLowerCase();
				Integer port = Integer.parseInt(hostParts[1].trim());
				
				res.add(new HostAddress(hostname, port));
			} else {
				res.add(new HostAddress(serializedHost.trim(), protocol.getDefaultPort()));
			}
			
			key = String.format("reporters.elastic.hosts[%s]", res.size());
		}
		
		// Use default host if required
		if(res.isEmpty()){
			res.add(new HostAddress("localhost", protocol.getDefaultPort()));
		}
		return res;
	}
}

@@ -1059,70 +1059,8 @@
 .%0A *
-  %0A * @author Loic DASSONVILLE (loic.dassonville at gmail.com)
 %0A * 
@@ -1087,29 +1087,28 @@
 LY (
-brasseld at gmail.com
+david at gravitee.io
 )%0A *
@@ -1353,16 +1353,22 @@
 .elastic
+search
 .protoco
@@ -1496,16 +1496,22 @@
 .elastic
+search
 .cluster
@@ -1621,16 +1621,22 @@
 .elastic
+search
 .index:g
@@ -1742,24 +1742,30 @@
 ters.elastic
+search
 .bulk.action
@@ -1875,24 +1875,30 @@
 ters.elastic
+search
 .bulk.flush_
@@ -2012,16 +2012,22 @@
 .elastic
+search
 .bulk.co
@@ -2788,32 +2788,38 @@
 eporters.elastic
+search
 .hosts%5B%25s%5D%22, 0);
@@ -3352,16 +3352,16 @@
 %09%09%7D%0A%09%09%09%0A
-
 %09%09%09key =
@@ -3393,16 +3393,22 @@
 .elastic
+search
 .hosts%5B%25

Update configuration to follow the convention as per the elasticsearch repository