X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2FPolicyEngineServices.java;h=aecc8f4f506d09b9249a116a83d7525ba4d14edf;hb=3ed8d43005f52b84ce04a7311ca97b4cb4a8b487;hp=5e6d9d98d011f2dd7fbe37f5df08472f3cf1f812;hpb=e65d457a2dfd6ebb5e1f5a28b74f05c16c285dc3;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 5e6d9d98..aecc8f4f 100644 --- a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java +++ b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java @@ -27,7 +27,6 @@ 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; @@ -38,10 +37,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; @@ -91,7 +92,14 @@ public class PolicyEngineServices { * @return A PolicyModel created from policyEngine data */ public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion) { - return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion); + if (!policyModelsService.existsById( + new PolicyModelId(policyType, policyVersion))) { + return policyModelsService.savePolicyModelInNewTransaction( + new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion)); + } + logger.info("Skipping policy model download as it exists already in the database " + policyType + + "/" + policyVersion); + return null; } /** @@ -118,11 +126,9 @@ public class PolicyEngineServices { LinkedHashMap policyTypesMap = (LinkedHashMap) loadedYaml .get("policy_types"); - policyTypesMap.entrySet().stream().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 +150,16 @@ 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(2); + options.setPrettyFlow(true); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + return (new Yaml(options)).dump(callCamelRoute( + ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType) .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model", - "Get one policy"); + "Get one policy")); } /** @@ -167,9 +180,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); } @@ -182,7 +194,8 @@ public class PolicyEngineServices { Exchange exchangeResponse = camelContext.createProducerTemplate().send(camelFlow, exchange); if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) { return (String) exchangeResponse.getIn().getBody(); - } else { + } + else { logger.info(logMsg + " query " + retryInterval + "ms before retrying ..."); // wait for a while and try to connect to DCAE again try {