From 3ed8d43005f52b84ce04a7311ca97b4cb4a8b487 Mon Sep 17 00:00:00 2001 From: sebdet Date: Tue, 24 Mar 2020 15:49:19 -0700 Subject: Improve the policy model download Improve the policy model download so that the yaml is now in pretty printing and the policy engine is not triggered if the model is already in the database Issue-ID: CLAMP-784 Signed-off-by: sebdet Change-Id: Ibd1e6bd617cf052ad4c475e4dd9c63eb2b046acd --- .../clamp/clds/client/PolicyEngineServices.java | 39 ++++++++++++++-------- .../clds/config/DefaultDictionaryElements.java | 4 +-- .../sdc/controller/installer/BlueprintParser.java | 2 +- .../clamp/loop/template/PolicyModelsService.java | 7 ++-- .../OperationalPolicyRepresentationBuilder.java | 4 +-- 5 files changed, 33 insertions(+), 23 deletions(-) (limited to 'src/main') 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 { diff --git a/src/main/java/org/onap/clamp/clds/config/DefaultDictionaryElements.java b/src/main/java/org/onap/clamp/clds/config/DefaultDictionaryElements.java index d9b017b5..1c52e410 100644 --- a/src/main/java/org/onap/clamp/clds/config/DefaultDictionaryElements.java +++ b/src/main/java/org/onap/clamp/clds/config/DefaultDictionaryElements.java @@ -47,7 +47,7 @@ public class DefaultDictionaryElements { preProvisionDefaultOperations(); } - private void preProvisionDefaultActors () { + private void preProvisionDefaultActors() { // Set up dictionary elements Dictionary actorDictionary = new Dictionary(); actorDictionary.setName("DefaultActors"); @@ -92,7 +92,7 @@ public class DefaultDictionaryElements { dictionaryService.saveOrUpdateDictionary(actorDictionary); } - private void preProvisionDefaultOperations () { + private void preProvisionDefaultOperations() { // Set up dictionary elements Dictionary operationDictionary = new Dictionary(); operationDictionary.setName("DefaultOperations"); diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java index 981a2041..5d5027d2 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java @@ -48,7 +48,7 @@ public class BlueprintParser { static final String TCA = "TCA"; private static final String NODE_TEMPLATES = "node_templates"; private static final String DCAE_NODES = "dcae.nodes."; - private static final String DCAE_NODES_POLICY = "dcae.nodes.policy"; + private static final String DCAE_NODES_POLICY = ".nodes.policy"; private static final String TYPE = "type"; private static final String PROPERTIES = "properties"; private static final String NAME = "name"; diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java index 98b92891..a1b8f7cf 100644 --- a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java @@ -145,11 +145,8 @@ public class PolicyModelsService { * @param policyModel The policyModel to save */ @Transactional(propagation = Propagation.REQUIRES_NEW) - public void createPolicyInDbIfNeeded(PolicyModel policyModel) { - if (!policyModelsRepository.existsById( - new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) { - policyModelsRepository.saveAndFlush(policyModel); - } + public PolicyModel savePolicyModelInNewTransaction(PolicyModel policyModel) { + return policyModelsRepository.saveAndFlush(policyModel); } /** diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java index 6577069e..44ee1197 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java @@ -165,7 +165,7 @@ public class OperationalPolicyRepresentationBuilder { } /** - * Create an anyOf array of possible structure we may have for Target + * Create an anyOf array of possible structure we may have for Target. * * @param modelJson The service object * @return A JsonArray with everything inside @@ -177,7 +177,7 @@ public class OperationalPolicyRepresentationBuilder { return targetOneOfStructure; } - public static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) { + private static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) { JsonArray anyOfStructure = new JsonArray(); anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("VF"))); anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("PNF"))); -- cgit 1.2.3-korg