// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.versions;

import com.yahoo.component.Version;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.zone.ZoneId;

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

/**
 * Version information for a node allocated to a {@link com.yahoo.vespa.hosted.controller.application.SystemApplication}.
 *
 * This is immutable.
 *
 * @author mpolden
 */
public class NodeVersion {

    private final HostName hostname;
    private final ZoneId zone;
    private final Version currentVersion;
    private final Version wantedVersion;
    private final Optional<Instant> suspendedAt;

    public NodeVersion(HostName hostname, ZoneId zone, Version currentVersion, Version wantedVersion,
                       Optional<Instant> suspendedAt) {
        this.hostname = Objects.requireNonNull(hostname, "hostname must be non-null");
        this.zone = Objects.requireNonNull(zone, "zone must be non-null");
        this.currentVersion = Objects.requireNonNull(currentVersion, "version must be non-null");
        this.wantedVersion = Objects.requireNonNull(wantedVersion, "wantedVersion must be non-null");
        this.suspendedAt = Objects.requireNonNull(suspendedAt, "suspendedAt must be non-null");
    }

    /** Hostname of this */
    public HostName hostname() {
        return hostname;
    }

    /** Zone of this */
    public ZoneId zone() {
        return zone;
    }

    /** Current version of this */
    public Version currentVersion() {
        return currentVersion;
    }

    /** Wanted version of this */
    public Version wantedVersion() {
        return wantedVersion;
    }

    /** Returns the duration of the change in this, measured relative to instant */
    public Duration changeDuration(Instant instant) {
        if (!changing()) return Duration.ZERO;
        if (suspendedAt.isEmpty()) return Duration.ZERO; // Node hasn't suspended to apply the change yet
        return Duration.between(suspendedAt.get(), instant).abs();
    }

    /** The most recent time the node referenced by this suspended. This is empty if the node is not suspended. */
    public Optional<Instant> suspendedAt() {
        return suspendedAt;
    }

    /** Returns a copy of this with current version set to given version */
    public NodeVersion withCurrentVersion(Version version) {
        if (currentVersion.equals(version)) return this;
        return new NodeVersion(hostname, zone, version, wantedVersion, suspendedAt);
    }

    /** Returns a copy of this with wanted version set to given version */
    public NodeVersion withWantedVersion(Version version) {
        if (wantedVersion.equals(version)) return this;
        return new NodeVersion(hostname, zone, currentVersion, version, suspendedAt);
    }

    /** Returns a copy of this with wanted version set to given version */
    public NodeVersion withSuspendedAt(Optional<Instant> suspendedAt) {
        if (suspendedAt.equals(this.suspendedAt)) return this;
        return new NodeVersion(hostname, zone, currentVersion, wantedVersion, suspendedAt);
    }

    @Override
    public String toString() {
        return hostname + ": " + currentVersion + " -> " + wantedVersion + " [zone=" + zone + ", suspendedAt=" + suspendedAt.map(Instant::toString).orElse("<not suspended>") + "]";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NodeVersion that = (NodeVersion) o;
        return hostname.equals(that.hostname) &&
               zone.equals(that.zone) &&
               currentVersion.equals(that.currentVersion) &&
               wantedVersion.equals(that.wantedVersion) &&
               suspendedAt.equals(that.suspendedAt);
    }

    @Override
    public int hashCode() {
        return Objects.hash(hostname, zone, currentVersion, wantedVersion, suspendedAt);
    }

    /** Returns whether this is changing (upgrading or downgrading) */
    private boolean changing() {
        return !currentVersion.equals(wantedVersion);
    }

}

@@ -2378,887 +2378,8 @@
  %7D%0A%0A
-    /** Returns a copy of this with current version set to given version */%0A    public NodeVersion withCurrentVersion(Version version) %7B%0A        if (currentVersion.equals(version)) return this;%0A        return new NodeVersion(hostname, zone, version, wantedVersion, suspendedAt);%0A    %7D%0A%0A    /** Returns a copy of this with wanted version set to given version */%0A    public NodeVersion withWantedVersion(Version version) %7B%0A        if (wantedVersion.equals(version)) return this;%0A        return new NodeVersion(hostname, zone, currentVersion, version, suspendedAt);%0A    %7D%0A%0A    /** Returns a copy of this with wanted version set to given version */%0A    public NodeVersion withSuspendedAt(Optional%3CInstant%3E suspendedAt) %7B%0A        if (suspendedAt.equals(this.suspendedAt)) return this;%0A        return new NodeVersion(hostname, zone, currentVersion, wantedVersion, suspendedAt);%0A    %7D%0A%0A
     

Remove unused methods