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=13af04399c236652dddfb5dc15a3dbc1c1ec7ce0;hb=c8e71ba3d736bfa4b05a3bc843b39328a596892c;hp=5a00a8280feb26138aff1f6e8600c1b7035914c0;hpb=018d85670c21af00991e1c29cce526a85b416df7;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 5a00a828..13af0439 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; @@ -35,6 +35,7 @@ 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; @@ -44,8 +45,11 @@ 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.dcae.DcaeInventoryResponse; +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; @@ -61,9 +65,11 @@ 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_PREFIX = "DCAE-Designer-ClosedLoopTemplate-"; - public static final String MODEL_NAME_PREFIX = "ClosedLoop-"; + public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-Template-"; + 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. */ @@ -79,6 +85,8 @@ public class CsarInstallerImpl implements CsarInstaller { CldsService cldsService; @Autowired DcaeInventoryServices dcaeInventoryService; + @Autowired + private XslTransformer cldsBpmnTransformer; @PostConstruct public void loadConfiguration() throws IOException { @@ -89,12 +97,29 @@ public class CsarInstallerImpl implements CsarInstaller { @Override public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException { - return (CldsModel.retrieve(cldsDao, buildModelName(csar), true).getId() != null) ? true : false; + boolean alreadyInstalled = true; + for (Entry blueprint : csar.getMapOfBlueprints().entrySet()) { + alreadyInstalled = alreadyInstalled + && (CldsModel.retrieve(cldsDao, buildModelName(csar, blueprint.getKey()), true).getId() != null) + ? true + : false; + } + return alreadyInstalled; } - public static String buildModelName(CsarHandler csar) { - return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + " v" - + csar.getSdcNotification().getServiceVersion(); + public static String buildModelName(CsarHandler csar, String resourceInstanceName) + throws SdcArtifactInstallerException { + String policyScopePrefix = searchForPolicyScopePrefix(csar.getMapOfBlueprints().get(resourceInstanceName)); + if (policyScopePrefix.contains("*")) { + // This is policy_filter type + policyScopePrefix = policyScopePrefix.replaceAll("\\*", ""); + } else { + // This is normally the get_input case + policyScopePrefix = MODEL_NAME_PREFIX; + } + return policyScopePrefix + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v" + + csar.getSdcNotification().getServiceVersion().replace('.', '_') + "_" + + resourceInstanceName.replaceAll(" ", ""); } @Override @@ -104,9 +129,10 @@ public class CsarInstallerImpl implements CsarInstaller { 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); + createFakeCldsModel(csar, blueprint.getValue(), + createFakeCldsTemplate(csar, blueprint.getValue(), + this.searchForRightMapping(blueprint.getValue())), + queryDcaeToGetServiceTypeId(blueprint.getValue())); } logger.info("Successfully installed the CSAR " + csar.getFilePath()); } catch (IOException e) { @@ -138,13 +164,14 @@ public class CsarInstallerImpl implements CsarInstaller { return listConfig.get(0); } - private String searchForPolicyName(BlueprintArtifact blueprintArtifact) throws SdcArtifactInstallerException { + private static String searchForPolicyScopePrefix(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 -> { + 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) { @@ -169,15 +196,26 @@ public class CsarInstallerImpl implements CsarInstaller { return policyNameList.get(0); } - private String queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact) + /** + * This call must be done when deploying the SDC notification as this call + * get the latest version of the artifact (version can be specified to DCAE + * call) + * + * @param blueprintArtifact + * @return The DcaeInventoryResponse object containing the dcae values + * @throws IOException + * @throws ParseException + * @throws InterruptedException + */ + private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact) throws IOException, ParseException, InterruptedException { return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact.getBlueprintInvariantServiceUuid(), - blueprintArtifact.getResourceAttached().getResourceInvariantUUID()).getTypeId(); + blueprintArtifact.getResourceAttached().getResourceInvariantUUID()); } private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintArtifact blueprintArtifact, - BlueprintParserFilesConfiguration configFiles) throws IOException { + BlueprintParserFilesConfiguration configFiles) throws IOException, SdcArtifactInstallerException { CldsTemplate template = new CldsTemplate(); template.setBpmnId("Sdc-Generated"); template.setBpmnText( @@ -186,7 +224,8 @@ public class CsarInstallerImpl implements CsarInstaller { "{\"global\":[{\"name\":\"service\",\"value\":[\"" + blueprintArtifact.getDcaeBlueprint() + "\"]}]}"); template.setImageText( IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream())); - template.setName(TEMPLATE_NAME_PREFIX + buildModelName(csar)); + template.setName(TEMPLATE_NAME_PREFIX + + buildModelName(csar, blueprintArtifact.getResourceAttached().getResourceInstanceName())); template.save(cldsDao, null); logger.info("Fake Clds Template created for blueprint " + blueprintArtifact.getBlueprintArtifactName() + " with name " + template.getName()); @@ -194,29 +233,45 @@ public class CsarInstallerImpl implements CsarInstaller { } private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintArtifact blueprintArtifact, - CldsTemplate cldsTemplate, String serviceTypeId) throws SdcArtifactInstallerException { - CldsModel cldsModel = new CldsModel(); - String policyName = searchForPolicyName(blueprintArtifact); - if (policyName.contains("*")) { - // It's a filter must add a specific prefix - cldsModel.setControlNamePrefix(policyName); - } else { - cldsModel.setControlNamePrefix(MODEL_NAME_PREFIX); + CldsTemplate cldsTemplate, DcaeInventoryResponse dcaeInventoryResponse) + throws SdcArtifactInstallerException { + try { + CldsModel cldsModel = new CldsModel(); + cldsModel.setName(buildModelName(csar, blueprintArtifact.getResourceAttached().getResourceInstanceName())); + cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint()); + cldsModel.setTemplateName(cldsTemplate.getName()); + cldsModel.setTemplateId(cldsTemplate.getId()); + cldsModel.setBpmnText(cldsTemplate.getBpmnText()); + cldsModel.setTypeId(dcaeInventoryResponse.getTypeId()); + cldsModel.setTypeName(dcaeInventoryResponse.getTypeName()); + cldsModel.setControlNamePrefix(CONTROL_NAME_PREFIX); + // We must save it otherwise object won't be created in db + // and proptext will always be null + cldsModel.setPropText("{\"global\":[]}"); + // Must save first to have the generated id available to generate + // the policyId + cldsModel = cldsModel.save(cldsDao, null); + cldsModel = setModelPropText(cldsModel, blueprintArtifact, cldsTemplate); + 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); } - cldsModel.setName(buildModelName(csar)); - cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint()); - cldsModel.setTemplateName(cldsTemplate.getName()); - cldsModel.setTemplateId(cldsTemplate.getId()); + } + + private CldsModel setModelPropText(CldsModel cldsModel, BlueprintArtifact blueprintArtifact, + CldsTemplate cldsTemplate) throws TransformerException { + // Do a test to validate the BPMN + new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), "PUT", false, + cldsBpmnTransformer.doXslTransformToString(cldsTemplate.getBpmnText()), "{}"); + String inputParams = "{\"name\":\"deployParameters\",\"value\":{\n" + "\"policy_id\": \"" + + "AUTO_GENERATED_POLICY_ID_AT_SUBMIT" + "\"" + "}}"; cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\"" + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\"" + blueprintArtifact.getResourceAttached().getResourceInvariantUUID() - + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},{\"name\":\"deployParameters\",\"value\":{\n" - + " \"policy_id\": \"" + "test" + "\"" + " }}]}"); - cldsModel.setBpmnText(cldsTemplate.getBpmnText()); - cldsModel.setTypeId(serviceTypeId); - cldsModel.save(cldsDao, null); - logger.info("Fake Clds Model created for blueprint " + blueprintArtifact.getBlueprintArtifactName() - + " with name " + cldsModel.getName()); - return cldsModel; + + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]}," + + inputParams + "]}"); + return cldsModel.save(cldsDao, null); } }