diff options
Diffstat (limited to 'catalog-be')
45 files changed, 14222 insertions, 109 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarArtifactsAndGroupsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarArtifactsAndGroupsBusinessLogic.java index 697d6faecb..c34a006b6c 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarArtifactsAndGroupsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarArtifactsAndGroupsBusinessLogic.java @@ -71,6 +71,7 @@ import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.info.ArtifactTemplateInfo; import org.openecomp.sdc.be.info.MergedArtifactInfo; import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentParametersView; import org.openecomp.sdc.be.model.GroupDefinition; import org.openecomp.sdc.be.model.GroupProperty; @@ -79,6 +80,7 @@ import org.openecomp.sdc.be.model.HeatParameterDefinition; import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.heat.HeatParameterType; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; @@ -137,21 +139,48 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { .right() .map(rf -> { componentsUtils.auditResource(rf, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE); return rf;}) .left() - .bind(this::getResourcetFromGraph); + .bind(c -> checkoutRes(c)); } + private Either<Resource, ResponseFormat> checkoutRes(Component component) { + Either<? extends Component, ResponseFormat> checkoutRes = getResourcetFromGraph(component); + Resource resource1 = (Resource) checkoutRes.left().value(); + Either<Resource, ResponseFormat> checkoutRes1 = Either.left(resource1); + return checkoutRes1; + } + + public Either<Service, ResponseFormat> createResourceArtifactsFromCsar(CsarInfo csarInfo, Service resource, + String artifactsMetaFile, String artifactsMetaFileName, List<ArtifactDefinition> createdArtifacts + ) { - public Either<Resource, ResponseFormat> updateResourceArtifactsFromCsar(CsarInfo csarInfo, Resource resource, + log.debug("parseResourceArtifactsInfoFromFile start"); + return parseResourceArtifactsInfoFromFile(resource, artifactsMetaFile, artifactsMetaFileName) + .left() + .bind(p -> createResourceArtifacts(csarInfo, resource, p, createdArtifacts)) + .right() + .map(rf -> rf) + .left() + .bind(c -> { + Either<? extends Component, ResponseFormat> checkoutRes = getResourcetFromGraph(c); + Service resource1 = (Service) checkoutRes.left().value(); + Either<Service, ResponseFormat> checkoutRes1 = Either.left(resource1); + return checkoutRes1; + }); + } + + public Either<Component, ResponseFormat> updateResourceArtifactsFromCsar(CsarInfo csarInfo, Component resource, String artifactsMetaFile, String artifactsMetaFileName, List<ArtifactDefinition> createdNewArtifacts, boolean shouldLock, boolean inTransaction){ - Resource updatedResource = resource; + Component updatedResource = resource; Either<Map<String, List<ArtifactTemplateInfo>>, ResponseFormat> parseResourceInfoFromYamlEither = parseResourceArtifactsInfoFromFile( updatedResource, artifactsMetaFile, artifactsMetaFileName); if (parseResourceInfoFromYamlEither.isRight()) { ResponseFormat responseFormat = parseResourceInfoFromYamlEither.right().value(); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE); + if (resource instanceof Resource) { + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), (Resource) resource, AuditingActionEnum.IMPORT_RESOURCE); + } return Either.right(responseFormat); } @@ -183,8 +212,8 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { Collection<List<ArtifactTemplateInfo>> parsedArifactsCollection = parsedArtifactsMap.values(); Either<Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>>, ResponseFormat> parsedArtifactsPerGroupEither = createArtifactsTemplateCollection(csarInfo, updatedResource, createdNewArtifacts, - createdDeploymentArtifactsAfterDelete, labelCounter, parsedArifactsCollection); - if(parsedArtifactsPerGroupEither.isRight()){ + createdDeploymentArtifactsAfterDelete, labelCounter, parsedArifactsCollection); + if (parsedArtifactsPerGroupEither.isRight()) { log.error("Failed to parse artifacts. Status is {} ", parsedArtifactsPerGroupEither.right().value()); return Either.right(parsedArtifactsPerGroupEither.right().value()); } @@ -207,7 +236,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { artifactsToDelete, groupToDelete, jsonMasterArtifacts, createdDeploymentArtifactsAfterDelete); List<ArtifactDefinition> deletedArtifacts = new ArrayList<>(); - Either<Resource, ResponseFormat> deletedArtifactsEither = deleteArtifactsInUpdateCsarFlow( + Either<Component, ResponseFormat> deletedArtifactsEither = deleteArtifactsInUpdateCsarFlow( updatedResource, csarInfo.getModifier(), shouldLock, inTransaction, artifactsToDelete, groupToDelete, deletedArtifacts); if (deletedArtifactsEither.isRight()) { log.debug("Failed to delete artifacts. Status is {} ", deletedArtifactsEither.right().value()); @@ -222,7 +251,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { ////////////// dissociate, associate or create ////////////// artifacts//////////////////////////// - Either<Resource, ResponseFormat> assDissotiateEither = associateAndDissociateArtifactsToGroup(csarInfo, + Either<Component, ResponseFormat> assDissotiateEither = associateAndDissociateArtifactsToGroup(csarInfo, updatedResource, createdNewArtifacts, labelCounter, createdDeploymentArtifactsAfterDelete, mergedgroup, deletedArtifacts); groups = updatedResource.getGroups(); @@ -239,37 +268,37 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { // update vfModule names Set<GroupDefinition> groupForAssociateWithMembers = mergedgroup.keySet(); - Either<Resource, ResponseFormat> validateUpdateVfGroupNamesRes = updateVfModuleNames(createdNewArtifacts, + Either<Component, ResponseFormat> validateUpdateVfGroupNamesRes = updateVfModuleNames(createdNewArtifacts, updatedResource, groups, createdDeploymentArtifactsAfterDelete, groupForAssociateWithMembers); if (validateUpdateVfGroupNamesRes != null) return validateUpdateVfGroupNamesRes; //////////////// create new artifacts in update //////////////// flow//////////////////////////// List<ArtifactTemplateInfo> newArtifactsGroup = createNewArtifcats(parsedGroup, groupArtifact); - Either<Resource, ResponseFormat> validateGroupNamesRes = handleArtifactsInGroup(csarInfo, createdNewArtifacts, + Either<Component, ResponseFormat> validateGroupNamesRes = handleArtifactsInGroup(csarInfo, createdNewArtifacts, updatedResource, groups, createdDeploymentArtifactsAfterDelete, labelCounter, newArtifactsGroup); if (validateGroupNamesRes != null) return validateGroupNamesRes; // updatedGroup - Either<Resource, ResponseFormat> updateVersionEither = updateGroupVersion(updatedResource, groupForAssociateWithMembers); + Either<Component, ResponseFormat> updateVersionEither = updateGroupVersion(updatedResource, groupForAssociateWithMembers); if (updateVersionEither != null) return updateVersionEither; if (!CollectionUtils.isEmpty(artifactsWithoutGroups)) { for (ArtifactTemplateInfo t : artifactsWithoutGroups) { List<ArtifactTemplateInfo> artifacts = new ArrayList<>(); artifacts.add(t); - Either<Resource, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, updatedResource, + Either<Component, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, updatedResource, artifacts, createdNewArtifacts, createdDeploymentArtifactsAfterDelete, labelCounter); if (checkResponse(resStatus)) return resStatus; } } - Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(updatedResource.getUniqueId()); + Either<Component, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(updatedResource.getUniqueId()); return mapResult(eitherGetResource, updatedResource); } - private Either<Resource, ResponseFormat> handleArtifactsInGroup(CsarInfo csarInfo, List<ArtifactDefinition> createdNewArtifacts, - Resource updatedResource, List<GroupDefinition> groups, + private Either<Component, ResponseFormat> handleArtifactsInGroup(CsarInfo csarInfo, List<ArtifactDefinition> createdNewArtifacts, + Component updatedResource, List<GroupDefinition> groups, List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete, int labelCounter, List<ArtifactTemplateInfo> newArtifactsGroup) { if (!newArtifactsGroup.isEmpty()) { @@ -280,18 +309,18 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { if (validateGroupNamesRes.isRight()) { return Either.right(validateGroupNamesRes.right().value()); } - Either<Resource, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, updatedResource, + Either<Component, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, updatedResource, newArtifactsGroup, createdNewArtifacts, createdDeploymentArtifactsAfterDelete, labelCounter); checkResponse(resStatus); } return null; } - private boolean checkResponse(Either<Resource, ResponseFormat> resStatus) { + private boolean checkResponse(Either<Component, ResponseFormat> resStatus) { return (resStatus.isRight()); } - private Either<Resource, ResponseFormat> updateVfModuleNames(List<ArtifactDefinition> createdNewArtifacts, Resource updatedResource, List<GroupDefinition> groups, List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete, Set<GroupDefinition> groupForAssociateWithMembers) { + private Either<Component, ResponseFormat> updateVfModuleNames(List<ArtifactDefinition> createdNewArtifacts, Component updatedResource, List<GroupDefinition> groups, List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete, Set<GroupDefinition> groupForAssociateWithMembers) { if (!CollectionUtils.isEmpty(groups)) { Either<List<GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic .validateUpdateVfGroupNamesOnGraph(groups, updatedResource); @@ -303,7 +332,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { return null; } - private Either<Resource, ResponseFormat> updateGroupVersion(Resource updatedResource, Set<GroupDefinition> groupForAssociateWithMembers) { + private Either<Component, ResponseFormat> updateGroupVersion(Component updatedResource, Set<GroupDefinition> groupForAssociateWithMembers) { if (!groupForAssociateWithMembers.isEmpty()) { List<GroupDefinition> groupsId = groupForAssociateWithMembers.stream() @@ -341,7 +370,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { return Character.isDigit(artifactLabel.charAt(artifactLabel.length()-1)); } - private Either<Resource, ResponseFormat> mapResult(Either<Resource, StorageOperationStatus> result, Resource resource) { + private Either<Component, ResponseFormat> mapResult(Either<Component, StorageOperationStatus> result, Component resource) { return result.right() .map(status -> componentsUtils.getResponseFormatByResource( componentsUtils.convertFromStorageResponse(status), resource)); @@ -396,7 +425,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { } - private void deleteGroupsByType(List<GroupDefinition> groups, String groupType, Resource resource) { + private void deleteGroupsByType(List<GroupDefinition> groups, String groupType, Component resource) { if(groups != null){ List<GroupDefinition> listToDelete = groups.stream() .filter(g -> g.getType().equals(groupType)) @@ -452,11 +481,11 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { } - private Either<Resource, ResponseFormat> createResourceArtifacts(CsarInfo csarInfo, Resource resource, + private Either<Component, ResponseFormat> createResourceArtifacts(CsarInfo csarInfo, Component resource, Map<String, List<ArtifactTemplateInfo>> artifactsMap, List<ArtifactDefinition> createdArtifacts) { - Either<Resource, ResponseFormat> resStatus = Either.left(resource); + Either<Component, ResponseFormat> resStatus = Either.left(resource); Collection<List<ArtifactTemplateInfo>> arifactsCollection = artifactsMap.values(); @@ -476,7 +505,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { private Either<Map<ArtifactTemplateInfo, Set<ArtifactTemplateInfo>>, ResponseFormat> createArtifactsTemplateCollection( - CsarInfo csarInfo, Resource resource, + CsarInfo csarInfo, Component resource, List<ArtifactDefinition> createdNewArtifacts, List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete, int labelCounter, Collection<List<ArtifactTemplateInfo>> parsedArifactsCollection) { @@ -498,7 +527,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { } else { List<ArtifactTemplateInfo> arrtifacts = new ArrayList<>(); arrtifacts.add(parsedGroupTemplate); - Either<Resource, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, + Either<Component, ResponseFormat> resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, resource, arrtifacts, createdNewArtifacts, createdDeploymentArtifactsAfterDelete, labelCounter); if (resStatus.isRight()) { @@ -514,7 +543,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { @SuppressWarnings({ "unchecked", "static-access" }) public Either<Map<String, List<ArtifactTemplateInfo>>, ResponseFormat> parseResourceArtifactsInfoFromFile( - Resource resource, String artifactsMetaFile, String artifactFileName) { + Component resource, String artifactsMetaFile, String artifactFileName) { try { JsonObject jsonElement = new JsonObject(); @@ -622,8 +651,8 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { - private Either<Resource, ResponseFormat> createGroupDeploymentArtifactsFromCsar(CsarInfo csarInfo, - Resource resource, List<ArtifactTemplateInfo> artifactsTemplateList, + private Either<Component, ResponseFormat> createGroupDeploymentArtifactsFromCsar(CsarInfo csarInfo, + Component resource, List<ArtifactTemplateInfo> artifactsTemplateList, List<ArtifactDefinition> createdArtifacts, int labelCounter) { List<GroupDefinition> createdGroups = resource.getGroups(); List<GroupDefinition> heatGroups = null; @@ -637,7 +666,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { Set<String> artifactsUUIDGroup = new HashSet<>(); log.debug("createDeploymentArtifactsFromCsar start"); - Either<Resource, ResponseFormat> resStatus = createDeploymentArtifactFromCsar(csarInfo, ARTIFACTS_PATH, resource, artifactsGroup, + Either<Component, ResponseFormat> resStatus = createDeploymentArtifactFromCsar(csarInfo, ARTIFACTS_PATH, resource, artifactsGroup, artifactsUUIDGroup, groupTemplateInfo, createdArtifacts, labelCounter); log.debug("createDeploymentArtifactsFromCsar end"); if (resStatus.isRight()) { @@ -707,10 +736,10 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { return Either.left(groupDefinition); } - private Either<Resource, ResponseFormat> createDeploymentArtifactFromCsar(CsarInfo csarInfo, String artifactPath, - Resource resource, Set<String> artifactsGroup, Set<String> artifactsUUIDGroup, + private Either<Component, ResponseFormat> createDeploymentArtifactFromCsar(CsarInfo csarInfo, String artifactPath, + Component resource, Set<String> artifactsGroup, Set<String> artifactsUUIDGroup, ArtifactTemplateInfo artifactTemplateInfo, List<ArtifactDefinition> createdArtifacts, int labelCounter) { - Either<Resource, ResponseFormat> resStatus = Either.left(resource); + Either<Component, ResponseFormat> resStatus = Either.left(resource); String artifactUid = ""; String artifactEnvUid = ""; @@ -801,7 +830,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { } - private Either<ArtifactDefinition, ResponseFormat> createDeploymentArtifact(CsarInfo csarInfo, Resource resource, + private Either<ArtifactDefinition, ResponseFormat> createDeploymentArtifact(CsarInfo csarInfo, Component resource, String artifactPath, ArtifactTemplateInfo artifactTemplateInfo, List<ArtifactDefinition> createdArtifacts, int label) { int updatedlabel = label; @@ -847,10 +876,10 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { - private Either<ArtifactDefinition, ResponseFormat> updateHeatParamsFromCsar(Resource resource, CsarInfo csarInfo, + private Either<ArtifactDefinition, ResponseFormat> updateHeatParamsFromCsar(Component resource, CsarInfo csarInfo, ArtifactTemplateInfo artifactTemplateInfo, ArtifactDefinition currentInfo, boolean isUpdateEnv) { - Resource updatedResource = resource; + Component updatedResource = resource; Either<ArtifactDefinition, ResponseFormat> resStatus = Either.left(currentInfo); if (artifactTemplateInfo.getEnv() != null && !artifactTemplateInfo.getEnv().isEmpty()) { @@ -920,7 +949,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { } - private Either<ArtifactDefinition, ResponseFormat> updateHeatParams(Resource resource, + private Either<ArtifactDefinition, ResponseFormat> updateHeatParams(Component resource, ArtifactDefinition currentInfo, List<HeatParameterDefinition> updatedHeatEnvParams) { Either<ArtifactDefinition, ResponseFormat> resStatus = Either.left(currentInfo); @@ -973,7 +1002,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { } public Either<Either<ArtifactDefinition, Operation>, ResponseFormat> createOrUpdateCsarArtifactFromJson( - Resource resource, User user, Map<String, Object> json, ArtifactOperationInfo operation) { + Component resource, User user, Map<String, Object> json, ArtifactOperationInfo operation) { String jsonStr = gson.toJson(json); ArtifactDefinition artifactDefinitionFromJson = RepresentationUtils.convertJsonToArtifactDefinition(jsonStr, @@ -1231,11 +1260,11 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { return groupArtifact; } - private Either<Resource, ResponseFormat> deleteArtifactsInUpdateCsarFlow(Resource resource, + private Either<Component, ResponseFormat> deleteArtifactsInUpdateCsarFlow(Component resource, User user, boolean shouldLock, boolean inTransaction, Set<ArtifactDefinition> artifactsToDelete, Map<String, List<ArtifactDefinition>> groupToDelete, List<ArtifactDefinition> deletedArtifacts) { - Resource updatedResource = resource; + Component updatedResource = resource; String resourceId = updatedResource.getUniqueId(); if (!artifactsToDelete.isEmpty()) { @@ -1279,8 +1308,8 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { Either<Resource, StorageOperationStatus> eitherGerResource = toscaOperationFacade .getToscaElement(updatedResource.getUniqueId()); if (eitherGerResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource( - componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), updatedResource); + ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), updatedResource, resource.getComponentType()); return Either.right(responseFormat); @@ -1302,14 +1331,14 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { } } - private Either<Resource, ResponseFormat> createGroupDeploymentArtifactsFromCsar(CsarInfo csarInfo, - Resource resource, List<ArtifactTemplateInfo> artifactsTemplateList, + private Either<Component, ResponseFormat> createGroupDeploymentArtifactsFromCsar(CsarInfo csarInfo, + Component resource, List<ArtifactTemplateInfo> artifactsTemplateList, List<ArtifactDefinition> createdNewArtifacts, List<ArtifactDefinition> artifactsFromResource, int labelCounter) { - Resource updatedResource = resource; + Component updatedResource = resource; - Either<Resource, ResponseFormat> resStatus = Either.left(updatedResource); + Either<Component, ResponseFormat> resStatus = Either.left(updatedResource); List<GroupDefinition> createdGroups = updatedResource.getGroups(); List<GroupDefinition> heatGroups = null; if (createdGroups != null && !createdGroups.isEmpty()) { @@ -1388,10 +1417,10 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { return resStatus; } - private Either<Resource, ResponseFormat> createDeploymentArtifactsFromCsar(CsarInfo csarInfo, Resource resource, + private Either<Component, ResponseFormat> createDeploymentArtifactsFromCsar(CsarInfo csarInfo, Component resource, Set<String> artifactsGroup, Set<String> artifactsUUIDGroup, ArtifactTemplateInfo artifactTemplateInfo, List<ArtifactDefinition> createdArtifacts, List<ArtifactDefinition> artifactsFromResource, int labelCounter) { - Either<Resource, ResponseFormat> resStatus = Either.left(resource); + Either<Component, ResponseFormat> resStatus = Either.left(resource); String artifactFileName = artifactTemplateInfo.getFileName(); String artifactUid = ""; String artifactUUID = ""; @@ -1487,13 +1516,13 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { return resStatus; } - private Either<Resource, ResponseFormat> associateAndDissociateArtifactsToGroup(CsarInfo csarInfo, - Resource resource, List<ArtifactDefinition> createdNewArtifacts, int labelCounter, + private Either<Component, ResponseFormat> associateAndDissociateArtifactsToGroup(CsarInfo csarInfo, + Component resource, List<ArtifactDefinition> createdNewArtifacts, int labelCounter, List<ArtifactDefinition> createdDeploymentArtifactsAfterDelete, Map<GroupDefinition, MergedArtifactInfo> mergedgroup, List<ArtifactDefinition> deletedArtifacts) { Map<GroupDefinition, List<ArtifactTemplateInfo>> artifactsToAssotiate = new HashMap<>(); Map<GroupDefinition, List<ImmutablePair<ArtifactDefinition, ArtifactTemplateInfo>>> artifactsToUpdateMap = new HashMap<>(); - Either<Resource, ResponseFormat> resEither; + Either<Component, ResponseFormat> resEither; for (Entry<GroupDefinition, MergedArtifactInfo> entry : mergedgroup.entrySet()) { List<ArtifactDefinition> dissArtifactsInGroup = entry.getValue() .getListToDissotiateArtifactFromGroup(deletedArtifacts); @@ -1649,8 +1678,8 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { .getToscaElement(resource.getUniqueId(), parametersView); if (eitherGerResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource( - componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource); + ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource, resource.getComponentType()); resEither = Either.right(responseFormat); return resEither; @@ -1661,7 +1690,7 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { } private Either<ArtifactDefinition, ResponseFormat> updateDeploymentArtifactsFromCsar(CsarInfo csarInfo, - Resource resource, ArtifactDefinition oldArtifact, ArtifactTemplateInfo artifactTemplateInfo, + Component resource, ArtifactDefinition oldArtifact, ArtifactTemplateInfo artifactTemplateInfo, List<ArtifactDefinition> updatedArtifacts, List<ArtifactTemplateInfo> updatedRequiredArtifacts) { Either<ArtifactDefinition, ResponseFormat> resStatus = null; @@ -1763,15 +1792,57 @@ public class CsarArtifactsAndGroupsBusinessLogic extends BaseBusinessLogic { return Either.left(updatedResource); } - private Either<Resource, ResponseFormat> getResourcetFromGraph(Resource component){ + public Either<Service, ResponseFormat> deleteVFModules(Service resource, CsarInfo csarInfo, boolean shouldLock, boolean inTransaction) { + Service updatedResource = resource; + List<GroupDefinition> groupsToDelete = updatedResource.getGroups(); + if (groupsToDelete != null && !groupsToDelete.isEmpty()) { + List<GroupDefinition> vfGroupsToDelete = groupsToDelete.stream().filter(g -> g.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)).collect(Collectors.toList()); + Either<Service, ResponseFormat> eitherGetResource = deleteVfGroups(vfGroupsToDelete, updatedResource, csarInfo, shouldLock, inTransaction); + if (eitherGetResource.isRight()) { + return Either.right(eitherGetResource.right().value()); + } + updatedResource = eitherGetResource.left().value(); + } + return Either.left(updatedResource); + } + + private Either<Service, ResponseFormat> deleteVfGroups(List<GroupDefinition> vfGroupsToDelete, Service resource, CsarInfo csarInfo, boolean shouldLock, boolean inTransaction) { + ResponseFormat responseFormat; + if (vfGroupsToDelete != null && !vfGroupsToDelete.isEmpty()) { + for (GroupDefinition gr : vfGroupsToDelete) { + List<String> artifacts = gr.getArtifacts(); + for (String artifactId : artifacts) { + Either<ArtifactDefinition, ResponseFormat> handleDelete = artifactsBusinessLogic.handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), + resource, shouldLock, inTransaction); + if (handleDelete.isRight()) { + log.debug("Couldn't delete artifact {}", artifactId); + return Either.right(handleDelete.right().value()); + } + } + } + groupBusinessLogic.deleteGroups(resource, vfGroupsToDelete); + + Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(resource.getUniqueId()); + if (eitherGetResource.isRight()) { + responseFormat = componentsUtils.getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource, resource.getComponentType()); + + return Either.right(responseFormat); + } + resource = eitherGetResource.left().value(); + return Either.left(resource); + } + responseFormat = componentsUtils.getResponseFormatByComponent(ActionStatus.INVALID_CONTENT, resource, resource.getComponentType()); + return Either.right(responseFormat); + } + + + private Either<? extends Component, ResponseFormat> getResourcetFromGraph(Component component) { log.debug("getResource start"); return toscaOperationFacade.getToscaElement(component.getUniqueId()) .right() - .map(rf -> componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(rf), component)) + .map(rf -> componentsUtils.getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(rf), component, component.getComponentType())) .left() - .map (c -> (Resource) c); - - + .map(c -> c); } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogic.java index e16f08b859..15cde435ed 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogic.java @@ -28,11 +28,14 @@ import org.openecomp.sdc.be.components.impl.CsarValidationUtils; import org.openecomp.sdc.be.components.impl.GroupBusinessLogic; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.NodeTypeInfo; import org.openecomp.sdc.be.model.ParsedToscaYamlInfo; import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation; @@ -98,6 +101,21 @@ public class CsarBusinessLogic extends BaseBusinessLogic { } } + public void validateCsarBeforeCreate(Service resource, String csarUUID) { + // check if VF with the same Csar UUID or with he same name already + // exists + StorageOperationStatus status = toscaOperationFacade.validateCsarUuidUniqueness(csarUUID); + log.debug("enter validateCsarBeforeCreate,get status:{}", status); + if (status == StorageOperationStatus.ENTITY_ALREADY_EXISTS) { + log.debug("Failed to create resource {}, csarUUID {} already exist for a different VF ", + resource.getSystemName(), csarUUID); + } else if (status != StorageOperationStatus.OK) { + log.debug("Failed to validate uniqueness of CsarUUID {} for resource", csarUUID, + resource.getSystemName()); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status)); + } + } + public CsarInfo getCsarInfo(Resource resource, Resource oldResource,User user, Map<String, byte[]> payload, String csarUUID){ Map<String, byte[]> csar = getCsar(resource, user, payload, csarUUID); ImmutablePair<String, String> toscaYamlCsarStatus = validateAndParseCsar(resource, @@ -120,11 +138,32 @@ public class CsarBusinessLogic extends BaseBusinessLogic { toscaYamlCsarStatus.getKey(), toscaYamlCsarStatus.getValue(), true); } + public CsarInfo getCsarInfo(Service service, Service oldResource,User user, Map<String, byte[]> payload, String csarUUID){ + Map<String, byte[]> csar = getCsar(service, user, payload, csarUUID); + ImmutablePair<String, String> toscaYamlCsarStatus = validateAndParseCsar(service, + user, csar, csarUUID) + .left().on(this::throwComponentException); - public ParsedToscaYamlInfo getParsedToscaYamlInfo(String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, String nodeName) { + String checksum = CsarValidationUtils.getToscaYamlChecksum(csar, + csarUUID, componentsUtils).left().on(r->logAndThrowComponentException(r, "Failed to calculate checksum for casrUUID {} error {} ", csarUUID)); + if (oldResource!=null && !checksum.equals( + oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum())) { + log.debug("The checksum of main template yaml of csar with csarUUID {} is not equal to the previous one, existing checksum is {}, new one is {}.", csarUUID, + oldResource.getComponentMetadataDefinition().getMetadataDataDefinition() + .getImportedToscaChecksum(), + checksum); + oldResource.getComponentMetadataDefinition().getMetadataDataDefinition() + .setImportedToscaChecksum(checksum); + } + + return new CsarInfo(user, csarUUID, csar, service.getName(), + toscaYamlCsarStatus.getKey(), toscaYamlCsarStatus.getValue(), true); + } + + public ParsedToscaYamlInfo getParsedToscaYamlInfo(String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, String nodeName, Component component) { return yamlHandler.parseResourceInfoFromYAML( yamlName, topologyTemplateYaml, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo, - nodeName); + nodeName, component); } private String logAndThrowComponentException(ResponseFormat responseFormat, String logMessage, String ...params) { @@ -136,9 +175,9 @@ public class CsarBusinessLogic extends BaseBusinessLogic { throw new ByResponseFormatComponentException(responseFormat); } - private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Resource resource, User user, + private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Component component, User user, Map<String, byte[]> payload, String csarUUID) { - Map<String, byte[]> csar = getCsar(resource, user, payload, csarUUID); + Map<String, byte[]> csar = getCsar(component, user, payload, csarUUID); Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar, csarUUID, componentsUtils); if (validateCsarStatus.isRight()) { @@ -146,7 +185,10 @@ public class CsarBusinessLogic extends BaseBusinessLogic { log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat); BeEcompErrorManager.getInstance() .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED); - componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE); + if(component instanceof Resource){ + componentsUtils.auditResource(responseFormat, user, (Resource)component, AuditingActionEnum.CREATE_RESOURCE); + } + return Either.right(responseFormat); } @@ -158,13 +200,15 @@ public class CsarBusinessLogic extends BaseBusinessLogic { log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat); BeEcompErrorManager.getInstance() .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED); - componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE); + if(component instanceof Resource) { + componentsUtils.auditResource(responseFormat, user, (Resource)component, AuditingActionEnum.CREATE_RESOURCE); + } return Either.right(responseFormat); } return toscaYamlCsarStatus; } - private Map<String, byte[]> getCsar(Resource resource, User user, Map<String, byte[]> payload, String csarUUID) { + private Map<String, byte[]> getCsar(Component component, User user, Map<String, byte[]> payload, String csarUUID) { if (payload != null) { return payload; } @@ -176,7 +220,11 @@ public class CsarBusinessLogic extends BaseBusinessLogic { .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED); ResponseFormat responseFormat = componentsUtils .getResponseFormat(componentsUtils.convertFromStorageResponse(value), csarUUID); - componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE); + if(component instanceof Resource){ + Resource newResource = (Resource) component; + componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.CREATE_RESOURCE); + } + throw new StorageException(csar.right().value()); } return csar.left().value(); @@ -187,4 +235,44 @@ public class CsarBusinessLogic extends BaseBusinessLogic { componentsUtils.auditResource(errorResponse, user, resource, auditingAction); throw new ByResponseFormatComponentException(errorResponse, params); } + + private Map<String, byte[]> getCsar(User user, Map<String, byte[]> payload, String csarUUID) { + if (payload != null) { + return payload; + } + Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUUID, user); + if (csar.isRight()) { + StorageOperationStatus value = csar.right().value(); + log.debug("#getCsar - failed to fetch csar with ID {}, error: {}", csarUUID, value); + throw new StorageException(value); + } + return csar.left().value(); + } + + private Either<ImmutablePair<String, String>, ResponseFormat> validateAndParseCsar(Service service, User user, + Map<String, byte[]> payload, String csarUUID) { + Map<String, byte[]> csar = getCsar(service, user, payload, csarUUID); + Either<Boolean, ResponseFormat> validateCsarStatus = CsarValidationUtils.validateCsar(csar, + csarUUID, componentsUtils); + if (validateCsarStatus.isRight()) { + ResponseFormat responseFormat = validateCsarStatus.right().value(); + log.debug("Error when validate csar with ID {}, error: {}", csarUUID, responseFormat); + BeEcompErrorManager.getInstance() + .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED); + + return Either.right(responseFormat); + } + + Either<ImmutablePair<String, String>, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils + .getToscaYaml(csar, csarUUID, componentsUtils); + + if (toscaYamlCsarStatus.isRight()) { + ResponseFormat responseFormat = toscaYamlCsarStatus.right().value(); + log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat); + BeEcompErrorManager.getInstance() + .logBeDaoSystemError(CREATING_RESOURCE_FROM_CSAR_FETCHING_CSAR_WITH_ID + csarUUID + FAILED); + return Either.right(responseFormat); + } + return toscaYamlCsarStatus; + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java index 41dc3a6db3..9636885190 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java @@ -54,6 +54,7 @@ import org.openecomp.sdc.be.model.NodeTypeInfo; import org.openecomp.sdc.be.model.ParsedToscaYamlInfo; import org.openecomp.sdc.be.model.PolicyDefinition; import org.openecomp.sdc.be.model.PolicyTypeDefinition; +import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.UploadArtifactInfo; import org.openecomp.sdc.be.model.UploadCapInfo; import org.openecomp.sdc.be.model.UploadComponentInstanceInfo; @@ -129,7 +130,7 @@ public class YamlTemplateParsingHandler { } public ParsedToscaYamlInfo parseResourceInfoFromYAML(String fileName, String resourceYml, Map<String, String> createdNodesToscaResourceNames, - Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName) { + Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName, org.openecomp.sdc.be.model.Component component) { log.debug("#parseResourceInfoFromYAML - Going to parse yaml {} ", fileName); Map<String, Object> mappedToscaTemplate = getMappedToscaTemplate(fileName, resourceYml, nodeTypesInfo, nodeName); ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo(); @@ -140,7 +141,9 @@ public class YamlTemplateParsingHandler { parsedToscaYamlInfo.setInputs(getInputs(mappedToscaTemplate)); parsedToscaYamlInfo.setInstances(getInstances(fileName, mappedToscaTemplate, createdNodesToscaResourceNames)); parsedToscaYamlInfo.setGroups(getGroups(fileName, mappedToscaTemplate)); - parsedToscaYamlInfo.setPolicies(getPolicies(fileName, mappedToscaTemplate)); + if(component instanceof Resource){ + parsedToscaYamlInfo.setPolicies(getPolicies(fileName, mappedToscaTemplate)); + } log.debug("#parseResourceInfoFromYAML - The yaml {} has been parsed ", fileName); return parsedToscaYamlInfo; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/AbstractTemplateBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/AbstractTemplateBusinessLogic.java new file mode 100644 index 0000000000..95422f4cfc --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/AbstractTemplateBusinessLogic.java @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openecomp.sdc.be.components.impl; + +import fj.data.Either; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.externalapi.servlet.representation.AbstractResourceInfo; +import org.openecomp.sdc.be.externalapi.servlet.representation.AbstractTemplateInfo; +import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.category.CategoryDefinition; +import org.openecomp.sdc.be.model.category.SubCategoryDefinition; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation; +import org.openecomp.sdc.be.model.operations.api.*; +import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; +import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +@org.springframework.stereotype.Component("abstractTemplateBusinessLogic") +public class AbstractTemplateBusinessLogic extends BaseBusinessLogic { + + private static final String INITIAL_VERSION = "0.1"; + + private static final Logger log = Logger.getLogger(AbstractTemplateBusinessLogic.class); + + @Autowired + ServiceDistributionValidation serviceDistributionValidation; + + @Autowired + protected ServiceImportManager serviceImportManager; + + @Autowired + protected ServiceBusinessLogic serviceBusinessLogic; + + @Autowired + public AbstractTemplateBusinessLogic(IElementOperation elementDao, IGroupOperation groupOperation, + IGroupInstanceOperation groupInstanceOperation, IGroupTypeOperation groupTypeOperation, + InterfaceOperation interfaceOperation, + InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ArtifactsOperations artifactToscaOperation) { + super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, + interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation); + } + + public Either<AbstractTemplateInfo, ResponseFormat> getServiceAbstractStatus(List<? extends Component> componentList) { + AbstractTemplateInfo abstractTemplateInfo = new AbstractTemplateInfo(); + if (componentList == null || componentList.isEmpty()) { + return Either.left(abstractTemplateInfo); + } + + for (Component curr : componentList) { + Service service = (Service) curr; + List<ComponentInstance> componentInstances = service.getComponentInstances(); + List<RequirementCapabilityRelDef> componentInstancesRelations = service.getComponentInstancesRelations(); + abstractTemplateInfo.setServiceUniqueId(service.getUniqueId()); + String serviceUniqueId = abstractTemplateInfo.getServiceUniqueId(); + Either<Boolean, ResponseFormat> isAbstractResourceData = getEveryServiceAbstractStatus(componentInstances, abstractTemplateInfo, componentInstancesRelations, serviceUniqueId); + if (isAbstractResourceData.isRight()) { + return Either.right(isAbstractResourceData.right().value()); + } + } + + return Either.left(abstractTemplateInfo); + } + + private Either<Boolean, ResponseFormat> getEveryServiceAbstractStatus(List<ComponentInstance> componentInstances, AbstractTemplateInfo abstractTemplateInfo, + List<RequirementCapabilityRelDef> componentInstancesRelations, String serviceUniqueId) { + Map<String, ImmutablePair<String, String>> uuidDuplicatesMap = new HashMap<>(); + List<AbstractResourceInfo> abstractResourceInfoList = new ArrayList<>(); + Boolean isContainAbstractResource = false; + + for (ComponentInstance componentInstance : componentInstances) { + String componentUid = componentInstance.getComponentUid(); + String invariantUUID, resourceUUID; + if (!uuidDuplicatesMap.containsKey(componentUid)) { + Either<Resource, StorageOperationStatus> eitherResource = toscaOperationFacade + .getToscaElement(componentInstance.getComponentUid()); + if (eitherResource.isRight()) { + log.debug("getEveryServiceAbstractStatus: Failed getting resource with UUid: {}", + componentInstance.getComponentUid()); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(eitherResource.right().value()); + return Either.right(responseFormat); + } else { + final Resource resource = eitherResource.left().value(); + invariantUUID = resource.getInvariantUUID(); + resourceUUID = resource.getUUID(); + ImmutablePair<String, String> uuidInvariantUUIDPair = new ImmutablePair<>(resourceUUID, + invariantUUID); + uuidDuplicatesMap.put(componentUid, uuidInvariantUUIDPair); + + Either<List<AbstractResourceInfo>, ResponseFormat> abstractResourceEither = getAbstractResourceInfoList(resource, componentInstancesRelations, serviceUniqueId); + if (abstractResourceEither.isRight()) { + return Either.right(abstractResourceEither.right().value()); + } + abstractResourceInfoList = abstractResourceEither.left().value(); + isContainAbstractResource = true; + } + } + } + abstractTemplateInfo.setIsAbstractTemplate(isContainAbstractResource); + abstractTemplateInfo.setAbstractResourceInfoList(abstractResourceInfoList); + return Either.left(isContainAbstractResource); + } + + private Either<List<AbstractResourceInfo>, ResponseFormat> getAbstractResourceInfoList(Resource resource, List<RequirementCapabilityRelDef> componentInstancesRelations, String serviceUniqueId) { + List<AbstractResourceInfo> abstractResourceInfoList = new ArrayList<>(); + boolean isAbstract = getIsAbstract(resource.getCategories()); + log.debug("before if isAbstract,get resource:{}", resource); + if (!isAbstract) { + log.debug("getAbstractResourceInfoList:Resource is not Abstract"); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.NOT_FOUND)); + return Either.right(responseFormat); + } + log.debug("getAbstractResourceInfoList:Resource is Abstract"); + + AbstractResourceInfo abstractResourceInfo = getAbstractResourceInfo(resource, componentInstancesRelations, serviceUniqueId); + abstractResourceInfoList.add(abstractResourceInfo); + return Either.left(abstractResourceInfoList); + } + + private AbstractResourceInfo getAbstractResourceInfo(Resource resource,List<RequirementCapabilityRelDef> componentInstancesRelations,String serviceUniqueId){ + AbstractResourceInfo abstractResourceInfo = new AbstractResourceInfo(); + abstractResourceInfo.setAbstractResourceUUid(resource.getUUID()); + abstractResourceInfo.setAbstractResourceName(resource.getName()); + abstractResourceInfo.setAbstractResourceUniqueId(resource.getUniqueId()); + String uniqueId = serviceUniqueId + "." + resource.getUniqueId() + "." + resource.getNormalizedName(); + + List<RequirementCapabilityRelDef> resourceComponentInstancesRelations = new ArrayList<>(); + log.debug("get serviceUniqueId:{},get UniqueId:{},get NormalizedName:{}", + serviceUniqueId, resource.getUniqueId(), resource.getNormalizedName()); + log.debug("get is Abstract,componentInstancesRelations:{}", componentInstancesRelations); + for (RequirementCapabilityRelDef componentInstancesRelation : componentInstancesRelations) { + log.debug("for componentInstancesRelation,get componentInstancesRelation:{}", componentInstancesRelation); + String fromNode = componentInstancesRelation.getFromNode(); + log.debug("for componentInstancesRelation,get fromNode:{},uniqueId:{}", fromNode,uniqueId); + if (fromNode.toUpperCase().contains(uniqueId.toUpperCase())) { + RequirementCapabilityRelDef resourceComponentInstancesRelation = new RequirementCapabilityRelDef(); + log.debug("fromNode contains name,get componentInstancesRelation:{}", componentInstancesRelation); + resourceComponentInstancesRelation.setFromNode(componentInstancesRelation.getFromNode()); + resourceComponentInstancesRelation.setOriginUI(componentInstancesRelation.isOriginUI()); + resourceComponentInstancesRelation.setRelationships(componentInstancesRelation.getRelationships()); + resourceComponentInstancesRelation.setToNode(componentInstancesRelation.getToNode()); + resourceComponentInstancesRelation.setUid(componentInstancesRelation.getUid()); + resourceComponentInstancesRelations.add(resourceComponentInstancesRelation); + } + } + abstractResourceInfo.setComponentInstancesRelations(resourceComponentInstancesRelations); + return abstractResourceInfo; + } + + private boolean getIsAbstract(List<CategoryDefinition> categories) { + boolean anAbstract = false; + if (categories != null && !categories.isEmpty()) { + CategoryDefinition categoryDef = categories.get(0); + if (categoryDef != null && categoryDef.getName() != null && categoryDef.getName() + .equals(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME)) { + SubCategoryDefinition subCategoryDef = categoryDef.getSubcategories().get(0); + if (subCategoryDef != null && subCategoryDef.getName().equals(ImportUtils.Constants.ABSTRACT_SUBCATEGORY)) { + anAbstract = true; + } + } + } + return anAbstract; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index cac36cbe0e..c9e0a44999 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -4491,7 +4491,7 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { return uploadedArtifacts; } - public Either<ArtifactDefinition, Operation> handleLoadedArtifact(Resource component, User user, ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction, + public Either<ArtifactDefinition, Operation> handleLoadedArtifact(Component component, User user, ArtifactOperationInfo operation, boolean shouldLock, boolean inTransaction, ComponentTypeEnum componentType, ArtifactDefinition artifactDefinition) { AuditingActionEnum auditingAction = detectAuditingType(operation, ""); String componentId = component.getUniqueId(); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java index ba1500c9be..34236eb644 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java @@ -296,6 +296,39 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic { return Either.left(result); } + protected void validateIcon(User user, Component component, AuditingActionEnum actionEnum) { + log.debug("validate Icon"); + ComponentTypeEnum type = component.getComponentType(); + String icon = component.getIcon(); + if (!ValidationUtils.validateStringNotEmpty(icon)) { + log.info("icon is missing."); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_ICON, type.getValue()); + componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, type); + throw new ComponentException(ActionStatus.COMPONENT_MISSING_ICON, type.getValue()); + } + try { + validateIcon(icon, type); + } catch(ComponentException e){ + ResponseFormat responseFormat = e.getResponseFormat() != null ? e.getResponseFormat() + : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()); + componentsUtils.auditComponentAdmin(responseFormat, user, component, actionEnum, type); + throw e; + } + } + + private void validateIcon(String icon, ComponentTypeEnum type) { + if (icon != null) { + if (!ValidationUtils.validateIconLength(icon)) { + log.debug("icon exceeds max length"); + throw new ComponentException(ActionStatus.COMPONENT_ICON_EXCEEDS_LIMIT, type.getValue(), "" + ValidationUtils.ICON_MAX_LENGTH); + } + + if (!ValidationUtils.validateIcon(icon)) { + log.info("icon is invalid."); + throw new ComponentException(ActionStatus.COMPONENT_INVALID_ICON, type.getValue()); + } + } + } protected void checkComponentFieldsForOverrideAttempt(Component component) { if (component.getLifecycleState() != null) { @@ -331,6 +364,24 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic { } } + protected void validateComponentFieldsBeforeCreate(User user, Component component, AuditingActionEnum actionEnum) { + // validate component name uniqueness + log.debug("validate component name "); + componentNameValidator.validateAndCorrectField(user, component, actionEnum); + // validate description + log.debug("validate description"); + componentDescriptionValidator.validateAndCorrectField(user, component, actionEnum); + // validate tags + log.debug("validate tags"); + componentTagsValidator.validateAndCorrectField(user, component, actionEnum); + // validate contact info + log.debug("validate contact info"); + componentContactIdValidator.validateAndCorrectField(user, component, actionEnum); + // validate icon + log.debug("validate icon"); + validateIcon(user, component, actionEnum); + } + public CapReqDef getRequirementsAndCapabilities(String componentId, ComponentTypeEnum componentTypeEnum, String userId) { validateUserExists(userId); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java index e7d8dd56f5..ffccc978db 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java @@ -421,6 +421,138 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { } } + /** + * Try using either to make a judgment + * @param containerComponentParam + * @param containerComponentId + * @param userId + * @param resourceInstance + * @return + */ + + public Either<ComponentInstance, ResponseFormat> createRealComponentInstance(String containerComponentParam, String containerComponentId, String userId, ComponentInstance resourceInstance) { + log.debug("enter createRealComponentInstance"); + return createRealComponentInstance(containerComponentParam, containerComponentId, userId, resourceInstance, false, true); + } + /** + * Try using either to make a judgment + * + * @param inTransaction + * @param needLock + * @param containerComponentParam + * @param containerComponentId + * @param userId + * @param resourceInstance + * @return + */ + public Either<ComponentInstance, ResponseFormat> createRealComponentInstance(String containerComponentParam, String containerComponentId, String userId, ComponentInstance resourceInstance, boolean inTransaction, boolean needLock) { + + log.debug("enter createRealComponentInstance"); + Component origComponent = null; + User user; + org.openecomp.sdc.be.model.Component containerComponent = null; + ComponentTypeEnum containerComponentType; + try { + user = validateUserExists(userId); + validateUserNotEmpty(user, "Create component instance"); + validateJsonBody(resourceInstance, ComponentInstance.class); + containerComponentType = validateComponentType(containerComponentParam); + containerComponent = validateComponentExists(containerComponentId, containerComponentType, null); + + log.debug("enter createRealComponentInstance,validate user json success"); + if (ModelConverter.isAtomicComponent(containerComponent)) { + log.debug("Cannot attach resource instances to container resource of type {}", containerComponent.assetType()); + throw new ByActionStatusComponentException(ActionStatus.RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, containerComponent.assetType()); + } + + validateCanWorkOnComponent(containerComponent, userId); + log.debug("enter createRealComponentInstance,validateCanWorkOnComponent success"); + + if (resourceInstance != null && containerComponentType != null) { + log.debug("enter createRealComponentInstance,start create ComponentInstance"); + OriginTypeEnum originType = resourceInstance.getOriginType(); + validateInstanceName(resourceInstance); + if (originType == OriginTypeEnum.ServiceProxy) { + + log.debug("enter createRealComponentInstance,originType equals ServiceProxy"); + Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade.getLatestByName("serviceProxy"); + if (isServiceProxyOrigin(serviceProxyOrigin)) { + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value())); + } + origComponent = serviceProxyOrigin.left().value(); + + StorageOperationStatus fillProxyRes = fillInstanceData(resourceInstance, origComponent); + if (isFillProxyRes(fillProxyRes)) { + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(fillProxyRes)); + } + } else { + log.debug("enter createRealComponentInstance,originType is not ServiceProxy"); + origComponent = getAndValidateOriginComponentOfComponentInstance(containerComponent, resourceInstance); + } + validateOriginAndResourceInstanceTypes(containerComponent, origComponent, originType); + validateResourceInstanceState(containerComponent, origComponent); + overrideFields(origComponent, resourceInstance); + compositionBusinessLogic.validateAndSetDefaultCoordinates(resourceInstance); + log.debug("enter createRealComponentInstance,final validate success"); + } + return createRealComponent(needLock, containerComponent, origComponent, resourceInstance, user); + + } catch (ComponentException e) { + log.debug("create Real Component Instance failed"); + throw e; + } + } + + private Either<ComponentInstance, ResponseFormat> createRealComponent(boolean needLock, Component containerComponent, Component origComponent, ComponentInstance resourceInstance, User user) { + + log.debug("enter createRealComponent"); + boolean failed = false; + try { + + ComponentInstance lockComponent = isNeedLock(needLock, containerComponent); + if (lockComponent != null) { + return Either.left(lockComponent); + } + log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH); + return createRealComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user); + } catch (ComponentException e) { + failed = true; + throw e; + } finally { + if (needLock) + unlockComponent(failed, containerComponent); + } + } + + private Either<ComponentInstance, ResponseFormat> createRealComponentInstanceOnGraph(org.openecomp.sdc.be.model.Component containerComponent, Component originComponent, ComponentInstance componentInstance, User user) { + Either<ComponentInstance, ResponseFormat> resultOp; + log.debug("enter createRealComponentInstanceOnGraph"); + + Either<ImmutablePair<Component, String>, StorageOperationStatus> result = toscaOperationFacade.addComponentInstanceToTopologyTemplate(containerComponent, originComponent, componentInstance, false, user); + + if (result.isRight()) { + log.debug("enter createRealComponentInstanceOnGraph,result is right"); + ActionStatus status = componentsUtils.convertFromStorageResponse(result.right().value()); + log.debug(FAILED_TO_CREATE_ENTRY_ON_GRAPH_FOR_COMPONENT_INSTANCE, componentInstance.getName()); + return Either.right(componentsUtils.getResponseFormat(status)); + } + + log.debug(ENTITY_ON_GRAPH_IS_CREATED); + log.debug("enter createRealComponentInstanceOnGraph,Entity on graph is created."); + Component updatedComponent = result.left().value().getLeft(); + Map<String, String> existingEnvVersions = new HashMap<>(); + // TODO existingEnvVersions ?? + addComponentInstanceArtifacts(updatedComponent, componentInstance, originComponent, user, existingEnvVersions); + + Optional<ComponentInstance> updatedInstanceOptional = updatedComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(result.left().value().getRight())).findFirst(); + if (!updatedInstanceOptional.isPresent()) { + log.debug("Failed to fetch new added component instance {} from component {}", componentInstance.getName(), containerComponent.getName()); + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName()); + } + log.debug("enter createRealComponentInstanceOnGraph,and final success"); + return Either.left(updatedInstanceOptional.get()); + } + private void overrideFields(Component origComponent, ComponentInstance resourceInstance) { resourceInstance.setComponentVersion(origComponent.getVersion()); resourceInstance.setIcon(origComponent.getIcon()); @@ -1158,6 +1290,67 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { } return componentInstance; } + + /** + * Try to modify the delete and return two cases + * + * @param containerComponentParam + * @param containerComponentId + * @param componentInstanceId + * @param userId + * @return + */ + public Either<ComponentInstance, ResponseFormat> deleteAbstractComponentInstance(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId) { + log.debug("enter deleteAbstractComponentInstance"); + validateUserExists(userId); + + final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam); + + org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null); + validateCanWorkOnComponent(containerComponent, userId); + + boolean failed = false; + ComponentInstance deletedRelatedInst; + try { + if (containerComponent instanceof Service) { + ComponentInstance componentInstance = containerComponent.getComponentInstanceById(componentInstanceId).get(); + Either<String, StorageOperationStatus> deleteServiceFilterEither = + nodeFilterOperation.deleteNodeFilter((Service) containerComponent, componentInstanceId); + if (deleteServiceFilterEither.isRight()) { + log.debug("enter deleteAbstractComponentInstance:deleteServiceFilterEither is right, filed"); + ActionStatus status = componentsUtils.convertFromStorageResponse(deleteServiceFilterEither.right().value(), + containerComponentType); + janusGraphDao.rollback(); + return Either.right(componentsUtils.getResponseFormat(status, componentInstance.getName())); + } + Either<ComponentInstance, ResponseFormat> resultOp = deleteNodeFiltersFromComponentInstance((Service) containerComponent, + componentInstance, ComponentTypeEnum.SERVICE, userId); + if (resultOp.isRight()) { + log.debug("enter deleteAbstractComponentInstance:resultOp is right, filed"); + janusGraphDao.rollback(); + return resultOp; + } + } + log.debug("enter deleteAbstractComponentInstance:"); + lockComponent(containerComponent, "deleteComponentInstance"); + ComponentInstance deletedCompInstance = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType); + + deletedRelatedInst = deleteForwardingPathsRelatedTobeDeletedComponentInstance(containerComponentId, + containerComponentType, deletedCompInstance); + ActionStatus onDeleteOperationsStatus = onChangeInstanceOperationOrchestrator.doOnDeleteInstanceOperations(containerComponent, componentInstanceId); + log.debug("enter deleteAbstractComponentInstance,get onDeleteOperationsStatus:{}",onDeleteOperationsStatus); + if (ActionStatus.OK != onDeleteOperationsStatus) { + throw new ByActionStatusComponentException(onDeleteOperationsStatus); + } + } catch (ComponentException e) { + failed = true; + throw e; + } finally { + unlockComponent(failed, containerComponent); + } + log.debug("enter deleteAbstractComponentInstance,deleted RelatedInst success"); + return Either.left(deletedRelatedInst); + } public Either<ComponentInstance, ResponseFormat> deleteNodeFiltersFromComponentInstance( final Component component, diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CompositionBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CompositionBusinessLogic.java index 0790c4b8bf..20e796e1fa 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CompositionBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CompositionBusinessLogic.java @@ -29,6 +29,7 @@ import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -83,6 +84,20 @@ public class CompositionBusinessLogic { userId, resource.getComponentInstances(), false); } } + + protected void setPositionsForComponentInstances(Service service, String userId) { + boolean isNotAllPositionsCalculated = service.getComponentInstances() == null + || service.getComponentInstances().stream().anyMatch(p -> (p.getPosX() == null || p.getPosX().isEmpty()) || (p.getPosY() == null || p.getPosY().isEmpty())); + if (isNotAllPositionsCalculated && service.getComponentInstances() != null) { + // Arrange Icons In Spiral Pattern + Map<ImmutablePair<Double, Double>, ComponentInstance> componentInstanceLocations = buildSpiralPatternPositioningForComponentInstances(service); + // Set Relative Locations According to Canvas Size + componentInstanceLocations.entrySet().forEach(this::setRelativePosition); + // Update in DB + componentInstanceBusinessLogic.updateComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, service, service.getUniqueId(), + userId, service.getComponentInstances(), false); + } + } private void setRelativePosition(Entry<ImmutablePair<Double, Double>, ComponentInstance> entry) { int xCenter = CANVAS_WIDTH / 2; @@ -120,13 +135,23 @@ public class CompositionBusinessLogic { return String.valueOf(offsetedCanvasPosition); } - protected Map<ImmutablePair<Double, Double>, ComponentInstance> buildSpiralPatternPositioningForComponentInstances(Resource resource) { + protected Map<ImmutablePair<Double, Double>, ComponentInstance> buildSpiralPatternPositioningForComponentInstances( + org.openecomp.sdc.be.model.Component component) { Map<ImmutablePair<Double, Double>, ComponentInstance> componentInstanceLocations = new HashMap<>(); List<ComponentInstance> componentInstances = new ArrayList<>(); - componentInstances.addAll(resource.getComponentInstances()); - Map<ComponentInstance, List<ComponentInstance>> connectededCps = getCpsConnectedToVFC(componentInstances, resource); + if (component instanceof Resource){ + Resource resource = (Resource) component; + List<ComponentInstance> componentInstanceList = resource.getComponentInstances(); + componentInstances.addAll(componentInstanceList); + }else if (component instanceof Service){ + Service service = (Service) component; + List<ComponentInstance> componentInstanceList = service.getComponentInstances(); + componentInstances.addAll(componentInstanceList); + } + + Map<ComponentInstance, List<ComponentInstance>> connectededCps = getCpsConnectedToVFC(componentInstances, component); // Remove all cp that are connected from the list componentInstances.removeAll(connectededCps.values().stream().flatMap(Collection::stream).collect(Collectors.toList())); @@ -186,9 +211,17 @@ public class CompositionBusinessLogic { } } - protected Map<ComponentInstance, List<ComponentInstance>> getCpsConnectedToVFC(List<ComponentInstance> allComponentInstances, Resource vf) { + protected Map<ComponentInstance, List<ComponentInstance>> getCpsConnectedToVFC(List<ComponentInstance> allComponentInstances, org.openecomp.sdc.be.model.Component component) { Map<ComponentInstance, List<ComponentInstance>> vfcWithItsCps = new HashMap<>(); - List<RequirementCapabilityRelDef> allRelations = vf.getComponentInstancesRelations(); + List<RequirementCapabilityRelDef> allRelations = new ArrayList<>(); + if (component instanceof Resource){ + Resource vf = (Resource) component; + allRelations = vf.getComponentInstancesRelations(); + }else if (component instanceof Service){ + Service sv = (Service) component; + allRelations = sv.getComponentInstancesRelations(); + } + for (ComponentInstance curr : allComponentInstances) { // Filters Only CPs if (curr.getOriginType() == OriginTypeEnum.CP) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java index 4db44d3a76..f04eca1744 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java @@ -182,6 +182,7 @@ public final class ImportUtils { public static final String CVFC_DESCRIPTION = "Complex node type that is used as nested type in VF"; public static final String ESCAPED_DOUBLE_QUOTE = "\""; public static final String QUOTE = "'"; + public static final String VF_DESCRIPTION = "Nested VF in service"; private Constants() { } 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 ffac13bdb5..50b2941f1b 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 @@ -603,7 +603,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { ParsedToscaYamlInfo uploadComponentInstanceInfoMap; try { uploadComponentInstanceInfoMap = csarBusinessLogic.getParsedToscaYamlInfo(yamlFileContent, yamlFileName, - nodeTypesInfo, csarInfo, nodeName); + nodeTypesInfo, csarInfo, nodeName, oldResource); Map<String, UploadComponentInstanceInfo> instances = uploadComponentInstanceInfoMap.getInstances(); if (MapUtils.isEmpty(instances) && newResource.getResourceType() != ResourceTypeEnum.PNF) { throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName); @@ -1193,7 +1193,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); Resource createdResource; try { - ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic.getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName); + ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic.getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, resource); if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances()) && resource.getResourceType() != ResourceTypeEnum.PNF) { throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); } @@ -2317,9 +2317,15 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic.createResourceArtifactsFromCsar( csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts); } else { - createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic.updateResourceArtifactsFromCsar( + Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic.updateResourceArtifactsFromCsar( csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts, shouldLock, inTransaction); + if ((result.left().value() instanceof Resource) && result.isLeft()) { + Resource service1 = (Resource) result.left().value(); + createArtifactsFromCsar = Either.left(service1); + } else { + createArtifactsFromCsar = Either.right(result.right().value()); + } } if (createArtifactsFromCsar.isRight()) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java index dbcaee4d30..4e6628c8dd 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java @@ -128,6 +128,16 @@ public class ResourceImportManager { private AuditingManager auditingManager; private ResourceBusinessLogic resourceBusinessLogic; + public ServiceBusinessLogic getServiceBusinessLogic() { + return serviceBusinessLogic; + } + + public void setServiceBusinessLogic(ServiceBusinessLogic serviceBusinessLogic) { + this.serviceBusinessLogic = serviceBusinessLogic; + } + + @Autowired + private ServiceBusinessLogic serviceBusinessLogic; private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic; private IGraphLockOperation graphLockOperation; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java index 552fbb0b16..a38ffe9ee0 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java @@ -179,6 +179,7 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { private static final String STATUS_SUCCESS_200 = "200"; private static final String STATUS_DEPLOYED = "DEPLOYED"; static final String IS_VALID = "isValid"; + private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes"; private ForwardingPathOperation forwardingPathOperation; private AuditCassandraDao auditCassandraDao; @@ -2271,12 +2272,51 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { @Override public void setDeploymentArtifactsPlaceHolder(Component component, User user) { - Service service = (Service) component; - Map<String, ArtifactDefinition> artifactMap = service.getDeploymentArtifacts(); - if (artifactMap == null) { - artifactMap = new HashMap<>(); + if(component instanceof Service){ + Service service = (Service) component; + Map<String, ArtifactDefinition> artifactMap = service.getDeploymentArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + service.setDeploymentArtifacts(artifactMap); + }else if(component instanceof Resource){ + Resource resource = (Resource) component; + Map<String, ArtifactDefinition> artifactMap = resource.getDeploymentArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + Map<String, Object> deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager() + .getConfiguration().getDeploymentResourceArtifacts(); + if (deploymentResourceArtifacts != null) { + Map<String, ArtifactDefinition> finalArtifactMap = artifactMap; + deploymentResourceArtifacts.forEach((k, v)->processDeploymentResourceArtifacts(user, resource, finalArtifactMap, k,v)); + } + resource.setDeploymentArtifacts(artifactMap); + } + + } + + private void processDeploymentResourceArtifacts(User user, Resource resource, Map<String, ArtifactDefinition> artifactMap, String k, Object v) { + Map<String, Object> artifactDetails = (Map<String, Object>) v; + Object object = artifactDetails.get(PLACE_HOLDER_RESOURCE_TYPES); + if (object != null) { + List<String> artifactTypes = (List<String>) object; + if (!artifactTypes.contains(resource.getResourceType().name())) { + return; + } + } else { + log.info("resource types for artifact placeholder {} were not defined. default is all resources", + k); + } + if (artifactsBusinessLogic != null) { + ArtifactDefinition artifactDefinition = artifactsBusinessLogic.createArtifactPlaceHolderInfo( + resource.getUniqueId(), k, (Map<String, Object>) v, + user, ArtifactGroupTypeEnum.DEPLOYMENT); + if (artifactDefinition != null + && !artifactMap.containsKey(artifactDefinition.getArtifactLabel())) { + artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); + } } - service.setDeploymentArtifacts(artifactMap); } @Override @@ -2520,4 +2560,9 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { public void setServiceCreationPluginList(List<ServiceCreationPlugin> serviceCreationPluginList) { this.serviceCreationPluginList = serviceCreationPluginList; } + + public boolean isServiceExist(String serviceName) { + Either<Service, StorageOperationStatus> latestByName = toscaOperationFacade.getLatestByServiceName(serviceName); + return latestByName.isLeft(); + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java new file mode 100644 index 0000000000..857c5e32b7 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java @@ -0,0 +1,3141 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openecomp.sdc.be.components.impl; + +import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; +import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStringElement; +import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue; +import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN; + +import fj.data.Either; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.regex.Pattern; + +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.collections4.ListUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic; +import org.openecomp.sdc.be.components.csar.CsarBusinessLogic; +import org.openecomp.sdc.be.components.csar.CsarInfo; +import org.openecomp.sdc.be.components.distribution.engine.IDistributionEngine; +import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum; +import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo; +import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; +import org.openecomp.sdc.be.components.impl.utils.CINodeFilterUtils; +import org.openecomp.sdc.be.components.impl.utils.CreateServiceFromYamlParameter; +import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; +import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; +import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic; +import org.openecomp.sdc.be.components.path.ForwardingPathValidator; +import org.openecomp.sdc.be.components.validation.NodeFilterValidator; +import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation; +import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator; +import org.openecomp.sdc.be.components.validation.component.ComponentValidator; +import org.openecomp.sdc.be.config.BeEcompErrorManager; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils; +import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter; +import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.CreatedFrom; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.CapabilityDefinition; +import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; +import org.openecomp.sdc.be.model.CapabilityTypeDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceInput; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.DistributionStatusEnum; +import org.openecomp.sdc.be.model.GroupDefinition; +import org.openecomp.sdc.be.model.InputDefinition; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.LifeCycleTransitionEnum; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.NodeTypeInfo; +import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.ParsedToscaYamlInfo; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.RelationshipImpl; +import org.openecomp.sdc.be.model.RelationshipInfo; +import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; +import org.openecomp.sdc.be.model.RequirementDefinition; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.UploadCapInfo; +import org.openecomp.sdc.be.model.UploadComponentInstanceInfo; +import org.openecomp.sdc.be.model.UploadInfo; +import org.openecomp.sdc.be.model.UploadNodeFilterInfo; +import org.openecomp.sdc.be.model.UploadPropInfo; +import org.openecomp.sdc.be.model.UploadReqInfo; +import org.openecomp.sdc.be.model.UploadResourceInfo; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.category.CategoryDefinition; +import org.openecomp.sdc.be.model.category.SubCategoryDefinition; +import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; +import org.openecomp.sdc.be.model.operations.StorageException; +import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation; +import org.openecomp.sdc.be.model.operations.api.IElementOperation; +import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation; +import org.openecomp.sdc.be.model.operations.api.IGroupOperation; +import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation; +import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; +import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; +import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils; +import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo; +import org.openecomp.sdc.be.tosca.CsarUtils; +import org.openecomp.sdc.be.utils.CommonBeUtils; +import org.openecomp.sdc.be.utils.TypeUtils; +import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.datastructure.Wrapper; +import org.openecomp.sdc.common.kpi.api.ASDCKpiApi; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.common.util.GeneralUtility; +import org.openecomp.sdc.common.util.ValidationUtils; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.beans.factory.annotation.Autowired; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +@Getter +@Setter +@org.springframework.stereotype.Component("serviceImportBusinessLogic") +public class ServiceImportBusinessLogic{ + + private final UiComponentDataConverter uiComponentDataConverter; + private static final String INITIAL_VERSION = "0.1"; + private static final String CREATE_RESOURCE = "Create Resource"; + private static final String IN_RESOURCE = " in resource {} "; + private static final String COMPONENT_INSTANCE_WITH_NAME = "component instance with name "; + private static final String COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE = "component instance with name {} in resource {} "; + private static final String CERTIFICATION_ON_IMPORT = "certification on import"; + private static final String VALIDATE_DERIVED_BEFORE_UPDATE = "validate derived before update"; + private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes"; + private static final String CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES = "Create Resource - validateCapabilityTypesCreate"; + private static final String CATEGORY_IS_EMPTY = "Resource category is empty"; + + + @Autowired + private ServiceBusinessLogic serviceBusinessLogic; + public ServiceBusinessLogic getServiceBusinessLogic() { + return serviceBusinessLogic; + } + + public void setServiceBusinessLogic( + ServiceBusinessLogic serviceBusinessLogic) { + this.serviceBusinessLogic = serviceBusinessLogic; + } + + @Autowired + private CsarBusinessLogic csarBusinessLogic; + @Autowired + protected ComponentsUtils componentsUtils; + @Autowired + protected ToscaOperationFacade toscaOperationFacade; + @Autowired + private CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic; + @Autowired + private LifecycleBusinessLogic lifecycleBusinessLogic; + @Autowired + private CompositionBusinessLogic compositionBusinessLogic; + @Autowired + private ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic; + @Autowired + private ServiceImportParseLogic serviceImportParseLogic; + + private static final Logger log = Logger.getLogger(ServiceImportBusinessLogic.class); + @Autowired + public ServiceImportBusinessLogic(IElementOperation elementDao, + IGroupOperation groupOperation, + IGroupInstanceOperation groupInstanceOperation, + IGroupTypeOperation groupTypeOperation, + GroupBusinessLogic groupBusinessLogic, + InterfaceOperation interfaceOperation, + InterfaceLifecycleOperation interfaceLifecycleTypeOperation, + ArtifactsBusinessLogic artifactsBusinessLogic, + IDistributionEngine distributionEngine, ComponentInstanceBusinessLogic componentInstanceBusinessLogic, + ServiceDistributionValidation serviceDistributionValidation, ForwardingPathValidator forwardingPathValidator, + UiComponentDataConverter uiComponentDataConverter, NodeFilterOperation serviceFilterOperation, + NodeFilterValidator serviceFilterValidator, ArtifactsOperations artifactToscaOperation, + ComponentContactIdValidator componentContactIdValidator, + ComponentNameValidator componentNameValidator, + ComponentTagsValidator componentTagsValidator, + ComponentValidator componentValidator, + ComponentIconValidator componentIconValidator, + ComponentProjectCodeValidator componentProjectCodeValidator, + ComponentDescriptionValidator componentDescriptionValidator) { + this.componentInstanceBusinessLogic = componentInstanceBusinessLogic; + this.uiComponentDataConverter = uiComponentDataConverter; + } + + private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic; + + public Service createService(Service service, AuditingActionEnum auditingAction, User user, Map<String, byte[]> csarUIPayload, String payloadName) { + log.debug("enter createService"); + service.setCreatorUserId(user.getUserId()); + service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + service.setVersion(INITIAL_VERSION); + service.setConformanceLevel(ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel()); + service.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED); + service.setInstantiationType("A-la-carte"); + service.setEnvironmentContext("General_Revenue-Bearing"); + service.setEcompGeneratedNaming(true); + + try { + serviceBusinessLogic.validateServiceBeforeCreate(service, user, auditingAction); + + log.debug("enter createService,validateServiceBeforeCreate success"); + String csarUUID = payloadName == null ? service.getCsarUUID() : payloadName; + + log.debug("enter createService,get csarUUID:{}", csarUUID); + csarBusinessLogic.validateCsarBeforeCreate(service, csarUUID); + log.debug("CsarUUID is {} - going to create resource from CSAR", csarUUID); + return createServiceFromCsar(service, user, csarUIPayload, csarUUID); + }catch (Exception e){ + log.debug("Exception occured when createService,error is:{}",e.getMessage(),e); + //ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected Service createServiceFromCsar(Service service, User user, Map<String, byte[]> csarUIPayload, String csarUUID) { + log.trace("************* created successfully from YAML, resource TOSCA "); + try { + CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(service, null, user, csarUIPayload, csarUUID); + + Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractNodeTypesInfo(); + Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = serviceImportParseLogic + .findNodeTypesArtifactsToHandle( + nodeTypesInfo, csarInfo, service); + if (findNodeTypesArtifactsToHandleRes.isRight()) { + log.debug("failed to find node types for update with artifacts during import csar {}. ", + csarInfo.getCsarUUID()); + throw new ComponentException(findNodeTypesArtifactsToHandleRes.right().value()); + } + Service cService = createServiceFromYaml(service, csarInfo.getMainTemplateContent(), csarInfo.getMainTemplateName(), + nodeTypesInfo, csarInfo, findNodeTypesArtifactsToHandleRes.left().value(), true, false, + null); + return cService; + }catch (Exception e){ + log.debug("Exception occured when createServiceFromCsar,error is:{}",e.getMessage(),e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected Service createServiceFromYaml(Service service, String topologyTemplateYaml, + String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate, + boolean shouldLock, boolean inTransaction, String nodeName) { + + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Service createdService; + CreateServiceFromYamlParameter csfyp = new CreateServiceFromYamlParameter(); + try { + ParsedToscaYamlInfo + parsedToscaYamlInfo = csarBusinessLogic.getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, service); + if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances())) { + throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", service.getName()); + csfyp.setYamlName(yamlName); + csfyp.setParsedToscaYamlInfo(parsedToscaYamlInfo); + csfyp.setCreatedArtifacts(createdArtifacts); + csfyp.setTopologyTemplateYaml(topologyTemplateYaml); + csfyp.setNodeTypesInfo(nodeTypesInfo); + csfyp.setCsarInfo(csarInfo); + csfyp.setNodeName(nodeName); + + createdService = createServiceAndRIsFromYaml(service, false, nodeTypesArtifactsToCreate, shouldLock, inTransaction, csfyp); + log.debug("#createResourceFromYaml - The resource {} has been created ", service.getName()); + } catch (ComponentException e) { + log.debug("create Service From Yaml failed,get ComponentException:{}", e); + throw e; + } catch (StorageException e) { + log.debug("create Service From Yaml failed,get StorageException:{}", e); + throw e; + } + return createdService; + } + + protected Service createServiceAndRIsFromYaml(Service service, boolean isNormative, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate, + boolean shouldLock, boolean inTransaction, CreateServiceFromYamlParameter csfyp) { + + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + String yamlName = csfyp.getYamlName(); + ParsedToscaYamlInfo parsedToscaYamlInfo = csfyp.getParsedToscaYamlInfo(); + List<ArtifactDefinition> createdArtifacts = csfyp.getCreatedArtifacts(); + String topologyTemplateYaml = csfyp.getTopologyTemplateYaml(); + Map<String, NodeTypeInfo> nodeTypesInfo = csfyp.getNodeTypesInfo(); + CsarInfo csarInfo = csfyp.getCsarInfo(); + String nodeName = csfyp.getNodeName(); + + if (shouldLock) { + Either<Boolean, ResponseFormat> lockResult = serviceBusinessLogic.lockComponentByName(service.getSystemName(), service, CREATE_RESOURCE); + if (lockResult.isRight()) { + serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ComponentException(lockResult.right().value()); + } + log.debug("name is locked {} status = {}", service.getSystemName(), lockResult); + } + try { + log.trace("************* createResourceFromYaml before full create resource {}", yamlName); + service = serviceImportParseLogic.createServiceTransaction(service, csarInfo.getModifier(), isNormative); + log.trace("************* Going to add inputs from yaml {}", yamlName); + + Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs(); + service = serviceImportParseLogic.createInputsOnService(service, inputs); + log.trace("************* Finish to add inputs from yaml {}", yamlName); + + Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo + .getInstances(); + log.trace("************* Going to create nodes, RI's and Relations from yaml {}", yamlName); + + service = createRIAndRelationsFromYaml(yamlName, service, uploadComponentInstanceInfoMap, + topologyTemplateYaml, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, + nodeTypesArtifactsToCreate, nodeName); + log.trace("************* Finished to create nodes, RI and Relation from yaml {}", yamlName); + Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = serviceBusinessLogic.groupBusinessLogic + .validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), service.getSystemName()); + if (validateUpdateVfGroupNamesRes.isRight()) { + serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ComponentException(validateUpdateVfGroupNamesRes.right().value()); + } + Map<String, GroupDefinition> groups; + log.trace("************* Going to add groups from yaml {}", yamlName); + + if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) { + groups = validateUpdateVfGroupNamesRes.left().value(); + } else { + groups = parsedToscaYamlInfo.getGroups(); + } + + Either<Service, ResponseFormat> createGroupsOnResource = createGroupsOnResource(service, groups); + if (createGroupsOnResource.isRight()) { + serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ComponentException(createGroupsOnResource.right().value()); + } + service = createGroupsOnResource.left().value(); + log.trace("************* Going to add artifacts from yaml {}", yamlName); + + NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, + nodeTypesArtifactsToCreate); + + Either<Service, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts( + ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, createdArtifacts, yamlName, + csarInfo, service, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock); + if (createArtifactsEither.isRight()) { + serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ComponentException(createArtifactsEither.right().value()); + } + service = serviceImportParseLogic.getServiceWithGroups(createArtifactsEither.left().value().getUniqueId()); + ASDCKpiApi.countCreatedResourcesKPI(); + return service; + } catch (ComponentException | StorageException e) { + serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw e; + } finally { + if (!inTransaction) { + serviceBusinessLogic.janusGraphDao.commit(); + } + if (shouldLock) { + serviceBusinessLogic.graphLockOperation.unlockComponentByName(service.getSystemName(), service.getUniqueId(), + NodeTypeEnum.Resource); + } + } + } + + protected Either<Resource, ResponseFormat> createOrUpdateArtifacts( + ArtifactsBusinessLogic.ArtifactOperationEnum operation, List<ArtifactDefinition> createdArtifacts, + String yamlFileName, CsarInfo csarInfo, Resource preparedResource, + NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts, boolean inTransaction, boolean shouldLock) { + + String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName(); + Resource resource = preparedResource; + + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts + .getNodeTypesArtifactsToHandle(); + if (preparedResource.getResourceType() == ResourceTypeEnum.VF) { + if (nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()) { + Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = + handleNodeTypeArtifacts(preparedResource, nodeTypesArtifactsToHandle.get(nodeName), createdArtifacts, csarInfo.getModifier(), inTransaction, true); + if (handleNodeTypeArtifactsRes.isRight()) { + return Either.right(handleNodeTypeArtifactsRes.right().value()); + } + } + } else { + Either<Resource, ResponseFormat> createdCsarArtifactsEither = handleVfCsarArtifacts(preparedResource, csarInfo, createdArtifacts, + new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction); + log.trace("************* Finished to add artifacts from yaml {}", yamlFileName); + if (createdCsarArtifactsEither.isRight()) { + return createdCsarArtifactsEither; + } + resource = createdCsarArtifactsEither.left().value(); + } + return Either.left(resource); + } + + protected Either<Resource, ResponseFormat> handleVfCsarArtifacts(Resource resource, CsarInfo csarInfo, + List<ArtifactDefinition> createdArtifacts, ArtifactOperationInfo artifactOperation, boolean shouldLock, + boolean inTransaction) { + if (csarInfo.getCsar() != null) { + createOrUpdateSingleNonMetaArtifactToComstants(resource, csarInfo, artifactOperation, shouldLock, inTransaction); + + Either<Resource, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, resource, + createdArtifacts, shouldLock, inTransaction, artifactOperation); + if (eitherCreateResult.isRight()) { + return Either.right(eitherCreateResult.right().value()); + } + Either<Resource, StorageOperationStatus> 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<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils.getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils); + + if (artifacsMetaCsarStatus.isLeft()) { + return getResourceResponseFormatEither(resource, csarInfo, createdArtifacts, artifactOperation, shouldLock, inTransaction, artifacsMetaCsarStatus); + } else { + return csarArtifactsAndGroupsBusinessLogic.deleteVFModules(resource, csarInfo, shouldLock, inTransaction); + } + } + return Either.left(resource); + } + + protected void createOrUpdateSingleNonMetaArtifactToComstants(Resource resource, CsarInfo csarInfo, ArtifactOperationInfo artifactOperation, boolean shouldLock, boolean inTransaction) { + String vendorLicenseModelId = null; + String vfLicenseModelId = null; + if (artifactOperation.getArtifactOperationEnum() == ArtifactOperationEnum.UPDATE) { + Map<String, ArtifactDefinition> deploymentArtifactsMap = resource.getDeploymentArtifacts(); + if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) { + for (Map.Entry<String, ArtifactDefinition> artifactEntry : deploymentArtifactsMap.entrySet()) { + if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VENDOR_LICENSE_MODEL)) { + vendorLicenseModelId = artifactEntry.getValue().getUniqueId(); + } + if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VF_LICENSE_MODEL)) { + vfLicenseModelId = artifactEntry.getValue().getUniqueId(); + } + } + } + } + createOrUpdateSingleNonMetaArtifact(resource, csarInfo, + CsarUtils.ARTIFACTS_PATH + Constants.VENDOR_LICENSE_MODEL, Constants.VENDOR_LICENSE_MODEL, + ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, + Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME, + Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId, artifactOperation, null, true, shouldLock, + inTransaction); + createOrUpdateSingleNonMetaArtifact(resource, csarInfo, + CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL, + ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL, + Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId, + artifactOperation, null, true, shouldLock, inTransaction); + } + + protected Either<Resource, ResponseFormat> getResourceResponseFormatEither(Resource resource, CsarInfo csarInfo, List<ArtifactDefinition> createdArtifacts, ArtifactOperationInfo artifactOperation, boolean shouldLock, boolean inTransaction, Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus) { + try { + String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey(); + String artifactsContents = artifacsMetaCsarStatus.left().value().getValue(); + Either<Resource, ResponseFormat> createArtifactsFromCsar; + if (ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) { + createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic + .createResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, + createdArtifacts); + } else { + Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic + .updateResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, + createdArtifacts, shouldLock, inTransaction); + if ((result.left().value() instanceof Resource) && result.isLeft()) { + Resource service1 = (Resource) result.left().value(); + createArtifactsFromCsar = Either.left(service1); + } else { + createArtifactsFromCsar = Either.right(result.right().value()); + } + } + if (createArtifactsFromCsar.isRight()) { + log.debug("Couldn't create artifacts from artifacts.meta"); + return Either.right(createArtifactsFromCsar.right().value()); + } + return Either.left(createArtifactsFromCsar.left().value()); + }catch(Exception e) + { + log.debug("Exception occured in getResourceResponseFormatEither, message:{}", e.getMessage(), e); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + } + } + + protected Either<Resource, ResponseFormat> createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Resource resource, + List<ArtifactDefinition> createdArtifacts, boolean shouldLock, boolean inTransaction, + ArtifactOperationInfo artifactOperation) { + Either<Resource, ResponseFormat> resStatus = null; + Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>(); + try { + Either<List<CsarUtils.NonMetaArtifactInfo>, String> artifactPathAndNameList = getValidArtifactNames(csarInfo, collectedWarningMessages); + if (artifactPathAndNameList.isRight()) { + return Either.right(getComponentsUtils().getResponseFormatByArtifactId( + ActionStatus.ARTIFACT_NAME_INVALID, artifactPathAndNameList.right().value())); + } + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle = null; + + if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) { + vfCsarArtifactsToHandle = new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); + vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value()); + } else { + Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle( + resource, artifactPathAndNameList.left().value(), csarInfo.getModifier()); + if (findVfCsarArtifactsToHandleRes.isRight()) { + resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value()); + } + if (resStatus == null) { + vfCsarArtifactsToHandle = findVfCsarArtifactsToHandleRes.left().value(); + } + } + if (resStatus == null && vfCsarArtifactsToHandle != null) { + resStatus = processCsarArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, resStatus, vfCsarArtifactsToHandle); + } + if (resStatus == null) { + resStatus = Either.left(resource); + } + } catch (Exception e) { + resStatus = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + log.debug("Exception occured in createNonMetaArtifacts, message:{}", e.getMessage(), e); + } finally { + CsarUtils.handleWarningMessages(collectedWarningMessages); + } + return resStatus; + } + + protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandle( + Resource resource, List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, User user) { + + List<ArtifactDefinition> existingArtifacts = new ArrayList<>(); + if (resource.getDeploymentArtifacts() != null && !resource.getDeploymentArtifacts().isEmpty()) { + existingArtifacts.addAll(resource.getDeploymentArtifacts().values()); + } + if (resource.getArtifacts() != null && !resource.getArtifacts().isEmpty()) { + existingArtifacts.addAll(resource.getArtifacts().values()); + } + existingArtifacts = existingArtifacts.stream() + .filter(this::isNonMetaArtifact).collect(toList()); + + List<String> artifactsToIgnore = new ArrayList<>(); + if (resource.getGroups() != null) { + resource.getGroups().stream().forEach(g -> { + if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) { + artifactsToIgnore.addAll(g.getArtifacts()); + } + }); + } + existingArtifacts = existingArtifacts.stream() + .filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(toList()); + return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, resource, user); + } + + protected boolean isNonMetaArtifact(ArtifactDefinition artifact) { + boolean result = true; + if (artifact.getMandatory() || artifact.getArtifactName() == null || !isValidArtifactType(artifact)) { + result = false; + } + return result; + } + + private boolean isValidArtifactType(ArtifactDefinition artifact) { + boolean result = true; + if (artifact.getArtifactType() == null + || ArtifactTypeEnum.findType(artifact.getArtifactType()).equals(ArtifactTypeEnum.VENDOR_LICENSE) + || ArtifactTypeEnum.findType(artifact.getArtifactType()).equals(ArtifactTypeEnum.VF_LICENSE)) { + result = false; + } + return result; + } + + protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation( + List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, List<ArtifactDefinition> existingArtifactsToHandle, + Resource resource, User user) { + + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> nodeTypeArtifactsToHandle = new EnumMap<>( + ArtifactsBusinessLogic.ArtifactOperationEnum.class); + Wrapper<ResponseFormat> responseWrapper = new Wrapper<>(); + Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either + .left(nodeTypeArtifactsToHandle); + try { + // add all found Csar artifacts to list to upload + List<CsarUtils.NonMetaArtifactInfo> artifactsToUpload = new ArrayList<>(artifactPathAndNameList); + List<CsarUtils.NonMetaArtifactInfo> artifactsToUpdate = new ArrayList<>(); + List<CsarUtils.NonMetaArtifactInfo> artifactsToDelete = new ArrayList<>(); + for (CsarUtils.NonMetaArtifactInfo currNewArtifact : artifactPathAndNameList) { + ArtifactDefinition foundArtifact; + if (!existingArtifactsToHandle.isEmpty()) { + foundArtifact = existingArtifactsToHandle.stream() + .filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName())).findFirst() + .orElse(null); + if (foundArtifact != null) { + if (ArtifactTypeEnum.findType(foundArtifact.getArtifactType()).equals(currNewArtifact + .getArtifactType())) { + if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) { + currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId()); + artifactsToUpdate.add(currNewArtifact); + } + existingArtifactsToHandle.remove(foundArtifact); + artifactsToUpload.remove(currNewArtifact); + } else { + log.debug("Can't upload two artifact with the same name {}.", + currNewArtifact.getArtifactName()); + ResponseFormat responseFormat = ResponseFormatManager.getInstance().getResponseFormat( + ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, + currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(), + foundArtifact.getArtifactType()); + AuditingActionEnum auditingAction = serviceBusinessLogic.artifactsBusinessLogic + .detectAuditingType(new ArtifactOperationInfo(false, false, + ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE), foundArtifact.getArtifactChecksum()); + serviceBusinessLogic.artifactsBusinessLogic.handleAuditing(auditingAction, resource, resource.getUniqueId(), + user, null, null, foundArtifact.getUniqueId(), responseFormat, + resource.getComponentType(), null); + responseWrapper.setInnerElement(responseFormat); + break; + } + } + } + } + if (responseWrapper.isEmpty()) { + for (ArtifactDefinition currArtifact : existingArtifactsToHandle) { + if (currArtifact.getIsFromCsar()) { + artifactsToDelete.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null, ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar())); + } else { + artifactsToUpdate.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null, ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar())); + + } + } + } + if (responseWrapper.isEmpty()) { + if (!artifactsToUpload.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, artifactsToUpload); + } + if (!artifactsToUpdate.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE, artifactsToUpdate); + } + if (!artifactsToDelete.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE, artifactsToDelete); + } + } + if (!responseWrapper.isEmpty()) { + nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement()); + } + } catch (Exception e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + responseWrapper.setInnerElement(responseFormat); + log.debug("Exception occured when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e); + nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement()); + } + return nodeTypeArtifactsToHandleRes; + } + + protected Either<Resource, ResponseFormat> processCsarArtifacts(CsarInfo csarInfo, Resource resource, List<ArtifactDefinition> createdArtifacts, boolean shouldLock, boolean inTransaction, Either<Resource, ResponseFormat> resStatus, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle) { + for (Map.Entry<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> currArtifactOperationPair : vfCsarArtifactsToHandle + .entrySet()) { + Optional<ResponseFormat> optionalCreateInDBError = + currArtifactOperationPair.getValue().stream() + .map(e -> createOrUpdateSingleNonMetaArtifact(resource, csarInfo, e.getPath(), + e.getArtifactName(), e.getArtifactType(), + e.getArtifactGroupType(), e.getArtifactLabel(), e.getDisplayName(), + CsarUtils.ARTIFACT_CREATED_FROM_CSAR, e.getArtifactUniqueId(), + new ArtifactOperationInfo(false, false, + currArtifactOperationPair.getKey()), + createdArtifacts, e.isFromCsar(), shouldLock, inTransaction)) + .filter(Either::isRight). + map(e -> e.right().value()). + findAny(); + if (optionalCreateInDBError.isPresent()) { + resStatus = Either.right(optionalCreateInDBError.get()); + break; + } + } + return resStatus; + } + + protected Either<Boolean, ResponseFormat> createOrUpdateSingleNonMetaArtifact(Resource resource, CsarInfo csarInfo, + String artifactPath, String artifactFileName, String artifactType, ArtifactGroupTypeEnum artifactGroupType, + String artifactLabel, String artifactDisplayName, String artifactDescription, String artifactId, + ArtifactOperationInfo operation, List<ArtifactDefinition> createdArtifacts, boolean isFromCsar, boolean shouldLock, + boolean inTransaction) { + byte[] artifactFileBytes = null; + + if (csarInfo.getCsar().containsKey(artifactPath)) { + artifactFileBytes = csarInfo.getCsar().get(artifactPath); + } + Either<Boolean, ResponseFormat> result = Either.left(true); + if (operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE || operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE) { + if (serviceImportParseLogic.isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar)) { + Either<ArtifactDefinition, ResponseFormat> handleDelete = serviceBusinessLogic.artifactsBusinessLogic.handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), resource, + shouldLock, inTransaction); + if (handleDelete.isRight()) { + result = Either.right(handleDelete.right().value()); + } + return result; + } + if (org.apache.commons.lang.StringUtils.isEmpty(artifactId) && artifactFileBytes != null) { + operation = new ArtifactOperationInfo(false, false, + ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE); + } + } + if (artifactFileBytes != null) { + Map<String, Object> vendorLicenseModelJson = ArtifactUtils + .buildJsonForUpdateArtifact(artifactId, artifactFileName, + artifactType, artifactGroupType, artifactLabel, artifactDisplayName, artifactDescription, + artifactFileBytes, null, isFromCsar); + Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts = csarArtifactsAndGroupsBusinessLogic.createOrUpdateCsarArtifactFromJson( + resource, csarInfo.getModifier(), vendorLicenseModelJson, operation); + serviceImportParseLogic + .addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts); + if (eitherNonMetaArtifacts.isRight()) { + BeEcompErrorManager.getInstance() + .logInternalFlowError("UploadLicenseArtifact", "Failed to upload license artifact: " + + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(), + BeEcompErrorManager.ErrorSeverity.WARNING); + return Either.right(eitherNonMetaArtifacts.right().value()); + } + } + return result; + } + + public Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifacts(Resource nodeTypeResource, + Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle, + List<ArtifactDefinition> createdArtifacts, User user, boolean inTransaction, boolean ignoreLifecycleState) { + List<ArtifactDefinition> handleNodeTypeArtifactsRequestRes; + Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = null; + Either<Resource, ResponseFormat> changeStateResponse; + try { + changeStateResponse = checkoutResource(nodeTypeResource, user, inTransaction); + if (changeStateResponse.isRight()) { + return Either.right(changeStateResponse.right().value()); + } + nodeTypeResource = changeStateResponse.left().value(); + + List<ArtifactDefinition> handledNodeTypeArtifacts = new ArrayList<>(); + log.debug("************* Going to handle artifacts of node type resource {}. ", nodeTypeResource.getName()); + for (Map.Entry<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> curOperationEntry : nodeTypeArtifactsToHandle + .entrySet()) { + ArtifactsBusinessLogic.ArtifactOperationEnum curOperation = curOperationEntry.getKey(); + List<ArtifactDefinition> curArtifactsToHandle = curOperationEntry.getValue(); + if (curArtifactsToHandle != null && !curArtifactsToHandle.isEmpty()) { + log.debug("************* Going to {} artifact to vfc {}", curOperation.name(), + nodeTypeResource.getName()); + handleNodeTypeArtifactsRequestRes = serviceBusinessLogic.artifactsBusinessLogic + .handleArtifactsRequestForInnerVfcComponent(curArtifactsToHandle, nodeTypeResource, user, + createdArtifacts, new ArtifactOperationInfo(false, + ignoreLifecycleState, curOperation), + false, inTransaction); + if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(curOperation)) { + createdArtifacts.addAll(handleNodeTypeArtifactsRequestRes); + } + handledNodeTypeArtifacts.addAll(handleNodeTypeArtifactsRequestRes); + } + } + if (handleNodeTypeArtifactsRes == null) { + handleNodeTypeArtifactsRes = Either.left(handledNodeTypeArtifacts); + } + } catch (Exception e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + handleNodeTypeArtifactsRes = Either.right(responseFormat); + log.debug("Exception occured when handleVfcArtifacts, error is:{}", e.getMessage(), e); + } + return handleNodeTypeArtifactsRes; + } + + protected Either<Resource, ResponseFormat> checkoutResource(Resource resource, User user, boolean inTransaction) { + Either<Resource, ResponseFormat> checkoutResourceRes; + try { + if (!resource.getComponentMetadataDefinition().getMetadataDataDefinition().getState() + .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) { + Either<? extends Component, ResponseFormat> checkoutRes = lifecycleBusinessLogic.changeComponentState( + resource.getComponentType(), resource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, + new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT, + LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR), + inTransaction, true); + if (checkoutRes.isRight()) { + checkoutResourceRes = Either.right(checkoutRes.right().value()); + } else { + checkoutResourceRes = Either.left((Resource) checkoutRes.left().value()); + } + } else { + checkoutResourceRes = Either.left(resource); + } + } catch (Exception e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + checkoutResourceRes = Either.right(responseFormat); + log.debug("Exception occured when checkoutResource {} , error is:{}", resource.getName(), e.getMessage(), + e); + } + return checkoutResourceRes; + } + + protected Either<Service, ResponseFormat> createOrUpdateArtifacts( + ArtifactsBusinessLogic.ArtifactOperationEnum operation, List<ArtifactDefinition> createdArtifacts, + String yamlFileName, CsarInfo csarInfo, Service preparedService, + NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts, boolean inTransaction, boolean shouldLock) { + + String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName(); + Service resource = preparedService; + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts + .getNodeTypesArtifactsToHandle(); + + Either<Service, ResponseFormat> createdCsarArtifactsEither = handleVfCsarArtifacts(preparedService, csarInfo, createdArtifacts, + new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction); + log.trace("************* Finished to add artifacts from yaml {}", yamlFileName); + if (createdCsarArtifactsEither.isRight()) { + return createdCsarArtifactsEither; + } + resource = createdCsarArtifactsEither.left().value(); + return Either.left(resource); + } + + protected Either<Service, ResponseFormat> handleVfCsarArtifacts(Service service, CsarInfo csarInfo, + List<ArtifactDefinition> createdArtifacts, ArtifactOperationInfo artifactOperation, boolean shouldLock, + boolean inTransaction) { + + if (csarInfo.getCsar() != null) { + String vendorLicenseModelId = null; + String vfLicenseModelId = null; + if (artifactOperation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE) { + Map<String, ArtifactDefinition> deploymentArtifactsMap = service.getDeploymentArtifacts(); + if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) { + for (Map.Entry<String, ArtifactDefinition> artifactEntry : deploymentArtifactsMap.entrySet()) { + if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VENDOR_LICENSE_MODEL)) { + vendorLicenseModelId = artifactEntry.getValue().getUniqueId(); + } + if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VF_LICENSE_MODEL)) { + vfLicenseModelId = artifactEntry.getValue().getUniqueId(); + } + } + } + } + createOrUpdateSingleNonMetaArtifact(service, csarInfo, + CsarUtils.ARTIFACTS_PATH + Constants.VENDOR_LICENSE_MODEL, Constants.VENDOR_LICENSE_MODEL, + ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, + Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME, + Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId, artifactOperation, null, true, shouldLock, + inTransaction); + createOrUpdateSingleNonMetaArtifact(service, csarInfo, + CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL, + ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL, + Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId, + artifactOperation, null, true, shouldLock, inTransaction); + + Either<Service, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, service, + createdArtifacts, shouldLock, inTransaction, artifactOperation); + if (eitherCreateResult.isRight()) { + return Either.right(eitherCreateResult.right().value()); + } + Either<Service, StorageOperationStatus> eitherGerResource = toscaOperationFacade + .getToscaElement(service.getUniqueId()); + if (eitherGerResource.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), service, ComponentTypeEnum.SERVICE); + return Either.right(responseFormat); + } + service = eitherGerResource.left().value(); + Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils.getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils); + + if (artifacsMetaCsarStatus.isLeft()) { + String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey(); + String artifactsContents = artifacsMetaCsarStatus.left().value().getValue(); + Either<Service, ResponseFormat> createArtifactsFromCsar; + if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) { + createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic.createResourceArtifactsFromCsar(csarInfo, service, artifactsContents, artifactsFileName, createdArtifacts); + } else { + Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic.updateResourceArtifactsFromCsar(csarInfo, service, artifactsContents, artifactsFileName, createdArtifacts, shouldLock, inTransaction); + if ((result.left().value() instanceof Service) && result.isLeft()) { + Service service1 = (Service) result.left().value(); + createArtifactsFromCsar = Either.left(service1); + } else { + createArtifactsFromCsar = Either.right(result.right().value()); + } + } + if (createArtifactsFromCsar.isRight()) { + log.debug("Couldn't create artifacts from artifacts.meta"); + return Either.right(createArtifactsFromCsar.right().value()); + } + return Either.left(createArtifactsFromCsar.left().value()); + } else { + return csarArtifactsAndGroupsBusinessLogic.deleteVFModules(service, csarInfo, shouldLock, inTransaction); + } + } + return Either.left(service); + } + + protected Either<Service, ResponseFormat> createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Service resource, + List<ArtifactDefinition> createdArtifacts, boolean shouldLock, boolean inTransaction, + ArtifactOperationInfo artifactOperation) { + Either<Service, ResponseFormat> resStatus = null; + Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>(); + + try { + Either<List<CsarUtils.NonMetaArtifactInfo>, String> artifactPathAndNameList = getValidArtifactNames(csarInfo, collectedWarningMessages); + if (artifactPathAndNameList.isRight()) { + return Either.right(getComponentsUtils().getResponseFormatByArtifactId( + ActionStatus.ARTIFACT_NAME_INVALID, artifactPathAndNameList.right().value())); + } + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle = null; + + if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) { + vfCsarArtifactsToHandle = new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); + vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value()); + } else { + Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle( + resource, artifactPathAndNameList.left().value(), csarInfo.getModifier()); + + if (findVfCsarArtifactsToHandleRes.isRight()) { + resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value()); + } + if (resStatus == null) { + vfCsarArtifactsToHandle = findVfCsarArtifactsToHandleRes.left().value(); + } + } + if (resStatus == null && vfCsarArtifactsToHandle != null) { + resStatus = processCsarArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, resStatus, vfCsarArtifactsToHandle); + } + if (resStatus == null) { + resStatus = Either.left(resource); + } + } catch (Exception e) { + resStatus = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + log.debug("Exception occured in createNonMetaArtifacts, message:{}", e.getMessage(), e); + } finally { + CsarUtils.handleWarningMessages(collectedWarningMessages); + } + return resStatus; + } + + protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandle( + Service resource, List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, User user) { + List<ArtifactDefinition> existingArtifacts = new ArrayList<>(); + if (resource.getDeploymentArtifacts() != null && !resource.getDeploymentArtifacts().isEmpty()) { + existingArtifacts.addAll(resource.getDeploymentArtifacts().values()); + } + if (resource.getArtifacts() != null && !resource.getArtifacts().isEmpty()) { + existingArtifacts.addAll(resource.getArtifacts().values()); + } + existingArtifacts = existingArtifacts.stream().filter(this::isNonMetaArtifact).collect(toList()); + + List<String> artifactsToIgnore = new ArrayList<>(); + if (resource.getGroups() != null) { + resource.getGroups().stream().forEach(g -> { + if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) { + artifactsToIgnore.addAll(g.getArtifacts()); + } + }); + } + existingArtifacts = existingArtifacts.stream() + .filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(toList()); + return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, resource, user); + } + + protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation( + List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, List<ArtifactDefinition> existingArtifactsToHandle, + Service resource, User user) { + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> nodeTypeArtifactsToHandle = new EnumMap<>( + ArtifactsBusinessLogic.ArtifactOperationEnum.class); + Wrapper<ResponseFormat> responseWrapper = new Wrapper<>(); + Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either + .left(nodeTypeArtifactsToHandle); + try { + List<CsarUtils.NonMetaArtifactInfo> artifactsToUpload = new ArrayList<>(artifactPathAndNameList); + List<CsarUtils.NonMetaArtifactInfo> artifactsToUpdate = new ArrayList<>(); + List<CsarUtils.NonMetaArtifactInfo> artifactsToDelete = new ArrayList<>(); + for (CsarUtils.NonMetaArtifactInfo currNewArtifact : artifactPathAndNameList) { + ArtifactDefinition foundArtifact; + + if (!existingArtifactsToHandle.isEmpty()) { + foundArtifact = existingArtifactsToHandle.stream() + .filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName())).findFirst() + .orElse(null); + if (foundArtifact != null) { + if (ArtifactTypeEnum.findType(foundArtifact.getArtifactType()).equals(currNewArtifact + .getArtifactType())) { + if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) { + currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId()); + artifactsToUpdate.add(currNewArtifact); + } + existingArtifactsToHandle.remove(foundArtifact); + artifactsToUpload.remove(currNewArtifact); + } else { + log.debug("Can't upload two artifact with the same name {}.", + currNewArtifact.getArtifactName()); + ResponseFormat responseFormat = ResponseFormatManager.getInstance().getResponseFormat( + ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, + currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(), + foundArtifact.getArtifactType()); + AuditingActionEnum auditingAction = serviceBusinessLogic.artifactsBusinessLogic + .detectAuditingType(new ArtifactOperationInfo(false, false, + ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE), foundArtifact.getArtifactChecksum()); + serviceBusinessLogic.artifactsBusinessLogic.handleAuditing(auditingAction, resource, resource.getUniqueId(), + user, null, null, foundArtifact.getUniqueId(), responseFormat, + resource.getComponentType(), null); + responseWrapper.setInnerElement(responseFormat); + break; + } + } + } + } + if (responseWrapper.isEmpty()) { + for (ArtifactDefinition currArtifact : existingArtifactsToHandle) { + if (currArtifact.getIsFromCsar()) { + artifactsToDelete.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null, ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar())); + } else { + artifactsToUpdate.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null, ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar())); + } + } + } + if (responseWrapper.isEmpty()) { + if (!artifactsToUpload.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, artifactsToUpload); + } + if (!artifactsToUpdate.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE, artifactsToUpdate); + } + if (!artifactsToDelete.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE, artifactsToDelete); + } + } + if (!responseWrapper.isEmpty()) { + nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement()); + } + } catch (Exception e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + responseWrapper.setInnerElement(responseFormat); + log.debug("Exception occured when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e); + nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement()); + } + return nodeTypeArtifactsToHandleRes; + } + + protected Either<Service, ResponseFormat> processCsarArtifacts(CsarInfo csarInfo, Service resource, List<ArtifactDefinition> createdArtifacts, boolean shouldLock, boolean inTransaction, Either<Service, ResponseFormat> resStatus, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle) { + for (Map.Entry<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> currArtifactOperationPair : vfCsarArtifactsToHandle + .entrySet()) { + Optional<ResponseFormat> optionalCreateInDBError = + currArtifactOperationPair.getValue().stream() + .map(e -> createOrUpdateSingleNonMetaArtifact(resource, csarInfo, e.getPath(), + e.getArtifactName(), e.getArtifactType(), + e.getArtifactGroupType(), e.getArtifactLabel(), e.getDisplayName(), + CsarUtils.ARTIFACT_CREATED_FROM_CSAR, e.getArtifactUniqueId(), + new ArtifactOperationInfo(false, false, + currArtifactOperationPair.getKey()), + createdArtifacts, e.isFromCsar(), shouldLock, inTransaction)) + .filter(Either::isRight). + map(e -> e.right().value()). + findAny(); + if (optionalCreateInDBError.isPresent()) { + resStatus = Either.right(optionalCreateInDBError.get()); + break; + } + } + return resStatus; + } + + public ComponentsUtils getComponentsUtils() { + return this.componentsUtils; + } + + public void setComponentsUtils(ComponentsUtils componentsUtils) { + this.componentsUtils = componentsUtils; + } + + protected Either<List<CsarUtils.NonMetaArtifactInfo>, String> getValidArtifactNames(CsarInfo csarInfo, Map<String, Set<List<String>>> collectedWarningMessages) { + List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList = + csarInfo.getCsar().entrySet().stream() + .filter(e -> Pattern.compile(VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()) + .matches()) + .map(e -> CsarUtils.validateNonMetaArtifact(e.getKey(), e.getValue(), + collectedWarningMessages)) + .filter(Either::isLeft) + .map(e -> e.left().value()) + .collect(toList()); + Pattern englishNumbersAndUnderScoresOnly = Pattern.compile(CsarUtils.VALID_ENGLISH_ARTIFACT_NAME); + for (CsarUtils.NonMetaArtifactInfo nonMetaArtifactInfo : artifactPathAndNameList) { + if (!englishNumbersAndUnderScoresOnly.matcher(nonMetaArtifactInfo.getDisplayName()).matches()) { + return Either.right(nonMetaArtifactInfo.getArtifactName()); + } + } + return Either.left(artifactPathAndNameList); + } + + protected Either<Boolean, ResponseFormat> createOrUpdateSingleNonMetaArtifact(Service service, CsarInfo csarInfo, + String artifactPath, String artifactFileName, String artifactType, ArtifactGroupTypeEnum artifactGroupType, + String artifactLabel, String artifactDisplayName, String artifactDescription, String artifactId, + ArtifactOperationInfo operation, List<ArtifactDefinition> createdArtifacts, boolean isFromCsar, boolean shouldLock, + boolean inTransaction) { + byte[] artifactFileBytes = null; + if (csarInfo.getCsar().containsKey(artifactPath)) { + artifactFileBytes = csarInfo.getCsar().get(artifactPath); + } + Either<Boolean, ResponseFormat> result = Either.left(true); + if (operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE || operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE) { + if (serviceImportParseLogic.isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar)) { + Either<ArtifactDefinition, ResponseFormat> handleDelete = serviceBusinessLogic.artifactsBusinessLogic.handleDelete(service.getUniqueId(), artifactId, csarInfo.getModifier(), service, + shouldLock, inTransaction); + if (handleDelete.isRight()) { + result = Either.right(handleDelete.right().value()); + } + return result; + } + if (org.apache.commons.lang.StringUtils.isEmpty(artifactId) && artifactFileBytes != null) { + operation = new ArtifactOperationInfo(false, false, + ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE); + } + } + if (artifactFileBytes != null) { + Map<String, Object> vendorLicenseModelJson = ArtifactUtils.buildJsonForUpdateArtifact(artifactId, artifactFileName, + artifactType, artifactGroupType, artifactLabel, artifactDisplayName, artifactDescription, + artifactFileBytes, null, isFromCsar); + Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts = csarArtifactsAndGroupsBusinessLogic.createOrUpdateCsarArtifactFromJson( + service, csarInfo.getModifier(), vendorLicenseModelJson, operation); + serviceImportParseLogic + .addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts); + if (eitherNonMetaArtifacts.isRight()) { + BeEcompErrorManager.getInstance() + .logInternalFlowError("UploadLicenseArtifact", "Failed to upload license artifact: " + + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(), + BeEcompErrorManager.ErrorSeverity.WARNING); + return Either.right(eitherNonMetaArtifacts.right().value()); + } + } + return result; + } + + protected Either<Service, ResponseFormat> createGroupsOnResource(Service service, + Map<String, GroupDefinition> groups) { + if (groups != null && !groups.isEmpty()) { + List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, service); + serviceImportParseLogic.handleGroupsProperties(service, groups); + serviceImportParseLogic.fillGroupsFinalFields(groupsAsList); + Either<List<GroupDefinition>, ResponseFormat> createGroups = serviceBusinessLogic.groupBusinessLogic.createGroups(service, + groupsAsList, true); + if (createGroups.isRight()) { + return Either.right(createGroups.right().value()); + } + } else { + return Either.left(service); + } + Either<Service, StorageOperationStatus> updatedResource = toscaOperationFacade + .getToscaElement(service.getUniqueId()); + if (updatedResource.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(updatedResource.right().value()), service, ComponentTypeEnum.SERVICE); + return Either.right(responseFormat); + } + return Either.left(updatedResource.left().value()); + } + + protected List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Service component) { + List<GroupDefinition> result = new ArrayList<>(); + List<ComponentInstance> componentInstances = component.getComponentInstances(); + if (groups != null) { + Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies = serviceImportParseLogic.validateCyclicGroupsDependencies(groups); + if (validateCyclicGroupsDependencies.isRight()) { + throw new ComponentException(validateCyclicGroupsDependencies.right().value()); + } + for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) { + String groupName = entry.getKey(); + GroupDefinition groupDefinition = entry.getValue(); + GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition); + updatedGroupDefinition.setMembers(null); + Map<String, String> members = groupDefinition.getMembers(); + if (members != null) { + serviceImportParseLogic + .updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members); + } + result.add(updatedGroupDefinition); + } + } + return result; + } + + + protected Resource createRIAndRelationsFromYaml(String yamlName, Resource resource, + Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap, + String topologyTemplateYaml, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, + Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate, + String nodeName) { + try { + log.debug("************* Going to create all nodes {}", yamlName); + handleNodeTypes(yamlName, resource, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, + nodeTypesInfo, csarInfo, nodeName); + log.debug("************* Going to create all resource instances {}", yamlName); + resource = createResourceInstances(yamlName, resource, + uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes()); + log.debug("************* Finished to create all resource instances {}", yamlName); + resource = createResourceInstancesRelations(csarInfo.getModifier(), yamlName, resource, uploadComponentInstanceInfoMap); + log.debug("************* Going to create positions {}", yamlName); + compositionBusinessLogic.setPositionsForComponentInstances(resource, csarInfo.getModifier().getUserId()); + log.debug("************* Finished to set positions {}", yamlName); + return resource; + } catch (Exception e) { + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected Resource createResourceInstancesRelations(User user, String yamlName, Resource resource, + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) { + log.debug("#createResourceInstancesRelations - Going to create relations "); + List<ComponentInstance> componentInstancesList = resource.getComponentInstances(); + if (((MapUtils.isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList)) && + resource.getResourceType() != ResourceTypeEnum.PNF)) { // PNF can have no resource instances + log.debug("#createResourceInstancesRelations - No instances found in the resource {} is empty, yaml template file name {}, ", resource.getUniqueId(), yamlName); + BeEcompErrorManager.getInstance().logInternalDataError("createResourceInstancesRelations", "No instances found in a resource or nn yaml template. ", BeEcompErrorManager.ErrorSeverity.ERROR); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName)); + } + Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>(); + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>(); + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>(); + Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>(); + Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>(); + Map<String, List<AttributeDataDefinition>> instAttributes = new HashMap<>(); + Map<String, Resource> originCompMap = new HashMap<>(); + List<RequirementCapabilityRelDef> relations = new ArrayList<>(); + Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>(); + + log.debug("enter ServiceImportBusinessLogic createResourceInstancesRelations#createResourceInstancesRelations - Before get all datatypes. "); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = serviceBusinessLogic.dataTypeCache.getAll(); + if (allDataTypes.isRight()) { + JanusGraphOperationStatus status = allDataTypes.right().value(); + BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", + "Failed to update property value on instance. Status is " + status, BeEcompErrorManager.ErrorSeverity.ERROR); + throw new ComponentException(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse( + DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)), yamlName)); + } + Resource finalResource = resource; + uploadResInstancesMap + .values() + .forEach(i -> processComponentInstance(yamlName, finalResource, componentInstancesList, allDataTypes, + instProperties, instCapabilities, instRequirements, instDeploymentArtifacts, + instArtifacts, instAttributes, originCompMap, instInputs, i)); + serviceImportParseLogic.associateComponentInstancePropertiesToComponent(yamlName, resource, instProperties); + serviceImportParseLogic.associateComponentInstanceInputsToComponent(yamlName, resource, instInputs); + serviceImportParseLogic + .associateDeploymentArtifactsToInstances(user, yamlName, resource, instDeploymentArtifacts); + serviceImportParseLogic.associateArtifactsToInstances(yamlName, resource, instArtifacts); + serviceImportParseLogic.associateOrAddCalculatedCapReq(yamlName, resource, instCapabilities, instRequirements); + serviceImportParseLogic.associateInstAttributeToComponentToInstances(yamlName, resource, instAttributes); + resource = serviceImportParseLogic.getResourceAfterCreateRelations(resource); + + serviceImportParseLogic + .addRelationsToRI(yamlName, resource, uploadResInstancesMap, componentInstancesList, relations); + serviceImportParseLogic.associateResourceInstances(yamlName, resource, relations); + handleSubstitutionMappings(resource, uploadResInstancesMap); + log.debug("************* in create relations, getResource start"); + Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(resource.getUniqueId()); + log.debug("************* in create relations, getResource end"); + if (eitherGetResource.isRight()) { + throw new ComponentException(componentsUtils.getResponseFormatByResource( + componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource)); + } + return eitherGetResource.left().value(); + } + + protected void processComponentInstance(String yamlName, Resource resource, List<ComponentInstance> componentInstancesList, Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes, Map<String, List<ComponentInstanceProperty>> instProperties, Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements, Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts, Map<String, Map<String, ArtifactDefinition>> instArtifacts, Map<String, List<AttributeDataDefinition>> instAttributes, Map<String, Resource> originCompMap, Map<String, List<ComponentInstanceInput>> instInputs, UploadComponentInstanceInfo uploadComponentInstanceInfo) { + Optional<ComponentInstance> currentCompInstanceOpt = componentInstancesList.stream() + .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName())) + .findFirst(); + if (!currentCompInstanceOpt.isPresent()) { + log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), + resource.getUniqueId()); + BeEcompErrorManager.getInstance().logInternalDataError( + COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, + resource.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + throw new ComponentException(responseFormat); + } + ComponentInstance currentCompInstance = currentCompInstanceOpt.get(); + String resourceInstanceId = currentCompInstance.getUniqueId(); + Resource originResource = getOriginResource(yamlName, originCompMap, currentCompInstance); + log.debug("enter processComponentInstance,get originResource Requirements:{}", + originResource.getRequirements()); + if (MapUtils.isNotEmpty(originResource.getRequirements())) { + instRequirements.put(currentCompInstance, originResource.getRequirements()); + } + if (MapUtils.isNotEmpty(originResource.getCapabilities())) { + processComponentInstanceCapabilities(allDataTypes, instCapabilties, uploadComponentInstanceInfo, + currentCompInstance, originResource); + } + if (originResource.getDeploymentArtifacts() != null && !originResource.getDeploymentArtifacts().isEmpty()) { + instDeploymentArtifacts.put(resourceInstanceId, originResource.getDeploymentArtifacts()); + } + if (originResource.getArtifacts() != null && !originResource.getArtifacts().isEmpty()) { + instArtifacts.put(resourceInstanceId, originResource.getArtifacts()); + } + if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) { + instAttributes.put(resourceInstanceId, originResource.getAttributes()); + } + if (originResource.getResourceType() != ResourceTypeEnum.VF) { + ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, resource, + originResource, currentCompInstance, instProperties, allDataTypes.left().value()); + if (addPropertiesValueToRiRes.getStatus() != 200) { + throw new ComponentException(addPropertiesValueToRiRes); + } + } else { + addInputsValuesToRi(uploadComponentInstanceInfo, resource, + originResource, currentCompInstance, instInputs, allDataTypes.left().value()); + } + } + + protected void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, + Resource resource, Resource originResource, ComponentInstance currentCompInstance, + Map<String, List<ComponentInstanceInput>> instInputs, Map<String, DataTypeDefinition> allDataTypes) { + Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties(); + try { + if (MapUtils.isNotEmpty(propMap)) { + Map<String, InputDefinition> currPropertiesMap = new HashMap<>(); + List<ComponentInstanceInput> instPropList = new ArrayList<>(); + + if (CollectionUtils.isEmpty(originResource.getInputs())) { + log.debug("failed to find properties "); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND)); + } + originResource.getInputs().forEach(p -> serviceImportParseLogic.addInput(currPropertiesMap, p)); + for (List<UploadPropInfo> propertyList : propMap.values()) { + processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList); + } + currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p))); + instInputs.put(currentCompInstance.getUniqueId(), instPropList); + } + } catch (Exception e) { + log.debug("failed to add Inputs Values To Ri"); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected void processProperty(Resource resource, ComponentInstance currentCompInstance, Map<String, DataTypeDefinition> allDataTypes, Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList, List<UploadPropInfo> propertyList) { + UploadPropInfo propertyInfo = propertyList.get(0); + String propName = propertyInfo.getName(); + if (!currPropertiesMap.containsKey(propName)) { + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, + propName)); + } + InputDefinition curPropertyDef = currPropertiesMap.get(propName); + ComponentInstanceInput property = null; + + String value = null; + List<GetInputValueDataDefinition> getInputs = null; + boolean isValidate = true; + if (propertyInfo.getValue() != null) { + getInputs = propertyInfo.getGet_input(); + isValidate = getInputs == null || getInputs.isEmpty(); + if (isValidate) { + value = getPropertyJsonStringValue(propertyInfo.getValue(), + curPropertyDef.getType()); + } else { + value = getPropertyJsonStringValue(propertyInfo.getValue(), + TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()); + } + } + String innerType = null; + property = new ComponentInstanceInput(curPropertyDef, value, null); + String validPropertyVAlue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes); + property.setValue(validPropertyVAlue); + + if (isNotEmpty(getInputs)) { + List<GetInputValueDataDefinition> getInputValues = new ArrayList<>(); + for (GetInputValueDataDefinition getInput : getInputs) { + List<InputDefinition> inputs = resource.getInputs(); + if (CollectionUtils.isEmpty(inputs)) { + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)); + } + + Optional<InputDefinition> optional = inputs.stream() + .filter(p -> p.getName().equals(getInput.getInputName())).findAny(); + if (!optional.isPresent()) { + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)); + } + InputDefinition input = optional.get(); + getInput.setInputId(input.getUniqueId()); + getInputValues.add(getInput); + + GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex(); + processGetInput(getInputValues, inputs, getInputIndex); + } + property.setGetInputValues(getInputValues); + } + instPropList.add(property); + currPropertiesMap.remove(property.getName()); + } + + protected void handleSubstitutionMappings(Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) { + if (resource.getResourceType() == ResourceTypeEnum.VF) { + Either<Resource, StorageOperationStatus> getResourceRes = toscaOperationFacade.getToscaFullElement(resource.getUniqueId()); + if (getResourceRes.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource( + componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource); + throw new ComponentException(responseFormat); + } + getResourceRes = updateCalculatedCapReqWithSubstitutionMappings(getResourceRes.left().value(), + uploadResInstancesMap); + if (getResourceRes.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource( + componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource); + throw new ComponentException(responseFormat); + } + } + } + + protected Resource createResourceInstances(String yamlName, Resource resource, + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap, + Map<String, Resource> nodeNamespaceMap) { + Either<Resource, ResponseFormat> eitherResource = null; + log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName); + if (MapUtils.isEmpty(uploadResInstancesMap) && resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE); + throw new ComponentException(responseFormat); + } + Map<String, Resource> existingNodeTypeMap = new HashMap<>(); + if (MapUtils.isNotEmpty(nodeNamespaceMap)) { + nodeNamespaceMap.forEach((k, v) -> existingNodeTypeMap.put(v.getToscaResourceName(), v)); + } + Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>(); + uploadResInstancesMap + .values() + .forEach(i -> createAndAddResourceInstance(i, yamlName, resource, nodeNamespaceMap, existingNodeTypeMap, resourcesInstancesMap)); + + if (MapUtils.isNotEmpty(resourcesInstancesMap)) { + try { + toscaOperationFacade.associateComponentInstancesToComponent(resource, + resourcesInstancesMap, false, false); + } catch (StorageException exp) { + if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) { + log.debug("Failed to add component instances to container component {}", resource.getName()); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus())); + eitherResource = Either.right(responseFormat); + throw new ByResponseFormatComponentException(eitherResource.right().value()); + } + } + } + log.debug("*************Going to get resource {}", resource.getUniqueId()); + Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade + .getToscaElement(resource.getUniqueId(), serviceImportParseLogic.getComponentWithInstancesFilter()); + log.debug("*************finished to get resource {}", resource.getUniqueId()); + if (eitherGetResource.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource( + componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource); + throw new ComponentException(responseFormat); + } + if (CollectionUtils.isEmpty(eitherGetResource.left().value().getComponentInstances()) && + resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances + log.debug("Error when create resource instance from csar. ComponentInstances list empty"); + BeEcompErrorManager.getInstance().logBeDaoSystemError( + "Error when create resource instance from csar. ComponentInstances list empty"); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE)); + } + return eitherGetResource.left().value(); + } + + protected void handleNodeTypes(String yamlName, Resource resource, + String topologyTemplateYaml, boolean needLock, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, + CsarInfo csarInfo, String nodeName) { + try { + for (Map.Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) { + if (nodeTypeEntry.getValue().isNested()) { + + handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, + nodeTypesInfo, csarInfo, nodeTypeEntry.getKey()); + log.trace("************* finished to create node {}", nodeTypeEntry.getKey()); + } + } + Map<String, Object> mappedToscaTemplate = null; + if (org.apache.commons.lang.StringUtils.isNotEmpty(nodeName) && MapUtils.isNotEmpty(nodeTypesInfo) + && nodeTypesInfo.containsKey(nodeName)) { + mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate(); + } + if (MapUtils.isEmpty(mappedToscaTemplate)) { + mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml); + } + createResourcesFromYamlNodeTypesList(yamlName, resource, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle, + nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo); + } catch (ComponentException e) { + ResponseFormat responseFormat = e.getResponseFormat() != null ? e.getResponseFormat() + : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE); + throw e; + } catch (StorageException e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus())); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE); + throw e; + }catch (Exception e){ + log.debug("Exception occured when handleNodeTypes, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected Resource handleNestedVfc(Resource resource, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle, + List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, + String nodeName) { + String yamlName = nodesInfo.get(nodeName).getTemplateFileName(); + Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate(); + + log.debug("************* Going to create node types from yaml {}", yamlName); + createResourcesFromYamlNodeTypesList(yamlName, resource, nestedVfcJsonMap, false, + nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo); + + if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) { + log.debug("************* Going to handle complex VFC from yaml {}", yamlName); + resource = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo, + csarInfo, nodeName, yamlName); + } + return resource; + } + + protected Resource handleComplexVfc(Resource resource, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle, + List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, + String nodeName, String yamlName) { + Resource oldComplexVfc = null; + Resource newComplexVfc = serviceImportParseLogic.buildValidComplexVfc(resource, csarInfo, nodeName, nodesInfo); + Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade + .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName()); + if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) { + oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName( + serviceImportParseLogic.buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(), + nodeName).getRight()); + } + if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) { + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } else if (oldComplexVfcRes.isLeft()) { + log.debug(VALIDATE_DERIVED_BEFORE_UPDATE); + Either<Boolean, ResponseFormat> eitherValidation = serviceImportParseLogic.validateNestedDerivedFromDuringUpdate( + oldComplexVfcRes.left().value(), newComplexVfc, + ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion())); + if (eitherValidation.isLeft()) { + oldComplexVfc = oldComplexVfcRes.left().value(); + } + } + newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, + oldComplexVfc, newComplexVfc); + csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName()); + LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction( + CERTIFICATION_ON_IMPORT, LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR); + log.debug("Going to certify cvfc {}. ", newComplexVfc.getName()); + final Resource result = serviceImportParseLogic + .propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, + true); + csarInfo.getCreatedNodes().put(nodeName, result); + csarInfo.removeNodeFromQueue(); + return result; + } + + public Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, Map<String, Object> mappedToscaTemplate, boolean needLock, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, + CsarInfo csarInfo) { + Either<String, ImportUtils.ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate, + TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION); + if (toscaVersion.isRight()) { + throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE); + } + Map<String, Object> mapToConvert = new HashMap<>(); + mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value()); + Map<String, Object> nodeTypes = serviceImportParseLogic.getNodeTypesFromTemplate(mappedToscaTemplate); + createNodeTypes(yamlName, resource, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert, nodeTypes); + return csarInfo.getCreatedNodes(); + } + + protected void createNodeTypes(String yamlName, Resource resource, boolean needLock, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) { + Iterator<Map.Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator(); + Resource vfcCreated = null; + while (nodesNameValueIter.hasNext()) { + Map.Entry<String, Object> nodeType = nodesNameValueIter.next(); + Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = nodeTypesArtifactsToHandle == null + || nodeTypesArtifactsToHandle.isEmpty() ? null + : nodeTypesArtifactsToHandle.get(nodeType.getKey()); + + if (nodeTypesInfo.containsKey(nodeType.getKey())) { + log.trace("************* Going to handle nested vfc {}", nodeType.getKey()); + vfcCreated = handleNestedVfc(resource, + nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, + nodeType.getKey()); + log.trace("************* Finished to handle nested vfc {}", nodeType.getKey()); + } else if (csarInfo.getCreatedNodesToscaResourceNames() != null + && !csarInfo.getCreatedNodesToscaResourceNames().containsKey(nodeType.getKey())) { + log.trace("************* Going to create node {}", nodeType.getKey()); + ImmutablePair<Resource, ActionStatus> resourceCreated = createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(), mapToConvert, + resource, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, + csarInfo, true); + log.debug("************* Finished to create node {}", nodeType.getKey()); + + vfcCreated = resourceCreated.getLeft(); + csarInfo.getCreatedNodesToscaResourceNames().put(nodeType.getKey(), + vfcCreated.getToscaResourceName()); + } + if (vfcCreated != null) { + csarInfo.getCreatedNodes().put(nodeType.getKey(), vfcCreated); + } + mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName()); + } + } + + protected ImmutablePair<Resource, ActionStatus> createNodeTypeResourceFromYaml( + String yamlName, Map.Entry<String, Object> nodeNameValue, User user, Map<String, Object> mapToConvert, + Resource resourceVf, boolean needLock, + Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle, + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, boolean forceCertificationAllowed, CsarInfo csarInfo, + boolean isNested) { + UploadResourceInfo resourceMetaData = serviceImportParseLogic + .fillResourceMetadata(yamlName, resourceVf, nodeNameValue.getKey(), user); + + String singleVfcYaml = serviceImportParseLogic.buildNodeTypeYaml(nodeNameValue, mapToConvert, + resourceMetaData.getResourceType(), csarInfo); + user = serviceBusinessLogic.validateUser(user, "CheckIn Resource", resourceVf, AuditingActionEnum.CHECKIN_RESOURCE, true); + return serviceImportParseLogic.createResourceFromNodeType(singleVfcYaml, resourceMetaData, user, true, needLock, + nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, + nodeNameValue.getKey(), isNested); + } + + protected Service createRIAndRelationsFromYaml(String yamlName, Service service, + Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap, + String topologyTemplateYaml, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, + Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate, + String nodeName) { + log.debug("************* Going to create all nodes {}", yamlName); + handleServiceNodeTypes(yamlName, service, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeName); + log.debug("************* Going to create all resource instances {}", yamlName); + service = createServiceInstances(yamlName, service, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes()); + log.debug("************* Going to create all relations {}", yamlName); + service = createServiceInstancesRelations(csarInfo.getModifier(), yamlName, service, uploadComponentInstanceInfoMap); + log.debug("************* Going to create positions {}", yamlName); + compositionBusinessLogic.setPositionsForComponentInstances(service, csarInfo.getModifier().getUserId()); + log.debug("************* Finished to set positions {}", yamlName); + return service; + } + + protected Service createServiceInstancesRelations(User user, String yamlName, Service service, + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) { + log.debug("#createResourceInstancesRelations - Going to create relations "); + List<ComponentInstance> componentInstancesList = service.getComponentInstances(); + if (((MapUtils.isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList)))) { // PNF can have no resource instances + log.debug("#createResourceInstancesRelations - No instances found in the resource {} is empty, yaml template file name {}, ", service.getUniqueId(), yamlName); + BeEcompErrorManager.getInstance().logInternalDataError("createResourceInstancesRelations", "No instances found in a resource or nn yaml template. ", BeEcompErrorManager.ErrorSeverity.ERROR); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName)); + } + Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>(); + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>(); + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>(); + Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>(); + Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>(); + Map<String, List<AttributeDataDefinition>> instAttributes = new HashMap<>(); + Map<String, Resource> originCompMap = new HashMap<>(); + List<RequirementCapabilityRelDef> relations = new ArrayList<>(); + Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>(); + + log.debug("enter ServiceImportBusinessLogic createServiceInstancesRelations#createResourceInstancesRelations - Before get all datatypes. "); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = serviceBusinessLogic.dataTypeCache.getAll(); + if (allDataTypes.isRight()) { + JanusGraphOperationStatus status = allDataTypes.right().value(); + BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", + "Failed to update property value on instance. Status is " + status, BeEcompErrorManager.ErrorSeverity.ERROR); + throw new ComponentException(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse( + DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)), yamlName)); + } + Service finalResource = service; + uploadResInstancesMap + .values() + .forEach(i -> processComponentInstance(yamlName, finalResource, componentInstancesList, allDataTypes, + instProperties, instCapabilities, instRequirements, instDeploymentArtifacts, + instArtifacts, instAttributes, originCompMap, instInputs, i)); + serviceImportParseLogic.associateComponentInstancePropertiesToComponent(yamlName, service, instProperties); + serviceImportParseLogic.associateComponentInstanceInputsToComponent(yamlName, service, instInputs); + serviceImportParseLogic.associateDeploymentArtifactsToInstances(user, yamlName, service, instDeploymentArtifacts); + serviceImportParseLogic.associateArtifactsToInstances(yamlName, service, instArtifacts); + serviceImportParseLogic.associateOrAddCalculatedCapReq(yamlName, service, instCapabilities, instRequirements); + log.debug("enter createServiceInstancesRelations test,instRequirements:{},instCapabilities:{}", + instRequirements, instCapabilities); + serviceImportParseLogic.associateInstAttributeToComponentToInstances(yamlName, service, instAttributes); + ToscaElement serviceTemplate = ModelConverter.convertToToscaElement(service); + Map<String, ListCapabilityDataDefinition> capabilities = serviceTemplate.getCapabilities(); + Map<String, ListRequirementDataDefinition> requirements = serviceTemplate.getRequirements(); + + serviceImportParseLogic.associateCapabilitiesToService(yamlName, service, capabilities); + serviceImportParseLogic.associateRequirementsToService(yamlName, service, requirements); + service = getResourceAfterCreateRelations(service); + + addRelationsToRI(yamlName, service, uploadResInstancesMap, componentInstancesList, relations); + serviceImportParseLogic.associateResourceInstances(yamlName, service, relations); + handleSubstitutionMappings(service, uploadResInstancesMap); + log.debug("************* in create relations, getResource start"); + Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(service.getUniqueId()); + log.debug("************* in create relations, getResource end"); + if (eitherGetResource.isRight()) { + throw new ComponentException(componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), service, service.getComponentType())); + } + return eitherGetResource.left().value(); + } + + protected void processComponentInstance(String yamlName, Service service, List<ComponentInstance> componentInstancesList, Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes, Map<String, List<ComponentInstanceProperty>> instProperties, Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements, Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts, Map<String, Map<String, ArtifactDefinition>> instArtifacts, Map<String, List<AttributeDataDefinition>> instAttributes, Map<String, Resource> originCompMap, Map<String, List<ComponentInstanceInput>> instInputs, UploadComponentInstanceInfo uploadComponentInstanceInfo) { + log.debug("enter ServiceImportBusinessLogic processComponentInstance"); + Optional<ComponentInstance> currentCompInstanceOpt = componentInstancesList.stream() + .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName())) + .findFirst(); + if (!currentCompInstanceOpt.isPresent()) { + log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), + service.getUniqueId()); + BeEcompErrorManager.getInstance().logInternalDataError( + COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, + service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + throw new ComponentException(responseFormat); + } + ComponentInstance currentCompInstance = currentCompInstanceOpt.get(); + String resourceInstanceId = currentCompInstance.getUniqueId(); + Resource originResource = getOriginResource(yamlName, originCompMap, currentCompInstance); + + if (MapUtils.isNotEmpty(originResource.getRequirements())) { + instRequirements.put(currentCompInstance, originResource.getRequirements()); + } + if (MapUtils.isNotEmpty(originResource.getCapabilities())) { + processComponentInstanceCapabilities(allDataTypes, instCapabilties, uploadComponentInstanceInfo, + currentCompInstance, originResource); + } + if (originResource.getDeploymentArtifacts() != null && !originResource.getDeploymentArtifacts().isEmpty()) { + instDeploymentArtifacts.put(resourceInstanceId, originResource.getDeploymentArtifacts()); + } + if (originResource.getArtifacts() != null && !originResource.getArtifacts().isEmpty()) { + instArtifacts.put(resourceInstanceId, originResource.getArtifacts()); + } + if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) { + instAttributes.put(resourceInstanceId, originResource.getAttributes()); + } + if (originResource.getResourceType() != ResourceTypeEnum.VF) { + ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, service, + originResource, currentCompInstance, instProperties, allDataTypes.left().value()); + if (addPropertiesValueToRiRes.getStatus() != 200) { + throw new ComponentException(addPropertiesValueToRiRes); + } + } else { + addInputsValuesToRi(uploadComponentInstanceInfo, service, + originResource, currentCompInstance, instInputs, allDataTypes.left().value()); + } + } + + protected void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, + Service resource, Resource originResource, ComponentInstance currentCompInstance, + Map<String, List<ComponentInstanceInput>> instInputs, Map<String, DataTypeDefinition> allDataTypes) { + Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties(); + if (MapUtils.isNotEmpty(propMap)) { + Map<String, InputDefinition> currPropertiesMap = new HashMap<>(); + List<ComponentInstanceInput> instPropList = new ArrayList<>(); + + if (CollectionUtils.isEmpty(originResource.getInputs())) { + log.debug("failed to find properties "); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND)); + } + originResource.getInputs().forEach(p -> serviceImportParseLogic.addInput(currPropertiesMap, p)); + for (List<UploadPropInfo> propertyList : propMap.values()) { + processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList); + } + currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p))); + instInputs.put(currentCompInstance.getUniqueId(), instPropList); + } + } + + protected void processProperty(Service resource, ComponentInstance currentCompInstance, Map<String, DataTypeDefinition> allDataTypes, Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList, List<UploadPropInfo> propertyList) { + UploadPropInfo propertyInfo = propertyList.get(0); + String propName = propertyInfo.getName(); + if (!currPropertiesMap.containsKey(propName)) { + log.debug("failed to find property {} ", propName); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, + propName)); + } + InputDefinition curPropertyDef = currPropertiesMap.get(propName); + ComponentInstanceInput property = null; + + String value = null; + List<GetInputValueDataDefinition> getInputs = null; + boolean isValidate = true; + if (propertyInfo.getValue() != null) { + getInputs = propertyInfo.getGet_input(); + isValidate = getInputs == null || getInputs.isEmpty(); + if (isValidate) { + value = getPropertyJsonStringValue(propertyInfo.getValue(), + curPropertyDef.getType()); + } else { + value = getPropertyJsonStringValue(propertyInfo.getValue(), + TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()); + } + } + String innerType = null; + property = new ComponentInstanceInput(curPropertyDef, value, null); + String validPropertyVAlue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes); + property.setValue(validPropertyVAlue); + + if (isNotEmpty(getInputs)) { + List<GetInputValueDataDefinition> getInputValues = new ArrayList<>(); + for (GetInputValueDataDefinition getInput : getInputs) { + List<InputDefinition> inputs = resource.getInputs(); + if (CollectionUtils.isEmpty(inputs)) { + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)); + } + Optional<InputDefinition> optional = inputs.stream() + .filter(p -> p.getName().equals(getInput.getInputName())).findAny(); + if (!optional.isPresent()) { + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)); + } + InputDefinition input = optional.get(); + getInput.setInputId(input.getUniqueId()); + getInputValues.add(getInput); + + GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex(); + processGetInput(getInputValues, inputs, getInputIndex); + } + property.setGetInputValues(getInputValues); + } + instPropList.add(property); + currPropertiesMap.remove(property.getName()); + } + + protected void processGetInput(List<GetInputValueDataDefinition> getInputValues, List<InputDefinition> inputs, GetInputValueDataDefinition getInputIndex) { + Optional<InputDefinition> optional; + if (getInputIndex != null) { + optional = inputs.stream().filter(p -> p.getName().equals(getInputIndex.getInputName())) + .findAny(); + if (!optional.isPresent()) { + log.debug("Failed to find input {} ", getInputIndex.getInputName()); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)); + } + InputDefinition inputIndex = optional.get(); + getInputIndex.setInputId(inputIndex.getUniqueId()); + getInputValues.add(getInputIndex); + } + } + + protected ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, + Resource resource, Resource originResource, ComponentInstance currentCompInstance, + Map<String, List<ComponentInstanceProperty>> instProperties, Map<String, DataTypeDefinition> allDataTypes) { + Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties(); + Map<String, PropertyDefinition> currPropertiesMap = new HashMap<>(); + + List<PropertyDefinition> listFromMap = originResource.getProperties(); + if ((propMap != null && !propMap.isEmpty()) && (listFromMap == null || listFromMap.isEmpty())) { + log.debug("failed to find properties "); + return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND); + } + 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<ComponentInstanceProperty> instPropList = new ArrayList<>(); + if (propMap != null && propMap.size() > 0) { + for (List<UploadPropInfo> propertyList : propMap.values()) { + UploadPropInfo propertyInfo = propertyList.get(0); + String propName = propertyInfo.getName(); + if (!currPropertiesMap.containsKey(propName)) { + log.debug("failed to find property {} ", propName); + return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, + propName); + } + PropertyDefinition curPropertyDef = currPropertiesMap.get(propName); + ComponentInstanceProperty property = null; + + String value = null; + List<GetInputValueDataDefinition> getInputs = null; + boolean isValidate = true; + if (propertyInfo.getValue() != null) { + getInputs = propertyInfo.getGet_input(); + isValidate = getInputs == null || getInputs.isEmpty(); + if (isValidate) { + value = getPropertyJsonStringValue(propertyInfo.getValue(), + curPropertyDef.getType()); + } else { + value = getPropertyJsonStringValue(propertyInfo.getValue(), + TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()); + } + } + String innerType = null; + property = new ComponentInstanceProperty(curPropertyDef, value, null); + + String validatePropValue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes); + property.setValue(validatePropValue); + + if (getInputs != null && !getInputs.isEmpty()) { + List<GetInputValueDataDefinition> getInputValues = new ArrayList<>(); + for (GetInputValueDataDefinition getInput : getInputs) { + List<InputDefinition> inputs = resource.getInputs(); + if (inputs == null || inputs.isEmpty()) { + log.debug("Failed to add property {} to instance. Inputs list is empty ", property); + serviceBusinessLogic.rollbackWithException(ActionStatus.INPUTS_NOT_FOUND, property.getGetInputValues() + .stream() + .map(GetInputValueDataDefinition::getInputName) + .collect(toList()).toString()); + } + InputDefinition input = serviceImportParseLogic.findInputByName(inputs, getInput); + getInput.setInputId(input.getUniqueId()); + getInputValues.add(getInput); + + GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex(); + if (getInputIndex != null) { + input = serviceImportParseLogic.findInputByName(inputs, getInputIndex); + getInputIndex.setInputId(input.getUniqueId()); + getInputValues.add(getInputIndex); + } + } + property.setGetInputValues(getInputValues); + } + instPropList.add(property); + currPropertiesMap.remove(property.getName()); + } + } + if (!currPropertiesMap.isEmpty()) { + for (PropertyDefinition value : currPropertiesMap.values()) { + instPropList.add(new ComponentInstanceProperty(value)); + } + } + instProperties.put(currentCompInstance.getUniqueId(), instPropList); + return componentsUtils.getResponseFormat(ActionStatus.OK); + } + + protected ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, + Service service, Resource originResource, ComponentInstance currentCompInstance, + Map<String, List<ComponentInstanceProperty>> instProperties, Map<String, DataTypeDefinition> allDataTypes) { + Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties(); + Map<String, PropertyDefinition> currPropertiesMap = new HashMap<>(); + + List<PropertyDefinition> listFromMap = originResource.getProperties(); + if ((propMap != null && !propMap.isEmpty()) && (listFromMap == null || listFromMap.isEmpty())) { + log.debug("failed to find properties "); + return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND); + } + 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<ComponentInstanceProperty> instPropList = new ArrayList<>(); + if (propMap != null && propMap.size() > 0) { + for (List<UploadPropInfo> propertyList : propMap.values()) { + + UploadPropInfo propertyInfo = propertyList.get(0); + String propName = propertyInfo.getName(); + if (!currPropertiesMap.containsKey(propName)) { + log.debug("failed to find property {} ", propName); + return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, + propName); + } + PropertyDefinition curPropertyDef = currPropertiesMap.get(propName); + ComponentInstanceProperty property = null; + + String value = null; + List<GetInputValueDataDefinition> getInputs = null; + boolean isValidate = true; + if (propertyInfo.getValue() != null) { + getInputs = propertyInfo.getGet_input(); + isValidate = getInputs == null || getInputs.isEmpty(); + if (isValidate) { + value = getPropertyJsonStringValue(propertyInfo.getValue(), + curPropertyDef.getType()); + } else { + value = getPropertyJsonStringValue(propertyInfo.getValue(), + TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()); + } + } + String innerType = null; + property = new ComponentInstanceProperty(curPropertyDef, value, null); + + String validatePropValue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes); + property.setValue(validatePropValue); + + if (getInputs != null && !getInputs.isEmpty()) { + List<GetInputValueDataDefinition> getInputValues = new ArrayList<>(); + for (GetInputValueDataDefinition getInput : getInputs) { + List<InputDefinition> inputs = service.getInputs(); + if (inputs == null || inputs.isEmpty()) { + log.debug("Failed to add property {} to instance. Inputs list is empty ", property); + serviceBusinessLogic.rollbackWithException(ActionStatus.INPUTS_NOT_FOUND, property.getGetInputValues() + .stream() + .map(GetInputValueDataDefinition::getInputName) + .collect(toList()).toString()); + } + InputDefinition input = serviceImportParseLogic.findInputByName(inputs, getInput); + getInput.setInputId(input.getUniqueId()); + getInputValues.add(getInput); + + GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex(); + if (getInputIndex != null) { + input = serviceImportParseLogic.findInputByName(inputs, getInputIndex); + getInputIndex.setInputId(input.getUniqueId()); + getInputValues.add(getInputIndex); + } + } + property.setGetInputValues(getInputValues); + } + instPropList.add(property); + currPropertiesMap.remove(property.getName()); + } + } + if (!currPropertiesMap.isEmpty()) { + for (PropertyDefinition value : currPropertiesMap.values()) { + instPropList.add(new ComponentInstanceProperty(value)); + } + } + instProperties.put(currentCompInstance.getUniqueId(), instPropList); + return componentsUtils.getResponseFormat(ActionStatus.OK); + } + + protected void processComponentInstanceCapabilities(Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes, Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, UploadComponentInstanceInfo uploadComponentInstanceInfo, ComponentInstance currentCompInstance, Resource originResource) { + log.debug("enter processComponentInstanceCapabilities"); + Map<String, List<CapabilityDefinition>> originCapabilities; + if (MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) { + originCapabilities = new HashMap<>(); + Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>(); + originResource.getCapabilities().forEach((k, v) -> serviceImportParseLogic + .addCapabilities(originCapabilities, k, v)); + uploadComponentInstanceInfo.getCapabilities().values().forEach(l -> serviceImportParseLogic + .addCapabilitiesProperties(newPropertiesMap, l)); + updateCapabilityPropertiesValues(allDataTypes, originCapabilities, newPropertiesMap); + } else { + originCapabilities = originResource.getCapabilities(); + } + instCapabilties.put(currentCompInstance, originCapabilities); + } + + protected void updateCapabilityPropertiesValues(Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes, Map<String, List<CapabilityDefinition>> originCapabilities, Map<String, Map<String, UploadPropInfo>> newPropertiesMap) { + originCapabilities.values().stream() + .flatMap(Collection::stream) + .filter(c -> newPropertiesMap.containsKey(c.getName())) + .forEach(c -> updatePropertyValues(c.getProperties(), newPropertiesMap.get(c.getName()), allDataTypes.left().value())); + } + + protected void updatePropertyValues(List<ComponentInstanceProperty> properties, Map<String, UploadPropInfo> newProperties, + Map<String, DataTypeDefinition> allDataTypes) { + properties.forEach(p -> updatePropertyValue(p, newProperties.get(p.getName()), allDataTypes)); + } + + protected String updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo, + Map<String, DataTypeDefinition> allDataTypes) { + String value = null; + List<GetInputValueDataDefinition> getInputs = null; + boolean isValidate = true; + if (null != propertyInfo && propertyInfo.getValue() != null) { + getInputs = propertyInfo.getGet_input(); + isValidate = getInputs == null || getInputs.isEmpty(); + if (isValidate) { + value = getPropertyJsonStringValue(propertyInfo.getValue(), property.getType()); + } else { + value = getPropertyJsonStringValue(propertyInfo.getValue(), + TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()); + } + } + property.setValue(value); + return serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes); + } + + protected Resource getOriginResource(String yamlName, Map<String, Resource> originCompMap, ComponentInstance currentCompInstance) { + Resource originResource; + log.debug("after enter ServiceImportBusinessLogic processComponentInstance, enter getOriginResource"); + if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) { + Either<Resource, StorageOperationStatus> getOriginResourceRes = toscaOperationFacade + .getToscaFullElement(currentCompInstance.getComponentUid()); + if (getOriginResourceRes.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat( + componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()), yamlName); + throw new ComponentException(responseFormat); + } + originResource = getOriginResourceRes.left().value(); + originCompMap.put(originResource.getUniqueId(), originResource); + } else { + originResource = originCompMap.get(currentCompInstance.getComponentUid()); + } + return originResource; + } + + protected void handleSubstitutionMappings(Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) { + if (false) { + Either<Resource, StorageOperationStatus> getResourceRes = toscaOperationFacade.getToscaFullElement(service.getUniqueId()); + if (getResourceRes.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), service, ComponentTypeEnum.SERVICE); + throw new ComponentException(responseFormat); + } + getResourceRes = updateCalculatedCapReqWithSubstitutionMappings(getResourceRes.left().value(), + uploadResInstancesMap); + if (getResourceRes.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), service, ComponentTypeEnum.SERVICE); + throw new ComponentException(responseFormat); + } + } + } + + protected Either<Resource, StorageOperationStatus> updateCalculatedCapReqWithSubstitutionMappings(Resource resource, + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) { + Either<Resource, StorageOperationStatus> updateRes = null; + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>(); + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new HashMap<>(); + StorageOperationStatus status = toscaOperationFacade + .deleteAllCalculatedCapabilitiesRequirements(resource.getUniqueId()); + if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) { + log.debug( + "Failed to delete all calculated capabilities and requirements of resource {} upon update. Status is {}", + resource.getUniqueId(), status); + updateRes = Either.right(status); + } + if (updateRes == null) { + fillUpdatedInstCapabilitiesRequirements(resource.getComponentInstances(), uploadResInstancesMap, + updatedInstCapabilities, updatedInstRequirements); + status = toscaOperationFacade.associateOrAddCalculatedCapReq(updatedInstCapabilities, updatedInstRequirements, + resource); + if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) { + updateRes = Either.right(status); + } + } + if (updateRes == null) { + updateRes = Either.left(resource); + } + return updateRes; + } + + protected void fillUpdatedInstCapabilitiesRequirements(List<ComponentInstance> componentInstances, + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap, + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities, + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements) { + componentInstances.stream().forEach(i -> { + fillUpdatedInstCapabilities(updatedInstCapabilities, i, + uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate()); + fillUpdatedInstRequirements(updatedInstRequirements, i, + uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate()); + }); + } + + protected void fillUpdatedInstCapabilities( + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties, + ComponentInstance instance, Map<String, String> capabilitiesNamesToUpdate) { + Map<String, List<CapabilityDefinition>> updatedCapabilities = new HashMap<>(); + Set<String> updatedCapNames = new HashSet<>(); + if (MapUtils.isNotEmpty(capabilitiesNamesToUpdate)) { + for (Map.Entry<String, List<CapabilityDefinition>> requirements : instance.getCapabilities().entrySet()) { + updatedCapabilities.put(requirements.getKey(), + requirements.getValue().stream() + .filter(c -> capabilitiesNamesToUpdate.containsKey(c.getName()) + && !updatedCapNames.contains(capabilitiesNamesToUpdate.get(c.getName()))) + .map(c -> { + c.setParentName(c.getName()); + c.setName(capabilitiesNamesToUpdate.get(c.getName())); + updatedCapNames.add(c.getName()); + return c; + }).collect(toList())); + } + } + if (MapUtils.isNotEmpty(updatedCapabilities)) { + updatedInstCapabilties.put(instance, updatedCapabilities); + } + } + + protected void fillUpdatedInstRequirements( + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements, + ComponentInstance instance, Map<String, String> requirementsNamesToUpdate) { + Map<String, List<RequirementDefinition>> updatedRequirements = new HashMap<>(); + Set<String> updatedReqNames = new HashSet<>(); + if (MapUtils.isNotEmpty(requirementsNamesToUpdate)) { + for (Map.Entry<String, List<RequirementDefinition>> requirements : instance.getRequirements().entrySet()) { + updatedRequirements.put(requirements.getKey(), + requirements.getValue().stream() + .filter(r -> requirementsNamesToUpdate.containsKey(r.getName()) + && !updatedReqNames.contains(requirementsNamesToUpdate.get(r.getName()))) + .map(r -> { + r.setParentName(r.getName()); + r.setName(requirementsNamesToUpdate.get(r.getName())); + updatedReqNames.add(r.getName()); + return r; + }).collect(toList())); + } + } + if (MapUtils.isNotEmpty(updatedRequirements)) { + updatedInstRequirements.put(instance, updatedRequirements); + } + } + + protected void addRelationsToRI(String yamlName, Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap, List<ComponentInstance> componentInstancesList, List<RequirementCapabilityRelDef> relations) { + for (Map.Entry<String, UploadComponentInstanceInfo> entry : uploadResInstancesMap.entrySet()) { + UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue(); + ComponentInstance currentCompInstance = null; + for (ComponentInstance compInstance : componentInstancesList) { + if (compInstance.getName().equals(uploadComponentInstanceInfo.getName())) { + currentCompInstance = compInstance; + break; + } + } + if (currentCompInstance == null) { + log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), + service.getUniqueId()); + BeEcompErrorManager.getInstance().logInternalDataError( + COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, + service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + throw new ComponentException(responseFormat); + } + + ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, service, entry.getValue(), relations); + if (addRelationToRiRes.getStatus() != 200) { + throw new ComponentException(addRelationToRiRes); + } + } + } + + protected ResponseFormat addRelationToRI(String yamlName, Service service, + UploadComponentInstanceInfo nodesInfoValue, List<RequirementCapabilityRelDef> relations) { + List<ComponentInstance> componentInstancesList = service.getComponentInstances(); + ComponentInstance currentCompInstance = null; + for (ComponentInstance compInstance : componentInstancesList) { + if (compInstance.getName().equals(nodesInfoValue.getName())) { + currentCompInstance = compInstance; + break; + } + } + if (currentCompInstance == null) { + log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, nodesInfoValue.getName(), + service.getUniqueId()); + BeEcompErrorManager.getInstance().logInternalDataError( + COMPONENT_INSTANCE_WITH_NAME + nodesInfoValue.getName() + IN_RESOURCE, + service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, + yamlName); + } + String resourceInstanceId = currentCompInstance.getUniqueId(); + Map<String, List<UploadReqInfo>> regMap = nodesInfoValue.getRequirements(); + + if (regMap != null) { + Iterator<Map.Entry<String, List<UploadReqInfo>>> nodesRegValue = regMap.entrySet().iterator(); + while (nodesRegValue.hasNext()) { + Map.Entry<String, List<UploadReqInfo>> nodesRegInfoEntry = nodesRegValue.next(); + List<UploadReqInfo> uploadRegInfoList = nodesRegInfoEntry.getValue(); + for (UploadReqInfo uploadRegInfo : uploadRegInfoList) { + log.debug("Going to create relation {}", uploadRegInfo.getName()); + String regName = uploadRegInfo.getName(); + RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef(); + regCapRelDef.setFromNode(resourceInstanceId); + log.debug("try to find available requirement {} ", regName); + Either<RequirementDefinition, ResponseFormat> eitherReqStatus = serviceImportParseLogic.findAviableRequiremen(regName, + yamlName, nodesInfoValue, currentCompInstance, + uploadRegInfo.getCapabilityName()); + if (eitherReqStatus.isRight()) { + log.debug("failed to find available requirement {} status is {}", regName, + eitherReqStatus.right().value()); + return eitherReqStatus.right().value(); + } + + RequirementDefinition validReq = eitherReqStatus.left().value(); + List<CapabilityRequirementRelationship> reqAndRelationshipPairList = regCapRelDef + .getRelationships(); + if (reqAndRelationshipPairList == null) { + reqAndRelationshipPairList = new ArrayList<>(); + } + RelationshipInfo reqAndRelationshipPair = new RelationshipInfo(); + reqAndRelationshipPair.setRequirement(regName); + reqAndRelationshipPair.setRequirementOwnerId(validReq.getOwnerId()); + reqAndRelationshipPair.setRequirementUid(validReq.getUniqueId()); + RelationshipImpl relationship = new RelationshipImpl(); + relationship.setType(validReq.getCapability()); + reqAndRelationshipPair.setRelationships(relationship); + + ComponentInstance currentCapCompInstance = null; + for (ComponentInstance compInstance : componentInstancesList) { + if (compInstance.getName().equals(uploadRegInfo.getNode())) { + currentCapCompInstance = compInstance; + break; + } + } + if (currentCapCompInstance == null) { + log.debug("The component instance with name {} not found on resource {} ", + uploadRegInfo.getNode(), service.getUniqueId()); + BeEcompErrorManager.getInstance().logInternalDataError( + COMPONENT_INSTANCE_WITH_NAME + uploadRegInfo.getNode() + IN_RESOURCE, + service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + return componentsUtils + .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + regCapRelDef.setToNode(currentCapCompInstance.getUniqueId()); + log.debug("try to find aviable Capability req name is {} ", validReq.getName()); + CapabilityDefinition aviableCapForRel = serviceImportParseLogic.findAvailableCapabilityByTypeOrName(validReq, + currentCapCompInstance, uploadRegInfo); + reqAndRelationshipPair.setCapability(aviableCapForRel.getName()); + reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId()); + reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId()); + if (aviableCapForRel == null) { + BeEcompErrorManager.getInstance().logInternalDataError( + "aviable capability was not found. req name is " + validReq.getName() + + " component instance is " + currentCapCompInstance.getUniqueId(), + service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + return componentsUtils + .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship(); + capReqRel.setRelation(reqAndRelationshipPair); + reqAndRelationshipPairList.add(capReqRel); + regCapRelDef.setRelationships(reqAndRelationshipPairList); + relations.add(regCapRelDef); + } + } + } + return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName); + } + + protected Service getResourceAfterCreateRelations(Service service) { + ComponentParametersView parametersView = serviceImportParseLogic.getComponentFilterAfterCreateRelations(); + Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade + .getToscaElement(service.getUniqueId(), parametersView); + if (eitherGetResource.isRight()) { + serviceImportParseLogic.throwComponentExceptionByResource(eitherGetResource.right().value(), service); + } + return eitherGetResource.left().value(); + } + + protected Service createServiceInstances(String yamlName, Service service, + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap, + Map<String, Resource> nodeNamespaceMap) { + Either<Resource, ResponseFormat> eitherResource = null; + log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName); + if (MapUtils.isEmpty(uploadResInstancesMap)) { // PNF can have no resource instances + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE); + throw new ComponentException(responseFormat); + } + Map<String, Resource> existingNodeTypeMap = new HashMap<>(); + if (MapUtils.isNotEmpty(nodeNamespaceMap)) { + nodeNamespaceMap.forEach((k, v) -> existingNodeTypeMap.put(v.getToscaResourceName(), v)); + } + Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>(); + uploadResInstancesMap + .values() + .forEach(i -> createAndAddResourceInstance(i, yamlName, service, nodeNamespaceMap, existingNodeTypeMap, resourcesInstancesMap)); + + if (MapUtils.isNotEmpty(resourcesInstancesMap)) { + try { + toscaOperationFacade.associateComponentInstancesToComponent(service, + resourcesInstancesMap, false, false); + } catch (StorageException exp) { + if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) { + log.debug("Failed to add component instances to container component {}", service.getName()); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus())); + eitherResource = Either.right(responseFormat); + throw new ComponentException(eitherResource.right().value()); + } + } + } + Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade + .getToscaElement(service.getUniqueId(), serviceImportParseLogic.getComponentWithInstancesFilter()); + log.debug("*************finished to get resource {}", service.getUniqueId()); + if (eitherGetResource.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), service, ComponentTypeEnum.SERVICE); + throw new ComponentException(responseFormat); + } + if (CollectionUtils.isEmpty(eitherGetResource.left().value().getComponentInstances())) { // PNF can have no resource instances + log.debug("Error when create resource instance from csar. ComponentInstances list empty"); + BeEcompErrorManager.getInstance().logBeDaoSystemError( + "Error when create resource instance from csar. ComponentInstances list empty"); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE)); + } + return eitherGetResource.left().value(); + } + + protected void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName, + Resource resource, Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap, Map<ComponentInstance, Resource> resourcesInstancesMap) { + Either<Resource, ResponseFormat> eitherResource; + log.debug("*************Going to create resource instances {}", uploadComponentInstanceInfo.getName()); + try { + if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) { + uploadComponentInstanceInfo + .setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName()); + } + Resource refResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo, + existingnodeTypeMap); + + ComponentInstance componentInstance = new ComponentInstance(); + componentInstance.setComponentUid(refResource.getUniqueId()); + + Collection<String> directives = uploadComponentInstanceInfo.getDirectives(); + if (directives != null && !directives.isEmpty()) { + componentInstance.setDirectives(new ArrayList<>(directives)); + } + UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo(); + if (uploadNodeFilterInfo != null) { + componentInstance.setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo, + componentInstance.getUniqueId())); + } + + ComponentTypeEnum containerComponentType = resource.getComponentType(); + NodeTypeEnum containerNodeType = containerComponentType.getNodeType(); + if (containerNodeType.equals(NodeTypeEnum.Resource) + && MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) + && MapUtils.isNotEmpty(refResource.getCapabilities())) { + serviceImportParseLogic.setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities()); + Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = serviceImportParseLogic.getValidComponentInstanceCapabilities( + refResource.getUniqueId(), refResource.getCapabilities(), + uploadComponentInstanceInfo.getCapabilities()); + componentInstance.setCapabilities(validComponentInstanceCapabilities); + } + if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, + yamlName, uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); + throw new ComponentException(responseFormat); + } + Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType()); + componentInstance.setName(uploadComponentInstanceInfo.getName()); + componentInstance.setIcon(origResource.getIcon()); + resourcesInstancesMap.put(componentInstance, origResource); + } catch (Exception e) { + throw new ComponentException(ActionStatus.GENERAL_ERROR,e.getMessage()); + } + } + + protected void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName, + Service service, Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap, Map<ComponentInstance, Resource> resourcesInstancesMap) { + Either<Resource, ResponseFormat> eitherResource; + log.debug("*************Going to create resource instances {}", uploadComponentInstanceInfo.getName()); + try { + if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) { + uploadComponentInstanceInfo + .setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName()); + } + Resource refResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo, + existingnodeTypeMap); + + ComponentInstance componentInstance = new ComponentInstance(); + componentInstance.setComponentUid(refResource.getUniqueId()); + Collection<String> directives = uploadComponentInstanceInfo.getDirectives(); + if (directives != null && !directives.isEmpty()) { + componentInstance.setDirectives(new ArrayList<>(directives)); + } + UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo(); + if (uploadNodeFilterInfo != null) { + componentInstance.setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo, + componentInstance.getUniqueId())); + } + + ComponentTypeEnum containerComponentType = service.getComponentType(); + NodeTypeEnum containerNodeType = containerComponentType.getNodeType(); + if (containerNodeType.equals(NodeTypeEnum.Resource) + && MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) + && MapUtils.isNotEmpty(refResource.getCapabilities())) { + serviceImportParseLogic.setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities()); + Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = serviceImportParseLogic.getValidComponentInstanceCapabilities( + refResource.getUniqueId(), refResource.getCapabilities(), + uploadComponentInstanceInfo.getCapabilities()); + componentInstance.setCapabilities(validComponentInstanceCapabilities); + } + if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) { + log.debug( + "createResourceInstances - not found lates version for resource instance with name {} and type ", + uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, + yamlName, uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); + throw new ComponentException(responseFormat); + } + Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType()); + componentInstance.setName(uploadComponentInstanceInfo.getName()); + componentInstance.setIcon(origResource.getIcon()); + resourcesInstancesMap.put(componentInstance, origResource); + } catch (Exception e) { + throw new ComponentException(ActionStatus.GENERAL_ERROR,e.getMessage()); + } + } + + protected Resource validateResourceInstanceBeforeCreate(String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo, + Map<String, Resource> nodeNamespaceMap) { + Resource refResource; + try { + if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) { + refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()); + } else { + Either<Resource, StorageOperationStatus> findResourceEither = toscaOperationFacade + .getLatestResourceByToscaResourceName(uploadComponentInstanceInfo.getType()); + if (findResourceEither.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat( + componentsUtils.convertFromStorageResponse(findResourceEither.right().value())); + throw new ComponentException(responseFormat); + } + refResource = findResourceEither.left().value(); + nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource); + } + String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState(); + if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.ILLEGAL_COMPONENT_STATE, + refResource.getComponentType().getValue(), refResource.getName(), componentState); + throw new ComponentException(responseFormat); + } + if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.VF) { + log.debug("validateResourceInstanceBeforeCreate - ref resource type is ", refResource.getResourceType()); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, + yamlName, uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); + throw new ComponentException(responseFormat); + } + return refResource; + } catch (Exception e) { + throw new ComponentException(ActionStatus.GENERAL_ERROR,e.getMessage()); + } + } + + protected void handleServiceNodeTypes(String yamlName, Service service, + String topologyTemplateYaml, boolean needLock, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, + CsarInfo csarInfo, String nodeName) { + try { + for (Map.Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) { + boolean isResourceNotExisted = validateResourceNotExisted(nodeTypeEntry.getKey()); + if (nodeTypeEntry.getValue().isNested() && isResourceNotExisted) { + handleNestedVF(service, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, + nodeTypesInfo, csarInfo, nodeTypeEntry.getKey()); + log.trace("************* finished to create node {}", nodeTypeEntry.getKey()); + } + } + Map<String, Object> mappedToscaTemplate = null; + if (org.apache.commons.lang.StringUtils.isNotEmpty(nodeName) && MapUtils.isNotEmpty(nodeTypesInfo) + && nodeTypesInfo.containsKey(nodeName)) { + mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate(); + } + if (MapUtils.isEmpty(mappedToscaTemplate)) { + mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml); + } + createResourcesFromYamlNodeTypesList(yamlName, service, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle, + nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo); + } catch (ComponentException e) { + throw e; + } catch (StorageException e) { + throw e; + }catch (Exception e){ + log.debug("Exception occured when handleServiceNodeTypes, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected boolean validateResourceNotExisted(String type) { + try { + Either<Resource, StorageOperationStatus> latestResource = toscaOperationFacade.getLatestResourceByToscaResourceName(type); + return latestResource.isRight() ? true : false; + } catch (Exception e) { + log.debug("Exception occured when validateResourceNotExisted, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected Resource handleNestedVF(Service service, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle, + List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, + String nodeName) { + try { + String yamlName = nodesInfo.get(nodeName).getTemplateFileName(); + Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate(); + + createResourcesFromYamlNodeTypesList(yamlName, service, nestedVfcJsonMap, false, + nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo); + log.debug("************* Finished to create node types from yaml {}", yamlName); + + if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) { + log.debug("************* Going to handle complex VFC from yaml {}", yamlName); + Resource resource = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, + csarInfo, nodeName, yamlName); + return resource; + } + return new Resource(); + } catch (Exception e) { + log.debug("Exception occured when handleNestedVF, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected Resource handleNestedVfc(Service service, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle, + List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, + String nodeName) { + try { + String yamlName = nodesInfo.get(nodeName).getTemplateFileName(); + Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate(); + createResourcesFromYamlNodeTypesList(yamlName, service, nestedVfcJsonMap, false, + nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo); + log.debug("************* Finished to create node types from yaml {}", yamlName); + + if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) { + log.debug("************* Going to handle complex VFC from yaml {}", yamlName); + Resource resource = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, + csarInfo, nodeName, yamlName); + return resource; + } + return new Resource(); + } catch (Exception e) { + log.debug("Exception occured when handleNestedVFc, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected Resource handleComplexVfc(Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle, + List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, + String nodeName, String yamlName) { + try { + Resource oldComplexVfc = null; + Resource newComplexVfc = serviceImportParseLogic.buildValidComplexVfc(csarInfo, nodeName, nodesInfo); + Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade + .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName()); + if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) { + oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName( + serviceImportParseLogic.buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(), + nodeName).getRight()); + } + 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()); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } else if (oldComplexVfcRes.isLeft()) { + log.debug(VALIDATE_DERIVED_BEFORE_UPDATE); + Either<Boolean, ResponseFormat> eitherValidation = serviceImportParseLogic.validateNestedDerivedFromDuringUpdate( + oldComplexVfcRes.left().value(), newComplexVfc, + ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion())); + if (eitherValidation.isLeft()) { + oldComplexVfc = oldComplexVfcRes.left().value(); + } + } + newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, + oldComplexVfc, newComplexVfc); + csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName()); + LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction( + CERTIFICATION_ON_IMPORT, LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR); + log.debug("Going to certify cvfc {}. ", newComplexVfc.getName()); + final Resource result = serviceImportParseLogic + .propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, + true); + csarInfo.getCreatedNodes().put(nodeName, result); + csarInfo.removeNodeFromQueue(); + return result; + } catch (Exception e) { + log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected Resource handleComplexVfc(Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle, + List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, + String nodeName, String yamlName, Resource oldComplexVfc, Resource newComplexVfc) { + Resource handleComplexVfcRes; + try { + Map<String, Object> mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate(); + String yamlContent = new String(csarInfo.getCsar().get(yamlName)); + Map<String, NodeTypeInfo> newNodeTypesInfo = nodesInfo.entrySet().stream() + .collect(toMap(Map.Entry::getKey, e -> e.getValue().getUnmarkedCopy())); + CsarInfo.markNestedVfc(mappedToscaTemplate, newNodeTypesInfo); + if (oldComplexVfc == null) { + handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo, + csarInfo, nodesArtifactsToHandle, false, true, nodeName); + } else { + handleComplexVfcRes = updateResourceFromYaml(oldComplexVfc, newComplexVfc, + AuditingActionEnum.UPDATE_RESOURCE_METADATA, createdArtifacts, yamlContent, yamlName, csarInfo, + newNodeTypesInfo, nodesArtifactsToHandle, nodeName, true); + } + return handleComplexVfcRes; + } catch (Exception e) { + log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected Resource updateResourceFromYaml(Resource oldRresource, Resource newRresource, + AuditingActionEnum actionEnum, List<ArtifactDefinition> createdArtifacts, + String yamlFileName, String yamlFileContent, CsarInfo csarInfo, Map<String, NodeTypeInfo> nodeTypesInfo, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, + String nodeName, boolean isNested) { + boolean inTransaction = true; + boolean shouldLock = false; + Resource preparedResource = null; + ParsedToscaYamlInfo uploadComponentInstanceInfoMap = null; + try { + uploadComponentInstanceInfoMap = csarBusinessLogic.getParsedToscaYamlInfo(yamlFileContent, yamlFileName, nodeTypesInfo, csarInfo, nodeName, oldRresource); + Map<String, UploadComponentInstanceInfo> instances = uploadComponentInstanceInfoMap.getInstances(); + if (MapUtils.isEmpty(instances) && newRresource.getResourceType() != ResourceTypeEnum.PNF) { + throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName); + } + preparedResource = updateExistingResourceByImport(newRresource, oldRresource, csarInfo.getModifier(), + inTransaction, shouldLock, isNested).left; + log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent); + serviceImportParseLogic.handleResourceGenericType(preparedResource); + handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, + shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo, csarInfo, nodeName); + preparedResource = serviceImportParseLogic + .createInputsOnResource(preparedResource, uploadComponentInstanceInfoMap.getInputs()); + preparedResource = createResourceInstances(yamlFileName, preparedResource, instances, csarInfo.getCreatedNodes()); + preparedResource = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, instances); + } catch (ComponentException e) { + ResponseFormat responseFormat = e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat(); + log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum); + throw e; + } catch (StorageException e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus())); + log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum); + throw e; + } + Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = serviceBusinessLogic.groupBusinessLogic + .validateUpdateVfGroupNames(uploadComponentInstanceInfoMap.getGroups(), + preparedResource.getSystemName()); + if (validateUpdateVfGroupNamesRes.isRight()) { + throw new ComponentException(validateUpdateVfGroupNamesRes.right().value()); + } + Map<String, GroupDefinition> groups; + if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) { + groups = validateUpdateVfGroupNamesRes.left().value(); + } else { + groups = uploadComponentInstanceInfoMap.getGroups(); + } + serviceImportParseLogic.handleGroupsProperties(preparedResource, groups); + preparedResource = serviceImportParseLogic.updateGroupsOnResource(preparedResource, groups); + NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, + nodeTypesArtifactsToHandle); + + Either<Resource, ResponseFormat> updateArtifactsEither = createOrUpdateArtifacts( + ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE, createdArtifacts, yamlFileName, + csarInfo, preparedResource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock); + if (updateArtifactsEither.isRight()) { + log.debug("failed to update artifacts {}", updateArtifactsEither.right().value()); + throw new ComponentException(updateArtifactsEither.right().value()); + } + preparedResource = serviceImportParseLogic.getResourceWithGroups(updateArtifactsEither.left().value().getUniqueId()); + + ActionStatus mergingPropsAndInputsStatus = resourceDataMergeBusinessLogic.mergeResourceEntities(oldRresource, preparedResource); + if (mergingPropsAndInputsStatus != ActionStatus.OK) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(mergingPropsAndInputsStatus, + preparedResource); + throw new ComponentException(responseFormat); + } + compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId()); + return preparedResource; + } + + protected Resource createResourceFromYaml(Resource resource, String topologyTemplateYaml, + String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate, + boolean shouldLock, boolean inTransaction, String nodeName) { + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Resource createdResource; + try { + ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic.getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, resource); + if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances()) && resource.getResourceType() != ResourceTypeEnum.PNF) { + throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", resource.getName()); + createdResource = createResourceAndRIsFromYaml(yamlName, resource, + parsedToscaYamlInfo, AuditingActionEnum.IMPORT_RESOURCE, false, createdArtifacts, topologyTemplateYaml, + nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, shouldLock, inTransaction, nodeName); + log.debug("#createResourceFromYaml - The resource {} has been created ", resource.getName()); + } catch (ComponentException e) { + ResponseFormat responseFormat = e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat(); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE); + throw e; + } catch (StorageException e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus())); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE); + throw e; + } + return createdResource; + } + + protected Resource createResourceAndRIsFromYaml(String yamlName, Resource resource, + ParsedToscaYamlInfo parsedToscaYamlInfo, AuditingActionEnum actionEnum, boolean isNormative, + List<ArtifactDefinition> createdArtifacts, String topologyTemplateYaml, + Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate, + boolean shouldLock, boolean inTransaction, String nodeName) { + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + if (shouldLock) { + Either<Boolean, ResponseFormat> lockResult = serviceBusinessLogic.lockComponentByName(resource.getSystemName(), resource, + CREATE_RESOURCE); + if (lockResult.isRight()) { + serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ComponentException(lockResult.right().value()); + } + log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult); + } + try { + log.trace("************* createResourceFromYaml before full create resource {}", yamlName); + Resource genericResource = serviceBusinessLogic.fetchAndSetDerivedFromGenericType(resource); + resource = createResourceTransaction(resource, + csarInfo.getModifier(), isNormative); + log.trace("************* Going to add inputs from yaml {}", yamlName); + + Map<String, Object> yamlMap = ImportUtils.loadYamlAsStrictMap(csarInfo.getMainTemplateContent()); + Map<String, Object> metadata = (Map<String, Object>) yamlMap.get("metadata"); + String type = (String) metadata.get("type"); + if (resource.shouldGenerateInputs() && !"Service".equalsIgnoreCase(type)) + serviceBusinessLogic.generateAndAddInputsFromGenericTypeProperties(resource, genericResource); + Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs(); + resource = serviceImportParseLogic.createInputsOnResource(resource, inputs); + Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo + .getInstances(); + resource = createRIAndRelationsFromYaml(yamlName, resource, uploadComponentInstanceInfoMap, + topologyTemplateYaml, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, + nodeTypesArtifactsToCreate, nodeName); + log.trace("************* Finished to create nodes, RI and Relation from yaml {}", yamlName); + // validate update vf module group names + Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = serviceBusinessLogic.groupBusinessLogic + .validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), resource.getSystemName()); + if (validateUpdateVfGroupNamesRes.isRight()) { + serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ComponentException(validateUpdateVfGroupNamesRes.right().value()); + } + Map<String, GroupDefinition> groups; + log.trace("************* Going to add groups from yaml {}", yamlName); + + if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) { + groups = validateUpdateVfGroupNamesRes.left().value(); + } else { + groups = parsedToscaYamlInfo.getGroups(); + } + + Either<Resource, ResponseFormat> createGroupsOnResource = createGroupsOnResource(resource, + groups); + if (createGroupsOnResource.isRight()) { + serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ComponentException(createGroupsOnResource.right().value()); + } + resource = createGroupsOnResource.left().value(); + log.trace("************* Going to add artifacts from yaml {}", yamlName); + + NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToCreate); + + Either<Resource, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts( + ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, createdArtifacts, yamlName, + csarInfo, resource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock); + if (createArtifactsEither.isRight()) { + serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ComponentException(createArtifactsEither.right().value()); + } + + resource = serviceImportParseLogic.getResourceWithGroups(createArtifactsEither.left().value().getUniqueId()); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, actionEnum); + ASDCKpiApi.countCreatedResourcesKPI(); + return resource; + } catch (ComponentException | StorageException e) { + serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw e; + } finally { + if (!inTransaction) { + serviceBusinessLogic.janusGraphDao.commit(); + } + if (shouldLock) { + serviceBusinessLogic.graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource); + } + } + } + + protected Either<Resource, ResponseFormat> createGroupsOnResource(Resource resource, + Map<String, GroupDefinition> groups) { + if (groups != null && !groups.isEmpty()) { + List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource( + groups, resource); + serviceImportParseLogic.handleGroupsProperties(resource, groups); + serviceImportParseLogic.fillGroupsFinalFields(groupsAsList); + Either<List<GroupDefinition>, ResponseFormat> createGroups = serviceBusinessLogic.groupBusinessLogic.createGroups(resource, + groupsAsList, true); + if (createGroups.isRight()) { + return Either.right(createGroups.right().value()); + } + } else { + return Either.left(resource); + } + Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade + .getToscaElement(resource.getUniqueId()); + if (updatedResource.isRight()) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource( + componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resource); + return Either.right(responseFormat); + } + return Either.left(updatedResource.left().value()); + } + + protected List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Resource component) { + List<GroupDefinition> result = new ArrayList<>(); + List<ComponentInstance> componentInstances = component.getComponentInstances(); + + if (groups != null) { + Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies = serviceImportParseLogic.validateCyclicGroupsDependencies(groups); + if (validateCyclicGroupsDependencies.isRight()) { + throw new ComponentException(validateCyclicGroupsDependencies.right().value()); + } + for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) { + String groupName = entry.getKey(); + GroupDefinition groupDefinition = entry.getValue(); + GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition); + updatedGroupDefinition.setMembers(null); + Map<String, String> members = groupDefinition.getMembers(); + if (members != null) { + updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members); + } + result.add(updatedGroupDefinition); + } + } + return result; + } + + protected void updateGroupMembers(Map<String, GroupDefinition> groups, GroupDefinition updatedGroupDefinition, Resource component, List<ComponentInstance> componentInstances, String groupName, Map<String, String> members) { + Set<String> compInstancesNames = members.keySet(); + if (CollectionUtils.isEmpty(componentInstances)) { + String membersAstString = compInstancesNames.stream().collect(joining(",")); + log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", + membersAstString, groupName, component.getNormalizedName()); + throw new ComponentException(componentsUtils.getResponseFormat( + ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, + component.getNormalizedName(), serviceImportParseLogic.getComponentTypeForResponse(component))); + } + Map<String, String> memberNames = componentInstances.stream() + .collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId)); + memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> ""))); + Map<String, String> relevantInstances = memberNames.entrySet().stream() + .filter(n -> compInstancesNames.contains(n.getKey())) + .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); + + if (relevantInstances == null || relevantInstances.size() != compInstancesNames.size()) { + List<String> foundMembers = new ArrayList<>(); + if (relevantInstances != null) { + foundMembers = relevantInstances.keySet().stream().collect(toList()); + } + compInstancesNames.removeAll(foundMembers); + String membersAstString = compInstancesNames.stream().collect(joining(",")); + throw new ComponentException(componentsUtils.getResponseFormat( + ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, + component.getNormalizedName(), serviceImportParseLogic.getComponentTypeForResponse(component))); + } + updatedGroupDefinition.setMembers(relevantInstances); + } + + protected Resource createResourceTransaction(Resource resource, User user, + boolean isNormative) { + Either<Boolean, StorageOperationStatus> eitherValidation = toscaOperationFacade.validateComponentNameExists( + resource.getName(), resource.getResourceType(), resource.getComponentType()); + if (eitherValidation.isRight()) { + ResponseFormat errorResponse = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(eitherValidation.right().value())); + throw new ComponentException(errorResponse); + } + if (eitherValidation.left().value()) { + log.debug("resource with name: {}, already exists", resource.getName()); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, + ComponentTypeEnum.RESOURCE.getValue(), resource.getName()); + throw new ComponentException(errorResponse); + } + log.debug("send resource {} to dao for create", resource.getName()); + serviceImportParseLogic.createArtifactsPlaceHolderData(resource, user); + if (!isNormative) { + log.debug("enrich resource with creator, version and state"); + resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + resource.setVersion(INITIAL_VERSION); + resource.setHighestVersion(true); + if (resource.getResourceType() != null && resource.getResourceType() != ResourceTypeEnum.VF) { + resource.setAbstract(false); + } + } + return toscaOperationFacade.createToscaComponent(resource) + .left() + .on(r -> serviceImportParseLogic.throwComponentExceptionByResource(r, resource)); + } + + protected ImmutablePair<Resource, ActionStatus> updateExistingResourceByImport( + Resource newResource, Resource oldResource, User user, boolean inTransaction, boolean needLock, + boolean isNested) { + String lockedResourceId = oldResource.getUniqueId(); + log.debug("found resource: name={}, id={}, version={}, state={}", oldResource.getName(), lockedResourceId, + oldResource.getVersion(), oldResource.getLifecycleState()); + ImmutablePair<Resource, ActionStatus> resourcePair = null; + try { + serviceBusinessLogic.lockComponent(lockedResourceId, oldResource, needLock, "Update Resource by Import"); + oldResource = serviceImportParseLogic + .prepareResourceForUpdate(oldResource, newResource, user, inTransaction, false); + serviceImportParseLogic.mergeOldResourceMetadataWithNew(oldResource, newResource); + serviceImportParseLogic.validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction, isNested); + serviceImportParseLogic + .validateCapabilityTypesCreate(user, serviceImportParseLogic.getCapabilityTypeOperation(), newResource, AuditingActionEnum.IMPORT_RESOURCE, inTransaction); + newResource.setContactId(newResource.getContactId().toLowerCase()); + newResource.setCreatorUserId(user.getUserId()); + newResource.setCreatorFullName(user.getFullName()); + newResource.setLastUpdaterUserId(user.getUserId()); + newResource.setLastUpdaterFullName(user.getFullName()); + newResource.setUniqueId(oldResource.getUniqueId()); + newResource.setVersion(oldResource.getVersion()); + newResource.setInvariantUUID(oldResource.getInvariantUUID()); + newResource.setLifecycleState(oldResource.getLifecycleState()); + newResource.setUUID(oldResource.getUUID()); + newResource.setNormalizedName(oldResource.getNormalizedName()); + newResource.setSystemName(oldResource.getSystemName()); + if (oldResource.getCsarUUID() != null) { + newResource.setCsarUUID(oldResource.getCsarUUID()); + } + if (oldResource.getImportedToscaChecksum() != null) { + newResource.setImportedToscaChecksum(oldResource.getImportedToscaChecksum()); + } + if (newResource.getDerivedFromGenericType() == null || newResource.getDerivedFromGenericType().isEmpty()) { + newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType()); + } + if (newResource.getDerivedFromGenericVersion() == null || newResource.getDerivedFromGenericVersion().isEmpty()) { + newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion()); + } + if (newResource.getToscaArtifacts() == null || newResource.getToscaArtifacts().isEmpty()) { + serviceBusinessLogic.setToscaArtifactsPlaceHolders(newResource, user); + } + if (newResource.getInterfaces() == null || newResource.getInterfaces().isEmpty()) { + newResource.setInterfaces(oldResource.getInterfaces()); + } + if (CollectionUtils.isEmpty(newResource.getProperties())) { + newResource.setProperties(oldResource.getProperties()); + } + + Either<Resource, StorageOperationStatus> overrideResource = toscaOperationFacade + .overrideComponent(newResource, oldResource); + if (overrideResource.isRight()) { + ResponseFormat responseFormat = new ResponseFormat(); + serviceBusinessLogic.throwComponentException(responseFormat); + } + log.debug("Resource updated successfully!!!"); + resourcePair = new ImmutablePair<>(overrideResource.left().value(), + ActionStatus.OK); + return resourcePair; + } finally { + if (resourcePair == null) { + BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify"); + serviceBusinessLogic.janusGraphDao.rollback(); + } else if (!inTransaction) { + serviceBusinessLogic.janusGraphDao.commit(); + } + if (needLock) { + log.debug("unlock resource {}", lockedResourceId); + serviceBusinessLogic.graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource); + } + } + } + + protected Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Service service, Map<String, Object> mappedToscaTemplate, boolean needLock, + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, + CsarInfo csarInfo) { + try { + Either<String, ImportUtils.ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate, + TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION); + if (toscaVersion.isRight()) { + throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE); + } + Map<String, Object> mapToConvert = new HashMap<>(); + mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value()); + Map<String, Object> nodeTypes = serviceImportParseLogic.getNodeTypesFromTemplate(mappedToscaTemplate); + createNodeTypes(yamlName, service, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert, nodeTypes); + return csarInfo.getCreatedNodes(); + } catch (Exception e) { + log.debug("Exception occured when createResourcesFromYamlNodeTypesList,error is:{}",e.getMessage(),e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected void createNodeTypes(String yamlName, Service service, boolean needLock, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) { + Iterator<Map.Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator(); + Resource vfcCreated = null; + while (nodesNameValueIter.hasNext()) { + Map.Entry<String, Object> nodeType = nodesNameValueIter.next(); + Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = nodeTypesArtifactsToHandle == null + || nodeTypesArtifactsToHandle.isEmpty() ? null + : nodeTypesArtifactsToHandle.get(nodeType.getKey()); + + if (nodeTypesInfo.containsKey(nodeType.getKey())) { + vfcCreated = handleNestedVfc(service, + nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, + nodeType.getKey()); + log.trace("************* Finished to handle nested vfc {}", nodeType.getKey()); + } else if (csarInfo.getCreatedNodesToscaResourceNames() != null + && !csarInfo.getCreatedNodesToscaResourceNames().containsKey(nodeType.getKey())) { + ImmutablePair<Resource, ActionStatus> resourceCreated = serviceImportParseLogic + .createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(), mapToConvert, + service, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, + csarInfo, true); + log.debug("************* Finished to create node {}", nodeType.getKey()); + + vfcCreated = resourceCreated.getLeft(); + csarInfo.getCreatedNodesToscaResourceNames().put(nodeType.getKey(), + vfcCreated.getName()); + } + if (vfcCreated != null) { + csarInfo.getCreatedNodes().put(nodeType.getKey(), vfcCreated); + } + mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName()); + } + } + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportManager.java new file mode 100644 index 0000000000..8e7c1e852c --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportManager.java @@ -0,0 +1,116 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 CMCC Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.components.impl; + + +import javax.servlet.ServletContext; +import org.openecomp.sdc.be.datatypes.components.ServiceMetadataDataDefinition; +import org.openecomp.sdc.be.impl.WebAppContextWrapper; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.UploadServiceInfo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.WebApplicationContext; + + +//upload Service model by Shiyong1989@hotmail.com +@Component("ServiceImportManager") +public class ServiceImportManager { + + private ServletContext servletContext; + + private ResponseFormatManager responseFormatManager; + + @Autowired + private ServiceBusinessLogic serviceBusinessLogic; + + public ServiceImportBusinessLogic getServiceImportBusinessLogic() { + return serviceImportBusinessLogic; + } + + public void setServiceImportBusinessLogic( + ServiceImportBusinessLogic serviceImportBusinessLogic) { + this.serviceImportBusinessLogic = serviceImportBusinessLogic; + } + + @Autowired + private ServiceImportBusinessLogic serviceImportBusinessLogic; + + public boolean isServiceExist(String serviceName) { + return serviceBusinessLogic.isServiceExist(serviceName); + } + + + public ServiceBusinessLogic getServiceBusinessLogic() { + return serviceBusinessLogic; + } + + public void setServiceBusinessLogic( + ServiceBusinessLogic serviceBusinessLogic) { + this.serviceBusinessLogic = serviceBusinessLogic; + } + + + public void populateServiceMetadata(UploadServiceInfo serviceMetaData, Service service) { + if (service != null && serviceMetaData != null) { + service.setDescription(serviceMetaData.getDescription()); + service.setTags(serviceMetaData.getTags()); + service.setCategories(serviceMetaData.getCategories()); + service.setContactId(serviceMetaData.getContactId()); + service.setName(serviceMetaData.getName()); + service.setIcon(serviceMetaData.getServiceIconPath()); + service.setServiceVendorModelNumber(serviceMetaData.getServiceVendorModelNumber()); + ServiceMetadataDataDefinition serviceMetadataDataDefinition = (ServiceMetadataDataDefinition) service + .getComponentMetadataDefinition().getMetadataDataDefinition(); + serviceMetadataDataDefinition.getServiceVendorModelNumber(); + service.setServiceType(""); + service.setProjectCode(serviceMetaData.getProjectCode()); + if (serviceMetaData.getVendorName() != null) { + service.setVendorName(serviceMetaData.getVendorName()); + } + if (serviceMetaData.getVendorRelease() != null) { + service.setVendorRelease(serviceMetaData.getVendorRelease()); + } + } + } + + public void populateRealServiceMetadata(UploadServiceInfo serviceMetaData, Service service) { + if (service != null && serviceMetaData != null) { + service.setTags(serviceMetaData.getTags()); + service.setContactId(serviceMetaData.getContactId()); + service.setName(serviceMetaData.getName()); + } + } + + public synchronized void init(ServletContext servletContext) { + if (this.servletContext == null) { + this.servletContext = servletContext; + responseFormatManager = ResponseFormatManager.getInstance(); + serviceBusinessLogic = getServiceBL(servletContext); + } + } + + private ServiceBusinessLogic getServiceBL(ServletContext context) { + WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(org.openecomp.sdc.common.api.Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR); + WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context); + return webApplicationContext.getBean(ServiceBusinessLogic.class); + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportParseLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportParseLogic.java new file mode 100644 index 0000000000..65576d1567 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportParseLogic.java @@ -0,0 +1,2602 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.be.components.impl; + + +import fj.data.Either; +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.openecomp.sdc.be.components.csar.CsarInfo; +import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo; +import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; +import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; +import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; +import org.openecomp.sdc.be.config.BeEcompErrorManager; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.datatypes.elements.*; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.CreatedFrom; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.category.CategoryDefinition; +import org.openecomp.sdc.be.model.category.SubCategoryDefinition; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; +import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation; +import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; +import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils; +import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo; +import org.openecomp.sdc.be.tosca.CsarUtils; +import org.openecomp.sdc.be.utils.CommonBeUtils; +import org.openecomp.sdc.be.utils.TypeUtils; +import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.common.util.GeneralUtility; +import org.openecomp.sdc.common.util.ValidationUtils; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.beans.factory.annotation.Autowired; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +import java.util.*; +import java.util.function.Function; + +import static java.util.stream.Collectors.*; +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; + +@Getter +@Setter +@org.springframework.stereotype.Component +public class ServiceImportParseLogic { + + private static final String INITIAL_VERSION = "0.1"; + private static final String CREATE_RESOURCE = "Create Resource"; + private static final String IN_RESOURCE = " in resource {} "; + private static final String COMPONENT_INSTANCE_WITH_NAME = "component instance with name "; + private static final String COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE = "component instance with name {} in resource {} "; + private static final String CERTIFICATION_ON_IMPORT = "certification on import"; + private static final String VALIDATE_DERIVED_BEFORE_UPDATE = "validate derived before update"; + private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes"; + private static final String CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES = "Create Resource - validateCapabilityTypesCreate"; + private static final String CATEGORY_IS_EMPTY = "Resource category is empty"; + + + @Autowired + private ServiceBusinessLogic serviceBusinessLogic; + public ServiceBusinessLogic getServiceBusinessLogic() { + return serviceBusinessLogic; + } + + public void setServiceBusinessLogic( + ServiceBusinessLogic serviceBusinessLogic) { + this.serviceBusinessLogic = serviceBusinessLogic; + } + + @Autowired + protected ComponentsUtils componentsUtils; + @Autowired + protected ToscaOperationFacade toscaOperationFacade; + @Autowired + private LifecycleBusinessLogic lifecycleBusinessLogic; + @Autowired + private InputsBusinessLogic inputsBusinessLogic; + @Autowired + private ResourceImportManager resourceImportManager; + @Autowired + private IInterfaceLifecycleOperation interfaceTypeOperation = null; + private static final Logger log = Logger.getLogger(ServiceImportParseLogic.class); + + @Autowired + private ICapabilityTypeOperation capabilityTypeOperation = null; + + public ICapabilityTypeOperation getCapabilityTypeOperation() { + return capabilityTypeOperation; + } + + public void setCapabilityTypeOperation(ICapabilityTypeOperation capabilityTypeOperation) { + this.capabilityTypeOperation = capabilityTypeOperation; + } + + public Either<Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandle( + Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, Service oldResource) { + + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + Either<Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> nodeTypesArtifactsToHandleRes + = Either.left(nodeTypesArtifactsToHandle); + + try { + Map<String, List<ArtifactDefinition>> extractedVfcsArtifacts = CsarUtils.extractVfcsArtifactsFromCsar(csarInfo.getCsar()); + Map<String, ImmutablePair<String, String>> extractedVfcToscaNames = + extractVfcToscaNames(nodeTypesInfo, oldResource.getName(), csarInfo); + log.debug("Going to fetch node types for resource with name {} during import csar with UUID {}. ", + oldResource.getName(), csarInfo.getCsarUUID()); + extractedVfcToscaNames.forEach((namespace, vfcToscaNames) -> findAddNodeTypeArtifactsToHandle(csarInfo, nodeTypesArtifactsToHandle, oldResource, + extractedVfcsArtifacts, + namespace, vfcToscaNames)); + } catch (Exception e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + nodeTypesArtifactsToHandleRes = Either.right(responseFormat); + log.debug("Exception occured when findNodeTypesUpdatedArtifacts, error is:{}", e.getMessage(), e); + } + return nodeTypesArtifactsToHandleRes; + } + + private Map<String, ImmutablePair<String, String>> extractVfcToscaNames(Map<String, NodeTypeInfo> nodeTypesInfo, + String vfResourceName, CsarInfo csarInfo) { + Map<String, ImmutablePair<String, String>> vfcToscaNames = new HashMap<>(); + + Map<String, Object> nodes = extractAllNodes(nodeTypesInfo, csarInfo); + if (!nodes.isEmpty()) { + Iterator<Map.Entry<String, Object>> nodesNameEntry = nodes.entrySet().iterator(); + while (nodesNameEntry.hasNext()) { + Map.Entry<String, Object> nodeType = nodesNameEntry.next(); + ImmutablePair<String, String> toscaResourceName = buildNestedToscaResourceName( + ResourceTypeEnum.VFC.name(), vfResourceName, nodeType.getKey()); + vfcToscaNames.put(nodeType.getKey(), toscaResourceName); + } + } + for (NodeTypeInfo cvfc : nodeTypesInfo.values()) { + vfcToscaNames.put(cvfc.getType(), + buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), vfResourceName, cvfc.getType())); + } + return vfcToscaNames; + } + + public String buildNodeTypeYaml(Map.Entry<String, Object> nodeNameValue, Map<String, Object> mapToConvert, + String nodeResourceType, CsarInfo csarInfo) { + // We need to create a Yaml from each node_types in order to create + // resource from each node type using import normative flow. + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + Yaml yaml = new Yaml(options); + + Map<String, Object> node = new HashMap<>(); + node.put(buildNestedToscaResourceName(nodeResourceType, csarInfo.getVfResourceName(), nodeNameValue.getKey()) + .getLeft(), nodeNameValue.getValue()); + mapToConvert.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(), node); + + return yaml.dumpAsMap(mapToConvert); + } + + ImmutablePair<String, String> buildNestedToscaResourceName(String nodeResourceType, String vfResourceName, + String nodeTypeFullName) { + String actualType; + String actualVfName; + if (ResourceTypeEnum.CVFC.name().equals(nodeResourceType)) { + actualVfName = vfResourceName + ResourceTypeEnum.CVFC.name(); + actualType = ResourceTypeEnum.VFC.name(); + } else { + actualVfName = vfResourceName; + actualType = nodeResourceType; + } + String nameWithouNamespacePrefix; + try { + StringBuilder toscaResourceName = new StringBuilder(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX); + if (!nodeTypeFullName.contains(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX)){ + nameWithouNamespacePrefix = nodeTypeFullName; + } else { + nameWithouNamespacePrefix = nodeTypeFullName + .substring(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX.length()); + } + String[] findTypes = nameWithouNamespacePrefix.split("\\."); + String resourceType = findTypes[0]; + String actualName = nameWithouNamespacePrefix.substring(resourceType.length()); + + if (actualName.startsWith(Constants.ABSTRACT)) { + toscaResourceName.append(resourceType.toLowerCase()).append('.') + .append(ValidationUtils.convertToSystemName(actualVfName)); + } else { + toscaResourceName.append(actualType.toLowerCase()).append('.').append(ValidationUtils.convertToSystemName(actualVfName)); + //toscaResourceName.append(actualType.toLowerCase()); + } + StringBuilder previousToscaResourceName = new StringBuilder(toscaResourceName); + return new ImmutablePair<>(toscaResourceName.append(actualName.toLowerCase()).toString(), + previousToscaResourceName + .append(actualName.substring(actualName.split("\\.")[1].length() + 1).toLowerCase()) + .toString()); + } catch (Exception e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_TOSCA_TEMPLATE); + log.debug("Exception occured when buildNestedToscaResourceName, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE, vfResourceName); + } + } + + private Map<String, Object> extractAllNodes(Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) { + Map<String, Object> nodes = new HashMap<>(); + for (NodeTypeInfo nodeTypeInfo : nodeTypesInfo.values()) { + extractNodeTypes(nodes, nodeTypeInfo.getMappedToscaTemplate()); + } + extractNodeTypes(nodes, csarInfo.getMappedToscaMainTemplate()); + return nodes; + } + + private void extractNodeTypes(Map<String, Object> nodes, Map<String, Object> mappedToscaTemplate) { + Either<Map<String, Object>, ImportUtils.ResultStatusEnum> eitherNodeTypes = ImportUtils + .findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES); + if (eitherNodeTypes.isLeft()) { + nodes.putAll(eitherNodeTypes.left().value()); + } + } + + protected void findAddNodeTypeArtifactsToHandle(CsarInfo csarInfo, Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, + Service resource, Map<String, List<ArtifactDefinition>> extractedVfcsArtifacts, String namespace, ImmutablePair<String, String> vfcToscaNames){ + + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> curNodeTypeArtifactsToHandle = null; + log.debug("Going to fetch node type with tosca name {}. ", vfcToscaNames.getLeft()); + Resource curNodeType = findVfcResource(csarInfo, resource, vfcToscaNames.getLeft(), vfcToscaNames.getRight(), null); + if (!MapUtils.isEmpty(extractedVfcsArtifacts)) { + List<ArtifactDefinition> currArtifacts = new ArrayList<>(); + if (extractedVfcsArtifacts.containsKey(namespace)) { + handleAndAddExtractedVfcsArtifacts(currArtifacts, extractedVfcsArtifacts.get(namespace)); + } + curNodeTypeArtifactsToHandle = findNodeTypeArtifactsToHandle(curNodeType, currArtifacts); + } else if (curNodeType != null) { + // delete all artifacts if have not received artifacts from + // csar + try { + curNodeTypeArtifactsToHandle = new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); + List<ArtifactDefinition> artifactsToDelete = new ArrayList<>(); + // delete all informational artifacts + artifactsToDelete.addAll(curNodeType.getArtifacts().values().stream() + .filter(a -> a.getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL) + .collect(toList())); + // delete all deployment artifacts + artifactsToDelete.addAll(curNodeType.getDeploymentArtifacts().values()); + if (!artifactsToDelete.isEmpty()) { + curNodeTypeArtifactsToHandle + .put(ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE, artifactsToDelete); + } + } catch (Exception e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_TOSCA_TEMPLATE); + log.debug("Exception occured when findAddNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE, vfcToscaNames.getLeft()); + } + } + if (MapUtils.isNotEmpty(curNodeTypeArtifactsToHandle)) { + nodeTypesArtifactsToHandle.put(namespace, curNodeTypeArtifactsToHandle); + } + } + + protected void handleAndAddExtractedVfcsArtifacts(List<ArtifactDefinition> vfcArtifacts, + List<ArtifactDefinition> artifactsToAdd) { + List<String> vfcArtifactNames = vfcArtifacts.stream().map(ArtifactDataDefinition::getArtifactName) + .collect(toList()); + artifactsToAdd.stream().forEach(a -> { + if (!vfcArtifactNames.contains(a.getArtifactName())) { + vfcArtifacts.add(a); + } else { + log.debug("Can't upload two artifact with the same name {}. ", a.getArtifactName()); + } + }); + } + + protected Resource findVfcResource(CsarInfo csarInfo, Service resource, String currVfcToscaName, String previousVfcToscaName, StorageOperationStatus status) { + if (status != null && status != StorageOperationStatus.NOT_FOUND) { + log.debug("Error occured during fetching node type with tosca name {}, error: {}", currVfcToscaName, status); + throw new ComponentException(componentsUtils.convertFromStorageResponse(status), csarInfo.getCsarUUID()); + } else if (org.apache.commons.lang.StringUtils.isNotEmpty(currVfcToscaName)) { + return (Resource)toscaOperationFacade.getLatestByToscaResourceName(currVfcToscaName) + .left() + .on(st -> findVfcResource(csarInfo, resource, previousVfcToscaName, null, st)); + } + return null; + } + + protected EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> findNodeTypeArtifactsToHandle( + Resource curNodeType, List<ArtifactDefinition> extractedArtifacts) { + + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = null; + try { + List<ArtifactDefinition> artifactsToUpload = new ArrayList<>(extractedArtifacts); + List<ArtifactDefinition> artifactsToUpdate = new ArrayList<>(); + List<ArtifactDefinition> artifactsToDelete = new ArrayList<>(); + processExistingNodeTypeArtifacts(extractedArtifacts, artifactsToUpload, artifactsToUpdate, artifactsToDelete, + collectExistingArtifacts(curNodeType)); + nodeTypeArtifactsToHandle = putFoundArtifacts(artifactsToUpload, artifactsToUpdate, artifactsToDelete); + } catch (Exception e) { + log.debug("Exception occured when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + return nodeTypeArtifactsToHandle; + } + + protected Map<String, ArtifactDefinition> collectExistingArtifacts(Resource curNodeType) { + Map<String, ArtifactDefinition> existingArtifacts = new HashMap<>(); + if (curNodeType == null) { + return existingArtifacts; + } + if (MapUtils.isNotEmpty(curNodeType.getDeploymentArtifacts())) { + existingArtifacts.putAll(curNodeType.getDeploymentArtifacts()); + } + if (MapUtils.isNotEmpty(curNodeType.getArtifacts())) { + existingArtifacts + .putAll(curNodeType.getArtifacts().entrySet() + .stream() + .filter(e -> e.getValue().getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL) + .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))); + } + return existingArtifacts; + } + + protected EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> putFoundArtifacts(List<ArtifactDefinition> artifactsToUpload, List<ArtifactDefinition> artifactsToUpdate, List<ArtifactDefinition> artifactsToDelete) { + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = null; + if (!artifactsToUpload.isEmpty() || !artifactsToUpdate.isEmpty() || !artifactsToDelete.isEmpty()) { + nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); + if (!artifactsToUpload.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, artifactsToUpload); + } + if (!artifactsToUpdate.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE, artifactsToUpdate); + } + if (!artifactsToDelete.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE, artifactsToDelete); + } + } + return nodeTypeArtifactsToHandle; + } + + protected void processExistingNodeTypeArtifacts(List<ArtifactDefinition> extractedArtifacts, List<ArtifactDefinition> artifactsToUpload, + List<ArtifactDefinition> artifactsToUpdate, List<ArtifactDefinition> artifactsToDelete, + Map<String, ArtifactDefinition> existingArtifacts) { + try { + if (!existingArtifacts.isEmpty()) { + extractedArtifacts.stream() + .forEach(a -> processNodeTypeArtifact(artifactsToUpload, artifactsToUpdate, existingArtifacts, a)); + artifactsToDelete.addAll(existingArtifacts.values()); + } + } catch (Exception e) { + log.debug("Exception occured when processExistingNodeTypeArtifacts, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + + } + + protected void processNodeTypeArtifact(List<ArtifactDefinition> artifactsToUpload, List<ArtifactDefinition> artifactsToUpdate, Map<String, ArtifactDefinition> existingArtifacts, ArtifactDefinition currNewArtifact) { + Optional<ArtifactDefinition> foundArtifact = existingArtifacts.values() + .stream() + .filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName())) + .findFirst(); + if (foundArtifact.isPresent()) { + if (foundArtifact.get().getArtifactType().equals(currNewArtifact.getArtifactType())) { + updateFoundArtifact(artifactsToUpdate, currNewArtifact, foundArtifact.get()); + existingArtifacts.remove(foundArtifact.get().getArtifactLabel()); + artifactsToUpload.remove(currNewArtifact); + } else { + log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName()); + throw new ComponentException(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, + currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(), + foundArtifact.get().getArtifactType()); + } + } + } + + protected void updateFoundArtifact(List<ArtifactDefinition> artifactsToUpdate, ArtifactDefinition currNewArtifact, ArtifactDefinition foundArtifact) { + if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) { + foundArtifact.setPayload(currNewArtifact.getPayloadData()); + foundArtifact.setPayloadData( + Base64.encodeBase64String(currNewArtifact.getPayloadData())); + foundArtifact.setArtifactChecksum(GeneralUtility + .calculateMD5Base64EncodedByByteArray(currNewArtifact.getPayloadData())); + artifactsToUpdate.add(foundArtifact); + } + } + + public void addNonMetaCreatedArtifactsToSupportRollback(ArtifactOperationInfo operation, + List<ArtifactDefinition> createdArtifacts, + Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts) { + if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(operation.getArtifactOperationEnum()) && createdArtifacts != null + && eitherNonMetaArtifacts.isLeft()) { + Either<ArtifactDefinition, Operation> eitherResult = eitherNonMetaArtifacts.left().value(); + if (eitherResult.isLeft()) { + createdArtifacts.add(eitherResult.left().value()); + } + } + } + + public boolean isArtifactDeletionRequired(String artifactId, byte[] artifactFileBytes, boolean isFromCsar) { + return !org.apache.commons.lang.StringUtils.isEmpty(artifactId) && artifactFileBytes == null && isFromCsar; + } + + public void fillGroupsFinalFields(List<GroupDefinition> groupsAsList) { + groupsAsList.forEach(groupDefinition -> { + groupDefinition.setInvariantName(groupDefinition.getName()); + groupDefinition.setCreatedFrom(CreatedFrom.CSAR); + }); + } + + public String getComponentTypeForResponse(Component component) { + String componentTypeForResponse = "SERVICE"; + if (component instanceof Resource) { + componentTypeForResponse = ((Resource) component).getResourceType().name(); + } + return componentTypeForResponse; + } + + protected boolean isfillGroupMemebersRecursivlyStopCondition(String groupName, Map<String, GroupDefinition> allGroups, + Set<String> allGroupMembers) { + + boolean stop = false; + // In Case Not Group Stop + if (!allGroups.containsKey(groupName)) { + stop = true; + } + // In Case Group Has no members stop + if (!stop) { + GroupDefinition groupDefinition = allGroups.get(groupName); + stop = MapUtils.isEmpty(groupDefinition.getMembers()); + + } + // In Case all group members already contained stop + if (!stop) { + final Set<String> allMembers = allGroups.get(groupName).getMembers().keySet(); + Set<String> membersOfTypeGroup = allMembers.stream(). + // Filter In Only Group members + filter(allGroups::containsKey). + // Collect + collect(toSet()); + stop = allGroupMembers.containsAll(membersOfTypeGroup); + } + return stop; + } + + public Resource buildValidComplexVfc(Resource resource, CsarInfo csarInfo, String nodeName, + Map<String, NodeTypeInfo> nodesInfo) { + + Resource complexVfc = buildComplexVfcMetadata(resource, csarInfo, nodeName, nodesInfo); + log.debug("************* Going to validate complex VFC from yaml {}", complexVfc.getName()); + csarInfo.addNodeToQueue(nodeName); + return validateResourceBeforeCreate(complexVfc, csarInfo.getModifier(), + AuditingActionEnum.IMPORT_RESOURCE, true, csarInfo); + } + + public Resource validateResourceBeforeCreate(Resource resource, User user, AuditingActionEnum actionEnum, boolean inTransaction, CsarInfo csarInfo) { + + validateResourceFieldsBeforeCreate(user, resource, actionEnum, inTransaction); + validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), resource, actionEnum, inTransaction); + validateLifecycleTypesCreate(user, resource, actionEnum); + validateResourceType(user, resource, actionEnum); + resource.setCreatorUserId(user.getUserId()); + resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); + resource.setContactId(resource.getContactId().toLowerCase()); + if (org.apache.commons.lang.StringUtils.isEmpty(resource.getToscaResourceName()) && !ModelConverter.isAtomicComponent(resource)) { + String resourceSystemName; + if (csarInfo != null && org.apache.commons.lang.StringUtils.isNotEmpty(csarInfo.getVfResourceName())) { + resourceSystemName = ValidationUtils.convertToSystemName(csarInfo.getVfResourceName()); + } else { + resourceSystemName = resource.getSystemName(); + } + resource.setToscaResourceName(CommonBeUtils + .generateToscaResourceName(resource.getResourceType().name().toLowerCase(), resourceSystemName)); + } + + // Generate invariant UUID - must be here and not in operation since it + // should stay constant during clone + // TODO + String invariantUUID = UniqueIdBuilder.buildInvariantUUID(); + resource.setInvariantUUID(invariantUUID); + + return resource; + } + + protected Either<Boolean, ResponseFormat> validateResourceType(User user, Resource resource, + AuditingActionEnum actionEnum) { + Either<Boolean, ResponseFormat> eitherResult = Either.left(true); + if (resource.getResourceType() == null) { + log.debug("Invalid resource type for resource"); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); + eitherResult = Either.right(errorResponse); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + } + return eitherResult; + } + + protected Either<Boolean, ResponseFormat> validateLifecycleTypesCreate(User user, Resource resource, + AuditingActionEnum actionEnum) { + Either<Boolean, ResponseFormat> eitherResult = Either.left(true); + if (resource.getInterfaces() != null && resource.getInterfaces().size() > 0) { + log.debug("validate interface lifecycle Types Exist"); + Iterator<InterfaceDefinition> intItr = resource.getInterfaces().values().iterator(); + while (intItr.hasNext() && eitherResult.isLeft()) { + InterfaceDefinition interfaceDefinition = intItr.next(); + String intType = interfaceDefinition.getUniqueId(); + Either<InterfaceDefinition, StorageOperationStatus> eitherCapTypeFound = interfaceTypeOperation + .getInterface(intType); + if (eitherCapTypeFound.isRight()) { + if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) { + BeEcompErrorManager.getInstance().logBeGraphObjectMissingError( + "Create Resource - validateLifecycleTypesCreate", "Interface", intType); + log.debug("Lifecycle Type: {} is required by resource: {} but does not exist in the DB", + intType, resource.getName()); + BeEcompErrorManager.getInstance() + .logBeDaoSystemError("Create Resource - validateLifecycleTypesCreate"); + log.debug("request to data model failed with error: {}", + eitherCapTypeFound.right().value().name()); + } + ResponseFormat errorResponse = componentsUtils + .getResponseFormat(ActionStatus.MISSING_LIFECYCLE_TYPE, intType); + eitherResult = Either.right(errorResponse); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + } + } + } + return eitherResult; + } + + public Either<Boolean, ResponseFormat> validateCapabilityTypesCreate(User user, + ICapabilityTypeOperation capabilityTypeOperation, Resource resource, AuditingActionEnum actionEnum, + boolean inTransaction) { + + Either<Boolean, ResponseFormat> eitherResult = Either.left(true); + if (resource.getCapabilities() != null && resource.getCapabilities().size() > 0) { + log.debug("validate capability Types Exist - capabilities section"); + + for (Map.Entry<String, List<CapabilityDefinition>> typeEntry : resource.getCapabilities().entrySet()) { + + eitherResult = validateCapabilityTypeExists(user, capabilityTypeOperation, resource, actionEnum, + eitherResult, typeEntry, inTransaction); + if (eitherResult.isRight()) { + return Either.right(eitherResult.right().value()); + } + } + } + if (resource.getRequirements() != null && resource.getRequirements().size() > 0) { + log.debug("validate capability Types Exist - requirements section"); + for (String type : resource.getRequirements().keySet()) { + eitherResult = validateCapabilityTypeExists(user, capabilityTypeOperation, resource, + resource.getRequirements().get(type), actionEnum, eitherResult, type, inTransaction); + if (eitherResult.isRight()) { + return Either.right(eitherResult.right().value()); + } + } + } + return eitherResult; + } + + protected Either<Boolean, ResponseFormat> validateCapabilityTypeExists(User user, + ICapabilityTypeOperation capabilityTypeOperation, Resource resource, AuditingActionEnum actionEnum, + Either<Boolean, ResponseFormat> eitherResult, Map.Entry<String, List<CapabilityDefinition>> typeEntry, + boolean inTransaction) { + Either<CapabilityTypeDefinition, StorageOperationStatus> eitherCapTypeFound = capabilityTypeOperation + .getCapabilityType(typeEntry.getKey(), inTransaction); + if (eitherCapTypeFound.isRight()) { + if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) { + BeEcompErrorManager.getInstance().logBeGraphObjectMissingError( + CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES, "Capability Type", typeEntry.getKey()); + log.debug("Capability Type: {} is required by resource: {} but does not exist in the DB", + typeEntry.getKey(), resource.getName()); + BeEcompErrorManager.getInstance() + .logBeDaoSystemError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES); + } + log.debug("Trying to get capability type {} failed with error: {}", typeEntry.getKey(), + eitherCapTypeFound.right().value().name()); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, + typeEntry.getKey()); + eitherResult = Either.right(errorResponse); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + return Either.right(eitherResult.right().value()); + } + CapabilityTypeDefinition capabilityTypeDefinition = eitherCapTypeFound.left().value(); + if (capabilityTypeDefinition.getProperties() != null) { + for (CapabilityDefinition capDef : typeEntry.getValue()) { + List<ComponentInstanceProperty> properties = capDef.getProperties(); + if (properties == null || properties.isEmpty()) { + properties = new ArrayList<>(); + for (Map.Entry<String, PropertyDefinition> prop : capabilityTypeDefinition.getProperties().entrySet()) { + ComponentInstanceProperty newProp = new ComponentInstanceProperty(prop.getValue()); + properties.add(newProp); + } + } else { + for (Map.Entry<String, PropertyDefinition> prop : capabilityTypeDefinition.getProperties().entrySet()) { + PropertyDefinition porpFromDef = prop.getValue(); + List<ComponentInstanceProperty> propsToAdd = new ArrayList<>(); + for (ComponentInstanceProperty cip : properties) { + if (!cip.getName().equals(porpFromDef.getName())) { + ComponentInstanceProperty newProp = new ComponentInstanceProperty(porpFromDef); + propsToAdd.add(newProp); + } + } + if (!propsToAdd.isEmpty()) { + properties.addAll(propsToAdd); + } + } + } + capDef.setProperties(properties); + } + } + return eitherResult; + } + + protected Either<Boolean, ResponseFormat> validateCapabilityTypeExists(User user, + ICapabilityTypeOperation capabilityTypeOperation, Resource resource, List<?> validationObjects, + AuditingActionEnum actionEnum, Either<Boolean, ResponseFormat> eitherResult, String type, + boolean inTransaction) { + try { + Either<CapabilityTypeDefinition, StorageOperationStatus> eitherCapTypeFound = capabilityTypeOperation + .getCapabilityType(type, inTransaction); + if (eitherCapTypeFound.isRight()) { + if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) { + BeEcompErrorManager.getInstance().logBeGraphObjectMissingError( + CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES, "Capability Type", type); + log.debug("Capability Type: {} is required by resource: {} but does not exist in the DB", type, + resource.getName()); + BeEcompErrorManager.getInstance() + .logBeDaoSystemError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES); + } + log.debug("Trying to get capability type {} failed with error: {}", type, + eitherCapTypeFound.right().value().name()); + ResponseFormat errorResponse = null; + if (type != null) { + errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, type); + } else { + errorResponse = componentsUtils.getResponseFormatByElement(ActionStatus.MISSING_CAPABILITY_TYPE, + validationObjects); + } + eitherResult = Either.right(errorResponse); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + } + + } catch (Exception e) { + log.debug("Exception occured when validateCapabilityTypeExists, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE, resource.getName()); + } + return eitherResult; + } + + protected Either<Boolean, ResponseFormat> validateResourceFieldsBeforeCreate(User user, Resource resource, + AuditingActionEnum actionEnum, boolean inTransaction) { + serviceBusinessLogic.validateComponentFieldsBeforeCreate(user, resource, actionEnum); + // validate category + log.debug("validate category"); + validateCategory(user, resource, actionEnum, inTransaction); + // validate vendor name & release & model number + log.debug("validate vendor name"); + validateVendorName(user, resource, actionEnum); + log.debug("validate vendor release"); + validateVendorReleaseName(user, resource, actionEnum); + log.debug("validate resource vendor model number"); + validateResourceVendorModelNumber(user, resource, actionEnum); + // validate cost + log.debug("validate cost"); + validateCost(resource); + // validate licenseType + log.debug("validate licenseType"); + validateLicenseType(user, resource, actionEnum); + // validate template (derived from) + log.debug("validate derived from"); + if (!ModelConverter.isAtomicComponent(resource) && resource.getResourceType() != ResourceTypeEnum.VF) { + resource.setDerivedFrom(null); + } + validateDerivedFromExist(user, resource, actionEnum); + serviceBusinessLogic.checkComponentFieldsForOverrideAttempt(resource); + String currentCreatorFullName = resource.getCreatorFullName(); + if (currentCreatorFullName != null) { + log.debug("Resource Creator fullname is automatically set and cannot be updated"); + } + + String currentLastUpdaterFullName = resource.getLastUpdaterFullName(); + if (currentLastUpdaterFullName != null) { + log.debug("Resource LastUpdater fullname is automatically set and cannot be updated"); + } + + Long currentLastUpdateDate = resource.getLastUpdateDate(); + if (currentLastUpdateDate != null) { + log.debug("Resource last update date is automatically set and cannot be updated"); + } + + Boolean currentAbstract = resource.isAbstract(); + if (currentAbstract != null) { + log.debug("Resource abstract is automatically set and cannot be updated"); + } + + return Either.left(true); + } + + protected void validateDerivedFromExist(User user, Resource resource, AuditingActionEnum actionEnum) { + if (resource.getDerivedFrom() == null || resource.getDerivedFrom().isEmpty()) { + return; + } + String templateName = resource.getDerivedFrom().get(0); + Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade + .validateToscaResourceNameExists(templateName); + if (dataModelResponse.isRight()) { + StorageOperationStatus storageStatus = dataModelResponse.right().value(); + BeEcompErrorManager.getInstance().logBeDaoSystemError("Create Resource - validateDerivedFromExist"); + log.debug("request to data model failed with error: {}", storageStatus); + ResponseFormat responseFormat = componentsUtils + .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus), resource); + log.trace("audit before sending response"); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw new ComponentException(componentsUtils.convertFromStorageResponse(storageStatus)); + } else if (!dataModelResponse.left().value()) { + log.info("resource template with name: {}, does not exists", templateName); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PARENT_RESOURCE_NOT_FOUND); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw new ComponentException(ActionStatus.PARENT_RESOURCE_NOT_FOUND); + } + } + + protected void validateLicenseType(User user, Resource resource, + AuditingActionEnum actionEnum) { + log.debug("validate licenseType"); + String licenseType = resource.getLicenseType(); + if (licenseType != null) { + List<String> licenseTypes = ConfigurationManager.getConfigurationManager().getConfiguration() + .getLicenseTypes(); + if (!licenseTypes.contains(licenseType)) { + log.debug("License type {} isn't configured", licenseType); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); + if (actionEnum != null) { + // In update case, no audit is required + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + } + throw new ComponentException(ActionStatus.INVALID_CONTENT); + } + } + } + + protected void validateCost(Resource resource) { + String cost = resource.getCost(); + if (cost != null) { + if (!ValidationUtils.validateCost(cost)) { + log.debug("resource cost is invalid."); + throw new ComponentException(ActionStatus.INVALID_CONTENT); + } + } + } + + protected void validateResourceVendorModelNumber(User user, Resource resource, AuditingActionEnum actionEnum) { + String resourceVendorModelNumber = resource.getResourceVendorModelNumber(); + if (org.apache.commons.lang.StringUtils.isNotEmpty(resourceVendorModelNumber)) { + if (!ValidationUtils.validateResourceVendorModelNumberLength(resourceVendorModelNumber)) { + log.info("resource vendor model number exceeds limit."); + ResponseFormat errorResponse = componentsUtils.getResponseFormat( + ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT, + "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + throw new ComponentException(ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT, + "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH); + } + // resource vendor model number is currently validated as vendor + // name + if (!ValidationUtils.validateVendorName(resourceVendorModelNumber)) { + log.info("resource vendor model number is not valid."); + ResponseFormat errorResponse = componentsUtils + .getResponseFormat(ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + throw new ComponentException(ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER); + } + } + } + + public void validateVendorReleaseName(User user, Resource resource, AuditingActionEnum actionEnum) { + String vendorRelease = resource.getVendorRelease(); + log.debug("validate vendor relese name"); + if (!ValidationUtils.validateStringNotEmpty(vendorRelease)) { + log.info("vendor relese name is missing."); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_RELEASE); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + throw new ComponentException(ActionStatus.MISSING_VENDOR_RELEASE); + } + + validateVendorReleaseName(vendorRelease, user, resource, actionEnum); + } + + public void validateVendorReleaseName(String vendorRelease, User user, Resource resource, AuditingActionEnum actionEnum) { + if (vendorRelease != null) { + if (!ValidationUtils.validateVendorReleaseLength(vendorRelease)) { + log.info("vendor release exceds limit."); + ResponseFormat errorResponse = componentsUtils.getResponseFormat( + ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + throw new ComponentException(ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH); + } + + if (!ValidationUtils.validateVendorRelease(vendorRelease)) { + log.info("vendor release is not valid."); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_RELEASE); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + throw new ComponentException(ActionStatus.INVALID_VENDOR_RELEASE); + } + } + } + + protected void validateCategory(User user, Resource resource, + AuditingActionEnum actionEnum, boolean inTransaction) { + + List<CategoryDefinition> categories = resource.getCategories(); + if (CollectionUtils.isEmpty(categories)) { + log.debug(CATEGORY_IS_EMPTY); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw new ComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + } + if (categories.size() > 1) { + log.debug("Must be only one category for resource"); + throw new ComponentException(ActionStatus.COMPONENT_TOO_MUCH_CATEGORIES, ComponentTypeEnum.RESOURCE.getValue()); + } + CategoryDefinition category = categories.get(0); + List<SubCategoryDefinition> subcategories = category.getSubcategories(); + if (CollectionUtils.isEmpty(subcategories)) { + log.debug("Missinig subcategory for resource"); + throw new ComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY); + } + if (subcategories.size() > 1) { + log.debug("Must be only one sub category for resource"); + throw new ComponentException(ActionStatus.RESOURCE_TOO_MUCH_SUBCATEGORIES); + } + + SubCategoryDefinition subcategory = subcategories.get(0); + + if (!ValidationUtils.validateStringNotEmpty(category.getName())) { + log.debug(CATEGORY_IS_EMPTY); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw new ComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + } + if (!ValidationUtils.validateStringNotEmpty(subcategory.getName())) { + log.debug(CATEGORY_IS_EMPTY); + ResponseFormat responseFormat = componentsUtils.getResponseFormat( + ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue()); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw new ComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue()); + } + + validateCategoryListed(category, subcategory, user, resource, actionEnum, inTransaction); + } + + protected void validateCategoryListed(CategoryDefinition category, SubCategoryDefinition subcategory, + User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) { + ResponseFormat responseFormat; + if (category != null && subcategory != null) { + try { + log.debug("validating resource category {} against valid categories list", category); + Either<List<CategoryDefinition>, ActionStatus> categories = serviceBusinessLogic.elementDao + .getAllCategories(NodeTypeEnum.ResourceNewCategory, inTransaction); + if (categories.isRight()) { + log.debug("failed to retrieve resource categories from Titan"); + responseFormat = componentsUtils.getResponseFormat(categories.right().value()); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw new ComponentException(categories.right().value()); + } + List<CategoryDefinition> categoryList = categories.left().value(); + Optional<CategoryDefinition> foundCategory = categoryList.stream() + .filter(cat -> cat.getName().equals(category.getName())) + .findFirst(); + if (!foundCategory.isPresent()) { + log.debug( + "Category {} is not part of resource category group. Resource category valid values are {}", + category, categoryList); + failOnInvalidCategory(user, resource, actionEnum); + } + Optional<SubCategoryDefinition> foundSubcategory = foundCategory.get() + .getSubcategories() + .stream() + .filter(subcat -> subcat.getName().equals(subcategory.getName())) + .findFirst(); + if (!foundSubcategory.isPresent()) { + log.debug( + "SubCategory {} is not part of resource category group. Resource subcategory valid values are {}", + subcategory, foundCategory.get().getSubcategories()); + failOnInvalidCategory(user, resource, actionEnum); + } + }catch(Exception e){ + log.debug("Exception occured when validateCategoryListed, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + } + + protected void failOnInvalidCategory(User user, Resource resource, AuditingActionEnum actionEnum) { + ResponseFormat responseFormat; + responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_CATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw new ComponentException(ActionStatus.COMPONENT_INVALID_CATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + } + + protected void validateVendorName(User user, Resource resource, + AuditingActionEnum actionEnum) { + String vendorName = resource.getVendorName(); + if (!ValidationUtils.validateStringNotEmpty(vendorName)) { + log.info("vendor name is missing."); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_NAME); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + throw new ComponentException(ActionStatus.MISSING_VENDOR_NAME); + } + validateVendorName(vendorName, user, resource, actionEnum); + } + + protected void validateVendorName(String vendorName, User user, Resource resource, + AuditingActionEnum actionEnum) { + if (vendorName != null) { + if (!ValidationUtils.validateVendorNameLength(vendorName)) { + log.info("vendor name exceds limit."); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT, + "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + throw new ComponentException(ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT, + "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH); + } + + if (!ValidationUtils.validateVendorName(vendorName)) { + log.info("vendor name is not valid."); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_NAME); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + throw new ComponentException(ActionStatus.INVALID_VENDOR_NAME); + } + } + } + + private Resource buildComplexVfcMetadata(Resource resourceVf, CsarInfo csarInfo, String nodeName, + Map<String, NodeTypeInfo> nodesInfo) { + Resource cvfc = new Resource(); + NodeTypeInfo nodeTypeInfo = nodesInfo.get(nodeName); + cvfc.setName(buildCvfcName(csarInfo.getVfResourceName(), nodeName)); + cvfc.setNormalizedName(ValidationUtils.normaliseComponentName(cvfc.getName())); + cvfc.setSystemName(ValidationUtils.convertToSystemName(cvfc.getName())); + cvfc.setResourceType(ResourceTypeEnum.VF); + cvfc.setAbstract(true); + cvfc.setDerivedFrom(nodeTypeInfo.getDerivedFrom()); + cvfc.setDescription(ImportUtils.Constants.CVFC_DESCRIPTION); + cvfc.setIcon(ImportUtils.Constants.DEFAULT_ICON); + cvfc.setContactId(csarInfo.getModifier().getUserId()); + cvfc.setCreatorUserId(csarInfo.getModifier().getUserId()); + cvfc.setVendorName(resourceVf.getVendorName()); + cvfc.setVendorRelease(resourceVf.getVendorRelease()); + cvfc.setResourceVendorModelNumber(resourceVf.getResourceVendorModelNumber()); + cvfc.setToscaResourceName( + buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(), nodeName) + .getLeft()); + cvfc.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID()); + + List<String> tags = new ArrayList<>(); + tags.add(cvfc.getName()); + cvfc.setTags(tags); + + CategoryDefinition category = new CategoryDefinition(); + category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME); + SubCategoryDefinition subCategory = new SubCategoryDefinition(); + subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY); + category.addSubCategory(subCategory); + List<CategoryDefinition> categories = new ArrayList<>(); + categories.add(category); + cvfc.setCategories(categories); + + cvfc.setVersion(ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION); + cvfc.setLifecycleState(ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT); + cvfc.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION); + + return cvfc; + } + + private String buildCvfcName(String resourceVfName, String nodeName) { + String nameWithouNamespacePrefix = nodeName + .substring(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX.length()); + String[] findTypes = nameWithouNamespacePrefix.split("\\."); + String resourceType = findTypes[0]; + String resourceName = resourceVfName + "-" + nameWithouNamespacePrefix.substring(resourceType.length() + 1); + return addCvfcSuffixToResourceName(resourceName); + } + + private String addCvfcSuffixToResourceName(String resourceName) { + return resourceName + "VF"; + } + + public UploadResourceInfo fillResourceMetadata(String yamlName, Resource resourceVf, + String nodeName, User user) { + UploadResourceInfo resourceMetaData = new UploadResourceInfo(); + + // validate nodetype name prefix + if (!nodeName.startsWith(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX)) { + log.debug("invalid nodeName:{} does not start with {}.", nodeName, + Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX); + throw new ComponentException(ActionStatus.INVALID_NODE_TEMPLATE, + yamlName, resourceMetaData.getName(), nodeName); + } + + String actualName = this.getNodeTypeActualName(nodeName); + String namePrefix = nodeName.replace(actualName, ""); + String resourceType = namePrefix.substring(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX.length()); + + // if we import from csar, the node_type name can be + // org.openecomp.resource.abstract.node_name - in this case we always + // create a vfc + if (resourceType.equals(Constants.ABSTRACT)) { + resourceType = ResourceTypeEnum.VFC.name().toLowerCase(); + } + // validating type + if (!ResourceTypeEnum.containsName(resourceType.toUpperCase())) { + log.debug("invalid resourceType:{} the type is not one of the valide types:{}.", resourceType.toUpperCase(), + ResourceTypeEnum.values()); + throw new ComponentException(ActionStatus.INVALID_NODE_TEMPLATE, + yamlName, resourceMetaData.getName(), nodeName); + } + + // Setting name + resourceMetaData.setName(resourceVf.getSystemName() + actualName); + + // Setting type from name + String type = resourceType.toUpperCase(); + resourceMetaData.setResourceType(type); + + resourceMetaData.setDescription(ImportUtils.Constants.INNER_VFC_DESCRIPTION); + resourceMetaData.setIcon(ImportUtils.Constants.DEFAULT_ICON); + resourceMetaData.setContactId(user.getUserId()); + resourceMetaData.setVendorName(resourceVf.getVendorName()); + resourceMetaData.setVendorRelease(resourceVf.getVendorRelease()); + + // Setting tag + List<String> tags = new ArrayList<>(); + tags.add(resourceMetaData.getName()); + resourceMetaData.setTags(tags); + + // Setting category + CategoryDefinition category = new CategoryDefinition(); + category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME); + SubCategoryDefinition subCategory = new SubCategoryDefinition(); + subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY); + category.addSubCategory(subCategory); + List<CategoryDefinition> categories = new ArrayList<>(); + categories.add(category); + resourceMetaData.setCategories(categories); + + return resourceMetaData; + } + + protected String getNodeTypeActualName(String fullName) { + String nameWithouNamespacePrefix = fullName + .substring(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX.length()); + String[] findTypes = nameWithouNamespacePrefix.split("\\."); + String resourceType = findTypes[0]; + return nameWithouNamespacePrefix.substring(resourceType.length()); + } + + public void addInput(Map<String, InputDefinition> currPropertiesMap, InputDefinition prop) { + String propName = prop.getName(); + if (!currPropertiesMap.containsKey(propName)) { + currPropertiesMap.put(propName, prop); + } + } + + public Either<RequirementDefinition, ResponseFormat> findAviableRequiremen(String regName, String yamlName, + UploadComponentInstanceInfo uploadComponentInstanceInfo, ComponentInstance currentCompInstance, + String capName) { + Map<String, List<RequirementDefinition>> comInstRegDefMap = currentCompInstance.getRequirements(); + List<RequirementDefinition> list = comInstRegDefMap.get(capName); + RequirementDefinition validRegDef = null; + if (list == null) { + for (Map.Entry<String, List<RequirementDefinition>> entry : comInstRegDefMap.entrySet()) { + for (RequirementDefinition reqDef : entry.getValue()) { + if (reqDef.getName().equals(regName)) { + if (reqDef.getMaxOccurrences() != null + && !reqDef.getMaxOccurrences().equals(RequirementDataDefinition.MAX_OCCURRENCES)) { + String leftOccurrences = reqDef.getLeftOccurrences(); + if (leftOccurrences == null) { + leftOccurrences = reqDef.getMaxOccurrences(); + } + int left = Integer.parseInt(leftOccurrences); + if (left > 0) { + --left; + reqDef.setLeftOccurrences(String.valueOf(left)); + validRegDef = reqDef; + break; + } else { + continue; + } + } else { + validRegDef = reqDef; + break; + } + + } + } + if (validRegDef != null) { + break; + } + } + } else { + for (RequirementDefinition reqDef : list) { + if (reqDef.getName().equals(regName)) { + if (reqDef.getMaxOccurrences() != null + && !reqDef.getMaxOccurrences().equals(RequirementDataDefinition.MAX_OCCURRENCES)) { + String leftOccurrences = reqDef.getLeftOccurrences(); + if (leftOccurrences == null) { + leftOccurrences = reqDef.getMaxOccurrences(); + } + int left = Integer.parseInt(leftOccurrences); + if (left > 0) { + --left; + reqDef.setLeftOccurrences(String.valueOf(left)); + validRegDef = reqDef; + break; + } else { + continue; + } + } else { + validRegDef = reqDef; + break; + } + } + } + } + if (validRegDef == null) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, + yamlName, uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); + return Either.right(responseFormat); + } + return Either.left(validRegDef); + } + + public CapabilityDefinition findAvailableCapabilityByTypeOrName(RequirementDefinition validReq, + ComponentInstance currentCapCompInstance, UploadReqInfo uploadReqInfo) { + try { + if (null == uploadReqInfo.getCapabilityName() + || validReq.getCapability().equals(uploadReqInfo.getCapabilityName())) { + // get by capability type + return findAvailableCapability(validReq, currentCapCompInstance); + } + return findAvailableCapability(validReq, currentCapCompInstance, uploadReqInfo); + }catch (Exception e){ + log.debug("Exception occured when findAvailableCapabilityByTypeOrName, error is:{}", e.getMessage(), e); + throw new ComponentException(ActionStatus.GENERAL_ERROR); + } + } + + protected CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance instance) { + Map<String, List<CapabilityDefinition>> capMap = instance.getCapabilities(); + if (capMap.containsKey(validReq.getCapability())) { + List<CapabilityDefinition> capList = capMap.get(validReq.getCapability()); + + for (CapabilityDefinition cap : capList) { + if (isBoundedByOccurrences(cap)) { + String leftOccurrences = cap.getLeftOccurrences() != null ? + cap.getLeftOccurrences() : cap.getMaxOccurrences(); + int left = Integer.parseInt(leftOccurrences); + if (left > 0) { + --left; + cap.setLeftOccurrences(String.valueOf(left)); + return cap; + } + } else { + return cap; + } + } + } + return null; + } + + protected CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, + ComponentInstance currentCapCompInstance, UploadReqInfo uploadReqInfo) { + CapabilityDefinition cap = null; + Map<String, List<CapabilityDefinition>> capMap = currentCapCompInstance.getCapabilities(); + if (!capMap.containsKey(validReq.getCapability())) { + return null; + } + Optional<CapabilityDefinition> capByName = capMap.get(validReq.getCapability()).stream() + .filter(p -> p.getName().equals(uploadReqInfo.getCapabilityName())).findAny(); + if (!capByName.isPresent()) { + return null; + } + cap = capByName.get(); + + if (isBoundedByOccurrences(cap)) { + String leftOccurrences = cap.getLeftOccurrences(); + int left = Integer.parseInt(leftOccurrences); + if (left > 0) { + --left; + cap.setLeftOccurrences(String.valueOf(left)); + + } + + } + return cap; + } + + private boolean isBoundedByOccurrences(CapabilityDefinition cap) { + return cap.getMaxOccurrences() != null && !cap.getMaxOccurrences().equals(CapabilityDataDefinition.MAX_OCCURRENCES); + } + + public ComponentParametersView getComponentFilterAfterCreateRelations() { + ComponentParametersView parametersView = new ComponentParametersView(); + parametersView.disableAll(); + parametersView.setIgnoreComponentInstances(false); + parametersView.setIgnoreComponentInstancesProperties(false); + parametersView.setIgnoreCapabilities(false); + parametersView.setIgnoreRequirements(false); + parametersView.setIgnoreGroups(false); + return parametersView; + } + + public ComponentParametersView getComponentWithInstancesFilter() { + ComponentParametersView parametersView = new ComponentParametersView(); + parametersView.disableAll(); + parametersView.setIgnoreComponentInstances(false); + parametersView.setIgnoreInputs(false); + // inputs are read when creating + // property values on instances + parametersView.setIgnoreUsers(false); + return parametersView; + } + + protected void addValidComponentInstanceCapabilities(String key, List<UploadCapInfo> capabilities, String resourceId, Map<String, List<CapabilityDefinition>> defaultCapabilities, Map<String, List<CapabilityDefinition>> validCapabilitiesMap){ + String capabilityType = capabilities.get(0).getType(); + if (defaultCapabilities.containsKey(capabilityType)) { + CapabilityDefinition defaultCapability = getCapability(resourceId, defaultCapabilities, capabilityType); + validateCapabilityProperties(capabilities, resourceId, defaultCapability); + List<CapabilityDefinition> validCapabilityList = new ArrayList<>(); + validCapabilityList.add(defaultCapability); + validCapabilitiesMap.put(key, validCapabilityList); + } else { + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, capabilityType)); + } + } + + protected CapabilityDefinition getCapability(String resourceId, Map<String, List<CapabilityDefinition>> defaultCapabilities, String capabilityType) { + CapabilityDefinition defaultCapability; + if (isNotEmpty(defaultCapabilities.get(capabilityType).get(0).getProperties())) { + defaultCapability = defaultCapabilities.get(capabilityType).get(0); + } else { + Either<Component, StorageOperationStatus> getFullComponentRes = toscaOperationFacade + .getToscaFullElement(resourceId); + if (getFullComponentRes.isRight()) { + log.debug("Failed to get full component {}. Status is {}. ", resourceId, + getFullComponentRes.right().value()); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_FOUND, + resourceId)); + } + defaultCapability = getFullComponentRes.left().value().getCapabilities().get(capabilityType).get(0); + } + return defaultCapability; + } + + protected void validateCapabilityProperties(List<UploadCapInfo> capabilities, String resourceId, CapabilityDefinition defaultCapability) { + if (CollectionUtils.isEmpty(defaultCapability.getProperties()) + && isNotEmpty(capabilities.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()); + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, resourceId)); + } else if (isNotEmpty(capabilities.get(0).getProperties())) { + validateUniquenessUpdateUploadedComponentInstanceCapability(defaultCapability, capabilities.get(0)); + } + } + + protected void validateUniquenessUpdateUploadedComponentInstanceCapability( + CapabilityDefinition defaultCapability, UploadCapInfo uploadedCapability) { + List<ComponentInstanceProperty> validProperties = new ArrayList<>(); + Map<String, PropertyDefinition> defaultProperties = defaultCapability.getProperties().stream() + .collect(toMap(PropertyDefinition::getName, Function + .identity())); + List<UploadPropInfo> uploadedProperties = uploadedCapability.getProperties(); + for (UploadPropInfo property : uploadedProperties) { + String propertyName = property.getName().toLowerCase(); + String propertyType = property.getType(); + ComponentInstanceProperty validProperty; + if (defaultProperties.containsKey(propertyName) && propertTypeEqualsTo(defaultProperties, propertyName, propertyType)) { + throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NAME_ALREADY_EXISTS, + propertyName)); + } + validProperty = new ComponentInstanceProperty(); + validProperty.setName(propertyName); + if (property.getValue() != null) { + validProperty.setValue(property.getValue().toString()); + } + validProperty.setDescription(property.getDescription()); + validProperty.setPassword(property.isPassword()); + validProperties.add(validProperty); + } + defaultCapability.setProperties(validProperties); + } + + private boolean propertTypeEqualsTo(Map<String, PropertyDefinition> defaultProperties, String propertyName, String propertyType) { + return propertyType != null && !defaultProperties.get(propertyName).getType().equals(propertyType); + } + + public void setDeploymentArtifactsPlaceHolder(Component component, User user) { + if(component instanceof Service){ + Service service = (Service) component; + Map<String, ArtifactDefinition> artifactMap = service.getDeploymentArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + service.setDeploymentArtifacts(artifactMap); + }else if(component instanceof Resource){ + Resource resource = (Resource) component; + Map<String, ArtifactDefinition> artifactMap = resource.getDeploymentArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + Map<String, Object> deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager() + .getConfiguration().getDeploymentResourceArtifacts(); + if (deploymentResourceArtifacts != null) { + Map<String, ArtifactDefinition> finalArtifactMap = artifactMap; + deploymentResourceArtifacts.forEach((k, v)->processDeploymentResourceArtifacts(user, resource, finalArtifactMap, k,v)); + } + resource.setDeploymentArtifacts(artifactMap); + } + + } + + protected void processDeploymentResourceArtifacts(User user, Resource resource, Map<String, ArtifactDefinition> artifactMap, String k, Object v) { + boolean shouldCreateArtifact = true; + Map<String, Object> artifactDetails = (Map<String, Object>) v; + Object object = artifactDetails.get(PLACE_HOLDER_RESOURCE_TYPES); + if (object != null) { + List<String> artifactTypes = (List<String>) object; + if (!artifactTypes.contains(resource.getResourceType().name())) { + shouldCreateArtifact = false; + return; + } + } else { + log.info("resource types for artifact placeholder {} were not defined. default is all resources", + k); + } + if (shouldCreateArtifact) { + if (serviceBusinessLogic.artifactsBusinessLogic != null) { + ArtifactDefinition artifactDefinition = serviceBusinessLogic.artifactsBusinessLogic.createArtifactPlaceHolderInfo( + resource.getUniqueId(), k, (Map<String, Object>) v, + user, ArtifactGroupTypeEnum.DEPLOYMENT); + if (artifactDefinition != null + && !artifactMap.containsKey(artifactDefinition.getArtifactLabel())) { + artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); + } + } + } + } + + public void mergeOldResourceMetadataWithNew(Resource oldResource, Resource newResource) { + + if (newResource.getTags() == null || newResource.getTags().isEmpty()) { + newResource.setTags(oldResource.getTags()); + } + + if (newResource.getDescription() == null) { + newResource.setDescription(oldResource.getDescription()); + } + + + if (newResource.getContactId() == null) { + newResource.setContactId(oldResource.getContactId()); + } + + newResource.setCategories(oldResource.getCategories()); + + } + + protected Resource buildComplexVfcMetadata(CsarInfo csarInfo, String nodeName, + Map<String, NodeTypeInfo> nodesInfo) { + Resource cvfc = new Resource(); + NodeTypeInfo nodeTypeInfo = nodesInfo.get(nodeName); + cvfc.setName(buildCvfcName(csarInfo.getVfResourceName(), nodeName)); + cvfc.setNormalizedName(ValidationUtils.normaliseComponentName(cvfc.getName())); + cvfc.setSystemName(ValidationUtils.convertToSystemName(cvfc.getName())); + cvfc.setResourceType(ResourceTypeEnum.VF); + cvfc.setAbstract(true); + cvfc.setDerivedFrom(nodeTypeInfo.getDerivedFrom()); + cvfc.setDescription(ImportUtils.Constants.VF_DESCRIPTION); + cvfc.setIcon(ImportUtils.Constants.DEFAULT_ICON); + cvfc.setContactId(csarInfo.getModifier().getUserId()); + cvfc.setCreatorUserId(csarInfo.getModifier().getUserId()); + cvfc.setVendorName("cmri"); + cvfc.setVendorRelease("1.0"); + cvfc.setResourceVendorModelNumber(""); + cvfc.setToscaResourceName( + buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(), nodeName) + .getLeft()); + cvfc.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID()); + + List<String> tags = new ArrayList<>(); + tags.add(cvfc.getName()); + cvfc.setTags(tags); + + CategoryDefinition category = new CategoryDefinition(); + category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME); + SubCategoryDefinition subCategory = new SubCategoryDefinition(); + subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY); + category.addSubCategory(subCategory); + List<CategoryDefinition> categories = new ArrayList<>(); + categories.add(category); + cvfc.setCategories(categories); + + cvfc.setVersion(ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION); + cvfc.setLifecycleState(ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT); + cvfc.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION); + + return cvfc; + } + + public Boolean validateResourceCreationFromNodeType(Resource resource, User creator) { + validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE); + return true; + } + + private void validateDerivedFromNotEmpty(User user, Resource resource, AuditingActionEnum actionEnum) { + log.debug("validate resource derivedFrom field"); + if ((resource.getDerivedFrom() == null) || (resource.getDerivedFrom().isEmpty()) + || (resource.getDerivedFrom().get(0)) == null || (resource.getDerivedFrom().get(0).trim().isEmpty())) { + log.info("derived from (template) field is missing for the resource"); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(ActionStatus.MISSING_DERIVED_FROM_TEMPLATE); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + + throw new ComponentException(ActionStatus.MISSING_DERIVED_FROM_TEMPLATE); + } + } + + public Service createInputsOnService(Service service, Map<String, InputDefinition> inputs) { + List<InputDefinition> resourceProperties = service.getInputs(); + if (MapUtils.isNotEmpty(inputs)|| isNotEmpty(resourceProperties)) { + + Either<List<InputDefinition>, ResponseFormat> createInputs = inputsBusinessLogic.createInputsInGraph(inputs, + service); + if (createInputs.isRight()) { + throw new ComponentException(createInputs.right().value()); + } + } else { + return service; + } + Either<Service, StorageOperationStatus> updatedResource = toscaOperationFacade + .getToscaElement(service.getUniqueId()); + if (updatedResource.isRight()) { + throw new ComponentException(componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(updatedResource.right().value()), service, ComponentTypeEnum.SERVICE)); + } + return updatedResource.left().value(); + } + + + public Service createServiceTransaction(Service service, User user, boolean isNormative) { + // validate resource name uniqueness + log.debug("validate resource name"); + Either<Boolean, StorageOperationStatus> eitherValidation = toscaOperationFacade.validateComponentNameExists( + service.getName(), null, service.getComponentType()); + if (eitherValidation.isRight()) { + log.debug("Failed to validate component name {}. Status is {}. ", service.getName(), + eitherValidation.right().value()); + ResponseFormat errorResponse = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(eitherValidation.right().value())); + throw new ComponentException(errorResponse); + } + if (eitherValidation.left().value()) { + log.debug("resource with name: {}, already exists", service.getName()); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, + ComponentTypeEnum.RESOURCE.getValue(), service.getName()); + throw new ComponentException(errorResponse); + } + + log.debug("send resource {} to dao for create", service.getName()); + + createArtifactsPlaceHolderData(service, user); + // enrich object + if (!isNormative) { + log.debug("enrich resource with creator, version and state"); + service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + service.setVersion(INITIAL_VERSION); + service.setHighestVersion(true); + } + return toscaOperationFacade.createToscaComponent(service) + .left() + .on(r->throwComponentExceptionByResource(r, service)); + } + + public Service throwComponentExceptionByResource(StorageOperationStatus status, Service service) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent( + componentsUtils.convertFromStorageResponse(status), service, ComponentTypeEnum.SERVICE); + throw new ComponentException(responseFormat); + } + + + protected void createArtifactsPlaceHolderData(Service service, User user) { + setInformationalArtifactsPlaceHolder(service, user); + serviceBusinessLogic.setDeploymentArtifactsPlaceHolder(service, user); + serviceBusinessLogic.setToscaArtifactsPlaceHolders(service, user); + } + + @SuppressWarnings("unchecked") + protected void setInformationalArtifactsPlaceHolder(Service service, User user) { + Map<String, ArtifactDefinition> artifactMap = service.getArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + String resourceUniqueId = service.getUniqueId(); + List<String> exludeResourceCategory = ConfigurationManager.getConfigurationManager().getConfiguration() + .getExcludeResourceCategory(); + List<String> exludeResourceType = ConfigurationManager.getConfigurationManager().getConfiguration() + .getExcludeResourceType(); + Map<String, Object> informationalResourceArtifacts = ConfigurationManager.getConfigurationManager() + .getConfiguration().getInformationalResourceArtifacts(); + List<CategoryDefinition> categories = service.getCategories(); + boolean isCreateArtifact = true; + if (exludeResourceCategory != null) { + String category = categories.get(0).getName(); + isCreateArtifact = exludeResourceCategory.stream().noneMatch(e->e.equalsIgnoreCase(category)); + } + + if (informationalResourceArtifacts != null && isCreateArtifact) { + Set<String> keys = informationalResourceArtifacts.keySet(); + for (String informationalResourceArtifactName : keys) { + Map<String, Object> artifactInfoMap = (Map<String, Object>) informationalResourceArtifacts + .get(informationalResourceArtifactName); + ArtifactDefinition artifactDefinition = serviceBusinessLogic.artifactsBusinessLogic.createArtifactPlaceHolderInfo( + resourceUniqueId, informationalResourceArtifactName, artifactInfoMap, user, + ArtifactGroupTypeEnum.INFORMATIONAL); + artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); + + } + } + service.setArtifacts(artifactMap); + } + + public void rollback(boolean inTransaction, Service service, List<ArtifactDefinition> createdArtifacts, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts) { + if(!inTransaction) { + serviceBusinessLogic.janusGraphDao.rollback(); + } + if (isNotEmpty(createdArtifacts) && isNotEmpty(nodeTypesNewCreatedArtifacts)) { + createdArtifacts.addAll(nodeTypesNewCreatedArtifacts); + log.debug("Found {} newly created artifacts to deleted, the component name: {}",createdArtifacts.size(), service.getName()); + } + } + + public Map<String,Object> getNodeTypesFromTemplate(Map<String, Object> mappedToscaTemplate) { + return ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES) + .left().orValue(HashMap::new); + } + + private Resource nodeForceCertification(Resource resource, User user, + LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, boolean needLock) { + return lifecycleBusinessLogic.forceResourceCertification(resource, user, lifecycleChangeInfo, inTransaction, + needLock); + } + + private Resource nodeFullCertification(String uniqueId, User user, + LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, boolean needLock) { + Either<Resource, ResponseFormat> resourceResponse = lifecycleBusinessLogic.changeState(uniqueId, user, LifeCycleTransitionEnum.CERTIFY, lifecycleChangeInfo, + inTransaction, needLock); + if(resourceResponse.isRight()){ + throw new ByResponseFormatComponentException(resourceResponse.right().value()); + } + return resourceResponse.left().value(); + } + + public Either<Boolean, ResponseFormat> validateNestedDerivedFromDuringUpdate(Resource currentResource, + Resource updateInfoResource, boolean hasBeenCertified) { + + List<String> currentDerivedFrom = currentResource.getDerivedFrom(); + List<String> updatedDerivedFrom = updateInfoResource.getDerivedFrom(); + if (currentDerivedFrom == null || currentDerivedFrom.isEmpty() || updatedDerivedFrom == null + || updatedDerivedFrom.isEmpty()) { + log.trace("Update normative types"); + return Either.left(true); + } + + String derivedFromCurrent = currentDerivedFrom.get(0); + String derivedFromUpdated = updatedDerivedFrom.get(0); + + if (!derivedFromCurrent.equals(derivedFromUpdated)) { + if (!hasBeenCertified) { + validateDerivedFromExist(null, updateInfoResource, null); + } else { + Either<Boolean, ResponseFormat> validateDerivedFromExtending = validateDerivedFromExtending(null, + currentResource, updateInfoResource, null); + + if (validateDerivedFromExtending.isRight() || !validateDerivedFromExtending.left().value()) { + log.debug("Derived from cannot be updated if it doesnt inherits directly or extends inheritance"); + return validateDerivedFromExtending; + } + } + } + return Either.left(true); + } + + protected Either<Boolean, ResponseFormat> validateDerivedFromExtending(User user, Resource currentResource, + Resource updateInfoResource, AuditingActionEnum actionEnum) { + String currentTemplateName = currentResource.getDerivedFrom().get(0); + String updatedTemplateName = updateInfoResource.getDerivedFrom().get(0); + + Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade + .validateToscaResourceNameExtends(currentTemplateName, updatedTemplateName); + if (dataModelResponse.isRight()) { + StorageOperationStatus storageStatus = dataModelResponse.right().value(); + BeEcompErrorManager.getInstance() + .logBeDaoSystemError("Create/Update Resource - validateDerivingFromExtendingType"); + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource( + componentsUtils.convertFromStorageResponse(storageStatus), currentResource); + log.trace("audit before sending response"); + componentsUtils.auditResource(responseFormat, user, currentResource, actionEnum); + return Either.right(responseFormat); + } + + if (!dataModelResponse.left().value()) { + log.info("resource template with name {} does not inherit as original {}", updatedTemplateName, + currentTemplateName); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(ActionStatus.PARENT_RESOURCE_DOES_NOT_EXTEND); + componentsUtils.auditResource(responseFormat, user, currentResource, actionEnum); + + return Either.right(responseFormat); + + } + return Either.left(true); + } + + public void validateResourceFieldsBeforeUpdate(Resource currentResource, Resource updateInfoResource, + boolean inTransaction, boolean isNested) { + validateFields(currentResource, updateInfoResource, inTransaction, isNested); + } + + + private void validateFields(Resource currentResource, Resource updateInfoResource, boolean inTransaction, boolean isNested) { + boolean hasBeenCertified = ValidationUtils.hasBeenCertified(currentResource.getVersion()); + log.debug("validate resource name before update"); + validateResourceName(currentResource, updateInfoResource, hasBeenCertified, isNested); + log.debug("validate description before update"); + serviceBusinessLogic.componentDescriptionValidator.validateAndCorrectField(null, updateInfoResource, null); + log.debug("validate icon before update"); + log.debug("validate tags before update"); + serviceBusinessLogic.componentTagsValidator.validateAndCorrectField(null, updateInfoResource, null); + log.debug("validate vendor name before update"); + log.debug("validate resource vendor model number before update"); + log.debug("validate vendor release before update"); + log.debug("validate contact info before update"); + serviceBusinessLogic.componentContactIdValidator.validateAndCorrectField(null, updateInfoResource, null); + log.debug(VALIDATE_DERIVED_BEFORE_UPDATE); + log.debug("validate category before update"); + } + + + protected void validateResourceName(Resource currentResource, Resource updateInfoResource, + boolean hasBeenCertified, boolean isNested) { + String resourceNameUpdated = updateInfoResource.getName(); + if (!isResourceNameEquals(currentResource, updateInfoResource)) { + if (isNested || !hasBeenCertified) { + serviceBusinessLogic.componentNameValidator.validateAndCorrectField(null, updateInfoResource, null); + currentResource.setName(resourceNameUpdated); + currentResource.setNormalizedName(ValidationUtils.normaliseComponentName(resourceNameUpdated)); + currentResource.setSystemName(ValidationUtils.convertToSystemName(resourceNameUpdated)); + + } else { + log.info("Resource name: {}, cannot be updated once the resource has been certified once.", + resourceNameUpdated); + throw new ComponentException(ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED); + } + } + } + + protected boolean isResourceNameEquals(Resource currentResource, Resource updateInfoResource) { + String resourceNameUpdated = updateInfoResource.getName(); + String resourceNameCurrent = currentResource.getName(); + if (resourceNameCurrent.equals(resourceNameUpdated)) { + return true; + } + return currentResource.getResourceType().equals(ResourceTypeEnum.VF) && + resourceNameUpdated.equals(addCvfcSuffixToResourceName(resourceNameCurrent)); + } + + public Resource prepareResourceForUpdate(Resource oldResource, Resource newResource, User user, + boolean inTransaction, boolean needLock) { + + if (!ComponentValidationUtils.canWorkOnResource(oldResource, user.getUserId())) { + // checkout + return lifecycleBusinessLogic.changeState( + oldResource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, + new LifecycleChangeInfoWithAction("update by import"), inTransaction, needLock) + .left() + .on(response -> failOnChangeState(response, user, oldResource, newResource)); + } + return oldResource; + } + + protected Resource failOnChangeState(ResponseFormat response, User user, Resource oldResource, Resource newResource) { + log.info("resource {} cannot be updated. reason={}", oldResource.getUniqueId(), + response.getFormattedMessage()); + componentsUtils.auditResource(response, user, newResource, AuditingActionEnum.IMPORT_RESOURCE, + ResourceVersionInfo.newBuilder() + .state(oldResource.getLifecycleState().name()) + .version(oldResource.getVersion()) + .build()); + throw new ComponentException(response); + } + + public Resource handleResourceGenericType(Resource resource) { + Resource genericResource = serviceBusinessLogic.fetchAndSetDerivedFromGenericType(resource); + if (resource.shouldGenerateInputs()) { + serviceBusinessLogic.generateAndAddInputsFromGenericTypeProperties(resource, genericResource); + } + return genericResource; + } + + public Resource createInputsOnResource(Resource resource, Map<String, InputDefinition> inputs) { + List<InputDefinition> resourceProperties = resource.getInputs(); + if (MapUtils.isNotEmpty(inputs)|| isNotEmpty(resourceProperties)) { + + Either<List<InputDefinition>, ResponseFormat> createInputs = inputsBusinessLogic.createInputsInGraph(inputs, + resource); + if (createInputs.isRight()) { + throw new ComponentException(createInputs.right().value()); + } + } else { + return resource; + } + Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade + .getToscaElement(resource.getUniqueId()); + if (updatedResource.isRight()) { + throw new ComponentException(componentsUtils.getResponseFormatByResource( + componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resource)); + } + return updatedResource.left().value(); + } + + protected void updateOrCreateGroups(Resource resource, Map<String, GroupDefinition> groups) { + List<GroupDefinition> groupsFromResource = resource.getGroups(); + List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, new Service()); + List<GroupDefinition> groupsToUpdate = new ArrayList<>(); + List<GroupDefinition> groupsToDelete = new ArrayList<>(); + List<GroupDefinition> groupsToCreate = new ArrayList<>(); + if (isNotEmpty(groupsFromResource)) { + addGroupsToCreateOrUpdate(groupsFromResource, groupsAsList, groupsToUpdate, groupsToCreate); + addGroupsToDelete(groupsFromResource, groupsAsList, groupsToDelete); + } else { + groupsToCreate.addAll(groupsAsList); + } + if (isNotEmpty(groupsToCreate)) { + fillGroupsFinalFields(groupsToCreate); + if (isNotEmpty(groupsFromResource)) { + serviceBusinessLogic.groupBusinessLogic.addGroups(resource, + groupsToCreate, true) + .left() + .on(serviceBusinessLogic::throwComponentException); + } else { + serviceBusinessLogic.groupBusinessLogic.createGroups(resource, + groupsToCreate, true) + .left() + .on(serviceBusinessLogic::throwComponentException); + } + } + if (isNotEmpty(groupsToDelete)) { + serviceBusinessLogic.groupBusinessLogic.deleteGroups(resource, groupsToDelete) + .left() + .on(serviceBusinessLogic::throwComponentException); + } + if (isNotEmpty(groupsToUpdate)) { + serviceBusinessLogic.groupBusinessLogic.updateGroups(resource, groupsToUpdate, true) + .left() + .on(serviceBusinessLogic::throwComponentException); + } + } + + protected void addGroupsToCreateOrUpdate(List<GroupDefinition> groupsFromResource, List<GroupDefinition> groupsAsList, List<GroupDefinition> groupsToUpdate, List<GroupDefinition> groupsToCreate) { + for (GroupDefinition group : groupsAsList) { + Optional<GroupDefinition> op = groupsFromResource.stream() + .filter(p -> p.getInvariantName().equalsIgnoreCase(group.getInvariantName())).findAny(); + if (op.isPresent()) { + GroupDefinition groupToUpdate = op.get(); + groupToUpdate.setMembers(group.getMembers()); + groupToUpdate.setCapabilities(group.getCapabilities()); + groupToUpdate.setProperties(group.getProperties()); + groupsToUpdate.add(groupToUpdate); + } else { + groupsToCreate.add(group); + } + } + } + + protected void addGroupsToDelete(List<GroupDefinition> groupsFromResource, List<GroupDefinition> groupsAsList, List<GroupDefinition> groupsToDelete) { + for (GroupDefinition group : groupsFromResource) { + Optional<GroupDefinition> op = groupsAsList.stream() + .filter(p -> p.getName().equalsIgnoreCase(group.getName())).findAny(); + if (!op.isPresent() && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) { + groupsToDelete.add(group); + } + } + } + + protected List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Service component) { + + List<GroupDefinition> result = new ArrayList<>(); + List<ComponentInstance> componentInstances = component.getComponentInstances(); + + if (groups != null) { + Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies = validateCyclicGroupsDependencies(groups); + if (validateCyclicGroupsDependencies.isRight()) { + throw new ComponentException(validateCyclicGroupsDependencies.right().value()); + } + for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) { + String groupName = entry.getKey(); + GroupDefinition groupDefinition = entry.getValue(); + GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition); + updatedGroupDefinition.setMembers(null); + Map<String, String> members = groupDefinition.getMembers(); + if (members != null) { + updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members); + } + result.add(updatedGroupDefinition); + } + } + return result; + } + + public void updateGroupMembers(Map<String, GroupDefinition> groups, GroupDefinition updatedGroupDefinition, Service component, List<ComponentInstance> componentInstances, String groupName, Map<String, String> members) { + Set<String> compInstancesNames = members.keySet(); + + if (CollectionUtils.isEmpty(componentInstances)) { + String membersAstString = compInstancesNames.stream().collect(joining(",")); + log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", + membersAstString, groupName, component.getNormalizedName()); + throw new ComponentException(componentsUtils.getResponseFormat( + ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, + component.getNormalizedName(), getComponentTypeForResponse(component))); + } + // Find all component instances with the member names + Map<String, String> memberNames = componentInstances.stream() + .collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId)); + memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> ""))); + Map<String, String> relevantInstances = memberNames.entrySet().stream() + .filter(n -> compInstancesNames.contains(n.getKey())) + .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); + + if (relevantInstances == null || relevantInstances.size() != compInstancesNames.size()) { + + List<String> foundMembers = new ArrayList<>(); + if (relevantInstances != null) { + foundMembers = relevantInstances.keySet().stream().collect(toList()); + } + compInstancesNames.removeAll(foundMembers); + String membersAstString = compInstancesNames.stream().collect(joining(",")); + log.debug("The members: {}, in group: {}, cannot be found in component: {}", membersAstString, + groupName, component.getNormalizedName()); + throw new ComponentException(componentsUtils.getResponseFormat( + ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, + component.getNormalizedName(), getComponentTypeForResponse(component))); + } + updatedGroupDefinition.setMembers(relevantInstances); + } + + public Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies(Map<String, GroupDefinition> allGroups) { + + Either<Boolean, ResponseFormat> result = Either.left(true); + try { + Iterator<Map.Entry<String, GroupDefinition>> allGroupsItr = allGroups.entrySet().iterator(); + while (allGroupsItr.hasNext() && result.isLeft()) { + Map.Entry<String, GroupDefinition> groupAEntry = allGroupsItr.next(); + // Fetches a group member A + String groupAName = groupAEntry.getKey(); + // Finds all group members in group A + Set<String> allGroupAMembersNames = new HashSet<>(); + fillAllGroupMemebersRecursivly(groupAEntry.getKey(), allGroups, allGroupAMembersNames); + // If A is a group member of itself found cyclic dependency + if (allGroupAMembersNames.contains(groupAName)) { + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(ActionStatus.GROUP_HAS_CYCLIC_DEPENDENCY, groupAName); + result = Either.right(responseFormat); + } + } + } catch (Exception e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + result = Either.right(responseFormat); + log.debug("Exception occured when validateCyclicGroupsDependencies, error is:{}", e.getMessage(), e); + } + return result; + } + + protected void fillAllGroupMemebersRecursivly(String groupName, Map<String, GroupDefinition> allGroups, + Set<String> allGroupMembers) { + + // Found Cyclic dependency + if (isfillGroupMemebersRecursivlyStopCondition(groupName, allGroups, allGroupMembers)) { + return; + } + GroupDefinition groupDefinition = allGroups.get(groupName); + // All Members Of Current Group Resource Instances & Other Groups + Set<String> currGroupMembers = groupDefinition.getMembers().keySet(); + // Filtered Members Of Current Group containing only members which + // are groups + List<String> currGroupFilteredMembers = currGroupMembers.stream(). + // Keep Only Elements of type group and not Resource Instances + filter(allGroups::containsKey). + // Add Filtered Elements to main Set + peek(allGroupMembers::add). + // Collect results + collect(toList()); + + // Recursively call the method for all the filtered group members + for (String innerGroupName : currGroupFilteredMembers) { + fillAllGroupMemebersRecursivly(innerGroupName, allGroups, allGroupMembers); + } + } + + public ImmutablePair<Resource, ActionStatus> createResourceFromNodeType(String nodeTypeYaml, UploadResourceInfo resourceMetaData, User creator, boolean isInTransaction, boolean needLock, + Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle, + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, boolean forceCertificationAllowed, CsarInfo csarInfo, + String nodeName, boolean isNested) { + + LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT, + LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR); + + Function<Resource, Boolean> validator = resource -> validateResourceCreationFromNodeType( + resource, creator); + + return resourceImportManager.importCertifiedResource(nodeTypeYaml, resourceMetaData, creator, validator, + lifecycleChangeInfo, isInTransaction, true, needLock, nodeTypeArtifactsToHandle, + nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeName, isNested); + } + + public ImmutablePair<Resource, ActionStatus> createNodeTypeResourceFromYaml( + String yamlName, Map.Entry<String, Object> nodeNameValue, User user, Map<String, Object> mapToConvert, + Service resourceVf, boolean needLock, + Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle, + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, boolean forceCertificationAllowed, CsarInfo csarInfo, + boolean isNested) { + + UploadResourceInfo resourceMetaData = fillResourceMetadata(yamlName, resourceVf, nodeNameValue.getKey(), user); + + String singleVfcYaml = buildNodeTypeYaml(nodeNameValue, mapToConvert, + resourceMetaData.getResourceType(), csarInfo); + user = serviceBusinessLogic.validateUser(user, "CheckIn Resource", resourceVf, AuditingActionEnum.CHECKIN_RESOURCE, true); + return createResourceFromNodeType(singleVfcYaml, resourceMetaData, user, true, needLock, + nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, + nodeNameValue.getKey(), isNested); + } + + protected UploadResourceInfo fillResourceMetadata(String yamlName, Service resourceVf, + String nodeName, User user) { + UploadResourceInfo resourceMetaData = new UploadResourceInfo(); + + // validate nodetype name prefix + if (!nodeName.startsWith(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX)) { + log.debug("invalid nodeName:{} does not start with {}.", nodeName, + Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX); + throw new ComponentException(ActionStatus.INVALID_NODE_TEMPLATE, + yamlName, resourceMetaData.getName(), nodeName); + } + + String actualName = this.getNodeTypeActualName(nodeName); + String namePrefix = nodeName.replace(actualName, ""); + String resourceType = namePrefix.substring(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX.length()); + + // if we import from csar, the node_type name can be + // org.openecomp.resource.abstract.node_name - in this case we always + // create a vfc + if (resourceType.equals(Constants.ABSTRACT)) { + resourceType = ResourceTypeEnum.VFC.name().toLowerCase(); + } + // validating type + if (!ResourceTypeEnum.containsName(resourceType.toUpperCase())) { + log.debug("invalid resourceType:{} the type is not one of the valide types:{}.", resourceType.toUpperCase(), + ResourceTypeEnum.values()); + throw new ComponentException(ActionStatus.INVALID_NODE_TEMPLATE, + yamlName, resourceMetaData.getName(), nodeName); + } + + // Setting name + resourceMetaData.setName(resourceVf.getSystemName() + actualName); + + // Setting type from name + String type = resourceType.toUpperCase(); + resourceMetaData.setResourceType(type); + + resourceMetaData.setDescription(ImportUtils.Constants.INNER_VFC_DESCRIPTION); + resourceMetaData.setIcon(ImportUtils.Constants.DEFAULT_ICON); + resourceMetaData.setContactId(user.getUserId()); + + // Setting tag + List<String> tags = new ArrayList<>(); + tags.add(resourceMetaData.getName()); + resourceMetaData.setTags(tags); + + // Setting category + CategoryDefinition category = new CategoryDefinition(); + category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME); + SubCategoryDefinition subCategory = new SubCategoryDefinition(); + subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY); + category.addSubCategory(subCategory); + List<CategoryDefinition> categories = new ArrayList<>(); + categories.add(category); + resourceMetaData.setCategories(categories); + + return resourceMetaData; + } + + + public Resource propagateStateToCertified(User user, Resource resource, + LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, boolean needLock, + boolean forceCertificationAllowed) { + + Either<Resource, ResponseFormat> result = null; + try { + if (resource.getLifecycleState() != LifecycleStateEnum.CERTIFIED && forceCertificationAllowed + && lifecycleBusinessLogic.isFirstCertification(resource.getVersion())) { + nodeForceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock); + } + if (resource.getLifecycleState() == LifecycleStateEnum.CERTIFIED) { + Either<ArtifactDefinition, Operation> eitherPopulated = serviceBusinessLogic.populateToscaArtifacts( + resource, user, false, inTransaction, needLock); + return resource; + } + return nodeFullCertification(resource.getUniqueId(), user, lifecycleChangeInfo, inTransaction, needLock); + } catch (Exception e) { + log.debug("The exception has occurred upon certification of resource {}. ", resource.getName(), e); + throw e; + } finally { + if (result == null || result.isRight()) { + BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify"); + if (!inTransaction) { + serviceBusinessLogic.janusGraphDao.rollback(); + } + } else if (!inTransaction) { + serviceBusinessLogic.janusGraphDao.commit(); + } + } + } + + public Resource buildValidComplexVfc(CsarInfo csarInfo, String nodeName, + Map<String, NodeTypeInfo> nodesInfo) { + + Resource complexVfc = buildComplexVfcMetadata(csarInfo, nodeName, nodesInfo); + log.debug("************* Going to validate complex VFC from yaml {}", complexVfc.getName()); + csarInfo.addNodeToQueue(nodeName); + return validateResourceBeforeCreate(complexVfc, csarInfo.getModifier(), + AuditingActionEnum.IMPORT_RESOURCE, true, csarInfo); + } + + public Resource updateGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) { + if (MapUtils.isEmpty(groups)) { + return resource; + } else { + updateOrCreateGroups(resource, groups); + } + Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade + .getToscaElement(resource.getUniqueId()); + if (updatedResource.isRight()) { + throw new ComponentException(componentsUtils.getResponseFormatByResource( + componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resource)); + } + return updatedResource.left().value(); + } + + protected void setInformationalArtifactsPlaceHolder(Resource resource, User user) { + Map<String, ArtifactDefinition> artifactMap = resource.getArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + String resourceUniqueId = resource.getUniqueId(); + List<String> exludeResourceCategory = ConfigurationManager.getConfigurationManager().getConfiguration() + .getExcludeResourceCategory(); + List<String> exludeResourceType = ConfigurationManager.getConfigurationManager().getConfiguration() + .getExcludeResourceType(); + Map<String, Object> informationalResourceArtifacts = ConfigurationManager.getConfigurationManager() + .getConfiguration().getInformationalResourceArtifacts(); + List<CategoryDefinition> categories = resource.getCategories(); + boolean isCreateArtifact = true; + if (exludeResourceCategory != null) { + String category = categories.get(0).getName(); + isCreateArtifact = exludeResourceCategory.stream().noneMatch(e->e.equalsIgnoreCase(category)); + } + if (isCreateArtifact && exludeResourceType != null) { + String resourceType = resource.getResourceType().name(); + isCreateArtifact = exludeResourceType.stream().noneMatch(e->e.equalsIgnoreCase(resourceType)); + } + if (informationalResourceArtifacts != null && isCreateArtifact) { + Set<String> keys = informationalResourceArtifacts.keySet(); + for (String informationalResourceArtifactName : keys) { + Map<String, Object> artifactInfoMap = (Map<String, Object>) informationalResourceArtifacts + .get(informationalResourceArtifactName); + ArtifactDefinition artifactDefinition = serviceBusinessLogic.artifactsBusinessLogic.createArtifactPlaceHolderInfo( + resourceUniqueId, informationalResourceArtifactName, artifactInfoMap, user, + ArtifactGroupTypeEnum.INFORMATIONAL); + artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); + + } + } + resource.setArtifacts(artifactMap); + } + + public void rollback(boolean inTransaction, Resource resource, List<ArtifactDefinition> createdArtifacts, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts) { + if(!inTransaction) { + serviceBusinessLogic.janusGraphDao.rollback(); + } + if (isNotEmpty(createdArtifacts) && isNotEmpty(nodeTypesNewCreatedArtifacts)) { + createdArtifacts.addAll(nodeTypesNewCreatedArtifacts); + log.debug("Found {} newly created artifacts to deleted, the component name: {}",createdArtifacts.size(), resource.getName()); + } + } + + public void createArtifactsPlaceHolderData(Resource resource, User user) { + setInformationalArtifactsPlaceHolder(resource, user); + setDeploymentArtifactsPlaceHolder(resource, user); + serviceBusinessLogic.setToscaArtifactsPlaceHolders(resource, user); + } + + public void handleGroupsProperties(Service service, Map<String, GroupDefinition> groups) { + List<InputDefinition> inputs = service.getInputs(); + if (MapUtils.isNotEmpty(groups)) { + groups.values() + .stream() + .filter(g -> isNotEmpty(g.getProperties())) + .flatMap(g -> g.getProperties().stream()) + .forEach(p -> handleGetInputs(p, inputs)); + } + } + + public void handleGroupsProperties(Resource resource, Map<String, GroupDefinition> groups) { + List<InputDefinition> inputs = resource.getInputs(); + if (MapUtils.isNotEmpty(groups)) { + groups.values() + .stream() + .filter(g -> isNotEmpty(g.getProperties())) + .flatMap(g -> g.getProperties().stream()) + .forEach(p -> handleGetInputs(p, inputs)); + } + } + + protected void handleGetInputs(PropertyDataDefinition property, List<InputDefinition> inputs) { + if (isNotEmpty(property.getGetInputValues())) { + if (inputs == null || inputs.isEmpty()) { + log.debug("Failed to add property {} to group. Inputs list is empty ", property); + serviceBusinessLogic.rollbackWithException(ActionStatus.INPUTS_NOT_FOUND, property.getGetInputValues() + .stream() + .map(GetInputValueDataDefinition::getInputName) + .collect(toList()).toString()); + } + ListIterator<GetInputValueDataDefinition> getInputValuesIter = property.getGetInputValues().listIterator(); + while (getInputValuesIter.hasNext()) { + GetInputValueDataDefinition getInput = getInputValuesIter.next(); + InputDefinition input = findInputByName(inputs, getInput); + getInput.setInputId(input.getUniqueId()); + if (getInput.getGetInputIndex() != null) { + GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex(); + input = findInputByName(inputs, getInputIndex); + getInputIndex.setInputId(input.getUniqueId()); + getInputValuesIter.add(getInputIndex); + } + } + } + } + + public InputDefinition findInputByName(List<InputDefinition> inputs, GetInputValueDataDefinition getInput) { + Optional<InputDefinition> inputOpt = inputs.stream() + .filter(p -> p.getName().equals(getInput.getInputName())) + .findFirst(); + if (!inputOpt.isPresent()) { + log.debug("#findInputByName - Failed to find the input {} ", getInput.getInputName()); + serviceBusinessLogic.rollbackWithException(ActionStatus.INPUTS_NOT_FOUND, getInput.getInputName()); + } + return inputOpt.get(); + } + + public void associateComponentInstancePropertiesToComponent(String yamlName, Resource resource, Map<String, List<ComponentInstanceProperty>> instProperties) { + Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addPropToInst = toscaOperationFacade + .associateComponentInstancePropertiesToComponent(instProperties, resource.getUniqueId()); + if (addPropToInst.isRight()) { + log.debug("failed to associate properties of resource {} status is {}", resource.getUniqueId(), + addPropToInst.right().value()); + throw new ComponentException(componentsUtils.getResponseFormat( + componentsUtils.convertFromStorageResponse(addPropToInst.right().value()), yamlName)); + } + } + + public void associateComponentInstanceInputsToComponent(String yamlName, Resource resource, Map<String, List<ComponentInstanceInput>> instInputs) { + if (MapUtils.isNotEmpty(instInputs)) { + Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addInputToInst = toscaOperationFacade + .associateComponentInstanceInputsToComponent(instInputs, resource.getUniqueId()); + if (addInputToInst.isRight()) { + log.debug("failed to associate inputs value of resource {} status is {}", resource.getUniqueId(), + addInputToInst.right().value()); + throw new ComponentException(componentsUtils.getResponseFormat( + componentsUtils.convertFromStorageResponse(addInputToInst.right().value()), yamlName)); + } + } + } + + public void associateDeploymentArtifactsToInstances(User user, String yamlName, Resource resource, Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts) { + StorageOperationStatus addArtToInst = toscaOperationFacade + .associateDeploymentArtifactsToInstances(instDeploymentArtifacts, resource, user); + if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName)); + } + } + + public void associateArtifactsToInstances(String yamlName, Resource resource, Map<String, Map<String, ArtifactDefinition>> instArtifacts) { + + StorageOperationStatus addArtToInst; + + addArtToInst = toscaOperationFacade.associateArtifactsToInstances(instArtifacts, resource); + if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName)); + } + } + + public void associateOrAddCalculatedCapReq(String yamlName, Resource resource, Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements) { + + StorageOperationStatus addArtToInst; + addArtToInst = toscaOperationFacade.associateOrAddCalculatedCapReq(instCapabilities, instRequirements, + resource); + if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate cap and req of resource {} status is {}", resource.getUniqueId(), + addArtToInst); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName)); + } + } + + public void associateInstAttributeToComponentToInstances(String yamlName, Resource resource, Map<String, List<AttributeDataDefinition>> instAttributes) { + + StorageOperationStatus addArtToInst; + addArtToInst = toscaOperationFacade.associateInstAttributeToComponentToInstances(instAttributes, + resource); + if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate attributes of resource {} status is {}", resource.getUniqueId(), + addArtToInst); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName)); + } + } + + public Resource getResourceAfterCreateRelations(Resource resource) { + ComponentParametersView parametersView = getComponentFilterAfterCreateRelations(); + Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade + .getToscaElement(resource.getUniqueId(), parametersView); + + if (eitherGetResource.isRight()) { + throwComponentExceptionByResource(eitherGetResource.right().value(),resource); + } + return eitherGetResource.left().value(); + } + + public Resource throwComponentExceptionByResource(StorageOperationStatus status, Resource resource) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource( + componentsUtils.convertFromStorageResponse(status), resource); + throw new ComponentException(responseFormat); + } + + public void setCapabilityNamesTypes(Map<String, List<CapabilityDefinition>> originCapabilities, + Map<String, List<UploadCapInfo>> uploadedCapabilities) { + for (Map.Entry<String, List<UploadCapInfo>> currEntry : uploadedCapabilities.entrySet()) { + if (originCapabilities.containsKey(currEntry.getKey())) { + currEntry.getValue().stream().forEach(cap -> cap.setType(currEntry.getKey())); + } + } + for (Map.Entry<String, List<CapabilityDefinition>> 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()); + }); + } + }); + } + } + + public Map<String, List<CapabilityDefinition>> getValidComponentInstanceCapabilities( + String resourceId, Map<String, List<CapabilityDefinition>> defaultCapabilities, + Map<String, List<UploadCapInfo>> uploadedCapabilities) { + + Map<String, List<CapabilityDefinition>> validCapabilitiesMap = new HashMap<>(); + uploadedCapabilities.forEach((k,v)->addValidComponentInstanceCapabilities(k,v,resourceId,defaultCapabilities,validCapabilitiesMap)); + return validCapabilitiesMap; + } + + public void associateComponentInstanceInputsToComponent(String yamlName, Service service, Map<String, List<ComponentInstanceInput>> instInputs) { + if (MapUtils.isNotEmpty(instInputs)) { + Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addInputToInst = toscaOperationFacade + .associateComponentInstanceInputsToComponent(instInputs, service.getUniqueId()); + if (addInputToInst.isRight()) { + log.debug("failed to associate inputs value of resource {} status is {}", service.getUniqueId(), + addInputToInst.right().value()); + throw new ComponentException(componentsUtils.getResponseFormat( + componentsUtils.convertFromStorageResponse(addInputToInst.right().value()), yamlName)); + } + } + } + + public void associateComponentInstancePropertiesToComponent(String yamlName, Service service, Map<String, List<ComponentInstanceProperty>> instProperties) { + Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addPropToInst = toscaOperationFacade + .associateComponentInstancePropertiesToComponent(instProperties, service.getUniqueId()); + if (addPropToInst.isRight()) { + throw new ComponentException(componentsUtils.getResponseFormat( + componentsUtils.convertFromStorageResponse(addPropToInst.right().value()), yamlName)); + } + } + + public void associateDeploymentArtifactsToInstances(User user, String yamlName, Service resource, Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts) { + StorageOperationStatus addArtToInst = toscaOperationFacade + .associateDeploymentArtifactsToInstances(instDeploymentArtifacts, resource, user); + if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName)); + } + } + + public void associateArtifactsToInstances(String yamlName, Service resource, Map<String, Map<String, ArtifactDefinition>> instArtifacts) { + StorageOperationStatus addArtToInst; + + addArtToInst = toscaOperationFacade.associateArtifactsToInstances(instArtifacts, resource); + if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName)); + } + } + + public void associateOrAddCalculatedCapReq(String yamlName, Service resource, Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements) { + StorageOperationStatus addArtToInst; + addArtToInst = toscaOperationFacade.associateOrAddCalculatedCapReq(instCapabilities, instRequirements, + resource); + log.debug("enter associateOrAddCalculatedCapReq,get instCapabilities:{},get instRequirements:{}", + instCapabilities, instRequirements); + if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate cap and req of resource {} status is {}", resource.getUniqueId(), + addArtToInst); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName)); + } + } + + public void associateInstAttributeToComponentToInstances(String yamlName, Service resource, Map<String, List<AttributeDataDefinition>> instAttributes) { + StorageOperationStatus addArtToInst; + + addArtToInst = toscaOperationFacade.associateInstAttributeToComponentToInstances(instAttributes, + resource); + if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate attributes of resource {} status is {}", resource.getUniqueId(), + addArtToInst); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName)); + } + } + + public void associateRequirementsToService(String yamlName, Service resource, Map<String, ListRequirementDataDefinition> requirements) { + StorageOperationStatus addReqToService; + addReqToService = toscaOperationFacade.associateRequirementsToService(requirements, + resource.getUniqueId()); + if (addReqToService != StorageOperationStatus.OK && addReqToService != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate attributes of resource {} status is {}", resource.getUniqueId(), + addReqToService); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addReqToService), yamlName)); + } + } + + public void associateCapabilitiesToService(String yamlName, Service resource, Map<String,ListCapabilityDataDefinition> capabilities) { + StorageOperationStatus addCapToService; + addCapToService = toscaOperationFacade.associateCapabilitiesToService(capabilities, + resource.getUniqueId()); + if (addCapToService != StorageOperationStatus.OK && addCapToService != StorageOperationStatus.NOT_FOUND) { + log.debug("failed to associate attributes of resource {} status is {}", resource.getUniqueId(), + addCapToService); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(addCapToService), yamlName)); + } + } + + public void associateResourceInstances(String yamlName, Service service, List<RequirementCapabilityRelDef> relations) { + Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> relationsEither = toscaOperationFacade.associateResourceInstances(service, service.getUniqueId(), relations); + + if (relationsEither.isRight() && relationsEither.right().value() != StorageOperationStatus.NOT_FOUND) { + StorageOperationStatus status = relationsEither.right().value(); + log.debug("failed to associate instances of service {} status is {}", service.getUniqueId(), + status); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(status), yamlName)); + } + } + + public void addCapabilities(Map<String, List<CapabilityDefinition>> originCapabilities, String type, List<CapabilityDefinition> capabilities) { + List<CapabilityDefinition> list = capabilities.stream().map(CapabilityDefinition::new) + .collect(toList()); + originCapabilities.put(type, list); + } + + public void addCapabilitiesProperties(Map<String, Map<String, UploadPropInfo>> newPropertiesMap, List<UploadCapInfo> capabilities) { + for (UploadCapInfo capability : capabilities) { + if (isNotEmpty(capability.getProperties())) { + newPropertiesMap.put(capability.getName(), capability.getProperties().stream() + .collect(toMap(UploadInfo::getName, p -> p))); + } + } + } + + public Service getServiceWithGroups(String resourceId) { + + ComponentParametersView filter = new ComponentParametersView(); + filter.setIgnoreGroups(false); + Either<Service, StorageOperationStatus> updatedResource = toscaOperationFacade.getToscaElement(resourceId, filter); + if (updatedResource.isRight()) { + serviceBusinessLogic.rollbackWithException(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resourceId); + } + return updatedResource.left().value(); + } + + public Resource getResourceWithGroups(String resourceId) { + + ComponentParametersView filter = new ComponentParametersView(); + filter.setIgnoreGroups(false); + Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade.getToscaElement(resourceId, filter); + if (updatedResource.isRight()) { + serviceBusinessLogic.rollbackWithException(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resourceId); + } + return updatedResource.left().value(); + } + + public void associateResourceInstances(String yamlName, Resource resource, List<RequirementCapabilityRelDef> relations) { + + Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> relationsEither = toscaOperationFacade.associateResourceInstances(resource, resource.getUniqueId(), relations); + if (relationsEither.isRight() && relationsEither.right().value() != StorageOperationStatus.NOT_FOUND) { + StorageOperationStatus status = relationsEither.right().value(); + log.debug("failed to associate instances of resource {} status is {}", resource.getUniqueId(), + status); + throw new ComponentException(componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(status), yamlName)); + } + } + + public void addRelationsToRI(String yamlName, Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap, List<ComponentInstance> componentInstancesList, List<RequirementCapabilityRelDef> relations) { + for (Map.Entry<String, UploadComponentInstanceInfo> entry : uploadResInstancesMap.entrySet()) { + UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue(); + ComponentInstance currentCompInstance = null; + for (ComponentInstance compInstance : componentInstancesList) { + + if (compInstance.getName().equals(uploadComponentInstanceInfo.getName())) { + currentCompInstance = compInstance; + break; + } + } + if (currentCompInstance == null) { + log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), + resource.getUniqueId()); + BeEcompErrorManager.getInstance().logInternalDataError( + COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, + resource.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + throw new ComponentException(responseFormat); + } + + ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, resource, entry.getValue(), relations); + if (addRelationToRiRes.getStatus() != 200) { + throw new ComponentException(addRelationToRiRes); + } + } + } + + protected ResponseFormat addRelationToRI(String yamlName, Resource resource, + UploadComponentInstanceInfo nodesInfoValue, List<RequirementCapabilityRelDef> relations) { + List<ComponentInstance> componentInstancesList = resource.getComponentInstances(); + ComponentInstance currentCompInstance = null; + + for (ComponentInstance compInstance : componentInstancesList) { + + if (compInstance.getName().equals(nodesInfoValue.getName())) { + currentCompInstance = compInstance; + break; + } + } + if (currentCompInstance == null) { + log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, nodesInfoValue.getName(), + resource.getUniqueId()); + BeEcompErrorManager.getInstance().logInternalDataError( + COMPONENT_INSTANCE_WITH_NAME + nodesInfoValue.getName() + IN_RESOURCE, + resource.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, + yamlName); + } + String resourceInstanceId = currentCompInstance.getUniqueId(); + Map<String, List<UploadReqInfo>> regMap = nodesInfoValue.getRequirements(); + + if (regMap != null) { + Iterator<Map.Entry<String, List<UploadReqInfo>>> nodesRegValue = regMap.entrySet().iterator(); + while (nodesRegValue.hasNext()) { + Map.Entry<String, List<UploadReqInfo>> nodesRegInfoEntry = nodesRegValue.next(); + List<UploadReqInfo> uploadRegInfoList = nodesRegInfoEntry.getValue(); + for (UploadReqInfo uploadRegInfo : uploadRegInfoList) { + log.debug("Going to create relation {}", uploadRegInfo.getName()); + String regName = uploadRegInfo.getName(); + RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef(); + regCapRelDef.setFromNode(resourceInstanceId); + log.debug("try to find available requirement {} ", regName); + Either<RequirementDefinition, ResponseFormat> eitherReqStatus = findAviableRequiremen(regName, + yamlName, nodesInfoValue, currentCompInstance, + uploadRegInfo.getCapabilityName()); + if (eitherReqStatus.isRight()) { + return eitherReqStatus.right().value(); + } + + RequirementDefinition validReq = eitherReqStatus.left().value(); + List<CapabilityRequirementRelationship> reqAndRelationshipPairList = regCapRelDef + .getRelationships(); + if (reqAndRelationshipPairList == null) { + reqAndRelationshipPairList = new ArrayList<>(); + } + RelationshipInfo reqAndRelationshipPair = new RelationshipInfo(); + reqAndRelationshipPair.setRequirement(regName); + reqAndRelationshipPair.setRequirementOwnerId(validReq.getOwnerId()); + reqAndRelationshipPair.setRequirementUid(validReq.getUniqueId()); + RelationshipImpl relationship = new RelationshipImpl(); + relationship.setType(validReq.getCapability()); + reqAndRelationshipPair.setRelationships(relationship); + + ComponentInstance currentCapCompInstance = null; + for (ComponentInstance compInstance : componentInstancesList) { + if (compInstance.getName().equals(uploadRegInfo.getNode())) { + currentCapCompInstance = compInstance; + break; + } + } + + if (currentCapCompInstance == null) { + log.debug("The component instance with name {} not found on resource {} ", + uploadRegInfo.getNode(), resource.getUniqueId()); + BeEcompErrorManager.getInstance().logInternalDataError( + COMPONENT_INSTANCE_WITH_NAME + uploadRegInfo.getNode() + IN_RESOURCE, + resource.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + return componentsUtils + .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + regCapRelDef.setToNode(currentCapCompInstance.getUniqueId()); + log.debug("try to find aviable Capability req name is {} ", validReq.getName()); + CapabilityDefinition aviableCapForRel = findAvailableCapabilityByTypeOrName(validReq, + currentCapCompInstance, uploadRegInfo); + reqAndRelationshipPair.setCapability(aviableCapForRel.getName()); + reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId()); + reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId()); + if (aviableCapForRel == null) { + BeEcompErrorManager.getInstance().logInternalDataError( + "aviable capability was not found. req name is " + validReq.getName() + + " component instance is " + currentCapCompInstance.getUniqueId(), + resource.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR); + return componentsUtils + .getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship(); + capReqRel.setRelation(reqAndRelationshipPair); + reqAndRelationshipPairList.add(capReqRel); + regCapRelDef.setRelationships(reqAndRelationshipPairList); + relations.add(regCapRelDef); + } + } + } else if (resource.getResourceType() != ResourceTypeEnum.VF) { + return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName); + } + return componentsUtils.getResponseFormat(ActionStatus.OK); + } + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/utils/CreateServiceFromYamlParameter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/utils/CreateServiceFromYamlParameter.java new file mode 100644 index 0000000000..5d2f26a991 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/utils/CreateServiceFromYamlParameter.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.be.components.impl.utils; + +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.components.csar.CsarInfo; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.NodeTypeInfo; +import org.openecomp.sdc.be.model.ParsedToscaYamlInfo; + +import java.util.List; +import java.util.Map; +@Getter +@Setter +public class CreateServiceFromYamlParameter { + private String yamlName; + private ParsedToscaYamlInfo parsedToscaYamlInfo; + private List<ArtifactDefinition> createdArtifacts; + private String topologyTemplateYaml; + private Map<String, NodeTypeInfo> nodeTypesInfo; + private CsarInfo csarInfo; + private boolean shouldLock; + private boolean inTransaction; + private String nodeName; +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/ServiceDistributionValidation.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/ServiceDistributionValidation.java index 118ca5bd4c..695ee58300 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/ServiceDistributionValidation.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/ServiceDistributionValidation.java @@ -72,6 +72,12 @@ public class ServiceDistributionValidation { } } + public void validateAbstractServiceRequest(String serviceUUID, User modifier) { + validateUserExists(modifier.getUserId()); + Service abstractService = validateServiceExists(serviceUUID); + validateDistributionServiceLifeCycleState(abstractService); + } + private Service validateServiceExists(String serviceUUID) { if (StringUtils.isEmpty(serviceUUID.trim())) { ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.BAD_REQUEST_MISSING_RESOURCE); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/AbstractTemplateServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/AbstractTemplateServlet.java new file mode 100644 index 0000000000..0320a0a795 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/AbstractTemplateServlet.java @@ -0,0 +1,321 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.be.externalapi.servlet; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.jcabi.aspects.Loggable; +import fj.data.Either; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.servers.Server; +import io.swagger.v3.oas.annotations.servers.Servers; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.tags.Tags; + +import java.io.IOException; +import java.util.List; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + + +import org.openecomp.sdc.be.components.impl.*; +import org.openecomp.sdc.be.components.impl.aaf.AafPermission; +import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed; +import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; +import org.openecomp.sdc.be.config.BeEcompErrorManager; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.externalapi.servlet.representation.AbstractTemplateInfo; +import org.openecomp.sdc.be.externalapi.servlet.representation.CopyServiceInfo; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.impl.ServletUtils; +import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData; +import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo; +import org.openecomp.sdc.be.servlets.*; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.datastructure.Wrapper; +import org.openecomp.sdc.common.log.elements.LoggerSupportability; +import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions; +import org.openecomp.sdc.common.log.enums.StatusCode; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.stereotype.Controller; + +/** + * This servlet provides external interfaces related to abstract templates. + * + * @author hekeguang + */ + +@Loggable(prepend = true, value = Loggable.DEBUG, trim = false) +@Path("/v1/catalog") +@Tags({@Tag(name = "SDC External APIs")}) +@Servers({@Server(url = "/sdc")}) +@Controller + +public class AbstractTemplateServlet extends AbstractValidationsServlet { + + @Context + private HttpServletRequest request; + + private final ElementBusinessLogic elementBusinessLogic; + private final AbstractTemplateBusinessLogic abstractTemplateBusinessLogic; + + private final ServiceBusinessLogic serviceBusinessLogic; + private final ResourceBusinessLogic resourceBusinessLogic; + + private static final Logger log = Logger.getLogger(AbstractTemplateServlet.class); + private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(AbstractTemplateServlet.class.getName()); + + @Inject + public AbstractTemplateServlet(UserBusinessLogic userBusinessLogic, + ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils, + ServletUtils servletUtils, ResourceImportManager resourceImportManager, + ElementBusinessLogic elementBusinessLogic, + AbstractTemplateBusinessLogic abstractTemplateBusinessLogic, ServiceBusinessLogic serviceBusinessLogic, ResourceBusinessLogic resourceBusinessLogic) { + super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager); + this.elementBusinessLogic = elementBusinessLogic; + this.abstractTemplateBusinessLogic = abstractTemplateBusinessLogic; + this.serviceBusinessLogic = serviceBusinessLogic; + this.resourceBusinessLogic = resourceBusinessLogic; + } + + private Wrapper<ResponseFormat> validateRequestHeaders(String instanceIdHeader, String userId) { + Wrapper<ResponseFormat> responseWrapper = new Wrapper<>(); + if (responseWrapper.isEmpty()) { + validateXECOMPInstanceIDHeader(instanceIdHeader, responseWrapper); + } + if (responseWrapper.isEmpty()) { + validateHttpCspUserIdHeader(userId, responseWrapper); + } + return responseWrapper; + } + + /** + * @param requestId + * @param instanceIdHeader + * @param accept + * @param authorization + * @param uuid + * @return + */ + @GET + @Path("/abstract/service/serviceUUID/{uuid}/status") + @Produces(MediaType.APPLICATION_JSON) + @Operation(description = "Fetch abstract status of service", method = "GET", + summary = "Return whether the service is a virtual service", responses = {@ApiResponse(responseCode = "200", + description = "The check result of whether the service is an abstract service is returned", + content = @Content(array = @ArraySchema(schema = @Schema(implementation = AbstractTemplateInfo.class)))), + @ApiResponse(responseCode = "400", description = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"), + @ApiResponse(responseCode = "401", + description = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"), + @ApiResponse(responseCode = "403", description = "ECOMP component is not authorized - POL5003"), + @ApiResponse(responseCode = "404", + description = "Error: Requested '%1' (uuid) resource was not found - SVC4063"), + @ApiResponse(responseCode = "405", + description = "Method Not Allowed : Invalid HTTP method type used ( PUT,DELETE,POST will be rejected) - POL4050"), + @ApiResponse(responseCode = "500", + description = "The GET request failed either due to internal SDC problem. ECOMP Component should continue the attempts to get the needed information - POL5000")}) + @PermissionAllowed(AafPermission.PermNames.READ_VALUE) + public Response getServiceAbstractStatus( + @Parameter(description = "X-ECOMP-RequestID header", + required = false) @HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId, + @Parameter(description = "X-ECOMP-InstanceID header", required = true) @HeaderParam( + value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader, + @Parameter(description = "Determines the format of the body of the response", + required = false) @HeaderParam(value = Constants.ACCEPT_HEADER) String accept, + @Parameter(description = "The username and password", + required = true) @HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization, + @Parameter(description = "The requested asset uuid", + required = true) @PathParam("uuid") final String uuid) throws IOException { + + Response response = null; + ResponseFormat responseFormat = null; + AuditingActionEnum auditingActionEnum = AuditingActionEnum.GET_TEMPLATE_ABSTRACT_STATUS; + String requestURI = request.getRequestURI(); + String url = request.getMethod() + " " + requestURI; + log.debug("getServiceAbstractStatus: Start handle request of {}", url); + + String assetType = "services"; + ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType); + ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(componentType.getValue()); + DistributionData distributionData = new DistributionData(instanceIdHeader, requestURI); + // Mandatory + if (instanceIdHeader == null || instanceIdHeader.isEmpty()) { + log.debug("getServiceAbstractStatus: Missing X-ECOMP-InstanceID header"); + responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID); + getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, distributionData, + resourceCommonInfo, requestId, uuid); + return buildErrorResponse(responseFormat); + } + + try { + + Either<List<? extends Component>, ResponseFormat> assetTypeData = elementBusinessLogic.getCatalogComponentsByUuidAndAssetType(assetType, uuid); + + if (assetTypeData.isRight()) { + log.debug("getServiceAbstractStatus: Service Fetching Failed"); + responseFormat = assetTypeData.right().value(); + getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, distributionData, + resourceCommonInfo, requestId, uuid); + + return buildErrorResponse(responseFormat); + } + resourceCommonInfo.setResourceName(assetTypeData.left().value().iterator().next().getName()); + log.debug("getServiceAbstractStatus: Service Fetching Success"); + Either<AbstractTemplateInfo, ResponseFormat> resMetadata = abstractTemplateBusinessLogic.getServiceAbstractStatus(assetTypeData.left().value()); + if (resMetadata.isRight()) { + log.debug("getServiceAbstractStatus: Service abstract status get Failed"); + responseFormat = resMetadata.right().value(); + + getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, distributionData, + resourceCommonInfo, requestId, uuid); + return buildErrorResponse(responseFormat); + } + Object result = RepresentationUtils.toRepresentation(resMetadata.left().value()); + responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK); + getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, distributionData, + resourceCommonInfo, requestId, uuid); + + response = buildOkResponse(responseFormat, result); + return response; + + } catch (Exception e) { + BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Fetch abstract status of service"); + log.debug("getServiceAbstractStatus: Fetch abstract status of service with exception", e); + throw e; + } + } + + /** + * @param requestId + * @param instanceIdHeader + * @param accept + * @param authorization + * @param uuid + * @return + */ + @POST + @Path("/abstract/service/serviceUUID/{uuid}/copy") + @Produces(MediaType.APPLICATION_JSON) + @Operation(description = "Copy a new service based on the existing service", method = "POST", + summary = "Return whether the copy service is successful", responses = {@ApiResponse(responseCode = "200", + description = "ECOMP component is authenticated and list of Catalog Assets Metadata is returned", + content = @Content(array = @ArraySchema(schema = @Schema(implementation = AbstractTemplateInfo.class)))), + @ApiResponse(responseCode = "400", description = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"), + @ApiResponse(responseCode = "401", + description = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"), + @ApiResponse(responseCode = "403", description = "ECOMP component is not authorized - POL5003"), + @ApiResponse(responseCode = "404", + description = "Error: Requested '%1' (uuid) resource was not found - SVC4063"), + @ApiResponse(responseCode = "405", + description = "Method Not Allowed : Invalid HTTP method type used ( PUT,DELETE,POST will be rejected) - POL4050"), + @ApiResponse(responseCode = "500", + description = "The GET request failed either due to internal SDC problem. ECOMP Component should continue the attempts to get the needed information - POL5000"), + @ApiResponse(responseCode = "409", description = "Service already exist")}) + @PermissionAllowed(AafPermission.PermNames.WRITE_VALUE) + public Response copyExistService( + @Parameter(description = "The user id", + required = true) @HeaderParam(value = Constants.USER_ID_HEADER) final String userId, + @Parameter(description = "X-ECOMP-RequestID header", + required = false) @HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId, + @Parameter(description = "X-ECOMP-InstanceID header", required = true) @HeaderParam( + value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader, + @Parameter(description = "Determines the format of the body of the response", + required = false) @HeaderParam(value = Constants.ACCEPT_HEADER) String accept, + @Parameter(description = "The username and password", + required = true) @HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization, + @Parameter(description = "The requested asset uuid", + required = true) @PathParam("uuid") final String uuid, + @Parameter(hidden = true) String data) throws IOException { + + + String url = request.getMethod() + " " + request.getRequestURI(); + log.debug("copyExistService: Start handle request of {}", url); + User modifier = new User(); + modifier.setUserId(userId); + log.debug("modifier id is {}", userId); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_SERVICE, StatusCode.STARTED, "Starting to create a service by user {} ", userId); + + validateNotEmptyBody(data); + + Either<CopyServiceInfo, ResponseFormat> convertResponse = parseToCopyServiceInfo(data, modifier); + if (convertResponse.isRight()) { + throw new ByResponseFormatComponentException(convertResponse.right().value()); + } + + String assetType = "services"; + CopyServiceInfo copyServiceInfo = convertResponse.left().value(); + Either<List<? extends Component>, ResponseFormat> assetTypeData = elementBusinessLogic.getCatalogComponentsByUuidAndAssetType(assetType, uuid); + + if (assetTypeData.isRight() || assetTypeData.left().value().size() != 1) { + log.debug("getServiceAbstractStatus: Service Fetching Failed"); + throw new ByResponseFormatComponentException(assetTypeData.right().value()); + } + + log.debug("getServiceAbstractStatus: Service Fetching Success"); + + Service service = (Service) assetTypeData.left().value().get(0); + List<String> tags = service.getTags(); + if (tags != null && !tags.isEmpty()) { + for (int i = tags.size() - 1; i >= 0; i--) { + String tag = tags.get(i); + if (service.getName().equals(tag)) { + tags.remove(tag); + } + } + } + service.setName(copyServiceInfo.getNewServiceName()); + tags.add(copyServiceInfo.getNewServiceName()); + Either<Service, ResponseFormat> actionResponse = serviceBusinessLogic.createService(service, modifier); + + if (actionResponse.isRight()) { + log.debug("Failed to create service"); + throw new ByResponseFormatComponentException(actionResponse.right().value()); + } + + loggerSupportability.log(LoggerSupportabilityActions.CREATE_SERVICE, service.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, "Service {} has been copyied by user {} ", service.getName(), userId); + + return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value()); + + } + + public Either<CopyServiceInfo, ResponseFormat> parseToCopyServiceInfo(String serviceJson, User user) { + return getComponentsUtils().convertJsonToObjectUsingObjectMapper(serviceJson, user, CopyServiceInfo.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.SERVICE); + } + + private CopyServiceInfo convertJsonToServiceInfo(String data) { + ObjectMapper mapper = new ObjectMapper(); + try { + return mapper.readValue(data, CopyServiceInfo.class); + } catch (IOException e) { + log.error("#convertJsonToServiceInfo - json deserialization failed with error: ", e); + return new CopyServiceInfo(); + } + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractResourceInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractResourceInfo.java new file mode 100644 index 0000000000..6bb8356fa4 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractResourceInfo.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openecomp.sdc.be.externalapi.servlet.representation; + +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; + +import java.util.List; + +@Getter +@Setter +public class AbstractResourceInfo { + + private String abstractResourceUUid; + private String abstractResourceName; + //获得abstractResourceUniqueId + private String abstractResourceUniqueId; + //private String abstractResourceNormalizedName; + //获得componentInstancesRelations + private List<RequirementCapabilityRelDef> componentInstancesRelations; +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractTemplateInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractTemplateInfo.java new file mode 100644 index 0000000000..ba4cde7ded --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractTemplateInfo.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openecomp.sdc.be.externalapi.servlet.representation; + +import java.util.List; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class AbstractTemplateInfo { + + private String serviceUUid; + private Boolean isAbstractTemplate; + //获得serviceUniqueId + private String serviceUniqueId; + private List<AbstractResourceInfo> abstractResourceInfoList; +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/CopyServiceInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/CopyServiceInfo.java new file mode 100644 index 0000000000..078d282aae --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/CopyServiceInfo.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openecomp.sdc.be.externalapi.servlet.representation; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CopyServiceInfo { + private String newServiceUUid; + + private String newServiceName; + + private String oldServiceUUid; +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/ReplaceVNFInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/ReplaceVNFInfo.java new file mode 100644 index 0000000000..db312e50ad --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/representation/ReplaceVNFInfo.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openecomp.sdc.be.externalapi.servlet.representation; + +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; + +import java.util.List; + +@Getter +@Setter +public class ReplaceVNFInfo { + /* + delete vnf param + */ + private String serviceUniqueId; + + private String abstractResourceUniqueId; + + /* + add vnf param + */ + private ComponentInstance realVNFComponentInstance; + + //private List<RequirementCapabilityRelDef> componentInstancesRelations; +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java index d3a8644136..7ac20893c9 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java @@ -226,7 +226,7 @@ public class ComponentsUtils { * @param resource * @return */ - public ResponseFormat getResponseFormatByResource(ActionStatus actionStatus, Resource resource) { + public ResponseFormat getResponseFormatByResource(ActionStatus actionStatus, Component resource) { if (resource == null) { return getResponseFormat(actionStatus); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/info/ServiceVersionInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/info/ServiceVersionInfo.java index 185c1a474e..09ac6747f4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/info/ServiceVersionInfo.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/info/ServiceVersionInfo.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.be.info; +import com.google.common.annotations.VisibleForTesting; import lombok.Getter; import lombok.Setter; @@ -29,6 +30,9 @@ public class ServiceVersionInfo { private String version; private String url; + @VisibleForTesting + ServiceVersionInfo() {} + public ServiceVersionInfo(String serviceName, String serviceVersion, String context) { super(); this.version = serviceVersion; @@ -36,4 +40,55 @@ public class ServiceVersionInfo { sb.append("services/").append(serviceName).append("/").append(serviceVersion); url = sb.toString(); } + + + private String artifactUuid; + private String state; + private String distributionStatus; + + + public static Builder newBuilder() { + return new Builder(); + } + + public String getArtifactUuid() { + return artifactUuid; + } + public String getState() { + return state; + } + public String getDistributionStatus() { return distributionStatus; } + + + public static class Builder { + private final ServiceVersionInfo instance; + + private Builder() { + instance = new ServiceVersionInfo(); + } + + public Builder artifactUuid(String artifactUuid) { + instance.artifactUuid = artifactUuid; + return this; + } + + public Builder state(String state) { + instance.state = state; + return this; + } + + public Builder version(String version) { + instance.version = version; + return this; + } + + public Builder distributionStatus(String distributionStatus) { + instance.distributionStatus = distributionStatus; + return this; + } + + public ServiceVersionInfo build() { + return instance; + } + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java index 0cdf872f71..cb0cc239f2 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java @@ -28,6 +28,7 @@ import com.google.gson.JsonSyntaxException; import fj.data.Either; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Type; @@ -52,6 +53,7 @@ import org.openecomp.sdc.be.components.impl.ImportUtils; import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum; import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum; import org.openecomp.sdc.be.components.impl.ResourceImportManager; +import org.openecomp.sdc.be.components.impl.ServiceImportManager; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; @@ -62,12 +64,17 @@ import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; +import org.openecomp.sdc.be.impl.WebAppContextWrapper; import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.UploadResourceInfo; +import org.openecomp.sdc.be.model.UploadServiceInfo; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; import org.openecomp.sdc.be.servlets.ResourceUploadServlet.ResourceAuthorityTypeEnum; +import org.openecomp.sdc.be.servlets.ServiceUploadServlet.ServiceAuthorityTypeEnum; import org.openecomp.sdc.be.user.Role; import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.be.utils.TypeUtils; @@ -80,6 +87,7 @@ import org.openecomp.sdc.common.util.YamlToObjectConverter; import org.openecomp.sdc.common.zip.ZipUtils; import org.openecomp.sdc.common.zip.exception.ZipException; import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.web.context.WebApplicationContext; import org.yaml.snakeyaml.Yaml; public abstract class AbstractValidationsServlet extends BeGenericServlet { @@ -94,12 +102,13 @@ public abstract class AbstractValidationsServlet extends BeGenericServlet { TOSCA_SIMPLE_YAML_PREFIX + "1_1", TOSCA_SIMPLE_YAML_PREFIX + "1_2", TOSCA_SIMPLE_YAML_PREFIX + "1_3"); - private static final List<String> TOSCA_YML_CSAR_VALID_SUFFIX = Arrays.asList(".yml", ".yaml", ".csar"); + private static final List<String> TOSCA_YML_CSAR_VALID_SUFFIX = Arrays.asList(".yml", ".yaml", ".csar", ".meta"); protected ServletUtils servletUtils; protected ResourceImportManager resourceImportManager; protected final ComponentInstanceBusinessLogic componentInstanceBusinessLogic; + protected ServiceImportManager serviceImportManager; public AbstractValidationsServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils, @@ -113,6 +122,16 @@ public abstract class AbstractValidationsServlet extends BeGenericServlet { protected void init() { } + protected synchronized void initSpringFromContext() { + if (serviceImportManager == null) { + ServletContext context = servletRequest.getSession().getServletContext(); + WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context + .getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR); + WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context); + serviceImportManager = webApplicationContext.getBean(ServiceImportManager.class); + } + } + protected void validateResourceDoesNotExist(Wrapper<Response> responseWrapper, User user, String resourceName) { if (resourceImportManager.isResourceExist(resourceName)) { ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.RESOURCE_ALREADY_EXISTS); @@ -483,19 +502,23 @@ public abstract class AbstractValidationsServlet extends BeGenericServlet { private void validateToscaTemplatePayloadName(Wrapper<Response> responseWrapper, UploadResourceInfo uploadResourceInfo, User user) { String toscaTemplatePayloadName = uploadResourceInfo.getPayloadName(); - boolean isValidSuffix = false; - if (toscaTemplatePayloadName != null && !toscaTemplatePayloadName.isEmpty()) { - for (String validSuffix : TOSCA_YML_CSAR_VALID_SUFFIX) { - isValidSuffix = isValidSuffix || toscaTemplatePayloadName.toLowerCase().endsWith(validSuffix); - } - } + boolean isValidSuffix = isToscaTemplatePayloadNameValid(responseWrapper, toscaTemplatePayloadName); if (!isValidSuffix) { ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_TOSCA_FILE_EXTENSION); Response errorResponse = buildErrorResponse(responseFormat); getComponentsUtils().auditResource(responseFormat, user, uploadResourceInfo.getName(), AuditingActionEnum.IMPORT_RESOURCE); responseWrapper.setInnerElement(errorResponse); } + } + private boolean isToscaTemplatePayloadNameValid(Wrapper<Response> responseWrapper, String toscaTemplatePayloadName) { + boolean isValidSuffix = false; + if (toscaTemplatePayloadName != null && !toscaTemplatePayloadName.isEmpty()) { + for (String validSuffix : TOSCA_YML_CSAR_VALID_SUFFIX) { + isValidSuffix = isValidSuffix || toscaTemplatePayloadName.toLowerCase().endsWith(validSuffix); + } + } + return isValidSuffix; } private void validateMD5(Wrapper<Response> responseWrapper, User user, UploadResourceInfo resourceInfo, HttpServletRequest request, String resourceInfoJsonString) { @@ -721,19 +744,29 @@ public abstract class AbstractValidationsServlet extends BeGenericServlet { return new ImmutablePair<>(newResource, actionStatus); } - private Resource throwComponentException(ResponseFormat responseFormat) { + protected Resource throwComponentException(ResponseFormat responseFormat) { throw new ByResponseFormatComponentException(responseFormat); } private void getAndValidateCsarYaml(Map<String, byte[]> csarUIPayload, Resource resource, User user, String csarUUID) { + getAndValidateComponentCsarYaml(csarUIPayload, resource, user, csarUUID); + } + + private void getAndValidateComponentCsarYaml(Map<String, byte[]> csarUIPayload, Component component, User user, String csarUUID) { Either<ImmutablePair<String, String>, ResponseFormat> getToscaYamlRes = CsarValidationUtils.getToscaYaml(csarUIPayload, csarUUID, getComponentsUtils()); if (getToscaYamlRes.isRight()) { ResponseFormat responseFormat = getToscaYamlRes.right().value(); log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat); - BeEcompErrorManager.getInstance().logBeDaoSystemError("Creating resource from CSAR: fetching CSAR with id " + csarUUID + " failed"); - getComponentsUtils().auditResource(responseFormat, user, resource, AuditingActionEnum.CREATE_RESOURCE); + if (component instanceof Resource) { + BeEcompErrorManager.getInstance() + .logBeDaoSystemError("Creating resource from CSAR: fetching CSAR with id " + csarUUID + " failed"); + getComponentsUtils().auditResource(responseFormat, user, (Resource)component, AuditingActionEnum.CREATE_RESOURCE); + }else { + BeEcompErrorManager.getInstance() + .logBeDaoSystemError("Creating service from CSAR: fetching CSAR with id " + csarUUID + " failed"); + } throwComponentException(responseFormat); } String toscaYaml = getToscaYamlRes.left().value().getValue(); @@ -743,7 +776,9 @@ public abstract class AbstractValidationsServlet extends BeGenericServlet { boolean isValid = yamlConvertor.isValidYaml(toscaYaml.getBytes()); if (!isValid) { ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_YAML_FILE); - getComponentsUtils().auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE); + if (component instanceof Resource) { + getComponentsUtils().auditResource(responseFormat, user, (Resource)component, AuditingActionEnum.IMPORT_RESOURCE); + } throwComponentException(responseFormat); } @@ -764,7 +799,10 @@ public abstract class AbstractValidationsServlet extends BeGenericServlet { if (!isValid) { ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_TOSCA_TEMPLATE); - getComponentsUtils().auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE); + if (component instanceof Resource) { + log.debug("enter getAndValidateComponentCsarYaml,component instanceof Resource"); + getComponentsUtils().auditResource(responseFormat, user, (Resource)component, AuditingActionEnum.IMPORT_RESOURCE); + } throwComponentException(responseFormat); } } @@ -780,23 +818,32 @@ public abstract class AbstractValidationsServlet extends BeGenericServlet { List<UploadArtifactInfo> artifactList = resourceInfoObject.getArtifactList(); if (artifactList != null) { Map<String, ArtifactDefinition> artifactsHM = new HashMap<>(); - for (UploadArtifactInfo artifact : artifactList) { - ArtifactDefinition artifactDef = new ArtifactDefinition(); - artifactDef.setArtifactName(artifact.getArtifactName()); - artifactDef.setArtifactType(artifact.getArtifactType().getType()); - artifactDef.setDescription(artifact.getArtifactDescription()); - artifactDef.setPayloadData(artifact.getArtifactData()); - artifactDef.setArtifactRef(artifact.getArtifactPath()); - artifactsHM.put(artifactDef.getArtifactName(), artifactDef); - } + buildArtifactsHM(artifactList, artifactsHM); resource.setArtifacts(artifactsHM); } } } + private void buildArtifactsHM(List<UploadArtifactInfo> artifactList, Map<String, ArtifactDefinition> artifactsHM){ + for (UploadArtifactInfo artifact : artifactList) { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setArtifactName(artifact.getArtifactName()); + artifactDef.setArtifactType(artifact.getArtifactType().getType()); + artifactDef.setDescription(artifact.getArtifactDescription()); + artifactDef.setPayloadData(artifact.getArtifactData()); + artifactDef.setArtifactRef(artifact.getArtifactPath()); + artifactsHM.put(artifactDef.getArtifactName(), artifactDef); + } + } + private Map<String, byte[]> getCsarFromPayload(UploadResourceInfo innerElement) { String csarUUID = innerElement.getPayloadName(); String payloadData = innerElement.getPayloadData(); + + return getComponentCsarFromPayload(csarUUID, payloadData); + } + + private Map<String, byte[]> getComponentCsarFromPayload(String csarUUID, String payloadData){ if (payloadData == null) { log.info("Failed to decode received csar {}", csarUUID); throw new ByActionStatusComponentException(ActionStatus.CSAR_NOT_FOUND, csarUUID); @@ -911,4 +958,373 @@ public abstract class AbstractValidationsServlet extends BeGenericServlet { throw new ByActionStatusComponentException(ActionStatus.MISSING_BODY); } } + + protected void commonServiceGeneralValidations( + Wrapper<Response> responseWrapper, Wrapper<User> userWrapper, + Wrapper<UploadServiceInfo> uploadServiceInfoWrapper, ServiceAuthorityTypeEnum serviceAuthorityEnum, + String userUserId, + String serviceInfoJsonString) { + + if (responseWrapper.isEmpty()) { + validateUserExist(responseWrapper, userWrapper, userUserId); + } + + if (responseWrapper.isEmpty()) { + validateUserRole(responseWrapper, userWrapper.getInnerElement(), serviceAuthorityEnum); + } + + if (responseWrapper.isEmpty()) { + validateAndFillServiceJson(responseWrapper, uploadServiceInfoWrapper, userWrapper.getInnerElement(), + serviceAuthorityEnum, serviceInfoJsonString); + } + + if (responseWrapper.isEmpty()) { + validateToscaTemplatePayloadName(responseWrapper, uploadServiceInfoWrapper.getInnerElement(), + userWrapper.getInnerElement()); + } + + } + + protected void validateUserRole(Wrapper<Response> errorResponseWrapper, User user, + ServiceAuthorityTypeEnum serviceAuthority) { + log.debug("validate user role"); + if (serviceAuthority == ServiceAuthorityTypeEnum.NORMATIVE_TYPE_BE) { + if (!user.getRole().equals(Role.ADMIN.name())) { + log.info("user is not in appropriate role to perform action"); + ResponseFormat responseFormat = + getComponentsUtils().getResponseFormat(ActionStatus.RESTRICTED_OPERATION); + log.debug("audit before sending response"); + Response response = buildErrorResponse(responseFormat); + errorResponseWrapper.setInnerElement(response); + } + } else { + validateUserRole(errorResponseWrapper, user); + } + + } + + protected void validateAndFillServiceJson(Wrapper<Response> responseWrapper, + Wrapper<UploadServiceInfo> uploadServiceInfoWrapper, User user, + ServiceAuthorityTypeEnum serviceAuthorityEnum, String serviceInfo) { + boolean isValid; + try { + log.debug("The received json is {}", serviceInfo); + UploadServiceInfo serviceInfoObject = gson.fromJson(serviceInfo, UploadServiceInfo.class); + if (serviceInfoObject == null) { + isValid = false; + } else { + if (!serviceAuthorityEnum.isBackEndImport()) { + isValid = + serviceInfoObject.getPayloadName() != null && !serviceInfoObject.getPayloadName().isEmpty(); + //only service name is checked + } else { + isValid = true; + } + uploadServiceInfoWrapper.setInnerElement(serviceInfoObject); + log.debug("get isValid:{},serviceInfoObject get name:{},get tags:{},getContactId:{}," + + " getPayloadName:{}",isValid, + uploadServiceInfoWrapper.getInnerElement().getName(), + uploadServiceInfoWrapper.getInnerElement().getTags(), + uploadServiceInfoWrapper.getInnerElement().getContactId(), + uploadServiceInfoWrapper.getInnerElement().getPayloadName()); + } + + } catch (JsonSyntaxException e) { + log.debug("enter validateAndFillServiceJson,Invalid json was received. {}", e.getMessage(), e); + isValid = false; + + } + if (!isValid) { + log.info("Invalid json was received."); + ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT); + Response errorResp = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(errorResp); + } + } + + protected void validateToscaTemplatePayloadName(Wrapper<Response> responseWrapper, + UploadServiceInfo uploadServiceInfo, User user) { + String toscaTemplatePayloadName = uploadServiceInfo.getPayloadName(); + boolean isValidSuffix = isToscaTemplatePayloadNameValid(responseWrapper, toscaTemplatePayloadName); + if (!isValidSuffix) { + ResponseFormat responseFormat = + getComponentsUtils().getResponseFormat(ActionStatus.INVALID_TOSCA_FILE_EXTENSION); + Response errorResponse = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(errorResponse); + } + + } + + + protected void specificServiceAuthorityValidations(Wrapper<Response> responseWrapper, + Wrapper<UploadServiceInfo> uploadServiceInfoWrapper, Wrapper<String> yamlStringWrapper, User user, + HttpServletRequest request, String serviceInfoJsonString, ServiceAuthorityTypeEnum serviceAuthorityEnum) + throws FileNotFoundException { + + if (responseWrapper.isEmpty()) { + // UI Only Validation + if (!serviceAuthorityEnum.isBackEndImport()) { + importUIValidations(responseWrapper, uploadServiceInfoWrapper.getInnerElement(), user, request, + serviceInfoJsonString); + } + + // User Defined Type Services + if (serviceAuthorityEnum.isUserTypeService() && !CsarValidationUtils.isCsarPayloadName( + uploadServiceInfoWrapper.getInnerElement().getPayloadName())) { + if (responseWrapper.isEmpty()) { + validatePayloadNameSpace(responseWrapper, uploadServiceInfoWrapper.getInnerElement(), user, + yamlStringWrapper.getInnerElement()); + } + + } + } + } + + protected void importUIValidations(Wrapper<Response> responseWrapper, UploadServiceInfo serviceInfo, User user, + HttpServletRequest request, String serviceInfoJsonString) { + if (responseWrapper.isEmpty()) { + validateMD5(responseWrapper, user, serviceInfo, request, serviceInfoJsonString); + } + if (responseWrapper.isEmpty() && request != null && request.getMethod() != null && request.getMethod() + .equals("POST")) { + validateServiceDoesNotExist(responseWrapper, user, serviceInfo.getName()); + } + } + + protected void validatePayloadNameSpace(Wrapper<Response> responseWrapper, UploadServiceInfo serviceInfo, User user, + String toscaPayload) { + boolean isValid; + String nameSpace = ""; + Map<String, Object> mappedToscaTemplate = (Map<String, Object>) new Yaml().load(toscaPayload); + Either<Map<String, Object>, ResultStatusEnum> toscaElement = + ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES); + if (toscaElement.isRight() || toscaElement.left().value().size() != 1) { + isValid = false; + } else { + nameSpace = toscaElement.left().value().keySet().iterator().next(); + isValid = nameSpace.startsWith(Constants.USER_DEFINED_SERVICE_NAMESPACE_PREFIX); + log.debug("enter validatePayloadNameSpace,get nameSpace:{},get Valid is:{}",nameSpace,isValid); + } + if (!isValid) { + ResponseFormat responseFormat = + getComponentsUtils().getResponseFormat(ActionStatus.INVALID_SERVICE_NAMESPACE); + Response errorResponse = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(errorResponse); + } else { + String str1 = nameSpace.substring(Constants.USER_DEFINED_SERVICE_NAMESPACE_PREFIX.length()); + String[] findTypes = str1.split("\\."); + if (ResourceTypeEnum.containsName(findTypes[0].toUpperCase())) { + String type = findTypes[0].toUpperCase(); + serviceInfo.setServiceType(type); + } else { + serviceInfo.setServiceType(ResourceTypeEnum.SERVICE.name()); + } + } + + } + + protected void validateMD5(Wrapper<Response> responseWrapper, User user, UploadServiceInfo serviceInfo, + HttpServletRequest request, String serviceInfoJsonString) { + boolean isValid; + String recievedMD5 = request.getHeader(Constants.MD5_HEADER); + if (recievedMD5 == null) { + isValid = false; + } else { + String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(serviceInfoJsonString); + isValid = calculateMD5.equals(recievedMD5); + } + if (!isValid) { + ResponseFormat responseFormat = + getComponentsUtils().getResponseFormat(ActionStatus.INVALID_SERVICE_CHECKSUM); + Response errorResponse = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(errorResponse); + } + } + + + protected void validateServiceDoesNotExist(Wrapper<Response> responseWrapper, User user, String serviceName) { + if (serviceImportManager.isServiceExist(serviceName)) { + ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.SERVICE_ALREADY_EXISTS); + Response errorResponse = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(errorResponse); + } + } + + protected void handleImportService(Wrapper<Response> responseWrapper, User user, + UploadServiceInfo serviceInfoObject, String yamlAsString, ServiceAuthorityTypeEnum authority, + boolean createNewVersion, String serviceUniqueId) throws ZipException { + + Response response = null; + Object representation = null; + ImmutablePair<Service, ActionStatus> importedServiceStatus = null; + if (CsarValidationUtils.isCsarPayloadName(serviceInfoObject.getPayloadName())) { + log.debug("import service from csar"); + importedServiceStatus = importServiceFromUICsar(serviceInfoObject, user, serviceUniqueId); + } + + if (importedServiceStatus != null) { + try { + representation = RepresentationUtils.toRepresentation(importedServiceStatus.left); + } catch (IOException e) { + log.debug("Error while building service representation : {}", e.getMessage(), e); + } + response = buildOkResponse(getComponentsUtils().getResponseFormat(importedServiceStatus.right), + representation); + } + responseWrapper.setInnerElement(response); + } + + private ImmutablePair<Service, ActionStatus> importServiceFromUICsar(UploadServiceInfo serviceInfoObject, User user, + String serviceUniqueId) throws ZipException { + + Service newService; + ImmutablePair<Service, ActionStatus> result = null; + ActionStatus actionStatus; + Service service = new Service(); + String payloadName = serviceInfoObject.getPayloadName(); + fillServiceFromServiceInfoObject(service, serviceInfoObject); + + Map<String, byte[]> csarUIPayloadRes = getCsarFromPayload(serviceInfoObject); + + getAndValidateCsarYaml(csarUIPayloadRes, service, user, payloadName); + + newService = serviceImportManager.getServiceImportBusinessLogic() + .createService(service, AuditingActionEnum.CREATE_SERVICE, user, csarUIPayloadRes, + payloadName); + actionStatus = ActionStatus.CREATED; + + return new ImmutablePair<>(newService, actionStatus); + } + + + private void fillServiceFromServiceInfoObject(Service service, UploadServiceInfo serviceInfoObject) { + serviceImportManager.populateServiceMetadata(serviceInfoObject, service); + fillArtifacts(service, serviceInfoObject); + + } + + private Map<String, byte[]> getCsarFromPayload(UploadServiceInfo innerElement) + throws ZipException { + String csarUUID = innerElement.getPayloadName(); + String payloadData = innerElement.getPayloadData(); + return getComponentCsarFromPayload(csarUUID, payloadData); + } + + private void getAndValidateCsarYaml(Map<String, byte[]> csarUIPayload, Service service, User user, + String csarUUID) { + getAndValidateComponentCsarYaml(csarUIPayload, service, user, csarUUID); + } + + private void fillArtifacts(Service service, UploadServiceInfo serviceInfoObject) { + if (service != null && serviceInfoObject != null) { + List<UploadArtifactInfo> artifactList = serviceInfoObject.getArtifactList(); + if (artifactList != null) { + Map<String, ArtifactDefinition> artifactsHM = new HashMap<>(); + buildArtifactsHM(artifactList, artifactsHM); + service.setArtifacts(artifactsHM); + } + } + } + /** + * import service payload to postman + * @param responseWrapper + * @param uploadServiceInfoWrapper + * @param yamlStringWrapper + * @param user + * @param serviceInfoJsonString + * @param serviceAuthorityEnum + * @param file + * @throws ZipException + */ + protected void fillServicePayload(Wrapper<Response> responseWrapper, Wrapper<UploadServiceInfo> uploadServiceInfoWrapper, Wrapper<String> yamlStringWrapper, User user, String serviceInfoJsonString, ServiceAuthorityTypeEnum serviceAuthorityEnum, + File file) throws ZipException { + log.debug("enter fillServicePayload"); + if (responseWrapper.isEmpty()) { + log.debug("enter fillServicePayload,get responseWrapper is empty"); + if (serviceAuthorityEnum.isBackEndImport()) { + // PrePayload Validations + if (responseWrapper.isEmpty()) { + validateDataNotNull(responseWrapper, file, serviceInfoJsonString); + } + if (responseWrapper.isEmpty()){ + log.debug("enter fillServicePayload,responseWrapper is empty"); + } + if(!serviceAuthorityEnum.equals(ServiceAuthorityTypeEnum.CSAR_TYPE_BE)){ + if (responseWrapper.isEmpty()) { + validateZip(responseWrapper, file, uploadServiceInfoWrapper.getInnerElement().getPayloadName()); + } + + // Fill PayLoad From File + if (responseWrapper.isEmpty()) { + fillToscaTemplateFromZip(yamlStringWrapper, uploadServiceInfoWrapper.getInnerElement().getPayloadName(), file); + } + }else{ + + log.debug("enter fillServicePayload,ServiceAuthorityTypeEnum is CSAR_TYPE_BE"); + if (responseWrapper.isEmpty()) { + validateCsar(responseWrapper, file, uploadServiceInfoWrapper.getInnerElement().getPayloadName()); + } + if (!responseWrapper.isEmpty()) { + log.debug("enter fillServicePayload,get responseWrapper:{}", responseWrapper); + } + // Fill PayLoad From File + if (responseWrapper.isEmpty()) { + fillServicePayloadDataFromFile(responseWrapper, uploadServiceInfoWrapper.getInnerElement(), file); + } + + } + + } else { + // Fill PayLoad From JSON + if (responseWrapper.isEmpty()) { + fillServiceToscaTemplateFromJson(responseWrapper, yamlStringWrapper, user, uploadServiceInfoWrapper.getInnerElement()); + } + } + + } + + } + + protected void fillServicePayloadDataFromFile(Wrapper<Response> responseWrapper, UploadServiceInfo uploadServiceInfoWrapper, File file) { + try(InputStream fileInputStream = new FileInputStream(file)){ + + log.debug("enter fillServicePayloadDataFromFile"); + byte [] data = new byte[(int)file.length()]; + if( fileInputStream.read(data) == -1){ + log.info("Invalid json was received."); + ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT); + + Response errorResp = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(errorResp); + } + String payloadData = Base64.encodeBase64String(data); + uploadServiceInfoWrapper.setPayloadData(payloadData); + log.debug("enter fillServicePayloadDataFromFile,get payloadData:{}", + uploadServiceInfoWrapper.getPayloadData()); + + log.debug("enter fillServicePayloadDataFromFile,get uploadService:{}",uploadServiceInfoWrapper); + + } catch (IOException e) { + log.info("Invalid json was received or Error while closing input Stream."); + log.debug("Invalid json was received or Error while closing input Stream. {}", e.getMessage(), e); + ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT); + + Response errorResp = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(errorResp); + } + } + + private void fillServiceToscaTemplateFromJson(Wrapper<Response> responseWrapper, Wrapper<String> yamlStringWrapper, User user, UploadServiceInfo serviceInfo) { + if (serviceInfo.getPayloadData() == null || serviceInfo.getPayloadData().isEmpty()) { + ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_RESOURCE_PAYLOAD); + Response errorResponse = buildErrorResponse(responseFormat); + getComponentsUtils().auditResource(responseFormat, user, serviceInfo.getName(), AuditingActionEnum.IMPORT_RESOURCE); + responseWrapper.setInnerElement(errorResponse); + } else { + String toscaPayload = serviceInfo.getPayloadData(); + String decodedPayload = new String(Base64.decodeBase64(toscaPayload)); + yamlStringWrapper.setInnerElement(decodedPayload); + } + } + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentInstanceServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentInstanceServlet.java index 5c3bd859be..0896891ad3 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentInstanceServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentInstanceServlet.java @@ -71,6 +71,7 @@ import org.openecomp.sdc.be.components.impl.aaf.AafPermission; import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed; import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; +import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; @@ -79,6 +80,7 @@ import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.CreatedFrom; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.externalapi.servlet.representation.ReplaceVNFInfo; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; import org.openecomp.sdc.be.info.CreateAndAssotiateInfo; @@ -1623,4 +1625,100 @@ public class ComponentInstanceServlet extends AbstractValidationsServlet { return Either.left(Arrays.asList(convertStatus.left().value())); } + @POST + @Path("/services/replaceVNF") + @Operation(description = "Replace new VNF based on the existing VNF", method = "POST", + summary = "Return whether the replace VNF is successful", responses = { + @ApiResponse(responseCode = "200", description = "ECOMP component is authenticated and list of Catalog Assets Metadata is returned", + content = @Content(array = @ArraySchema(schema = @Schema(implementation = ReplaceVNFInfo.class)))), + @ApiResponse(responseCode = "400", description = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"), + @ApiResponse(responseCode = "401", + description = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"), + @ApiResponse(responseCode = "403", description = "ECOMP component is not authorized - POL5003"), + @ApiResponse(responseCode = "404", + description = "Error: Requested '%1' (uuid) resource was not found - SVC4063"), + @ApiResponse(responseCode = "405", + description = "Method Not Allowed : Invalid HTTP method type used ( PUT,DELETE,POST will be rejected) - POL4050"), + @ApiResponse(responseCode = "500", + description = "The GET request failed either due to internal SDC problem. ECOMP Component should continue the attempts to get the needed information - POL5000"), + @ApiResponse(responseCode = "409", description = "Service already exist")}) + @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) + public Response replaceVNF( + @Parameter(description = "The user id", + required = true) @HeaderParam(value = Constants.USER_ID_HEADER) String userId, + @Parameter(description = "X-ECOMP-RequestID header", + required = false) @HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId, + @Parameter(description = "X-ECOMP-InstanceID header", required = true) + @HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader, + @Parameter(description = "Determines the format of the body of the response", + required = false) @HeaderParam(value = Constants.ACCEPT_HEADER) String accept, + @Parameter(description = "The username and password", + required = true) @HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization, + @Context final HttpServletRequest request, + @Parameter(description = "Resource object to be created", required = true) String data) { + + String url = request.getMethod() + " " + request.getRequestURI(); + log.debug("replaceVNF: Start handle request of {}", url); + User modifier = new User(); + modifier.setUserId(userId); + log.debug("replaceVNF:modifier id is {}", userId); + validateNotEmptyBody(data); + + Either<ReplaceVNFInfo, ResponseFormat> convertResponse = parseToReplaceVNFInfo(data, modifier); + if (convertResponse.isRight()) { + throw new ByResponseFormatComponentException(convertResponse.right().value()); + } + + log.debug("replaceVNF:get ReplaceVNFInfo success"); + + String containerComponentType = "services"; + ReplaceVNFInfo replaceVNFInfo = convertResponse.left().value(); + String serviceUniqueId = replaceVNFInfo.getServiceUniqueId(); + String abstractResourceUniqueId = replaceVNFInfo.getAbstractResourceUniqueId(); + + ComponentInstance componentInstance = replaceVNFInfo.getRealVNFComponentInstance(); + log.debug("replaceVNF:get ReplaceVNFInfo,serviceUniqueId:{},abstractResourceUniqueId:{}", + serviceUniqueId,abstractResourceUniqueId); + try { + /** + * delete vnf + */ + ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType); + if (componentInstanceBusinessLogic == null) { + log.debug("replaceVNF:Unsupported component type {}", containerComponentType); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType)); + } + Either<ComponentInstance, ResponseFormat> componentInstanceEither = componentInstanceBusinessLogic.deleteAbstractComponentInstance(containerComponentType, serviceUniqueId, abstractResourceUniqueId, userId); + if (componentInstanceEither.isRight()) { + log.debug("replaceVNF:delete Abstract ComponentInstance field"); + return buildErrorResponse(componentInstanceEither.right().value()); + } + /** + * add vnf + */ + log.debug("replaceVNF,start add vnf"); + componentInstance.setInvariantName(null); + componentInstance.setCreatedFrom(CreatedFrom.UI); + + Either<ComponentInstance, ResponseFormat> realComponentInstance = componentInstanceBusinessLogic.createRealComponentInstance(containerComponentType, serviceUniqueId, userId, componentInstance); + if (realComponentInstance.isRight()) { + log.debug("replaceVNF:filed to add vnf"); + return buildErrorResponse(realComponentInstance.right().value()); + } + ComponentInstance newComponentInstance = realComponentInstance.left().value(); + log.debug("replaceVNF:success to add vnf"); + + return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), newComponentInstance); + } catch (Exception e) { + BeEcompErrorManager.getInstance().logBeRestApiGeneralError("replaceVNF"); + log.debug("replaceVNF with exception", e); + throw e; + } + } + + private Either<ReplaceVNFInfo, ResponseFormat> parseToReplaceVNFInfo(String serviceJson, User user) { + log.debug("enter parseToReplaceVNFInfo,get serviceJson:{}",serviceJson); + return getComponentsUtils().convertJsonToObjectUsingObjectMapper(serviceJson, user, ReplaceVNFInfo.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.SERVICE); + } + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourcesServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourcesServlet.java index 3a341f127b..168a70aad2 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourcesServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourcesServlet.java @@ -32,11 +32,15 @@ import io.swagger.v3.oas.annotations.servers.Server; import io.swagger.v3.oas.annotations.servers.Servers; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tags; +import java.io.File; import org.apache.http.HttpStatus; +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataParam; import org.json.JSONException; import org.json.JSONObject; import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; import org.openecomp.sdc.be.components.impl.CsarValidationUtils; +import org.openecomp.sdc.be.components.impl.ElementBusinessLogic; import org.openecomp.sdc.be.components.impl.ImportUtils; import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic; import org.openecomp.sdc.be.components.impl.ResourceImportManager; @@ -53,6 +57,8 @@ import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.UploadResourceInfo; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData; +import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo; import org.openecomp.sdc.be.servlets.ResourceUploadServlet.ResourceAuthorityTypeEnum; import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.common.api.Constants; @@ -594,4 +600,97 @@ public class ResourcesServlet extends AbstractValidationsServlet { throw e; } } + + @POST + @Path("/resources/importReplaceResource") + @Produces(MediaType.APPLICATION_JSON) + @Operation(description = "Import Resource", method = "POST", summary = "Returns imported resource", responses = { + @ApiResponse(responseCode = "201", description = "Resource created"), + @ApiResponse(responseCode = "403", description = "Restricted operation"), + @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"), + @ApiResponse(responseCode = "409", description = "Resource already exist")}) + @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) + public Response importReplaceResource( + @Parameter(description = "The user id", + required = true) @HeaderParam(value = Constants.USER_ID_HEADER) String userId, + @Parameter(description = "X-ECOMP-RequestID header", + required = false) @HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId, + @Parameter(description = "X-ECOMP-InstanceID header", required = true) @HeaderParam( + value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader, + @Parameter(description = "Determines the format of the body of the response", + required = false) @HeaderParam(value = Constants.ACCEPT_HEADER) String accept, + @Parameter(description = "The username and password", + required = true) @HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization, + @Context final HttpServletRequest request, + @Parameter(description = "FileInputStream") + @FormDataParam("resourceZip") File file, + @Parameter(description = "ContentDisposition") + @FormDataParam("resourceZip") FormDataContentDisposition contentDispositionHeader, + @Parameter(description = "resourceMetadata") + @FormDataParam("resourceZipMetadata") String resourceInfoJsonString) { + + init(); + + String requestURI = request.getRequestURI(); + String url = request.getMethod() + " " + requestURI; + log.debug("importReplaceResource,Start handle request of {}", url); + + // get modifier id + User modifier = new User(); + modifier.setUserId(userId); + log.debug("importReplaceResource,modifier id is {}", userId); + + log.debug("importReplaceResource,get file:{},fileName:{}", file, file.getName()); + + Response response; + ResponseFormat responseFormat = null; + AuditingActionEnum auditingActionEnum = AuditingActionEnum.Import_Replace_Resource; + String assetType = "resources"; + + ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType); + ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(componentType.getValue()); + DistributionData distributionData = new DistributionData(instanceIdHeader, requestURI); + // Mandatory + if (instanceIdHeader == null || instanceIdHeader.isEmpty()) { + log.debug("importReplaceResource: Missing X-ECOMP-InstanceID header"); + responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID); + getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, distributionData, + resourceCommonInfo, requestId, null); + return buildErrorResponse(responseFormat); + } + + try { + Wrapper<Response> responseWrapper = new Wrapper<>(); + // file import + Wrapper<User> userWrapper = new Wrapper<>(); + Wrapper<UploadResourceInfo> uploadResourceInfoWrapper = new Wrapper<>(); + Wrapper<String> yamlStringWrapper = new Wrapper<>(); + + ResourceAuthorityTypeEnum serviceAuthorityEnum = ResourceAuthorityTypeEnum.CSAR_TYPE_BE; + + // PayLoad Validations + commonGeneralValidations(responseWrapper, userWrapper, uploadResourceInfoWrapper, serviceAuthorityEnum, userId, resourceInfoJsonString); + + fillPayload(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, modifier, resourceInfoJsonString, serviceAuthorityEnum, file); + + specificResourceAuthorityValidations(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), request, resourceInfoJsonString, serviceAuthorityEnum); + + log.debug("importReplaceResource:get payload:{}", uploadResourceInfoWrapper.getInnerElement().getPayloadData()); + + log.debug("importReplaceResource:get ResourceType:{}", + uploadResourceInfoWrapper.getInnerElement().getResourceType()); + + if (responseWrapper.isEmpty()) { + log.debug("importReplaceService:start handleImport"); + handleImport(responseWrapper, userWrapper.getInnerElement(), uploadResourceInfoWrapper.getInnerElement(), yamlStringWrapper.getInnerElement(), serviceAuthorityEnum, true, null); + } + + return responseWrapper.getInnerElement(); + } catch (ZipException e) { + BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Import Resource"); + log.debug("import resource failed with exception", e); + response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); + return response; + } + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceServlet.java index 5292f6e9f2..57a29566b1 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceServlet.java @@ -35,8 +35,13 @@ import io.swagger.v3.oas.annotations.servers.Server; import io.swagger.v3.oas.annotations.servers.Servers; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tags; +import java.io.File; +import java.io.FileNotFoundException; import org.apache.http.HttpStatus; +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataParam; import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; +import org.openecomp.sdc.be.components.impl.ElementBusinessLogic; import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic; import org.openecomp.sdc.be.components.impl.ResourceImportManager; import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic; @@ -46,15 +51,21 @@ import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponent import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datamodel.ServiceRelations; +import org.openecomp.sdc.be.datatypes.components.ServiceMetadataDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; +import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.DistributionStatusEnum; import org.openecomp.sdc.be.model.GroupInstanceProperty; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.UploadServiceInfo; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData; +import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo; +import org.openecomp.sdc.be.servlets.ServiceUploadServlet.ServiceAuthorityTypeEnum; import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.datastructure.Wrapper; @@ -62,6 +73,7 @@ import org.openecomp.sdc.common.log.elements.LoggerSupportability; import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions; import org.openecomp.sdc.common.log.enums.StatusCode; import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.common.zip.exception.ZipException; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.stereotype.Controller; @@ -93,6 +105,7 @@ import java.util.Map; @Controller public class ServiceServlet extends AbstractValidationsServlet { + private final ElementBusinessLogic elementBusinessLogic; private static final Logger log = Logger.getLogger(ServiceServlet.class); private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(ServiceServlet.class.getName()); @@ -107,9 +120,10 @@ public class ServiceServlet extends AbstractValidationsServlet { ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager, ServiceBusinessLogic serviceBusinessLogic, - ResourceBusinessLogic resourceBusinessLogic) { + ResourceBusinessLogic resourceBusinessLogic, ElementBusinessLogic elementBusinessLogic) { super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager); this.serviceBusinessLogic = serviceBusinessLogic; + this.elementBusinessLogic = elementBusinessLogic; } @POST @@ -723,4 +737,181 @@ public class ServiceServlet extends AbstractValidationsServlet { throw e; } } + + @POST + @Path("/services/importService") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @Operation(description = "Import Service", method = "POST", summary = "Returns imported service", responses = { + @ApiResponse(responseCode = "201", description = "Service created"), @ApiResponse(responseCode = "403", description = "Restricted operation"), @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"), @ApiResponse(responseCode = "409", description = "Service already exist")}) + public Response importNsService(@Parameter(description = "Service object to be imported", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) { + + userId = (userId != null) ? userId : request.getHeader(Constants.USER_ID_HEADER); + initSpringFromContext(); + + String url = request.getMethod() + " " + request.getRequestURI(); + log.debug("Start handle request of {}" , url); + + // get modifier id + User modifier = new User(); + modifier.setUserId(userId); + log.debug("modifier id is {}", userId); + + Response response; + try { + + Wrapper<Response> responseWrapper = new Wrapper<>(); + performUIImport(responseWrapper, data, request, userId, null); + return responseWrapper.getInnerElement(); + } catch (IOException | ZipException e) { + BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Import Service"); + log.debug("import service failed with exception", e); + response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); + return response; + } + } + + private void performUIImport(Wrapper<Response> responseWrapper, String data, final HttpServletRequest request, String userId, String ServiceUniqueId) + throws FileNotFoundException, ZipException { + + Wrapper<User> userWrapper = new Wrapper<>(); + Wrapper<UploadServiceInfo> uploadServiceInfoWrapper = new Wrapper<>(); + Wrapper<String> yamlStringWrapper = new Wrapper<>(); + + ServiceAuthorityTypeEnum ServiceAuthorityEnum = ServiceAuthorityTypeEnum.USER_TYPE_UI; + + commonServiceGeneralValidations(responseWrapper, userWrapper, uploadServiceInfoWrapper, ServiceAuthorityEnum, userId, data); + + specificServiceAuthorityValidations(responseWrapper, uploadServiceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), request, data, ServiceAuthorityEnum); + + if (responseWrapper.isEmpty()) { + handleImportService(responseWrapper, userWrapper.getInnerElement(), uploadServiceInfoWrapper.getInnerElement(), yamlStringWrapper.getInnerElement(), ServiceAuthorityEnum, true, ServiceUniqueId); + } + } + + /**import ReplaceService + * + * @param userId + * @param requestId + * @param instanceIdHeader + * @param accept + * @param authorization + * @param request + * @param file + * @param contentDispositionHeader + * @param serviceInfoJsonString + * @param uuid + * @return + */ + @POST + @Path("/services/serviceUUID/{uuid}/importReplaceService") + @Produces(MediaType.APPLICATION_JSON) + @Operation(description = "Import Service", method = "POST", summary = "Returns imported service", responses = { + @ApiResponse(responseCode = "201", description = "Service created"), + @ApiResponse(responseCode = "403", description = "Restricted operation"), + @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"), + @ApiResponse(responseCode = "409", description = "Service already exist")}) + @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) + public Response importReplaceService( + @Parameter(description = "The user id", + required = true) @HeaderParam(value = Constants.USER_ID_HEADER) String userId, + @Parameter(description = "X-ECOMP-RequestID header", + required = false) @HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId, + @Parameter(description = "X-ECOMP-InstanceID header", required = true) @HeaderParam( + value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader, + @Parameter(description = "Determines the format of the body of the response", + required = false) @HeaderParam(value = Constants.ACCEPT_HEADER) String accept, + @Parameter(description = "The username and password", + required = true) @HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization, + @Context final HttpServletRequest request, + @Parameter(description = "FileInputStream") + @FormDataParam("serviceZip") File file, + @Parameter(description = "ContentDisposition") + @FormDataParam("serviceZip") FormDataContentDisposition contentDispositionHeader, + @Parameter(description = "serviceMetadata") + @FormDataParam("serviceZipMetadata") String serviceInfoJsonString, + @Parameter(description = "The requested asset uuid", + required = true) @PathParam("uuid") final String uuid) { + + initSpringFromContext(); + + String requestURI = request.getRequestURI(); + String url = request.getMethod() + " " + requestURI; + log.debug("importReplaceService,Start handle request of {}", url); + + // get modifier id + User modifier = new User(); + modifier.setUserId(userId); + log.debug("importReplaceService,modifier id is {}", userId); + + log.debug("importReplaceService,get file:{},fileName:{}",file,file.getName()); + + Response response; + ResponseFormat responseFormat =null; + AuditingActionEnum auditingActionEnum = AuditingActionEnum.Import_Replace_Service; + String assetType = "services"; + + Either<List<? extends Component>, ResponseFormat> assetTypeData = elementBusinessLogic.getCatalogComponentsByUuidAndAssetType(assetType, uuid); + + if (assetTypeData.isRight() || assetTypeData.left().value().size() != 1) { + log.debug("getServiceAbstractStatus: Service Fetching Failed"); + throw new ByResponseFormatComponentException(assetTypeData.right().value()); + } + + log.debug("getServiceAbstractStatus: Service Fetching Success"); + + Service oldService = (Service) assetTypeData.left().value().get(0); + + ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType); + ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(componentType.getValue()); + DistributionData distributionData = new DistributionData(instanceIdHeader, requestURI); + // Mandatory + if (instanceIdHeader == null || instanceIdHeader.isEmpty()) { + log.debug("importReplaceService: Missing X-ECOMP-InstanceID header"); + responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID); + getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, distributionData, + resourceCommonInfo, requestId, null); + return buildErrorResponse(responseFormat); + } + + try { + Wrapper<Response> responseWrapper = new Wrapper<>(); + // file import + Wrapper<User> userWrapper = new Wrapper<>(); + Wrapper<UploadServiceInfo> uploadServiceInfoWrapper = new Wrapper<>(); + Wrapper<String> yamlStringWrapper = new Wrapper<>(); + + ServiceUploadServlet.ServiceAuthorityTypeEnum serviceAuthorityEnum = ServiceUploadServlet.ServiceAuthorityTypeEnum.CSAR_TYPE_BE; + + // PayLoad Validations + commonServiceGeneralValidations(responseWrapper, userWrapper, uploadServiceInfoWrapper, serviceAuthorityEnum, userId, serviceInfoJsonString); + + fillServicePayload(responseWrapper, uploadServiceInfoWrapper, yamlStringWrapper, modifier, serviceInfoJsonString, serviceAuthorityEnum, file); + + specificServiceAuthorityValidations(responseWrapper, uploadServiceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), request, serviceInfoJsonString, serviceAuthorityEnum); + + log.debug("importReplaceService:get payload:{}", uploadServiceInfoWrapper.getInnerElement().getPayloadData()); + + ServiceMetadataDataDefinition serviceMetadataDataDefinition = (ServiceMetadataDataDefinition)oldService.getComponentMetadataDefinition().getMetadataDataDefinition(); + + uploadServiceInfoWrapper.getInnerElement().setServiceVendorModelNumber(serviceMetadataDataDefinition.getServiceVendorModelNumber()); + uploadServiceInfoWrapper.getInnerElement().setDescription(oldService.getDescription()); + uploadServiceInfoWrapper.getInnerElement().setCategories(oldService.getCategories()); + uploadServiceInfoWrapper.getInnerElement().setIcon(oldService.getIcon()); + uploadServiceInfoWrapper.getInnerElement().setProjectCode(oldService.getProjectCode()); + + + if (responseWrapper.isEmpty()) { + log.debug("importReplaceService:start handleImportService"); + handleImportService(responseWrapper, userWrapper.getInnerElement(), uploadServiceInfoWrapper.getInnerElement(), yamlStringWrapper.getInnerElement(), serviceAuthorityEnum, true, null); + } + + return responseWrapper.getInnerElement(); + } catch (IOException | ZipException e) { + BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Import Service"); + log.debug("import service failed with exception", e); + response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); + return response; + } + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceUploadServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceUploadServlet.java new file mode 100644 index 0000000000..285568560d --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceUploadServlet.java @@ -0,0 +1,129 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.servlets; + +import com.jcabi.aspects.Loggable; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.servers.Server; +import io.swagger.v3.oas.annotations.servers.Servers; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.tags.Tags; +import java.io.File; +import javax.inject.Singleton; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataParam; +import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; +import org.openecomp.sdc.be.components.impl.ResourceImportManager; +import org.openecomp.sdc.be.components.impl.ServiceImportManager; +import org.openecomp.sdc.be.config.BeEcompErrorManager; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.impl.ServletUtils; +import org.openecomp.sdc.be.impl.WebAppContextWrapper; +import org.openecomp.sdc.be.model.UploadServiceInfo; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.datastructure.Wrapper; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.stereotype.Controller; +import org.springframework.web.context.WebApplicationContext; + +/** + * Root service (exposed at "/" path) + */ + +//upload Service model by Shiyong1989@hotmail.com +@Loggable(prepend = true, value = Loggable.DEBUG, trim = false) +@Path("/v1/catalog/uploadservice") +@Tags({@Tag(name = "SDC Internal APIs")}) +@Singleton + +public class ServiceUploadServlet extends AbstractValidationsServlet { + + private static final Logger log = Logger.getLogger(ServiceUploadServlet.class); + public static final String NORMATIVE_TYPE_SERVICE = "multipart"; + public static final String CSAR_TYPE_SERVICE = "csar"; + public static final String USER_TYPE_SERVICE = "user-service"; + public static final String USER_TYPE_SERVICE_UI_IMPORT = "user-servcie-ui-import"; + + public ServiceUploadServlet(UserBusinessLogic userBusinessLogic, + ComponentInstanceBusinessLogic componentInstanceBL, + ComponentsUtils componentsUtils, ServletUtils servletUtils, + ResourceImportManager resourceImportManager) { + super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager); + } + + public enum ServiceAuthorityTypeEnum { + NORMATIVE_TYPE_BE(NORMATIVE_TYPE_SERVICE, true, false), USER_TYPE_BE(USER_TYPE_SERVICE, true, true), + USER_TYPE_UI(USER_TYPE_SERVICE_UI_IMPORT, false, true), CSAR_TYPE_BE(CSAR_TYPE_SERVICE, true, true); + + private String urlPath; + private boolean isBackEndImport, isUserTypeService; + + public static ServiceAuthorityTypeEnum findByUrlPath(String urlPath) { + ServiceAuthorityTypeEnum found = null; + for (ServiceAuthorityTypeEnum curr : ServiceAuthorityTypeEnum.values()) { + if (curr.getUrlPath().equals(urlPath)) { + found = curr; + break; + } + } + return found; + } + + private ServiceAuthorityTypeEnum(String urlPath, boolean isBackEndImport, boolean isUserTypeService) { + this.urlPath = urlPath; + this.isBackEndImport = isBackEndImport; + this.isUserTypeService = isUserTypeService; + } + + public String getUrlPath() { + return urlPath; + } + + public boolean isBackEndImport() { + return isBackEndImport; + } + + public boolean isUserTypeService() { + return isUserTypeService; + } + } +} + diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java index 1f33a2424b..e19ea21c74 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java @@ -25,6 +25,7 @@ import static org.openecomp.sdc.be.tosca.ComponentCache.MergeStrategy.overwriteI import fj.F; import fj.data.Either; +import java.text.SimpleDateFormat; import io.vavr.Tuple2; import io.vavr.control.Try; import io.vavr.control.Option; @@ -34,6 +35,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.EnumMap; import java.util.HashMap; import java.util.HashSet; @@ -43,6 +45,7 @@ import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.TimeZone; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; @@ -150,6 +153,8 @@ public class CsarUtils { private static final String TOSCA_META_PATH_FILE_NAME = "TOSCA-Metadata/TOSCA.meta"; private static final String TOSCA_META_VERSION = "1.0"; private static final String CSAR_VERSION = "1.1"; + // add manifest + private static final String Service_Manifest = "NS.mf"; public static final String ARTIFACTS = "Artifacts"; private static final String DEFINITION = "Definitions"; private static final String DEL_PATTERN = "([/\\\\]+)"; @@ -270,6 +275,9 @@ public class CsarUtils { String fileName = artifactDef.getArtifactName(); zip.putNextEntry(new ZipEntry(DEFINITIONS_PATH + fileName)); zip.write(mainYaml); + + LifecycleStateEnum lifecycleState = component.getLifecycleState(); + addServiceMf(component, zip, lifecycleState, isInCertificationRequest, fileName, mainYaml); //US798487 - Abstraction of complex types if (!ModelConverter.isAtomicComponent(component)){ @@ -317,6 +325,41 @@ public class CsarUtils { return writeAllFilesToCsar(component, collectedComponentCsarDefinition.left().value(), zip, isInCertificationRequest); } + private void addServiceMf(Component component, ZipOutputStream zip, LifecycleStateEnum lifecycleState, boolean isInCertificationRequest, String fileName, byte[] mainYaml) throws IOException{ + // add mf + if((component.getComponentType() == ComponentTypeEnum.SERVICE) && (lifecycleState != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)){ + String serviceName = component.getName(); + String createdBy = component.getCreatorUserId(); + String serviceVersion; + if(isInCertificationRequest){ + int tmp = Integer.valueOf(component.getVersion().split("\\.")[0]) + 1; + serviceVersion = String.valueOf(tmp) + ".0"; + } + else{ + serviceVersion = component.getVersion(); + } + + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'"); + format.setTimeZone(TimeZone.getTimeZone("UTC")); + //String createDate = format.format(new Date(component.getCreationDate())); + Date date = new Date(); + String releaseTime = format.format(date); + + if (component.getCategories() == null || component.getCategories().get(0) == null){ + return; + } + + String serviceType = component.getCategories().get(0).getName(); + String description = component.getDescription(); + String serviceTemplate = DEFINITIONS_PATH + fileName; + String hash = GeneralUtility.calculateMD5Base64EncodedByByteArray(mainYaml); + String nsMfBlock0 = createNsMfBlock0(serviceName, createdBy, serviceVersion, releaseTime, serviceType, description, serviceTemplate, hash); + byte[] nsMfBlock0Byte = nsMfBlock0.getBytes(); + zip.putNextEntry(new ZipEntry(Service_Manifest)); + zip.write(nsMfBlock0Byte); + } + } + private Either<ToscaRepresentation, ResponseFormat> fetchToscaRepresentation( Component component, boolean getFromCS, @@ -748,6 +791,24 @@ public class CsarUtils { final String block0template = "TOSCA-Meta-File-Version: %s\nCSAR-Version: %s\nCreated-By: %s\nEntry-Definitions: Definitions/%s\n\nName: csar.meta\nContent-Type: text/plain\n"; return String.format(block0template, metaFileVersion, csarVersion, createdBy, entryDef); } + + private String createNsMfBlock0(String serviceName, String createdBy, String serviceVersion, String releaseTime, + String serviceType, String description, String serviceTemplate, String hash) { + final String block0template = "metadata??\n" + + "ns_product_name: %s\n" + + "ns_provider_id: %s\n" + + "ns_package_version: %s\n" + + //"ns_create_date_time: %s\n" + + "ns_release_data_time: %s\n" + + "ns_type: %s\n" + + "ns_package_description: %s\n\n" + + "Source: %s\n" + + "Algorithm: MD5\n" + + "Hash: %s\n\n"; + return String + .format(block0template, serviceName, createdBy, serviceVersion, releaseTime, serviceType, description, + serviceTemplate, hash); + } /** * Extracts artifacts of VFCs from CSAR diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaUtils.java index 902d789d87..5b03b6378e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaUtils.java @@ -38,7 +38,7 @@ public class ToscaUtils { public static boolean isNotComplexVfc(Component component) { if (ComponentTypeEnum.RESOURCE == component.getComponentType()) { ResourceTypeEnum resourceType = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition()).getResourceType(); - if (ResourceTypeEnum.CVFC == resourceType) { + if (ResourceTypeEnum.CVFC == resourceType ) { return false; } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java new file mode 100644 index 0000000000..02e145ec65 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java @@ -0,0 +1,1890 @@ +/* + + * Copyright (c) 2018 AT&T Intellectual Property. + + * + + * Licensed under the Apache License, Version 2.0 (the "License"); + + * you may not use this file except in compliance with the License. + + * You may obtain a copy of the License at + + * + + * http://www.apache.org/licenses/LICENSE-2.0 + + * + + * Unless required by applicable law or agreed to in writing, software + + * distributed under the License is distributed on an "AS IS" BASIS, + + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + + * See the License for the specific language governing permissions and + + * limitations under the License. + + */ + +package org.openecomp.sdc.be.components.impl; + + +import fj.data.Either; +import io.cucumber.java.hu.Ha; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.collections.map.HashedMap; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.auditing.impl.AuditingManager; +import org.openecomp.sdc.be.components.csar.CsarBusinessLogic; +import org.openecomp.sdc.be.components.csar.CsarInfo; +import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; +import org.openecomp.sdc.be.components.impl.utils.CreateServiceFromYamlParameter; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition; +import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.be.externalapi.servlet.ArtifactExternalServlet; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.impl.ServletUtils; +import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts; +import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.servlets.AbstractValidationsServlet; +import org.openecomp.sdc.be.tosca.CsarUtils; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.exception.ResponseFormat; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +public class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTestSetup { + private final static String DEFAULT_ICON = "defaulticon"; + + @InjectMocks + static ServiceImportBusinessLogic serviceImportBusinessLogic; + @Mock + private ServiceBusinessLogic serviceBusinessLogic; + @Mock + private CsarBusinessLogic csarBusinessLogic; + @Mock + private ToscaOperationFacade toscaOperationFacade; + @Mock + private ServiceImportParseLogic serviceImportParseLogic; + @Mock + ArtifactDefinition artifactDefinition =new ArtifactDefinition(); + + private static UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class); + private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class); + private static ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class)); + private static ServletUtils servletUtils = mock(ServletUtils.class); + private static ResourceImportManager resourceImportManager = mock(ResourceImportManager.class); + private static ArtifactsBusinessLogic artifactsBusinessLogic = mock(ArtifactsBusinessLogic.class); + + private static AbstractValidationsServlet servlet = new ArtifactExternalServlet(userBusinessLogic, + componentInstanceBusinessLogic, componentsUtils, servletUtils, resourceImportManager, artifactsBusinessLogic); + + @Before + public void initMocks() { + MockitoAnnotations.initMocks(this); + when(artifactDefinition.getMandatory()).thenReturn(true); + when(artifactDefinition.getArtifactName()).thenReturn("creatorFullName"); + when(artifactDefinition.getArtifactType()).thenReturn("TOSCA_CSAR"); + + sIB1.setServiceBusinessLogic(serviceBusinessLogic); + sIB1.setCsarBusinessLogic(csarBusinessLogic); + sIB1.setServiceImportParseLogic(serviceImportParseLogic); + sIB1.setToscaOperationFacade(toscaOperationFacade); + + } + + @Test + public void testGetComponentsUtils() { + ComponentsUtils result; + result = serviceImportBusinessLogic.getComponentsUtils(); + } + + @Test + public void testSetComponentsUtils() { + ComponentsUtils componentsUtils = null; + + serviceImportBusinessLogic.setComponentsUtils(componentsUtils); + } + + @Test + public void testCreateService() { + Service oldService = createServiceObject(false); + String payloadName = "valid_vf"; + Map<String, byte[]> payload = crateCsarFromPayload(); + Service newService = createServiceObject(true); + try { + when(serviceBusinessLogic.validateServiceBeforeCreate(newService,user,AuditingActionEnum.CREATE_RESOURCE)) + .thenReturn(Either.left(newService)); + when(toscaOperationFacade.validateCsarUuidUniqueness(payloadName)).thenReturn(StorageOperationStatus.OK); + sIB1.createService(oldService, AuditingActionEnum.CREATE_RESOURCE, user, payload, payloadName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateServiceFromCsar(){ + Service oldService = createServiceObject(false); + String csarUUID = "valid_vf"; + Map<String, byte[]> payload = crateCsarFromPayload(); + try { + sIB1.createServiceFromCsar(oldService, user, payload, csarUUID); + }catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateServiceFromYaml(){ + Service oldService = createServiceObject(false); + String topologyTemplateYaml = getMainTemplateContent("service_import_template.yml");; + String yamlName = "group.yml"; + Map<String, NodeTypeInfo> nodeTypesInfo = new HashedMap(); + CsarInfo csarInfo =getCsarInfo(); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate = new HashMap<>(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + try { + sIB1.createServiceFromYaml(oldService,topologyTemplateYaml,yamlName,nodeTypesInfo, + csarInfo,nodeTypesArtifactsToCreate,true,true,nodeName); + }catch (Exception e){ + e.printStackTrace(); + } + } + + @Test + public void testCreateServiceAndRIsFromYaml(){ + Service oldService = createServiceObject(false); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate = new HashMap<>(); + CreateServiceFromYamlParameter csfyp = getCsfyp(); + try { + sIB1.createServiceAndRIsFromYaml(oldService,false,nodeTypesArtifactsToCreate,true,true,csfyp); + }catch (Exception e){ + e.printStackTrace(); + } + } + + @Test + public void testCreateOrUpdateArtifacts(){ + ArtifactsBusinessLogic.ArtifactOperationEnum operation = ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE; + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + String yamlFileName = "group.yml"; + CsarInfo csarInfo =getCsarInfo(); + Resource preparedResource = createParseResourceObject(false); + preparedResource.setResourceType(ResourceTypeEnum.VF); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle= new HashMap<>(); + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> enumListEnumMap = + new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); + List<ArtifactDefinition> artifactDefinitions = new ArrayList<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactName"); + artifactDefinitions.add(artifactDefinition); + enumListEnumMap.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, + artifactDefinitions); + nodeTypesArtifactsToHandle.put(nodeName,enumListEnumMap); + NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName,nodeTypesArtifactsToHandle); + nodeTypeInfoToUpdateArtifacts.setNodeName(nodeName); + nodeTypeInfoToUpdateArtifacts.setNodeTypesArtifactsToHandle(nodeTypesArtifactsToHandle); + + try { + sIB1.createOrUpdateArtifacts(operation,createdArtifacts,yamlFileName,csarInfo, + preparedResource,nodeTypeInfoToUpdateArtifacts,true,true); + }catch (Exception e){ + e.printStackTrace(); + } + } + + @Test + public void testHandleVfCsarArtifacts(){ + Resource resource = createParseResourceObject(false); + CsarInfo csarInfo = getCsarInfo(); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + ArtifactOperationInfo artifactOperation = new ArtifactOperationInfo(true,true, ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE); + try { + sIB1.handleVfCsarArtifacts(resource,csarInfo,createdArtifacts,artifactOperation,true,true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateOrUpdateSingleNonMetaArtifactToComstants(){ + Resource resource = createParseResourceObject(false); + CsarInfo csarInfo = getCsarInfo(); + ArtifactOperationInfo artifactOperation = new ArtifactOperationInfo(true,true, ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE); + Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactDefinition"); + deploymentArtifacts.put("deploymentArtifacts",artifactDefinition); + resource.setDeploymentArtifacts(deploymentArtifacts); + try { + sIB1.createOrUpdateSingleNonMetaArtifactToComstants(resource,csarInfo,artifactOperation,true,true); + }catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testCreateOrUpdateNonMetaArtifacts(){ + CsarInfo csarInfo = getCsarInfo(); + Resource resource = createParseResourceObject(false); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + ArtifactOperationInfo artifactOperation = new ArtifactOperationInfo(true,true, ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE); + + Either<Resource, ResponseFormat> result = sIB1.createOrUpdateNonMetaArtifacts(csarInfo, resource, + createdArtifacts, true, true, artifactOperation); + assertEquals(result.left().value(),resource); + } + + @Test + public void testFindVfCsarArtifactsToHandle(){ + Resource resource = createParseResourceObject(false); + Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactDefinition"); + deploymentArtifacts.put("deploymentArtifacts",artifactDefinition); + Map<String, ArtifactDefinition> artifacts = new HashMap<>(); + artifacts.put("artifacts",artifactDefinition); + List<GroupDefinition> groups = new ArrayList<>(); + GroupDefinition groupDefinition = new GroupDefinition(); + groupDefinition.setUniqueId("groupDefinitionUniqueId"); + groupDefinition.setName("groupDefinition"); + groups.add(groupDefinition); + resource.setDeploymentArtifacts(deploymentArtifacts); + resource.setArtifacts(artifacts); + resource.setGroups(groups); + List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList = new ArrayList<>(); + + Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, + ResponseFormat> result = sIB1.findVfCsarArtifactsToHandle(resource,artifactPathAndNameList,user); + assertNotNull(result.left().value()); + } + + + @Test + public void testIsNonMetaArtifact() { + ArtifactDefinition artifactDefinition =new ArtifactDefinition(); + artifactDefinition.setMandatory(false); + artifactDefinition.setArtifactName("creatorFullName"); + artifactDefinition.setArtifactType("TOSCA_CSAR"); + + boolean nonMetaArtifact = sIB1.isNonMetaArtifact(artifactDefinition); + assertTrue(nonMetaArtifact); + + } + + @Test + public void testOrganizeVfCsarArtifactsByArtifactOperation(){ + List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList = new ArrayList<>(); + artifactPathAndNameList.add(getNonMetaArtifactInfo()); + List<ArtifactDefinition> existingArtifactsToHandle = new ArrayList<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactName"); + artifactDefinition.setArtifactType(ArtifactTypeEnum.AAI_SERVICE_MODEL.name()); + artifactDefinition.setArtifactChecksum("artifactChecksum"); + existingArtifactsToHandle.add(artifactDefinition); + Resource resource = createParseResourceObject(false); + + Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> + enumMapResponseFormatEither = sIB1.organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifactsToHandle, resource, user); + assertNotNull(enumMapResponseFormatEither.left().value()); + } + + @Test + public void testProcessCsarArtifacts(){ + CsarInfo csarInfo = getCsarInfo(); + Resource resource = createParseResourceObject(false); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Either<Resource, ResponseFormat> resStatus = null; + List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList = new ArrayList<>(); + artifactPathAndNameList.add(getNonMetaArtifactInfo()); + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle = new + EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); + vfCsarArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE,artifactPathAndNameList); + Either<Resource, ResponseFormat> rrfe = sIB1.processCsarArtifacts(csarInfo, + resource, createdArtifacts, true, true, resStatus, vfCsarArtifactsToHandle); + assertNull(rrfe); + } + + @Test + public void testCreateOrUpdateSingleNonMetaArtifact(){ + Resource resource = createParseResourceObject(false); + CsarInfo csarInfo = getCsarInfo(); + Map<String, byte[]> csar = csarInfo.getCsar(); + String rootPath = System.getProperty("user.dir"); + Path path; + byte[] data = new byte[0]; + path = Paths.get(rootPath + "/src/test/resources/valid_vf.csar"); + try { + data = Files.readAllBytes(path); + } catch (IOException e) { + e.printStackTrace(); + } + csar.put("valid_vf.csar",data); + String artifactPath = "valid_vf.csar", artifactFileName = "", artifactType = ""; + ArtifactGroupTypeEnum artifactGroupType = ArtifactGroupTypeEnum.TOSCA; + String artifactLabel = "", artifactDisplayName = "", artifactDescription = "", artifactId = "artifactId"; + ArtifactOperationInfo artifactOperation = new ArtifactOperationInfo(true,true, ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + try { + sIB1.createOrUpdateSingleNonMetaArtifact(resource, csarInfo, artifactPath, artifactFileName, artifactType, artifactGroupType, + artifactLabel, artifactDisplayName, artifactDescription, artifactId, artifactOperation, createdArtifacts, + true, true, true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleNodeTypeArtifacts(){ + Resource nodeTypeResource = createParseResourceObject(false); + ResourceMetadataDataDefinition componentMetadataDataDefinition = new ResourceMetadataDataDefinition(); + componentMetadataDataDefinition.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name()); + ComponentMetadataDefinition componentMetadataDefinition = new ComponentMetadataDefinition(componentMetadataDataDefinition); + nodeTypeResource.setComponentMetadataDefinition(componentMetadataDefinition); + Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> artifactDefinitions = new ArrayList<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactName"); + artifactDefinitions.add(artifactDefinition); + nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, + artifactDefinitions); + + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + try { + sIB1.handleNodeTypeArtifacts(nodeTypeResource, nodeTypeArtifactsToHandle, + createdArtifacts, user, true, true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testCreateOrUpdateServiceArtifacts(){ + ArtifactsBusinessLogic.ArtifactOperationEnum operation = ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE; + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + String yamlFileName = "group.yml"; + CsarInfo csarInfo =getCsarInfo(); + Service preparedService = createServiceObject(false); + Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactDefinition"); + deploymentArtifacts.put("deploymentArtifacts",artifactDefinition); + preparedService.setDeploymentArtifacts(deploymentArtifacts); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle= new HashMap<>(); + NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName,nodeTypesArtifactsToHandle); + + try { + sIB1.createOrUpdateArtifacts(operation,createdArtifacts,yamlFileName,csarInfo, + preparedService,nodeTypeInfoToUpdateArtifacts,true,true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleVfCsarServiceArtifacts(){ + Service service = createServiceObject(true); + Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName(Constants.VENDOR_LICENSE_MODEL); + artifactDefinition.setUniqueId("uniqueId"); + deploymentArtifacts.put("deploymentArtifacts",artifactDefinition); + service.setDeploymentArtifacts(deploymentArtifacts); + CsarInfo csarInfo = getCsarInfo(); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + ArtifactOperationInfo artifactOperation = new ArtifactOperationInfo(true,true, ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE); + try { + sIB1.handleVfCsarArtifacts(service,csarInfo,createdArtifacts,artifactOperation,true,true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateOrUpdateNonMetaServiceArtifacts(){ + CsarInfo csarInfo = getCsarInfo(); + Service service = createServiceObject(true); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + ArtifactOperationInfo artifactOperation = new ArtifactOperationInfo(true,true, ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE); + + Either<Service, ResponseFormat> result = sIB1.createOrUpdateNonMetaArtifacts(csarInfo, + service, createdArtifacts, true, true, artifactOperation); + assertEquals(result.left().value(),service); + } + + @Test + public void testFindServiceCsarArtifactsToHandle(){ + Service service = createServiceObject(true); + Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactDefinition"); + deploymentArtifacts.put("deploymentArtifacts",artifactDefinition); + Map<String, ArtifactDefinition> artifacts = new HashMap<>(); + artifacts.put("artifacts",artifactDefinition); + List<GroupDefinition> groups = new ArrayList<>(); + GroupDefinition groupDefinition = new GroupDefinition(); + groupDefinition.setUniqueId("groupDefinitionUniqueId"); + groupDefinition.setName("groupDefinition"); + groups.add(groupDefinition); + service.setDeploymentArtifacts(deploymentArtifacts); + service.setArtifacts(artifacts); + service.setGroups(groups); + List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList = new ArrayList<>(); + + Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, + ResponseFormat> result = sIB1.findVfCsarArtifactsToHandle(service, artifactPathAndNameList, user); + assertNotNull(result.left().value()); + } + + @Test + public void testOrganizeVfCsarArtifactsByServiceArtifactOperation(){ + List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList = new ArrayList<>(); + artifactPathAndNameList.add(getNonMetaArtifactInfo()); + List<ArtifactDefinition> existingArtifactsToHandle = new ArrayList<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactName"); + artifactDefinition.setArtifactType(ArtifactTypeEnum.AAI_SERVICE_MODEL.name()); + artifactDefinition.setArtifactChecksum("artifactChecksum"); + existingArtifactsToHandle.add(artifactDefinition); + Service service = createServiceObject(true); + + Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> + enumMapResponseFormatEither = sIB1.organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, + existingArtifactsToHandle, service, user); + assertNotNull(enumMapResponseFormatEither.left().value()); + } + + @Test + public void testProcessServiceCsarArtifacts(){ + CsarInfo csarInfo = getCsarInfo(); + Service service = createServiceObject(true); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Either<Service, ResponseFormat> resStatus = null; + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle = new + EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); + Either<Service, ResponseFormat> srfe = sIB1.processCsarArtifacts(csarInfo, + service, createdArtifacts, true, true, resStatus, vfCsarArtifactsToHandle); + assertNull(srfe); + } + + @Test + public void testGetValidArtifactNames(){ + CsarInfo csarInfo = getCsarInfo(); + Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>(); + Either<List<CsarUtils.NonMetaArtifactInfo>, String> result = sIB1.getValidArtifactNames(csarInfo, collectedWarningMessages); + System.out.println(result.left().value()); + assertNotNull(result.left().value()); + } + + @Test + public void testCreateOrUpdateSingleNonMetaServiceArtifact(){ + Service service = createServiceObject(true); + CsarInfo csarInfo = getCsarInfo(); + String rootPath = System.getProperty("user.dir"); + Path path = Paths.get(rootPath + "/src/test/resources/valid_vf.csar"); + String artifactPath = path.toString(), artifactFileName = "", artifactType = ""; + ArtifactGroupTypeEnum artifactGroupType = ArtifactGroupTypeEnum.TOSCA; + String artifactLabel = "", artifactDisplayName = "", artifactDescription = "", artifactId = "artifactId"; + ArtifactOperationInfo artifactOperation = new ArtifactOperationInfo(true,true, ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + try { + sIB1.createOrUpdateSingleNonMetaArtifact(service, csarInfo, artifactPath, artifactFileName, artifactType, artifactGroupType, + artifactLabel, artifactDisplayName, artifactDescription, artifactId, artifactOperation, createdArtifacts, + true, true, true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateGroupsOnResource(){ + Service service = createServiceObject(true); + Map<String, GroupDefinition> groups = getGroups(); + + try { + Either<Service, ResponseFormat> result = sIB1.createGroupsOnResource(service, groups); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testUpdateGroupsMembersUsingResource(){ + Service service = createServiceObject(true); + Map<String, GroupDefinition> groups = null; + + List<GroupDefinition> groupDefinitions = sIB1.updateGroupsMembersUsingResource(groups, service); + for (GroupDefinition groupDefinition : groupDefinitions) { + assertNull(groupDefinition); + } + } + + @Test + public void testCreateRIAndRelationsFromResourceYaml(){ + String yamlName = "group.yml"; + Resource resource = createParseResourceObject(true); + Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = new HashMap<>(); + String topologyTemplateYaml = getMainTemplateContent(); + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + CsarInfo csarInfo = getCsarInfo(); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate = new HashMap<>(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + + try { + sIB1.createRIAndRelationsFromYaml(yamlName,resource,uploadComponentInstanceInfoMap,topologyTemplateYaml,nodeTypesNewCreatedArtifacts, + nodeTypesInfo,csarInfo,nodeTypesArtifactsToCreate,nodeName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testCreateResourceInstancesRelations(){ + String yamlName = "group.yml"; + Resource resource = createParseResourceObject(true); + resource.setComponentInstances(creatComponentInstances()); + resource.setResourceType(ResourceTypeEnum.VF); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + uploadResInstancesMap.put("uploadResInstancesMap",getuploadComponentInstanceInfo()); + + try { + sIB1.createResourceInstancesRelations(user,yamlName,resource,uploadResInstancesMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testProcessComponentInstance1(){ + String yamlName = "group.yml"; + Resource resource = createParseResourceObject(true); + Resource originResource = createParseResourceObject(false); + List<ComponentInstance> componentInstancesList = creatComponentInstances(); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = null; + Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>(); + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties = new HashMap<>(); + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>(); + Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>(); + Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>(); + Map<String, List< AttributeDataDefinition >> instAttributes = new HashMap<>(); + Map<String, Resource> originCompMap = new HashMap<>(); + originCompMap.put("componentUid",originResource); + Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>(); + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + uploadComponentInstanceInfo.setName("zxjTestImportServiceAb"); + + try { + sIB1.processComponentInstance(yamlName, resource, componentInstancesList,allDataTypes,instProperties,instCapabilties,instRequirements, + instDeploymentArtifacts,instArtifacts,instAttributes,originCompMap,instInputs,uploadComponentInstanceInfo); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testAddInputsValuesToRi(){ + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + Map<String, List<UploadPropInfo>> properties = new HashMap<>(); + List<UploadPropInfo> uploadPropInfoList = new ArrayList<>(); + UploadPropInfo uploadPropInfo = new UploadPropInfo(); + uploadPropInfo.setName("uploadPropInfo"); + uploadPropInfoList.add(uploadPropInfo); + uploadPropInfoList.add(uploadPropInfo); + properties.put("propertiesMap",uploadPropInfoList); + uploadComponentInstanceInfo.setProperties(properties); + Resource resource = createParseResourceObject(true); + Resource originResource =createParseResourceObject(false); + List<InputDefinition> inputs = new ArrayList<>(); + InputDefinition inputDefinition = new InputDefinition(); + inputDefinition.setUniqueId("uniqueId"); + inputs.add(inputDefinition); + originResource.setInputs(inputs); + ComponentInstance currentCompInstance = new ComponentInstance(); + Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>(); + Map<String, DataTypeDefinition> allDataTypes = new HashMap<>(); + + try { + sIB1.addInputsValuesToRi(uploadComponentInstanceInfo,resource,originResource, + currentCompInstance,instInputs,allDataTypes); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testProcessProperty(){ + Resource resource = createParseResourceObject(true); + List<InputDefinition> inputs = new ArrayList<>(); + InputDefinition inputDefinition = new InputDefinition(); + inputDefinition.setUniqueId("uniqueId"); + inputs.add(inputDefinition); + resource.setInputs(inputs); + ComponentInstance currentCompInstance = null; + Map<String, DataTypeDefinition> allDataTypes = new HashMap<>(); + Map<String, InputDefinition> currPropertiesMap = new HashMap<>(); + inputDefinition.setType("inputDefinitionType"); + currPropertiesMap.put("propertyInfoName",inputDefinition); + List<ComponentInstanceInput> instPropList = new ArrayList<>(); + List<UploadPropInfo> propertyList = getPropertyList(); + + try { + sIB1.processProperty(resource, currentCompInstance, allDataTypes, + currPropertiesMap, instPropList, propertyList); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleSubstitutionMappings(){ + Resource resource = createParseResourceObject(true); + resource.setResourceType(ResourceTypeEnum.VF); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + + try { + sIB1.handleSubstitutionMappings(resource, uploadResInstancesMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateResourceInstances(){ + String yamlName = "group.yml"; + Resource resource = createParseResourceObject(true); + Resource originResource = createParseResourceObject(false); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + UploadComponentInstanceInfo nodesInfoValue = new UploadComponentInstanceInfo(); + nodesInfoValue.setName("zxjTestImportServiceAb"); + nodesInfoValue.setRequirements(gerRequirements()); + uploadResInstancesMap.put("uploadComponentInstanceInfo", nodesInfoValue); + Map<String, Resource> nodeNamespaceMap = new HashMap<>(); + nodeNamespaceMap.put("resources",originResource); + + try { + sIB1.createResourceInstances(yamlName,resource,uploadResInstancesMap,nodeNamespaceMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleNodeTypes(){ + String yamlName = "group.yml"; + Resource resource = createParseResourceObject(true); + String topologyTemplateYaml = getMainTemplateContent(); + boolean needLock = true; + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); + nodeTypeInfo.setNested(false); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + nodeTypesInfo.put(nodeName,nodeTypeInfo); + CsarInfo csarInfo = getCsarInfo(); + + try { + sIB1.handleNodeTypes(yamlName,resource,topologyTemplateYaml,needLock, + nodeTypesArtifactsToHandle,nodeTypesNewCreatedArtifacts,nodeTypesInfo, + csarInfo,nodeName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleNestedVfc1(){ + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + Resource resource = createParseResourceObject(false); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodesInfo = new HashMap<>(); + NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); + nodeTypeInfo.setTemplateFileName("groups.yml"); + nodeTypeInfo.setMappedToscaTemplate(new HashMap<>()); + nodesInfo.put(nodeName,nodeTypeInfo); + CsarInfo csarInfo = getCsarInfo(); + + try { + sIB1.handleNestedVfc(resource,nodeTypesArtifactsToHandle,createdArtifacts, + nodesInfo,csarInfo,nodeName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleComplexVfc1(){ + Resource resource = createParseResourceObject(true); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodesInfo = new HashMap<>(); + CsarInfo csarInfo = getCsarInfo(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + String yamlName = "group.yml"; + + try { + sIB1.handleComplexVfc(resource,nodeTypesArtifactsToHandle,createdArtifacts, + nodesInfo,csarInfo,nodeName,yamlName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateResourcesFromYamlNodeTypesList1(){ + String yamlName = "group.yml"; + Resource resource = createParseResourceObject(false); + Map<String, Object> mappedToscaTemplate = new HashMap<>(); + boolean needLock = true; + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + CsarInfo csarInfo = getCsarInfo(); + + try { + sIB1.createResourcesFromYamlNodeTypesList(yamlName,resource,mappedToscaTemplate, + needLock, nodeTypesArtifactsToHandle,nodeTypesNewCreatedArtifacts, + nodeTypesInfo,csarInfo); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateNodeTypes1(){ + String yamlName = "group.yml"; + Resource resource = createParseResourceObject(false); + boolean needLock = true; + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> enumListEnumMap = + new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); + List<ArtifactDefinition> artifactDefinitions = new ArrayList<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactName"); + artifactDefinitions.add(artifactDefinition); + enumListEnumMap.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, + artifactDefinitions); + nodeTypesArtifactsToHandle.put("nodeTyp",enumListEnumMap); + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); + nodeTypesInfo.put("nodeTyp",nodeTypeInfo); + CsarInfo csarInfo = getCsarInfo(); + Map<String, Object> mapToConvert =new HashMap<>(); + Map<String, Object> nodeTypes =new HashMap<>(); + nodeTypes.put("nodeTyp",nodeTypeInfo); + + try { + sIB1.createNodeTypes(yamlName, resource, needLock, nodeTypesArtifactsToHandle, + nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert, + nodeTypes); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateNodeTypeResourceFromYaml(){ + String yamlName = "group.yml"; + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + Map<String,Object> nodeMap = new HashMap<>(); + nodeMap.put(nodeName,getGroupsYaml()); + Map.Entry<String, Object> nodeNameValue = nodeMap.entrySet().iterator().next(); + Map<String, Object> mapToConvert = new HashedMap(); + Resource resourceVf = createParseResourceObject(false); + boolean needLock = true; + Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + boolean forceCertificationAllowed = true; + CsarInfo csarInfo = getCsarInfo(); + boolean isNested = true; + + try { + sIB1.createNodeTypeResourceFromYaml(yamlName,nodeNameValue,user,mapToConvert,resourceVf, + needLock,nodeTypeArtifactsToHandle,nodeTypesNewCreatedArtifacts, + forceCertificationAllowed,csarInfo,isNested); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateRIAndRelationsFromYaml(){ + String yamlName = "group.yml"; + Service service = createServiceObject(true); + Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = new HashMap<>(); + String topologyTemplateYaml = getMainTemplateContent("service_import_template.yml");; + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + CsarInfo csarInfo = getCsarInfo(); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate = new HashMap<>(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + try { + sIB1.createRIAndRelationsFromYaml(yamlName,service,uploadComponentInstanceInfoMap,topologyTemplateYaml,nodeTypesNewCreatedArtifacts, + nodeTypesInfo,csarInfo,nodeTypesArtifactsToCreate,nodeName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateServiceInstancesRelations(){ + String yamlName = "group.yml"; + Service service = createServiceObject(true); + service.setComponentInstances(creatComponentInstances()); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + uploadResInstancesMap.put("uploadResInstancesMap",getuploadComponentInstanceInfo()); + + try { + sIB1.createServiceInstancesRelations(user, yamlName, service, uploadResInstancesMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testProcessComponentInstance(){ + String yamlName = "group.yml"; + Service service = createServiceObject(true); + Resource originResource = createParseResourceObject(false); + List<ComponentInstance> componentInstancesList = creatComponentInstances(); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = null; + Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>(); + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties = new HashMap<>(); + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>(); + Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>(); + Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>(); + Map<String, List< AttributeDataDefinition >> instAttributes = new HashMap<>(); + Map<String, Resource> originCompMap = new HashMap<>(); + originCompMap.put("componentUid",originResource); + Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>(); + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + uploadComponentInstanceInfo.setName("zxjTestImportServiceAb"); + + try { + sIB1.processComponentInstance(yamlName, service, componentInstancesList,allDataTypes,instProperties,instCapabilties,instRequirements, + instDeploymentArtifacts,instArtifacts,instAttributes,originCompMap,instInputs,uploadComponentInstanceInfo); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testAddInputsValuesToRi2(){ + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + Map<String, List<UploadPropInfo>> properties = new HashMap<>(); + List<UploadPropInfo> uploadPropInfoList = new ArrayList<>(); + UploadPropInfo uploadPropInfo = new UploadPropInfo(); + uploadPropInfo.setName("uploadPropInfo"); + uploadPropInfoList.add(uploadPropInfo); + uploadPropInfoList.add(uploadPropInfo); + properties.put("propertiesMap",uploadPropInfoList); + uploadComponentInstanceInfo.setProperties(properties); + Service resource = createServiceObject(true); + Resource originResource =createParseResourceObject(false); + List<InputDefinition> inputs = new ArrayList<>(); + InputDefinition inputDefinition = new InputDefinition(); + inputDefinition.setUniqueId("uniqueId"); + inputs.add(inputDefinition); + originResource.setInputs(inputs); + ComponentInstance currentCompInstance = new ComponentInstance(); + Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>(); + Map<String, DataTypeDefinition> allDataTypes = new HashMap<>(); + + try { + sIB1.addInputsValuesToRi(uploadComponentInstanceInfo,resource,originResource, + currentCompInstance,instInputs,allDataTypes); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testProcessProperty2(){ + Service resource = createServiceObject(true); + ComponentInstance currentCompInstance = null; + Map<String, DataTypeDefinition> allDataTypes = new HashMap<>(); + Map<String, InputDefinition> currPropertiesMap = new HashMap<>(); + InputDefinition inputDefinition = new InputDefinition(); + inputDefinition.setType("inputDefinitionType"); + currPropertiesMap.put("propertyInfoName",inputDefinition); + List<ComponentInstanceInput> instPropList = new ArrayList<>(); + List<UploadPropInfo> propertyList = new ArrayList<>(); + List<GetInputValueDataDefinition> get_input = new ArrayList<>(); + GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition(); + getInputValueDataDefinition.setPropName("getInputValueDataDefinitionName"); + get_input.add(getInputValueDataDefinition); + UploadPropInfo propertyInfo = new UploadPropInfo(); + propertyInfo.setValue("value"); + propertyInfo.setGet_input(get_input); + propertyInfo.setName("propertyInfoName"); + propertyList.add(propertyInfo); + + try { + sIB1.processProperty(resource, currentCompInstance, allDataTypes, + currPropertiesMap, instPropList, propertyList); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testProcessGetInput(){ + List<GetInputValueDataDefinition> getInputValues = new ArrayList<>(); + List<InputDefinition> inputs = new ArrayList<>(); + GetInputValueDataDefinition getInputIndex = new GetInputValueDataDefinition(); + + try { + sIB1.processGetInput(getInputValues,inputs,getInputIndex); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testAddPropertyValuesToRi(){ + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + uploadComponentInstanceInfo.setProperties(getUploadPropInfoProperties()); + Resource resource = createParseResourceObject(true); + Resource originResource = createParseResourceObject(false); + originResource.setProperties(getProperties()); + ComponentInstance currentCompInstance = new ComponentInstance(); + Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>(); + Map<String, DataTypeDefinition> allDataTypes = new HashMap<>(); + try { + sIB1.addPropertyValuesToRi(uploadComponentInstanceInfo, resource, originResource, currentCompInstance, + instProperties, allDataTypes); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testAddPropertyValuesToRi2(){ + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + uploadComponentInstanceInfo.setProperties(getUploadPropInfoProperties()); + Service service = createServiceObject(false); + Resource originResource = createParseResourceObject(false); + originResource.setProperties(getProperties()); + ComponentInstance currentCompInstance = new ComponentInstance(); + Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>(); + Map<String, DataTypeDefinition> allDataTypes = new HashMap<>(); + + try { + sIB1.addPropertyValuesToRi(uploadComponentInstanceInfo, service, originResource, currentCompInstance, + instProperties, allDataTypes); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testProcessComponentInstanceCapabilities(){ + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = null; + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties = new HashMap<>(); + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + uploadComponentInstanceInfo.setProperties(getUploadPropInfoProperties()); Map<String, List<UploadPropInfo>> properties = new HashMap<>(); + uploadComponentInstanceInfo.setProperties(properties); + ComponentInstance currentCompInstance = new ComponentInstance(); + Resource originResource = createParseResourceObject(false); + + sIB1.processComponentInstanceCapabilities(allDataTypes, instCapabilties, uploadComponentInstanceInfo, + currentCompInstance, originResource); + } + + @Test + public void testUpdateCapabilityPropertiesValues(){ + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = null; + Map<String, List<CapabilityDefinition>> originCapabilities = new HashMap<>(); + Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>(); + + sIB1.updateCapabilityPropertiesValues(allDataTypes, originCapabilities, newPropertiesMap); + } + + @Test + public void testUpdatePropertyValues(){ + List<ComponentInstanceProperty> properties = new ArrayList<>(); + Map<String, UploadPropInfo> newProperties = new HashMap<>(); + Map<String, DataTypeDefinition> allDataTypes = new HashMap<>(); + + sIB1.updatePropertyValues(properties,newProperties,allDataTypes); + } + + @Test + public void testUpdatePropertyValue(){ + ComponentInstanceProperty property = new ComponentInstanceProperty(); + property.setType("services"); + UploadPropInfo propertyInfo = new UploadPropInfo(); + propertyInfo.setValue("value"); + Map<String, DataTypeDefinition> allDataTypes = new HashMap<>(); + + try { + sIB1.updatePropertyValue(property,propertyInfo,allDataTypes); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testGetOriginResource(){ + String yamlName = "group.yml"; + Map<String, Resource> originCompMap = new HashMap<>(); + ComponentInstance currentCompInstance = new ComponentInstance(); + currentCompInstance.setComponentUid("currentCompInstance"); + + try { + sIB1.getOriginResource(yamlName,originCompMap,currentCompInstance); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleSubstitutionMappings2(){ + Service service = createServiceObject(false); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + + sIB1.handleSubstitutionMappings(service, uploadResInstancesMap); + } + + @Test + public void testUpdateCalculatedCapReqWithSubstitutionMappings(){ + Resource resource = createParseResourceObject(false); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + + when(toscaOperationFacade.deleteAllCalculatedCapabilitiesRequirements(any())).thenReturn(StorageOperationStatus.OK); + + try { + sIB1.updateCalculatedCapReqWithSubstitutionMappings(resource,uploadResInstancesMap); + } catch (Exception e) { + + } + } + + @Test + public void testFillUpdatedInstCapabilitiesRequirements(){ + List<ComponentInstance> componentInstances = creatComponentInstances(); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = getUploadResInstancesMap(); + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>(); + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirement = new HashMap<>(); + + sIB1.fillUpdatedInstCapabilitiesRequirements(componentInstances,uploadResInstancesMap, + updatedInstCapabilities,updatedInstRequirement); + } + + @Test + public void testFillUpdatedInstCapabilities(){ + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties = new HashMap<>(); + Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>(); + List<CapabilityDefinition> capabilityDefinitionList = new ArrayList<>(); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setName("mme_ipu_vdu.feature"); + capabilityDefinitionList.add(capabilityDefinition); + capabilities.put("tosca.capabilities.Node",capabilityDefinitionList); + ComponentInstance instance = new ComponentInstance(); + instance.setCapabilities(capabilities); + Map<String, String> capabilitiesNamesToUpdate = new HashMap<>(); + capabilitiesNamesToUpdate.put("mme_ipu_vdu.feature","capabilitiesNamesToUpdate"); + + sIB1.fillUpdatedInstCapabilities(updatedInstCapabilties,instance,capabilitiesNamesToUpdate); + } + + @Test + public void testFillUpdatedInstRequirements(){ + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new + HashMap<>(); + ComponentInstance instance = new ComponentInstance(); + Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); + List<RequirementDefinition> requirementDefinitionList = new ArrayList<>(); + RequirementDefinition requirementDefinition = new RequirementDefinition(); + requirementDefinition.setName("zxjtestimportserviceab0.mme_ipu_vdu.dependency.test"); + requirementDefinitionList.add(requirementDefinition); + requirements.put("tosca.capabilities.Node",requirementDefinitionList); + instance.setRequirements(requirements); + Map<String, String> requirementsNamesToUpdate = new HashMap<>(); + requirementsNamesToUpdate.put("zxjtestimportserviceab0.mme_ipu_vdu.dependency.test", + "requirementsNamesToUpdate"); + + + sIB1.fillUpdatedInstRequirements(updatedInstRequirements,instance,requirementsNamesToUpdate); + } + + @Test + public void testAddRelationsToRI(){ + String yamlName = "group.yml"; + Service service = createServiceObject(false); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + UploadComponentInstanceInfo nodesInfoValue = getuploadComponentInstanceInfo(); + uploadResInstancesMap.put("uploadComponentInstanceInfo", nodesInfoValue); + List<ComponentInstance> componentInstancesList = creatComponentInstances(); + List<RequirementCapabilityRelDef> relations = new ArrayList<>(); + + try { + sIB1.addRelationsToRI(yamlName,service,uploadResInstancesMap,componentInstancesList, + relations); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testAddRelationToRI(){ + String yamlName = "group.yml"; + Service service = createServiceObject(false); + service.setComponentInstances(creatComponentInstances()); + + UploadComponentInstanceInfo nodesInfoValue = getuploadComponentInstanceInfo(); + List<RequirementCapabilityRelDef> relations = new ArrayList<>(); + + try { + sIB1.addRelationToRI(yamlName,service,nodesInfoValue,relations); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testGetResourceAfterCreateRelations(){ + Service service = createServiceObject(false); + ComponentParametersView componentParametersView = createComponentParametersView(); + when(serviceImportParseLogic.getComponentFilterAfterCreateRelations()).thenReturn(componentParametersView); + try { + sIB1.getResourceAfterCreateRelations(service); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateServiceInstances(){ + String yamlName = "group.yml"; + Service service = createServiceObject(false); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + UploadComponentInstanceInfo nodesInfoValue = getuploadComponentInstanceInfo(); + uploadResInstancesMap.put("uploadResInstancesMap",nodesInfoValue); + Map<String, Resource> nodeNamespaceMap = new HashMap<>(); + Resource resource = createParseResourceObject(true); + resource.setToscaResourceName("toscaResourceName"); + nodeNamespaceMap.put("nodeNamespaceMap",resource); + + try { + sIB1.createServiceInstances(yamlName,service,uploadResInstancesMap,nodeNamespaceMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateAndAddResourceInstance(){ + UploadComponentInstanceInfo uploadComponentInstanceInfo = getuploadComponentInstanceInfo(); + String yamlName = "group.yml"; + Resource resource = createParseResourceObject(false); + Resource originResource = createParseResourceObject(true); + originResource.setResourceType(ResourceTypeEnum.VF); + Map<String, Resource> nodeNamespaceMap = new HashMap<>(); + nodeNamespaceMap.put("resources",originResource); + Map<String, Resource> existingnodeTypeMap = new HashMap<>(); + Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>(); + + try { + sIB1.createAndAddResourceInstance(uploadComponentInstanceInfo,yamlName,resource,nodeNamespaceMap, + existingnodeTypeMap,resourcesInstancesMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateAndAddResourceInstances(){ + UploadComponentInstanceInfo uploadComponentInstanceInfo = getuploadComponentInstanceInfo(); + String yamlName = "group.yml"; + Service service = createServiceObject(false); + service.setServiceType("services"); + Resource originResource = createParseResourceObject(true); + originResource.setResourceType(ResourceTypeEnum.VF); + Map<String, Resource> nodeNamespaceMap = new HashMap<>(); + nodeNamespaceMap.put("resources", originResource); + Map<String, Resource> existingnodeTypeMap = new HashMap<>(); + Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>(); + + try { + sIB1.createAndAddResourceInstance(uploadComponentInstanceInfo, yamlName, service, nodeNamespaceMap, + existingnodeTypeMap, resourcesInstancesMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testValidateResourceInstanceBeforeCreate(){ + String yamlName = "group.yml"; + UploadComponentInstanceInfo uploadComponentInstanceInfo = getuploadComponentInstanceInfo(); + Resource originResource = createParseResourceObject(true); + originResource.setResourceType(ResourceTypeEnum.VF); + Map<String, Resource> nodeNamespaceMap = new HashMap<>(); + nodeNamespaceMap.put("resources", originResource); + + try { + sIB1.validateResourceInstanceBeforeCreate(yamlName,uploadComponentInstanceInfo,nodeNamespaceMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleServiceNodeTypes(){ + String yamlName = "group.yml"; + Service service =createServiceObject(false); + String topologyTemplateYaml = getMainTemplateContent("service_import_template.yml");; + boolean needLock = true; + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodeTypesInfo = getNodeTypesInfo(); + CsarInfo csarInfo = getCsarInfo(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + + try { + sIB1.handleServiceNodeTypes(yamlName,service,topologyTemplateYaml,needLock, + nodeTypesArtifactsToHandle,nodeTypesNewCreatedArtifacts,nodeTypesInfo, + csarInfo,nodeName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testValidateResourceNotExisted(){ + String type = "org.openecomp.resource.vf"; + boolean b = false; + try { + b = sIB1.validateResourceNotExisted(type); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleNestedVF(){ + Service service =createServiceObject(false); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodesInfo = getNodeTypesInfo(); + CsarInfo csarInfo = getCsarInfo(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + + try { + sIB1.handleNestedVF(service,nodeTypesArtifactsToHandle,createdArtifacts, + nodesInfo,csarInfo,nodeName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleNestedVfc(){ + Service service =createServiceObject(false); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodesInfo = new HashMap<>(); + CsarInfo csarInfo = getCsarInfo(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + + try { + sIB1.handleNestedVfc(service,nodeTypesArtifactsToHandle,createdArtifacts, + nodesInfo,csarInfo,nodeName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleComplexVfc(){ + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodesInfo = new HashMap<>(); + CsarInfo csarInfo = getCsarInfo(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + String yamlName = "group.yml"; + + try { + sIB1.handleComplexVfc(nodeTypesArtifactsToHandle,createdArtifacts, + nodesInfo,csarInfo,nodeName,yamlName); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testHandleComplexVfc2(){ + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodesInfo = getNodeTypesInfo(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + String yamlName = "group.yml"; + CsarInfo csarInfo = getCsarInfo(); + Map<String, byte[]> csar = new HashMap<>(); + csar.put(yamlName,yamlName.getBytes()); + csarInfo.setCsar(csar); + Resource oldComplexVfc = createParseResourceObject(false); + Resource newComplexVfc = createParseResourceObject(true); + + try { + sIB1.handleComplexVfc(nodeTypesArtifactsToHandle,createdArtifacts,nodesInfo, + csarInfo,nodeName,yamlName,oldComplexVfc,newComplexVfc); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testUpdateResourceFromYaml(){ + Resource oldRresource = createParseResourceObject(false); + Resource newRresource = createParseResourceObject(true); + AuditingActionEnum actionEnum = AuditingActionEnum.CREATE_RESOURCE; + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + String yamlFileName = "group.yml"; + String yamlFileContent = getYamlFileContent(); + CsarInfo csarInfo = getCsarInfo(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + boolean isNested = true; + + try { + sIB1.updateResourceFromYaml(oldRresource,newRresource,actionEnum,createdArtifacts,yamlFileName,yamlFileContent, + csarInfo,nodeTypesInfo,nodeTypesArtifactsToHandle,nodeName,isNested); + } catch (Exception e) { + + } + } + + @Test + public void testCreateResourceFromYaml(){ + Resource resource = createParseResourceObject(true); + String topologyTemplateYaml = getMainTemplateContent(); + String yamlName = "group.yml"; + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + CsarInfo csarInfo = getCsarInfo(); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate = new HashMap<>(); + boolean shouldLock = true; + boolean inTransaction =true; + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + + try { + sIB1.createResourceFromYaml(resource,topologyTemplateYaml,yamlName,nodeTypesInfo,csarInfo, + nodeTypesArtifactsToCreate,shouldLock,inTransaction,nodeName); + } catch (Exception e) { + + } + } + + @Test + public void testCreateResourceAndRIsFromYaml(){ + String yamlName = "group.yml"; + Resource resource = createParseResourceObject(false); + ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo(); + AuditingActionEnum actionEnum = AuditingActionEnum.CREATE_RESOURCE; + boolean isNormative = true; + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + String topologyTemplateYaml = getMainTemplateContent(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + CsarInfo csarInfo = getCsarInfo(); + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate = new HashMap<>(); + boolean shouldLock = true; + boolean inTransaction = true; + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + + try { + sIB1.createResourceAndRIsFromYaml(yamlName,resource,parsedToscaYamlInfo,actionEnum, + isNormative,createdArtifacts,topologyTemplateYaml,nodeTypesInfo,csarInfo, + nodeTypesArtifactsToCreate,shouldLock,inTransaction,nodeName); + } catch (Exception e) { + + } + } + + @Test + public void testCreateGroupsOnResource2(){ + Resource resource = createParseResourceObject(false); + Map<String, GroupDefinition> groups = new HashMap<>(); + + Either<Resource, ResponseFormat> result = sIB1.createGroupsOnResource(resource, groups); + assertEquals(result.left().value(),resource); + } + + @Test + public void testUpdateGroupsMembersUsingResource2(){ + Resource resource = createParseResourceObject(true); + Map<String, GroupDefinition> groups = null; + + List<GroupDefinition> groupDefinitions = sIB1.updateGroupsMembersUsingResource(groups,resource); + for (GroupDefinition groupDefinition : groupDefinitions) { + assertNull(groupDefinition); + } + } + + @Test + public void testUpdateGroupMembers(){ + Map<String, GroupDefinition> groups = new HashMap<>(); + GroupDefinition updatedGroupDefinition = new GroupDefinition(); + Resource component = createParseResourceObject(true); + List<ComponentInstance> componentInstances = creatComponentInstances(); + String groupName = "tosca_simple_yaml_1_1"; + Map<String, String> members = new HashMap<>(); + members.put("zxjTestImportServiceAb",getGroupsYaml()); + + try { + sIB1.updateGroupMembers(groups,updatedGroupDefinition,component,componentInstances, + groupName,members); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void setCreateResourceTransaction(){ + Resource resource = createParseResourceObject(false); + resource.setComponentType(ComponentTypeEnum.RESOURCE); + boolean isNormative = true; + + try { + sIB1.createResourceTransaction(resource,user,isNormative); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testUpdateExistingResourceByImport(){ + Resource newResource = createParseResourceObject(false); + Resource oldResource = createParseResourceObject(true); + + try { + sIB1.updateExistingResourceByImport(newResource,oldResource,user, + true,true,true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateResourcesFromYamlNodeTypesList(){ + String yamlName = "group.yml"; + Service service =createServiceObject(false); + Map<String, Object> mappedToscaTemplate = new HashMap<>(); + boolean needLock = true; + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + CsarInfo csarInfo = getCsarInfo(); + + try { + sIB1.createResourcesFromYamlNodeTypesList(yamlName,service,mappedToscaTemplate,needLock, + nodeTypesArtifactsToHandle,nodeTypesNewCreatedArtifacts,nodeTypesInfo,csarInfo); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateNodeTypes(){ + String yamlName = "group.yml"; + Service service =createServiceObject(false); + boolean needLock = true; + Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> enumListEnumMap = + new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class); + List<ArtifactDefinition> artifactDefinitions = new ArrayList<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setArtifactName("artifactName"); + artifactDefinitions.add(artifactDefinition); + enumListEnumMap.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, + artifactDefinitions); + nodeTypesArtifactsToHandle.put("nodeTyp",enumListEnumMap); List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>(); + Map<String, NodeTypeInfo> nodeTypesInfo = getNodeTypesInfo(); + CsarInfo csarInfo = getCsarInfo(); + Map<String, Object> mapToConvert =new HashMap<>(); + Map<String, Object> nodeTypes =new HashMap<>(); + NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); + nodeTypesInfo.put("nodeTyp",nodeTypeInfo); + nodeTypes.put("org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test", + nodeTypeInfo); + + try { + sIB1.createNodeTypes(yamlName, service, needLock, nodeTypesArtifactsToHandle, + nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert, + nodeTypes); + } catch (Exception e) { + e.printStackTrace(); + } + } + + protected List<UploadPropInfo> getPropertyList() { + List<UploadPropInfo> propertyList = new ArrayList<>(); + UploadPropInfo uploadPropInfo = new UploadPropInfo(); + List<GetInputValueDataDefinition> get_input = new ArrayList<>(); + GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition(); + getInputValueDataDefinition.setPropName("getInputValueDataDefinitionName"); + get_input.add(getInputValueDataDefinition); + uploadPropInfo.setName("propertiesName"); + uploadPropInfo.setValue("value"); + uploadPropInfo.setGet_input(get_input); + propertyList.add(uploadPropInfo); + return propertyList; + } + + protected Map<String, GroupDefinition> getGroups() { + Map<String, GroupDefinition> groups = new HashMap<>(); + GroupDefinition groupDefinition = new GroupDefinition(); + groupDefinition.setName("groupDefinitionName"); + groups.put("groupsMap",groupDefinition); + return groups; + } + + protected Map<String, NodeTypeInfo> getNodeTypesInfo(){ + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); + Map<String, Object> mappedToscaTemplate = new HashMap<>(); + nodeTypeInfo.setTemplateFileName("templateFileName"); + nodeTypeInfo.setMappedToscaTemplate(mappedToscaTemplate); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + nodeTypesInfo.put(nodeName,nodeTypeInfo); + return nodeTypesInfo; + } + + protected Map<String, UploadComponentInstanceInfo> getUploadResInstancesMap(){ + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + UploadComponentInstanceInfo uploadComponentInstanceInfo = getuploadComponentInstanceInfo(); + Map<String, String> capabilitiesNamesToUpdate = new HashMap<>(); + capabilitiesNamesToUpdate.put("mme_ipu_vdu.feature","capabilitiesNamesToUpdate"); + Map<String, String> requirementsNamesToUpdate = new HashMap<>(); + requirementsNamesToUpdate.put("mme_ipu_vdu.feature","capabilitiesNamesToUpdate"); + uploadResInstancesMap.put("zxjTestImportServiceAb",uploadComponentInstanceInfo); + return uploadResInstancesMap; + } + + protected Map<String, List<UploadPropInfo>> getUploadPropInfoProperties(){ + Map<String, List<UploadPropInfo>> properties = new HashMap<>(); + List<UploadPropInfo> uploadPropInfoList = new ArrayList<>(); + UploadPropInfo uploadPropInfo = new UploadPropInfo(); + List<GetInputValueDataDefinition> get_input = new ArrayList<>(); + GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition(); + getInputValueDataDefinition.setPropName("getInputValueDataDefinitionName"); + get_input.add(getInputValueDataDefinition); + uploadPropInfo.setName("propertiesName"); + uploadPropInfo.setValue("value"); + uploadPropInfo.setGet_input(get_input); + uploadPropInfoList.add(uploadPropInfo); + properties.put("uploadComponentInstanceInfo",uploadPropInfoList); + return properties; + } + + protected List<PropertyDefinition> getProperties(){ + List<PropertyDefinition> properties = new ArrayList<>(); + PropertyDefinition propertyDefinition = new PropertyDefinition(); + propertyDefinition.setName("propertiesName"); + properties.add(propertyDefinition); + return properties; + } + + protected UploadComponentInstanceInfo getuploadComponentInstanceInfo(){ + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + uploadComponentInstanceInfo.setType("resources"); + Collection<String> directives = new Collection<String>() { + @Override + public int size() { + return 0; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(Object o) { + return false; + } + + @Override + public Iterator<String> iterator() { + return null; + } + + @Override + public Object[] toArray() { + return new Object[0]; + } + + @Override + public <T> T[] toArray(T[] ts) { + return null; + } + + @Override + public boolean add(String s) { + return false; + } + + @Override + public boolean remove(Object o) { + return false; + } + + @Override + public boolean containsAll(Collection<?> collection) { + return false; + } + + @Override + public boolean addAll(Collection<? extends String> collection) { + return false; + } + + @Override + public boolean removeAll(Collection<?> collection) { + return false; + } + + @Override + public boolean retainAll(Collection<?> collection) { + return false; + } + + @Override + public void clear() { + + } + }; + uploadComponentInstanceInfo.setDirectives(directives); + UploadNodeFilterInfo uploadNodeFilterInfo = new UploadNodeFilterInfo(); + Map<String, List<UploadReqInfo>> requirements = new HashMap<>(); + List<UploadReqInfo> uploadReqInfoList = new ArrayList<>(); + UploadReqInfo uploadReqInfo = new UploadReqInfo(); + uploadReqInfo.setName("uploadReqInfo"); + uploadReqInfo.setCapabilityName("tosca.capabilities.Node"); + uploadReqInfoList.add(uploadReqInfo); + requirements.put("requirements",uploadReqInfoList); + uploadNodeFilterInfo.setName("mme_ipu_vdu.virtualbinding"); + uploadComponentInstanceInfo.setCapabilities(getCapabilities()); + uploadComponentInstanceInfo.setRequirements(requirements); + return uploadComponentInstanceInfo; + } + + protected Map<String, List<UploadCapInfo>> getCapabilities(){ + List<UploadCapInfo> uploadCapInfoList = new ArrayList<>(); + UploadCapInfo uploadCapInfo = new UploadCapInfo(); + uploadCapInfo.setNode("tosca.nodes.Root"); + uploadCapInfo.setName("mme_ipu_vdu.dependency"); + uploadCapInfoList.add(uploadCapInfo); + Map<String, List<UploadCapInfo>> uploadCapInfoMap = new HashMap<>(); + uploadCapInfoMap.put("tosca.capabilities.Node",uploadCapInfoList); + return uploadCapInfoMap; + } + + protected Map<String, List<UploadReqInfo>> gerRequirements(){ + Map<String, List<UploadReqInfo>> uploadReqInfoMap = new HashMap<>(); + String requirementName = "tosca.capabilities.Node"; + List<UploadReqInfo> uploadReqInfoList = new ArrayList<>(); + UploadReqInfo uploadReqInfo = new UploadReqInfo(); + uploadReqInfo.setCapabilityName("tosca.capabilities.Node"); + uploadReqInfoMap.put(requirementName,uploadReqInfoList); + return uploadReqInfoMap; + } + + protected ComponentParametersView createComponentParametersView() { + ComponentParametersView parametersView = new ComponentParametersView(); + parametersView.disableAll(); + parametersView.setIgnoreComponentInstances(false); + parametersView.setIgnoreComponentInstancesProperties(false); + parametersView.setIgnoreCapabilities(false); + parametersView.setIgnoreRequirements(false); + parametersView.setIgnoreGroups(false); + return parametersView; + } + protected Map<String, byte[]> crateCsarFromPayload() { + String payloadName = "valid_vf.csar"; + String rootPath = System.getProperty("user.dir"); + Path path; + byte[] data; + String payloadData; + Map<String, byte[]> returnValue = null; + try { + path = Paths.get(rootPath + "/src/test/resources/valid_vf.csar"); + data = Files.readAllBytes(path); + payloadData = Base64.encodeBase64String(data); + UploadResourceInfo resourceInfo = new UploadResourceInfo(); + resourceInfo.setPayloadName(payloadName); + resourceInfo.setPayloadData(payloadData); + Method privateMethod = null; + privateMethod = AbstractValidationsServlet.class.getDeclaredMethod("getCsarFromPayload", UploadResourceInfo.class); + privateMethod.setAccessible(true); + returnValue = (Map<String, byte[]>) privateMethod.invoke(servlet, resourceInfo); + } catch (IOException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + return returnValue; + } + + + protected List<ComponentInstance> creatComponentInstances(){ + List<ComponentInstance> componentInstances = new ArrayList<>(); + ComponentInstance componentInstance = new ComponentInstance(); + Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>(); + List<CapabilityDefinition> capabilityDefinitionList = new ArrayList<>(); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setName("mme_ipu_vdu.feature"); + capabilityDefinitionList.add(capabilityDefinition); + capabilities.put("tosca.capabilities.Node",capabilityDefinitionList); + + Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); + List<RequirementDefinition> requirementDefinitionList = new ArrayList<>(); + RequirementDefinition requirementDefinition = new RequirementDefinition(); + requirementDefinition.setName("zxjtestimportserviceab0.mme_ipu_vdu.dependency.test"); + requirementDefinitionList.add(requirementDefinition); + requirements.put("tosca.capabilities.Node",requirementDefinitionList); + componentInstance.setRequirements(requirements); + componentInstance.setCapabilities(capabilities); + componentInstance.setUniqueId("uniqueId"); + componentInstance.setComponentUid("componentUid"); + componentInstance.setName("zxjTestImportServiceAb"); + componentInstances.add(componentInstance); + return componentInstances; + } + + protected CreateServiceFromYamlParameter getCsfyp() { + CreateServiceFromYamlParameter csfyp = new CreateServiceFromYamlParameter(); + List<ArtifactDefinition> createdArtifacts =new ArrayList<>(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashedMap(); + + csfyp.setNodeName("org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"); + csfyp.setTopologyTemplateYaml(getMainTemplateContent("service_import_template.yml")); + csfyp.setCreatedArtifacts(createdArtifacts); + csfyp.setInTransaction(true); + csfyp.setShouldLock(true); + csfyp.setCsarInfo(getCsarInfo()); + csfyp.setParsedToscaYamlInfo(getParsedToscaYamlInfo()); + csfyp.setNodeTypesInfo(nodeTypesInfo); + csfyp.setYamlName("group.yml"); + return csfyp; + } + + protected ParsedToscaYamlInfo getParsedToscaYamlInfo(){ + ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo(); + Map<String, InputDefinition> inputs = new HashMap<>(); + Map<String, UploadComponentInstanceInfo> instances=new HashMap<>(); + Map<String, GroupDefinition> groups=new HashMap<>(); + Map<String, PolicyDefinition> policies=new HashMap<>(); + parsedToscaYamlInfo.setGroups(groups); + parsedToscaYamlInfo.setInputs(inputs); + parsedToscaYamlInfo.setInstances(instances); + parsedToscaYamlInfo.setPolicies(policies); + return parsedToscaYamlInfo; + } + + String getMainTemplateContent(String fileName){ + String mainTemplateContent = null; + try { + mainTemplateContent = loadFileNameToJsonString(fileName); + } catch (IOException e) { + e.printStackTrace(); + } + return mainTemplateContent; + } + + protected CsarInfo getCsarInfo () + { + String csarUuid = "0010"; + User user = new User(); + Map<String, byte[]> csar = crateCsarFromPayload(); + String vfReousrceName = "resouceName"; + String mainTemplateName = "mainTemplateName"; + String mainTemplateContent = getMainTemplateContent("service_import_template.yml"); + CsarInfo csarInfo = new CsarInfo(user, csarUuid, csar, vfReousrceName, mainTemplateName, mainTemplateContent, false); + return csarInfo; + } + + public static String loadFileNameToJsonString(String fileName) throws IOException { + String sourceDir = "src/test/resources/normativeTypes"; + return loadFileNameToJsonString(sourceDir, fileName); + } + + private static String loadFileNameToJsonString(String sourceDir, String fileName) throws IOException { + java.nio.file.Path filePath = FileSystems.getDefault().getPath(sourceDir, fileName); + byte[] fileContent = Files.readAllBytes(filePath); + return new String(fileContent); + } + + + protected CsarUtils.NonMetaArtifactInfo getNonMetaArtifactInfo(){ + String artifactName = "artifactName",path = "/src/test/resources/valid_vf.csar",artifactType = "AAI_SERVICE_MODEL"; + ArtifactGroupTypeEnum artifactGroupType = ArtifactGroupTypeEnum.TOSCA; + String rootPath = System.getProperty("user.dir"); + Path path2; + byte[] data = new byte[0]; + path2 = Paths.get(rootPath + "/src/test/resources/valid_vf.csar"); + try { + data = Files.readAllBytes(path2); + } catch (IOException e) { + e.printStackTrace(); + } + String artifactUniqueId = "artifactUniqueId"; + boolean isFromCsar = true; + CsarUtils.NonMetaArtifactInfo nonMetaArtifactInfo = new CsarUtils.NonMetaArtifactInfo(artifactName, + path,artifactType,artifactGroupType,data,artifactUniqueId,isFromCsar); + return nonMetaArtifactInfo; + + } + + protected void assertComponentException(ComponentException e, ActionStatus expectedStatus, String... variables) { + ResponseFormat actualResponse = e.getResponseFormat() != null ? + e.getResponseFormat() : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()); + assertParseResponse(actualResponse, expectedStatus, variables); + } + + private void assertParseResponse(ResponseFormat actualResponse, ActionStatus expectedStatus, String... variables) { + ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables); + assertThat(expectedResponse.getStatus()).isEqualTo(actualResponse.getStatus()); + assertThat(expectedResponse.getFormattedMessage()).isEqualTo(actualResponse.getFormattedMessage()); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBussinessLogicBaseTestSetup.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBussinessLogicBaseTestSetup.java new file mode 100644 index 0000000000..15487d7ea0 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBussinessLogicBaseTestSetup.java @@ -0,0 +1,1078 @@ +/* + + * Copyright (c) 2018 AT&T Intellectual Property. + + * + + * Licensed under the Apache License, Version 2.0 (the "License"); + + * you may not use this file except in compliance with the License. + + * You may obtain a copy of the License at + + * + + * http://www.apache.org/licenses/LICENSE-2.0 + + * + + * Unless required by applicable law or agreed to in writing, software + + * distributed under the License is distributed on an "AS IS" BASIS, + + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + + * See the License for the specific language governing permissions and + + * limitations under the License. + + */ + +package org.openecomp.sdc.be.components.impl; + +import fj.data.Either; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.codec.binary.Base64; +import org.junit.Before; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.openecomp.sdc.ElementOperationMock; +import org.openecomp.sdc.be.auditing.impl.AuditingManager; +import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic; +import org.openecomp.sdc.be.components.csar.CsarInfo; +import org.openecomp.sdc.be.components.distribution.engine.DistributionEngine; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; +import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic; +import org.openecomp.sdc.be.components.impl.utils.CreateServiceFromYamlParameter; +import org.openecomp.sdc.be.components.path.ForwardingPathValidator; +import org.openecomp.sdc.be.components.validation.NodeFilterValidator; +import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation; +import org.openecomp.sdc.be.components.validation.UserValidations; +import org.openecomp.sdc.be.components.validation.component.*; +import org.openecomp.sdc.be.components.validation.service.*; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; +import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.externalapi.servlet.representation.*; +import org.openecomp.sdc.be.facade.operations.CatalogOperation; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.impl.WebAppContextWrapper; +import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.category.CategoryDefinition; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.operations.api.IElementOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation; +import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent; +import org.openecomp.sdc.be.servlets.AbstractValidationsServlet; +import org.openecomp.sdc.be.user.Role; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.api.UploadArtifactInfo; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.web.context.WebApplicationContext; + +import javax.servlet.ServletContext; +import java.util.*; + +import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.when; + +public class ServiceImportBussinessLogicBaseTestSetup extends BaseBusinessLogicMock { + protected ServiceImportBusinessLogic sIB1; + protected static final String SERVICE_CATEGORY = "Mobility"; + protected static final String INSTANTIATION_TYPE = "A-la-carte"; + protected final ServletContext servletContext = Mockito.mock(ServletContext.class); + protected UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class); + protected WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class); + protected WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class); + protected ResponseFormatManager responseManager = null; + protected ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class)); + protected AuditCassandraDao auditingDao = Mockito.mock(AuditCassandraDao.class); + protected ArtifactsBusinessLogic artifactBl = Mockito.mock(ArtifactsBusinessLogic.class); + protected GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class); + protected JanusGraphDao mockJanusGraphDao = Mockito.mock(JanusGraphDao.class); + protected ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); + protected CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic = Mockito.mock(CsarArtifactsAndGroupsBusinessLogic.class); + protected GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class); + protected UserValidations userValidations = Mockito.mock(UserValidations.class); + protected ResourceAdminEvent auditArchive1 = Mockito.mock(ResourceAdminEvent.class); + protected CatalogOperation catalogOperation = Mockito.mock(CatalogOperation.class); + protected ResourceAdminEvent auditArchive2 = Mockito.mock(ResourceAdminEvent.class); + protected ResourceAdminEvent auditRestore = Mockito.mock(ResourceAdminEvent.class); + protected ServiceImportParseLogic serviceImportParseLogic = Mockito.mock(ServiceImportParseLogic.class); + IElementOperation mockElementDao = new ElementOperationMock(); + DistributionEngine distributionEngine = Mockito.mock(DistributionEngine.class); + ServiceDistributionValidation serviceDistributionValidation = Mockito.mock(ServiceDistributionValidation.class); + ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class); + ForwardingPathValidator forwardingPathValidator = Mockito.mock(ForwardingPathValidator.class); + UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class); + NodeFilterOperation serviceFilterOperation = Mockito.mock(NodeFilterOperation.class); + NodeFilterValidator serviceFilterValidator = Mockito.mock(NodeFilterValidator.class); + protected ServiceTypeValidator serviceTypeValidator = new ServiceTypeValidator(componentsUtils); + protected ServiceCategoryValidator serviceCategoryValidator = new ServiceCategoryValidator(componentsUtils, mockElementDao); + protected ServiceRoleValidator serviceRoleValidator = new ServiceRoleValidator(componentsUtils); + protected ServiceFunctionValidator serviceFunctionValidator = new ServiceFunctionValidator(componentsUtils); + protected ServiceInstantiationTypeValidator serviceInstantiationTypeValidator = new ServiceInstantiationTypeValidator(componentsUtils); + protected ComponentDescriptionValidator componentDescriptionValidator = new ComponentDescriptionValidator(componentsUtils); + protected ComponentProjectCodeValidator componentProjectCodeValidator = new ComponentProjectCodeValidator(componentsUtils); + protected ComponentIconValidator componentIconValidator = new ComponentIconValidator(componentsUtils); + protected ComponentContactIdValidator componentContactIdValidator = new ComponentContactIdValidator(componentsUtils); + protected ComponentTagsValidator componentTagsValidator = new ComponentTagsValidator(componentsUtils); + protected ComponentNameValidator componentNameValidator = new ComponentNameValidator(componentsUtils, toscaOperationFacade); + protected final ComponentValidator componentValidator = Mockito.mock(ComponentValidator.class); + protected ServiceValidator serviceValidator = createServiceValidator(); + + protected User user = null; + protected Resource genericService = null; + + private static final String RESOURCE_NAME = "My-Resource_Name with space"; + private static final String RESOURCE_TOSCA_NAME = "My-Resource_Tosca_Name"; + private static final String RESOURCE_CATEGORY1 = "Network Layer 2-3"; + private static final String RESOURCE_SUBCATEGORY = "Router"; + protected static final String CERTIFIED_VERSION = "1.0"; + protected static final String UNCERTIFIED_VERSION = "0.2"; + protected static final String COMPONNET_ID = "myUniqueId"; + protected static final String GENERIC_SERVICE_NAME = "org.openecomp.resource.abstract.nodes.service"; + + protected static final String SERVICE_ROLE = JsonPresentationFields.SERVICE_ROLE.getPresentation(); + protected static final String SERVICE_TYPE = JsonPresentationFields.SERVICE_TYPE.getPresentation(); + protected static final String SERVICE_FUNCTION = JsonPresentationFields.SERVICE_FUNCTION.getPresentation(); + + public ServiceImportBussinessLogicBaseTestSetup() { + + } + + protected ServiceValidator createServiceValidator() { + List<ComponentFieldValidator> componentFieldValidators = Arrays.asList(componentContactIdValidator, + componentDescriptionValidator, + componentIconValidator, componentNameValidator, + new ComponentProjectCodeValidator(componentsUtils), + componentTagsValidator); + + List<ServiceFieldValidator> serviceFieldValidators = Arrays.asList(serviceCategoryValidator, new ServiceEnvironmentContextValidator(), + serviceInstantiationTypeValidator, new ServiceNamingPolicyValidator(componentsUtils), + serviceRoleValidator, serviceTypeValidator); + return new ServiceValidator(componentsUtils, componentFieldValidators, serviceFieldValidators); + } + + @Before + public void setup() { + + // Elements + IElementOperation mockElementDao = new ElementOperationMock(); + + // User data and management + user = new User(); + user.setUserId("jh0003"); + user.setFirstName("Jimmi"); + user.setLastName("Hendrix"); + user.setRole(Role.ADMIN.name()); + + when(mockUserAdmin.getUser("jh0003", false)).thenReturn(user); + when(userValidations.validateUserExists(eq("jh0003"))).thenReturn(user); + when(userValidations.validateUserNotEmpty(eq(user), anyString())).thenReturn(user); +// when(userValidations.validateUserRole(user)) + // Servlet Context attributes + when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager); +// when(servletContext.getAttribute(Constants.SERVICE_OPERATION_MANAGER)).thenReturn(new ServiceOperation()); + when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper); + when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext); + when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao); + when(graphLockOperation.lockComponent(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK); + when(graphLockOperation.lockComponentByName(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK); + when(catalogOperation.updateCatalog(Mockito.any(), Mockito.any())).thenReturn(ActionStatus.OK); + // artifact bussinesslogic + ArtifactDefinition artifactDef = new ArtifactDefinition(); + when(artifactBl.createArtifactPlaceHolderInfo(Mockito.any(), Mockito.anyString(), Mockito.anyMap(), Mockito.any(User.class), Mockito.any(ArtifactGroupTypeEnum.class))).thenReturn(artifactDef); + + // createService + Service serviceResponse = createServiceObject(true); + Either<Component, StorageOperationStatus> eitherCreate = Either.left(serviceResponse); + when(toscaOperationFacade.createToscaComponent(Mockito.any(Component.class))).thenReturn(eitherCreate); + when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherCreate); + Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false); + when(toscaOperationFacade.validateComponentNameExists("Service", null, ComponentTypeEnum.SERVICE)).thenReturn(eitherCount); + Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true); + when(toscaOperationFacade.validateComponentNameExists("alreadyExist", null, ComponentTypeEnum.SERVICE)).thenReturn(eitherCountExist); + when(userValidations.validateUserExists(user)).thenReturn(user); + + // createResource + Resource resourceRsponse = createParseResourceObject(true); + Either<Component, StorageOperationStatus> eitherResourceCreate = Either.left(resourceRsponse); + when(toscaOperationFacade.createToscaComponent(Mockito.any(Component.class))).thenReturn(eitherResourceCreate); + Either<Component, StorageOperationStatus> eitherResourceRes = Either.left(resourceRsponse); + when(toscaOperationFacade.getToscaFullElement(Mockito.anyString())).thenReturn(eitherResourceRes); + + Either<Boolean, StorageOperationStatus> eitherResourceCount = Either.left(false); + when(toscaOperationFacade.validateComponentNameExists("Resource", null, ComponentTypeEnum.RESOURCE)).thenReturn(eitherResourceCount); + Either<Boolean, StorageOperationStatus> eitherResourceCountExist = Either.left(true); + when(toscaOperationFacade.validateComponentNameExists("alreadyExist", null, ComponentTypeEnum.RESOURCE)).thenReturn(eitherResourceCountExist); + + genericService = setupGenericServiceMock(); + Either<Resource, StorageOperationStatus> findLatestGeneric = Either.left(genericService); + when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_SERVICE_NAME)).thenReturn(findLatestGeneric); + + when(serviceImportParseLogic.isArtifactDeletionRequired(anyString(),any(),anyBoolean())).thenReturn(true); + Either<Boolean, ResponseFormat> validateCGD = Either.left(true); + when(serviceImportParseLogic.validateCyclicGroupsDependencies(any())).thenReturn(validateCGD); + + sIB1 = new ServiceImportBusinessLogic(elementDao, groupOperation, groupInstanceOperation, + groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation, + artifactBl, distributionEngine, componentInstanceBusinessLogic, + serviceDistributionValidation, forwardingPathValidator, uiComponentDataConverter, serviceFilterOperation, + serviceFilterValidator, artifactToscaOperation, componentContactIdValidator, + componentNameValidator, componentTagsValidator, componentValidator, + componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator); + + + mockAbstract(); + + responseManager = ResponseFormatManager.getInstance(); + } + + protected Service createServiceObject(boolean afterCreate) { + Service service = new Service(); + service.setUniqueId("sid"); + service.setName("Service"); + CategoryDefinition category = new CategoryDefinition(); + category.setName(SERVICE_CATEGORY); + category.setIcons(Collections.singletonList("defaulticon")); + List<CategoryDefinition> categories = new ArrayList<>(); + categories.add(category); + service.setCategories(categories); + service.setInstantiationType(INSTANTIATION_TYPE); + + service.setDescription("description"); + List<String> tgs = new ArrayList<>(); + tgs.add(service.getName()); + service.setTags(tgs); + // service.setVendorName("Motorola"); + // service.setVendorRelease("1.0.0"); + service.setIcon("defaulticon"); + // service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + service.setContactId("aa1234"); + service.setProjectCode("12345"); + service.setEcompGeneratedNaming(true); + + if (afterCreate) { + service.setVersion("0.1"); + service.setUniqueId(service.getName() + ":" + service.getVersion()); + service.setCreatorUserId(user.getUserId()); + service.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); + } + return service; + } + + protected Resource createResourceObject(boolean afterCreate) { + Resource resource = new Resource(); + resource.setUniqueId("sid"); + resource.setName("Service"); + CategoryDefinition category = new CategoryDefinition(); + category.setName(SERVICE_CATEGORY); + category.setIcons(Collections.singletonList("defaulticon")); + List<CategoryDefinition> categories = new ArrayList<>(); + categories.add(category); + resource.setCategories(categories); + + + resource.setDescription("description"); + List<String> tgs = new ArrayList<>(); + tgs.add(resource.getName()); + resource.setTags(tgs); + resource.setIcon("defaulticon"); + resource.setContactId("aa1234"); + resource.setProjectCode("12345"); + + + if (afterCreate) { + resource.setVersion("0.1"); + resource.setUniqueId(resource.getName() + ":" + resource.getVersion()); + resource.setCreatorUserId(user.getUserId()); + resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); + } + return resource; + } + +protected Resource createParseResourceObject(boolean afterCreate) { + Resource resource = new Resource(); + resource.setName(RESOURCE_NAME); + resource.setToscaResourceName(RESOURCE_TOSCA_NAME); + resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY); + resource.setDescription("My short description"); + List<String> tgs = new ArrayList<>(); + tgs.add("test"); + tgs.add(resource.getName()); + resource.setTags(tgs); + List<String> template = new ArrayList<>(); + template.add("tosca.nodes.Root"); + resource.setDerivedFrom(template); + resource.setVendorName("Motorola"); + resource.setVendorRelease("1.0.0"); + resource.setContactId("ya5467"); + resource.setIcon("defaulticon"); + Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); + List<RequirementDefinition> requirementDefinitionList= new ArrayList<>(); + requirements.put("test", requirementDefinitionList); + resource.setRequirements(requirements); + + if (afterCreate) { + resource.setName(resource.getName()); + resource.setVersion("0.1"); + resource.setUniqueId(resource.getName() + .toLowerCase() + ":" + resource.getVersion()); + resource.setCreatorUserId(user.getUserId()); + resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); + resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + } + return resource; + } + protected Resource setupGenericServiceMock() { + Resource genericService = new Resource(); + genericService.setVersion("1.0"); + genericService.setToscaResourceName(GENERIC_SERVICE_NAME); + return genericService; + } + + protected UploadComponentInstanceInfo createUploadComponentInstanceInfo(){ + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + uploadComponentInstanceInfo.setName("UploadComponentInstanceInfo"); + return uploadComponentInstanceInfo; + } + + private void mockAbstract() { + checkCreateAbstract(); + checkCreateOther(); + checkCreateFile(); + } + + private void checkCreateAbstract(){ + AbstractResourceInfo abstractResourceInfo = new AbstractResourceInfo(); + List<RequirementCapabilityRelDef> componentInstancesRelations = new ArrayList<>(); + abstractResourceInfo.setComponentInstancesRelations(componentInstancesRelations); + abstractResourceInfo.setAbstractResourceUniqueId("abstractResourceUniqueId"); + abstractResourceInfo.setAbstractResourceName("abstractResourceName"); + abstractResourceInfo.setAbstractResourceUUid("abstractResourceUUid"); + + AbstractResourceInfo getAbstractResourceInfo = new AbstractResourceInfo(); + getAbstractResourceInfo.getAbstractResourceName(); + getAbstractResourceInfo.getAbstractResourceUniqueId(); + getAbstractResourceInfo.getAbstractResourceUUid(); + getAbstractResourceInfo.getComponentInstancesRelations(); + + AbstractTemplateInfo createAbstractTemplateInfo = new AbstractTemplateInfo(); + List<AbstractResourceInfo> abstractResourceInfoList = new ArrayList<>(); + abstractResourceInfoList.add(abstractResourceInfo); + createAbstractTemplateInfo.setAbstractResourceInfoList(abstractResourceInfoList); + createAbstractTemplateInfo.setServiceUniqueId("serviceUniqueId"); + createAbstractTemplateInfo.setIsAbstractTemplate(true); + createAbstractTemplateInfo.setServiceUUid("serviceUUid"); + + AbstractTemplateInfo getAbstractTemplateInfo = new AbstractTemplateInfo(); + getAbstractTemplateInfo.getAbstractResourceInfoList(); + getAbstractTemplateInfo.getIsAbstractTemplate(); + getAbstractTemplateInfo.getServiceUniqueId(); + getAbstractTemplateInfo.getServiceUUid(); + + CopyServiceInfo copyServiceInfo = new CopyServiceInfo(); + copyServiceInfo.setNewServiceName("newServiceName"); + copyServiceInfo.setNewServiceUUid("serviceUUid"); + copyServiceInfo.setOldServiceUUid("oldServiceUUid"); + + CopyServiceInfo getCopyServiceInfo = new CopyServiceInfo(); + getCopyServiceInfo.getNewServiceName(); + getCopyServiceInfo.getNewServiceUUid(); + getCopyServiceInfo.getOldServiceUUid(); + + ReplaceVNFInfo replaceVNFInfo = new ReplaceVNFInfo(); + ComponentInstance realVNFComponentInstance = new ComponentInstance(); + replaceVNFInfo.setAbstractResourceUniqueId("abstractResourceUniqueId"); + replaceVNFInfo.setRealVNFComponentInstance(realVNFComponentInstance); + replaceVNFInfo.setServiceUniqueId("serviceUniqueId"); + + ReplaceVNFInfo getReplaceVNFInfo = new ReplaceVNFInfo(); + getReplaceVNFInfo.getServiceUniqueId(); + getReplaceVNFInfo.getAbstractResourceUniqueId(); + getReplaceVNFInfo.getRealVNFComponentInstance(); + } + + private void checkCreateOther(){ + ResourceInstanceMetadata resourceInstanceMetadata = new ResourceInstanceMetadata(); + List<ArtifactMetadata> artifacts = new ArrayList<>(); + resourceInstanceMetadata.setArtifacts(artifacts); + resourceInstanceMetadata.setResoucreType("resoucreType"); + resourceInstanceMetadata.setResourceInstanceName("resourceInstanceName"); + resourceInstanceMetadata.setResourceInvariantUUID("resourceInvariantUUID"); + resourceInstanceMetadata.setResourceName("resourceName"); + resourceInstanceMetadata.setResourceUUID("resourceUUID"); + resourceInstanceMetadata.setResourceVersion("resourceVersion"); + + ResourceInstanceMetadata getResourceInstanceMetadata = new ResourceInstanceMetadata(); + getResourceInstanceMetadata.getArtifacts(); + getResourceInstanceMetadata.getResoucreType(); + getResourceInstanceMetadata.getResourceInstanceName(); + getResourceInstanceMetadata.getResourceInvariantUUID(); + getResourceInstanceMetadata.getResourceName(); + getResourceInstanceMetadata.getResourceUUID(); + getResourceInstanceMetadata.getResourceVersion(); + + UploadServiceInfo uploadServiceInfo = new UploadServiceInfo(); + List<String> tags = new ArrayList<>(); + List<CategoryDefinition> categories = new ArrayList<>(); + List<UploadArtifactInfo> artifactList = new ArrayList<>(); + uploadServiceInfo.setProjectCode("projectCode"); + uploadServiceInfo.setCategories(categories); + uploadServiceInfo.setServiceType(""); + uploadServiceInfo.setServiceVendorModelNumber("serviceVendorModelNumber"); + uploadServiceInfo.setVendorRelease("vendorRelease"); + uploadServiceInfo.setVendorName("vendorName"); + uploadServiceInfo.setServiceIconPath("serviceIconPath"); + uploadServiceInfo.setName("uploadServiceInfo"); + uploadServiceInfo.setContactId("contactId"); + uploadServiceInfo.setIcon("icon"); + uploadServiceInfo.setNamingPolicy("namingPolicy"); + uploadServiceInfo.setEcompGeneratedNaming("ecompGeneratedNaming"); + uploadServiceInfo.setServiceEcompNaming("serviceEcompNaming"); + uploadServiceInfo.setServiceRole("serviceRole"); + uploadServiceInfo.setSubcategory("subcategory"); + uploadServiceInfo.setCategory("category"); + uploadServiceInfo.setType("type"); + uploadServiceInfo.setUUID("UUID"); + uploadServiceInfo.setInvariantUUID("invariantUUID"); + uploadServiceInfo.setResourceVendorRelease("resourceVendorRelease"); + uploadServiceInfo.setResourceVendor("resourceVendor"); + uploadServiceInfo.setDescription("description"); + uploadServiceInfo.setTags(tags); + uploadServiceInfo.setArtifactList(artifactList); + uploadServiceInfo.setPayloadName("payloadName"); + uploadServiceInfo.setPayloadData("payloadData"); + } + + protected void checkGetUploadServiceInfo(){ + UploadServiceInfo uploadServiceInfo = new UploadServiceInfo(); + List<String> tags = new ArrayList<>(); + List<CategoryDefinition> categories = new ArrayList<>(); + List<UploadArtifactInfo> artifactList = new ArrayList<>(); + uploadServiceInfo.getProjectCode(); + uploadServiceInfo.getCategories(); + uploadServiceInfo.getServiceType(); + uploadServiceInfo.getServiceVendorModelNumber(); + uploadServiceInfo.getVendorRelease(); + uploadServiceInfo.getVendorName(); + uploadServiceInfo.getServiceIconPath(); + uploadServiceInfo.getName(); + uploadServiceInfo.getContactId(); + uploadServiceInfo.getIcon(); + uploadServiceInfo.getNamingPolicy(); + uploadServiceInfo.getEcompGeneratedNaming(); + uploadServiceInfo.getServiceEcompNaming(); + uploadServiceInfo.getServiceRole(); + uploadServiceInfo.getSubcategory(); + uploadServiceInfo.getCategory(); + uploadServiceInfo.getType(); + uploadServiceInfo.getUUID(); + uploadServiceInfo.getInvariantUUID(); + uploadServiceInfo.getResourceVendorRelease(); + uploadServiceInfo.getResourceVendor(); + uploadServiceInfo.getDescription(); + uploadServiceInfo.getTags(); + uploadServiceInfo.getArtifactList(); + uploadServiceInfo.getPayloadName(); + uploadServiceInfo.getPayloadData(); + } + + private void checkCreateFile(){ + CreateServiceFromYamlParameter csfp = new CreateServiceFromYamlParameter(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); + ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo(); + List<ArtifactDefinition> createdArtifacts = new ArrayList<>(); + CsarInfo csarInfo = getCsarInfo(); + csfp.setYamlName("yamlName"); + csfp.setNodeTypesInfo(nodeTypesInfo); + csfp.setParsedToscaYamlInfo(parsedToscaYamlInfo); + csfp.setCsarInfo(csarInfo); + csfp.setCreatedArtifacts(createdArtifacts); + csfp.setYamlName("yamlName"); + csfp.setShouldLock(true); + csfp.setInTransaction(true); + csfp.setNodeName("nodeName"); + + CreateServiceFromYamlParameter getCsfy = new CreateServiceFromYamlParameter(); + getCsfy.getYamlName(); + getCsfy.getNodeTypesInfo(); + getCsfy.getParsedToscaYamlInfo(); + getCsfy.getCsarInfo(); + getCsfy.getCreatedArtifacts(); + getCsfy.getYamlName(); + getCsfy.isShouldLock(); + getCsfy.isInTransaction(); + getCsfy.getNodeName(); + } + + protected CsarInfo getCsarInfo () + { + String csarUuid = "0010"; + User user = new User(); + Map<String, byte[]> csar = crateCsarFromPayload(); + String vfReousrceName = "resouceName"; + String mainTemplateName = "mainTemplateName"; + String mainTemplateContent = getMainTemplateContent(); + final Service service = createServiceObject(false); + CsarInfo csarInfo = new CsarInfo(user, csarUuid, csar, vfReousrceName, mainTemplateName, mainTemplateContent, false); + return csarInfo; + } + + protected Map<String, byte[]> crateCsarFromPayload() { + String payloadName = "valid_vf.csar"; + byte[] data = new byte[1024]; + Map<String, byte[]> returnValue = new HashMap<>(); + returnValue.put(payloadName,data); + + return returnValue; + } + + protected String getGroupsYaml(){ + return "zxjTestImportServiceAb 0:\n" + + " type: org.openecomp.resource.vf.Zxjtestimportserviceab\n" + + " metadata:\n" + + " invariantUUID: 41474f7f-3195-443d-a0a2-eb6020a56279\n" + + " UUID: 92e32e49-55f8-46bf-984d-a98c924037ec\n" + + " customizationUUID: 40286158-96d0-408e-9f27-21d43817d37c\n" + + " version: '1.0'\n" + + " name: zxjTestImportServiceAb\n" + + " description: zxjTestImportServiceAbstract\n" + + " type: VF\n" + + " category: Generic\n" + + " subcategory: Abstract\n" + + " resourceVendor: zxjImportService\n" + + " resourceVendorRelease: '1.0'\n" + + " resourceVendorModelNumber: ''\n" + + " properties:\n" + + " skip_post_instantiation_configuration: true\n" + + " nf_naming:\n" + + " ecomp_generated_naming: true\n" + + " multi_stage_design: 'false'\n" + + " controller_actor: SO-REF-DATA\n" + + " availability_zone_max_count: 1\n" + + " requirements:\n" + + " - imagefile.dependency:\n" + + " capability: feature\n" + + " node: ext ZTE VL 0\n" + + " - mme_ipu_vdu.dependency:\n" + + " capability: feature\n" + + " node: ExtCP 0\n" + + " capabilities:\n" + + " mme_ipu_vdu.scalable:\n" + + " properties:\n" + + " max_instances: 1\n" + + " min_instances: 1\n" + + " mme_ipu_vdu.nfv_compute:\n" + + " properties:\n" + + " num_cpus: '2'\n" + + " flavor_extra_specs: {\n" + + " }\n" + + " mem_size: '8192'"; + } + + protected String getYamlFileContent(){ + return "tosca_definitions_version: tosca_simple_yaml_1_1\n" + + "imports:\n" + + "- data.yml\n" + + "group_types:\n" + + " tosca.groups.Root:\n" + + " description: The TOSCA Group Type all other TOSCA Group Types derive from\n" + + " interfaces:\n" + + " Standard:\n" + + " type: tosca.interfaces.node.lifecycle.Standard\n" + + " org.openecomp.groups.heat.HeatStack:\n" + + " derived_from: tosca.groups.Root\n" + + " description: Grouped all heat resources which are in the same heat stack\n" + + " properties:\n" + + " heat_file:\n" + + " type: string\n" + + " description: Heat file which associate to this group/heat stack\n" + + " required: true\n" + + " status: supported\n" + + " description:\n" + + " type: string\n" + + " description: group description\n" + + " required: true\n" + + " status: supported\n" + + " org.openecomp.groups.VfModule:\n" + + " derived_from: tosca.groups.Root\n" + + " description: Grouped all heat resources which are in the same VF Module\n" + + " properties:\n" + + " isBase:\n" + + " type: boolean\n" + + " description: Whether this module should be deployed before other modules\n" + + " required: true\n" + + " default: false\n" + + " status: supported\n" + + " vf_module_label:\n" + + " type: string\n" + + " required: true\n" + + " description: |\n" + + " Alternate textual key used to reference this VF-Module model. Must be unique within the VNF model\n" + + " vf_module_description:\n" + + " type: string\n" + + " required: true\n" + + " description: |\n" + + " Description of the VF-modules contents and purpose (e.g. \"Front-End\" or \"Database Cluster\")\n" + + " min_vf_module_instances:\n" + + " type: integer\n" + + " required: true\n" + + " description: The minimum instances of this VF-Module\n" + + " max_vf_module_instances:\n" + + " type: integer\n" + + " required: false\n" + + " description: The maximum instances of this VF-Module\n" + + " initial_count:\n" + + " type: integer\n" + + " required: false\n" + + " description: |\n" + + " The initial count of instances of the VF-Module. The value must be in the range between min_vfmodule_instances and max_vfmodule_instances. If no value provided the initial count is the min_vfmodule_instances.\n" + + " vf_module_type:\n" + + " type: string\n" + + " required: true\n" + + " constraint:\n" + + " - valid_values:\n" + + " - Base\n" + + " - Expansion\n" + + " volume_group:\n" + + " type: boolean\n" + + " required: true\n" + + " default: false\n" + + " description: |\n" + + " \"true\" indicates that this VF Module model requires attachment to a Volume Group. VID operator must select the Volume Group instance to attach to a VF-Module at deployment time.\n" + + " availability_zone_count:\n" + + " type: integer\n" + + " required: false\n" + + " description: |\n" + + " Quantity of Availability Zones needed for this VF-Module (source: Extracted from VF-Module HEAT template)\n" + + " vfc_list:\n" + + " type: map\n" + + " entry_schema:\n" + + " description: <vfc_id>:<count>\n" + + " type: string\n" + + " required: false\n" + + " description: |\n" + + " Identifies the set of VM types and their count included in the VF-Module\n" + + " org.openecomp.groups.NetworkCollection:\n" + + " derived_from: tosca.groups.Root\n" + + " description: groups l3-networks in network collection\n" + + " properties:\n" + + " network_collection_function:\n" + + " type: string\n" + + " required: true\n" + + " description: network collection function\n" + + " network_collection_description:\n" + + " type: string\n" + + " required: true\n" + + " description: network collection description, free format text\n" + + " org.openecomp.groups.VfcInstanceGroup:\n" + + " derived_from: tosca.groups.Root\n" + + " description: groups VFCs with same parent port role\n" + + " properties:\n" + + " vfc_instance_group_function:\n" + + " type: string\n" + + " required: true\n" + + " description: function of this VFC group\n" + + " vfc_parent_port_role:\n" + + " type: string\n" + + " required: true\n" + + " description: common role of parent ports of VFCs in this group\n" + + " network_collection_function:\n" + + " type: string\n" + + " required: true\n" + + " description: network collection function assigned to this group\n" + + " subinterface_role:\n" + + " type: string\n" + + " required: true\n" + + " description: common role of subinterfaces of VFCs in this group, criteria the group is created\n" + + " capabilities:\n" + + " vlan_assignment:\n" + + " type: org.openecomp.capabilities.VLANAssignment\n" + + " properties:\n" + + " vfc_instance_group_reference:\n" + + " type: string\n" + + " tosca.groups.nfv.PlacementGroup:\n" + + " derived_from: tosca.groups.Root\n" + + " description: PlacementGroup is used for describing the affinity or anti-affinity relationship applicable between the virtualization containers to be created based on different VDUs, or between internal VLs to be created based on different VnfVirtualLinkDesc(s)\n" + + " properties:\n" + + " description:\n" + + " type: string\n" + + " description: Human readable description of the group\n" + + " required: true\n" + + " members:\n" + + " - tosca.nodes.nfv.Vdu.Compute\n" + + " - tosca.nodes.nfv.VnfVirtualLink"; + } + + public String getMainTemplateContent(){ + return "tosca_definitions_version: tosca_simple_yaml_1_1\n" + + "metadata:\n" + + " invariantUUID: 6d17f281-683b-4198-a676-0faeecdc9025\n" + + " UUID: bfeab6b4-199b-4a2b-b724-de416c5e9811\n" + + " name: ser09080002\n" + + " description: ser09080002\n" + + " type: Service\n" + + " category: E2E Service\n" + + " serviceType: ''\n" + + " serviceRole: ''\n" + + " instantiationType: A-la-carte\n" + + " serviceEcompNaming: true\n" + + " ecompGeneratedNaming: true\n" + + " namingPolicy: ''\n" + + " environmentContext: General_Revenue-Bearing\n" + + " serviceFunction: ''\n" + + "imports:\n" + + "- nodes:\n" + + " file: nodes.yml\n" + + "- datatypes:\n" + + " file: data.yml\n" + + "- capabilities:\n" + + " file: capabilities.yml\n" + + "- relationships:\n" + + " file: relationships.yml\n" + + "- groups:\n" + + " file: groups.yml\n" + + "- policies:\n" + + " file: policies.yml\n" + + "- annotations:\n" + + " file: annotations.yml\n" + + "- service-ser09080002-interface:\n" + + " file: service-Ser09080002-template-interface.yml\n" + + "- resource-ExtCP:\n" + + " file: resource-Extcp-template.yml\n" + + "- resource-zxjTestImportServiceAb:\n" + + " file: resource-Zxjtestimportserviceab-template.yml\n" + + "- resource-zxjTestImportServiceAb-interface:\n" + + " file: resource-Zxjtestimportserviceab-template-interface.yml\n" + + "- resource-zxjTestServiceNotAbatract:\n" + + " file: resource-Zxjtestservicenotabatract-template.yml\n" + + "- resource-zxjTestServiceNotAbatract-interface:\n" + + " file: resource-Zxjtestservicenotabatract-template-interface.yml\n" + + "- resource-ext ZTE VL:\n" + + " file: resource-ExtZteVl-template.yml\n" + + "topology_template:\n" + + " inputs:\n" + + " skip_post_instantiation_configuration:\n" + + " default: true\n" + + " type: boolean\n" + + " required: false\n" + + " controller_actor:\n" + + " default: SO-REF-DATA\n" + + " type: string\n" + + " required: false\n" + + " cds_model_version:\n" + + " type: string\n" + + " required: false\n" + + " cds_model_name:\n" + + " type: string\n" + + " required: false\n" + + " node_templates:\n" + + " ext ZTE VL 0:\n" + + " type: tosca.nodes.nfv.ext.zte.VL\n" + + " metadata:\n" + + " invariantUUID: 27ab7610-1a97-4daa-938a-3b48e7afcfd0\n" + + " UUID: 9ea63e2c-4b8a-414f-93e3-5703ca5cee0d\n" + + " customizationUUID: e45e79b0-07ab-46b4-ac26-1e9f155ce53c\n" + + " version: '1.0'\n" + + " name: ext ZTE VL\n" + + " description: Ext ZTE VL\n" + + " type: VL\n" + + " category: Generic\n" + + " subcategory: Network Elements\n" + + " resourceVendor: ONAP (Tosca)\n" + + " resourceVendorRelease: 1.0.0.wd03\n" + + " resourceVendorModelNumber: ''\n" + + " zxjTestServiceNotAbatract 0:\n" + + " type: org.openecomp.resource.vf.Zxjtestservicenotabatract\n" + + " metadata:\n" + + " invariantUUID: ce39ce8d-6f97-4e89-8555-ae6789cdcf1c\n" + + " UUID: 4ac822be-f1ae-4ace-a4b8-bf6b5d977005\n" + + " customizationUUID: ee34e1e8-68e2-480f-8ba6-f257bbe90d6a\n" + + " version: '1.0'\n" + + " name: zxjTestServiceNotAbatract\n" + + " description: zxjTestServiceNotAbatract\n" + + " type: VF\n" + + " category: Network L4+\n" + + " subcategory: Common Network Resources\n" + + " resourceVendor: zxjImportService\n" + + " resourceVendorRelease: '1.0'\n" + + " resourceVendorModelNumber: ''\n" + + " properties:\n" + + " nf_naming:\n" + + " ecomp_generated_naming: true\n" + + " skip_post_instantiation_configuration: true\n" + + " multi_stage_design: 'false'\n" + + " controller_actor: SO-REF-DATA\n" + + " availability_zone_max_count: 1\n" + + " capabilities:\n" + + " mme_ipu_vdu.scalable:\n" + + " properties:\n" + + " max_instances: 1\n" + + " min_instances: 1\n" + + " mme_ipu_vdu.nfv_compute:\n" + + " properties:\n" + + " num_cpus: '2'\n" + + " flavor_extra_specs: {\n" + + " }\n" + + " mem_size: '8192'\n" + + " ExtCP 0:\n" + + " type: org.openecomp.resource.cp.extCP\n" + + " metadata:\n" + + " invariantUUID: 9b772728-93f5-424f-bb07-f4cae2783614\n" + + " UUID: 424ac220-4864-453e-b757-917fe4568ff8\n" + + " customizationUUID: 6e65d8a8-4379-4693-87aa-82f9e34b92fd\n" + + " version: '1.0'\n" + + " name: ExtCP\n" + + " description: The AT&T Connection Point base type all other CP derive from\n" + + " type: CP\n" + + " category: Generic\n" + + " subcategory: Network Elements\n" + + " resourceVendor: ONAP (Tosca)\n" + + " resourceVendorRelease: 1.0.0.wd03\n" + + " resourceVendorModelNumber: ''\n" + + " properties:\n" + + " mac_requirements:\n" + + " mac_count_required:\n" + + " is_required: false\n" + + " exCP_naming:\n" + + " ecomp_generated_naming: true\n" + + " zxjTestImportServiceAb 0:\n" + + " type: org.openecomp.resource.vf.Zxjtestimportserviceab\n" + + " metadata:\n" + + " invariantUUID: 41474f7f-3195-443d-a0a2-eb6020a56279\n" + + " UUID: 92e32e49-55f8-46bf-984d-a98c924037ec\n" + + " customizationUUID: 98c7a6c7-a867-45fb-8597-dd464f98e4aa\n" + + " version: '1.0'\n" + + " name: zxjTestImportServiceAb\n" + + " description: zxjTestImportServiceAbstract\n" + + " type: VF\n" + + " category: Generic\n" + + " subcategory: Abstract\n" + + " resourceVendor: zxjImportService\n" + + " resourceVendorRelease: '1.0'\n" + + " resourceVendorModelNumber: ''\n" + + " properties:\n" + + " nf_naming:\n" + + " ecomp_generated_naming: true\n" + + " skip_post_instantiation_configuration: true\n" + + " multi_stage_design: 'false'\n" + + " controller_actor: SO-REF-DATA\n" + + " availability_zone_max_count: 1\n" + + " requirements:\n" + + " - mme_ipu_vdu.dependency:\n" + + " capability: feature\n" + + " node: ExtCP 0\n" + + " - imagefile.dependency:\n" + + " capability: feature\n" + + " node: ext ZTE VL 0\n" + + " capabilities:\n" + + " mme_ipu_vdu.scalable:\n" + + " properties:\n" + + " max_instances: 1\n" + + " min_instances: 1\n" + + " mme_ipu_vdu.nfv_compute:\n" + + " properties:\n" + + " num_cpus: '2'\n" + + " flavor_extra_specs: {\n" + + " }\n" + + " mem_size: '8192'\n" + + " substitution_mappings:\n" + + " node_type: org.openecomp.service.Ser09080002\n" + + " capabilities:\n" + + " extcp0.feature:\n" + + " - ExtCP 0\n" + + " - feature\n" + + " zxjtestservicenotabatract0.mme_ipu_vdu.monitoring_parameter:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - mme_ipu_vdu.monitoring_parameter\n" + + " zxjtestimportserviceab0.imagefile.guest_os:\n" + + " - zxjTestImportServiceAb 0\n" + + " - imagefile.guest_os\n" + + " zxjtestimportserviceab0.imagefile.feature:\n" + + " - zxjTestImportServiceAb 0\n" + + " - imagefile.feature\n" + + " zxjtestservicenotabatract0.imagefile.guest_os:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - imagefile.guest_os\n" + + " zxjtestimportserviceab0.ipu_cpd.feature:\n" + + " - zxjTestImportServiceAb 0\n" + + " - ipu_cpd.feature\n" + + " zxjtestservicenotabatract0.mme_ipu_vdu.virtualbinding:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - mme_ipu_vdu.virtualbinding\n" + + " zxjtestimportserviceab0.mme_ipu_vdu.feature:\n" + + " - zxjTestImportServiceAb 0\n" + + " - mme_ipu_vdu.feature\n" + + " extztevl0.feature:\n" + + " - ext ZTE VL 0\n" + + " - feature\n" + + " zxjtestimportserviceab0.imagefile.image_fle:\n" + + " - zxjTestImportServiceAb 0\n" + + " - imagefile.image_fle\n" + + " zxjtestimportserviceab0.mme_ipu_vdu.monitoring_parameter:\n" + + " - zxjTestImportServiceAb 0\n" + + " - mme_ipu_vdu.monitoring_parameter\n" + + " zxjtestservicenotabatract0.ipu_cpd.feature:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - ipu_cpd.feature\n" + + " zxjtestservicenotabatract0.mme_ipu_vdu.nfv_compute:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - mme_ipu_vdu.nfv_compute\n" + + " zxjtestservicenotabatract0.mme_ipu_vdu.scalable:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - mme_ipu_vdu.scalable\n" + + " extcp0.internal_connectionPoint:\n" + + " - ExtCP 0\n" + + " - internal_connectionPoint\n" + + " zxjtestimportserviceab0.mme_ipu_vdu.virtualbinding:\n" + + " - zxjTestImportServiceAb 0\n" + + " - mme_ipu_vdu.virtualbinding\n" + + " zxjtestservicenotabatract0.imagefile.image_fle:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - imagefile.image_fle\n" + + " extztevl0.virtual_linkable:\n" + + " - ext ZTE VL 0\n" + + " - virtual_linkable\n" + + " zxjtestservicenotabatract0.imagefile.feature:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - imagefile.feature\n" + + " zxjtestimportserviceab0.localstorage.feature:\n" + + " - zxjTestImportServiceAb 0\n" + + " - localstorage.feature\n" + + " zxjtestservicenotabatract0.localstorage.local_attachment:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - localstorage.local_attachment\n" + + " zxjtestimportserviceab0.mme_ipu_vdu.scalable:\n" + + " - zxjTestImportServiceAb 0\n" + + " - mme_ipu_vdu.scalable\n" + + " zxjtestservicenotabatract0.localstorage.feature:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - localstorage.feature\n" + + " zxjtestimportserviceab0.mme_ipu_vdu.nfv_compute:\n" + + " - zxjTestImportServiceAb 0\n" + + " - mme_ipu_vdu.nfv_compute\n" + + " zxjtestimportserviceab0.localstorage.local_attachment:\n" + + " - zxjTestImportServiceAb 0\n" + + " - localstorage.local_attachment\n" + + " zxjtestservicenotabatract0.mme_ipu_vdu.feature:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - mme_ipu_vdu.feature\n" + + " zxjtestimportserviceab0.ipu_cpd.forwarder:\n" + + " - zxjTestImportServiceAb 0\n" + + " - ipu_cpd.forwarder\n" + + " zxjtestservicenotabatract0.ipu_cpd.forwarder:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - ipu_cpd.forwarder\n" + + " requirements:\n" + + " zxjtestservicenotabatract0.imagefile.dependency:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - imagefile.dependency\n" + + " zxjtestservicenotabatract0.mme_ipu_vdu.local_storage:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - mme_ipu_vdu.local_storage\n" + + " zxjtestservicenotabatract0.ipu_cpd.dependency:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - ipu_cpd.dependency\n" + + " zxjtestservicenotabatract0.mme_ipu_vdu.volume_storage:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - mme_ipu_vdu.volume_storage\n" + + " zxjtestservicenotabatract0.ipu_cpd.virtualbinding:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - ipu_cpd.virtualbinding\n" + + " zxjtestservicenotabatract0.mme_ipu_vdu.dependency:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - mme_ipu_vdu.dependency\n" + + " zxjtestservicenotabatract0.localstorage.dependency:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - localstorage.dependency\n" + + " zxjtestimportserviceab0.imagefile.dependency:\n" + + " - zxjTestImportServiceAb 0\n" + + " - imagefile.dependency\n" + + " zxjtestimportserviceab0.mme_ipu_vdu.volume_storage:\n" + + " - zxjTestImportServiceAb 0\n" + + " - mme_ipu_vdu.volume_storage\n" + + " zxjtestimportserviceab0.ipu_cpd.virtualbinding:\n" + + " - zxjTestImportServiceAb 0\n" + + " - ipu_cpd.virtualbinding\n" + + " extcp0.virtualLink:\n" + + " - ExtCP 0\n" + + " - virtualLink\n" + + " extcp0.virtualBinding:\n" + + " - ExtCP 0\n" + + " - virtualBinding\n" + + " zxjtestimportserviceab0.mme_ipu_vdu.guest_os:\n" + + " - zxjTestImportServiceAb 0\n" + + " - mme_ipu_vdu.guest_os\n" + + " extcp0.dependency:\n" + + " - ExtCP 0\n" + + " - dependency\n" + + " zxjtestimportserviceab0.localstorage.dependency:\n" + + " - zxjTestImportServiceAb 0\n" + + " - localstorage.dependency\n" + + " zxjtestservicenotabatract0.ipu_cpd.virtualLink:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - ipu_cpd.virtualLink\n" + + " extztevl0.dependency:\n" + + " - ext ZTE VL 0\n" + + " - dependency\n" + + " zxjtestimportserviceab0.ipu_cpd.dependency:\n" + + " - zxjTestImportServiceAb 0\n" + + " - ipu_cpd.dependency\n" + + " zxjtestimportserviceab0.mme_ipu_vdu.dependency:\n" + + " - zxjTestImportServiceAb 0\n" + + " - mme_ipu_vdu.dependency\n" + + " zxjtestimportserviceab0.mme_ipu_vdu.local_storage:\n" + + " - zxjTestImportServiceAb 0\n" + + " - mme_ipu_vdu.local_storage\n" + + " zxjtestimportserviceab0.ipu_cpd.virtualLink:\n" + + " - zxjTestImportServiceAb 0\n" + + " - ipu_cpd.virtualLink\n" + + " extcp0.external_virtualLink:\n" + + " - ExtCP 0\n" + + " - external_virtualLink\n" + + " zxjtestservicenotabatract0.mme_ipu_vdu.guest_os:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - mme_ipu_vdu.guest_os\n" + + " zxjtestimportserviceab0.ipu_cpd.forwarder:\n" + + " - zxjTestImportServiceAb 0\n" + + " - ipu_cpd.forwarder\n" + + " zxjtestservicenotabatract0.ipu_cpd.forwarder:\n" + + " - zxjTestServiceNotAbatract 0\n" + + " - ipu_cpd.forwarder\n"; + } + + protected void assertComponentException(ComponentException e, ActionStatus expectedStatus, String... variables) { + ResponseFormat actualResponse = e.getResponseFormat() != null ? + e.getResponseFormat() : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()); + assertResponse(actualResponse, expectedStatus, variables); + } + + protected void assertResponse(ResponseFormat actualResponse, ActionStatus expectedStatus, String... variables) { + ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables); + assertEquals(expectedResponse.getStatus(), actualResponse.getStatus()); + assertEquals("assert error description", expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage()); + } + +} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportManagerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportManagerTest.java new file mode 100644 index 0000000000..16cc44f764 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportManagerTest.java @@ -0,0 +1,95 @@ +/* + + * Copyright (c) 2018 AT&T Intellectual Property. + + * + + * Licensed under the Apache License, Version 2.0 (the "License"); + + * you may not use this file except in compliance with the License. + + * You may obtain a copy of the License at + + * + + * http://www.apache.org/licenses/LICENSE-2.0 + + * + + * Unless required by applicable law or agreed to in writing, software + + * distributed under the License is distributed on an "AS IS" BASIS, + + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + + * See the License for the specific language governing permissions and + + * limitations under the License. + + */ + +package org.openecomp.sdc.be.components.impl; + + +import org.junit.Test; +import org.mockito.InjectMocks; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.UploadServiceInfo; + + + +public class ServiceImportManagerTest { + @InjectMocks + private ServiceImportManager serviceImportManager; + + private ServiceImportManager createTestSubject() { + return new ServiceImportManager(); + } + + @Test + public void testGetServiceImportBusinessLogic() { + ServiceImportManager testSubject; + ServiceImportBusinessLogic result; + + testSubject = createTestSubject(); + result = testSubject.getServiceImportBusinessLogic(); + } + + @Test + public void testSetServiceImportBusinessLogic() { + ServiceImportManager testSubject; + ServiceImportBusinessLogic serviceImportBusinessLogic=null; + + testSubject = createTestSubject(); + testSubject.setServiceImportBusinessLogic(serviceImportBusinessLogic); + } + + + @Test + public void testGetServiceBusinessLogic() { + ServiceImportManager testSubject; + ServiceBusinessLogic result; + + testSubject = createTestSubject(); + result = testSubject.getServiceBusinessLogic(); + } + + @Test + public void testSetServiceBusinessLogic() { + ServiceImportManager testSubject; + ServiceBusinessLogic serviceBusinessLogic = null; + + testSubject = createTestSubject(); + testSubject.setServiceBusinessLogic(serviceBusinessLogic); + } + + @Test + public void testPopulateServiceMetadata() { + UploadServiceInfo serviceMetaData = null; + Service service = null; + if (serviceMetaData != null || service != null) { + serviceImportManager.populateServiceMetadata(serviceMetaData, service); + } + } + +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportParseLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportParseLogicTest.java new file mode 100644 index 0000000000..18408dc913 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportParseLogicTest.java @@ -0,0 +1,2152 @@ +/* + * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.be.components.impl; + +import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyObject; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.be.auditing.impl.AuditTestUtils.RESOURCE_NAME; + +import fj.data.Either; +import java.io.IOException; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashSet; +import java.util.List; + +import java.util.Map.Entry; +import java.util.Set; +import org.apache.commons.collections.map.HashedMap; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.MutablePair; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.ElementOperationMock; +import org.openecomp.sdc.be.auditing.impl.AuditingManager; +import org.openecomp.sdc.be.components.csar.CsarInfo; +import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum; +import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; +import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.CapabilityDefinition; +import org.openecomp.sdc.be.model.CapabilityTypeDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceInput; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.GroupDefinition; +import org.openecomp.sdc.be.model.InputDefinition; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.NodeTypeInfo; +import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; +import org.openecomp.sdc.be.model.RequirementDefinition; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.UploadCapInfo; +import org.openecomp.sdc.be.model.UploadComponentInstanceInfo; +import org.openecomp.sdc.be.model.UploadPropInfo; +import org.openecomp.sdc.be.model.UploadReqInfo; +import org.openecomp.sdc.be.model.UploadResourceInfo; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.category.CategoryDefinition; +import org.openecomp.sdc.be.model.category.SubCategoryDefinition; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation; +import org.openecomp.sdc.be.model.User; + +import java.util.HashMap; +import java.util.Map; +import org.openecomp.sdc.be.model.operations.api.IElementOperation; +import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.user.Role; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.util.ValidationUtils; +import org.openecomp.sdc.exception.ResponseFormat; + +public class ServiceImportParseLogicTest extends ServiceImportBussinessLogicBaseTestSetup { + + ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class)); + ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); + ServiceBusinessLogic serviceBusinessLogic = Mockito.mock(ServiceBusinessLogic.class); + ICapabilityTypeOperation capabilityTypeOperation = Mockito.mock(ICapabilityTypeOperation.class); + IElementOperation elementDao = Mockito.mock(IElementOperation.class); + IInterfaceLifecycleOperation interfaceTypeOperation = Mockito.mock(IInterfaceLifecycleOperation.class); + InputsBusinessLogic inputsBusinessLogic = Mockito.mock(InputsBusinessLogic.class); + + private static final String RESOURCE_NAME = "My-Resource_Name with space"; + private static final String RESOURCE_TOSCA_NAME = "My-Resource_Tosca_Name"; + private static final String GENERIC_ROOT_NAME = "tosca.nodes.Root"; + private static final String GENERIC_VF_NAME = "org.openecomp.resource.abstract.nodes.VF"; + private static final String GENERIC_CR_NAME = "org.openecomp.resource.abstract.nodes.CR"; + private static final String GENERIC_PNF_NAME = "org.openecomp.resource.abstract.nodes.PNF"; + private static final String RESOURCE_CATEGORY1 = "Network Layer 2-3"; + private static final String RESOURCE_SUBCATEGORY = "Router"; + + @InjectMocks + private ServiceImportParseLogic serviceImportParseLogic; + ResponseFormatManager responseManager = null; + User user = null; + + private ServiceImportParseLogic createTestSubject() { + return new ServiceImportParseLogic(); + } + + ServiceImportParseLogic bl; + + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + + // Elements + mockElementDao = new ElementOperationMock(); + + // User data and management + user = new User(); + user.setUserId("jh0003"); + user.setFirstName("Jimmi"); + user.setLastName("Hendrix"); + user.setRole(Role.ADMIN.name()); + responseManager = ResponseFormatManager.getInstance(); + + bl = new ServiceImportParseLogic(); + bl.setComponentsUtils(componentsUtils); + bl.setToscaOperationFacade(toscaOperationFacade); + serviceBusinessLogic.setElementDao(elementDao); + bl.setServiceBusinessLogic(serviceBusinessLogic); + bl.setCapabilityTypeOperation(capabilityTypeOperation); + bl.setInterfaceTypeOperation(interfaceTypeOperation); + bl.setInputsBusinessLogic(inputsBusinessLogic); + + } + + @Test + public void testGetServiceBusinessLogic() { + ServiceImportParseLogic testSubject; + ServiceBusinessLogic result; + + testSubject = createTestSubject(); + result = testSubject.getServiceBusinessLogic(); + } + + @Test + public void testSetServiceBusinessLogic() { + ServiceImportParseLogic testSubject; + ServiceBusinessLogic serviceBusinessLogic = null; + + testSubject = createTestSubject(); + testSubject.setServiceBusinessLogic(serviceBusinessLogic); + } + + @Test + public void testGetCapabilityTypeOperation() { + ServiceImportParseLogic testSubject; + ICapabilityTypeOperation result; + + testSubject = createTestSubject(); + result = testSubject.getCapabilityTypeOperation(); + } + + @Test + public void testSetCapabilityTypeOperation() { + ServiceImportParseLogic testSubject; + ICapabilityTypeOperation iCapabilityTypeOperation = null; + + testSubject = createTestSubject(); + testSubject.setCapabilityTypeOperation(iCapabilityTypeOperation); + } + + private CsarInfo createCsarInfo() { + Map<String, byte[]> csar = new HashMap<>(); + User user = new User(); + CsarInfo csarInfo = new CsarInfo(user, "csar_UUID", csar, "vfResourceName", "mainTemplateName", + "mainTemplateContent", true); + csarInfo.setVfResourceName("vfResourceName"); + csarInfo.setCsar(csar); + csarInfo.setCsarUUID("csarUUID"); + csarInfo.setModifier(user); + csarInfo.setUpdate(true); + return csarInfo; + } + + @Test + public void testFindNodeTypesArtifactsToHandle() { + ServiceImportParseLogic testSubject = createTestSubject(); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashedMap(); + final Service service = createServiceObject(false); + + bl.findNodeTypesArtifactsToHandle( + nodeTypesInfo, getCsarInfo(), service); + } + + @Test + public void testBuildNodeTypeYaml() { + Map.Entry<String, Object> nodeNameValue = new Entry<String, Object>() { + @Override + public String getKey() { + return null; + } + + @Override + public Object getValue() { + return null; + } + + @Override + public Object setValue(Object value) { + return null; + } + }; + Map<String, Object> mapToConvert = new HashMap<>(); + String nodeResourceType = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX; + + try { + bl.buildNodeTypeYaml( + nodeNameValue, mapToConvert, nodeResourceType, getCsarInfo()); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_TOSCA_TEMPLATE, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testFindAddNodeTypeArtifactsToHandle() { + + Map<String, List<ArtifactDefinition>> extractedVfcsArtifacts = new HashMap<>(); + Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>(); + String namespace = "namespace"; + + ImmutablePair p1 = ImmutablePair.<String, String>of("s", "sd"); + Map<String, NodeTypeInfo> nodeTypesInfo = new HashedMap(); + final Service service = createServiceObject(false); + Resource resource = new Resource(); + Either<Component, StorageOperationStatus> getCompLatestResult = Either.left(resource); + when(toscaOperationFacade.getLatestByToscaResourceName(anyString())) + .thenReturn(getCompLatestResult); + try { + bl.findAddNodeTypeArtifactsToHandle(getCsarInfo(), nodeTypesArtifactsToHandle, service, + extractedVfcsArtifacts, namespace, p1); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_TOSCA_TEMPLATE, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testHandleAndAddExtractedVfcsArtifacts() { + + List<ArtifactDefinition> vfcArtifacts = new ArrayList<>(); + List<ArtifactDefinition> artifactsToAdd = new ArrayList<>(); + bl.handleAndAddExtractedVfcsArtifacts(vfcArtifacts, artifactsToAdd); + } + + @Test + public void testFindNodeTypeArtifactsToHandle() { + + Resource curNodeType = createParseResourceObject(true); + List<ArtifactDefinition> extractedArtifacts = new ArrayList<>(); + bl.findNodeTypeArtifactsToHandle(curNodeType, extractedArtifacts); + } + + @Test + public void testCollectExistingArtifacts() { + + Resource curNodeType = createParseResourceObject(true); + bl.collectExistingArtifacts(curNodeType); + } + + @Test + public void testPutFoundArtifacts() { + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + List<ArtifactDefinition> artifactsToUpload = new ArrayList<>(); + artifactsToUpload.add(artifactDefinition); + List<ArtifactDefinition> artifactsToUpdate = new ArrayList<>(); + artifactsToUpdate.add(artifactDefinition); + List<ArtifactDefinition> artifactsToDelete = new ArrayList<>(); + artifactsToDelete.add(artifactDefinition); + bl.putFoundArtifacts(artifactsToUpload, artifactsToUpdate, artifactsToDelete); + } + + @Test + public void testProcessExistingNodeTypeArtifacts() { + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + List<ArtifactDefinition> extractedArtifacts = new ArrayList<>(); + extractedArtifacts.add(artifactDefinition); + List<ArtifactDefinition> artifactsToUpload = new ArrayList<>(); + artifactsToUpload.add(artifactDefinition); + List<ArtifactDefinition> artifactsToUpdate = new ArrayList<>(); + artifactsToUpdate.add(artifactDefinition); + List<ArtifactDefinition> artifactsToDelete = new ArrayList<>(); + artifactsToDelete.add(artifactDefinition); + Map<String, ArtifactDefinition> existingArtifacts = new HashMap<>(); + existingArtifacts.put("test", artifactDefinition); + try { + bl.processExistingNodeTypeArtifacts(extractedArtifacts, artifactsToUpload, artifactsToUpdate, + artifactsToDelete, existingArtifacts); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + + } + + @Test + public void testProcessNodeTypeArtifact() { + List<ArtifactDefinition> artifactsToUpload = new ArrayList<>(); + List<ArtifactDefinition> artifactsToUpdate = new ArrayList<>(); + Map<String, ArtifactDefinition> existingArtifacts = new HashMap<>(); + ArtifactDefinition currNewArtifact = new ArtifactDefinition(); + bl.processNodeTypeArtifact(artifactsToUpload, artifactsToUpdate, existingArtifacts, currNewArtifact); + } + + @Test + public void testUpdateFoundArtifact() { + List<ArtifactDefinition> artifactsToUpdate = new ArrayList<>(); + ArtifactDefinition currNewArtifact = new ArtifactDefinition(); + currNewArtifact.setArtifactChecksum("090909"); + currNewArtifact.setPayloadData("data"); + ArtifactDefinition foundArtifact = new ArtifactDefinition(); + foundArtifact.setArtifactChecksum("08767"); + bl.updateFoundArtifact(artifactsToUpdate, currNewArtifact, foundArtifact); + } + + @Test + public void testIsArtifactDeletionRequired() { + String artifactId = "artifactId"; + byte[] artifactFileBytes = new byte[100]; + boolean isFromCsar = true; + bl.isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar); + } + + @Test + public void testFillGroupsFinalFields() { + List<GroupDefinition> groupsAsList = new ArrayList<>(); + bl.fillGroupsFinalFields(groupsAsList); + } + + @Test + public void testGetComponentTypeForResponse() { + Resource resource = createParseResourceObject(true); + bl.getComponentTypeForResponse(resource); + } + + @Test + public void testGetComponentTypeForResponseByService() { + Service service = createServiceObject(true); + bl.getComponentTypeForResponse(service); + } + + @Test + public void testIsfillGroupMemebersRecursivlyStopCondition() { + String groupName = "groupName"; + Map<String, GroupDefinition> allGroups = new HashMap<>(); + Set<String> allGroupMembers = new HashSet<>(); + bl.isfillGroupMemebersRecursivlyStopCondition(groupName, allGroups, allGroupMembers); + } + + @Test + public void testIsfillGroupMemebersRecursivlyStopCondition2() { + String groupName = "groupName"; + Map<String, GroupDefinition> allGroups = new HashMap<>(); + GroupDefinition groupDefinition = new GroupDefinition(); + Map<String, String> members = new HashMap<>(); + members.put("members", "members"); + groupDefinition.setMembers(members); + allGroups.put(groupName, groupDefinition); + Set<String> allGroupMembers = new HashSet<>(); + bl.isfillGroupMemebersRecursivlyStopCondition(groupName, allGroups, allGroupMembers); + } + + @Test + public void testBuildValidComplexVfc() { + Resource resource = createParseResourceObject(true); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + Map<String, NodeTypeInfo> nodesInfo = new HashMap<>(); + NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); + List<String> derivedFrom = new ArrayList<>(); + derivedFrom.add("derivedFrom"); + nodeTypeInfo.setDerivedFrom(derivedFrom); + nodesInfo.put(nodeName, nodeTypeInfo); + + try { + bl.buildValidComplexVfc(resource, getCsarInfo(), nodeName, nodesInfo); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateResourceBeforeCreate() { + Resource resource = createParseResourceObject(true); + + try { + bl.getServiceBusinessLogic().setElementDao(elementDao); + bl.validateResourceBeforeCreate(resource, user, AuditingActionEnum.IMPORT_RESOURCE,false, getCsarInfo()); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + + } + + @Test + public void testValidateResourceType() { + Resource resource = createParseResourceObject(true); + bl.validateResourceType( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } + + @Test + public void testValidateResourceTypeIsEmpty() { + Resource resource = new Resource(); + resource.setResourceType(null); + bl.validateResourceType(user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } + + @Test + public void testValidateLifecycleTypesCreate() { + Resource resource = createParseResourceObject(true); + Map<String, InterfaceDefinition> mapInterfaces = new HashMap<>(); + InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); + String uniqueId = "01932342212"; + interfaceDefinition.setUniqueId(uniqueId); + mapInterfaces.put("uniqueId", interfaceDefinition); + resource.setInterfaces(mapInterfaces); + when(interfaceTypeOperation.getInterface(anyString())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + bl.validateLifecycleTypesCreate(user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } + + @Test + public void testValidateCapabilityTypesCreate() { + Resource resource = createParseResourceObject(true); + Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>(); + String uniqueId = "18982938994"; + List<CapabilityDefinition> capabilityDefinitionList= new ArrayList<>(); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinitionList.add(capabilityDefinition); + capabilities.put(uniqueId, capabilityDefinitionList); + resource.setCapabilities(capabilities); + when(capabilityTypeOperation.getCapabilityType(anyString(), anyBoolean())). + thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + try { + bl.validateCapabilityTypesCreate(user, bl.getCapabilityTypeOperation(), resource, + AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_TOSCA_TEMPLATE, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateCapabilityTypesCreateWhenHaveCapability() { + Resource resource = createParseResourceObject(true); + Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>(); + String uniqueId = "18982938994"; + List<CapabilityDefinition> capabilityDefinitionList= new ArrayList<>(); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinitionList.add(capabilityDefinition); + capabilities.put(uniqueId, capabilityDefinitionList); + resource.setCapabilities(capabilities); + CapabilityTypeDefinition capabilityTypeDefinition = new CapabilityTypeDefinition(); + when(capabilityTypeOperation.getCapabilityType(anyString(), anyBoolean())). + thenReturn(Either.left(capabilityTypeDefinition)); + try { + bl.validateCapabilityTypesCreate(user, bl.getCapabilityTypeOperation(), resource, + AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_TOSCA_TEMPLATE, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateCapabilityTypeExists() { + Resource resource = createParseResourceObject(true); + Either<Boolean, ResponseFormat> eitherResult = Either.left(true); + for (Map.Entry<String, List<CapabilityDefinition>> typeEntry : resource.getCapabilities().entrySet()) { + + bl.validateCapabilityTypeExists(user, bl.getCapabilityTypeOperation(), resource, + AuditingActionEnum.IMPORT_RESOURCE, + eitherResult, typeEntry, false); + } + } + + @Test + public void testValidateCapabilityTypeExistsWhenPropertiesIsNull() { + Resource resource = createParseResourceObject(true); + Either<Boolean, ResponseFormat> eitherResult = Either.left(true); + CapabilityTypeDefinition capabilityTypeDefinition = new CapabilityTypeDefinition(); + + String uniqueId = "0987348532"; + PropertyDefinition propertyDefinition = new PropertyDefinition(); + Map<String, PropertyDefinition> properties = new HashMap<>(); + properties.put(uniqueId, propertyDefinition); + capabilityTypeDefinition.setProperties(properties); + + Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>(); + List<CapabilityDefinition> capabilityDefinitionList = new ArrayList<>(); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinitionList.add(capabilityDefinition); + capabilities.put(uniqueId, capabilityDefinitionList); + resource.setCapabilities(capabilities); + + when(capabilityTypeOperation.getCapabilityType(anyString(), anyBoolean())). + thenReturn(Either.left(capabilityTypeDefinition)); + for (Map.Entry<String, List<CapabilityDefinition>> typeEntry : resource.getCapabilities().entrySet()) { + + bl.validateCapabilityTypeExists(user, bl.getCapabilityTypeOperation(), resource, + AuditingActionEnum.IMPORT_RESOURCE, + eitherResult, typeEntry, false); + } + } + + @Test + public void testValidateCapabilityTypeExistsWhenPropertiesNotNull() { + Resource resource = createParseResourceObject(true); + Either<Boolean, ResponseFormat> eitherResult = Either.left(true); + CapabilityTypeDefinition capabilityTypeDefinition = new CapabilityTypeDefinition(); + + String uniqueId = "0987348532"; + PropertyDefinition propertyDefinition = new PropertyDefinition(); + propertyDefinition.setName(uniqueId); + Map<String, PropertyDefinition> properties = new HashMap<>(); + properties.put(uniqueId, propertyDefinition); + capabilityTypeDefinition.setProperties(properties); + + Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>(); + + + List<CapabilityDefinition> capabilityDefinitionList = new ArrayList<>(); + List<ComponentInstanceProperty> componentInstancePropertyList = new ArrayList<>(); + ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(); + componentInstanceProperty.setValueUniqueUid(uniqueId); + componentInstanceProperty.setName(uniqueId); + componentInstancePropertyList.add(componentInstanceProperty); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setProperties(componentInstancePropertyList); + capabilityDefinitionList.add(capabilityDefinition); + + capabilities.put(uniqueId, capabilityDefinitionList); + resource.setCapabilities(capabilities); + + + when(capabilityTypeOperation.getCapabilityType(anyString(), anyBoolean())). + thenReturn(Either.left(capabilityTypeDefinition)); + for (Map.Entry<String, List<CapabilityDefinition>> typeEntry : resource.getCapabilities().entrySet()) { + + bl.validateCapabilityTypeExists(user, bl.getCapabilityTypeOperation(), resource, + AuditingActionEnum.IMPORT_RESOURCE, + eitherResult, typeEntry, false); + } + } + + @Test + public void testValidateCapabilityTypeExists2() { + Resource resource = createParseResourceObject(true); + Either<Boolean, ResponseFormat> eitherResult = Either.left(true); + when(capabilityTypeOperation.getCapabilityType(anyString(), anyBoolean())). + thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + try { + for (String type : resource.getRequirements().keySet()) { + bl.validateCapabilityTypeExists(user, bl.getCapabilityTypeOperation(), resource, + resource.getRequirements().get(type), AuditingActionEnum.IMPORT_RESOURCE, eitherResult, type, + false); + } + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_TOSCA_TEMPLATE, + ComponentTypeEnum.RESOURCE.getValue()); + } + + + } + + @Test + public void testValidateResourceFieldsBeforeCreate() { + Resource resource = createParseResourceObject(true); + try { + bl.validateResourceFieldsBeforeCreate( user, resource, AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateDerivedFromExist() { + Resource resource = createParseResourceObject(true); + try { + when(toscaOperationFacade.validateToscaResourceNameExists(anyString())) + .thenReturn(Either.left(true)); + bl.validateDerivedFromExist( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateDerivedFromExistFailure1() { + Resource resource = createParseResourceObject(true); + try { + when(toscaOperationFacade.validateToscaResourceNameExists(anyString())) + .thenReturn(Either.left(false)); + bl.validateDerivedFromExist( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.PARENT_RESOURCE_NOT_FOUND, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateDerivedFromExistFailure2() { + Resource resource = createParseResourceObject(true); + try { + when(toscaOperationFacade.validateToscaResourceNameExists(anyString())) + .thenReturn(Either.right(StorageOperationStatus.OK)); + bl.validateDerivedFromExist( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.OK, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateLicenseType() { + Resource resource = createParseResourceObject(true); + + try { + bl.validateLicenseType( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_CONTENT, + ComponentTypeEnum.RESOURCE.getValue()); + } + + + } + + @Test + public void testValidateCost() { + Resource resource = createParseResourceObject(true); + try { + bl.validateCost( resource); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_CONTENT, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateResourceVendorModelNumber() { + Resource resource = createParseResourceObject(true); + bl.validateResourceVendorModelNumber( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } + + @Test + public void testValidateResourceVendorModelNumberWrongLen() { + Resource resource = createParseResourceObject(true); + resource.setResourceVendorModelNumber("000000000011122221111222333444443222556677788778889999998776554332340"); + try { + bl.validateResourceVendorModelNumber( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + e.printStackTrace(); + } + } + + @Test + public void testValidateResourceVendorModelNumberWrongValue() { + Resource resource = createParseResourceObject(true); + resource.setResourceVendorModelNumber(""); + try { + bl.validateResourceVendorModelNumber( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateVendorReleaseName() { + Resource resource = createParseResourceObject(true); + resource.setVendorRelease("0.1"); + bl.validateVendorReleaseName( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } + + @Test + public void testValidateVendorReleaseNameFailure() { + Resource resource = createParseResourceObject(true); + resource.setVendorRelease(""); + try { + bl.validateVendorReleaseName( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.MISSING_VENDOR_RELEASE, + ComponentTypeEnum.RESOURCE.getValue()); + } + + } + + @Test + public void testValidateVendorReleaseNameWrongLen() { + Resource resource = createParseResourceObject(true); + resource.setVendorRelease("000000000011122221111222333444443222556677788778889999998776554332340"); + try { + bl.validateVendorReleaseName(user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + e.printStackTrace(); + } + } + + @Test + public void testValidateCategory() { + Resource resource = createParseResourceObject(true); + try { + bl.validateCategory( user, resource, AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateEmptyCategory() { + Resource resource = createParseResourceObject(true); + resource.setCategories(null); + try { + bl.validateCategory(user, resource, AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.COMPONENT_MISSING_CATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateCategorySizeBiggerThan1() { + Resource resource = createParseResourceObject(true); + List<CategoryDefinition> categories = new ArrayList<>(); + CategoryDefinition categoryDefinition1 = new CategoryDefinition(); + CategoryDefinition categoryDefinition2 = new CategoryDefinition(); + categories.add(categoryDefinition1); + categories.add(categoryDefinition2); + + resource.setCategories(categories); + try { + bl.validateCategory(user, resource, AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.COMPONENT_TOO_MUCH_CATEGORIES, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateEmptySubCategory() { + Resource resource = createParseResourceObject(true); + List<CategoryDefinition> categories = resource.getCategories(); + CategoryDefinition categoryDefinition = categories.get(0); + categoryDefinition.setSubcategories(null); + + try { + bl.validateCategory(user, resource, AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.COMPONENT_MISSING_SUBCATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateEmptySubCategorySizeBiggerThan1() { + Resource resource = createParseResourceObject(true); + List<CategoryDefinition> categories = resource.getCategories(); + CategoryDefinition categoryDefinition = categories.get(0); + List<SubCategoryDefinition> subcategories = categoryDefinition.getSubcategories(); + SubCategoryDefinition subCategoryDefinition1 = new SubCategoryDefinition(); + SubCategoryDefinition subCategoryDefinition2 = new SubCategoryDefinition(); + subcategories.add(subCategoryDefinition1); + subcategories.add(subCategoryDefinition2); + + try { + bl.validateCategory(user, resource, AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.RESOURCE_TOO_MUCH_SUBCATEGORIES, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateEmptyCategoryName() { + Resource resource = createParseResourceObject(true); + List<CategoryDefinition> categories = resource.getCategories(); + CategoryDefinition categoryDefinition = categories.get(0); + categoryDefinition.setName(null); + + try { + bl.validateCategory(user, resource, AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.COMPONENT_MISSING_CATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateEmptySubCategoryName() { + Resource resource = createParseResourceObject(true); + List<CategoryDefinition> categories = resource.getCategories(); + CategoryDefinition categoryDefinition = categories.get(0); + List<SubCategoryDefinition> subcategories = categoryDefinition.getSubcategories(); + SubCategoryDefinition subCategoryDefinition1 = subcategories.get(0); + subCategoryDefinition1.setName(null); + + try { + bl.validateCategory(user, resource, AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.COMPONENT_MISSING_SUBCATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateCategoryListed() { + Resource resource = createParseResourceObject(true); + CategoryDefinition category = resource.getCategories().get(0); + SubCategoryDefinition subcategory = category.getSubcategories().get(0); + try { + bl.validateCategoryListed( category, subcategory, user, resource, AuditingActionEnum.IMPORT_RESOURCE, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testFailOnInvalidCategory() { + Resource resource = createParseResourceObject(true); + try { + bl.failOnInvalidCategory( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, + ComponentTypeEnum.RESOURCE.getValue()); + } + + } + + @Test + public void testValidateVendorName() { + Resource resource = createParseResourceObject(true); + try { + bl.validateVendorName( user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateVendorNameEmpty() { + Resource resource = createParseResourceObject(true); + resource.setVendorName(null); + try { + bl.validateVendorName(user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.MISSING_VENDOR_NAME, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateVendorNameWrongLen() { + Resource resource = createParseResourceObject(true); + resource.setVendorName("000000000011122221111222333444443222556677788778889999998776554332340"); + try { + bl.validateVendorName(user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + e.printStackTrace(); + } + } + @Test + public void testValidateVendorName2() { + Resource resource = createParseResourceObject(true); + CategoryDefinition category = resource.getCategories().get(0); + SubCategoryDefinition subcategory = category.getSubcategories().get(0); + String vendorName = "vendorName"; + try { + bl.validateVendorName( vendorName, user, resource, AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testFillResourceMetadata2() { + String yamlName = "yamlName"; + Resource resourceVf = createParseResourceObject(true); + String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + "test"; + resourceVf.setSystemName("systemName"); + try { + bl.fillResourceMetadata( yamlName, resourceVf, nodeName, user); + } catch (ComponentException e) { + e.printStackTrace(); + } + } + + @Test + public void testFillResourceMetadataWrongStart() { + String yamlName = "yamlName"; + Resource resourceVf = createParseResourceObject(true); + String nodeName = "WrongStart" + "test"; + try { + bl.fillResourceMetadata(yamlName, resourceVf, nodeName, user); + } catch (ComponentException e) { + e.printStackTrace(); + } + } + + @Test + public void testFillResourceMetadataResourceTypeIsAbs() { + String yamlName = "yamlName"; + Resource resourceVf = createParseResourceObject(true); + String nodeName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX + Constants.ABSTRACT; + try { + bl.fillResourceMetadata(yamlName, resourceVf, nodeName, user); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_NODE_TEMPLATE, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testGetNodeTypeActualName() { + String fullName = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX+"test"; + try { + bl.getNodeTypeActualName(fullName); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testAddInput() { + Map<String, InputDefinition> currPropertiesMap = new HashMap<>(); + InputDefinition prop = new InputDefinition(); + try { + bl.addInput(currPropertiesMap, prop); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testFindAviableRequirement() { + String uniqueId = "101929382910"; + String regName = uniqueId; + String yamlName = uniqueId; + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + ComponentInstance currentCompInstance = new ComponentInstance(); + + Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); + List<RequirementDefinition> requirementDefinitionList = new ArrayList<>(); + RequirementDefinition requirementDefinition = new RequirementDefinition(); + requirementDefinition.setName(uniqueId); + requirementDefinition.setMaxOccurrences("10"); + requirementDefinition.setLeftOccurrences("3"); + requirementDefinitionList.add(requirementDefinition); + requirements.put(uniqueId, requirementDefinitionList); + currentCompInstance.setRequirements(requirements); + + String capName = "capName"; + try { + bl.findAviableRequiremen(regName, yamlName, uploadComponentInstanceInfo, currentCompInstance, capName); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_NODE_TEMPLATE, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testFindAviableRequirementSameCapName() { + String uniqueId = "101929382910"; + String regName = uniqueId; + String yamlName = uniqueId; + UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo(); + ComponentInstance currentCompInstance = new ComponentInstance(); + + Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); + List<RequirementDefinition> requirementDefinitionList = new ArrayList<>(); + RequirementDefinition requirementDefinition = new RequirementDefinition(); + requirementDefinition.setName(uniqueId); + requirementDefinition.setMaxOccurrences("10"); + requirementDefinition.setLeftOccurrences("3"); + requirementDefinitionList.add(requirementDefinition); + requirements.put(uniqueId, requirementDefinitionList); + currentCompInstance.setRequirements(requirements); + + String capName = uniqueId; + try { + bl.findAviableRequiremen(regName, yamlName, uploadComponentInstanceInfo, currentCompInstance, capName); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.INVALID_NODE_TEMPLATE, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testFindAvailableCapabilityByTypeOrName() { + RequirementDefinition validReq = new RequirementDefinition(); + ComponentInstance currentCapCompInstance = new ComponentInstance(); + UploadReqInfo uploadReqInfo = new UploadReqInfo(); + + try { + bl.findAvailableCapabilityByTypeOrName(validReq, currentCapCompInstance, uploadReqInfo); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + + @Test + public void testFindAvailableCapability() { + String uniqueId = "23422345677"; + RequirementDefinition validReq = new RequirementDefinition(); + validReq.setCapability(uniqueId); + ComponentInstance instance = new ComponentInstance(); + Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>(); + List<CapabilityDefinition> capabilityDefinitionList = new ArrayList<>(); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setMaxOccurrences("3"); + capabilityDefinition.setLeftOccurrences("2"); + capabilityDefinitionList.add(capabilityDefinition); + capabilityMap.put(uniqueId, capabilityDefinitionList); + instance.setCapabilities(capabilityMap); + + try { + bl.findAvailableCapability(validReq, instance); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testfindAvailableCapability2() { + String uniqueId = "23422345677"; + RequirementDefinition validReq = new RequirementDefinition(); + validReq.setCapability(uniqueId); + ComponentInstance instance = new ComponentInstance(); + Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>(); + List<CapabilityDefinition> capabilityDefinitionList = new ArrayList<>(); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setName(uniqueId); + capabilityDefinition.setMaxOccurrences("3"); + capabilityDefinition.setLeftOccurrences("2"); + capabilityDefinitionList.add(capabilityDefinition); + capabilityMap.put(uniqueId, capabilityDefinitionList); + instance.setCapabilities(capabilityMap); + UploadReqInfo uploadReqInfo = new UploadReqInfo(); + uploadReqInfo.setCapabilityName(uniqueId); + try { + bl.findAvailableCapability(validReq, instance, uploadReqInfo); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testGetComponentWithInstancesFilter() { + try { + bl.getComponentWithInstancesFilter(); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testCreateParseResourceObject() { + String key = "0923928394"; + List<UploadCapInfo> capabilities = new ArrayList<>(); + UploadCapInfo uploadCapInfo = new UploadCapInfo(); + uploadCapInfo.setType(key); + capabilities.add(uploadCapInfo); + + String resourceId = "resourceId"; + Map<String, List<CapabilityDefinition>> defaultCapabilities = new HashMap<>(); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setName(key); + List<CapabilityDefinition> capabilityDefinitionList = new ArrayList<>(); + capabilityDefinitionList.add(capabilityDefinition); + defaultCapabilities.put(key, capabilityDefinitionList); + + Map<String, List<CapabilityDefinition>> validCapabilitiesMap = new HashMap<>(); + InputDefinition prop = new InputDefinition(); + + Resource resource = createParseResourceObject(true); + Map<String, List<CapabilityDefinition>> capabilitiesMap = resource.getCapabilities(); + capabilitiesMap.put(key, capabilityDefinitionList); + + when(toscaOperationFacade.getToscaFullElement(anyString())) + .thenReturn(Either.left(resource)); + + try { + bl.addValidComponentInstanceCapabilities(key, capabilities, resourceId, defaultCapabilities, + validCapabilitiesMap); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testGetCapabilityFailure() { + String resourceId = "resourceId"; + String key = "0923928394"; + Map<String, List<CapabilityDefinition>> defaultCapabilities = new HashMap<>(); + CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); + capabilityDefinition.setName(key); + List<CapabilityDefinition> capabilityDefinitionList = new ArrayList<>(); + capabilityDefinitionList.add(capabilityDefinition); + defaultCapabilities.put(key, capabilityDefinitionList); + String capabilityType = key; + when(toscaOperationFacade.getToscaFullElement(anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + try { + bl.getCapability(resourceId, defaultCapabilities, capabilityType); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.COMPONENT_NOT_FOUND, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + + @Test + public void testValidateCapabilityProperties() { + List<UploadCapInfo> capabilities = new ArrayList<>(); + UploadCapInfo uploadCapInfo = new UploadCapInfo(); + List<UploadPropInfo> properties = new ArrayList<>(); + UploadPropInfo uploadPropInfo = new UploadPropInfo(); + properties.add(uploadPropInfo); + uploadCapInfo.setProperties(properties); + capabilities.add(uploadCapInfo); + String resourceId = "resourceId"; + CapabilityDefinition defaultCapability = new CapabilityDefinition(); + defaultCapability.setProperties(null); + defaultCapability.setName("test"); + + try { + bl.validateCapabilityProperties(capabilities, resourceId, defaultCapability); + } catch (ComponentException e) { + e.printStackTrace(); + } + } + + @Test + public void testValidateUniquenessUpdateUploadedComponentInstanceCapability() { + String key = "02124568"; + List<UploadCapInfo> capabilities = new ArrayList<>(); + UploadCapInfo uploadCapInfo = new UploadCapInfo(); + List<UploadPropInfo> properties = new ArrayList<>(); + UploadPropInfo uploadPropInfo = new UploadPropInfo(); + uploadPropInfo.setName(key); + properties.add(uploadPropInfo); + uploadCapInfo.setProperties(properties); + capabilities.add(uploadCapInfo); + String resourceId = "resourceId"; + CapabilityDefinition defaultCapability = new CapabilityDefinition(); + List<ComponentInstanceProperty> componentInstancePropertyList = new ArrayList<>(); + ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(); + componentInstancePropertyList.add(componentInstanceProperty); + defaultCapability.setProperties(componentInstancePropertyList); + defaultCapability.setName(key); + + try { + bl.validateUniquenessUpdateUploadedComponentInstanceCapability(defaultCapability, uploadCapInfo); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testSetDeploymentArtifactsPlaceHolderByResource() { + Resource resource = createParseResourceObject(true); + + try { + bl.setDeploymentArtifactsPlaceHolder(resource, user); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testSetDeploymentArtifactsPlaceHolderByService() { + Service Service = createServiceObject(true); + + try { + bl.setDeploymentArtifactsPlaceHolder(Service, user); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testProcessDeploymentResourceArtifacts() { + Resource resource = createParseResourceObject(true); + Map<String, ArtifactDefinition> artifactMap = new HashMap<>(); + String k = "key"; + Object v = new Object(); + try { + bl.processDeploymentResourceArtifacts(user, resource, artifactMap, k, v); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testMergeOldResourceMetadataWithNew() { + Resource oldResource = createParseResourceObject(true); + Resource newResource = new Resource(); + + try { + bl.mergeOldResourceMetadataWithNew(oldResource, newResource); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testBuildComplexVfcMetadata() { + Resource resource = createParseResourceObject(true); + String nodeName = "org.openecomp.resource.derivedFrom.zxjTestImportServiceAb.test"; + Map<String, NodeTypeInfo> nodesInfo = new HashMap<>(); + NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); + List<String> derivedFrom = new ArrayList<>(); + derivedFrom.add("derivedFrom"); + nodeTypeInfo.setDerivedFrom(derivedFrom); + nodesInfo.put(nodeName, nodeTypeInfo); + + try { + bl.buildComplexVfcMetadata(getCsarInfo(), nodeName, nodesInfo); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateResourceCreationFromNodeType() { + Resource resource = createParseResourceObject(true); + resource.setDerivedFrom(null); + try { + bl.validateResourceCreationFromNodeType(resource, user); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.MISSING_DERIVED_FROM_TEMPLATE, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testCreateInputsOnResource() { + Resource resource = createParseResourceObject(true); + Map<String, InputDefinition> inputs = new HashMap<>(); + + try { + bl.createInputsOnResource(resource, inputs); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testCreateInputsOnResourceWhenIsNotEmpty() { + String key = "12345667"; + Resource resource = createParseResourceObject(true); + List<InputDefinition> inputDefinitionList = new ArrayList<>(); + InputDefinition inputDefinition = new InputDefinition(); + inputDefinitionList.add(inputDefinition); + resource.setInputs(inputDefinitionList); + Map<String, InputDefinition> inputs = new HashMap<>(); + inputs.put(key, inputDefinition); + + try { + bl.createInputsOnResource(resource, inputs); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateInputsOnService() { + Service service = createServiceObject(true); + Map<String, InputDefinition> inputs = new HashMap<>(); + + try { + bl.createInputsOnService(service, inputs); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateServiceTransaction() { + Service service = createServiceObject(true); + + try { + bl.createServiceTransaction(service, user, true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateArtifactsPlaceHolderData() { + Service service = createServiceObject(true); + + try { + bl.createArtifactsPlaceHolderData(service, user); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testSetInformationalArtifactsPlaceHolder() { + Service service = createServiceObject(true); + + try { + bl.setInformationalArtifactsPlaceHolder(service, user); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testValidateNestedDerivedFromDuringUpdate() { + Resource currentResource = createParseResourceObject(true); + Resource updateInfoResource = createParseResourceObject(true); + String key = "2323456"; + + List<String> currentDerivedFromList = new ArrayList<>(); + currentDerivedFromList.add(key); + currentResource.setDerivedFrom(currentDerivedFromList); + List<String> updatedDerivedFromList = new ArrayList<>(); + updatedDerivedFromList.add("23344567778"); + updateInfoResource.setDerivedFrom(updatedDerivedFromList); + + when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + + try { + bl.validateNestedDerivedFromDuringUpdate(currentResource, updateInfoResource, true); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateDerivedFromExtending() { + Resource currentResource = createParseResourceObject(true); + Resource updateInfoResource = createParseResourceObject(true); + + when(toscaOperationFacade.validateToscaResourceNameExtends(anyString(), anyString())) + .thenReturn(Either.left(false)); + try { + bl.validateDerivedFromExtending(user, currentResource, updateInfoResource, + AuditingActionEnum.IMPORT_RESOURCE); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.PARENT_RESOURCE_DOES_NOT_EXTEND, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testValidateResourceFieldsBeforeUpdate() { + Resource currentResource = createParseResourceObject(true); + Resource updateInfoResource = createParseResourceObject(true); + + try { + bl.validateResourceFieldsBeforeUpdate(currentResource, updateInfoResource, true, true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testValidateResourceName() { + Resource currentResource = createParseResourceObject(true); + Resource updateInfoResource = createParseResourceObject(true); + currentResource.setName("test1"); + updateInfoResource.setName("test2"); + + try { + bl.validateResourceName(currentResource, updateInfoResource, true, false); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testIsResourceNameEquals() { + Resource currentResource = createParseResourceObject(true); + Resource updateInfoResource = createParseResourceObject(true); + + try { + bl.isResourceNameEquals(currentResource, updateInfoResource); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testPrepareResourceForUpdate() { + Resource oldResource = createParseResourceObject(true); + Resource newResource = createParseResourceObject(true); + + try { + bl.prepareResourceForUpdate(oldResource, newResource, user, true, true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testFailOnChangeState() { + ResponseFormat response = new ResponseFormat(); + Resource oldResource = createParseResourceObject(true); + Resource newResource = createParseResourceObject(true); + + try { + bl.failOnChangeState(response, user, oldResource, newResource); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testHandleResourceGenericType() { + Resource resource = createParseResourceObject(true); + + try { + bl.handleResourceGenericType(resource); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testUpdateOrCreateGroups() { + Resource resource = createParseResourceObject(true); + Map<String, GroupDefinition> groups = new HashMap<>(); + + try { + bl.updateOrCreateGroups(resource, groups); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testAddGroupsToCreateOrUpdate() { + Resource resource = createParseResourceObject(true); + Map<String, GroupDefinition> groups = new HashMap<>(); + + List<GroupDefinition> groupsFromResource = new ArrayList<>(); + List<GroupDefinition> groupsAsList = new ArrayList<>(); + List<GroupDefinition> groupsToUpdate = new ArrayList<>(); + List<GroupDefinition> groupsToCreate = new ArrayList<>(); + + try { + bl.addGroupsToCreateOrUpdate(groupsFromResource, groupsAsList, groupsToUpdate, groupsToCreate); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testAddGroupsToDelete() { + Map<String, GroupDefinition> groups = new HashMap<>(); + + List<GroupDefinition> groupsFromResource = new ArrayList<>(); + List<GroupDefinition> groupsAsList = new ArrayList<>(); + List<GroupDefinition> groupsToDelete = new ArrayList<>(); + + try { + bl.addGroupsToDelete(groupsFromResource, groupsAsList, groupsToDelete); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testUpdateGroupsMembersUsingResource() { + Service component = createServiceObject(true); + Map<String, GroupDefinition> groups = new HashMap<>(); + + try { + bl.updateGroupsMembersUsingResource(groups, component); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testupdateGroupMembers() { + Service component = createServiceObject(true); + Map<String, GroupDefinition> groups = new HashMap<>(); + GroupDefinition updatedGroupDefinition = new GroupDefinition(); + List<ComponentInstance> componentInstances = new ArrayList<>(); + String groupName = "groupName"; + Map<String, String> members = new HashMap<>(); + + try { + bl.updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members); + } catch (ComponentException e) { + e.printStackTrace(); + } + } + + @Test + public void testValidateCyclicGroupsDependencies() { + Service component = createServiceObject(true); + Map<String, GroupDefinition> groups = new HashMap<>(); + String key = "098738485"; + GroupDefinition groupDefinition = new GroupDefinition(); + groups.put(key, groupDefinition); + + try { + bl.validateCyclicGroupsDependencies(groups); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.GENERAL_ERROR, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + @Test + public void testFillAllGroupMemebersRecursivly() { + Map<String, GroupDefinition> allGroups = new HashMap<>(); + Set<String> allGroupMembers = new HashSet<>(); + String groupName = "groupName"; + + try { + bl.fillAllGroupMemebersRecursivly(groupName, allGroups, allGroupMembers); + } catch (Exception e) { + + } + } + + @Test + public void testFillResourceMetadataForService() { + String yamlName = "yamlName"; + Service resourceVf = createServiceObject(true); + String nodeName = "nodeName"; + + try { + bl.fillResourceMetadata(yamlName, resourceVf, nodeName, user); + } catch (Exception e) { + + } + } + + @Test + public void testpropagateStateToCertified() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(); + + try { + bl.propagateStateToCertified(user, resource, lifecycleChangeInfo, true, true, true); + } catch (Exception e) { + + } + } + + @Test + public void testBuildValidComplexVfc2() { + String nodeName = "nodeName"; + Resource resource = createParseResourceObject(true); + Map<String, NodeTypeInfo> nodesInfo = new HashMap<>(); + + try { + bl.buildValidComplexVfc(getCsarInfo(), nodeName, nodesInfo); + } catch (Exception e) { + + } + } + + @Test + public void testUpdateGroupsOnResource() { + Resource resource = createParseResourceObject(true); + Map<String, GroupDefinition> groups = new HashMap<>(); + + try { + bl.updateGroupsOnResource(resource, groups); + } catch (Exception e) { + + } + } + + @Test + public void testSetInformationalArtifactsPlaceHolder2() { + Resource resource = createParseResourceObject(true); + + try { + bl.setInformationalArtifactsPlaceHolder(resource, user); + } catch (Exception e) { + + } + } + + @Test + public void testCreateArtifactsPlaceHolderData2() { + Resource resource = createParseResourceObject(true); + + try { + bl.createArtifactsPlaceHolderData(resource, user); + } catch (Exception e) { + + } + } + + @Test + public void testHandleGroupsProperties() { + Service service = createServiceObject(true); + Map<String, GroupDefinition> groups = new HashMap<>(); + try { + bl.handleGroupsProperties(service, groups); + } catch (Exception e) { + + } + } + + @Test + public void testHandleGroupsProperties2() { + Resource resource = createParseResourceObject(true); + Map<String, GroupDefinition> groups = new HashMap<>(); + try { + bl.handleGroupsProperties(resource, groups); + } catch (Exception e) { + + } + } + + @Test + public void testHandleGetInputs() { + PropertyDataDefinition property = new PropertyDataDefinition(); + List<InputDefinition> inputs = new ArrayList<>(); + try { + bl.handleGetInputs(property, inputs); + } catch (Exception e) { + + } + } + + @Test + public void testFindInputByName() { + GetInputValueDataDefinition getInput = new GetInputValueDataDefinition(); + List<InputDefinition> inputs = new ArrayList<>(); + try { + bl.findInputByName(inputs, getInput); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateComponentInstancePropertiesToComponent() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>(); + List<InputDefinition> inputs = new ArrayList<>(); + try { + bl.associateComponentInstancePropertiesToComponent(yamlName, resource, instProperties); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateComponentInstanceInputsToComponent() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>(); + + try { + bl.associateComponentInstanceInputsToComponent(yamlName, resource, instInputs); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateDeploymentArtifactsToInstances() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>(); + + try { + bl.associateDeploymentArtifactsToInstances(user, yamlName, resource, instDeploymentArtifacts); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateArtifactsToInstances() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>(); + + try { + bl.associateDeploymentArtifactsToInstances(user, yamlName, resource, instDeploymentArtifacts); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateArtifactsToInstances2() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>(); + + try { + bl.associateArtifactsToInstances(yamlName, resource, instDeploymentArtifacts); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateOrAddCalculatedCapReq() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>(); + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>(); + try { + bl.associateOrAddCalculatedCapReq(yamlName, resource, instCapabilities, instRequirements); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateInstAttributeToComponentToInstances() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + Map<String, List<AttributeDataDefinition>> instAttributes = new HashMap<>(); + try { + bl.associateInstAttributeToComponentToInstances(yamlName, resource, instAttributes); + } catch (Exception e) { + + } + } + + @Test + public void testThrowComponentExceptionByResource() { + StorageOperationStatus status = StorageOperationStatus.OK; + Resource resource = createParseResourceObject(true); + try { + bl.throwComponentExceptionByResource(status, resource); + } catch (Exception e) { + + } + } + + @Test + public void testGetResourceAfterCreateRelations() { + + Resource resource = createParseResourceObject(true); + + try { + bl.getResourceAfterCreateRelations(resource); + } catch (Exception e) { + + } + } + + @Test + public void testSetCapabilityNamesTypes() { + + Map<String, List<CapabilityDefinition>> originCapabilities = new HashMap<>(); + Map<String, List<UploadCapInfo>> uploadedCapabilities = new HashMap<>(); + + try { + bl.setCapabilityNamesTypes(originCapabilities, uploadedCapabilities); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateComponentInstanceInputsToComponent2() { + String yamlName = "yamlName"; + Service service = createServiceObject(true); + Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>(); + + try { + bl.associateComponentInstanceInputsToComponent(yamlName, service, instInputs); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateComponentInstancePropertiesToComponent2() { + String yamlName = "yamlName"; + Service service = createServiceObject(true); + Map<String, List<ComponentInstanceProperty>> instInputs = new HashMap<>(); + + try { + bl.associateComponentInstancePropertiesToComponent(yamlName, service, instInputs); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateDeploymentArtifactsToInstances2() { + String yamlName = "yamlName"; + Service service = createServiceObject(true); + Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>(); + + try { + bl.associateDeploymentArtifactsToInstances(user, yamlName, service, instDeploymentArtifacts); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateArtifactsToInstances3() { + String yamlName = "yamlName"; + Service service = createServiceObject(true); + Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>(); + + try { + bl.associateArtifactsToInstances(yamlName, service, instArtifacts); + } catch (Exception e) { + + } + } + @Test + public void testAssociateOrAddCalculatedCapReq2() { + String yamlName = "yamlName"; + Service resource = createServiceObject(true); + Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>(); + Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>(); + try { + bl.associateOrAddCalculatedCapReq(yamlName, resource, instCapabilities, instRequirements); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateInstAttributeToComponentToInstances2() { + String yamlName = "yamlName"; + Service resource = createServiceObject(true); + Map<String, List<AttributeDataDefinition>> instAttributes = new HashMap<>(); + try { + bl.associateInstAttributeToComponentToInstances(yamlName, resource, instAttributes); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateRequirementsToService() { + String yamlName = "yamlName"; + Service resource = createServiceObject(true); + Map<String, ListRequirementDataDefinition> requirements = new HashMap<>(); + try { + bl.associateRequirementsToService(yamlName, resource, requirements); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateCapabilitiesToService() { + String yamlName = "yamlName"; + Service resource = createServiceObject(true); + Map<String, ListCapabilityDataDefinition> capabilities = new HashMap<>(); + try { + bl.associateCapabilitiesToService(yamlName, resource, capabilities); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateResourceInstances() { + String yamlName = "yamlName"; + Service resource = createServiceObject(true); + List<RequirementCapabilityRelDef> relations = new ArrayList<>(); + try { + bl.associateResourceInstances(yamlName, resource, relations); + } catch (Exception e) { + + } + } + + @Test + public void testAddCapabilities() { + Map<String, List<CapabilityDefinition>> originCapabilities = new HashMap<>(); + String type = "type"; + List<CapabilityDefinition> capabilities = new ArrayList<>(); + try { + bl.addCapabilities(originCapabilities, type, capabilities); + } catch (Exception e) { + + } + } + + @Test + public void testAddCapabilitiesProperties() { + Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>(); + List<UploadCapInfo> capabilities = new ArrayList<>(); + try { + bl.addCapabilitiesProperties(newPropertiesMap, capabilities); + } catch (Exception e) { + + } + } + + @Test + public void testGetServiceWithGroups() { + String resourceId = "resourceId"; + try { + bl.getServiceWithGroups(resourceId); + } catch (Exception e) { + + } + } + + @Test + public void testGetResourceWithGroups() { + String resourceId = "resourceId"; + try { + bl.getResourceWithGroups(resourceId); + } catch (Exception e) { + + } + } + + @Test + public void testAssociateResourceInstances2() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + List<RequirementCapabilityRelDef> relations = new ArrayList<>(); + try { + bl.associateResourceInstances(yamlName, resource, relations); + } catch (Exception e) { + + } + } + + @Test + public void testAddRelationsToRI() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + Map<String, UploadComponentInstanceInfo> uploadResInstancesMap = new HashMap<>(); + List<ComponentInstance> componentInstancesList = new ArrayList<>(); + List<RequirementCapabilityRelDef> relations = new ArrayList<>(); + try { + bl.addRelationsToRI(yamlName, resource, uploadResInstancesMap, componentInstancesList, relations); + } catch (Exception e) { + + } + } + + @Test + public void testAddRelationsToRI2() { + String yamlName = "yamlName"; + Resource resource = createParseResourceObject(true); + UploadComponentInstanceInfo nodesInfoValue = new UploadComponentInstanceInfo(); + List<RequirementCapabilityRelDef> relations = new ArrayList<>(); + try { + bl.addRelationToRI(yamlName, resource, nodesInfoValue, relations); + } catch (Exception e) { + + } + } + + @Test + public void testFindVfcResource() { + Service service = createServiceObject(true); + String currVfcToscaName = "currVfcToscaName"; + String previousVfcToscaName = "previousVfcToscaName"; + UploadComponentInstanceInfo nodesInfoValue = new UploadComponentInstanceInfo(); + List<RequirementCapabilityRelDef> relations = new ArrayList<>(); + try { + bl.findVfcResource(getCsarInfo(), service, currVfcToscaName, previousVfcToscaName, + StorageOperationStatus.OK); + } catch (ComponentException e) { + assertComponentException(e, ActionStatus.OK, + ComponentTypeEnum.RESOURCE.getValue()); + } + } + + protected Resource createParseResourceObject(boolean afterCreate) { + Resource resource = new Resource(); + resource.setName(RESOURCE_NAME); + resource.setToscaResourceName(RESOURCE_TOSCA_NAME); + resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY); + resource.setDescription("My short description"); + List<String> tgs = new ArrayList<>(); + tgs.add("test"); + tgs.add(resource.getName()); + resource.setTags(tgs); + List<String> template = new ArrayList<>(); + template.add("tosca.nodes.Root"); + resource.setDerivedFrom(template); + resource.setVendorName("Motorola"); + resource.setVendorRelease("1.0.0"); + resource.setContactId("ya5467"); + resource.setIcon("defaulticon"); + Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); + List<RequirementDefinition> requirementDefinitionList = new ArrayList<>(); + requirements.put("test", requirementDefinitionList); + resource.setRequirements(requirements); + resource.setCost("cost"); + resource.setResourceVendorModelNumber("02312233"); + + + Map<String, ArtifactDefinition> artifacts = new HashMap<>(); + ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifacts.put("artifact", artifactDefinition); + resource.setArtifacts(artifacts); + + resource.setLicenseType("licType"); + + if (afterCreate) { + resource.setName(resource.getName()); + resource.setVersion("0.1"); + resource.setUniqueId(resource.getName() + .toLowerCase() + ":" + resource.getVersion()); + resource.setCreatorUserId(user.getUserId()); + resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); + resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + } + return resource; + } + + protected CsarInfo getCsarInfo() { + String csarUuid = "0010"; + User user = new User(); + Map<String, byte[]> csar = new HashMap<>(); + String vfReousrceName = "resouceName"; + String mainTemplateName = "mainTemplateName"; + String mainTemplateContent = null; + try { + mainTemplateContent = loadFileNameToJsonString("service_import_template.yml"); + } catch (IOException e) { + e.printStackTrace(); + } + CsarInfo csarInfo = new CsarInfo(user, csarUuid, csar, vfReousrceName, mainTemplateName, mainTemplateContent, + false); + return csarInfo; + } + + public static String loadFileNameToJsonString(String fileName) throws IOException { + String sourceDir = "src/test/resources/normativeTypes"; + return loadFileNameToJsonString(sourceDir, fileName); + } + + private static String loadFileNameToJsonString(String sourceDir, String fileName) throws IOException { + java.nio.file.Path filePath = FileSystems.getDefault().getPath(sourceDir, fileName); + byte[] fileContent = Files.readAllBytes(filePath); + return new String(fileContent); + } + + protected Service createServiceObject (boolean afterCreate) { + Service service = new Service(); + service.setUniqueId("sid"); + service.setName("Service"); + CategoryDefinition category = new CategoryDefinition(); + category.setName(SERVICE_CATEGORY); + category.setIcons(Collections.singletonList("defaulticon")); + List<CategoryDefinition> categories = new ArrayList<>(); + categories.add(category); + service.setCategories(categories); + service.setInstantiationType(INSTANTIATION_TYPE); + + service.setDescription("description"); + List<String> tgs = new ArrayList<>(); + tgs.add(service.getName()); + service.setTags(tgs); + service.setIcon("defaulticon"); + service.setContactId("aa1234"); + service.setProjectCode("12345"); + service.setEcompGeneratedNaming(true); + + if (afterCreate) { + service.setVersion("0.1"); + service.setUniqueId(service.getName() + ":" + service.getVersion()); + service.setCreatorUserId(user.getUserId()); + service.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); + } + return service; + } + + protected void assertComponentException(ComponentException e, ActionStatus expectedStatus, String... variables) { + ResponseFormat actualResponse = e.getResponseFormat() != null ? + e.getResponseFormat() : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()); + assertParseResponse(actualResponse, expectedStatus, variables); + } + + private void assertParseResponse(ResponseFormat actualResponse, ActionStatus expectedStatus, String... variables) { + ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables); + assertThat(expectedResponse.getStatus()).isEqualTo(actualResponse.getStatus()); + assertThat(expectedResponse.getFormattedMessage()).isEqualTo(actualResponse.getFormattedMessage()); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/CreateServiceFromYamlParameterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/CreateServiceFromYamlParameterTest.java new file mode 100644 index 0000000000..0ff0d133c5 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/CreateServiceFromYamlParameterTest.java @@ -0,0 +1,51 @@ +/* + + * Copyright (c) 2018 AT&T Intellectual Property. + + * + + * Licensed under the Apache License, Version 2.0 (the "License"); + + * you may not use this file except in compliance with the License. + + * You may obtain a copy of the License at + + * + + * http://www.apache.org/licenses/LICENSE-2.0 + + * + + * Unless required by applicable law or agreed to in writing, software + + * distributed under the License is distributed on an "AS IS" BASIS, + + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + + * See the License for the specific language governing permissions and + + * limitations under the License. + + */ + +package org.openecomp.sdc.be.components.impl.utils; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CreateServiceFromYamlParameterTest { + private CreateServiceFromYamlParameter createTestSubject(){ + return new CreateServiceFromYamlParameter(); + } + + @Test + public void testYamlName() { + CreateServiceFromYamlParameter testSubject; + + // default test + testSubject = createTestSubject(); + assertThat(testSubject).isInstanceOf(CreateServiceFromYamlParameter.class); + } + +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/YamlTemplateParsingHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/YamlTemplateParsingHandlerTest.java index 1d7a1c5eed..ac64e06917 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/YamlTemplateParsingHandlerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/YamlTemplateParsingHandlerTest.java @@ -46,6 +46,7 @@ import org.openecomp.sdc.be.model.GroupTypeDefinition; import org.openecomp.sdc.be.model.ParsedToscaYamlInfo; import org.openecomp.sdc.be.model.PolicyDefinition; import org.openecomp.sdc.be.model.PolicyTypeDefinition; +import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.UploadArtifactInfo; import org.openecomp.sdc.be.model.UploadComponentInstanceInfo; import org.openecomp.sdc.be.model.User; @@ -145,8 +146,9 @@ public class YamlTemplateParsingHandlerTest { CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME, MAIN_TEMPLATE_NAME, main_template_content, true); + Resource resource = new Resource(); ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(), - csarInfo.extractNodeTypesInfo(), NODE_NAME); + csarInfo.extractNodeTypesInfo(), NODE_NAME, resource); validateParsedYaml(parsedYaml, NESTED_GROUP_NAME, Lists.newArrayList("heat_file", "description")); @@ -155,8 +157,9 @@ public class YamlTemplateParsingHandlerTest { @Test public void parseResourceInfoFromYAMLTest() { + Resource resource = new Resource(); ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(), - new HashMap<>(), ""); + new HashMap<>(), "", resource); validateParsedYamlWithCapability(parsedYaml); } @@ -228,8 +231,9 @@ public class YamlTemplateParsingHandlerTest { @Test public void parseResourceWithPoliciesDefined() { + Resource resource = new Resource(); ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(), - new HashMap<>(), ""); + new HashMap<>(), "", resource); validateParsedYamlWithPolicies(parsedYaml); } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/AbstractTemplateServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/AbstractTemplateServletTest.java new file mode 100644 index 0000000000..40341be868 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/AbstractTemplateServletTest.java @@ -0,0 +1,219 @@ +/* + + * Copyright (c) 2018 AT&T Intellectual Property. + + * + + * Licensed under the Apache License, Version 2.0 (the "License"); + + * you may not use this file except in compliance with the License. + + * You may obtain a copy of the License at + + * + + * http://www.apache.org/licenses/LICENSE-2.0 + + * + + * Unless required by applicable law or agreed to in writing, software + + * distributed under the License is distributed on an "AS IS" BASIS, + + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + + * See the License for the specific language governing permissions and + + * limitations under the License. + + */ + +package org.openecomp.sdc.be.externalapi.servlet; + + +import fj.data.Either; +import org.apache.http.HttpStatus; +import org.glassfish.hk2.utilities.binding.AbstractBinder; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.TestProperties; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; +import org.openecomp.sdc.be.components.impl.*; +import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.config.SpringConfig; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.ecomp.converters.AssetMetadataConverter; +import org.openecomp.sdc.be.externalapi.servlet.representation.ResourceAssetMetadata; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.impl.ServletUtils; +import org.openecomp.sdc.be.impl.WebAppContextWrapper; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.category.CategoryDefinition; +import org.openecomp.sdc.be.model.category.SubCategoryDefinition; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.ConfigurationSource; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.datastructure.FunctionalInterfaces; +import org.openecomp.sdc.common.impl.ExternalConfiguration; +import org.openecomp.sdc.common.impl.FSConfigurationSource; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.web.context.WebApplicationContext; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +public class AbstractTemplateServletTest extends JerseyTest { + + private static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + private static final HttpSession session = Mockito.mock(HttpSession.class); + private static final ServletContext servletContext = Mockito.mock(ServletContext.class); + private static final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class); + private static final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class); + private static final ServletUtils servletUtils = Mockito.mock(ServletUtils.class); + private static final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class); + private static final ResourceImportManager resourceImportManager = Mockito.mock(ResourceImportManager.class); + private static final ResourceBusinessLogic resourceBusinessLogic = Mockito.mock(ResourceBusinessLogic.class); + private static final ServiceBusinessLogic serviceBusinessLogic = Mockito.mock(ServiceBusinessLogic.class); + private static final ElementBusinessLogic elementBusinessLogic = Mockito.mock(ElementBusinessLogic.class); + private static final Resource resource = Mockito.mock(Resource.class); + private static final CategoryDefinition categoryDefinition = Mockito.mock(CategoryDefinition.class); + private static final SubCategoryDefinition subCategoryDefinition = Mockito.mock(SubCategoryDefinition.class); + private static final AssetMetadataConverter assetMetadataConverter = Mockito.mock(AssetMetadataConverter.class); + private static final ResourceAssetMetadata resourceAssetMetadata = new ResourceAssetMetadata(); + private static final LifecycleBusinessLogic lifecycleBusinessLogic = Mockito.mock(LifecycleBusinessLogic.class); + private static final UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class); + private static final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class); + private static String serviceVertexUuid; + + + @BeforeClass + public static void setup() { + ExternalConfiguration.setAppName("catalog-be"); + when(request.getSession()).thenReturn(session); + when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn("mockXEcompInstanceId"); + when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn("mockAttID"); + when(request.getRequestURL()).thenReturn(new StringBuffer("sdc/v1/catalog/abstract")); + + when(session.getServletContext()).thenReturn(servletContext); + when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper); + when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext); + + when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils); + when(webApplicationContext.getBean(ResourceBusinessLogic.class)).thenReturn(resourceBusinessLogic); + + when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils); + mockResponseFormat(); + + when(resource.getName()).thenReturn("MockVFCMT"); + when(resource.getSystemName()).thenReturn("mockvfcmt"); + Either<Resource, ResponseFormat> eitherRet = Either.left(resource); + when(componentsUtils.convertJsonToObjectUsingObjectMapper(Mockito.any(), Mockito.any(), Mockito.eq(Resource.class), Mockito.any(), Mockito.eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherRet); + + when(webApplicationContext.getBean(ResourceImportManager.class)).thenReturn(resourceImportManager); + when(webApplicationContext.getBean(ElementBusinessLogic.class)).thenReturn(elementBusinessLogic); + when(categoryDefinition.getName()).thenReturn("Template"); + when(subCategoryDefinition.getName()).thenReturn("Monitoring Template"); + when(categoryDefinition.getSubcategories()).thenReturn(Arrays.asList(subCategoryDefinition)); + when(elementBusinessLogic.getAllResourceCategories()).thenReturn(Either.left(Arrays.asList(categoryDefinition))); + when(resourceBusinessLogic.createResource(Mockito.eq(resource), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(resource); + when(webApplicationContext.getBean(AssetMetadataConverter.class)).thenReturn(assetMetadataConverter); + when(request.isUserInRole(anyString())).thenReturn(true); + + Mockito.doReturn(Either.left(resourceAssetMetadata)).when(assetMetadataConverter).convertToSingleAssetMetadata(Mockito.eq(resource), Mockito.anyString(), + Mockito.eq(true)); + + String appConfigDir = "src/test/abstract/config"; + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); + ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + + org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration(); + configuration.setJanusGraphInMemoryGraph(true); + + configurationManager.setConfiguration(configuration); + } + + private static void mockResponseFormat() { + when(componentsUtils.getResponseFormat(Mockito.any(ActionStatus.class), Mockito.any(String[].class))).thenAnswer((Answer<ResponseFormat>) invocation -> { + ResponseFormat ret; + final ActionStatus actionStatus = invocation.getArgument(0); + switch( actionStatus ){ + case CREATED :{ + ret = new ResponseFormat(HttpStatus.SC_CREATED); + break; + } + default :{ + ret = new ResponseFormat(HttpStatus.SC_INTERNAL_SERVER_ERROR); + break; + } + } + return ret; + }); + } + @Test + public void createVfcmtHappyScenario() { + final JSONObject createRequest = buildCreateJsonRequest(); + Response response = target().path("/v1/catalog/abstract").request(MediaType.APPLICATION_JSON).header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").header(Constants.USER_ID_HEADER, "mockAttID") + .post(Entity.json(createRequest.toJSONString()), Response.class); + assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR); + + } + private static final String BASIC_CREATE_REQUEST = "{\r\n" + + " \"name\": \"VFCMT_1\",\r\n" + + " \"description\": \"VFCMT Description\",\r\n" + + " \"resourceType\" : \"VFCMT\",\r\n" + + " \"category\": \"Template\",\r\n" + + " \"subcategory\": \"Monitoring Template\",\r\n" + + " \"vendorName\" : \"DCAE\",\r\n" + + " \"vendorRelease\" : \"1.0\",\r\n" + + " \"tags\": [\r\n" + + " \"VFCMT_1\"\r\n" + + " ],\r\n" + + " \"icon\" : \"defaulticon\",\r\n" + + " \"contactId\": \"cs0008\"\r\n" + + "}"; + private JSONObject buildCreateJsonRequest() { + + JSONParser parser = new JSONParser(); + return (JSONObject) FunctionalInterfaces.swallowException( () -> parser.parse(BASIC_CREATE_REQUEST)); + + } + + @Override + protected Application configure() { + ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); + forceSet(TestProperties.CONTAINER_PORT, "0"); + return new ResourceConfig() + .register(new CrudExternalServlet(userBusinessLogic, componentInstanceBusinessLogic,componentsUtils,servletUtils,resourceImportManager, elementBusinessLogic, assetMetadataConverter, lifecycleBusinessLogic, resourceBusinessLogic, serviceBusinessLogic)) + .register(new AbstractBinder() { + + @Override + protected void configure() { + bind(request).to(HttpServletRequest.class); + } + }) + .property("contextConfig", context); + } + +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractResourceInfoTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractResourceInfoTest.java new file mode 100644 index 0000000000..45fdf570e7 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractResourceInfoTest.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.externalapi.servlet.representation; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AbstractResourceInfoTest { + + private AbstractResourceInfo createTestSubject(){ + return new AbstractResourceInfo(); + } + + @Test + public void testAbstractResourceNameInstance() { + AbstractResourceInfo testSubject; + + // default test + testSubject = createTestSubject(); + assertThat(testSubject).isInstanceOf(AbstractResourceInfo.class); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractTemplateInfoTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractTemplateInfoTest.java new file mode 100644 index 0000000000..883c4021af --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/AbstractTemplateInfoTest.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.externalapi.servlet.representation; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AbstractTemplateInfoTest { + private AbstractTemplateInfo createTestSubject(){ + return new AbstractTemplateInfo(); + } + + @Test + public void testAbstractTemplateNameInstance() { + AbstractTemplateInfo testSubject; + + // default test + testSubject = createTestSubject(); + assertThat(testSubject).isInstanceOf(AbstractTemplateInfo.class); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/CopyServiceInfoTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/CopyServiceInfoTest.java new file mode 100644 index 0000000000..00f78a78b7 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/CopyServiceInfoTest.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.externalapi.servlet.representation; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CopyServiceInfoTest { + + private CopyServiceInfo createTestSubject(){ + return new CopyServiceInfo(); + } + + @Test + public void testCopyServiceNameInstance() { + CopyServiceInfo testSubject; + + // default test + testSubject = createTestSubject(); + assertThat(testSubject).isInstanceOf(CopyServiceInfo.class); + } + +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/ReplaceVNFInfoTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/ReplaceVNFInfoTest.java new file mode 100644 index 0000000000..3dd80c7f2a --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/representation/ReplaceVNFInfoTest.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.externalapi.servlet.representation; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ReplaceVNFInfoTest { + + private ReplaceVNFInfo createTestSubject(){ + return new ReplaceVNFInfo(); + } + + @Test + public void testReplaceVNFInstance() { + ReplaceVNFInfo testSubject; + + // default test + testSubject = createTestSubject(); + assertThat(testSubject).isInstanceOf(ReplaceVNFInfo.class); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ResourceServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ResourceServletTest.java index af28aca26b..6c1c447f9a 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ResourceServletTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ResourceServletTest.java @@ -30,6 +30,7 @@ import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.text.StrSubstitutor; import org.apache.http.HttpStatus; import org.glassfish.hk2.utilities.binding.AbstractBinder; +import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.glassfish.jersey.test.TestProperties; @@ -1033,6 +1034,7 @@ public class ResourceServletTest extends JerseyTest { bind(resourceImportManager).to(ResourceImportManager.class); } }) + .register(MultiPartFeature.class) .property("contextConfig", context); } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ServiceUploadServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ServiceUploadServletTest.java new file mode 100644 index 0000000000..0c1119808d --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ServiceUploadServletTest.java @@ -0,0 +1,25 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.servlets; + +public class ServiceUploadServletTest { + +}
\ No newline at end of file diff --git a/catalog-be/src/test/resources/normativeTypes/service_import_template.yml b/catalog-be/src/test/resources/normativeTypes/service_import_template.yml new file mode 100644 index 0000000000..6d8e80433b --- /dev/null +++ b/catalog-be/src/test/resources/normativeTypes/service_import_template.yml @@ -0,0 +1,334 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 +metadata: + invariantUUID: 6d17f281-683b-4198-a676-0faeecdc9025 + UUID: bfeab6b4-199b-4a2b-b724-de416c5e9811 + name: ser09080002 + description: ser09080002 + type: Service + category: E2E Service + serviceType: '' + serviceRole: '' + instantiationType: A-la-carte + serviceEcompNaming: true + ecompGeneratedNaming: true + namingPolicy: '' + environmentContext: General_Revenue-Bearing + serviceFunction: '' +imports: +- nodes: + file: nodes.yml +- datatypes: + file: data.yml +- capabilities: + file: capabilities.yml +- relationships: + file: relationships.yml +- groups: + file: groups.yml +- policies: + file: policies.yml +- annotations: + file: annotations.yml +- service-ser09080002-interface: + file: service-Ser09080002-template-interface.yml +- resource-ExtCP: + file: resource-Extcp-template.yml +- resource-zxjTestImportServiceAb: + file: resource-Zxjtestimportserviceab-template.yml +- resource-zxjTestImportServiceAb-interface: + file: resource-Zxjtestimportserviceab-template-interface.yml +- resource-zxjTestServiceNotAbatract: + file: resource-Zxjtestservicenotabatract-template.yml +- resource-zxjTestServiceNotAbatract-interface: + file: resource-Zxjtestservicenotabatract-template-interface.yml +- resource-ext ZTE VL: + file: resource-ExtZteVl-template.yml +topology_template: + inputs: + skip_post_instantiation_configuration: + default: true + type: boolean + required: false + controller_actor: + default: SO-REF-DATA + type: string + required: false + cds_model_version: + type: string + required: false + cds_model_name: + type: string + required: false + node_templates: + ext ZTE VL 0: + type: tosca.nodes.nfv.ext.zte.VL + metadata: + invariantUUID: 27ab7610-1a97-4daa-938a-3b48e7afcfd0 + UUID: 9ea63e2c-4b8a-414f-93e3-5703ca5cee0d + customizationUUID: e45e79b0-07ab-46b4-ac26-1e9f155ce53c + version: '1.0' + name: ext ZTE VL + description: Ext ZTE VL + type: VL + category: Generic + subcategory: Network Elements + resourceVendor: ONAP (Tosca) + resourceVendorRelease: 1.0.0.wd03 + resourceVendorModelNumber: '' + zxjTestServiceNotAbatract 0: + type: org.openecomp.resource.vf.Zxjtestservicenotabatract + metadata: + invariantUUID: ce39ce8d-6f97-4e89-8555-ae6789cdcf1c + UUID: 4ac822be-f1ae-4ace-a4b8-bf6b5d977005 + customizationUUID: ee34e1e8-68e2-480f-8ba6-f257bbe90d6a + version: '1.0' + name: zxjTestServiceNotAbatract + description: zxjTestServiceNotAbatract + type: VF + category: Network L4+ + subcategory: Common Network Resources + resourceVendor: zxjImportService + resourceVendorRelease: '1.0' + resourceVendorModelNumber: '' + properties: + nf_naming: + ecomp_generated_naming: true + skip_post_instantiation_configuration: true + multi_stage_design: 'false' + controller_actor: SO-REF-DATA + availability_zone_max_count: 1 + capabilities: + mme_ipu_vdu.scalable: + properties: + max_instances: 1 + min_instances: 1 + mme_ipu_vdu.nfv_compute: + properties: + num_cpus: '2' + flavor_extra_specs: { + } + mem_size: '8192' + ExtCP 0: + type: org.openecomp.resource.cp.extCP + metadata: + invariantUUID: 9b772728-93f5-424f-bb07-f4cae2783614 + UUID: 424ac220-4864-453e-b757-917fe4568ff8 + customizationUUID: 6e65d8a8-4379-4693-87aa-82f9e34b92fd + version: '1.0' + name: ExtCP + description: The AT&T Connection Point base type all other CP derive from + type: CP + category: Generic + subcategory: Network Elements + resourceVendor: ONAP (Tosca) + resourceVendorRelease: 1.0.0.wd03 + resourceVendorModelNumber: '' + properties: + mac_requirements: + mac_count_required: + is_required: false + exCP_naming: + ecomp_generated_naming: true + zxjTestImportServiceAb 0: + type: org.openecomp.resource.vf.Zxjtestimportserviceab + metadata: + invariantUUID: 41474f7f-3195-443d-a0a2-eb6020a56279 + UUID: 92e32e49-55f8-46bf-984d-a98c924037ec + customizationUUID: 98c7a6c7-a867-45fb-8597-dd464f98e4aa + version: '1.0' + name: zxjTestImportServiceAb + description: zxjTestImportServiceAbstract + type: VF + category: Generic + subcategory: Abstract + resourceVendor: zxjImportService + resourceVendorRelease: '1.0' + resourceVendorModelNumber: '' + properties: + nf_naming: + ecomp_generated_naming: true + skip_post_instantiation_configuration: true + multi_stage_design: 'false' + controller_actor: SO-REF-DATA + availability_zone_max_count: 1 + requirements: + - mme_ipu_vdu.dependency: + capability: feature + node: ExtCP 0 + - imagefile.dependency: + capability: feature + node: ext ZTE VL 0 + capabilities: + mme_ipu_vdu.scalable: + properties: + max_instances: 1 + min_instances: 1 + mme_ipu_vdu.nfv_compute: + properties: + num_cpus: '2' + flavor_extra_specs: { + } + mem_size: '8192' + substitution_mappings: + node_type: org.openecomp.service.Ser09080002 + capabilities: + extcp0.feature: + - ExtCP 0 + - feature + zxjtestservicenotabatract0.mme_ipu_vdu.monitoring_parameter: + - zxjTestServiceNotAbatract 0 + - mme_ipu_vdu.monitoring_parameter + zxjtestimportserviceab0.imagefile.guest_os: + - zxjTestImportServiceAb 0 + - imagefile.guest_os + zxjtestimportserviceab0.imagefile.feature: + - zxjTestImportServiceAb 0 + - imagefile.feature + zxjtestservicenotabatract0.imagefile.guest_os: + - zxjTestServiceNotAbatract 0 + - imagefile.guest_os + zxjtestimportserviceab0.ipu_cpd.feature: + - zxjTestImportServiceAb 0 + - ipu_cpd.feature + zxjtestservicenotabatract0.mme_ipu_vdu.virtualbinding: + - zxjTestServiceNotAbatract 0 + - mme_ipu_vdu.virtualbinding + zxjtestimportserviceab0.mme_ipu_vdu.feature: + - zxjTestImportServiceAb 0 + - mme_ipu_vdu.feature + extztevl0.feature: + - ext ZTE VL 0 + - feature + zxjtestimportserviceab0.imagefile.image_fle: + - zxjTestImportServiceAb 0 + - imagefile.image_fle + zxjtestimportserviceab0.mme_ipu_vdu.monitoring_parameter: + - zxjTestImportServiceAb 0 + - mme_ipu_vdu.monitoring_parameter + zxjtestservicenotabatract0.ipu_cpd.feature: + - zxjTestServiceNotAbatract 0 + - ipu_cpd.feature + zxjtestservicenotabatract0.mme_ipu_vdu.nfv_compute: + - zxjTestServiceNotAbatract 0 + - mme_ipu_vdu.nfv_compute + zxjtestservicenotabatract0.mme_ipu_vdu.scalable: + - zxjTestServiceNotAbatract 0 + - mme_ipu_vdu.scalable + extcp0.internal_connectionPoint: + - ExtCP 0 + - internal_connectionPoint + zxjtestimportserviceab0.mme_ipu_vdu.virtualbinding: + - zxjTestImportServiceAb 0 + - mme_ipu_vdu.virtualbinding + zxjtestservicenotabatract0.imagefile.image_fle: + - zxjTestServiceNotAbatract 0 + - imagefile.image_fle + extztevl0.virtual_linkable: + - ext ZTE VL 0 + - virtual_linkable + zxjtestservicenotabatract0.imagefile.feature: + - zxjTestServiceNotAbatract 0 + - imagefile.feature + zxjtestimportserviceab0.localstorage.feature: + - zxjTestImportServiceAb 0 + - localstorage.feature + zxjtestservicenotabatract0.localstorage.local_attachment: + - zxjTestServiceNotAbatract 0 + - localstorage.local_attachment + zxjtestimportserviceab0.mme_ipu_vdu.scalable: + - zxjTestImportServiceAb 0 + - mme_ipu_vdu.scalable + zxjtestservicenotabatract0.localstorage.feature: + - zxjTestServiceNotAbatract 0 + - localstorage.feature + zxjtestimportserviceab0.mme_ipu_vdu.nfv_compute: + - zxjTestImportServiceAb 0 + - mme_ipu_vdu.nfv_compute + zxjtestimportserviceab0.localstorage.local_attachment: + - zxjTestImportServiceAb 0 + - localstorage.local_attachment + zxjtestservicenotabatract0.mme_ipu_vdu.feature: + - zxjTestServiceNotAbatract 0 + - mme_ipu_vdu.feature + zxjtestimportserviceab0.ipu_cpd.forwarder: + - zxjTestImportServiceAb 0 + - ipu_cpd.forwarder + zxjtestservicenotabatract0.ipu_cpd.forwarder: + - zxjTestServiceNotAbatract 0 + - ipu_cpd.forwarder + requirements: + zxjtestservicenotabatract0.imagefile.dependency: + - zxjTestServiceNotAbatract 0 + - imagefile.dependency + zxjtestservicenotabatract0.mme_ipu_vdu.local_storage: + - zxjTestServiceNotAbatract 0 + - mme_ipu_vdu.local_storage + zxjtestservicenotabatract0.ipu_cpd.dependency: + - zxjTestServiceNotAbatract 0 + - ipu_cpd.dependency + zxjtestservicenotabatract0.mme_ipu_vdu.volume_storage: + - zxjTestServiceNotAbatract 0 + - mme_ipu_vdu.volume_storage + zxjtestservicenotabatract0.ipu_cpd.virtualbinding: + - zxjTestServiceNotAbatract 0 + - ipu_cpd.virtualbinding + zxjtestservicenotabatract0.mme_ipu_vdu.dependency: + - zxjTestServiceNotAbatract 0 + - mme_ipu_vdu.dependency + zxjtestservicenotabatract0.localstorage.dependency: + - zxjTestServiceNotAbatract 0 + - localstorage.dependency + zxjtestimportserviceab0.imagefile.dependency: + - zxjTestImportServiceAb 0 + - imagefile.dependency + zxjtestimportserviceab0.mme_ipu_vdu.volume_storage: + - zxjTestImportServiceAb 0 + - mme_ipu_vdu.volume_storage + zxjtestimportserviceab0.ipu_cpd.virtualbinding: + - zxjTestImportServiceAb 0 + - ipu_cpd.virtualbinding + extcp0.virtualLink: + - ExtCP 0 + - virtualLink + extcp0.virtualBinding: + - ExtCP 0 + - virtualBinding + zxjtestimportserviceab0.mme_ipu_vdu.guest_os: + - zxjTestImportServiceAb 0 + - mme_ipu_vdu.guest_os + extcp0.dependency: + - ExtCP 0 + - dependency + zxjtestimportserviceab0.localstorage.dependency: + - zxjTestImportServiceAb 0 + - localstorage.dependency + zxjtestservicenotabatract0.ipu_cpd.virtualLink: + - zxjTestServiceNotAbatract 0 + - ipu_cpd.virtualLink + extztevl0.dependency: + - ext ZTE VL 0 + - dependency + zxjtestimportserviceab0.ipu_cpd.dependency: + - zxjTestImportServiceAb 0 + - ipu_cpd.dependency + zxjtestimportserviceab0.mme_ipu_vdu.dependency: + - zxjTestImportServiceAb 0 + - mme_ipu_vdu.dependency + zxjtestimportserviceab0.mme_ipu_vdu.local_storage: + - zxjTestImportServiceAb 0 + - mme_ipu_vdu.local_storage + zxjtestimportserviceab0.ipu_cpd.virtualLink: + - zxjTestImportServiceAb 0 + - ipu_cpd.virtualLink + extcp0.external_virtualLink: + - ExtCP 0 + - external_virtualLink + zxjtestservicenotabatract0.mme_ipu_vdu.guest_os: + - zxjTestServiceNotAbatract 0 + - mme_ipu_vdu.guest_os + zxjtestimportserviceab0.ipu_cpd.forwarder: + - zxjTestImportServiceAb 0 + - ipu_cpd.forwarder + zxjtestservicenotabatract0.ipu_cpd.forwarder: + - zxjTestServiceNotAbatract 0 + - ipu_cpd.forwarder |