summaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/vcmr/VCMRReport.java
blob: 8ebfde9f47585d96e4a6de7e6a1a821f4b81ceb1 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.vcmr;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.Node;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static com.yahoo.yolean.Exceptions.uncheck;

/**
 * @author olaa
 *
 * Node repository report containing list of upcoming VCMRs impacting a node
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class VCMRReport {

    private static final String REPORT_ID = "vcmr";
    private static final ObjectMapper objectMapper = new ObjectMapper()
            .registerModule(new JavaTimeModule());

    @JsonProperty("upcoming")
    private Set<VCMR> vcmrs;

    public VCMRReport() {
        this(new HashSet<>());
    }

    public VCMRReport(Set<VCMR> vcmrs) {
        this.vcmrs = vcmrs;
    }

    public Set<VCMR> getVcmrs() {
        return vcmrs;
    }

    /**
     * @return true if list of VCMRs is changed
     */
    public boolean addVcmr(String id, ZonedDateTime plannedStartTime, ZonedDateTime plannedEndtime) {
        var vcmr = new VCMR(id, plannedStartTime, plannedEndtime);
        if (vcmrs.contains(vcmr))
            return false;

        // Remove to catch any changes in start/end time
        removeVcmr(id);
        return vcmrs.add(vcmr);
    }

    public boolean removeVcmr(String id) {
        return vcmrs.removeIf(vcmr -> id.equals(vcmr.getId()));
    }

    public static String getReportId() {
        return REPORT_ID;
    }

    /**
     * Serialization functions - mapped to {@link Node#reports()}
     */
    public static VCMRReport fromReports(Map<String, String> reports) {
        var serialized = reports.get(REPORT_ID);
        if (serialized == null)
            return new VCMRReport();

        return uncheck(() -> objectMapper.readValue(serialized, VCMRReport.class));
    }

    /**
     * Set report to 'null' if list is empty - clearing the report
     * See NodePatcher in node-repository
     */
    public Map<String, JsonNode> toNodeReports() {
        Map<String, JsonNode> reports = new HashMap<>();
        JsonNode jsonNode = vcmrs.isEmpty() ?
                null : uncheck(() -> objectMapper.valueToTree(this));
        reports.put(REPORT_ID, jsonNode);
        return reports;
    }

    @Override
    public String toString() {
        return "VCMRReport{" + vcmrs + "}";
    }

    public static class VCMR {

        private String id;
        private ZonedDateTime plannedStartTime;
        private ZonedDateTime plannedEndTime;

        VCMR(@JsonProperty("id") String id,
                          @JsonProperty("plannedStartTime") ZonedDateTime plannedStartTime,
                          @JsonProperty("plannedEndTime") ZonedDateTime plannedEndTime) {
            this.id = id;
            this.plannedStartTime = plannedStartTime;
            this.plannedEndTime = plannedEndTime;
        }

        public String getId() {
            return id;
        }

        public ZonedDateTime getPlannedStartTime() {
            return plannedStartTime;
        }

        public ZonedDateTime getPlannedEndTime() {
            return plannedEndTime;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            VCMR vcmr = (VCMR) o;
            return Objects.equals(id, vcmr.id) &&
                    Objects.equals(plannedStartTime, vcmr.plannedStartTime) &&
                    Objects.equals(plannedEndTime, vcmr.plannedEndTime);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, plannedStartTime, plannedEndTime);
        }

        @Override
        public String toString() {
            return "VCMR{" +
                    "id='" + id + '\'' +
                    ", plannedStartTime=" + plannedStartTime +
                    ", plannedEndTime=" + plannedEndTime +
                    '}';
        }
    }

}