Search in sources :

Example 1 with UnitState

use of com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState in project vespa by vespa-engine.

the class SetNodeStateRequest method getRequestedNodeState.

static NodeState getRequestedNodeState(Map<String, UnitState> newStates, Node n) throws StateRestApiException {
    UnitState newState = newStates.get("user");
    if (newState == null)
        throw new InvalidContentException("No new user state given in request");
    State state;
    switch(newState.getId().toLowerCase()) {
        case "up":
            state = State.UP;
            break;
        case "retired":
            state = State.RETIRED;
            break;
        case "maintenance":
            state = State.MAINTENANCE;
            break;
        case "down":
            state = State.DOWN;
            break;
        default:
            throw new InvalidContentException("Invalid user state '" + newState.getId() + "' given.");
    }
    return new NodeState(n.getType(), state).setDescription(newState.getReason());
}
Also used : InvalidContentException(com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException) NodeState(com.yahoo.vdslib.state.NodeState) UnitState(com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState) ClusterState(com.yahoo.vdslib.state.ClusterState) NodeState(com.yahoo.vdslib.state.NodeState) State(com.yahoo.vdslib.state.State) UnitState(com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState)

Example 2 with UnitState

use of com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState in project vespa by vespa-engine.

the class JsonReader method getStateRequestData.

public SetRequestData getStateRequestData(HttpRequest request) throws Exception {
    JSONObject json = new JSONObject(request.getPostContent().toString());
    final SetUnitStateRequest.Condition condition;
    if (json.has("condition")) {
        condition = SetUnitStateRequest.Condition.fromString(json.getString("condition"));
    } else {
        condition = SetUnitStateRequest.Condition.FORCE;
    }
    final SetUnitStateRequest.ResponseWait responseWait = json.has("response-wait") ? SetUnitStateRequest.ResponseWait.fromString(json.getString("response-wait")) : SetUnitStateRequest.ResponseWait.WAIT_UNTIL_CLUSTER_ACKED;
    Map<String, UnitState> stateMap = new HashMap<>();
    if (!json.has("state")) {
        throw new InvalidContentException("Set state requests must contain a state object");
    }
    Object o = json.get("state");
    if (!(o instanceof JSONObject)) {
        throw new InvalidContentException("value of state is not a json object");
    }
    JSONObject state = (JSONObject) o;
    JSONArray stateTypes = state.names();
    for (int i = 0; i < stateTypes.length(); ++i) {
        o = stateTypes.get(i);
        String type = (String) o;
        o = state.get(type);
        if (!(o instanceof JSONObject)) {
            throw new InvalidContentException("value of state->" + type + " is not a json object");
        }
        JSONObject userState = (JSONObject) o;
        String code = "up";
        if (userState.has("state")) {
            o = userState.get("state");
            if (!(o instanceof String)) {
                throw new InvalidContentException("value of state->" + type + "->state is not a string");
            }
            code = o.toString();
        }
        String reason = "";
        if (userState.has("reason")) {
            o = userState.get("reason");
            if (!(o instanceof String)) {
                throw new InvalidContentException("value of state->" + type + "->reason is not a string");
            }
            reason = o.toString();
        }
        stateMap.put(type, new UnitStateImpl(code, reason));
    }
    return new SetRequestData(stateMap, condition, responseWait);
}
Also used : HashMap(java.util.HashMap) JSONArray(org.codehaus.jettison.json.JSONArray) UnitState(com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState) InvalidContentException(com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException) JSONObject(org.codehaus.jettison.json.JSONObject) JSONObject(org.codehaus.jettison.json.JSONObject) SetUnitStateRequest(com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest)

Aggregations

InvalidContentException (com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException)2 UnitState (com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState)2 ClusterState (com.yahoo.vdslib.state.ClusterState)1 NodeState (com.yahoo.vdslib.state.NodeState)1 State (com.yahoo.vdslib.state.State)1 SetUnitStateRequest (com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest)1 HashMap (java.util.HashMap)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 JSONObject (org.codehaus.jettison.json.JSONObject)1