X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2FPolicyEngineServices.java;h=c75d733a89f2549ac299ab89de076a738eb2a4a3;hb=e1d5007e4688bfbbb176f0fc6c119a73e9f19882;hp=23586712ab7947c874afa6c3132ce2ed1e7e3fc7;hpb=d3027a41ce1fa76a1871cbfb87f065d8f9ab77e8;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java index 23586712..c75d733a 100644 --- a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java +++ b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java @@ -27,10 +27,10 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.builder.ExchangeBuilder; @@ -38,10 +38,12 @@ import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService; import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelId; import org.onap.clamp.loop.template.PolicyModelsService; import org.onap.clamp.policy.pdpgroup.PdpGroup; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -85,13 +87,29 @@ public class PolicyEngineServices { /** * This method query Policy engine and create a PolicyModel object with type and version. + * If the policy already exist in the db it returns the existing one. * * @param policyType The policyType id * @param policyVersion The policy version of that type - * @return A PolicyModel created from policyEngine data + * @return A PolicyModel created from policyEngine data or null if nothing is found on policyEngine */ public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion) { - return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion); + PolicyModel policyModelFound = policyModelsService.getPolicyModel(policyType, policyVersion); + if (policyModelFound == null) { + String policyTosca = this.downloadOnePolicy(policyType, policyVersion); + if (policyTosca != null && !policyTosca.isEmpty()) { + return policyModelsService.savePolicyModelInNewTransaction( + new PolicyModel(policyType, policyTosca, policyVersion)); + } else { + logger.error("Policy not found in the Policy Engine, returning null: " + policyType + + "/" + policyVersion); + return null; + } + } else { + logger.info("Skipping policy model download as it exists already in the database " + policyType + + "/" + policyVersion); + return policyModelFound; + } } /** @@ -118,11 +136,9 @@ public class PolicyEngineServices { LinkedHashMap policyTypesMap = (LinkedHashMap) loadedYaml .get("policy_types"); - policyTypesMap.entrySet().parallelStream().forEach(entryPolicyType -> { - policyModelsService.createPolicyInDbIfNeeded( - createPolicyModelFromPolicyEngine(entryPolicyType.getKey(), - ((String) ((LinkedHashMap) entryPolicyType.getValue()).get("version")))); - }); + policyTypesMap.forEach((key, value) -> + this.createPolicyModelFromPolicyEngine(key, + ((String) ((LinkedHashMap) value).get("version")))); } /** @@ -144,9 +160,24 @@ public class PolicyEngineServices { * @return A string with the whole policy tosca model */ public String downloadOnePolicy(String policyType, String policyVersion) { - return callCamelRoute(ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType) + logger.info("Downloading the policy model " + policyType + "/" + policyVersion); + DumperOptions options = new DumperOptions(); + options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); + options.setIndent(4); + options.setPrettyFlow(true); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + Yaml yamlParser = new Yaml(options); + String responseBody = callCamelRoute( + ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType) .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model", "Get one policy"); + + if (responseBody == null || responseBody.isEmpty()) { + logger.warn("getPolicyModel returned by policy engine could not be decoded, as it's null or empty"); + return null; + } + + return yamlParser.dump((Map) yamlParser.load(responseBody)); } /** @@ -167,9 +198,8 @@ public class PolicyEngineServices { List pdpGroupList = new LinkedList<>(); JsonArray itemsArray = (JsonArray) jsonObj.get("groups"); - Iterator it = itemsArray.iterator(); - while (it.hasNext()) { - JsonObject item = (JsonObject) it.next(); + for (com.google.gson.JsonElement jsonElement : itemsArray) { + JsonObject item = (JsonObject) jsonElement; PdpGroup pdpGroup = JsonUtils.GSON.fromJson(item.toString(), PdpGroup.class); pdpGroupList.add(pdpGroup); }