aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/identifiers/Identifier.java
blob: e8814b199ba0499c18fdfe0ff55809808c19259d (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.identifiers;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @author smorgrav
 */
public abstract class Identifier {

    // Note: The limit of 20 characters is due to hostname being 'cluster--app--tenant', which can be max. 63 characters.
    // This is an issue for level 7 routing, with level 4 routing we do not have the same limitation
    protected static final String strictPatternExplanation =
            "Tenant, application and instance names must start with a letter, may contain no more than 20 " +
            "characters, and may only contain lowercase letters, digits or dashes, but no double-dashes.";
    protected static final Pattern strictPattern = Pattern.compile("^(?=.{1,20}$)[a-z](-?[a-z0-9]+)*$");
    private static final Pattern serializedIdentifierPattern = Pattern.compile("[a-zA-Z0-9_-]+");
    private static final Pattern serializedPattern = Pattern.compile("[a-zA-Z0-9_.-]+");

    private final String id;

    @JsonCreator
    public Identifier(String id) {
        Objects.requireNonNull(id, "Id string cannot be null");
        this.id = id;
        validate();
    }

    public String toDns() {
        return id.replace('_', '-');
    }

    @Override
    public String toString() {
        return id;
    }

    @JsonValue
    public String id() { return id; }

    public String capitalizedType() {
        String simpleName = this.getClass().getSimpleName();
        String suffix = "Id";
        if (simpleName.endsWith(suffix)) {
            simpleName = simpleName.substring(0, simpleName.length() - suffix.length());
        }
        return simpleName;
    }

    public void validate() {
        if (id.equals("api")) {
            throwInvalidId(id, "'api' not allowed.");
        }
    }

    protected void validateSerialized() {
        if (!serializedPattern.matcher(id).matches()) {
            throwInvalidId(id, "Must match pattern " + serializedPattern);
        }
    }

    protected void validateSerializedIdentifier() {
        if (!serializedIdentifierPattern.matcher(id).matches()) {
            throwInvalidId(id, "Must match pattern " + serializedIdentifierPattern);
        }
    }

    protected void validateNoDefault() {
        if (id.equals("default")) {
            throwInvalidId(id, "'default' not allowed.");
        }
    }

    protected void validateNoUpperCase() {
        if (!id.equals(id.toLowerCase()))
            throwInvalidId(id, "Uppercase not allowed.");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Identifier identity = (Identifier) o;

        return id.equals(identity.id);

    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    public static void throwInvalidId(String id, String explanation) {
        throw new IllegalArgumentException(String.format("Invalid id '%s'. %s", id, explanation));
    }

}