package com.yahoo.vespa.hosted.node.admin.logging;

import com.yahoo.vespa.hosted.node.admin.ContainerNodeSpec;
import com.yahoo.vespa.hosted.node.admin.util.Environment;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author mortent
 */
public class FilebeatConfigProvider {

    private static final String TEMPLATE = "filebeat.yml.template";
    private final Environment environment;

    private static final String TENANT_FIELD = "%%TENANT%%";
    private static final String APPLICATION_FIELD = "%%APPLICATION%%";
    private static final String INSTANCE_FIELD = "%%INSTANCE%%";
    private static final String ENVIRONMENT_FIELD = "%%ENVIRONMENT%%";
    private static final String REGION_FIELD = "%%REGION%%";
    private static final String FILEBEAT_SPOOL_SIZE_FIELD = "%%FILEBEAT_SPOOL_SIZE%%";
    private static final String LOGSTASH_HOSTS_FIELD = "%%LOGSTASH_HOSTS%%";
    private static final String LOGSTASH_WORKERS_FIELD = "%%LOGSTASH_WORKERS%%";
    private static final String LOGSTASH_BULK_MAX_SIZE_FIELD = "%%LOGSTASH_BULK_MAX_SIZE%%";

    private static final int logstashWorkers = 3;
    private static final int logstashBulkMaxSize = 2048;

    public FilebeatConfigProvider(Environment environment) {
        this.environment = environment;
    }

    public Optional<String> getConfig(ContainerNodeSpec containerNodeSpec) throws IOException {
        if(environment.getLogstashNodes().size() == 0) {
            return Optional.empty();
        }

        // Get a template with the environment stuff filled in
        String template = replaceEnvironmentInfo(readTemplate());

        // Replace application specific parts
        Optional<ContainerNodeSpec.Owner> owner = containerNodeSpec.owner;
        return owner.map(o -> replaceOwnerInfo(o, template));
    }

    private String replaceEnvironmentInfo(String template) {
        template = template.replaceAll(ENVIRONMENT_FIELD, environment.getEnvironment());
        template = template.replaceAll(REGION_FIELD, environment.getRegion());

        int spoolSize = environment.getLogstashNodes().size() * logstashWorkers * logstashBulkMaxSize;
        template = template.replaceAll(FILEBEAT_SPOOL_SIZE_FIELD, Integer.toString(spoolSize));
        template = template.replaceAll(LOGSTASH_HOSTS_FIELD, environment.getLogstashNodes().stream().collect(Collectors.joining(",")));
        template = template.replaceAll(LOGSTASH_WORKERS_FIELD, Integer.toString(logstashWorkers));
        template = template.replaceAll(LOGSTASH_BULK_MAX_SIZE_FIELD, Integer.toString(logstashBulkMaxSize));
        return template;
    }

    private String replaceOwnerInfo(ContainerNodeSpec.Owner owner, String template) {
        template = template.replaceAll(TENANT_FIELD, owner.tenant);
        template = template.replaceAll(APPLICATION_FIELD, owner.application);
        template = template.replaceAll(INSTANCE_FIELD, owner.instance);
        return template;
    }

    private String readTemplate() throws IOException {
        String file = getClass().getClassLoader().getResource(TEMPLATE).getFile();
        List<String> lines = Files.readAllLines(Paths.get(file));
        return lines.stream().collect(Collectors.joining("\n"));
    }
}

@@ -474,52 +474,8 @@
 e%22;%0A
-    private final Environment environment;%0A%0A
     
@@ -1137,17 +1137,16 @@
 IZE%25%25%22;%0A
-%0A
     priv
@@ -1243,16 +1243,59 @@
  = 2048;
+%0A    private final Environment environment;
 %0A%0A    pu
@@ -1484,32 +1484,33 @@
 s IOException %7B%0A
+%0A
         if(envir
@@ -1503,16 +1503,17 @@
       if
+ 
 (environ
@@ -1551,198 +1551,97 @@
 == 0
-) %7B%0A            return Optional.empty();%0A        %7D%0A%0A        // Get a template with the environment stuff filled in%0A        String template = replaceEnvironmentInfo(readTemplate()
+ %7C%7C !containerNodeSpec.owner.isPresent()) %7B%0A            return Optional.empty(
 );%0A
-%0A
         
 // R
@@ -1640,63 +1640,18 @@
     
-// Replace application specific parts%0A        Optional%3C
+%7D%0A        
 Cont
@@ -1669,17 +1669,16 @@
 ec.Owner
-%3E
  owner =
@@ -1701,16 +1701,22 @@
 ec.owner
+.get()
 ;%0A      
@@ -1721,138 +1721,156 @@
     
-return owner.map(o -%3E replaceOwnerInfo(o, template))
+int spoolSize = environment.getLogstashNodes().size() * logstashWorkers * logstashBulkMaxSize
 ;%0A    
-%7D%0A%0A
     
-private String replaceEnvironmentInfo(String t
+return Optional.of(readT
 emplate
-) %7B%0A
+()%0A   
         
 temp
@@ -1865,35 +1865,21 @@
         
-template = template
+     
 .replace
@@ -1930,26 +1930,30 @@
 nment())
-;
 %0A
+     
         
 template
@@ -1944,35 +1944,19 @@
         
-template = template
+   
 .replace
@@ -1997,18 +1997,16 @@
 egion())
-;%0A
 %0A       
@@ -2010,130 +2010,16 @@
     
-int spoolSize = environment.getLogstashNodes().size() * logstashWorkers * logstashBulkMaxSize;%0A        template = template
+        
 .rep
@@ -2077,25 +2077,24 @@
 (spoolSize))
-;
 %0A        tem
@@ -2090,35 +2090,24 @@
         
-template = template
+        
 .replace
@@ -2205,17 +2205,16 @@
 g(%22,%22)))
-;
 %0A       
@@ -2214,35 +2214,24 @@
         
-template = template
+        
 .replace
@@ -2292,17 +2292,16 @@
 orkers))
-;
 %0A       
@@ -2301,35 +2301,24 @@
         
-template = template
+        
 .replace
@@ -2389,17 +2389,16 @@
 axSize))
-;
 %0A       
@@ -2402,145 +2402,16 @@
     
-return template;%0A    %7D%0A%0A    private String replaceOwnerInfo(ContainerNodeSpec.Owner owner, String template) %7B%0A        template = template
+        
 .rep
@@ -2445,17 +2445,16 @@
 .tenant)
-;
 %0A       
@@ -2454,35 +2454,24 @@
         
-template = template
+        
 .replace
@@ -2511,17 +2511,16 @@
 ication)
-;
 %0A       
@@ -2524,27 +2524,16 @@
     
-template = template
+        
 .rep
@@ -2575,33 +2575,9 @@
 nce)
-;%0A        return template
+)
 ;%0A  

Improve replace impl