aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/vcmr/VcmrReport.java
blob: 89cf6f4b28dc8612932fceacd60a7c615499610d (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
// Copyright Yahoo. 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.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.Set;

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

/**
 *
 * Node repository report containing list of upcoming VCMRs impacting a node
 *
 * @author olaa
 */
@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(ChangeRequestSource source) {
        var vcmr = new Vcmr(source.id(), source.status().name(), source.plannedStartTime(), source.plannedEndTime(), source.category());
        if (vcmrs.contains(vcmr))
            return false;

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

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

    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, String> toNodeReports() {
        Map<String, String> reports = new HashMap<>();
        String json = vcmrs.isEmpty() ?
                null : uncheck(() -> objectMapper.valueToTree(this).toString());
        reports.put(REPORT_ID, json);
        return reports;
    }

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

    public record Vcmr (@JsonProperty("id") String id,
                        @JsonProperty("status") String status,
                        @JsonProperty("plannedStartTime") ZonedDateTime plannedStartTime,
                        @JsonProperty("plannedEndTime") ZonedDateTime plannedEndTime,
                        @JsonProperty("category") String category) {}

}