diff options
author | jonmv <venstad@gmail.com> | 2022-10-21 16:55:26 +0200 |
---|---|---|
committer | jonmv <venstad@gmail.com> | 2022-10-21 16:55:26 +0200 |
commit | 49bc9f5bcc05852954833f1c3c74dfa54e382fcb (patch) | |
tree | a64a32f730c2ddf9c010d3eba74859666ac6adf5 /clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi | |
parent | 064490e91198ebb77102e3d6e3d649596c843c4c (diff) |
Replace jettison with jackson
Diffstat (limited to 'clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi')
3 files changed, 73 insertions, 103 deletions
diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/response/UnitMetrics.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/response/UnitMetrics.java index f0470a1b845..71e57fef6aa 100644 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/response/UnitMetrics.java +++ b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/response/UnitMetrics.java @@ -5,6 +5,6 @@ import java.util.Map; public interface UnitMetrics { - Map<String, Number> getMetricMap(); + Map<String, Long> getMetricMap(); } diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/server/JsonReader.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/server/JsonReader.java index 44c45a2e8da..26ee121af79 100644 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/server/JsonReader.java +++ b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/server/JsonReader.java @@ -1,18 +1,24 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.clustercontroller.utils.staterestapi.server; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest; import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException; import com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest; import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState; -import org.codehaus.jettison.json.JSONArray; -import org.codehaus.jettison.json.JSONObject; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; public class JsonReader { + + private static final ObjectMapper mapper = new ObjectMapper(); + private static class UnitStateImpl implements UnitState { + private final String id; private final String reason; @@ -30,6 +36,7 @@ public class JsonReader { public String getReason() { return reason; } + } static class SetRequestData { @@ -50,60 +57,56 @@ public class JsonReader { } public SetRequestData getStateRequestData(HttpRequest request) throws Exception { - JSONObject json = new JSONObject(request.getPostContent().toString()); + JsonNode json = mapper.readTree(request.getPostContent().toString()); - final boolean probe = json.has("probe") && json.getBoolean("probe"); + final boolean probe = json.has("probe") && json.get("probe").booleanValue(); final SetUnitStateRequest.Condition condition; if (json.has("condition")) { - condition = SetUnitStateRequest.Condition.fromString(json.getString("condition")); + condition = SetUnitStateRequest.Condition.fromString(json.get("condition").textValue()); } else { condition = SetUnitStateRequest.Condition.FORCE; } final SetUnitStateRequest.ResponseWait responseWait = json.has("response-wait") - ? SetUnitStateRequest.ResponseWait.fromString(json.getString("response-wait")) + ? SetUnitStateRequest.ResponseWait.fromString(json.get("response-wait").textValue()) : 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)) { + JsonNode o = json.get("state"); + if ( ! (o instanceof ObjectNode state)) { 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)) { + for (Iterator<Map.Entry<String, JsonNode>> fields = state.fields(); fields.hasNext(); ) { + Map.Entry<String, JsonNode> entry = fields.next(); + String type = entry.getKey(); + if ( ! (entry.getValue() instanceof ObjectNode userState)) { 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)) { + if ( ! o.isTextual()) { throw new InvalidContentException("value of state->" + type + "->state is not a string"); } - code = o.toString(); + code = o.textValue(); } String reason = ""; if (userState.has("reason")) { o = userState.get("reason"); - if (!(o instanceof String)) { + if ( ! o.isTextual()) { throw new InvalidContentException("value of state->" + type + "->reason is not a string"); } - reason = o.toString(); + reason = o.textValue(); } stateMap.put(type, new UnitStateImpl(code, reason)); } return new SetRequestData(probe, stateMap, condition, responseWait); } + } diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/server/JsonWriter.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/server/JsonWriter.java index a6cde601ef4..af776ddf5cd 100644 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/server/JsonWriter.java +++ b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/staterestapi/server/JsonWriter.java @@ -1,19 +1,29 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.clustercontroller.utils.staterestapi.server; -import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.*; -import org.codehaus.jettison.json.JSONArray; -import org.codehaus.jettison.json.JSONException; -import org.codehaus.jettison.json.JSONObject; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.CurrentUnitState; +import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.DistributionState; +import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.DistributionStates; +import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.SetResponse; +import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.SubUnitList; +import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitAttributes; +import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitMetrics; +import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitResponse; +import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState; import java.util.Map; public class JsonWriter { + private static final ObjectMapper mapper = new ObjectMapper(); + private String pathPrefix = "/"; - public JsonWriter() { - } + public JsonWriter() { } public void setDefaultPathPrefix(String defaultPathPrefix) { if (defaultPathPrefix.isEmpty() || defaultPathPrefix.charAt(0) != '/') { @@ -22,16 +32,16 @@ public class JsonWriter { this.pathPrefix = defaultPathPrefix; } - public JSONObject createJson(UnitResponse data) throws Exception { - JSONObject json = new JSONObject(); + public JsonNode createJson(UnitResponse data) { + ObjectNode json = new ObjectNode(mapper.getNodeFactory()); fillInJson(data, json); return json; } - public void fillInJson(UnitResponse data, JSONObject json) throws Exception { + public void fillInJson(UnitResponse data, ObjectNode json) { UnitAttributes attributes = data.getAttributes(); if (attributes != null) { - fillInJson(attributes, json); + json.putPOJO("attributes", attributes.getAttributeValues()); } CurrentUnitState stateData = data.getCurrentState(); if (stateData != null) { @@ -39,7 +49,7 @@ public class JsonWriter { } UnitMetrics metrics = data.getMetrics(); if (metrics != null) { - fillInJson(metrics, json); + json.putPOJO("metrics", metrics.getMetricMap()); } Map<String, SubUnitList> subUnits = data.getSubUnits(); if (subUnits != null) { @@ -51,88 +61,45 @@ public class JsonWriter { } } - public void fillInJson(CurrentUnitState stateData, JSONObject json) throws Exception { - JSONObject stateJson = new JSONObject(); - json.put("state", stateJson); + public void fillInJson(CurrentUnitState stateData, ObjectNode json) { + ObjectNode stateJson = json.putObject("state"); Map<String, UnitState> state = stateData.getStatePerType(); - for (Map.Entry<String, UnitState> e : state.entrySet()) { - String stateType = e.getKey(); - UnitState unitState = e.getValue(); - JSONObject stateTypeJson = new JSONObject() - .put("state", unitState.getId()) - .put("reason", unitState.getReason()); - stateJson.put(stateType, stateTypeJson); - } + state.forEach((stateType, unitState) -> stateJson.putObject(stateType) + .put("state", unitState.getId()) + .put("reason", unitState.getReason())); } - public void fillInJson(UnitMetrics metrics, JSONObject json) throws Exception { - JSONObject metricsJson = new JSONObject(); - for (Map.Entry<String, Number> e : metrics.getMetricMap().entrySet()) { - metricsJson.put(e.getKey(), e.getValue()); - } - json.put("metrics", metricsJson); - } - public void fillInJson(UnitAttributes attributes, JSONObject json) throws Exception { - JSONObject attributesJson = new JSONObject(); - for (Map.Entry<String, String> e : attributes.getAttributeValues().entrySet()) { - attributesJson.put(e.getKey(), e.getValue()); - } - json.put("attributes", attributesJson); + public void fillInJson(Map<String, SubUnitList> subUnitMap, ObjectNode json) { + subUnitMap.forEach((subUnitType, units) -> { + ObjectNode typeJson = json.putObject(subUnitType); + units.getSubUnitLinks().forEach((key, value) -> typeJson.putObject(key).put("link", pathPrefix + "/" + value)); + units.getSubUnits().forEach((key, value) -> fillInJson(value, typeJson.putObject(key))); + }); } - public void fillInJson(Map<String, SubUnitList> subUnitMap, JSONObject json) throws Exception { - for(Map.Entry<String, SubUnitList> e : subUnitMap.entrySet()) { - String subUnitType = e.getKey(); - JSONObject typeJson = new JSONObject(); - for (Map.Entry<String, String> f : e.getValue().getSubUnitLinks().entrySet()) { - JSONObject linkJson = new JSONObject(); - linkJson.put("link", pathPrefix + "/" + f.getValue()); - typeJson.put(f.getKey(), linkJson); - } - for (Map.Entry<String, UnitResponse> f : e.getValue().getSubUnits().entrySet()) { - JSONObject subJson = new JSONObject(); - fillInJson(f.getValue(), subJson); - typeJson.put(f.getKey(), subJson); - } - json.put(subUnitType, typeJson); - } - } - - private static void fillInJson(DistributionStates states, JSONObject json) throws Exception { - JSONObject statesJson = new JSONObject(); - statesJson.put("published", distributionStateToJson(states.getPublishedState())); - json.put("distribution-states", statesJson); + private static void fillInJson(DistributionStates states, ObjectNode json) { + fillDistributionState(states.getPublishedState(), + json.putObject("distribution-states") + .putObject("published")); } - private static JSONObject distributionStateToJson(DistributionState state) throws Exception { - JSONObject result = new JSONObject(); + private static void fillDistributionState(DistributionState state, ObjectNode result) { result.put("baseline", state.getBaselineState()); - JSONArray bucketSpacesJson = new JSONArray(); - for (Map.Entry<String, String> entry : state.getBucketSpaceStates().entrySet()) { - JSONObject bucketSpaceJson = new JSONObject(); - bucketSpaceJson.put("name", entry.getKey()); - bucketSpaceJson.put("state", entry.getValue()); - bucketSpacesJson.put(bucketSpaceJson); - } - result.put("bucket-spaces", bucketSpacesJson); - return result; + ArrayNode bucketSpacesJson = result.putArray("bucket-spaces"); + state.getBucketSpaceStates().forEach((key, value) -> { + ObjectNode bucketSpaceJson = bucketSpacesJson.addObject(); + bucketSpaceJson.put("name", key); + bucketSpaceJson.put("state", value); + }); } - public JSONObject createErrorJson(String description) { - JSONObject o = new JSONObject(); - try{ - o.put("message", description); - } catch (JSONException e) { - // Can't really do anything if we get an error trying to report an error. - } - return o; + public JsonNode createErrorJson(String description) { + return new ObjectNode(mapper.getNodeFactory()).put("message", description); } - public JSONObject createJson(SetResponse setResponse) throws JSONException { - JSONObject jsonObject = new JSONObject(); - jsonObject.put("wasModified", setResponse.getWasModified()); - jsonObject.put("reason", setResponse.getReason()); - return jsonObject; + public JsonNode createJson(SetResponse setResponse) { + return new ObjectNode(mapper.getNodeFactory()).put("wasModified", setResponse.getWasModified()) + .put("reason", setResponse.getReason()); } } |