All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.monitor.console.model.StatusInfo Maven / Gradle / Ivy

package org.zodiac.monitor.console.model;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.springframework.lang.Nullable;
import org.zodiac.sdk.toolkit.util.AssertUtil;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Instance status with details fetched from the info endpoint.
 *
 * @author Johannes Edmeier
 * @author zodiac
 */
public class StatusInfo implements Serializable {

    private static final long serialVersionUID = 2071706571470345118L;

    public static final String STATUS_UNKNOWN = "UNKNOWN";
    public static final String STATUS_OUT_OF_SERVICE = "OUT_OF_SERVICE";
    public static final String STATUS_UP = "UP";
    public static final String STATUS_DOWN = "DOWN";
    public static final String STATUS_OFFLINE = "OFFLINE";
    public static final String STATUS_RESTRICTED = "RESTRICTED";
    private static final List STATUS_ORDER =
        Arrays.asList(STATUS_DOWN, STATUS_OUT_OF_SERVICE, STATUS_OFFLINE, STATUS_UNKNOWN, STATUS_RESTRICTED, STATUS_UP);

    private final String status;
    private final Map details;

    private StatusInfo(String status, @Nullable Map details) {
        AssertUtil.notBlank(status, "'status' must not be empty.");
        this.status = status.toUpperCase();
        this.details = details != null ? CollUtil.map(details) : Collections.emptyMap();
    }

    public Map getDetails() {
        return Collections.unmodifiableMap(details);
    }

    public String getStatus() {
        return status;
    }

    @JsonIgnore
    public boolean isUp() {
        return STATUS_UP.equals(status);
    }

    @JsonIgnore
    public boolean isOffline() {
        return STATUS_OFFLINE.equals(status);
    }

    @JsonIgnore
    public boolean isDown() {
        return STATUS_DOWN.equals(status);
    }

    @JsonIgnore
    public boolean isUnknown() {
        return STATUS_UNKNOWN.equals(status);
    }

    public static StatusInfo valueOf(String statusCode, @Nullable Map details) {
        return new StatusInfo(statusCode, details);
    }

    public static StatusInfo valueOf(String statusCode) {
        return valueOf(statusCode, null);
    }

    public static StatusInfo ofUnknown() {
        return valueOf(STATUS_UNKNOWN, null);
    }

    public static StatusInfo ofUp() {
        return ofUp(null);
    }

    public static StatusInfo ofDown() {
        return ofDown(null);
    }

    public static StatusInfo ofOffline() {
        return ofOffline(null);
    }

    public static StatusInfo ofUp(@Nullable Map details) {
        return valueOf(STATUS_UP, details);
    }

    public static StatusInfo ofDown(@Nullable Map details) {
        return valueOf(STATUS_DOWN, details);
    }

    public static StatusInfo ofOffline(@Nullable Map details) {
        return valueOf(STATUS_OFFLINE, details);
    }

    public static Comparator severity() {
        return Comparator.comparingInt(STATUS_ORDER::indexOf);
    }

    @SuppressWarnings("unchecked")
    public static StatusInfo from(Map body) {
        return StatusInfo.valueOf((String)(body).get("status"), (Map)body.get("details"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy