// 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.admin.clustercontroller;

import com.yahoo.cloud.config.ZookeeperServerConfig;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.config.model.api.container.ContainerServiceType;
import com.yahoo.config.model.producer.AbstractConfigProducer;
import com.yahoo.container.bundle.BundleInstantiationSpecification;
import com.yahoo.container.di.config.PlatformBundlesConfig;
import com.yahoo.osgi.provider.model.ComponentModel;
import com.yahoo.vespa.config.content.FleetcontrollerConfig;
import com.yahoo.vespa.model.application.validation.RestartConfigs;
import com.yahoo.vespa.model.container.Container;
import com.yahoo.vespa.model.container.component.AccessLogComponent;
import com.yahoo.vespa.model.container.component.Component;
import com.yahoo.vespa.model.container.component.Handler;

import java.util.Set;
import java.util.TreeSet;

import static com.yahoo.vespa.defaults.Defaults.getDefaults;

/**
 * Container implementation for cluster-controllers
 */
@RestartConfigs({FleetcontrollerConfig.class, ZookeeperServerConfig.class})
public class ClusterControllerContainer extends Container implements
        PlatformBundlesConfig.Producer,
        ZookeeperServerConfig.Producer
{
    private static final ComponentSpecification CLUSTERCONTROLLER_BUNDLE = new ComponentSpecification("clustercontroller-apps");
    private static final ComponentSpecification ZOOKEEPER_SERVER_BUNDLE = new ComponentSpecification("zookeeper-server");

    private final Set<String> bundles = new TreeSet<>();

    public ClusterControllerContainer(AbstractConfigProducer parent, int index, boolean runStandaloneZooKeeper, boolean isHosted) {
        super(parent, "" + index, index);
        addHandler("clustercontroller-status",
                   "com.yahoo.vespa.clustercontroller.apps.clustercontroller.StatusHandler",
                   "clustercontroller-status/*");
        addHandler("clustercontroller-state-restapi-v2",
                   "com.yahoo.vespa.clustercontroller.apps.clustercontroller.StateRestApiV2Handler",
                   "cluster/v2/*");
        if (runStandaloneZooKeeper) {
            addComponent("clustercontroller-zkrunner",
                         "com.yahoo.vespa.zookeeper.VespaZooKeeperServerImpl",
                         ZOOKEEPER_SERVER_BUNDLE);
            addComponent("clustercontroller-zkprovider",
                         "com.yahoo.vespa.clustercontroller.apps.clustercontroller.StandaloneZooKeeperProvider",
                         CLUSTERCONTROLLER_BUNDLE);
        } else {
            addComponent("clustercontroller-zkprovider",
                         "com.yahoo.vespa.clustercontroller.apps.clustercontroller.DummyZooKeeperProvider",
                         CLUSTERCONTROLLER_BUNDLE);
        }
        addComponent(new AccessLogComponent(AccessLogComponent.AccessLogType.jsonAccessLog, "controller", isHosted));

        addFileBundle("lib/jars/clustercontroller-apps-jar-with-dependencies.jar");
        addFileBundle("lib/jars/clustercontroller-apputil-jar-with-dependencies.jar");
        addFileBundle("lib/jars/clustercontroller-core-jar-with-dependencies.jar");
        addFileBundle("lib/jars/clustercontroller-utils-jar-with-dependencies.jar");
        addFileBundle("lib/jars/zookeeper-server-jar-with-dependencies.jar");
    }

    @Override
    public int getWantedPort() {
        return 19050;
    }

    @Override
    public boolean requiresWantedPort() {
        return index() == 0;
    }

    @Override
    public ContainerServiceType myServiceType() {
        return ContainerServiceType.CLUSTERCONTROLLER_CONTAINER;
    }

    private void addHandler(Handler h, String binding) {
        h.addServerBindings("http://*/" + binding);
        super.addHandler(h);
    }

    private void addFileBundle(String bundlePath) {
        bundles.add("file:" + getDefaults().underVespaHome(bundlePath));
    }

    private ComponentModel createComponentModel(String id, String className, ComponentSpecification bundle) {
        return new ComponentModel(new BundleInstantiationSpecification(new ComponentSpecification(id),
                                                                       new ComponentSpecification(className),
                                                                       bundle));
    }

    private void addComponent(String id, String className, ComponentSpecification bundle) {
        addComponent(new Component<>(createComponentModel(id, className, bundle)));
    }

    private void addHandler(String id, String className, String binding) {
        addHandler(new Handler(createComponentModel(id, className, CLUSTERCONTROLLER_BUNDLE)),
                   binding);
    }

    @Override
    public void getConfig(PlatformBundlesConfig.Builder builder) {
        bundles.forEach(builder::bundles);
    }

    @Override
    public void getConfig(ZookeeperServerConfig.Builder builder) {
        builder.myid(index());
    }

}

@@ -944,24 +944,111 @@
 nt.Handler;%0A
+import com.yahoo.vespa.model.container.xml.PlatformBundles;%0A%0Aimport java.nio.file.Path;
 %0Aimport java
@@ -3105,24 +3105,95 @@
 sHosted));%0A%0A
+        // TODO: Why are bundles added here instead of in the cluster?%0A
         addF
@@ -3199,33 +3199,24 @@
 FileBundle(%22
-lib/jars/
 clustercontr
@@ -3229,34 +3229,8 @@
 apps
--jar-with-dependencies.jar
 %22);%0A
@@ -3248,33 +3248,24 @@
 FileBundle(%22
-lib/jars/
 clustercontr
@@ -3281,34 +3281,8 @@
 util
--jar-with-dependencies.jar
 %22);%0A
@@ -3300,33 +3300,24 @@
 FileBundle(%22
-lib/jars/
 clustercontr
@@ -3330,34 +3330,8 @@
 core
--jar-with-dependencies.jar
 %22);%0A
@@ -3353,25 +3353,16 @@
 Bundle(%22
-lib/jars/
 clusterc
@@ -3380,34 +3380,8 @@
 tils
--jar-with-dependencies.jar
 %22);%0A
@@ -3407,17 +3407,8 @@
 le(%22
-lib/jars/
 zook
@@ -3423,34 +3423,8 @@
 rver
--jar-with-dependencies.jar
 %22);%0A
@@ -3923,20 +3923,20 @@
 g bundle
-Path
+Name
 ) %7B%0A    
@@ -3955,57 +3955,64 @@
 add(
-%22file:%22 + getDefaults().underVespaHome(bundlePath
+PlatformBundles.absoluteBundlePath(bundleName).toString(
 ));%0A

Use utililty to simplify adding bundles