// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.staterestapi.requests;

import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState;

import java.util.Map;

public interface SetUnitStateRequest extends UnitRequest {

    Map<String, UnitState> getNewState();

    enum Condition {
        FORCE(1), // Don't check for any condition before setting unit state
        SAFE(2); // Only set condition if it is deemed safe (e.g. redundancy is still ok during upgrade)

        public final int value;

        Condition(int value) {
            this.value = value;
        }

        public static Condition fromString(String value) throws InvalidContentException {
            try {
                return Condition.valueOf(value.toUpperCase());
            } catch (IllegalArgumentException e) {
                throw new InvalidContentException(String.format("Invalid value for condition: '%s', expected one of 'force', 'safe'", value));
            }
        }
    }
    Condition getCondition();

    enum ResponseWait {
        WAIT_UNTIL_CLUSTER_ACKED("wait-until-cluster-acked"), // Wait for state change to be ACKed by cluster
        NO_WAIT("no-wait"); // Return without waiting for state change to be ACKed by cluster

        private final String name;

        ResponseWait(String name) { this.name = name; }

        public String getName() { return this.name; }

        @Override
        public String toString() { return name; }

        public static ResponseWait fromString(String value) throws InvalidContentException {
            if (value.equalsIgnoreCase(WAIT_UNTIL_CLUSTER_ACKED.name)) {
                return WAIT_UNTIL_CLUSTER_ACKED;
            } else if (value.equalsIgnoreCase(NO_WAIT.name)) {
                return NO_WAIT;
            }
            throw new InvalidContentException(String.format("Invalid value for response-wait: '%s', expected one of '%s', '%s'",
                    value, WAIT_UNTIL_CLUSTER_ACKED.name, NO_WAIT.name));
        }
    }
    ResponseWait getResponseWait();
}

@@ -1256,140 +1256,231 @@
     
-WAIT_UNTIL_CLUSTER_ACKED(%22wait-until-cluster-acked%22), // Wait for state change to be ACKed by cluster%0A        NO_WAIT(%22no-wait%22); //
+/**%0A         * Wait for state change to be ACKed by cluster. Default unless request%0A         * explicitly specifies otherwise.%0A         */%0A        WAIT_UNTIL_CLUSTER_ACKED(%22wait-until-cluster-acked%22),%0A        /**%0A         *
  Ret
@@ -1538,16 +1538,57 @@
  cluster
+.%0A         */%0A        NO_WAIT(%22no-wait%22);
 %0A%0A      

Add comment that cluster state ACK waiting is the default