diff options
author | Determe, Sebastien (sd378r) <sebastien.determe@intl.att.com> | 2019-05-28 11:54:27 +0200 |
---|---|---|
committer | Determe, Sebastien (sd378r) <sebastien.determe@intl.att.com> | 2019-05-28 11:54:27 +0200 |
commit | 785b17789adc4817e129fcd389d91046b195044a (patch) | |
tree | 5a7acac8b2f868215880b3b7fc77e6781293f30f /src/main/java | |
parent | ef9aed4b3a69128fa10feaa9ab787a57ef6772d7 (diff) |
Fix the op policy payload
Fix the operational policy payload as it was not the one expected by
policy engine + rollback staging plugin
Issue-ID: CLAMP-395
Change-Id: I7b690eb02f50617b7aa402266c13aafa82f5b59b
Signed-off-by: Determe, Sebastien (sd378r) <sebastien.determe@intl.att.com>
Diffstat (limited to 'src/main/java')
5 files changed, 193 insertions, 15 deletions
diff --git a/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java b/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java index de6e4dcdb..3dc807388 100644 --- a/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java +++ b/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java @@ -118,8 +118,7 @@ public class CamelConfiguration extends RouteBuilder { .apiContextPath("api-doc").apiVendorExtension(true).apiProperty("api.title", "Clamp Rest API") .apiProperty("api.version", ClampVersioning.getCldsVersionFromProps()) .apiProperty("base.path", "/restservices/clds/"); - // .apiProperty("cors", "true"); - camelContext.setTracing(true); + // camelContext.setTracing(true); configureDefaultSslProperties(); registerTrustStore(); diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index 897708604..6de2863ea 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -23,6 +23,8 @@ package org.onap.clamp.loop; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -47,6 +49,7 @@ import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.OrderBy; import javax.persistence.Table; +import javax.persistence.Transient; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; @@ -66,6 +69,9 @@ public class Loop implements Serializable { */ private static final long serialVersionUID = -286522707701388642L; + @Transient + private static final EELFLogger logger = EELFManager.getInstance().getLogger(Loop.class); + @Id @Expose @Column(nullable = false, name = "name", unique = true) @@ -279,7 +285,9 @@ public class Loop implements Serializable { jsonArray.add(policyNode); policyNode.addProperty("policy-id", policyName); } - return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject); + String payload = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject); + logger.info("PdpGroup policy payload: " + payload); + return payload; } /** diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java index 2bbb91183..d8d15a5b2 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -23,6 +23,8 @@ package org.onap.clamp.policy.microservice; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; @@ -40,6 +42,7 @@ import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; +import javax.persistence.Transient; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; @@ -61,6 +64,9 @@ public class MicroServicePolicy implements Serializable, Policy { */ private static final long serialVersionUID = 6271238288583332616L; + @Transient + private static final EELFLogger logger = EELFManager.getInstance().getLogger(MicroServicePolicy.class); + @Expose @Id @Column(nullable = false, name = "name", unique = true) @@ -271,7 +277,9 @@ public class MicroServicePolicy implements Serializable, Policy { JsonObject policyProperties = new JsonObject(); policyDetails.add("properties", policyProperties); policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getProperties()); - return new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult); + String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult); + logger.info("Micro service policy payload: " + policyPayload); + return policyPayload; } } diff --git a/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java new file mode 100644 index 000000000..33148f0b6 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java @@ -0,0 +1,150 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.policy.operational; + +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import org.apache.commons.lang3.math.NumberUtils; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.DumperOptions.ScalarStyle; +import org.yaml.snakeyaml.Yaml; + +/** + * + * This class contains the code required to support the sending of Legacy + * operational payload to policy engine. This will probably disappear in El + * Alto. + * + */ +public class LegacyOperationalPolicy { + + private LegacyOperationalPolicy() { + + } + + private static void translateStringValues(String jsonKey, String stringValue, JsonElement parentJsonElement) { + if (stringValue.equalsIgnoreCase("true") || stringValue.equalsIgnoreCase("false")) { + parentJsonElement.getAsJsonObject().addProperty(jsonKey, Boolean.valueOf(stringValue)); + + } else if (NumberUtils.isParsable(stringValue)) { + parentJsonElement.getAsJsonObject().addProperty(jsonKey, Long.parseLong(stringValue)); + } + } + + private static JsonElement removeAllQuotes(JsonElement jsonElement) { + if (jsonElement.isJsonArray()) { + for (JsonElement element : jsonElement.getAsJsonArray()) { + removeAllQuotes(element); + } + } else if (jsonElement.isJsonObject()) { + for (Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) { + if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isString()) { + translateStringValues(entry.getKey(), entry.getValue().getAsString(), jsonElement); + } else { + removeAllQuotes(entry.getValue()); + } + } + } + return jsonElement; + } + + public static JsonElement reworkPayloadAttributes(JsonElement policyJson) { + for (JsonElement policy : policyJson.getAsJsonObject().get("policies").getAsJsonArray()) { + JsonElement payloadElem = policy.getAsJsonObject().get("payload"); + String payloadString = payloadElem != null ? payloadElem.getAsString() : ""; + if (!payloadString.isEmpty()) { + Map<String, String> testMap = new Yaml().load(payloadString); + String json = new GsonBuilder().create().toJson(testMap); + policy.getAsJsonObject().add("payload", new GsonBuilder().create().fromJson(json, JsonElement.class)); + } + } + return policyJson; + } + + private static void replacePropertiesIfEmpty(JsonElement policy, String key, String valueIfEmpty) { + JsonElement payloadElem = policy.getAsJsonObject().get(key); + String payloadString = payloadElem != null ? payloadElem.getAsString() : ""; + if (payloadString.isEmpty()) { + policy.getAsJsonObject().addProperty(key, valueIfEmpty); + } + } + + private static JsonElement fulfillPoliciesTreeField(JsonElement policyJson) { + for (JsonElement policy : policyJson.getAsJsonObject().get("policies").getAsJsonArray()) { + replacePropertiesIfEmpty(policy, "success", "final_success"); + replacePropertiesIfEmpty(policy, "failure", "final_failure"); + replacePropertiesIfEmpty(policy, "failure_timeout", "final_failure_timeout"); + replacePropertiesIfEmpty(policy, "failure_retries", "final_failure_retries"); + replacePropertiesIfEmpty(policy, "failure_exception", "final_failure_exception"); + replacePropertiesIfEmpty(policy, "failure_guard", "final_failure_guard"); + } + return policyJson; + } + + private static Map<String, Object> createMap(JsonElement jsonElement) { + Map<String, Object> mapResult = new TreeMap<>(); + + if (jsonElement.isJsonObject()) { + for (Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) { + if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isString()) { + mapResult.put(entry.getKey(), entry.getValue().getAsString()); + } else if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isBoolean()) { + mapResult.put(entry.getKey(), entry.getValue().getAsBoolean()); + } else if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isNumber()) { + // Only int ro long normally, we don't need float here + mapResult.put(entry.getKey(), entry.getValue().getAsLong()); + } else if (entry.getValue().isJsonArray()) { + List<Map<String, Object>> newArray = new ArrayList<>(); + mapResult.put(entry.getKey(), newArray); + for (JsonElement element : entry.getValue().getAsJsonArray()) { + newArray.add(createMap(element)); + } + } else if (entry.getValue().isJsonObject()) { + mapResult.put(entry.getKey(), createMap(entry.getValue())); + } + } + } + return mapResult; + } + + public static String createPolicyPayloadYamlLegacy(JsonElement operationalPolicyJsonElement) { + JsonElement opPolicy = fulfillPoliciesTreeField( + removeAllQuotes(reworkPayloadAttributes(operationalPolicyJsonElement.getAsJsonObject().deepCopy()))); + Map<?, ?> jsonMap = createMap(opPolicy); + DumperOptions options = new DumperOptions(); + options.setDefaultScalarStyle(ScalarStyle.PLAIN); + options.setIndent(2); + options.setPrettyFlow(true); + // Policy can't support { } in the yaml + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + return (new Yaml(options)).dump(jsonMap); + } +} diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java index 2ce5afff9..62c5a1e9f 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java @@ -23,6 +23,8 @@ package org.onap.clamp.policy.operational; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; @@ -45,6 +47,7 @@ import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; +import javax.persistence.Transient; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; @@ -52,6 +55,7 @@ import org.hibernate.annotations.TypeDefs; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.Loop; import org.onap.clamp.policy.Policy; +import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @Entity @@ -63,6 +67,9 @@ public class OperationalPolicy implements Serializable, Policy { */ private static final long serialVersionUID = 6117076450841538255L; + @Transient + private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicy.class); + @Id @Expose @Column(nullable = false, name = "name", unique = true) @@ -176,28 +183,33 @@ public class OperationalPolicy implements Serializable, Policy { operationalPolicyDetails.add("metadata", metadata); metadata.addProperty("policy-id", this.name); - operationalPolicyDetails.add("properties", this.configurationsJson.get("operational_policy")); + operationalPolicyDetails.add("properties", LegacyOperationalPolicy + .reworkPayloadAttributes(this.configurationsJson.get("operational_policy").deepCopy())); Gson gson = new GsonBuilder().create(); + Map<?, ?> jsonMap = gson.fromJson(gson.toJson(policyPayloadResult), Map.class); - return (new Yaml()).dump(jsonMap); - } - public String createPolicyPayloadYamlLegacy() { - Gson gson = new GsonBuilder().create(); - Map<?, ?> jsonMap = gson.fromJson(gson.toJson(this.configurationsJson.get("operational_policy")), Map.class); - return (new Yaml()).dump(jsonMap); + DumperOptions options = new DumperOptions(); + options.setIndent(2); + options.setPrettyFlow(true); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + + return (new Yaml(options)).dump(jsonMap); } @Override public String createPolicyPayload() throws UnsupportedEncodingException { - // Now the Yaml payload must be injected in a json ... + // Now using the legacy payload fo Dublin JsonObject payload = new JsonObject(); payload.addProperty("policy-id", this.getName()); - payload.addProperty("content", - URLEncoder.encode(createPolicyPayloadYamlLegacy(), StandardCharsets.UTF_8.toString())); - return new GsonBuilder().setPrettyPrinting().create().toJson(payload); + payload.addProperty("content", URLEncoder.encode( + LegacyOperationalPolicy.createPolicyPayloadYamlLegacy(this.configurationsJson.get("operational_policy")), + StandardCharsets.UTF_8.toString())); + String opPayload = new GsonBuilder().setPrettyPrinting().create().toJson(payload); + logger.info("Operational policy payload: " + opPayload); + return opPayload; } /** @@ -217,6 +229,7 @@ public class OperationalPolicy implements Serializable, Policy { result.put(guardElem.getKey(), new GsonBuilder().create().toJson(guard)); } } + logger.info("Guard policy payload: " + result); return result; } |