/*
 * Copyright 2015 Richard Thurston.
 *
 * 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 com.northernwall.hadrian.workItem.action;

import com.northernwall.hadrian.config.Const;
import com.northernwall.hadrian.domain.Host;
import com.northernwall.hadrian.domain.WorkItem;
import com.northernwall.hadrian.workItem.Result;
import com.northernwall.hadrian.workItem.dao.CallbackData;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HostDeleteAction extends Action {

    private final static Logger LOGGER = LoggerFactory.getLogger(HostDeleteAction.class);

    @Override
    public void updateStatus(WorkItem workItem) {
        Host host = dataAccess.getHost(workItem.getService().serviceId, workItem.getHost().hostId);
        if (host == null) {
            LOGGER.warn("Could not find host {} being deleted", workItem.getHost().hostId);
            return;
        }
        dataAccess.updateStatus(
                workItem.getHost().hostId,
                true,
                "Deleting...",
                Const.STATUS_WIP);
    }

    @Override
    public Result process(WorkItem workItem) {
        LOGGER.info("Deleting host {} for {}", workItem.getHost().hostName, workItem.getService().serviceName);
        return Result.success;
    }

    @Override
    public Result processCallback(WorkItem workItem, CallbackData callbackData) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void recordAudit(WorkItem workItem, Result result, Map<String, String> notes, String output) {
        notes.put("Reason", workItem.getReason());
        writeAudit(workItem, result, notes, output);
    }

    @Override
    public void success(WorkItem workItem) {
        Host host = dataAccess.getHost(workItem.getService().serviceId, workItem.getHost().hostId);
        if (host == null) {
            LOGGER.warn("Could not find host {} being delete.", workItem.getHost().hostId);
            return;
        }
        
        dataAccess.deleteHost(host);
        dataAccess.deleteSearch(
                Const.SEARCH_SPACE_HOST_NAME, 
                host.getHostName());
    }

    @Override
    public void error(WorkItem workItem) {
        Host host = dataAccess.getHost(workItem.getService().serviceId, workItem.getHost().hostId);
        if (host == null) {
            LOGGER.warn("Could not find host {} being delete.", workItem.getHost().hostId);
            return;
        }
        
        dataAccess.updateStatus(
                host.getHostId(),
                false,
                "Delete failed %% ago",
                Const.STATUS_ERROR);
    }

}

@@ -916,16 +916,85 @@
 kData;%0D%0A
+import java.net.InetAddress;%0D%0Aimport java.net.UnknownHostException;%0D%0A
 import j
@@ -1008,16 +1008,16 @@
 l.Map;%0D%0A
-
 import o
@@ -1766,32 +1766,126 @@
 em workItem) %7B%0D%0A
+        String hostname = workItem.getHost().hostName;%0D%0A        checkIfHostExists(hostname);%0D%0A
         LOGGER.i
@@ -1919,32 +1919,13 @@
 %7D%22, 
-workItem.getHost().
 host
-N
+n
 ame,
@@ -1992,32 +1992,640 @@
 success;%0D%0A    %7D%0D
+%0A    %0D%0A    protected boolean checkIfHostExists(String hostname) %7B%0D%0A        if (parameters.getBoolean(Const.CHECK_RESOLVE_HOSTNAME, Const.CHECK_RESOLVE_HOSTNAME_DEFAULT)) %7B%0D%0A            try %7B%0D%0A                InetAddress address = InetAddress.getByName(hostname);%0D%0A                LOGGER.info(%22Deleting host %7B%7D with IP address %7B%7D%22, hostname, address.getHostAddress());%0D%0A            %7D catch (UnknownHostException ex) %7B%0D%0A                LOGGER.warn(%22Could not find host %7B%7D to delete, IP address does not resolve%22, hostname);%0D%0A                return false;%0D%0A            %7D%0D%0A        %7D%0D%0A        return true;%0D%0A    %7D%0D
 %0A%0D%0A    @Override

Add helper method that checks if a host is already decommissioned