// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.maintenance;

import com.yahoo.config.provision.SystemName;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.Node;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.NodeRepository;
import com.yahoo.vespa.hosted.controller.api.integration.entity.EntityService;
import com.yahoo.vespa.hosted.controller.api.integration.entity.NodeEntity;
import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeRepositoryNode;

import java.time.Duration;
import java.util.EnumSet;
import java.util.Map;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * Ensures that the host information for all hosts is up to date.
 *
 * @author mpolden
 * @author bjormel
 */
public class HostInfoUpdater extends ControllerMaintainer {

    private static final Logger LOG = Logger.getLogger(HostInfoUpdater.class.getName());
    private static final Pattern HOST_PATTERN = Pattern.compile("^(proxy|cfg|controller)host(.+)$");

    private final NodeRepository nodeRepository;

    public HostInfoUpdater(Controller controller, Duration interval) {
        super(controller, interval, null, EnumSet.of(SystemName.cd, SystemName.main));
        this.nodeRepository = controller.serviceRegistry().configServer().nodeRepository();
    }

    @Override
    protected boolean maintain() {
        Map<String, NodeEntity> nodeEntities = controller().serviceRegistry().entityService().listNodes().stream()
                                                           .collect(Collectors.toMap(NodeEntity::hostname,
                                                                                     Function.identity()));
        int nodesUpdated = 0;
        try {
            for (var zone : controller().zoneRegistry().zones().controllerUpgraded().all().ids()) {
                for (var node : nodeRepository.list(zone, false)) {
                    if (!node.type().isHost()) continue;
                    NodeEntity nodeEntity = nodeEntities.get(registeredHostnameOf(node));
                    if (!shouldUpdateSwitch(node, nodeEntity) && !shouldUpdateModel(node, nodeEntity)) continue;

                    NodeRepositoryNode updatedNode = new NodeRepositoryNode();
                    if (nodeEntity.switchHostname().isPresent()) {
                        updatedNode.setSwitchHostname(nodeEntity.switchHostname().get());
                    }
                    if (nodeEntity.model().isPresent()) {
                        updatedNode.setModelName(nodeEntity.model().get());
                    }
                    nodeRepository.patchNode(zone, node.hostname().value(), updatedNode);
                    nodesUpdated++;
                }
            }
        } finally {
            if (nodesUpdated > 0) {
                LOG.info("Updated switch hostname for " + nodesUpdated + " node(s)");
            }
        }
        return true;
    }

    /** Returns the hostname that given host is registered under in the {@link EntityService} */
    private static String registeredHostnameOf(Node host) {
        String hostname = host.hostname().value();
        if (!host.type().isHost()) return hostname;
        Matcher matcher = HOST_PATTERN.matcher(hostname);
        if (!matcher.matches()) return hostname;
        return matcher.replaceFirst("$1$2");
    }

    private static boolean shouldUpdateSwitch(Node node, NodeEntity nodeEntity) {
        if (nodeEntity == null) return false;
        if (nodeEntity.switchHostname().isEmpty())  return false;
        return !node.switchHostname().equals(nodeEntity.switchHostname());
    }

    private static boolean shouldUpdateModel(Node node, NodeEntity nodeEntity) {
        if (nodeEntity == null) return false;
        if (nodeEntity.model().isEmpty())  return false;
        return !node.modelName().equals(nodeEntity.model());
    }

}

@@ -1981,20 +1981,20 @@
     int 
-node
+host
 sUpdated
@@ -2966,20 +2966,20 @@
         
-node
+host
 sUpdated
@@ -3050,20 +3050,20 @@
     if (
-node
+host
 sUpdated
@@ -3108,23 +3108,19 @@
 ted 
-switch hostname
+information
  for
@@ -3124,20 +3124,20 @@
 for %22 + 
-node
+host
 sUpdated
@@ -3141,20 +3141,21 @@
 ted + %22 
-node
+hosts
 (s)%22);%0A 

Update log message