aboutsummaryrefslogtreecommitdiffstats
path: root/flags/src/main/java/com/yahoo/vespa/flags/json/DimensionHelper.java
blob: 4323af5a4fdc7a537a3c4d7e3f088e745f184d11 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.flags.json;

import com.yahoo.vespa.flags.FetchVector;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hakonhall
 */
public class DimensionHelper {

    private static final Map<FetchVector.Dimension, String> serializedDimensions = new HashMap<>();

    static {
        // WARNING: If you ever change the serialized form of a dimension, ensure the new serialized
        // flag data are pushed out everywhere before removing support for old format, see VESPA-27760.
        serializedDimensions.put(FetchVector.Dimension.APPLICATION_ID, "application");
        serializedDimensions.put(FetchVector.Dimension.CLOUD, "cloud");
        serializedDimensions.put(FetchVector.Dimension.CLOUD_ACCOUNT, "cloud-account");
        serializedDimensions.put(FetchVector.Dimension.CLUSTER_ID, "cluster-id");
        serializedDimensions.put(FetchVector.Dimension.CLUSTER_TYPE, "cluster-type");
        serializedDimensions.put(FetchVector.Dimension.CONSOLE_USER_EMAIL, "console-user-email");
        serializedDimensions.put(FetchVector.Dimension.ENVIRONMENT, "environment");
        serializedDimensions.put(FetchVector.Dimension.HOSTNAME, "hostname");
        serializedDimensions.put(FetchVector.Dimension.INSTANCE_ID, "instance");
        serializedDimensions.put(FetchVector.Dimension.NODE_TYPE, "node-type");
        serializedDimensions.put(FetchVector.Dimension.SYSTEM, "system");
        serializedDimensions.put(FetchVector.Dimension.TENANT_ID, "tenant");
        serializedDimensions.put(FetchVector.Dimension.VESPA_VERSION, "vespa-version");
        serializedDimensions.put(FetchVector.Dimension.ZONE_ID, "zone");

        if (serializedDimensions.size() != FetchVector.Dimension.values().length) {
            throw new IllegalStateException(FetchVectorHelper.class.getName() + " is not in sync with " +
                    FetchVector.Dimension.class.getName());
        }
    }

    private static final Map<String, FetchVector.Dimension> deserializedDimensions = serializedDimensions.
            entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));

    public static String toWire(FetchVector.Dimension dimension) {
        String serializedDimension = serializedDimensions.get(dimension);
        if (serializedDimension == null) {
            throw new IllegalArgumentException("Unsupported dimension (please add it): '" + dimension + "'");
        }

        return serializedDimension;
    }

    public static FetchVector.Dimension fromWire(String serializedDimension) {
        FetchVector.Dimension dimension = deserializedDimensions.get(serializedDimension);
        if (dimension == null) {
            throw new IllegalArgumentException("Unknown serialized dimension: '" + serializedDimension + "'");
        }

        return dimension;
    }

    private DimensionHelper() { }

}