summaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/main/java/com/yahoo/config/provision/zone/UpgradePolicy.java
blob: 85cc384660d29816a7f1b1a850fa12e5c380bedb (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision.zone;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * This class declares the steps (zones) to follow when upgrading a system. If a step contains multiple zones, those
 * will be upgraded in parallel.
 *
 * @author mpolden
 */
public class UpgradePolicy {

    private final List<Set<ZoneApi>> steps;

    private UpgradePolicy(List<Set<ZoneApi>> steps) {
        for (int i = 0; i < steps.size(); i++) {
            for (int j = 0; j < i; j++) {
                if (!Collections.disjoint(steps.get(i), steps.get(j))) {
                    throw new IllegalArgumentException("One or more zones are declared in multiple steps");
                }
            }
        }
        this.steps = List.copyOf(steps);
    }

    /** Returns the steps in this */
    public List<Set<ZoneApi>> steps() {
        return steps;
    }

    /** Returns a copy of this with the step order inverted */
    public UpgradePolicy inverted() {
        List<Set<ZoneApi>> copy = new ArrayList<>(steps);
        Collections.reverse(copy);
        return new UpgradePolicy(copy);
    }

    public static UpgradePolicy.Builder builder() {
        return new UpgradePolicy.Builder();
    }

    public static class Builder {

        private final List<Set<ZoneApi>> steps = new ArrayList<>();

        private Builder() {}

        /** Upgrade given zone as the next step */
        public Builder upgrade(ZoneApi zone) {
            return upgradeInParallel(zone);
        }

        /** Upgrade given zones in parallel as the next step */
        public Builder upgradeInParallel(ZoneApi... zone) {
            this.steps.add(Set.of(zone));
            return this;
        }

        public UpgradePolicy build() {
            return new UpgradePolicy(steps);
        }

    }

}