// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.persistence;

import com.yahoo.component.Version;
import com.yahoo.config.provision.NodeType;
import com.yahoo.slime.ObjectTraverser;
import com.yahoo.slime.Slime;
import com.yahoo.vespa.config.SlimeUtils;
import com.yahoo.vespa.hosted.provision.node.OsVersion;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.TreeMap;

/**
 * Serializer for a map of {@link NodeType} and {@link OsVersion}.
 *
 * @author mpolden
 */
// TODO(mpolden): Remove this and replaces usages with NodeTypeVersionsSerializer after January 2020
public class OsVersionsSerializer {

    private static final String VERSION_FIELD = "version";
    private static final String ACTIVE_FIELD = "active";

    private OsVersionsSerializer() {}

    public static byte[] toJson(Map<NodeType, Version> versions) {
        var slime = new Slime();
        var object = slime.setObject();
        versions.forEach((nodeType, osVersion) -> {
            var versionObject = object.setObject(NodeSerializer.toString(nodeType));
            versionObject.setString(VERSION_FIELD, osVersion.toFullString());
            versionObject.setBool(ACTIVE_FIELD, true);
        });
        try {
            return SlimeUtils.toJsonBytes(slime);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    public static Map<NodeType, Version> fromJson(byte[] data) {
        var versions = new TreeMap<NodeType, Version>(); // Use TreeMap to sort by node type
        var inspector = SlimeUtils.jsonToSlime(data).get();
        inspector.traverse((ObjectTraverser) (key, value) -> {
            var version = Version.fromString(value.field(VERSION_FIELD).asString());
            versions.put(NodeSerializer.nodeTypeFromString(key), version);
        });
        return versions;
    }

}

@@ -625,109 +625,8 @@
  */%0A
-// TODO(mpolden): Remove this and replaces usages with NodeTypeVersionsSerializer after January 2020%0A
 publ
@@ -720,65 +720,8 @@
 on%22;
-%0A    private static final String ACTIVE_FIELD = %22active%22;
 %0A%0A  
@@ -1116,63 +1116,8 @@
 ));%0A
-            versionObject.setBool(ACTIVE_FIELD, true);%0A
     

Stop writing unused field