/*
 * 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.
 */
package io.brooklyn.ambari.agent;

import static java.lang.String.format;
import static org.apache.brooklyn.util.ssh.BashCommands.installPackage;
import static org.apache.brooklyn.util.ssh.BashCommands.sudo;

import org.apache.brooklyn.api.entity.EntityLocal;
import org.apache.brooklyn.entity.java.JavaSoftwareProcessSshDriver;
import org.apache.brooklyn.entity.software.base.SoftwareProcess;
import org.apache.brooklyn.location.ssh.SshMachineLocation;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.ssh.BashCommands;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableList;

import io.brooklyn.ambari.AmbariCluster;
import io.brooklyn.ambari.AmbariInstallCommands;
import io.brooklyn.ambari.server.AmbariServer;

public class AmbariAgentSshDriver extends JavaSoftwareProcessSshDriver implements AmbariAgentDriver {
    public static final Logger log = LoggerFactory.getLogger(AmbariAgentSshDriver.class);
    private final AmbariInstallCommands defaultAmbariInstallHelper = new AmbariInstallCommands(entity.getConfig(SoftwareProcess.SUGGESTED_VERSION));

    public AmbariAgentSshDriver(EntityLocal entity, SshMachineLocation machine) {
        super(entity, machine);
    }

    @Override
    protected String getLogFileLocation() {
        return "/var/log/ambari-agent/ambari-agent.log";
    }

    @Override
    public boolean isRunning() {
        return newScript(MutableMap.of("usePidFile", false), CHECK_RUNNING).body.append(sudo("ambari-agent status")).execute() == 0;
    }

    @Override
    public void stop() {
        newScript(STOPPING).body.append(sudo("ambari-agent stop")).execute();
    }

    @Override
    public AmbariAgentImpl getEntity() {
        return AmbariAgentImpl.class.cast(super.getEntity());
    }

    @Override
    public void install() {
        String parentFQDN =
        entity.getParent() instanceof AmbariServer
                ? ((AmbariServer) entity.getParent()).getFqdn()
                : "";

        String fqdn =
                parentFQDN.isEmpty()
                ? String.format("%s-%s.%s", entity.getParent().getDisplayName().toLowerCase(), entity.getId().toLowerCase(), entity.getConfig(AmbariCluster.DOMAIN_NAME))
                : parentFQDN;

        getEntity().setFqdn(fqdn);
        ImmutableList<String> commands =
                ImmutableList.<String>builder()
                        .add(defaultAmbariInstallHelper.installAmbariRequirements(getMachine()))
                        .addAll(BashCommands.setHostname(fqdn))
                        .add(installPackage("ambari-agent"))
                        .build();

        newScript(INSTALLING).body
                .append(commands)
                .failOnNonZeroResultCode()
                .execute();
    }

    @Override
    public void customize() {
        String tmpConfigFileLoc = "/tmp/ambari-agent.ini";
        String destinationConfigFile = "/etc/ambari-agent/conf/ambari-agent.ini";

        copyTemplate(getTemplateConfigurationUrl(), tmpConfigFileLoc);

        newScript(CUSTOMIZING)
                .body.append(sudo(format("mv %s %s", tmpConfigFileLoc, destinationConfigFile)))
                .failOnNonZeroResultCode()
                .execute();

    }

    @Override
    public void launch() {
        newScript(LAUNCHING).body.append(sudo("ambari-agent start")).failOnNonZeroResultCode().execute();
    }

    String getTemplateConfigurationUrl() {
        return entity.getConfig(AmbariAgent.TEMPLATE_CONFIGURATION_URL);
    }

}

@@ -1056,21 +1056,116 @@
 y.Entity
-Local
+;%0Aimport org.apache.brooklyn.api.entity.EntityLocal;%0Aimport org.apache.brooklyn.core.entity.Entities
 ;%0Aimport
@@ -1563,16 +1563,60 @@
 bleList;
+%0Aimport com.google.common.collect.Iterables;
 %0A%0Aimport
@@ -1695,24 +1695,77 @@
 llCommands;%0A
+import io.brooklyn.ambari.hostgroup.AmbariHostGroup;%0A
 import io.br
@@ -3024,24 +3024,220 @@
      : %22%22;%0A%0A
+        Entity parentHostGroup =%0A                Iterables.getFirst(%0A                        Iterables.filter(Entities.ancestors(entity), AmbariHostGroup.class),%0A                        entity);%0A%0A
         Stri
@@ -3327,34 +3327,31 @@
 %25s.%25s%22, 
-entity.getParent()
+parentHostGroup
 .getDisp

Use hostgroup ancestor for fqdn