aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/restapi/NodeResourcesSerializer.java
blob: 518474c3711631dbdf7f281f06092e3a0f359984 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.restapi;

import com.yahoo.config.provision.NodeResources;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Inspector;

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

    static void toSlime(NodeResources resources, Cursor object) {
        object.setDouble("vcpu", resources.vcpu());
        object.setDouble("memoryGb", resources.memoryGb());
        object.setDouble("diskGb", resources.diskGb());
        object.setDouble("bandwidthGbps", resources.bandwidthGbps());
        object.setString("diskSpeed", toString(resources.diskSpeed()));
        object.setString("storageType", toString(resources.storageType()));
        object.setString("architecture", toString(resources.architecture()));
        if (!resources.gpuResources().isDefault()) {
            object.setLong("gpuCount", resources.gpuResources().count());
            object.setDouble("gpuMemoryGb", resources.gpuResources().memoryGb());
        }
    }

    public static NodeResources.DiskSpeed diskSpeedFrom(String diskSpeed) {
        return switch (diskSpeed) {
            case "fast" -> NodeResources.DiskSpeed.fast;
            case "slow" -> NodeResources.DiskSpeed.slow;
            case "any" -> NodeResources.DiskSpeed.any;
            default -> throw new IllegalArgumentException("Unknown disk speed '" + diskSpeed + "'");
        };
    }

    private static String toString(NodeResources.DiskSpeed diskSpeed) {
        return switch (diskSpeed) {
            case fast -> "fast";
            case slow -> "slow";
            case any -> "any";
        };
    }

    public static NodeResources.StorageType storageTypeFrom(String storageType) {
        return switch (storageType) {
            case "local" -> NodeResources.StorageType.local;
            case "remote" -> NodeResources.StorageType.remote;
            case "any" -> NodeResources.StorageType.any;
            default -> throw new IllegalArgumentException("Unknown storage type '" + storageType + "'");
        };
    }

    private static String toString(NodeResources.StorageType storageType) {
        return switch (storageType) {
            case remote -> "remote";
            case local -> "local";
            case any -> "any";
        };
    }

    private static String toString(NodeResources.Architecture architecture) {
        return switch (architecture) {
            case arm64 -> "arm64";
            case x86_64 -> "x86_64";
            case any -> "any";
        };
    }

    public static NodeResources.Architecture architectureFrom(String architecture) {
        return switch (architecture) {
            case "arm64" -> NodeResources.Architecture.arm64;
            case "x86_64" -> NodeResources.Architecture.x86_64;
            case "any" -> NodeResources.Architecture.any;
            default -> throw new IllegalArgumentException("Unknown architecture '" + architecture + "'");
        };
    }

    public static NodeResources.GpuResources gpuResourcesFromSlime(Inspector gpuObject) {
        if (!gpuObject.valid()) return NodeResources.GpuResources.getDefault();
        return new NodeResources.GpuResources((int) gpuObject.field("gpuCount").asLong(),
                                              gpuObject.field("gpuMemory").asDouble());
    }

}