X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fpolicy%2Foperational%2FLegacyOperationalPolicy.java;h=033f2cebb3344bd4df0c447978b370409afdc6ae;hb=3121b5bee2e70b1f753f9988344d41f3132edf69;hp=ff7777f7f2f4b2fb3203db154cff6ef81ee0a3b4;hpb=f0e00e7c9ea8dd15a4aacdd880aa648539caf087;p=clamp.git diff --git a/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java index ff7777f7..033f2ceb 100644 --- a/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java +++ b/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights * reserved. + * Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +47,11 @@ import org.yaml.snakeyaml.Yaml; */ public class LegacyOperationalPolicy { + private static final String ACTOR = "actor"; + private static final String RECIPE = "recipe"; + private static final String POLICIES = "policies"; + private static final String PAYLOAD = "payload"; + private LegacyOperationalPolicy() { } @@ -79,7 +85,7 @@ public class LegacyOperationalPolicy { /** * This method rework the payload attribute (yaml) that is normally wrapped in a * string when coming from the UI. - * + * * @param policyJson The operational policy json config * @return The same object reference but modified */ @@ -150,7 +156,7 @@ public class LegacyOperationalPolicy { /** * This method transforms the configuration json to a Yaml format. - * + * * @param operationalPolicyJsonElement The operational policy json config * @return The Yaml as string */ @@ -162,13 +168,13 @@ public class LegacyOperationalPolicy { // Policy can't support { } in the yaml options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); return (new Yaml(options)).dump(createMap(fulfillPoliciesTreeField( - removeAllQuotes(reworkPayloadAttributes(operationalPolicyJsonElement.getAsJsonObject().deepCopy()))))); + removeAllQuotes(reworkActorAttributes(operationalPolicyJsonElement.getAsJsonObject().deepCopy()))))); } /** * This method load mandatory field in the operational policy configuration * JSON. - * + * * @param configurationsJson The operational policy JSON * @param loop The parent loop object */ @@ -182,4 +188,45 @@ public class LegacyOperationalPolicy { configurationsJson.add("operational_policy", controlLoop); } } + + /** + * This method rework on the actor/recipe and payload attribute. + * + * @param policyJson The operational policy json config + * @return The same object reference but modified + */ + public static JsonElement reworkActorAttributes(JsonElement policyJson) { + for (JsonElement policy : policyJson.getAsJsonObject().get(POLICIES).getAsJsonArray()) { + JsonObject actor = policy.getAsJsonObject().get(ACTOR).getAsJsonObject(); + policy.getAsJsonObject().remove(ACTOR); + String actorStr = actor.getAsJsonObject().get(ACTOR).getAsString(); + policy.getAsJsonObject().addProperty(ACTOR, actorStr); + policy.getAsJsonObject().addProperty(RECIPE, getRecipe(actor)); + + if ("CDS".equalsIgnoreCase(actorStr)) { + addPayloadAttributes(actor.getAsJsonObject(ACTOR).getAsJsonObject(RECIPE), policy); + } else { + addPayloadAttributes(actor, policy); + } + } + return policyJson; + } + + private static void addPayloadAttributes(JsonObject jsonObject, + JsonElement policy) { + JsonElement payloadElem = jsonObject.getAsJsonObject().get(PAYLOAD); + String payloadString = payloadElem != null ? payloadElem.getAsString() : ""; + if (!payloadString.isEmpty()) { + Map testMap = new Yaml().load(payloadString); + String json = new GsonBuilder().create().toJson(testMap); + policy.getAsJsonObject().add(PAYLOAD, + new GsonBuilder().create().fromJson(json, JsonElement.class)); + } else { + policy.getAsJsonObject().addProperty(PAYLOAD, ""); + } + } + + private static String getRecipe(JsonObject actor) { + return actor.getAsJsonObject().get("type").getAsString(); + } }