X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fsdc%2Fcontroller%2Finstaller%2FCsarInstallerImpl.java;h=e28e8ab7058c384af41a716924b5e528ce3f0d3a;hb=d67bf35d358de09054049641892cab72eacbb7ce;hp=c56eed517b813fdd03af86973a0003400eade37f;hpb=e71a59552ad658cf75e2fb0bfd375bdf51695f0d;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java index c56eed51..e28e8ab7 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java @@ -18,7 +18,7 @@ * limitations under the License. * ============LICENSE_END============================================ * =================================================================== - * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * */ package org.onap.clamp.clds.sdc.controller.installer; @@ -32,9 +32,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import javax.annotation.PostConstruct; +import javax.xml.transform.TransformerException; +import org.json.simple.parser.ParseException; +import org.onap.clamp.clds.client.DcaeInventoryServices; import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration; import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration; @@ -42,21 +46,30 @@ import org.onap.clamp.clds.dao.CldsDao; import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException; import org.onap.clamp.clds.model.CldsModel; import org.onap.clamp.clds.model.CldsTemplate; +import org.onap.clamp.clds.model.properties.ModelProperties; import org.onap.clamp.clds.service.CldsService; import org.onap.clamp.clds.service.CldsTemplateService; +import org.onap.clamp.clds.transform.XslTransformer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; import org.yaml.snakeyaml.Yaml; -@Component +/** + * This class will be instantiated by spring config, and used by Sdc Controller. + * There is no state kept by the bean. It's used to deploy the csar/notification + * received from SDC in DB. + */ public class CsarInstallerImpl implements CsarInstaller { private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class); private Map bpmnMapping = new HashMap<>(); - public static final String TEMPLATE_NAME_SUFFIX = "-template-dcae-designer"; - public static final String MODEL_NAME_SUFFIX = "-model-dcae-designer"; + public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-ClosedLoopTemplate-"; + public static final String CONTROL_NAME_PREFIX = "ClosedLoop-"; + public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input"; + // This will be used later as the policy scope + public static final String MODEL_NAME_PREFIX = "CLAMP"; /** * The file name that will be loaded by Spring. */ @@ -67,11 +80,15 @@ public class CsarInstallerImpl implements CsarInstaller { @Autowired private CldsDao cldsDao; @Autowired - private ClampProperties refProp; - @Autowired CldsTemplateService cldsTemplateService; @Autowired CldsService cldsService; + @Autowired + DcaeInventoryServices dcaeInventoryService; + @Autowired + private XslTransformer cldsBpmnTransformer; + @Autowired + private ClampProperties refProp; @PostConstruct public void loadConfiguration() throws IOException { @@ -82,25 +99,39 @@ public class CsarInstallerImpl implements CsarInstaller { @Override public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException { - return false; + return (CldsModel.retrieve(cldsDao, buildModelName(csar), true).getId() != null) ? true : false; + } + + public static String buildModelName(CsarHandler csar) { + return MODEL_NAME_PREFIX + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v" + + csar.getSdcNotification().getServiceVersion().replace('.', '_'); } @Override + @Transactional public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException { try { - BlueprintParserFilesConfiguration configFiles = this.searchForRightMapping(csar); - createFakeCldsModel(csar, configFiles, createFakeCldsTemplate(csar, configFiles)); + logger.info("Installing the CSAR " + csar.getFilePath()); + for (Entry blueprint : csar.getMapOfBlueprints().entrySet()) { + logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName()); + String serviceTypeId = queryDcaeToGetServiceTypeId(blueprint.getValue()); + createFakeCldsModel(csar, blueprint.getValue(), createFakeCldsTemplate(csar, blueprint.getValue(), + this.searchForRightMapping(blueprint.getValue())), serviceTypeId); + } + logger.info("Successfully installed the CSAR " + csar.getFilePath()); } catch (IOException e) { throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e); + } catch (ParseException | InterruptedException e) { + throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e); } } - private BlueprintParserFilesConfiguration searchForRightMapping(CsarHandler csar) + private BlueprintParserFilesConfiguration searchForRightMapping(BlueprintArtifact blueprintArtifact) throws SdcArtifactInstallerException { List listConfig = new ArrayList<>(); Yaml yaml = new Yaml(); Map templateNodes = ((Map) ((Map) yaml - .load(csar.getDcaeBlueprint())).get("node_templates")); + .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates")); bpmnMapping.entrySet().forEach(e -> { if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) { listConfig.add(e.getValue()); @@ -112,36 +143,99 @@ public class CsarInstallerImpl implements CsarInstaller { } else if (listConfig.isEmpty()) { throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint"); } + logger.info("Mapping found for blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is " + + listConfig.get(0).getBpmnXmlFilePath()); return listConfig.get(0); } - private String createTemplateName(CsarHandler csar) { - return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + TEMPLATE_NAME_SUFFIX; + private String searchForPolicyName(BlueprintArtifact blueprintArtifact) throws SdcArtifactInstallerException { + String policyName = null; + Yaml yaml = new Yaml(); + List policyNameList = new ArrayList<>(); + Map templateNodes = ((Map) ((Map) yaml + .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates")); + templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy")).forEach(ef -> { + String filteredPolicyName = (String) ((Map) ((Map) ef.getValue()) + .get("properties")).get("policy_filter"); + if (policyName != null) { + policyNameList.add(filteredPolicyName); + } else { + String inputPolicyName = (String) ((Map) ((Map) ((Map) ef + .getValue()).get("properties")).get("policy_id")).get(GET_INPUT_BLUEPRINT_PARAM); + if (inputPolicyName != null) { + policyNameList.add(GET_INPUT_BLUEPRINT_PARAM); + } + } + }); + if (policyNameList.size() > 1) { + throw new SdcArtifactInstallerException( + "The code does not currently support multiple Policy MicroServices in the blueprint"); + } else if (policyNameList.isEmpty()) { + throw new SdcArtifactInstallerException( + "There is no recognized Policy MicroService found in the blueprint"); + } + logger.info("policyName found in blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is " + + policyNameList.get(0)); + return policyNameList.get(0); } - private String createModelName(CsarHandler csar) { - return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + MODEL_NAME_SUFFIX; + private String queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact) + throws IOException, ParseException, InterruptedException { + return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(), + blueprintArtifact.getBlueprintInvariantServiceUuid(), + blueprintArtifact.getResourceAttached().getResourceInvariantUUID()).getTypeId(); } - private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintParserFilesConfiguration configFiles) - throws IOException, SdcArtifactInstallerException { + private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintArtifact blueprintArtifact, + BlueprintParserFilesConfiguration configFiles) throws IOException { CldsTemplate template = new CldsTemplate(); template.setBpmnId("Sdc-Generated"); template.setBpmnText( IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream())); - // ((ObjectNode)refProp.getJsonTemplate(CldsService.GLOBAL_PROPERTIES_KEY)); - template.setPropText(csar.getDcaeBlueprint()); + template.setPropText( + "{\"global\":[{\"name\":\"service\",\"value\":[\"" + blueprintArtifact.getDcaeBlueprint() + "\"]}]}"); template.setImageText( IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream())); - return cldsTemplateService.putTemplate(createTemplateName(csar), template); + template.setName(TEMPLATE_NAME_PREFIX + buildModelName(csar)); + template.save(cldsDao, null); + logger.info("Fake Clds Template created for blueprint " + blueprintArtifact.getBlueprintArtifactName() + + " with name " + template.getName()); + return template; } - private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintParserFilesConfiguration configFiles, - CldsTemplate cldsTemplate) { - CldsModel cldsModel = new CldsModel(); - cldsModel.setBlueprintText(csar.getDcaeBlueprint()); - cldsModel.setTemplateName(cldsTemplate.getName()); - // cldsModel.set - return cldsService.putModel(createModelName(csar), cldsModel); + private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintArtifact blueprintArtifact, + CldsTemplate cldsTemplate, String serviceTypeId) throws SdcArtifactInstallerException { + try { + CldsModel cldsModel = new CldsModel(); + cldsModel.setName(buildModelName(csar)); + cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint()); + cldsModel.setTemplateName(cldsTemplate.getName()); + cldsModel.setTemplateId(cldsTemplate.getId()); + cldsModel.setBpmnText(cldsTemplate.getBpmnText()); + cldsModel.setTypeId(serviceTypeId); + ModelProperties modelProp = new ModelProperties(cldsModel.getName(), "test", "PUT", false, + cldsBpmnTransformer.doXslTransformToString(cldsTemplate.getBpmnText()), "{}"); + String policyName = searchForPolicyName(blueprintArtifact); + String inputParams = ""; + if (policyName.contains("*")) { + // It's a filter must add a specific prefix + cldsModel.setControlNamePrefix(policyName); + } else { + cldsModel.setControlNamePrefix(CONTROL_NAME_PREFIX); + inputParams = "{\"name\":\"deployParameters\",\"value\":{\n" + "\"policy_id\": \"" + + modelProp.getPolicyNameForDcaeDeploy(refProp) + "\"" + "}}"; + } + cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\"" + + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\"" + + blueprintArtifact.getResourceAttached().getResourceInvariantUUID() + + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]}," + + inputParams + "]}"); + cldsModel = cldsModel.save(cldsDao, null); + logger.info("Fake Clds Model created for blueprint " + blueprintArtifact.getBlueprintArtifactName() + + " with name " + cldsModel.getName()); + return cldsModel; + } catch (TransformerException e) { + throw new SdcArtifactInstallerException("TransformerException when decoding the BpmnText", e); + } } }