From 46f9fd57f24428f6a6fd0d290b0463e188975231 Mon Sep 17 00:00:00 2001 From: Michael Lando Date: Thu, 10 Aug 2017 12:08:45 +0300 Subject: [SDC] code sync Change-Id: Ia33a7874802b8ac2f49aed9199e109ea509e950e Signed-off-by: Michael Lando --- .../be/components/impl/ResourceBusinessLogic.java | 159 +++++++++++---------- 1 file changed, 87 insertions(+), 72 deletions(-) (limited to 'catalog-be') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java index 383f1eb2ec..82382f961d 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java @@ -526,11 +526,11 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { String yamlFileName = toscaYamlCsarStatus.left().value().getKey(); String yamlFileContents = toscaYamlCsarStatus.left().value().getValue(); - CsarInfo csarInfo = new CsarInfo(newRresource.getName(), user, csarUUID, csar.left().value(), true); - Map nodeTypesInfo = extractNodeTypesInfo(csar.left().value(), yamlFileContents); - Either result =null; + CsarInfo csarInfo = new CsarInfo(newRresource.getName(), user, csarUUID, csar.left().value(), yamlFileContents, true); + Map nodeTypesInfo = extractNodeTypesInfo(csarInfo); + Either result = null; - Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo.getCsar(), csarInfo.getCsarUUID(), yamlFileName, oldRresource, csarInfo.getModifier()); + Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo, oldRresource); if (findNodeTypesArtifactsToHandleRes.isRight()) { log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID()); result = Either.right(findNodeTypesArtifactsToHandleRes.right().value()); @@ -566,14 +566,14 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { private Either updateResourceFromYaml(Resource oldRresource, Resource newRresource, AuditingActionEnum updateResource, List createdArtifacts, boolean isUpdateYaml, String yamlFileName, String yamlFileContent, CsarInfo csarInfo, Map nodeTypesInfo, Map>> nodeTypesArtifactsToHandle, String nodeName) { - Either result = null; + Either result; Either, ResponseFormat> parseNodeTypeInfoYamlEither; boolean inTransaction = true; boolean shouldLock = false; - Either, ResponseFormat> prepareForUpdate = null; - Resource preparedResource = null; - Either uploadComponentInstanceInfoMap = parseResourceInfoFromYaml(yamlFileName, newRresource, yamlFileContent, csarInfo.getModifier(), csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo, nodeName); + Either, ResponseFormat> prepareForUpdate; + Resource preparedResource; + Either uploadComponentInstanceInfoMap = parseResourceInfoFromYaml(yamlFileName, newRresource, yamlFileContent, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo, nodeName); if (uploadComponentInstanceInfoMap.isRight()) { ResponseFormat responseFormat = uploadComponentInstanceInfoMap.right().value(); componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), newRresource, "", "", updateResource, null); @@ -687,18 +687,18 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return result; } - private Either>>, ResponseFormat> findNodeTypesArtifactsToHandle(Map nodeTypesInfo, Map csar, String csarUUID, String yamlFileName, Resource oldResource, User user) { + private Either>>, ResponseFormat> findNodeTypesArtifactsToHandle(Map nodeTypesInfo, CsarInfo csarInfo, Resource oldResource) { - Map> extractedVfcsArtifacts = CsarUtils.extractVfcsArtifactsFromCsar(csar); + Map> extractedVfcsArtifacts = CsarUtils.extractVfcsArtifactsFromCsar(csarInfo.getCsar()); Map>> nodeTypesArtifactsToHandle = new HashMap<>(); Either>>, ResponseFormat> nodeTypesArtifactsToHandleRes; try { nodeTypesArtifactsToHandleRes = Either.left(nodeTypesArtifactsToHandle); - Map extractedVfcToscaNames = extractVfcToscaNames(nodeTypesInfo, yamlFileName, oldResource.getName()); + Map extractedVfcToscaNames = extractVfcToscaNames(nodeTypesInfo, oldResource.getName(), csarInfo); Either>, ResponseFormat> curNodeTypeArtifactsToHandleRes; EnumMap> curNodeTypeArtifactsToHandle = null; - log.debug("Going to fetch node types for resource with name {} during import csar with UUID {}. ", oldResource.getName(), csarUUID); + log.debug("Going to fetch node types for resource with name {} during import csar with UUID {}. ", oldResource.getName(), csarInfo.getCsarUUID()); for (Entry currVfcToscaNameEntry : extractedVfcToscaNames.entrySet()) { String currVfcToscaName = currVfcToscaNameEntry.getValue(); @@ -709,8 +709,8 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { Resource curNodeType = null; if (curVfcRes.isRight() && curVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) { log.debug("Error occured during fetching node type with tosca name {}, error: {}", currVfcToscaName, curVfcRes.right().value()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(curVfcRes.right().value()), csarUUID); - componentsUtils.auditResource(responseFormat, user, oldResource, "", "", AuditingActionEnum.CREATE_RESOURCE, null); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(curVfcRes.right().value()), csarInfo.getCsarUUID()); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), oldResource, "", "", AuditingActionEnum.CREATE_RESOURCE, null); nodeTypesArtifactsToHandleRes = Either.right(responseFormat); break; } else if (curVfcRes.isLeft()) { @@ -905,10 +905,10 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return handleNodeTypeArtifactsRes; } - private Map extractVfcToscaNames(Map nodeTypesInfo, String yamlFileName, String vfResourceName) { + private Map extractVfcToscaNames(Map nodeTypesInfo, String vfResourceName, CsarInfo csarInfo) { Map vfcToscaNames = new HashMap<>(); - Map nodes = extractAllNodes(nodeTypesInfo); + Map nodes = extractAllNodes(nodeTypesInfo, csarInfo); if (!nodes.isEmpty()) { Iterator> nodesNameEntry = nodes.entrySet().iterator(); while (nodesNameEntry.hasNext()) { @@ -920,17 +920,22 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return vfcToscaNames; } - private Map extractAllNodes(Map nodeTypesInfo) { + private Map extractAllNodes(Map nodeTypesInfo, CsarInfo csarInfo) { Map nodes = new HashMap<>(); for(NodeTypeInfo nodeTypeInfo: nodeTypesInfo.values()){ - Either, ResultStatusEnum> eitherNodeTypes = ImportUtils.findFirstToscaMapElement(nodeTypeInfo.getMappedToscaTemplate(), ToscaTagNamesEnum.NODE_TYPES); - if (eitherNodeTypes.isLeft()) { - nodes.putAll(eitherNodeTypes.left().value()); - } + extractNodeTypes(nodes, nodeTypeInfo.getMappedToscaTemplate()); } + extractNodeTypes(nodes, csarInfo.getMappedToscaMainTemplate()); return nodes; } + private void extractNodeTypes(Map nodes, Map mappedToscaTemplate) { + Either, ResultStatusEnum> eitherNodeTypes = ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, ToscaTagNamesEnum.NODE_TYPES); + if (eitherNodeTypes.isLeft()) { + nodes.putAll(eitherNodeTypes.left().value()); + } + } + public Either createResourceFromCsar(Resource resource, User user, Either, StorageOperationStatus> csarUIPayload, String csarUUID) { log.trace("************* created successfully from YAML, resource TOSCA "); @@ -956,9 +961,9 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { String yamlFileContents = toscaYamlCsarStatus.left().value().getValue(); log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContents); - Map nodeTypesInfo = extractNodeTypesInfo(csar.left().value(), yamlFileContents); - CsarInfo csarInfo = new CsarInfo(resource.getName(), user, csarUUID, csar.left().value(), false); - Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo.getCsar(), csarInfo.getCsarUUID(), yamlFileName, resource, csarInfo.getModifier()); + CsarInfo csarInfo = new CsarInfo(resource.getName(), user, csarUUID, csar.left().value(), yamlFileContents, false); + Map nodeTypesInfo = extractNodeTypesInfo(csarInfo); + Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo, resource); if (findNodeTypesArtifactsToHandleRes.isRight()) { log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID()); return Either.right(findNodeTypesArtifactsToHandleRes.right().value()); @@ -974,60 +979,69 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return Either.left(vfResource); } - @SuppressWarnings("unchecked") - private Map extractNodeTypesInfo(Map csar, String yamlFileContent) { + private Map extractNodeTypesInfo(CsarInfo csarInfo) { Map nodeTypesInfo = new HashMap<>(); List> globalSubstitutes = new ArrayList<>(); - for (Map.Entry entry : csar.entrySet()) { - if (Pattern.compile(CsarUtils.SERVICE_TEMPLATE_PATH_PATTERN).matcher(entry.getKey()).matches()) { - if (!isGlobalSubstitute(entry.getKey())) { - String yamlFileContents = new String(entry.getValue()); - Map mappedToscaTemplate = (Map) new Yaml().load(yamlFileContents); - Either substitutionMappingsEither = ImportUtils.findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS, ToscaElementTypeEnum.MAP); - if (substitutionMappingsEither.isLeft()) { - Map substitutionMappings = (Map) substitutionMappingsEither.left().value(); - if (substitutionMappings.containsKey(ToscaTagNamesEnum.NODE_TYPE.getElementName())) { - NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); - nodeTypeInfo.setType((String) substitutionMappings.get(ToscaTagNamesEnum.NODE_TYPE.getElementName())); - nodeTypeInfo.setTemplateFileName(entry.getKey()); - nodeTypeInfo.setMappedToscaTemplate(mappedToscaTemplate); - nodeTypesInfo.put(nodeTypeInfo.getType(), nodeTypeInfo); + for (Map.Entry entry : csarInfo.getCsar().entrySet()) { + extractNodeTypeInfo(nodeTypesInfo, globalSubstitutes, entry); + } + if (CollectionUtils.isNotEmpty(globalSubstitutes)) { + setDerivedFrom(nodeTypesInfo, globalSubstitutes); + } + markNestedVfc(csarInfo.getMappedToscaMainTemplate(), nodeTypesInfo); + return nodeTypesInfo; + } + + @SuppressWarnings("unchecked") + private void setDerivedFrom(Map nodeTypesInfo, List> globalSubstitutes) { + for (Map.Entry entry : globalSubstitutes) { + String yamlFileContents = new String(entry.getValue()); + Map mappedToscaTemplate = (Map) new Yaml().load(yamlFileContents); + Either nodeTypesEither = ImportUtils.findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP); + if (nodeTypesEither.isLeft()) { + Map nodeTypes = (Map) nodeTypesEither.left().value(); + for (Entry nodeType : nodeTypes.entrySet()) { + Map nodeTypeMap = (Map) nodeType.getValue(); + if (nodeTypeMap.containsKey(ToscaTagNamesEnum.DERIVED_FROM.getElementName())) { + if (nodeTypesInfo.containsKey(nodeType.getKey())) { + NodeTypeInfo nodeTypeInfo = nodeTypesInfo.get(nodeType.getKey()); + List derivedFrom = new ArrayList<>(); + derivedFrom.add((String) nodeTypeMap.get(ToscaTagNamesEnum.DERIVED_FROM.getElementName())); + nodeTypeInfo.setDerivedFrom(derivedFrom); } } - } else { - globalSubstitutes.add(entry); } } } - if (CollectionUtils.isNotEmpty(globalSubstitutes)) { - for (Map.Entry entry : globalSubstitutes) { + } + + @SuppressWarnings("unchecked") + private void extractNodeTypeInfo(Map nodeTypesInfo, + List> globalSubstitutes, Map.Entry entry) { + if (Pattern.compile(CsarUtils.SERVICE_TEMPLATE_PATH_PATTERN).matcher(entry.getKey()).matches()) { + if (!isGlobalSubstitute(entry.getKey())) { String yamlFileContents = new String(entry.getValue()); Map mappedToscaTemplate = (Map) new Yaml().load(yamlFileContents); - Either nodeTypesEither = ImportUtils.findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP); - if (nodeTypesEither.isLeft()) { - Map nodeTypes = (Map) nodeTypesEither.left().value(); - for (Entry nodeType : nodeTypes.entrySet()) { - Map nodeTypeMap = (Map) nodeType.getValue(); - if (nodeTypeMap.containsKey(ToscaTagNamesEnum.DERIVED_FROM.getElementName())) { - if (nodeTypesInfo.containsKey(nodeType.getKey())) { - NodeTypeInfo nodeTypeInfo = nodeTypesInfo.get(nodeType.getKey()); - List derivedFrom = new ArrayList<>(); - derivedFrom.add((String) nodeTypeMap.get(ToscaTagNamesEnum.DERIVED_FROM.getElementName())); - nodeTypeInfo.setDerivedFrom(derivedFrom); - } - } + Either substitutionMappingsEither = ImportUtils.findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS, ToscaElementTypeEnum.MAP); + if (substitutionMappingsEither.isLeft()) { + Map substitutionMappings = (Map) substitutionMappingsEither.left().value(); + if (substitutionMappings.containsKey(ToscaTagNamesEnum.NODE_TYPE.getElementName())) { + NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); + nodeTypeInfo.setType((String) substitutionMappings.get(ToscaTagNamesEnum.NODE_TYPE.getElementName())); + nodeTypeInfo.setTemplateFileName(entry.getKey()); + nodeTypeInfo.setMappedToscaTemplate(mappedToscaTemplate); + nodeTypesInfo.put(nodeTypeInfo.getType(), nodeTypeInfo); } } + } else { + globalSubstitutes.add(entry); } } - markNestedVfc(yamlFileContent, nodeTypesInfo); - return nodeTypesInfo; } @SuppressWarnings("unchecked") - private void markNestedVfc(String yamlFileContent, Map nodeTypesInfo) { - Map mappedToscaTemplate = (Map) new Yaml().load(yamlFileContent); + private void markNestedVfc(Map mappedToscaTemplate, Map nodeTypesInfo) { Either nodeTemplatesEither = ImportUtils.findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP); if (nodeTemplatesEither.isLeft()) { Map nodeTemplates = (Map) nodeTemplatesEither.left().value(); @@ -1110,7 +1124,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { List createdArtifacts = new ArrayList(); log.trace("************* createResourceFromYaml before parse yaml "); - Either parseResourceInfoFromYamlEither = parseResourceInfoFromYaml(yamlName, resource, topologyTemplateYaml, csarInfo.getModifier(), csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo, nodeName); + Either parseResourceInfoFromYamlEither = parseResourceInfoFromYaml(yamlName, resource, topologyTemplateYaml, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo, nodeName); if (parseResourceInfoFromYamlEither.isRight()) { ResponseFormat responseFormat = parseResourceInfoFromYamlEither.right().value(); componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, "", "", AuditingActionEnum.IMPORT_RESOURCE, null); @@ -1134,7 +1148,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } - public Either, ResponseFormat> createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, String resourceYml, Map mappedToscaTemplate, boolean needLock, + public Either, ResponseFormat> createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, Map mappedToscaTemplate, boolean needLock, Map>> nodeTypesArtifactsToHandle, List nodeTypesNewCreatedArtifacts, Map nodeTypesInfo, CsarInfo csarInfo) { @@ -1187,11 +1201,10 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { Either handleNestedVfcRes = Either.left(resource); String yamlName = nodesInfo.get(nodeName).getTemplateFileName(); - String yamlContent = new String(csarInfo.getCsar().get(yamlName)); Map nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate(); log.debug("************* Going to create node types from yaml {}", yamlName); - Either, ResponseFormat> createNodeTypesRes = createResourcesFromYamlNodeTypesList(yamlName, resource, yamlContent, + Either, ResponseFormat> createNodeTypesRes = createResourcesFromYamlNodeTypesList(yamlName, resource, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo); if (createNodeTypesRes.isRight()) { log.debug("Failed to create node types from yaml {}. Status is {}", yamlName, createNodeTypesRes.right().value()); @@ -1201,13 +1214,13 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { if (nestedVfcJsonMap.containsKey(ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) { log.debug("************* Going to handle complex VFC from yaml {}", yamlName); - handleNestedVfcRes = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, yamlContent); + handleNestedVfcRes = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName); } return handleNestedVfcRes; } private Either handleComplexVfc(Resource resource, Map>> nodesArtifactsToHandle, List createdArtifacts, - Map nodesInfo, CsarInfo csarInfo, String nodeName, String yamlName, String yamlContent) { + Map nodesInfo, CsarInfo csarInfo, String nodeName, String yamlName) { Either result = null; Resource oldComplexVfc = null; @@ -1229,7 +1242,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } } if(result == null){ - result = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, yamlContent, oldComplexVfc, newComplexVfc); + result = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, oldComplexVfc, newComplexVfc); } if(result.isLeft()){ newComplexVfc = result.left().value(); @@ -1249,11 +1262,13 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } private Either handleComplexVfc(Map>> nodesArtifactsToHandle, List createdArtifacts, Map nodesInfo, - CsarInfo csarInfo, String nodeName, String yamlName, String yamlContent, Resource oldComplexVfc, Resource newComplexVfc) { + CsarInfo csarInfo, String nodeName, String yamlName, Resource oldComplexVfc, Resource newComplexVfc) { Either handleComplexVfcRes; + Map mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate(); + String yamlContent = new String(csarInfo.getCsar().get(yamlName)); Map newNodeTypesInfo = nodesInfo.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().getUnmarkedCopy())); - markNestedVfc(yamlContent, newNodeTypesInfo); + markNestedVfc(mappedToscaTemplate, newNodeTypesInfo); if(oldComplexVfc == null){ handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo, csarInfo, nodesArtifactsToHandle, false, true, nodeName); if (handleComplexVfcRes.isRight()) { @@ -1929,7 +1944,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { mappedToscaTemplate = (Map) new Yaml().load(topologyTemplateYaml); } - Either, ResponseFormat> createdNodeTypeFromMainTemplateEither = createResourcesFromYamlNodeTypesList(yamlName, resource, topologyTemplateYaml, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle, + Either, ResponseFormat> createdNodeTypeFromMainTemplateEither = createResourcesFromYamlNodeTypesList(yamlName, resource, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo); if (createdNodeTypeFromMainTemplateEither.isRight()) { ResponseFormat responseFormat = createdNodeTypeFromMainTemplateEither.right().value(); @@ -4206,7 +4221,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } @SuppressWarnings("unchecked") - public Either parseResourceInfoFromYaml(String yamlFileName, Resource resource, String resourceYml, User user, Map createdNodesToscaResourceNames, Map nodeTypesInfo, String nodeName) { + public Either parseResourceInfoFromYaml(String yamlFileName, Resource resource, String resourceYml, Map createdNodesToscaResourceNames, Map nodeTypesInfo, String nodeName) { Map mappedToscaTemplate; if(nodeTypesInfo != null && nodeName != null && nodeTypesInfo.containsKey(nodeName)){ -- cgit 1.2.3-korg