// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.xml;

import com.yahoo.config.application.Xml;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.model.ConfigModelContext;
import com.yahoo.path.Path;
import com.yahoo.vespa.model.container.ContainerModel;
import com.yahoo.vespa.model.container.configserver.ConfigserverCluster;
import com.yahoo.vespa.model.container.configserver.option.CloudConfigOptions;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.util.List;

/**
 * Builds the config model for the standalone config server.
 *
 * @author Ulf Lilleengen
 */
public class ConfigServerContainerModelBuilder extends ContainerModelBuilder {

    private final CloudConfigOptions options;
    private final static String HOSTED_VESPA_INCLUDE_DIR = "hosted-vespa";

    public ConfigServerContainerModelBuilder(CloudConfigOptions options) {
        super(true, Networking.enable);
        this.options = options;
    }


    @Override
    public void doBuild(ContainerModel model, Element spec, ConfigModelContext modelContext) {
        ApplicationPackage app = modelContext.getDeployState().getApplicationPackage();
        if ( ! app.getFiles(Path.fromString(HOSTED_VESPA_INCLUDE_DIR), ".xml").isEmpty()) {
            app.validateIncludeDir(HOSTED_VESPA_INCLUDE_DIR);
            List<Element> configModelElements = Xml.allElemsFromPath(app, HOSTED_VESPA_INCLUDE_DIR);
            mergeInto(spec, configModelElements);
        }

        ConfigserverCluster cluster = new ConfigserverCluster(modelContext.getParentProducer(), "configserver", options);
        super.doBuild(model, spec, modelContext.withParent(cluster));
        cluster.setContainerCluster(model.getCluster());
    }

    private void mergeInto(Element destination, List<Element> configModelElements) {
        for (Element jdiscElement: configModelElements) {
            for (Node child = jdiscElement.getFirstChild();  child != null; child = child.getNextSibling()) {
                Node copiedNode = destination.getOwnerDocument().importNode(child, true);
                destination.appendChild(copiedNode);
            }
        }
    }
}

@@ -186,161 +186,32 @@
 fig.
-application.Xml;%0Aimport com.yahoo.config.application.api.ApplicationPackage;%0Aimport com.yahoo.config.model.ConfigModelContext;%0Aimport com.yahoo.path.Path
+model.ConfigModelContext
 ;%0Aim
@@ -446,57 +446,8 @@
 ent;
-%0Aimport org.w3c.dom.Node;%0A%0Aimport java.util.List;
 %0A%0A/*
@@ -671,83 +671,8 @@
 ons;
-%0A    private final static String HOSTED_VESPA_INCLUDE_DIR = %22hosted-vespa%22;
 %0A%0A  
@@ -823,17 +823,16 @@
 %0A    %7D%0A%0A
-%0A
     @Ove
@@ -936,412 +936,8 @@
 ) %7B%0A
-        ApplicationPackage app = modelContext.getDeployState().getApplicationPackage();%0A        if ( ! app.getFiles(Path.fromString(HOSTED_VESPA_INCLUDE_DIR), %22.xml%22).isEmpty()) %7B%0A            app.validateIncludeDir(HOSTED_VESPA_INCLUDE_DIR);%0A            List%3CElement%3E configModelElements = Xml.allElemsFromPath(app, HOSTED_VESPA_INCLUDE_DIR);%0A            mergeInto(spec, configModelElements);%0A        %7D%0A%0A
     
@@ -1043,16 +1043,78 @@
 server%22,
+%0A                                                             
  options
@@ -1254,432 +1254,6 @@
  %7D%0A%0A
-    private void mergeInto(Element destination, List%3CElement%3E configModelElements) %7B%0A        for (Element jdiscElement: configModelElements) %7B%0A            for (Node child = jdiscElement.getFirstChild();  child != null; child = child.getNextSibling()) %7B%0A                Node copiedNode = destination.getOwnerDocument().importNode(child, true);%0A                destination.appendChild(copiedNode);%0A            %7D%0A        %7D%0A    %7D%0A
 %7D%0A

Remove magic include