aboutsummaryrefslogtreecommitdiffstats
path: root/application-model/src/main/java/com/yahoo/vespa/applicationmodel/ServiceStatusInfo.java
blob: c83e371f0f71c7ba8b673db0e64eb1b5e00465bb (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.applicationmodel;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.Instant;
import java.util.Objects;
import java.util.Optional;

/**
 * @author hakonhall
 */
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ServiceStatusInfo {
    private final ServiceStatus status;
    private final Optional<Instant> since;
    private final Optional<Instant> lastChecked;
    private final Optional<String> error;
    private final Optional<String> endpoint;

    public ServiceStatusInfo(ServiceStatus status) {
        this(status, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    }

    public ServiceStatusInfo(ServiceStatus status, Instant since, Instant lastChecked, Optional<String> error,
                             Optional<String> endpoint) {
        this(status, Optional.of(since), Optional.of(lastChecked), error, endpoint);
    }

    public ServiceStatusInfo(ServiceStatus status, Optional<Instant> since, Optional<Instant> lastChecked,
                             Optional<String> error, Optional<String> endpoint) {
        this.status = status;
        this.since = since;
        this.lastChecked = lastChecked;
        this.error = error;
        this.endpoint = endpoint;
    }

    @JsonCreator
    public ServiceStatusInfo(@JsonProperty("serviceStatus") ServiceStatus status,
                             @JsonProperty("since") Instant since,
                             @JsonProperty("lastChecked") Instant lastChecked,
                             @JsonProperty("error") String error,
                             @JsonProperty("endpoint") String endpoint) {
        this(status, Optional.ofNullable(since), Optional.ofNullable(lastChecked), Optional.ofNullable(error), Optional.ofNullable(endpoint));
    }

    @JsonProperty("endpoint")
    public String endpointOrNull() {
        return endpoint.orElse(null);
    }

    @JsonProperty("serviceStatus")
    public ServiceStatus serviceStatus() {
        return status;
    }

    /** The current service status was first seen at this time, and has since stayed constant. */
    public Optional<Instant> since() {
        return since;
    }

    @JsonProperty("since")
    public Instant sinceOrNull() {
        return since.orElse(null);
    }

    /** The last time the status was checked. */
    public Optional<Instant> lastChecked() {
        return lastChecked;
    }

    @JsonProperty("lastChecked")
    public Instant lastCheckedOrNull() {
        return lastChecked.orElse(null);
    }

    @JsonProperty("error")
    public String errorOrNull() {
        return error.orElse(null);
    }

    @Override
    public String toString() {
        return "ServiceStatusInfo{" +
                "status=" + status +
                ", since=" + since +
                ", lastChecked=" + lastChecked +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ServiceStatusInfo that = (ServiceStatusInfo) o;
        return status == that.status &&
                Objects.equals(since, that.since) &&
                Objects.equals(lastChecked, that.lastChecked);
    }

    @Override
    public int hashCode() {
        return Objects.hash(status, since, lastChecked);
    }
}