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

import java.time.Instant;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

/**
 * @author hakonhall
 */
public class FlagDefinition {
    private final UnboundFlag<?, ?, ?> unboundFlag;
    private final List<String> owners;
    private final Instant createdAt;
    private final Instant expiresAt;
    private final String description;
    private final String modificationEffect;
    private final List<FetchVector.Dimension> dimensions;

    public FlagDefinition(
            UnboundFlag<?, ?, ?> unboundFlag,
            List<String> owners,
            Instant createdAt,
            Instant expiresAt,
            String description,
            String modificationEffect,
            FetchVector.Dimension... dimensions) {
        this.unboundFlag = unboundFlag;
        this.owners = owners;
        this.createdAt = createdAt;
        this.expiresAt = expiresAt;
        this.description = description;
        this.modificationEffect = modificationEffect;
        this.dimensions = List.of(dimensions);
        validate(owners, createdAt, expiresAt, this.dimensions);
    }

    public UnboundFlag<?, ?, ?> getUnboundFlag() {
        return unboundFlag;
    }

    public List<FetchVector.Dimension> getDimensions() {
        return dimensions;
    }

    public String getDescription() {
        return description;
    }

    public String getModificationEffect() {
        return modificationEffect;
    }

    public List<String> getOwners() { return owners; }

    public Instant getCreatedAt() { return createdAt; }

    public Instant getExpiresAt() { return expiresAt; }

    private static void validate(List<String> owners, Instant createdAt, Instant expiresAt, List<FetchVector.Dimension> dimensions) {
        if (expiresAt.isBefore(createdAt)) {
            throw new IllegalArgumentException(
                    String.format(
                            "Flag cannot expire before its creation date (createdAt='%s', expiresAt='%s')",
                            createdAt, expiresAt));
        }

        if (owners == PermanentFlags.OWNERS) {
            if (!createdAt.equals(PermanentFlags.CREATED_AT) || !expiresAt.equals(PermanentFlags.EXPIRES_AT)) {
                throw new IllegalArgumentException("Invalid creation or expiration date for permanent flag");
            }
        } else if (owners.isEmpty()) {
            throw new IllegalArgumentException("Owner(s) must be specified");
        }

        if (dimensions.contains(FetchVector.Dimension.CONSOLE_USER_EMAIL)) {
            Set<FetchVector.Dimension> disallowedCombinations = EnumSet.allOf(FetchVector.Dimension.class);
            disallowedCombinations.remove(FetchVector.Dimension.CONSOLE_USER_EMAIL);
            disallowedCombinations.remove(FetchVector.Dimension.INSTANCE_ID);
            disallowedCombinations.remove(FetchVector.Dimension.TENANT_ID);
            disallowedCombinations.retainAll(dimensions);
            if (!disallowedCombinations.isEmpty())
                throw new IllegalArgumentException("Dimension " + FetchVector.Dimension.CONSOLE_USER_EMAIL + " cannot be combined with " + disallowedCombinations);
        }
    }
}