+ return (new Yaml(options)).dump(createMap(fulfillPoliciesTreeField(
+ 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
+ */
+ public static void preloadConfiguration(JsonObject configurationsJson, Loop loop) {
+ if (configurationsJson != null && configurationsJson.entrySet().isEmpty()) {
+ JsonObject controlLoopName = new JsonObject();
+ controlLoopName.addProperty("controlLoopName",
+ loop != null ? loop.getName() : "Empty (NO loop loaded yet)");
+ JsonObject controlLoop = new JsonObject();
+ controlLoop.add("controlLoop", controlLoopName);
+ 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<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));
+ } else {
+ policy.getAsJsonObject().addProperty(PAYLOAD, "");
+ }
+ }
+
+ private static String getRecipe(JsonObject actor) {
+ return actor.getAsJsonObject().get("type").getAsString();