// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.util;

import com.yahoo.vespa.applicationmodel.HostName;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Various utilities for interacting with node-admin's environment.
 *
 * @author bakksjo
 */
public class Environment {
    private static final String ENV_CONFIGSERVERS = "services__addr_configserver";
    private static final String ENV_NETWORK_TYPE = "NETWORK_TYPE";

    public enum NetworkType { normal, local, vm }

    public Set<HostName> getConfigServerHosts() {
        final String configServerHosts = System.getenv(ENV_CONFIGSERVERS);
        if (configServerHosts == null) {
            return Collections.emptySet();
        }

        final List<String> hostNameStrings = Arrays.asList(configServerHosts.split("[,\\s]+"));
        return hostNameStrings.stream()
                .map(HostName::new)
                .collect(Collectors.toSet());
    }

    public NetworkType networkType() throws IllegalArgumentException {
        String networkTypeInEnvironment = System.getenv(ENV_NETWORK_TYPE);
        if (networkTypeInEnvironment == null) {
            return NetworkType.normal;
        }
        return NetworkType.valueOf(networkTypeInEnvironment);
    }

    public InetAddress getInetAddressForHost(String hostname) throws UnknownHostException {
        return InetAddress.getByName(hostname);
    }
}

@@ -685,16 +685,128 @@
 K_TYPE%22;
+%0A    private static final String ENVIRONMENT = %22ENVIRONMENT%22;%0A    private static final String REGION = %22REGION%22;
 %0A%0A    pu
@@ -1222,16 +1222,30 @@
         
+              
 .map(Hos
@@ -1256,16 +1256,30 @@
 e::new)%0A
+              
         
@@ -1566,16 +1566,16 @@
       %7D%0A
-
         
@@ -1627,32 +1627,559 @@
 onment);%0A    %7D%0A%0A
+    public String getEnvironment() %7B%0A        return getEnvironmentVariable(ENVIRONMENT);%0A    %7D%0A%0A    public String getRegion() %7B%0A        return getEnvironmentVariable(REGION);%0A    %7D%0A%0A    private String getEnvironmentVariable(String name) %7B%0A        final String region = System.getenv(name);%0A        if (region == null) throw new IllegalStateException(String.format(%22Environment variable %25s not set%22, name));%0A        return region;%0A    %7D%0A%0A    public String getZone() %7B%0A        return getEnvironment() + %22.%22 + getRegion();%0A    %7D%0A%0A
     public InetA

Add method for getting zone