From e1d5007e4688bfbbb176f0fc6c119a73e9f19882 Mon Sep 17 00:00:00 2001 From: sebdet Date: Mon, 9 Nov 2020 11:57:47 +0100 Subject: Fix blueprint installation This fix crashes the blueprint installation when it contains a link to a microservice that does not exist in the policy engine. Issue-ID: CLAMP-977 Signed-off-by: sebdet Change-Id: I659d864d202d9d77ef14560b1391397196ae1fbe --- .../clamp/clds/client/PolicyEngineServices.java | 39 +++++++++++++++------- .../java/org/onap/clamp/loop/CsarInstaller.java | 26 +++++++++------ .../clamp/loop/template/PolicyModelsService.java | 6 +--- 3 files changed, 43 insertions(+), 28 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 260bd1e48..c75d733a8 100644 --- a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java +++ b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java @@ -87,20 +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) { - if (!policyModelsService.existsById( - new PolicyModelId(policyType, policyVersion))) { - return policyModelsService.savePolicyModelInNewTransaction( - 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; } - logger.info("Skipping policy model download as it exists already in the database " + policyType - + "/" + policyVersion); - return null; } /** @@ -158,10 +167,17 @@ public class PolicyEngineServices { options.setPrettyFlow(true); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yamlParser = new Yaml(options); - return yamlParser.dump((Map) yamlParser.load(callCamelRoute( + String responseBody = callCamelRoute( ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType) .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model", - "Get one policy"))); + "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)); } /** @@ -196,8 +212,7 @@ 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/loop/CsarInstaller.java b/src/main/java/org/onap/clamp/loop/CsarInstaller.java index 67c7ce5cf..6752a1a36 100644 --- a/src/main/java/org/onap/clamp/loop/CsarInstaller.java +++ b/src/main/java/org/onap/clamp/loop/CsarInstaller.java @@ -152,7 +152,8 @@ public class CsarInstaller { private LoopTemplate createLoopTemplateFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact, Service service) - throws IOException, ParseException, InterruptedException, BlueprintParserException { + throws IOException, ParseException, InterruptedException, BlueprintParserException, + SdcArtifactInstallerException { LoopTemplate newLoopTemplate = new LoopTemplate(); newLoopTemplate.setBlueprint(blueprintArtifact.getDcaeBlueprint()); newLoopTemplate.setName(LoopTemplate.generateLoopTemplateName(csar.getSdcNotification().getServiceName(), @@ -165,32 +166,35 @@ public class CsarInstaller { microServicesChain = BlueprintParser.fallbackToOneMicroService(); } newLoopTemplate.setModelService(service); - newLoopTemplate.addLoopElementModels(createMicroServiceModels(microServicesChain)); + newLoopTemplate.addLoopElementModels(createMicroServiceModels(blueprintArtifact, microServicesChain)); newLoopTemplate.setMaximumInstancesAllowed(0); DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact); newLoopTemplate.setDcaeBlueprintId(dcaeResponse.getTypeId()); return newLoopTemplate; } - private HashSet createMicroServiceModels(List microServicesChain) - throws InterruptedException { + private HashSet createMicroServiceModels(BlueprintArtifact blueprintArtifact, + List microServicesChain) + throws SdcArtifactInstallerException { HashSet newSet = new HashSet<>(); for (BlueprintMicroService microService : microServicesChain) { LoopElementModel loopElementModel = new LoopElementModel(microService.getModelType(), LoopElementModel.MICRO_SERVICE_TYPE, null); newSet.add(loopElementModel); - loopElementModel.addPolicyModel(getPolicyModel(microService)); + PolicyModel newPolicyModel = policyEngineServices.createPolicyModelFromPolicyEngine(microService); + if (newPolicyModel != null) { + loopElementModel.addPolicyModel(newPolicyModel); + } else { + throw new SdcArtifactInstallerException( + "Unable to find the policy specified in the blueprint " + + blueprintArtifact.getBlueprintArtifactName() + ") on the Policy Engine:" + + microService.getModelType() + "/" + microService.getModelVersion()); + } } return newSet; } - private PolicyModel getPolicyModel(BlueprintMicroService microService) throws InterruptedException { - return policyModelsRepository - .findById(new PolicyModelId(microService.getModelType(), microService.getModelVersion())) - .orElse(policyEngineServices.createPolicyModelFromPolicyEngine(microService)); - } - /** * Get the service blueprint Id in the Dcae inventory using the SDC UUID. * 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 a1b8f7cf2..17cf5c1c8 100644 --- a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java @@ -100,7 +100,7 @@ public class PolicyModelsService { public PolicyModel updatePolicyModelTosca(String policyModelType, String policyModelVersion, String policyModelTosca) { JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca); - PolicyModel thePolicyModel = getPolicyModelByType(policyModelType, policyModelVersion); + PolicyModel thePolicyModel = getPolicyModel(policyModelType, policyModelVersion); thePolicyModel.setPolicyAcronym(toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject, ToscaSchemaConstants.METADATA_ACRONYM)); thePolicyModel.setPolicyModelTosca(policyModelTosca); @@ -123,10 +123,6 @@ public class PolicyModelsService { return policyModelsRepository.findByPolicyModelType(type); } - public PolicyModel getPolicyModelByType(String type, String version) { - return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null); - } - /** * Retrieves the Tosca model Yaml string. * -- cgit 1.2.3-korg