// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.lb;

import com.yahoo.vespa.hosted.provision.maintenance.LoadBalancerExpirer;

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

/**
 * Represents a load balancer for an application's cluster. This is immutable.
 *
 * @author mpolden
 */
public class LoadBalancer {

    private final LoadBalancerId id;
    private final LoadBalancerInstance instance;
    private final State state;
    private final Instant changedAt;

    public LoadBalancer(LoadBalancerId id, LoadBalancerInstance instance, State state, Instant changedAt) {
        this.id = Objects.requireNonNull(id, "id must be non-null");
        this.instance = Objects.requireNonNull(instance, "instance must be non-null");
        this.state = Objects.requireNonNull(state, "state must be non-null");
        this.changedAt = Objects.requireNonNull(changedAt, "changedAt must be non-null");
    }

    /** An identifier for this load balancer. The ID is unique inside the zone */
    public LoadBalancerId id() {
        return id;
    }

    /** The instance associated with this */
    public LoadBalancerInstance instance() {
        return instance;
    }

    /** The current state of this */
    public State state() {
        return state;
    }

    /** Returns when this was last changed */
    public Instant changedAt() {
        return changedAt;
    }

    /** Returns a copy of this with state set to given state */
    public LoadBalancer with(State state, Instant changedAt) {
        if (this.state != State.reserved && state == State.reserved) {
            throw new IllegalArgumentException("Invalid state transition: " + this.state + " -> " + state);
        }
        return new LoadBalancer(id, instance, state, changedAt);
    }

    /** Returns a copy of this with instance set to given instance */
    public LoadBalancer with(LoadBalancerInstance instance) {
        return new LoadBalancer(id, instance, state, changedAt);
    }

    public enum State {

        /** This load balancer has been provisioned and reserved for an application */
        reserved,

        /**
         * The load balancer has been deactivated and is ready to be removed. Inactive load balancers are eventually
         * removed by {@link LoadBalancerExpirer}. Inactive load balancers may be reactivated if a deleted cluster is
         * redeployed.
         */
        inactive,

        /** The load balancer is in active use by an application */
        active,

    }

}

@@ -1604,32 +1604,278 @@
 nt changedAt) %7B%0A
+        if (changedAt.isBefore(this.changedAt)) %7B%0A            throw new IllegalArgumentException(%22Invalid changeAt: '%22 + changedAt + %22' is before existing value '%22 +%0A                                               this.changedAt + %22'%22);%0A        %7D%0A
         if (this

Revert "Do not require changeAt to be after previous value"