// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;

import java.util.List;

/**
 * The possible types of nodes in the node repository
 * 
 * @author bratseth
 */
public enum NodeType {

    /** Node assignable to a tenant to run application workloads */
    tenant("Tenant node"),

    /** Host of a tenant nodes */
    host("Tenant host", tenant),

    /** Node serving the shared proxy layer */
    proxy("Proxy node"),

    /** Host of a proxy node */
    proxyhost("Proxy host", proxy),

    /** Config server node */
    config("Config server node"),

    /** Host of a config server node */
    confighost("Config server host", config),

    /** Controller node */
    controller("Controller node"),

    /** Host of a controller node */
    controllerhost("Controller host", controller),

    /** Host capable of running multiple node types, only used in {@link SystemName#dev} */
    devhost("Dev host", config, controller, tenant);

    private final String description;
    private final List<NodeType> childNodeTypes;

    NodeType(String description, NodeType... childNodeTypes) {
        this.childNodeTypes = List.of(childNodeTypes);
        this.description = description;
    }

    public boolean isHost() {
        return !childNodeTypes.isEmpty();
    }

    /** Returns whether this supports host sharing */
    public boolean isSharable() {
        return this == NodeType.host;
    }

    public String description() {
        return description;
    }

    /**
     * @return {@link NodeType} of the node(s) that run on this host
     * @throws IllegalStateException if this type is not a host
     */
    public NodeType childNodeType() {
        return childNodeTypes().get(0);
    }

    /**
     * @return all {@link NodeType}s that can run on this host
     * @throws IllegalStateException if this type is not a host
     */
    public List<NodeType> childNodeTypes() {
        if (! isHost())
            throw new IllegalStateException(this + " has no children");
        return childNodeTypes;
    }

    /** Returns whether given node type can run on this */
    public boolean canRun(NodeType type) {
        return childNodeTypes.contains(type);
    }

    /** Returns the host type of this */
    public NodeType hostType() {
        if (isHost()) return this;
        for (NodeType nodeType : values()) {
            if (nodeType.childNodeTypes.size() == 1 && nodeType.canRun(this)) {
                return nodeType;
            }
        }
        throw new IllegalArgumentException("No host of " + this + " exists");
    }

}

@@ -1363,32 +1363,350 @@
 Empty();%0A    %7D%0A%0A
+    /** either config server or controller */%0A    public boolean isConfigServerLike() %7B%0A        return this == config %7C%7C this == controller;%0A    %7D%0A%0A    /** either config server host or controller host */%0A    public boolean isConfigServerHostLike() %7B%0A        return this == confighost %7C%7C this == controllerhost;%0A    %7D%0A%0A
     /** Returns 

Add short-cut method for config server host or controller host