// 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.serialization;

import com.yahoo.component.Version;
import com.yahoo.config.provision.AllocatedHosts;
import com.yahoo.config.provision.ClusterMembership;
import com.yahoo.config.provision.DockerImage;
import com.yahoo.config.provision.HostSpec;
import com.yahoo.config.provision.NetworkPorts;
import com.yahoo.config.provision.NodeResources;
import org.junit.Test;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static com.yahoo.config.provision.serialization.AllocatedHostsSerializer.fromJson;
import static com.yahoo.config.provision.serialization.AllocatedHostsSerializer.toJson;
import static org.junit.Assert.assertEquals;

/**
 * @author bratseth
 */
public class AllocatedHostsSerializerTest {

    private static final NodeResources slowSmallNode = new NodeResources(0.5, 3.1, 4, 1, NodeResources.DiskSpeed.slow);
    private static final NodeResources slowBiggerNode = new NodeResources(1.0, 6.2, 8, 2, NodeResources.DiskSpeed.slow);
    private static final NodeResources fastNode = new NodeResources(0.5, 3.1, 4, 1, NodeResources.DiskSpeed.any);

    @Test
    public void testAllocatedHostsSerialization() throws IOException {
        Set<HostSpec> hosts = new LinkedHashSet<>();
        hosts.add(new HostSpec("empty", List.of(), Optional.empty()));
        hosts.add(new HostSpec("with-aliases", List.of("alias1", "alias2"), Optional.empty()));
        hosts.add(new HostSpec("allocated",
                               slowSmallNode,
                               slowBiggerNode,
                               fastNode,
                               ClusterMembership.from("container/test/0/0", Version.fromString("6.73.1"),
                                                      Optional.of(DockerImage.fromString("docker.foo.com:4443/vespa/bar"))),
                               Optional.empty(),
                               Optional.empty(),
                               Optional.of(DockerImage.fromString("docker.foo.com:4443/vespa/bar"))));
        hosts.add(new HostSpec("flavor-from-resources-2",
                               slowSmallNode,
                               slowBiggerNode,
                               fastNode,
                               ClusterMembership.from("container/test/0/0", Version.fromString("6.73.1"),
                                                      Optional.empty()),
                               Optional.empty(),
                               Optional.empty(),
                               Optional.empty()));
        hosts.add(new HostSpec("with-version",
                               slowSmallNode,
                               slowBiggerNode,
                               fastNode,
                               ClusterMembership.from("container/test/0/0", Version.fromString("6.73.1"),
                                                      Optional.empty()),
                               Optional.of(Version.fromString("3.4.5")),
                               Optional.empty(), Optional.empty()));
        hosts.add(new HostSpec("with-ports",
                               slowSmallNode,
                               slowBiggerNode,
                               fastNode,
                               ClusterMembership.from("container/test/0/0", Version.fromString("6.73.1"),
                                                      Optional.empty()),
                               Optional.empty(),
                               Optional.of(new NetworkPorts(List.of(new NetworkPorts.Allocation(1234, "service1", "configId1", "suffix1"),
                                                                    new NetworkPorts.Allocation(4567, "service2", "configId2", "suffix2")))),
                               Optional.empty()));

        assertAllocatedHosts(AllocatedHosts.withHosts(hosts));
    }

    private void assertAllocatedHosts(AllocatedHosts expectedHosts) throws IOException {
        AllocatedHosts deserializedHosts = fromJson(toJson(expectedHosts));

        assertEquals(expectedHosts, deserializedHosts);
        for (HostSpec expectedHost : expectedHosts.getHosts()) {
            HostSpec deserializedHost = requireHost(expectedHost.hostname(), deserializedHosts);
            assertEquals(expectedHost.hostname(), deserializedHost.hostname());
            assertEquals(expectedHost.membership(), deserializedHost.membership());
            assertEquals(expectedHost.realResources(), deserializedHost.realResources());
            assertEquals(expectedHost.advertisedResources(), deserializedHost.advertisedResources());
            assertEquals(expectedHost.requestedResources(), deserializedHost.requestedResources());
            assertEquals(expectedHost.version(), deserializedHost.version());
            assertEquals(expectedHost.networkPorts(), deserializedHost.networkPorts());
            assertEquals(expectedHost.aliases(), deserializedHost.aliases());
            assertEquals(expectedHost.dockerImageRepo(), deserializedHost.dockerImageRepo());
        }
    }

    private HostSpec requireHost(String hostname, AllocatedHosts hosts) {
        for (HostSpec host : hosts.getHosts())
            if (host.hostname().equals(hostname))
                return host;
        throw new IllegalArgumentException("No host " + hostname + " is present");
    }

}

@@ -984,24 +984,33 @@
 ources s
-lowS
 mall
+SlowDiskSpeed
 Node = n
@@ -1112,26 +1112,32 @@
 sources 
-slowBigger
+bigSlowDiskSpeed
 Node = n
@@ -1239,20 +1239,28 @@
 sources 
-fast
+anyDiskSpeed
 Node = n
@@ -1696,32 +1696,41 @@
            s
-lowS
 mall
+SlowDiskSpeed
 Node,%0A      
@@ -1750,34 +1750,40 @@
             
-slowBigger
+bigSlowDiskSpeed
 Node,%0A      
@@ -1799,36 +1799,44 @@
                 
-fast
+anyDiskSpeed
 Node,%0A          
@@ -2343,32 +2343,41 @@
            s
-lowS
 mall
+SlowDiskSpeed
 Node,%0A      
@@ -2397,34 +2397,40 @@
             
-slowBigger
+bigSlowDiskSpeed
 Node,%0A      
@@ -2446,36 +2446,44 @@
                 
-fast
+anyDiskSpeed
 Node,%0A          
@@ -2875,32 +2875,41 @@
            s
-lowS
 mall
+SlowDiskSpeed
 Node,%0A      
@@ -2929,34 +2929,40 @@
             
-slowBigger
+bigSlowDiskSpeed
 Node,%0A      
@@ -2978,36 +2978,44 @@
                 
-fast
+anyDiskSpeed
 Node,%0A          
@@ -3406,16 +3406,25 @@
    s
-lowS
 mall
+SlowDiskSpeed
 Node
@@ -3460,18 +3460,24 @@
     
-slowBigger
+bigSlowDiskSpeed
 Node
@@ -3513,12 +3513,20 @@
     
-fast
+anyDiskSpeed
 Node

Rename node test node resources