X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-be%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fcomponents%2Fimpl%2FResourceBusinessLogic.java;h=b664efebcb091b26e28f2e63dce1cd80d8960205;hb=88e37209870c269747d9dff49ab1169cb6399654;hp=e5282ce437c9ad25028830116f383f5437fd7264;hpb=0819f24218af38d1875defe46d233141648c1705;p=sdc.git 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 e5282ce437..b664efebcb 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 @@ -111,6 +111,7 @@ import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.category.SubCategoryDefinition; import org.openecomp.sdc.be.model.heat.HeatParameterType; +import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation; import org.openecomp.sdc.be.model.operations.api.IElementOperation; @@ -595,6 +596,14 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent); + Either genericResourceEither = handleResourceGenericType(preparedResource); + if (genericResourceEither.isRight()) { + log.debug("failed to get resource generic type. status is {}", genericResourceEither.right().value()); + ResponseFormat responseFormat = genericResourceEither.right().value(); + componentsUtils.auditResource(genericResourceEither.right().value(), csarInfo.getModifier(), preparedResource, "", "", updateResource, null); + return Either.right(responseFormat); + } + parseNodeTypeInfoYamlEither = this.handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo, csarInfo, nodeName); if (parseNodeTypeInfoYamlEither.isRight()) { ResponseFormat responseFormat = parseNodeTypeInfoYamlEither.right().value(); @@ -687,6 +696,17 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return result; } + private Either handleResourceGenericType(Resource resource) { + Either genericResourceEither = fetchAndSetDerivedFromGenericType(resource); + if (genericResourceEither.isRight()) { + return genericResourceEither; + } + if (resource.shouldGenerateInputs()) { + generateInputsFromGenericTypeProperties(resource, genericResourceEither.left().value()); + } + return genericResourceEither; + } + private Either>>, ResponseFormat> findNodeTypesArtifactsToHandle(Map nodeTypesInfo, CsarInfo csarInfo, Resource oldResource) { Map> extractedVfcsArtifacts = CsarUtils.extractVfcsArtifactsFromCsar(csarInfo.getCsar()); @@ -888,7 +908,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { handleNodeTypeArtifactsRes = Either.right(handleNodeTypeArtifactsRequestRes.right().value()); break; } - if (curOperation == ArtifactOperationEnum.Create) { + if (ArtifactOperationEnum.isCreateOrLink(curOperation)) { createdArtifacts.addAll(handleNodeTypeArtifactsRequestRes.left().value()); } handledNodeTypeArtifacts.addAll(handleNodeTypeArtifactsRequestRes.left().value()); @@ -917,6 +937,9 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { vfcToscaNames.put(nodeType.getKey(), toscaResourceName); } } + for(NodeTypeInfo cvfc : nodeTypesInfo.values()){ + vfcToscaNames.put(cvfc.getType(), buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), vfResourceName, cvfc.getType())); + } return vfcToscaNames; } @@ -1107,7 +1130,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return Either.right(validateRes.right().value()); } // VF / PNF "derivedFrom" should be null (or ignored) - if (ToscaUtils.isAtomicType(resource)) { + if (ModelConverter.isAtomicComponent(resource)) { Either validateDerivedFromNotEmpty = validateDerivedFromNotEmpty(user, resource, AuditingActionEnum.CREATE_RESOURCE); if (validateDerivedFromNotEmpty.isRight()) { return Either.right(validateDerivedFromNotEmpty.right().value()); @@ -1233,7 +1256,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } if(result == null){ newComplexVfc = buildCvfcRes.left().value(); - Either oldComplexVfcRes = toscaOperationFacade.getLatestByToscaResourceName(newComplexVfc.getToscaResourceName()); + Either oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName()); if(oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND){ log.debug("Failed to fetch previous complex VFC by tosca resource name {}. Status is {}. ", newComplexVfc.getToscaResourceName(), oldComplexVfcRes.right().value()); result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); @@ -1461,7 +1484,8 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { String nameWithouNamespacePrefix = nodeName.substring(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX.length()); String[] findTypes = nameWithouNamespacePrefix.split("\\."); String resourceType = findTypes[0]; - return resourceVfName + "-" + nameWithouNamespacePrefix.substring(resourceType.length() + 1) + "Cvfc"; + String resourceName = resourceVfName + "-" + nameWithouNamespacePrefix.substring(resourceType.length() + 1); + return addCvfcSuffixToResourceName(resourceName); } private Either createResourceAndRIsFromYaml(String yamlName, Resource resource, ParsedToscaYamlInfo parsedToscaYamlInfo, AuditingActionEnum actionEnum, boolean isNormative, @@ -1988,6 +2012,14 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { if (eitherCreateResult.isRight()) { return Either.right(eitherCreateResult.right().value()); } + Either eitherGerResource = toscaOperationFacade.getToscaElement(resource.getUniqueId()); + if (eitherGerResource.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource); + + return Either.right(responseFormat); + + } + resource = eitherGerResource.left().value(); Either, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils.getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils); if (artifacsMetaCsarStatus.isLeft()) { @@ -1995,7 +2027,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey(); String artifactsContents = artifacsMetaCsarStatus.left().value().getValue(); Either createArtifactsFromCsar = Either.left(resource); - if (artifactOperation.getArtifactOperationEnum() == ArtifactOperationEnum.Create) + if (ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) createArtifactsFromCsar = createResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts, shouldLock, inTransaction); else createArtifactsFromCsar = updateResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts, shouldLock, inTransaction); @@ -2075,7 +2107,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } private void addNonMetaCreatedArtifactsToSupportRollback(ArtifactOperationInfo operation, List createdArtifacts, Either, ResponseFormat> eitherNonMetaArtifacts) { - if (operation.getArtifactOperationEnum() == ArtifactOperationEnum.Create && createdArtifacts != null && eitherNonMetaArtifacts.isLeft()) { + if (ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum()) && createdArtifacts != null && eitherNonMetaArtifacts.isLeft()) { Either eitherResult = eitherNonMetaArtifacts.left().value(); if (eitherResult.isLeft()) { createdArtifacts.add(eitherResult.left().value()); @@ -2141,16 +2173,27 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { Map> parsedGroup = new HashMap>(); for (List parsedGroupTemplateList : parsedArifactsCollection) { + for (ArtifactTemplateInfo parsedGroupTemplate : parsedGroupTemplateList) { - parsedGroupTemplate.setGroupName(""); - Set parsedArtifactsNames = new HashSet(); - parsedArtifactsNames.add(parsedGroupTemplate); - List relatedGroupTemplateList = parsedGroupTemplate.getRelatedArtifactsInfo(); - if (relatedGroupTemplateList != null && !relatedGroupTemplateList.isEmpty()) { - createArtifactsGroupSet(parsedGroupTemplateList, parsedArtifactsNames); + if(parsedGroupTemplate.getGroupName() != null){ + parsedGroupTemplate.setGroupName(""); + Set parsedArtifactsNames = new HashSet(); + parsedArtifactsNames.add(parsedGroupTemplate); + List relatedGroupTemplateList = parsedGroupTemplate.getRelatedArtifactsInfo(); + if (relatedGroupTemplateList != null && !relatedGroupTemplateList.isEmpty()) { + createArtifactsGroupSet(parsedGroupTemplateList, parsedArtifactsNames); + } + parsedGroup.put(parsedGroupTemplate, parsedArtifactsNames); + }else{ + List arrtifacts = new ArrayList(); + arrtifacts.add(parsedGroupTemplate); + Either resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, resource, arrtifacts, createdNewArtifacts, createdDeplymentArtifactsAfterDelete, labelCounter, shouldLock, inTransaction); + if (resStatus.isRight()) + return resStatus; + } - parsedGroup.put(parsedGroupTemplate, parsedArtifactsNames); } + } ///////////////////////////////// find artifacts to @@ -2671,42 +2714,40 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { resStatus = createDeploymentArtifactsFromCsar(csarInfo, resource, artifactsGroup, artifactsUUIDGroup, groupTemplateInfo, createdNewArtifacts, artifactsFromResource, labelCounter, shouldLock, inTransaction); if (resStatus.isRight()) return resStatus; - - Map members = new HashMap(); - associateMembersToArtifacts(createdNewArtifacts, artifactsFromResource, heatGroups, artifactsGroup, members); - - List artifactsList = new ArrayList(artifactsGroup); - List artifactsUUIDList = new ArrayList(artifactsUUIDGroup); - - GroupDefinition groupDefinition = new GroupDefinition(); - groupDefinition.setName(groupName); - groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE); - groupDefinition.setArtifacts(artifactsList); - groupDefinition.setArtifactsUuid(artifactsUUIDList); - - if (!members.isEmpty()) - groupDefinition.setMembers(members); - - List properties = new ArrayList(); - GroupProperty prop = new GroupProperty(); - prop.setName(Constants.IS_BASE); - prop.setValue(Boolean.toString(groupTemplateInfo.isBase())); - properties.add(prop); - - List createdArtifacts = new ArrayList<>(); - createdArtifacts.addAll(createdNewArtifacts); - createdArtifacts.addAll(artifactsFromResource); - Either getLatestGroupTypeRes = groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, true); - if (getLatestGroupTypeRes.isRight()) { - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getLatestGroupTypeRes.right().value()))); + if(groupName != null && !groupName.isEmpty()){ + Map members = new HashMap(); + associateMembersToArtifacts(createdNewArtifacts, artifactsFromResource, heatGroups, artifactsGroup, members); + + List artifactsList = new ArrayList(artifactsGroup); + List artifactsUUIDList = new ArrayList(artifactsUUIDGroup); + + GroupDefinition groupDefinition = new GroupDefinition(); + groupDefinition.setName(groupName); + groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE); + groupDefinition.setArtifacts(artifactsList); + groupDefinition.setArtifactsUuid(artifactsUUIDList); + + if (!members.isEmpty()) + groupDefinition.setMembers(members); + + List properties = new ArrayList(); + GroupProperty prop = new GroupProperty(); + prop.setName(Constants.IS_BASE); + prop.setValue(Boolean.toString(groupTemplateInfo.isBase())); + properties.add(prop); + + List createdArtifacts = new ArrayList<>(); + createdArtifacts.addAll(createdNewArtifacts); + createdArtifacts.addAll(artifactsFromResource); + Either getLatestGroupTypeRes = groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, true); + if (getLatestGroupTypeRes.isRight()) { + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getLatestGroupTypeRes.right().value()))); + } + properties = createVfModuleAdditionalProperties(groupTemplateInfo.isBase(), groupName, properties, createdArtifacts, artifactsList, getLatestGroupTypeRes.left().value()); + groupDefinition.convertFromGroupProperties(properties); + + needToAdd.add(groupDefinition); } - properties = createVfModuleAdditionalProperties(groupTemplateInfo.isBase(), groupName, properties, createdArtifacts, artifactsList, getLatestGroupTypeRes.left().value()); - groupDefinition.convertFromGroupProperties(properties); - - // Either createGroup = groupBusinessLogic.createGroup(resource.getUniqueId(), user.getUserId(), ComponentTypeEnum.RESOURCE, groupDefinition, inTransaction); - // if (createGroup.isRight()) - // return Either.right(createGroup.right().value()); - needToAdd.add(groupDefinition); } ComponentParametersView componentParametersView = new ComponentParametersView(); componentParametersView.disableAll(); @@ -2859,7 +2900,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { EnumMap> vfCsarArtifactsToHandle = null; - if (artifactOperation.getArtifactOperationEnum() == ArtifactOperationEnum.Create) { + if (ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) { vfCsarArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class); vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList); } else { @@ -2990,42 +3031,36 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { log.debug("createDeploymentArtifactsFromCsar end"); if (resStatus.isRight()) return resStatus; - - Map members = new HashMap(); - associateMembersToArtifacts(createdArtifacts, null, heatGroups, artifactsGroup, members); - - List artifactsList = new ArrayList(artifactsGroup); - List artifactsUUIDList = new ArrayList(artifactsUUIDGroup); - - GroupDefinition groupDefinition = new GroupDefinition(); - groupDefinition.setName(groupName); - groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE); - groupDefinition.setArtifacts(artifactsList); - groupDefinition.setArtifactsUuid(artifactsUUIDList); - - if (!members.isEmpty()) - groupDefinition.setMembers(members); - List properties = new ArrayList(); - GroupProperty prop = new GroupProperty(); - prop.setName(Constants.IS_BASE); - prop.setValue(Boolean.toString(groupTemplateInfo.isBase())); - properties.add(prop); - Either getLatestGroupTypeRes = groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, true); - if (getLatestGroupTypeRes.isRight()) { - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getLatestGroupTypeRes.right().value()))); + if(groupName != null && !groupName.isEmpty()){ + Map members = new HashMap(); + associateMembersToArtifacts(createdArtifacts, null, heatGroups, artifactsGroup, members); + + List artifactsList = new ArrayList(artifactsGroup); + List artifactsUUIDList = new ArrayList(artifactsUUIDGroup); + + GroupDefinition groupDefinition = new GroupDefinition(); + groupDefinition.setName(groupName); + groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE); + groupDefinition.setArtifacts(artifactsList); + groupDefinition.setArtifactsUuid(artifactsUUIDList); + + if (!members.isEmpty()) + groupDefinition.setMembers(members); + List properties = new ArrayList(); + GroupProperty prop = new GroupProperty(); + prop.setName(Constants.IS_BASE); + prop.setValue(Boolean.toString(groupTemplateInfo.isBase())); + properties.add(prop); + Either getLatestGroupTypeRes = groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, true); + if (getLatestGroupTypeRes.isRight()) { + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getLatestGroupTypeRes.right().value()))); + } + properties = createVfModuleAdditionalProperties(groupTemplateInfo.isBase(), groupName, properties, createdArtifacts, artifactsList, getLatestGroupTypeRes.left().value()); + groupDefinition.convertFromGroupProperties(properties); + log.debug("createGroup start"); + + needToCreate.add(groupDefinition); } - properties = createVfModuleAdditionalProperties(groupTemplateInfo.isBase(), groupName, properties, createdArtifacts, artifactsList, getLatestGroupTypeRes.left().value()); - groupDefinition.convertFromGroupProperties(properties); - log.debug("createGroup start"); - - // Since in these groups we handle only artifacts, then no need to - // fetch component instances - - // Either createGroup = groupBusinessLogic.createGroup(comp, user, ComponentTypeEnum.RESOURCE, groupDefinition, inTransaction); - // log.debug("createGroup end"); - // if (createGroup.isRight()) - // return Either.right(createGroup.right().value()); - needToCreate.add(groupDefinition); } ComponentParametersView componentParametersView = new ComponentParametersView(); @@ -3497,7 +3532,8 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName)); } - allGroups.addAll(artifactTemplateInfoList); + if(!artifactsTypeKey.equalsIgnoreCase(ArtifactTemplateInfo.CSAR_ARTIFACT)) + allGroups.addAll(artifactTemplateInfoList); artifactsMap.put(artifactsTypeKey, artifactTemplateInfoList); } int counter = groupBusinessLogic.getNextVfModuleNameCounter(resource.getGroups()); @@ -3603,10 +3639,40 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } else { originResource = originCompMap.get(currentCompInstance.getComponentUid()); } - if (originResource.getCapabilities() != null && !originResource.getCapabilities().isEmpty()) - instCapabilties.put(currentCompInstance, originResource.getCapabilities()); if (originResource.getRequirements() != null && !originResource.getRequirements().isEmpty()) instRequirements.put(currentCompInstance, originResource.getRequirements()); + if (MapUtils.isNotEmpty(originResource.getCapabilities())) { + Map> originCapabilities ; + if (MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) { + originCapabilities = new HashMap<>(); + originResource.getCapabilities().entrySet().stream().forEach(e ->{ + List list = e.getValue().stream().map(l -> new CapabilityDefinition(l)).collect(Collectors.toList()); + originCapabilities.put(e.getKey(), list); + }); + Map> newPropertiesMap = new HashMap<>(); + for(List capabilities : uploadComponentInstanceInfo.getCapabilities().values()){ + for(UploadCapInfo capability :capabilities){ + if(CollectionUtils.isNotEmpty(capability.getProperties())){ + newPropertiesMap.put(capability.getName(), capability.getProperties().stream().collect(Collectors.toMap(p->p.getName(), p->p))); + } + } + } + for (List capabilities : originCapabilities.values()) { + List filteredCapabilities = capabilities.stream().filter(c -> newPropertiesMap.containsKey(c.getName())).collect(Collectors.toList()); + for(CapabilityDefinition cap : filteredCapabilities){ + Either updateRes = updatePropertyValues(cap.getProperties(),newPropertiesMap.get(cap.getName()), allDataTypes.left().value()); + if(updateRes.isRight()){ + log.debug("Failed to update capability properties of capability {} . Status is {}. ", cap.getName(), updateRes.right().value()); + return Either.right(updateRes.right().value()); + } + } + } + } + else{ + originCapabilities = originResource.getCapabilities(); + } + instCapabilties.put(currentCompInstance, originCapabilities); + } if (originResource.getDeploymentArtifacts() != null && !originResource.getDeploymentArtifacts().isEmpty()) instDeploymentArtifacts.put(resourceInstanceId, originResource.getDeploymentArtifacts()); if (originResource.getArtifacts() != null && !originResource.getArtifacts().isEmpty()) @@ -3619,14 +3685,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return Either.right(addPropertiesValueToRiRes); } } else { - Either genericResourceEither = fetchAndSetDerivedFromGenericType(originResource); - if (genericResourceEither.isRight()) { - return genericResourceEither; - } - log.trace("************* Going to add inputs from from original resource {} to resource instance. ", originResource.getName()); - if (originResource.shouldGenerateInputs()) - generateInputsFromGenericTypeProperties(originResource, genericResourceEither.left().value()); - ResponseFormat addInputValueToRiRes = addInputsValuesToRi(uploadComponentInstanceInfo, resource, originResource, currentCompInstance, yamlName, instInputs, allDataTypes.left().value()); if (addInputValueToRiRes.getStatus() != 200) { return Either.right(addInputValueToRiRes); @@ -3725,25 +3783,54 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return Either.right(responseFormat); } + if(resource.getResourceType() == ResourceTypeEnum.CVFC){ + eitherGetResource = toscaOperationFacade.getToscaFullElement(resource.getUniqueId()); + if (eitherGetResource.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource); + return Either.right(responseFormat); + } + eitherGetResource = updateCalculatedCapReqWithSubstitutionMappings(eitherGetResource.left().value(), uploadResInstancesMap); + if (eitherGetResource.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource); + return Either.right(responseFormat); + } + } + log.debug("************* in create relations, getResource start"); - eitherGetResource = toscaOperationFacade.getToscaElement(resource.getUniqueId()); log.debug("************* in create relations, getResource end"); if (eitherGetResource.isRight()) { ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource); - return Either.right(responseFormat); - } - resource = eitherGetResource.left().value(); - if(resource.getResourceType() == ResourceTypeEnum.CVFC){ - eitherGetResource = updateCalculatedCapReqWithSubstitutionMappings(resource, uploadResInstancesMap); - if (eitherGetResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource); - return Either.right(responseFormat); + return Either.left(eitherGetResource.left().value()); + } + + private Either updatePropertyValues(List properties, Map newProperties, Map allDataTypes) { + for(ComponentInstanceProperty property : properties){ + Either updateRes = updatePropertyValue(property ,newProperties.get(property.getName()), allDataTypes); + if(updateRes.isRight()){ + log.debug("Failed to update capability property {} . Status is {}. ", property.getName(), updateRes.right().value()); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateRes.right().value()))); } } - return Either.left(eitherGetResource.left().value()); + return Either.left(true); + } + + private Either updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo, Map allDataTypes) { + String value = null; + List getInputs = null; + boolean isValidate = true; + if (propertyInfo.getValue() != null) { + getInputs = propertyInfo.getGet_input(); + isValidate = getInputs == null || getInputs.isEmpty(); + if (isValidate) { + value = ImportUtils.getPropertyJsonStringValue(propertyInfo.getValue(), property.getType()); + } else + value = ImportUtils.getPropertyJsonStringValue(propertyInfo.getValue(), ToscaTagNamesEnum.GET_INPUT.getElementName()); + } + property.setValue(value); + return validatePropValueBeforeCreate(property, value, isValidate, null, allDataTypes); } private Either updateCalculatedCapReqWithSubstitutionMappings(Resource resource, Map uploadResInstancesMap) { @@ -3764,7 +3851,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } } if(updateRes == null){ - updateRes = toscaOperationFacade.getToscaElement( resource.getUniqueId()); + updateRes = Either.left(resource); } return updateRes; } @@ -4011,25 +4098,28 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } private ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource, ComponentInstance currentCompInstance, String yamlName, - Map> instProperties, Map allDataTypes) { + Map> instProperties, Map allDataTypes) { Map> propMap = uploadComponentInstanceInfo.getProperties(); - if (propMap != null && propMap.size() > 0) { - Map currPropertiesMap = new HashMap(); + Map currPropertiesMap = new HashMap(); - List listFromMap = originResource.getProperties(); - if (listFromMap == null || listFromMap.isEmpty()) { - log.debug("failed to find properties "); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND); - return responseFormat; - } - for (PropertyDefinition prop : listFromMap) { - String propName = prop.getName(); - if (!currPropertiesMap.containsKey(propName)) { - currPropertiesMap.put(propName, prop); - } + List listFromMap = originResource.getProperties(); + if ((propMap != null && !propMap.isEmpty()) && (listFromMap == null || listFromMap.isEmpty())) { + log.debug("failed to find properties "); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND); + return responseFormat; + } + if(listFromMap == null || listFromMap.isEmpty()){ + return componentsUtils.getResponseFormat(ActionStatus.OK); + } + for (PropertyDefinition prop : listFromMap) { + String propName = prop.getName(); + if (!currPropertiesMap.containsKey(propName)) { + currPropertiesMap.put(propName, prop); } - List instPropList = new ArrayList<>(); + } + List instPropList = new ArrayList<>(); + if (propMap != null && propMap.size() > 0) { for (List propertyList : propMap.values()) { UploadPropInfo propertyInfo = propertyList.get(0); @@ -4105,14 +4195,14 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { // delete overriden property currPropertiesMap.remove(property.getName()); } - // add rest of properties - if (!currPropertiesMap.isEmpty()) { - for (PropertyDefinition value : currPropertiesMap.values()) { - instPropList.add(new ComponentInstanceProperty(value)); - } + } + // add rest of properties + if (!currPropertiesMap.isEmpty()) { + for (PropertyDefinition value : currPropertiesMap.values()) { + instPropList.add(new ComponentInstanceProperty(value)); } - instProperties.put(currentCompInstance.getUniqueId(), instPropList); } + instProperties.put(currentCompInstance.getUniqueId(), instPropList); return componentsUtils.getResponseFormat(ActionStatus.OK); } @@ -4357,9 +4447,10 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { ComponentTypeEnum containerComponentType = resource.getComponentType(); NodeTypeEnum containerNodeType = containerComponentType.getNodeType(); - - if (containerNodeType.equals(NodeTypeEnum.Resource) && uploadComponentInstanceInfo.getCapabilities() != null) { - Either>, ResponseFormat> getValidComponentInstanceCapabilitiesRes = getValidComponentInstanceCapabilities(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities()); + + if (containerNodeType.equals(NodeTypeEnum.Resource) && MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) && MapUtils.isNotEmpty(refResource.getCapabilities())) { + setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities()); + Either>, ResponseFormat> getValidComponentInstanceCapabilitiesRes = getValidComponentInstanceCapabilities(refResource.getUniqueId(), refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities()); if (getValidComponentInstanceCapabilitiesRes.isRight()) { return Either.right(getValidComponentInstanceCapabilitiesRes.right().value()); } else { @@ -4418,6 +4509,18 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return Either.left(eitherGerResource.left().value()); } + private void setCapabilityNamesTypes(Map> originCapabilities, Map> uploadedCapabilities) { + for(Entry> currEntry : uploadedCapabilities.entrySet()){ + if(originCapabilities.containsKey(currEntry.getKey())){ + currEntry.getValue().stream().forEach(cap -> cap.setType(currEntry.getKey())); + } + } + for(Map.Entry> capabilities : originCapabilities.entrySet()){ + capabilities.getValue().stream().forEach(cap -> {if(uploadedCapabilities.containsKey(cap.getName())){uploadedCapabilities.get(cap.getName()).stream().forEach(c -> {c.setName(cap.getName());c.setType(cap.getType());});};}); + } + + } + private Either validateResourceInstanceBeforeCreate(String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo, Map nodeNamespaceMap) { log.debug("validateResourceInstanceBeforeCreate - going to validate resource instance with name {} and type before create", uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); Resource refResource = null; @@ -4440,7 +4543,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return Either.right(responseFormat); } - if (!ToscaUtils.isAtomicType(refResource) && refResource.getResourceType() != ResourceTypeEnum.CVFC) { + if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.CVFC) { log.debug("validateResourceInstanceBeforeCreate - ref resource type is ", refResource.getResourceType()); ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); return Either.right(responseFormat); @@ -4773,57 +4876,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return result; } - - @SuppressWarnings("unchecked") - private Either>, ResponseFormat> createCapModuleFromYaml(UploadComponentInstanceInfo nodeTemplateInfo, Map nodeTemplateJsonMap) { - Map> moduleCap = new HashMap<>(); - Either>, ResponseFormat> response = Either.left(moduleCap); - Either, ResultStatusEnum> capabilitiesListRes = ImportUtils.findFirstToscaListElement(nodeTemplateJsonMap, ToscaTagNamesEnum.CAPABILITIES); - if (capabilitiesListRes.isLeft()) { - for (Object jsonCapObj : capabilitiesListRes.left().value()) { - String key = ((Map) jsonCapObj).keySet().iterator().next(); - Object capJson = ((Map) jsonCapObj).get(key); - Either eitherCap = addModuleNodeTemplateCap(nodeTemplateInfo, moduleCap, capJson, key); - if (eitherCap.isRight()) { - return Either.right(eitherCap.right().value()); - } - } - } else { - Either, ResultStatusEnum> capabilitiesMapRes = ImportUtils.findFirstToscaMapElement(nodeTemplateJsonMap, ToscaTagNamesEnum.CAPABILITIES); - if (capabilitiesMapRes.isLeft()) { - for (Map.Entry entry : capabilitiesMapRes.left().value().entrySet()) { - String capName = entry.getKey(); - Object capJson = entry.getValue(); - Either eitherCap = addModuleNodeTemplateCap(nodeTemplateInfo, moduleCap, capJson, capName); - if (eitherCap.isRight()) { - return Either.right(eitherCap.right().value()); - } - } - } - } - return response; - } - - private Either addModuleNodeTemplateCap(UploadComponentInstanceInfo nodeTemplateInfo, Map> moduleCap, Object capJson, String key) { - - Either eitherCap = createModuleNodeTemplateCap(capJson); - if (eitherCap.isRight()) { - log.info("error when creating Capability:{}, for node:{}", key, nodeTemplateInfo); - return Either.right(eitherCap.right().value()); - } else { - UploadCapInfo capabilityDef = eitherCap.left().value(); - capabilityDef.setKey(key); - if (moduleCap.containsKey(key)) { - moduleCap.get(key).add(capabilityDef); - } else { - List list = new ArrayList(); - list.add(capabilityDef); - moduleCap.put(key, list); - } - } - return Either.left( eitherCap.left().value()); - } - @SuppressWarnings("unchecked") private Either>, ResponseFormat> createReqModuleFromYaml(UploadComponentInstanceInfo nodeTemplateInfo, Map nodeTemplateJsonMap) { Map> moduleRequirements = new HashMap>(); @@ -4854,9 +4906,9 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } return response; } - - private Either addModuleNodeTemplateReq(UploadComponentInstanceInfo nodeTemplateInfo, Map> moduleRequirements, Object requirementJson, String requirementName) { + private Either addModuleNodeTemplateReq(UploadComponentInstanceInfo nodeTemplateInfo,Map> moduleRequirements, Object requirementJson, String requirementName) { + Either eitherRequirement = createModuleNodeTemplateReg(requirementJson); if (eitherRequirement.isRight()) { log.info("error when creating Requirement:{}, for node:{}", requirementName, nodeTemplateInfo); @@ -4875,6 +4927,56 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return Either.left(eitherRequirement.left().value()); } + @SuppressWarnings("unchecked") + private Either>, ResponseFormat> createCapModuleFromYaml(UploadComponentInstanceInfo nodeTemplateInfo, Map nodeTemplateJsonMap) { + Map> moduleCap = new HashMap<>(); + Either>, ResponseFormat> response = Either.left(moduleCap); + Either, ResultStatusEnum> capabilitiesListRes = ImportUtils.findFirstToscaListElement(nodeTemplateJsonMap, ToscaTagNamesEnum.CAPABILITIES); + if (capabilitiesListRes.isLeft()) { + for (Object jsonCapObj : capabilitiesListRes.left().value()) { + String key = ((Map) jsonCapObj).keySet().iterator().next(); + Object capJson = ((Map) jsonCapObj).get(key); + Either eitherCap = addModuleNodeTemplateCap(nodeTemplateInfo, moduleCap, capJson, key); + if (eitherCap.isRight()) { + return Either.right(eitherCap.right().value()); + } + } + } else { + Either, ResultStatusEnum> capabilitiesMapRes = ImportUtils.findFirstToscaMapElement(nodeTemplateJsonMap, ToscaTagNamesEnum.CAPABILITIES); + if (capabilitiesMapRes.isLeft()) { + for (Map.Entry entry: capabilitiesMapRes.left().value().entrySet()) { + String capName = entry.getKey(); + Object capJson = entry.getValue(); + Either eitherCap = addModuleNodeTemplateCap(nodeTemplateInfo, moduleCap, capJson, capName); + if (eitherCap.isRight()) { + return Either.right(eitherCap.right().value()); + } + } + } + } + return response; + } + + private Either addModuleNodeTemplateCap(UploadComponentInstanceInfo nodeTemplateInfo, Map> moduleCap, Object capJson, String key) { + + Either eitherCap = createModuleNodeTemplateCap(capJson); + if (eitherCap.isRight()) { + log.info("error when creating Capability:{}, for node:{}", key, nodeTemplateInfo); + return Either.right(eitherCap.right().value()); + } else { + UploadCapInfo capabilityDef = eitherCap.left().value(); + capabilityDef.setKey(key); + if (moduleCap.containsKey(key)) { + moduleCap.get(key).add(capabilityDef); + } else { + List list = new ArrayList(); + list.add(capabilityDef); + moduleCap.put(key, list); + } + } + return Either.left( eitherCap.left().value()); + } + @SuppressWarnings("unchecked") private Either createModuleNodeTemplateCap(Object capObject) { UploadCapInfo capTemplateInfo = new UploadCapInfo(); @@ -5087,6 +5189,11 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { result = Either.right(validateFieldsResponse.right().value()); return result; } + + validateFieldsResponse = validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), newResource, AuditingActionEnum.IMPORT_RESOURCE, inTransaction); + if (validateFieldsResponse.isRight()) { + return Either.right(validateFieldsResponse.right().value()); + } // contact info normalization newResource.setContactId(newResource.getContactId().toLowerCase()); @@ -5223,7 +5330,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { resource.setCreatorUserId(user.getUserId()); resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); resource.setContactId(resource.getContactId().toLowerCase()); - if (StringUtils.isEmpty(resource.getToscaResourceName()) && !ToscaUtils.isAtomicType(resource)) { + if (StringUtils.isEmpty(resource.getToscaResourceName()) && !ModelConverter.isAtomicComponent(resource)) { String resourceSystemName; if(csarInfo != null && StringUtils.isNotEmpty(csarInfo.getVfResourceName())){ resourceSystemName = ValidationUtils.convertToSystemName(csarInfo.getVfResourceName()); @@ -5392,11 +5499,9 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } try { if (resource.deriveFromGeneric()) { - Either genericResourceEither = fetchAndSetDerivedFromGenericType(resource); + Either genericResourceEither = handleResourceGenericType(resource); if (genericResourceEither.isRight()) return genericResourceEither; - if (resource.shouldGenerateInputs()) - generateInputsFromGenericTypeProperties(resource, genericResourceEither.left().value()); } Either respStatus = createResourceTransaction(resource, user, isNormative, inTransaction); @@ -5761,7 +5866,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { // list // This code is not called from import resources, because of root // VF "derivedFrom" should be null (or ignored) - if (ToscaUtils.isAtomicType(currentResource)) { + if (ModelConverter.isAtomicComponent(currentResource)) { Either derivedFromNotEmptyEither = validateDerivedFromNotEmpty(null, newResource, null); if (derivedFromNotEmptyEither.isRight()) { log.debug("for updated resource {}, derived from field is empty", newResource.getName()); @@ -5975,7 +6080,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { // validate template (derived from) log.debug("validate derived from"); - if (!ToscaUtils.isAtomicType(resource) && resource.getResourceType() != ResourceTypeEnum.CVFC) { + if (!ModelConverter.isAtomicComponent(resource) && resource.getResourceType() != ResourceTypeEnum.CVFC) { resource.setDerivedFrom(null); } eitherValidation = validateDerivedFromExist(user, resource, actionEnum); @@ -6195,10 +6300,23 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { * return Either.left(true); } */ - private Either validateResourceName(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) { + private boolean isResourceNameEquals(Resource currentResource, Resource updateInfoResource) { String resourceNameUpdated = updateInfoResource.getName(); String resourceNameCurrent = currentResource.getName(); - if (!resourceNameCurrent.equals(resourceNameUpdated)) { + if (resourceNameCurrent.equals(resourceNameUpdated)) + return true; + // In case of CVFC type we should support the case of old VF with CVFC instances that were created without the "Cvfc" suffix + return (currentResource.getResourceType().equals(ResourceTypeEnum.CVFC) + && resourceNameUpdated.equals(addCvfcSuffixToResourceName(resourceNameCurrent))); + } + + private String addCvfcSuffixToResourceName(String resourceName) { + return resourceName+"Cvfc"; + } + + private Either validateResourceName(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) { + String resourceNameUpdated = updateInfoResource.getName(); + if (!isResourceNameEquals(currentResource, updateInfoResource)) { if (!hasBeenCertified) { Either validateResourceNameResponse = validateComponentName(null, updateInfoResource, null); if (validateResourceNameResponse.isRight()) { @@ -7058,7 +7176,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return null; } - private Either>, ResponseFormat> getValidComponentInstanceCapabilities(Map> defaultCapabilities, Map> uploadedCapabilities) { + private Either>, ResponseFormat> getValidComponentInstanceCapabilities(String resourceId, Map> defaultCapabilities, Map> uploadedCapabilities) { ResponseFormat responseFormat; Map> validCapabilitiesMap = new HashMap<>(); @@ -7068,14 +7186,33 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, capabilityType); return Either.right(responseFormat); } else { - CapabilityDefinition delaultCapability = defaultCapabilities.get(capabilityType).get(0); - Either validationRes = validateUniquenessUpdateUploadedComponentInstanceCapability(delaultCapability, uploadedCapabilitiesEntry.getValue().get(0)); - if (validationRes.isRight()) { - responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NAME_ALREADY_EXISTS, validationRes.right().value()); + CapabilityDefinition defaultCapability; + if(CollectionUtils.isNotEmpty(defaultCapabilities.get(capabilityType).get(0).getProperties())){ + defaultCapability = defaultCapabilities.get(capabilityType).get(0); + } else { + Either getFullComponentRes = toscaOperationFacade.getToscaFullElement(resourceId); + if(getFullComponentRes.isRight()){ + log.debug("Failed to get full component {}. Status is {}. ", resourceId, getFullComponentRes.right().value()); + responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_FOUND, resourceId); + return Either.right(responseFormat); + } + defaultCapability = getFullComponentRes.left().value().getCapabilities().get(capabilityType).get(0); + } + if(CollectionUtils.isEmpty(defaultCapability.getProperties()) && CollectionUtils.isNotEmpty(uploadedCapabilitiesEntry.getValue().get(0).getProperties())){ + log.debug("Failed to validate capability {} of component {}. Property list is empty. ", defaultCapability.getName(), resourceId); + log.debug("Failed to update capability property values. Property list of fetched capability {} is empty. ", defaultCapability.getName()); + responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, resourceId); return Either.right(responseFormat); } + if(CollectionUtils.isNotEmpty(defaultCapability.getProperties()) && CollectionUtils.isNotEmpty(uploadedCapabilitiesEntry.getValue().get(0).getProperties())){ + Either validationRes = validateUniquenessUpdateUploadedComponentInstanceCapability(defaultCapability, uploadedCapabilitiesEntry.getValue().get(0)); + if (validationRes.isRight()) { + responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NAME_ALREADY_EXISTS, validationRes.right().value()); + return Either.right(responseFormat); + } + } List validCapabilityList = new ArrayList<>(); - validCapabilityList.add(delaultCapability); + validCapabilityList.add(defaultCapability); validCapabilitiesMap.put(uploadedCapabilitiesEntry.getKey(), validCapabilityList); } } @@ -7225,5 +7362,20 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { UiComponentDataTransfer dataTransfer = UiComponentDataConverter.getUiDataTransferFromResourceByParams(resource, dataParamsToReturn); return Either.left(dataTransfer); } + @Override + public Either shouldUpgradeToLatestDerived(Component clonedComponent) { + Resource resource = (Resource) clonedComponent; + if (ModelConverter.isAtomicComponent(resource.getResourceType())) { + Either shouldUpgradeToLatestDerived = toscaOperationFacade.shouldUpgradeToLatestDerived(resource); + if (shouldUpgradeToLatestDerived.isRight()) { + return Either.right(componentsUtils.convertFromStorageResponse(shouldUpgradeToLatestDerived.right().value())); + } + return Either.left(shouldUpgradeToLatestDerived.left().value()); + } else { + return super.shouldUpgradeToLatestDerived(clonedComponent); + } + } + + }