X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-be%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fcomponents%2Fimpl%2FResourceBusinessLogic.java;h=7e7348d89e27e0df60c1d366adca27887884b530;hb=216ea4998de9ae4ee603d7f8f5f6725b218ca710;hp=0ae546479a72c9c73843d733df4b68ff9d83d046;hpb=371880722fa899f59c82b1a98b0e3d65ff250e41;p=sdc.git diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java index 0ae546479a..7e7348d89e 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 @@ -7,9 +7,9 @@ * 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. @@ -17,12 +17,20 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.be.components.impl; -import static org.openecomp.sdc.be.tosca.CsarUtils.ARTIFACTS_PATH; +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.apache.commons.collections.MapUtils.isEmpty; +import static org.apache.commons.collections.MapUtils.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 static org.openecomp.sdc.common.api.Constants.DEFAULT_GROUP_VF_MODULE; +import com.google.common.annotations.VisibleForTesting; +import fj.data.Either; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -31,78 +39,106 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Function; -import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; - -import javax.servlet.ServletContext; - import org.apache.commons.codec.binary.Base64; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.collections4.ListUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.openecomp.sdc.be.catalog.enums.ChangeTypeEnum; +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.csar.OnboardedCsarInfo; import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum; -import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationInfo; 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.ImportUtils.ToscaTagNamesEnum; +import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo; +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.components.impl.utils.CINodeFilterUtils; import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum; +import org.openecomp.sdc.be.components.merge.TopologyComparator; +import org.openecomp.sdc.be.components.merge.property.PropertyDataValueMergeBusinessLogic; +import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic; +import org.openecomp.sdc.be.components.property.PropertyConstraintsUtils; +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.BeEcompErrorManager.ErrorSeverity; -import org.openecomp.sdc.be.config.Configuration.VfModuleProperty; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum; import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils; import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter; +import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition; +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.ModelTypeEnum; 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.impl.WebAppContextWrapper; -import org.openecomp.sdc.be.info.ArtifactTemplateInfo; -import org.openecomp.sdc.be.info.MergedArtifactInfo; +import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts; import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.AttributeDefinition; 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.CsarInfo; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.GroupDefinition; import org.openecomp.sdc.be.model.GroupProperty; -import org.openecomp.sdc.be.model.GroupTypeDefinition; -import org.openecomp.sdc.be.model.HeatParameterDefinition; 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.Model; 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.PolicyDefinition; +import org.openecomp.sdc.be.model.PolicyTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.RelationshipImpl; -import org.openecomp.sdc.be.model.RequirementAndRelationshipPair; +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.UploadArtifactInfo; 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; @@ -110,7117 +146,5235 @@ import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.category.SubCategoryDefinition; -import org.openecomp.sdc.be.model.heat.HeatParameterType; -import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; +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.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.IPropertyOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.CsarOperation; -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.ModelOperation; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; -import org.openecomp.sdc.be.servlets.RepresentationUtils; +import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo; import org.openecomp.sdc.be.tosca.CsarUtils; import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo; -import org.openecomp.sdc.be.tosca.ToscaUtils; import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer; -import org.openecomp.sdc.be.user.IUserBusinessLogic; -import org.openecomp.sdc.be.user.Role; import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.be.utils.CommonBeUtils; +import org.openecomp.sdc.be.utils.TypeUtils; +import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; 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.AuditingFieldsKeysEnum; -import org.openecomp.sdc.common.datastructure.FunctionalInterfaces; import org.openecomp.sdc.common.datastructure.Wrapper; import org.openecomp.sdc.common.kpi.api.ASDCKpiApi; +import org.openecomp.sdc.common.log.elements.LoggerSupportability; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; +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.util.GeneralUtility; import org.openecomp.sdc.common.util.ValidationUtils; import org.openecomp.sdc.exception.ResponseFormat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.context.WebApplicationContext; +import org.springframework.context.annotation.Lazy; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.parser.ParserException; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - -import fj.data.Either; @org.springframework.stereotype.Component("resourceBusinessLogic") public class ResourceBusinessLogic extends ComponentBusinessLogic { - private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes"; - public static final String INITIAL_VERSION = "0.1"; - - private Pattern STR_REPLACE_PATTERN = Pattern.compile("^[ ]*\\{[ ]*" + "str_replace" + "="); - private Pattern TOKEN_PATTERN = Pattern.compile("[ ]*\\{[ ]*" + "token" + "="); - private Pattern GET_PROPERTY_PATTERN = Pattern.compile("[ ]*\\{[ ]*" + "get_property" + "="); - private Pattern CONCAT_PATTERN = Pattern.compile("[ ]*\\{[ ]*" + "concat" + "="); - - private static Logger log = LoggerFactory.getLogger(ResourceBusinessLogic.class.getName()); - - private static Pattern pattern = Pattern.compile("\\..(.*?)\\.."); - - /** - * Default constructor - */ - public ResourceBusinessLogic() { - log.debug("ResourceBusinessLogic started"); - } - - @Autowired - private ICapabilityTypeOperation capabilityTypeOperation = null; - - @Autowired - private IInterfaceLifecycleOperation interfaceTypeOperation = null; - - @Autowired - private LifecycleBusinessLogic lifecycleBusinessLogic; - - @Autowired - private IPropertyOperation propertyOperation; - - @Autowired - private CsarOperation csarOperation; - - @Autowired - private VFComponentInstanceBusinessLogic vfComponentInstanceBusinessLogic; - - @Autowired - private ResourceImportManager resourceImportManager; - - @Autowired - private GroupBusinessLogic groupBusinessLogic; - - @Autowired - private InputsBusinessLogic inputsBusinessLogic; - - @Autowired - private CompositionBusinessLogic compositionBusinessLogic; - - @Autowired - private ICacheMangerOperation cacheManagerOperation; - - @Autowired - private ApplicationDataTypeCache dataTypeCache; - - private Gson gson = new Gson(); - - public CsarOperation getCsarOperation() { - return csarOperation; - } - - public void setCsarOperation(CsarOperation csarOperation) { - this.csarOperation = csarOperation; - } - - public LifecycleBusinessLogic getLifecycleBusinessLogic() { - return lifecycleBusinessLogic; - } - - public void setLifecycleManager(LifecycleBusinessLogic lifecycleBusinessLogic) { - this.lifecycleBusinessLogic = lifecycleBusinessLogic; - } - - public IElementOperation getElementDao() { - return elementDao; - } - - public void setElementDao(IElementOperation elementDao) { - this.elementDao = elementDao; - } - - public IUserBusinessLogic getUserAdmin() { - return this.userAdmin; - } - - public void setUserAdmin(UserBusinessLogic userAdmin) { - this.userAdmin = userAdmin; - } - - public ComponentsUtils getComponentsUtils() { - return this.componentsUtils; - } - - public void setComponentsUtils(ComponentsUtils componentsUtils) { - this.componentsUtils = componentsUtils; - } - - public ArtifactsBusinessLogic getArtifactsManager() { - return artifactsBusinessLogic; - } - - public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) { - this.artifactsBusinessLogic = artifactsManager; - } - - public void setPropertyOperation(IPropertyOperation propertyOperation) { - this.propertyOperation = propertyOperation; - } - - public ApplicationDataTypeCache getApplicationDataTypeCache() { - return applicationDataTypeCache; - } - - public void setApplicationDataTypeCache(ApplicationDataTypeCache applicationDataTypeCache) { - this.applicationDataTypeCache = applicationDataTypeCache; - } - - /** - * the method returns a list of all the resources that are certified, the returned resources are only abstract or only none abstract according to the given param - * - * @param getAbstract - * @param userId TODO - * @return - */ - public Either, ResponseFormat> getAllCertifiedResources(boolean getAbstract, HighestFilterEnum highestFilter, String userId) { - Either resp = validateUserExists(userId, "get All Certified Resources", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } - Boolean isHighest = null; - switch (highestFilter) { - case ALL: - break; - case HIGHEST_ONLY: - isHighest = true; - break; - case NON_HIGHEST_ONLY: - isHighest = false; - break; - default: - break; - } - Either, StorageOperationStatus> getResponse = toscaOperationFacade.getAllCertifiedResources(getAbstract, isHighest); - - if (getResponse.isRight()) { - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getResponse.right().value()))); - } - - return Either.left(getResponse.left().value()); - } - - public Either, ResponseFormat> validateResourceNameExists(String resourceName, ResourceTypeEnum resourceTypeEnum, String userId) { - - Either resp = validateUserExists(userId, "validate Resource Name Exists", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } - - Either dataModelResponse = toscaOperationFacade.validateComponentNameUniqueness(resourceName, resourceTypeEnum, ComponentTypeEnum.RESOURCE); - // DE242223 - titanDao.commit(); - - if (dataModelResponse.isLeft()) { - Map result = new HashMap<>(); - result.put("isValid", dataModelResponse.left().value()); - log.debug("validation was successfully performed."); - return Either.left(result); - } - - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value())); - - return Either.right(responseFormat); - } - - public Either createResource(Resource resource, AuditingActionEnum auditingAction, User user, Map csarUIPayload, String payloadName) { - Either createResourceResponse = validateResourceBeforeCreate(resource, user, false); - if (createResourceResponse.isRight()) { - return createResourceResponse; - } - - // Creating resource either by DAO or from CSAR - String csarUUID = null; - if (payloadName == null) { - csarUUID = resource.getCsarUUID(); - } else { - csarUUID = payloadName; - } - if (csarUUID != null && !csarUUID.isEmpty()) { - // check if VF with the same Csar UUID or with he same name already - // exists - Either validateCsarUuidUniquenessRes = toscaOperationFacade.validateCsarUuidUniqueness(csarUUID); - if (validateCsarUuidUniquenessRes.isRight()) { - log.debug("Failed to validate uniqueness of CsarUUID {} for resource", csarUUID, resource.getSystemName()); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(validateCsarUuidUniquenessRes.right().value()))); - } - - Integer existingResourceRes = validateCsarUuidUniquenessRes.left().value(); - if (existingResourceRes.intValue() > 0) { - log.debug("Failed to create resource {}, csarUUID {} already exist for a different VF ", resource.getSystemName(), csarUUID); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, csarUUID); - componentsUtils.auditResource(errorResponse, user, resource, "", "", auditingAction, null); - return Either.right(errorResponse); - } - - log.debug("CsarUUID is {} - going to create resource from CSAR", csarUUID); - createResourceResponse = createResourceFromCsar(resource, user, Either.left(csarUIPayload), csarUUID); - return createResourceResponse; - } - - return createResourceByDao(resource, user, auditingAction, false, false, null); - } - - public Either validateAndUpdateResourceFromCsar(Resource resource, User user, Map csarUIPayload, String payloadName, String resourceUniqueId) { - Either updateResourceResponse = null; - Either validateResourceResponse = null; - Wrapper responseWrapper = new Wrapper(); - String csarUUID = null; - String csarVersion = null; - if (payloadName == null) { - csarUUID = resource.getCsarUUID(); - csarVersion = resource.getCsarVersion(); - } else { - csarUUID = payloadName; - } - if (csarUUID != null && !csarUUID.isEmpty()) { - Resource oldResource = getResourceByUniqueId(responseWrapper, resourceUniqueId); - if (responseWrapper.isEmpty()) { - validateCsarUuidMatching(responseWrapper, oldResource, resource, csarUUID, resourceUniqueId, user); - } - if (responseWrapper.isEmpty()) { - validateCsarIsNotAlreadyUsed(responseWrapper, oldResource, resource, csarUUID, user); - } - if (responseWrapper.isEmpty()) { - if (oldResource != null && ValidationUtils.hasBeenCertified(oldResource.getVersion())) { - overrideImmutableMetadata(oldResource, resource); - } - validateResourceResponse = validateResourceBeforeCreate(resource, user, false); - if (validateResourceResponse.isRight()) { - responseWrapper.setInnerElement(validateResourceResponse.right().value()); - } - } - if (responseWrapper.isEmpty()) { - String oldCsarVersion = oldResource.getCsarVersion(); - log.debug("CsarUUID is {} - going to update resource with UniqueId {} from CSAR", csarUUID, resourceUniqueId); - // (on boarding flow): If the update includes same csarUUID and - // same csarVersion as already in the VF - no need to import the - // csar (do only metadata changes if there are). - if (csarVersion != null && oldCsarVersion != null && oldCsarVersion.equals(csarVersion)) { - updateResourceResponse = updateResourceMetadata(resourceUniqueId, resource, oldResource, user, false); - } else { - updateResourceResponse = updateResourceFromCsar(oldResource, resource, user, AuditingActionEnum.UPDATE_RESOURCE_METADATA, false, Either.left(csarUIPayload), csarUUID); - } - } - } else { - log.debug("Failed to update resource {}, csarUUID or payload name is missing", resource.getSystemName()); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CSAR_UUID, resource.getName()); - componentsUtils.auditResource(errorResponse, user, resource, "", "", AuditingActionEnum.CREATE_RESOURCE, null); - responseWrapper.setInnerElement(errorResponse); - } - if (responseWrapper.isEmpty()) { - return updateResourceResponse; - } - return Either.right(responseWrapper.getInnerElement()); - } - - private void validateCsarIsNotAlreadyUsed(Wrapper responseWrapper, Resource oldResource, Resource resource, String csarUUID, User user) { - // (on boarding flow): If the update includes a csarUUID: verify this - // csarUUID is not in use by another VF, If it is - use same error as - // above: - // "Error: The VSP with UUID %1 was already imported for VF %2. Please - // select another or update the existing VF." %1 - csarUUID, %2 - VF - // name - Either resourceLinkedToCsarRes = toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUUID, resource.getSystemName()); - if (resourceLinkedToCsarRes.isRight()) { - if (!StorageOperationStatus.NOT_FOUND.equals(resourceLinkedToCsarRes.right().value())) { - log.debug("Failed to find previous resource by CSAR {} and system name {}", csarUUID, resource.getSystemName()); - responseWrapper.setInnerElement(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceLinkedToCsarRes.right().value()))); - } - } else if (!resourceLinkedToCsarRes.left().value().getUniqueId().equals(oldResource.getUniqueId()) && !resourceLinkedToCsarRes.left().value().getName().equals(oldResource.getName())) { - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, csarUUID, resourceLinkedToCsarRes.left().value().getName()); - componentsUtils.auditResource(errorResponse, user, resource, "", "", AuditingActionEnum.UPDATE_RESOURCE_METADATA, null); - responseWrapper.setInnerElement(errorResponse); - } - } - - private void validateCsarUuidMatching(Wrapper responseWrapper, Resource resource, Resource oldResource, String csarUUID, String resourceUniqueId, User user) { - // (on boarding flow): If the update includes csarUUID which is - // different from the csarUUID of the VF - fail with - // error: "Error: Resource %1 cannot be updated using since it is linked - // to a different VSP" %1 - VF name - String oldCsarUUID = oldResource.getCsarUUID(); - if (oldCsarUUID != null && !oldCsarUUID.isEmpty() && !csarUUID.equals(oldCsarUUID)) { - log.debug("Failed to update resource with UniqueId {} using Csar {}, since the resource is linked to a different VSP {}", resourceUniqueId, csarUUID, oldCsarUUID); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_LINKED_TO_DIFFERENT_VSP, resource.getName(), csarUUID, oldCsarUUID); - componentsUtils.auditResource(errorResponse, user, resource, "", "", AuditingActionEnum.UPDATE_RESOURCE_METADATA, null); - responseWrapper.setInnerElement(errorResponse); - } - } - - private Resource getResourceByUniqueId(Wrapper responseWrapper, String resourceUniqueId) { - Either oldResourceRes = toscaOperationFacade.getToscaElement(resourceUniqueId); - if (oldResourceRes.isRight()) { - log.debug("Failed to find previous resource by UniqueId {}, status: {}", resourceUniqueId, oldResourceRes.right().value()); - responseWrapper.setInnerElement(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(oldResourceRes.right().value()))); - return null; - } - return oldResourceRes.left().value(); - } - - private void overrideImmutableMetadata(Resource oldRresource, Resource resource) { - resource.setName(oldRresource.getName()); - resource.setIcon(oldRresource.getIcon()); - resource.setTags(oldRresource.getTags()); - resource.setVendorName(oldRresource.getVendorName()); - resource.setCategories(oldRresource.getCategories()); - resource.setDerivedFrom(oldRresource.getDerivedFrom()); - } - - private Either updateResourceFromCsar(Resource oldRresource, Resource newRresource, User user, AuditingActionEnum updateResource, boolean inTransaction, Either, StorageOperationStatus> csarUIPayload, - String csarUUID) { - - // check state - if (LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.equals(oldRresource.getLifecycleState())) { - if (!oldRresource.getLastUpdaterUserId().equals(user.getUserId())) { - log.debug("Current user is not last updater, last updater userId: {}, current user userId: {}", oldRresource.getLastUpdaterUserId(), user.getUserId()); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)); - } - } - String lockedResourceId = oldRresource.getUniqueId(); - List createdArtifacts = new ArrayList<>(); - - Either, StorageOperationStatus> csar = null; - if (csarUIPayload != null && csarUIPayload.left() != null && csarUIPayload.left().value() != null) { - csar = csarUIPayload; - } else { - csar = csarOperation.getCsar(csarUUID, user); - } - if (csar.isRight()) { - log.debug("Failed to get csar for casrUUID{} ", csarUUID); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(csar.right().value()))); - } - - Either, ResponseFormat> toscaYamlCsarStatus = validateAndParseCsar(newRresource, user, csarUUID, csar); - if (toscaYamlCsarStatus.isRight()) { - return Either.right(toscaYamlCsarStatus.right().value()); - } - Either checksum = CsarValidationUtils.getToscaYamlChecksum(csar.left().value(), csarUUID, componentsUtils); - if (checksum.isRight()) { - log.debug("Failed to calculate checksum for casrUUID{} error {} ", csarUUID, checksum.right().value()); - return Either.right(checksum.right().value()); - } - boolean isUpdateYaml = true; - if (checksum.left().value().equals(oldRresource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum())) { - log.debug("The checksums are equals for csarUUID {}, existing checsum is {}, new one is {} ", csarUUID, oldRresource.getComponentMetadataDefinition().getMetadataDataDefinition().getImportedToscaChecksum(), checksum.left().value()); - if (oldRresource.getLifecycleState().equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) - isUpdateYaml = false; - } else { - oldRresource.getComponentMetadataDefinition().getMetadataDataDefinition().setImportedToscaChecksum(checksum.left().value()); - } - - Either lockResult = lockComponent(lockedResourceId, oldRresource, "update Resource From Csar"); - if (lockResult.isRight()) { - return Either.right(lockResult.right().value()); - } - - String yamlFileName = toscaYamlCsarStatus.left().value().getKey(); - String yamlFileContents = toscaYamlCsarStatus.left().value().getValue(); - CsarInfo csarInfo = new CsarInfo(newRresource.getName(), user, csarUUID, csar.left().value(), yamlFileContents, true); - Map nodeTypesInfo = extractNodeTypesInfo(csarInfo); - Either result = null; - - Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo, oldRresource); - if (findNodeTypesArtifactsToHandleRes.isRight()) { - log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID()); - result = Either.right(findNodeTypesArtifactsToHandleRes.right().value()); - return result; - } - Map>> nodeTypesArtifactsToHandle = findNodeTypesArtifactsToHandleRes.left().value(); - try{ - result = updateResourceFromYaml(oldRresource, newRresource, updateResource, createdArtifacts, isUpdateYaml, yamlFileName, yamlFileContents, csarInfo, nodeTypesInfo, nodeTypesArtifactsToHandle, null); - - } finally { - if (result == null || result.isRight()) { - log.warn("operation failed. do rollback"); - titanDao.rollback(); - if (!createdArtifacts.isEmpty()) { - StorageOperationStatus deleteFromEsRes = artifactsBusinessLogic.deleteAllComponentArtifactsIfNotOnGraph(createdArtifacts); - if (!deleteFromEsRes.equals(StorageOperationStatus.OK)) { - ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(deleteFromEsRes); - result = Either.right(componentsUtils.getResponseFormat(actionStatus, oldRresource.getName())); - } - log.debug("component and all its artifacts were deleted, id = {}", oldRresource.getName()); - } - } else { - log.debug("operation success. do commit"); - titanDao.commit(); - } - log.debug("unlock resource {}", lockedResourceId); - graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource); - } - return result; - - } - - private Either updateResourceFromYaml(Resource oldRresource, Resource newRresource, AuditingActionEnum updateResource, List createdArtifacts, - boolean isUpdateYaml, String yamlFileName, String yamlFileContent, CsarInfo csarInfo, Map nodeTypesInfo, - Map>> nodeTypesArtifactsToHandle, String nodeName) { - Either result; - Either, ResponseFormat> parseNodeTypeInfoYamlEither; - boolean inTransaction = true; - boolean shouldLock = false; - - Either, ResponseFormat> prepareForUpdate; - Resource preparedResource; - Either uploadComponentInstanceInfoMap = parseResourceInfoFromYaml(yamlFileName, newRresource, yamlFileContent, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo, nodeName); - if (uploadComponentInstanceInfoMap.isRight()) { - ResponseFormat responseFormat = uploadComponentInstanceInfoMap.right().value(); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), newRresource, "", "", updateResource, null); - result = Either.right(responseFormat); - return result; - } - - Map instances = uploadComponentInstanceInfoMap.left().value().getInstances(); - - if (isUpdateYaml || !nodeTypesArtifactsToHandle.isEmpty()) { - - prepareForUpdate = updateExistingResourceByImport(newRresource, oldRresource, csarInfo.getModifier(), inTransaction, shouldLock); - if (prepareForUpdate.isRight()) { - log.debug("Failed to prepare resource for update : {}", prepareForUpdate.right().value()); - result = Either.right(prepareForUpdate.right().value()); - return result; - } - preparedResource = prepareForUpdate.left().value().left; - - log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent); - - parseNodeTypeInfoYamlEither = this.handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo, csarInfo, nodeName); - if (parseNodeTypeInfoYamlEither.isRight()) { - ResponseFormat responseFormat = parseNodeTypeInfoYamlEither.right().value(); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), preparedResource, "", "", updateResource, null); - result = Either.right(responseFormat); - return result; - } - - Map inputs = uploadComponentInstanceInfoMap.left().value().getInputs(); - Either createInputsOnResource = createInputsOnResource(preparedResource, csarInfo.getModifier(), inputs, inTransaction); - if (createInputsOnResource.isRight()) { - log.debug("failed to create resource inputs status is {}", createInputsOnResource.right().value()); - ResponseFormat responseFormat = createInputsOnResource.right().value(); - componentsUtils.auditResource(createInputsOnResource.right().value(), csarInfo.getModifier(), preparedResource, "", "", updateResource, null); - result = Either.right(responseFormat); - return result; - } - preparedResource = createInputsOnResource.left().value(); - - Either createResourcesInstancesEither = createResourceInstances(csarInfo.getModifier(), yamlFileName, preparedResource, instances, inTransaction, shouldLock, csarInfo.getCreatedNodes()); - if (createResourcesInstancesEither.isRight()) { - log.debug("failed to create resource instances status is {}", createResourcesInstancesEither.right().value()); - ResponseFormat responseFormat = createResourcesInstancesEither.right().value(); - componentsUtils.auditResource(createResourcesInstancesEither.right().value(), csarInfo.getModifier(), preparedResource, "", "", updateResource, null); - result = Either.right(responseFormat); - return result; - } - preparedResource = createResourcesInstancesEither.left().value(); - - createResourcesInstancesEither = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, instances); - if (createResourcesInstancesEither.isRight()) { - log.debug("failed to create relation between resource instances status is {}", createResourcesInstancesEither.right().value()); - result = Either.right(createResourcesInstancesEither.right().value()); - return result; - } - - preparedResource = createResourcesInstancesEither.left().value(); - - Either, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNames(uploadComponentInstanceInfoMap.left().value().getGroups(), preparedResource.getSystemName()); - if (validateUpdateVfGroupNamesRes.isRight()) { - - return Either.right(validateUpdateVfGroupNamesRes.right().value()); - } - // add groups to resource - Map groups; - - if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) { - groups = validateUpdateVfGroupNamesRes.left().value(); - } else { - groups = uploadComponentInstanceInfoMap.left().value().getGroups(); - } - Either updatedGroupsOnResource = updateGroupsOnResource(preparedResource, csarInfo.getModifier(), groups); - if (updatedGroupsOnResource.isRight()) { - - return updatedGroupsOnResource; - } - preparedResource = updatedGroupsOnResource.left().value(); - - } else { - Either dataModelResponse = updateResourceMetadata(oldRresource.getUniqueId(), newRresource, csarInfo.getModifier(), oldRresource, shouldLock, inTransaction); - if (dataModelResponse.isRight()) { - log.debug("failed to update resource metadata {}", dataModelResponse.right().value()); - result = Either.right(dataModelResponse.right().value()); - return result; - } - preparedResource = dataModelResponse.left().value(); - } - - if(preparedResource.getResourceType() == ResourceTypeEnum.CVFC){ - if(nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()){ - Either, ResponseFormat> handleNodeTypeArtifactsRes = - handleNodeTypeArtifacts(preparedResource, nodeTypesArtifactsToHandle.get(nodeName), createdArtifacts, csarInfo.getModifier(), inTransaction, true); - if(handleNodeTypeArtifactsRes.isRight()){ - return Either.right(handleNodeTypeArtifactsRes.right().value()); - } - } - } else { - Either createdCsarArtifactsEither = handleVfCsarArtifacts(preparedResource, csarInfo, createdArtifacts, - artifactsBusinessLogic.new ArtifactOperationInfo(false, false, ArtifactOperationEnum.Update), shouldLock, inTransaction); - log.trace("************* Finished to add artifacts from yaml {}", yamlFileName); - if (createdCsarArtifactsEither.isRight()) { - return createdCsarArtifactsEither; - } - preparedResource = createdCsarArtifactsEither.left().value(); - } - - Either, ResponseFormat> eitherSetPosition = compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId()); - result = eitherSetPosition.isRight() ? Either.right(eitherSetPosition.right().value()) : Either.left(preparedResource); - - return result; - - } - private Either>>, ResponseFormat> findNodeTypesArtifactsToHandle(Map nodeTypesInfo, CsarInfo csarInfo, Resource oldResource) { - - Map> extractedVfcsArtifacts = CsarUtils.extractVfcsArtifactsFromCsar(csarInfo.getCsar()); - Map>> nodeTypesArtifactsToHandle = new HashMap<>(); - Either>>, ResponseFormat> nodeTypesArtifactsToHandleRes; - - try { - nodeTypesArtifactsToHandleRes = Either.left(nodeTypesArtifactsToHandle); - Map extractedVfcToscaNames = extractVfcToscaNames(nodeTypesInfo, oldResource.getName(), csarInfo); - Either>, ResponseFormat> curNodeTypeArtifactsToHandleRes; - EnumMap> curNodeTypeArtifactsToHandle = null; - log.debug("Going to fetch node types for resource with name {} during import csar with UUID {}. ", oldResource.getName(), csarInfo.getCsarUUID()); - - for (Entry currVfcToscaNameEntry : extractedVfcToscaNames.entrySet()) { - String currVfcToscaName = currVfcToscaNameEntry.getValue(); - String currNamespace = currVfcToscaNameEntry.getKey(); - log.debug("Going to fetch node type with tosca name {}. ", currVfcToscaName); - - Either curVfcRes = toscaOperationFacade.getLatestByToscaResourceName(currVfcToscaName); - Resource curNodeType = null; - if (curVfcRes.isRight() && curVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) { - log.debug("Error occured during fetching node type with tosca name {}, error: {}", currVfcToscaName, curVfcRes.right().value()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(curVfcRes.right().value()), csarInfo.getCsarUUID()); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), oldResource, "", "", AuditingActionEnum.CREATE_RESOURCE, null); - nodeTypesArtifactsToHandleRes = Either.right(responseFormat); - break; - } else if (curVfcRes.isLeft()) { - curNodeType = curVfcRes.left().value(); - } - if (!MapUtils.isEmpty(extractedVfcsArtifacts)) { - List currArtifacts = new ArrayList<>(); - if (extractedVfcsArtifacts.containsKey(currNamespace)) { - handleAndAddExtractedVfcsArtifacts(currArtifacts, extractedVfcsArtifacts.get(currNamespace)); - } - curNodeTypeArtifactsToHandleRes = findNodeTypeArtifactsToHandle(curNodeType, currArtifacts); - if (curNodeTypeArtifactsToHandleRes.isRight()) { - nodeTypesArtifactsToHandleRes = Either.right(curNodeTypeArtifactsToHandleRes.right().value()); - break; - } - curNodeTypeArtifactsToHandle = curNodeTypeArtifactsToHandleRes.left().value(); - - } else if (curNodeType != null) { - // delete all artifacts if have not received artifacts from csar - curNodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class); - List artifactsToDelete = new ArrayList<>(); - // delete all informational artifacts - artifactsToDelete.addAll(curNodeType.getArtifacts().values().stream().filter(a -> a.getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL).collect(Collectors.toList())); - // delete all deployment artifacts - artifactsToDelete.addAll(curNodeType.getDeploymentArtifacts().values()); - if (!artifactsToDelete.isEmpty()) { - curNodeTypeArtifactsToHandle.put(ArtifactOperationEnum.Delete, artifactsToDelete); - } - } - if (MapUtils.isNotEmpty(curNodeTypeArtifactsToHandle)) { - nodeTypesArtifactsToHandle.put(currVfcToscaNameEntry.getKey(), curNodeTypeArtifactsToHandle); - } - } - } 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 Either>, ResponseFormat> findNodeTypeArtifactsToHandle(Resource curNodeType, List extractedArtifacts) { - - Either>, ResponseFormat> nodeTypeArtifactsToHandleRes = null; - EnumMap> nodeTypeArtifactsToHandle = null; - Wrapper responseWrapper = new Wrapper<>(); - try { - List artifactsToUpload = new ArrayList<>(extractedArtifacts); - List artifactsToUpdate = new ArrayList<>(); - List artifactsToDelete = new ArrayList<>(); - if (curNodeType != null) { - Map existingArtifacts = new HashMap<>(); - if (curNodeType.getDeploymentArtifacts() != null) { - existingArtifacts.putAll(curNodeType.getDeploymentArtifacts()); - } - if (curNodeType.getArtifacts() != null) { - existingArtifacts.putAll(curNodeType.getArtifacts().entrySet().stream().filter(e -> e.getValue().getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()))); - } - for (ArtifactDefinition currNewArtifact : extractedArtifacts) { - ArtifactDefinition foundArtifact; - - if (!existingArtifacts.isEmpty()) { - foundArtifact = existingArtifacts.values().stream().filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName())).findFirst().orElse(null); - if (foundArtifact != null) { - if (foundArtifact.getArtifactType().equals(currNewArtifact.getArtifactType())) { - 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); - } - existingArtifacts.remove(foundArtifact.getArtifactLabel()); - artifactsToUpload.remove(currNewArtifact); - } else { - log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName()); - ResponseFormat responseFormat = ResponseFormatManager.getInstance().getResponseFormat(ActionStatus.ARTIFACT_ALRADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(), - foundArtifact.getArtifactType()); - responseWrapper.setInnerElement(responseFormat); - break; - } - } - } - } - if (responseWrapper.isEmpty()) { - artifactsToDelete.addAll(existingArtifacts.values()); - } - } - if (responseWrapper.isEmpty()) { - if (!artifactsToUpload.isEmpty() || !artifactsToUpdate.isEmpty() || !artifactsToDelete.isEmpty()) { - nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class); - if (!artifactsToUpload.isEmpty()) - nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.Create, artifactsToUpload); - if (!artifactsToUpdate.isEmpty()) - nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.Update, artifactsToUpdate); - if (!artifactsToDelete.isEmpty()) - nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.Delete, artifactsToDelete); - } - nodeTypeArtifactsToHandleRes = Either.left(nodeTypeArtifactsToHandle); - } - 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); - } - return nodeTypeArtifactsToHandleRes; - } - - /** - * Changes resource life cycle state to checked out - * - * @param resource - * @param user - * @param inTransaction - * @return - */ - private Either checkoutResource(Resource resource, User user, boolean inTransaction) { - Either checkoutResourceRes; - try { - if (!resource.getComponentMetadataDefinition().getMetadataDataDefinition().getState().equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) { - log.debug("************* Going to change life cycle state of resource {} to not certified checked out. ", resource.getName()); - Either checkoutRes = lifecycleBusinessLogic.changeComponentState(resource.getComponentType(), resource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, - new LifecycleChangeInfoWithAction("certification on import", LifecycleChanceActionEnum.CREATE_FROM_CSAR), inTransaction, true); - if (checkoutRes.isRight()) { - log.debug("Could not change state of component {} with uid {} to checked out. Status is {}. ", resource.getComponentType().getNodeType(), resource.getUniqueId(), checkoutRes.right().value().getStatus()); - 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; - } - - /** - * Handles Artifacts of NodeType - * - * @param nodeTypeResource - * @param nodeTypeArtifactsToHandle - * @param vfcsNewCreatedArtifacts - * @param user - * @param inTransaction - * @return - */ - public Either, ResponseFormat> handleNodeTypeArtifacts(Resource nodeTypeResource, Map> nodeTypeArtifactsToHandle, List createdArtifacts, - User user, boolean inTransaction, boolean ignoreLifecycleState) { - Either, ResponseFormat> handleNodeTypeArtifactsRequestRes; - Either, ResponseFormat> handleNodeTypeArtifactsRes = null; - Either changeStateResponse; - try { - changeStateResponse = checkoutResource(nodeTypeResource, user, inTransaction); - if (changeStateResponse.isRight()) { - return Either.right(changeStateResponse.right().value()); - } - nodeTypeResource = changeStateResponse.left().value(); - - List handledNodeTypeArtifacts = new ArrayList<>(); - log.debug("************* Going to handle artifacts of node type resource {}. ", nodeTypeResource.getName()); - for (Entry> curOperationEntry : nodeTypeArtifactsToHandle.entrySet()) { - ArtifactOperationEnum curOperation = curOperationEntry.getKey(); - List curArtifactsToHandle = curOperationEntry.getValue(); - if (curArtifactsToHandle != null && !curArtifactsToHandle.isEmpty()) { - log.debug("************* Going to {} artifact to vfc {}", curOperation.name(), nodeTypeResource.getName()); - handleNodeTypeArtifactsRequestRes = artifactsBusinessLogic.handleArtifactsRequestForInnerVfcComponent(curArtifactsToHandle, nodeTypeResource, user, createdArtifacts, - artifactsBusinessLogic.new ArtifactOperationInfo(false, ignoreLifecycleState, curOperation), false, inTransaction); - if (handleNodeTypeArtifactsRequestRes.isRight()) { - handleNodeTypeArtifactsRes = Either.right(handleNodeTypeArtifactsRequestRes.right().value()); - break; - } - if (curOperation == ArtifactOperationEnum.Create) { - createdArtifacts.addAll(handleNodeTypeArtifactsRequestRes.left().value()); - } - handledNodeTypeArtifacts.addAll(handleNodeTypeArtifactsRequestRes.left().value()); - } - } - 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; - } - - private Map extractVfcToscaNames(Map nodeTypesInfo, String vfResourceName, CsarInfo csarInfo) { - Map vfcToscaNames = new HashMap<>(); - - Map nodes = extractAllNodes(nodeTypesInfo, csarInfo); - if (!nodes.isEmpty()) { - Iterator> nodesNameEntry = nodes.entrySet().iterator(); - while (nodesNameEntry.hasNext()) { - Entry nodeType = nodesNameEntry.next(); - String toscaResourceName = buildNestedToscaResourceName(ResourceTypeEnum.VFC.name(), vfResourceName, nodeType.getKey()); - vfcToscaNames.put(nodeType.getKey(), toscaResourceName); - } - } - return vfcToscaNames; - } - - private Map extractAllNodes(Map nodeTypesInfo, CsarInfo csarInfo) { - Map nodes = new HashMap<>(); - for(NodeTypeInfo nodeTypeInfo: nodeTypesInfo.values()){ - extractNodeTypes(nodes, nodeTypeInfo.getMappedToscaTemplate()); - } - extractNodeTypes(nodes, csarInfo.getMappedToscaMainTemplate()); - return nodes; - } - - private void extractNodeTypes(Map nodes, Map mappedToscaTemplate) { - Either, ResultStatusEnum> eitherNodeTypes = ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, ToscaTagNamesEnum.NODE_TYPES); - if (eitherNodeTypes.isLeft()) { - nodes.putAll(eitherNodeTypes.left().value()); - } - } - - public Either createResourceFromCsar(Resource resource, User user, Either, StorageOperationStatus> csarUIPayload, String csarUUID) { - log.trace("************* created successfully from YAML, resource TOSCA "); - - Either, StorageOperationStatus> csar = null; - if (csarUIPayload != null && csarUIPayload.left() != null && csarUIPayload.left().value() != null) { - csar = csarUIPayload; - } else { - csar = csarOperation.getCsar(csarUUID, user); - } - - Either, ResponseFormat> toscaYamlCsarStatus = validateAndParseCsar(resource, user, csarUUID, csar); - if (toscaYamlCsarStatus.isRight()) { - return Either.right(toscaYamlCsarStatus.right().value()); - } - Either toscaYamlChecksum = CsarValidationUtils.getToscaYamlChecksum(csar.left().value(), csarUUID, componentsUtils); - if (toscaYamlChecksum.isRight()) { - log.debug("Failed to calculate checksum for CSAR {}, error {}", csarUUID, toscaYamlChecksum.right().value()); - return Either.right(toscaYamlChecksum.right().value()); - } - resource.getComponentMetadataDefinition().getMetadataDataDefinition().setImportedToscaChecksum(toscaYamlChecksum.left().value()); - - String yamlFileName = toscaYamlCsarStatus.left().value().getKey(); - String yamlFileContents = toscaYamlCsarStatus.left().value().getValue(); - log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContents); - - CsarInfo csarInfo = new CsarInfo(resource.getName(), user, csarUUID, csar.left().value(), yamlFileContents, false); - Map nodeTypesInfo = extractNodeTypesInfo(csarInfo); - Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo, resource); - if (findNodeTypesArtifactsToHandleRes.isRight()) { - log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID()); - return Either.right(findNodeTypesArtifactsToHandleRes.right().value()); - } - Either createResourceFromYaml = createResourceFromYaml(resource, yamlFileContents, yamlFileName, nodeTypesInfo, csarInfo, findNodeTypesArtifactsToHandleRes.left().value(), true, false, null); - if (createResourceFromYaml.isRight()) { - log.debug("Couldn't create resource from YAML"); - return Either.right(createResourceFromYaml.right().value()); - } - - Resource vfResource = createResourceFromYaml.left().value(); - log.trace("*************VF Resource created successfully from YAML, resource TOSCA name: {}", vfResource.getToscaResourceName()); - return Either.left(vfResource); - } - - private Map extractNodeTypesInfo(CsarInfo csarInfo) { - Map nodeTypesInfo = new HashMap<>(); - List> globalSubstitutes = new ArrayList<>(); - for (Map.Entry entry : csarInfo.getCsar().entrySet()) { - extractNodeTypeInfo(nodeTypesInfo, globalSubstitutes, entry); - } - if (CollectionUtils.isNotEmpty(globalSubstitutes)) { - setDerivedFrom(nodeTypesInfo, globalSubstitutes); - } - markNestedVfc(csarInfo.getMappedToscaMainTemplate(), nodeTypesInfo); - return nodeTypesInfo; - } - - @SuppressWarnings("unchecked") - private void setDerivedFrom(Map nodeTypesInfo, List> globalSubstitutes) { - for (Map.Entry entry : globalSubstitutes) { - String yamlFileContents = new String(entry.getValue()); - Map mappedToscaTemplate = (Map) new Yaml().load(yamlFileContents); - Either nodeTypesEither = ImportUtils.findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP); - if (nodeTypesEither.isLeft()) { - Map nodeTypes = (Map) nodeTypesEither.left().value(); - for (Entry nodeType : nodeTypes.entrySet()) { - Map nodeTypeMap = (Map) nodeType.getValue(); - if (nodeTypeMap.containsKey(ToscaTagNamesEnum.DERIVED_FROM.getElementName())) { - if (nodeTypesInfo.containsKey(nodeType.getKey())) { - NodeTypeInfo nodeTypeInfo = nodeTypesInfo.get(nodeType.getKey()); - List derivedFrom = new ArrayList<>(); - derivedFrom.add((String) nodeTypeMap.get(ToscaTagNamesEnum.DERIVED_FROM.getElementName())); - nodeTypeInfo.setDerivedFrom(derivedFrom); - } - } - } - } - } - } - - @SuppressWarnings("unchecked") - private void extractNodeTypeInfo(Map nodeTypesInfo, - List> globalSubstitutes, Map.Entry entry) { - if (Pattern.compile(CsarUtils.SERVICE_TEMPLATE_PATH_PATTERN).matcher(entry.getKey()).matches()) { - if (!isGlobalSubstitute(entry.getKey())) { - String yamlFileContents = new String(entry.getValue()); - Map mappedToscaTemplate = (Map) new Yaml().load(yamlFileContents); - Either substitutionMappingsEither = ImportUtils.findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS, ToscaElementTypeEnum.MAP); - if (substitutionMappingsEither.isLeft()) { - Map substitutionMappings = (Map) substitutionMappingsEither.left().value(); - if (substitutionMappings.containsKey(ToscaTagNamesEnum.NODE_TYPE.getElementName())) { - NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); - nodeTypeInfo.setType((String) substitutionMappings.get(ToscaTagNamesEnum.NODE_TYPE.getElementName())); - nodeTypeInfo.setTemplateFileName(entry.getKey()); - nodeTypeInfo.setMappedToscaTemplate(mappedToscaTemplate); - nodeTypesInfo.put(nodeTypeInfo.getType(), nodeTypeInfo); - } - } - } else { - globalSubstitutes.add(entry); - } - } - } - - - @SuppressWarnings("unchecked") - private void markNestedVfc(Map mappedToscaTemplate, Map nodeTypesInfo) { - Either nodeTemplatesEither = ImportUtils.findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP); - if (nodeTemplatesEither.isLeft()) { - Map nodeTemplates = (Map) nodeTemplatesEither.left().value(); - for (Entry nodeTemplateEntry : nodeTemplates.entrySet()) { - Map nodeTemplate = (Map) nodeTemplateEntry.getValue(); - if (nodeTemplate.containsKey(ToscaTagNamesEnum.TYPE.getElementName())) { - String type = (String) nodeTemplate.get(ToscaTagNamesEnum.TYPE.getElementName()); - if (nodeTypesInfo.containsKey(type)) { - NodeTypeInfo nodeTypeInfo = nodeTypesInfo.get(type); - nodeTypeInfo.setNested(true); - } - } - } - } - } - - private boolean isGlobalSubstitute(String fileName) { - return fileName.equalsIgnoreCase(Constants.GLOBAL_SUBSTITUTION_TYPES_SERVICE_TEMPLATE) || fileName.equalsIgnoreCase(Constants.ABSTRACT_SUBSTITUTE_GLOBAL_TYPES_SERVICE_TEMPLATE); - } - - private Either, ResponseFormat> validateAndParseCsar(Resource resource, User user, String csarUUID, Either, StorageOperationStatus> csar) { - if (csar.isRight()) { - StorageOperationStatus value = csar.right().value(); - log.debug("Error when fetching csar with ID {}, error: {}", csarUUID, value); - BeEcompErrorManager.getInstance().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, null); - return Either.right(responseFormat); - } - - Either validateCsarStatus = CsarValidationUtils.validateCsar(csar.left().value(), 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"); - componentsUtils.auditResource(responseFormat, user, resource, "", "", AuditingActionEnum.CREATE_RESOURCE, null); - return Either.right(responseFormat); - } - - Either, ResponseFormat> toscaYamlCsarStatus = CsarValidationUtils.getToscaYaml(csar.left().value(), 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"); - componentsUtils.auditResource(responseFormat, user, resource, "", "", AuditingActionEnum.CREATE_RESOURCE, null); - return Either.right(responseFormat); - } - return toscaYamlCsarStatus; - } - - private Either validateResourceBeforeCreate(Resource resource, User user, boolean inTransaction) { - log.trace("validating resource before create"); - Either eitherCreator = validateUser(user, "Create Resource", resource, AuditingActionEnum.CREATE_RESOURCE, false); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); - } - user.copyData(eitherCreator.left().value()); - - // validate user role - Either validateRes = validateUserRole(user, resource, new ArrayList(), AuditingActionEnum.CREATE_RESOURCE, null); - if (validateRes.isRight()) { - return Either.right(validateRes.right().value()); - } - // VF / PNF "derivedFrom" should be null (or ignored) - if (ToscaUtils.isAtomicType(resource)) { - Either validateDerivedFromNotEmpty = validateDerivedFromNotEmpty(user, resource, AuditingActionEnum.CREATE_RESOURCE); - if (validateDerivedFromNotEmpty.isRight()) { - return Either.right(validateDerivedFromNotEmpty.right().value()); - } - } - return validateResourceBeforeCreate(resource, user, AuditingActionEnum.CREATE_RESOURCE, inTransaction, null); - - } -//resource, yamlFileContents, yamlFileName, nodeTypesInfo,csarInfo, nodeTypesArtifactsToCreate, true, false, null - private Either createResourceFromYaml(Resource resource, String topologyTemplateYaml, String yamlName, - Map nodeTypesInfo, CsarInfo csarInfo, - Map>> nodeTypesArtifactsToCreate, - boolean shouldLock, boolean inTransaction, String nodeName) { - - List createdArtifacts = new ArrayList(); - log.trace("************* createResourceFromYaml before parse yaml "); - Either parseResourceInfoFromYamlEither = parseResourceInfoFromYaml(yamlName, resource, topologyTemplateYaml, csarInfo.getCreatedNodesToscaResourceNames(), nodeTypesInfo, nodeName); - if (parseResourceInfoFromYamlEither.isRight()) { - ResponseFormat responseFormat = parseResourceInfoFromYamlEither.right().value(); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, "", "", AuditingActionEnum.IMPORT_RESOURCE, null); - return Either.right(responseFormat); - } - log.trace("************* createResourceFromYaml after parse yaml "); - ParsedToscaYamlInfo parsedToscaYamlInfo = parseResourceInfoFromYamlEither.left().value(); - log.debug("The parsed tosca yaml info is {}", parsedToscaYamlInfo); - log.trace("************* createResourceFromYaml before create "); - Either createdResourceResponse = createResourceAndRIsFromYaml(yamlName, resource, parsedToscaYamlInfo, - AuditingActionEnum.IMPORT_RESOURCE, false, createdArtifacts, topologyTemplateYaml, - nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, shouldLock, inTransaction, nodeName); - log.trace("************* createResourceFromYaml after create "); - if (createdResourceResponse.isRight()) { - ResponseFormat responseFormat = createdResourceResponse.right().value(); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, "", "", AuditingActionEnum.IMPORT_RESOURCE, null); - return Either.right(responseFormat); - } - - return createdResourceResponse; - - } - - public Either, ResponseFormat> createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, Map mappedToscaTemplate, boolean needLock, - Map>> nodeTypesArtifactsToHandle, List nodeTypesNewCreatedArtifacts, - Map nodeTypesInfo, CsarInfo csarInfo) { - - Either tosca_version = ImportUtils.findFirstToscaStringElement(mappedToscaTemplate, ToscaTagNamesEnum.TOSCA_VERSION); - if (tosca_version.isRight()) { - ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_TOSCA_TEMPLATE); - return Either.right(responseFormat); - } - Either, ResultStatusEnum> eitherNodeTypes = ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, ToscaTagNamesEnum.NODE_TYPES); - Either, ResponseFormat> result = Either.left(csarInfo.getCreatedNodes()); - Map mapToConvert = new HashMap(); - mapToConvert.put(ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), tosca_version.left().value()); - Resource vfcCreated = null; - if (eitherNodeTypes.isLeft()) { - Iterator> nodesNameValue = eitherNodeTypes.left().value().entrySet().iterator(); - while (nodesNameValue.hasNext()) { - Entry nodeType = nodesNameValue.next(); - Map> nodeTypeArtifactsToHandle = nodeTypesArtifactsToHandle == null || nodeTypesArtifactsToHandle.isEmpty() ? null : nodeTypesArtifactsToHandle.get(nodeType.getKey()); - - if (nodeTypesInfo.containsKey(nodeType.getKey())) { - log.trace("************* Going to handle nested vfc {}", nodeType.getKey()); - Either handleNeatedVfcYaml = handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeType.getKey()); - log.trace("************* Finished to handle nested vfc {}", nodeType.getKey()); - if (handleNeatedVfcYaml.isRight()) { - return Either.right(handleNeatedVfcYaml.right().value()); - } - vfcCreated = handleNeatedVfcYaml.left().value(); - } else if(csarInfo.getCreatedNodesToscaResourceNames()!= null && !csarInfo.getCreatedNodesToscaResourceNames().containsKey(nodeType.getKey())){ - log.trace("************* Going to create node {}", nodeType.getKey()); - Either, ResponseFormat> resourceCreated = this.createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(), mapToConvert, resource, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, csarInfo); - log.debug("************* Finished to create node {}", nodeType.getKey()); - - if (resourceCreated.isRight()) { - return Either.right(resourceCreated.right().value()); - } - vfcCreated = resourceCreated.left().value().getLeft(); - csarInfo.getCreatedNodesToscaResourceNames().put(nodeType.getKey(),vfcCreated.getToscaResourceName()); - } - if (vfcCreated != null) { - csarInfo.getCreatedNodes().put(nodeType.getKey(), vfcCreated); - } - mapToConvert.remove(ToscaTagNamesEnum.NODE_TYPES.getElementName()); - } - } - return result; - } - - private Either handleNestedVfc(Resource resource, Map>> nodesArtifactsToHandle, - List createdArtifacts, Map nodesInfo, CsarInfo csarInfo, String nodeName) { - - Either handleNestedVfcRes = Either.left(resource); - String yamlName = nodesInfo.get(nodeName).getTemplateFileName(); - Map nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate(); - - log.debug("************* Going to create node types from yaml {}", yamlName); - Either, ResponseFormat> createNodeTypesRes = createResourcesFromYamlNodeTypesList(yamlName, resource, - nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo); - if (createNodeTypesRes.isRight()) { - log.debug("Failed to create node types from yaml {}. Status is {}", yamlName, createNodeTypesRes.right().value()); - return Either.right(createNodeTypesRes.right().value()); - } - log.debug("************* Finished to create node types from yaml {}", yamlName); - - if (nestedVfcJsonMap.containsKey(ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) { - log.debug("************* Going to handle complex VFC from yaml {}", yamlName); - handleNestedVfcRes = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName); - } - return handleNestedVfcRes; - } - - private Either handleComplexVfc(Resource resource, Map>> nodesArtifactsToHandle, List createdArtifacts, - Map nodesInfo, CsarInfo csarInfo, String nodeName, String yamlName) { - - Either result = null; - Resource oldComplexVfc = null; - Resource newComplexVfc = null; - - Either buildCvfcRes = buildValidComplexVfc(resource, csarInfo, nodeName, nodesInfo); - if(buildCvfcRes.isRight()){ - log.debug("Failed to validate complex VFC for node {}. ", nodeName); - result = buildCvfcRes; - } - if(result == null){ - newComplexVfc = buildCvfcRes.left().value(); - Either oldComplexVfcRes = toscaOperationFacade.getLatestByToscaResourceName(newComplexVfc.getToscaResourceName()); - if(oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND){ - log.debug("Failed to fetch previous complex VFC by tosca resource name {}. Status is {}. ", newComplexVfc.getToscaResourceName(), oldComplexVfcRes.right().value()); - result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } else if(oldComplexVfcRes.isLeft()){ - oldComplexVfc = oldComplexVfcRes.left().value(); - } - } - if(result == null){ - result = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, oldComplexVfc, newComplexVfc); - } - if(result.isLeft()){ - newComplexVfc = result.left().value(); - csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName()); - LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction("certification on import", LifecycleChanceActionEnum.CREATE_FROM_CSAR); - log.debug("Going to certify cvfc {}. ", newComplexVfc.getName()); - result = propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, true); - if (result.isRight()) { - log.debug("Failed to certify complex VFC resource {}. ", newComplexVfc.getName()); - } - } - if(result.isLeft()){ - csarInfo.getCreatedNodes().put(nodeName, result.left().value()); - csarInfo.getCvfcToCreateQueue().remove(); - } - return result; - } - - private Either handleComplexVfc(Map>> nodesArtifactsToHandle, List createdArtifacts, Map nodesInfo, - CsarInfo csarInfo, String nodeName, String yamlName, Resource oldComplexVfc, Resource newComplexVfc) { - - Either handleComplexVfcRes; - Map mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate(); - String yamlContent = new String(csarInfo.getCsar().get(yamlName)); - Map newNodeTypesInfo = nodesInfo.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().getUnmarkedCopy())); - markNestedVfc(mappedToscaTemplate, newNodeTypesInfo); - if(oldComplexVfc == null){ - handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo, csarInfo, nodesArtifactsToHandle, false, true, nodeName); - if (handleComplexVfcRes.isRight()) { - log.debug("Failed to create resource {} from YAML {}. ", newComplexVfc.getName(), yamlName); - } - } else { - handleComplexVfcRes = updateResourceFromYaml(oldComplexVfc, newComplexVfc, AuditingActionEnum.UPDATE_RESOURCE_METADATA, createdArtifacts, true, yamlContent, yamlName, csarInfo, newNodeTypesInfo, nodesArtifactsToHandle, nodeName); - if (handleComplexVfcRes.isRight()) { - log.debug("Failed to update resource {} from YAML {}. ", oldComplexVfc.getName(), yamlName); - } - } - return handleComplexVfcRes; - } - - private Either buildValidComplexVfc(Resource resource, CsarInfo csarInfo, String nodeName, Map nodesInfo) { - - Either result = null; - Resource complexVfc = buildComplexVfcMetadata(resource, csarInfo, nodeName, nodesInfo); - log.debug("************* Going to validate complex VFC from yaml {}", complexVfc.getName()); - - if(!csarInfo.getCvfcToCreateQueue().contains(nodeName)){ - csarInfo.getCvfcToCreateQueue().add(nodeName); - } else { - log.debug("Failed to validate complex VFC {}. Loop detected, VSP {}. ", complexVfc.getName(), csarInfo.getVfResourceName()); - result = Either.right(componentsUtils.getResponseFormat(ActionStatus.CFVC_LOOP_DETECTED, csarInfo.getVfResourceName(), complexVfc.getName())); - } - if(result == null){ - result = validateResourceBeforeCreate(complexVfc, csarInfo.getModifier(), AuditingActionEnum.IMPORT_RESOURCE, true, csarInfo); - if(result.isRight()){ - log.debug("Failed to validate complex VFC {}. ", complexVfc.getName()); - } - } - return result; - } - - private 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()); - } - - private Either, ResponseFormat> createNodeTypeResourceFromYaml(String yamlName, Entry nodeNameValue, User user, Map mapToConvert, Resource resourceVf, boolean needLock, - Map> nodeTypeArtifactsToHandle, List nodeTypesNewCreatedArtifacts, boolean forceCertificationAllowed, CsarInfo csarInfo) { - - Either resourceMetaData = fillResourceMetadata(yamlName, resourceVf, nodeNameValue.getKey(), user); - if (resourceMetaData.isRight()) { - return Either.right(resourceMetaData.right().value()); - } - String singleVfcYaml = buildNodeTypeYaml(nodeNameValue, mapToConvert, resourceMetaData.left().value().getResourceType(), csarInfo.getVfResourceName()); - - Either eitherCreator = validateUser(user, "CheckIn Resource", resourceVf, AuditingActionEnum.CHECKIN_RESOURCE, true); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); - } - user = eitherCreator.left().value(); - - return this.createResourceFromNodeType(singleVfcYaml, resourceMetaData.left().value(), user, true, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo); - } - - private String buildNodeTypeYaml(Entry nodeNameValue, Map mapToConvert, String nodeResourceType, String csarVfName) { - // 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 node = new HashMap<>(); - node.put(buildNestedToscaResourceName(nodeResourceType, csarVfName, nodeNameValue.getKey()), nodeNameValue.getValue()); - mapToConvert.put(ToscaTagNamesEnum.NODE_TYPES.getElementName(), node); - - return yaml.dumpAsMap(mapToConvert); - } - - public Either validateResourceCreationFromNodeType(Resource resource, User creator) { - - Either validateDerivedFromNotEmpty = this.validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE); - if (validateDerivedFromNotEmpty.isRight()) { - return Either.right(validateDerivedFromNotEmpty.right().value()); - } - return Either.left(true); - } - - public Either, ResponseFormat> createResourceFromNodeType(String nodeTypeYaml, UploadResourceInfo resourceMetaData, User creator, boolean isInTransaction, boolean needLock, - Map> nodeTypeArtifactsToHandle, List nodeTypesNewCreatedArtifacts, boolean forceCertificationAllowed, CsarInfo csarInfo) { - - LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction("certification on import", LifecycleChanceActionEnum.CREATE_FROM_CSAR); - Function> validator = (resource) -> this.validateResourceCreationFromNodeType(resource, creator); - return this.resourceImportManager.importCertifiedResource(nodeTypeYaml, resourceMetaData, creator, validator, lifecycleChangeInfo, isInTransaction, true, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo); - } - - private Either 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); - ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName); - return Either.right(responseFormat); - } - - 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()); - ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName); - return Either.right(responseFormat); - } - - // 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 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 categories = new ArrayList<>(); - categories.add(category); - resourceMetaData.setCategories(categories); - - return Either.left(resourceMetaData); - } - - private Resource buildComplexVfcMetadata(Resource resourceVf, CsarInfo csarInfo, String nodeName, Map 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.CVFC); - 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.CVFC.name(), csarInfo.getVfResourceName(), nodeName)); - cvfc.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID()); - - List 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 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]; - return resourceVfName + "-" + nameWithouNamespacePrefix.substring(resourceType.length() + 1) + "Cvfc"; - } - - private Either createResourceAndRIsFromYaml(String yamlName, Resource resource, ParsedToscaYamlInfo parsedToscaYamlInfo, AuditingActionEnum actionEnum, boolean isNormative, - List createdArtifacts, String topologyTemplateYaml, Map nodeTypesInfo, - CsarInfo csarInfo, Map>> nodeTypesArtifactsToCreate, - boolean shouldLock, boolean inTransaction, String nodeName) { - - boolean result = true; - List nodeTypesNewCreatedArtifacts = new ArrayList<>(); - - if (shouldLock) { - Either lockResult = lockComponentByName(resource.getSystemName(), resource, "Create Resource"); - if (lockResult.isRight()) { - ResponseFormat responseFormat = lockResult.right().value(); - return Either.right(responseFormat); - } - log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult); - } - try { - log.trace("************* createResourceFromYaml before full create resource {}", yamlName); - Either genericResourceEither = fetchAndSetDerivedFromGenericType(resource); - if (genericResourceEither.isRight()) { - result = false; - return genericResourceEither; - } - Either createResourcesEither = createResourceTransaction(resource, csarInfo.getModifier(), isNormative, inTransaction); - log.trace("************* createResourceFromYaml after full create resource {}", yamlName); - if (createResourcesEither.isRight()) { - result = false; - return createResourcesEither; - } - resource = createResourcesEither.left().value(); - // add groups to resource - log.trace("************* Going to add inputs from yaml {}", yamlName); - if (resource.shouldGenerateInputs()) - generateInputsFromGenericTypeProperties(resource, genericResourceEither.left().value()); - - Map inputs = parsedToscaYamlInfo.getInputs(); - Either createInputsOnResource = createInputsOnResource(resource, csarInfo.getModifier(), inputs, inTransaction); - if (createInputsOnResource.isRight()) { - result = false; - return createInputsOnResource; - } - resource = createInputsOnResource.left().value(); - log.trace("************* Finish to add inputs from yaml {}", yamlName); - - Map uploadComponentInstanceInfoMap = parsedToscaYamlInfo.getInstances(); - log.trace("************* Going to create nodes, RI's and Relations from yaml {}", yamlName); - - createResourcesEither = createRIAndRelationsFromYaml(yamlName, resource, uploadComponentInstanceInfoMap, actionEnum, topologyTemplateYaml, - nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName); - log.trace("************* Finished to create nodes, RI and Relation from yaml {}", yamlName); - if (createResourcesEither.isRight()) { - result = false; - return createResourcesEither; - } - - resource = createResourcesEither.left().value(); - // validate update vf module group names - Either, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), resource.getSystemName()); - if (validateUpdateVfGroupNamesRes.isRight()) { - result = false; - return Either.right(validateUpdateVfGroupNamesRes.right().value()); - } - // add groups to resource - Map groups; - log.trace("************* Going to add groups from yaml {}", yamlName); - - if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) { - groups = validateUpdateVfGroupNamesRes.left().value(); - } else { - groups = parsedToscaYamlInfo.getGroups(); - } - Either createGroupsOnResource = createGroupsOnResource(resource, csarInfo.getModifier(), groups); - if (createGroupsOnResource.isRight()) { - result = false; - return createGroupsOnResource; - } - resource = createGroupsOnResource.left().value(); - log.trace("************* Finished to add groups from yaml {}", yamlName); - - log.trace("************* Going to add artifacts from yaml {}", yamlName); - - if(resource.getResourceType() == ResourceTypeEnum.CVFC){ - if(nodeTypesArtifactsToCreate.containsKey(nodeName) && nodeTypesArtifactsToCreate.get(nodeName) !=null && !nodeTypesArtifactsToCreate.get(nodeName).isEmpty()){ - Either, ResponseFormat> handleNodeTypeArtifactsRes = - handleNodeTypeArtifacts(resource, nodeTypesArtifactsToCreate.get(nodeName), nodeTypesNewCreatedArtifacts, csarInfo.getModifier(), true, false); - if(handleNodeTypeArtifactsRes.isRight()){ - return Either.right(handleNodeTypeArtifactsRes.right().value()); - } - } - } else { - Either createdCsarArtifactsEither = this.handleVfCsarArtifacts(resource, csarInfo, createdArtifacts, artifactsBusinessLogic.new ArtifactOperationInfo(false, false, ArtifactOperationEnum.Create), false, - inTransaction); - log.trace("************* Finished to add artifacts from yaml {}", yamlName); - if (createdCsarArtifactsEither.isRight()) { - result = false; - return createdCsarArtifactsEither; - } - resource = createdCsarArtifactsEither.left().value(); - } - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, "", "", actionEnum, null); - ASDCKpiApi.countCreatedResourcesKPI(); - return Either.left(resource); - - } finally { - if (!inTransaction) { - if (!result) { - log.warn("operation failed. do rollback"); - titanDao.rollback(); - if (!createdArtifacts.isEmpty() || !nodeTypesNewCreatedArtifacts.isEmpty()) { - createdArtifacts.addAll(nodeTypesNewCreatedArtifacts); - StorageOperationStatus deleteFromEsRes = artifactsBusinessLogic.deleteAllComponentArtifactsIfNotOnGraph(createdArtifacts); - if (!deleteFromEsRes.equals(StorageOperationStatus.OK)) { - ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(deleteFromEsRes); - return Either.right(componentsUtils.getResponseFormat(actionStatus, resource.getName())); - } - log.debug("component and all its artifacts were deleted, id = {}", resource.getName()); - } - - } else { - log.debug("operation success. do commit"); - titanDao.commit(); - } - } - if (shouldLock) { - graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource); - } - } - } - - private Either createGroupsOnResource(Resource resource, User user, Map groups) { - if (groups != null && !groups.isEmpty()) { - Either, ResponseFormat> mergeGroupsUsingResource = updateGroupMembersUsingResource(groups, resource); - - if (mergeGroupsUsingResource.isRight()) { - log.debug("Failed to prepare groups for creation"); - return Either.right(mergeGroupsUsingResource.right().value()); - } - List groupsAsList = mergeGroupsUsingResource.left().value(); - Either, ResponseFormat> createGroups = groupBusinessLogic.createGroups(resource, user, ComponentTypeEnum.RESOURCE, groupsAsList); - if (createGroups.isRight()) { - return Either.right(createGroups.right().value()); - } - } else { - return Either.left(resource); - } - Either 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()); - } - - private Either updateGroupsOnResource(Resource resource, User user, Map groups) { - if (groups != null && false == groups.isEmpty()) { - List groupsFromResource = resource.getGroups(); - Either, ResponseFormat> mergeGroupsUsingResource = updateGroupMembersUsingResource(groups, resource); - - if (mergeGroupsUsingResource.isRight()) { - log.debug("Failed to prepare groups for creation"); - return Either.right(mergeGroupsUsingResource.right().value()); - } - List groupsAsList = mergeGroupsUsingResource.left().value(); - List groupsToUpdate = new ArrayList(); - List groupsToDelete = new ArrayList(); - List groupsToCreate = new ArrayList(); - if (groupsFromResource != null && !groupsFromResource.isEmpty()) { - for (GroupDefinition group : groupsAsList) { - Optional op = groupsFromResource.stream().filter(p -> p.getName().equalsIgnoreCase(group.getName())).findAny(); - if (op.isPresent()) { - GroupDefinition groupToUpdate = op.get(); - groupToUpdate.setMembers(group.getMembers()); - groupsToUpdate.add(groupToUpdate); - } else { - groupsToCreate.add(group); - } - } - for (GroupDefinition group : groupsFromResource) { - Optional op = groupsAsList.stream().filter(p -> p.getName().equalsIgnoreCase(group.getName())).findAny(); - if (!op.isPresent() && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) { - - groupsToDelete.add(group); - } - - } - } else - groupsToCreate.addAll(groupsAsList); - Either, ResponseFormat> prepareGroups = null; - if (!groupsToCreate.isEmpty()) { - - if (groupsFromResource != null && !groupsFromResource.isEmpty()) { - prepareGroups = groupBusinessLogic.addGroups(resource, user, ComponentTypeEnum.RESOURCE, groupsToCreate); - } else { - prepareGroups = groupBusinessLogic.createGroups(resource, user, ComponentTypeEnum.RESOURCE, groupsToCreate); - } - - if (prepareGroups.isRight()) { - return Either.right(prepareGroups.right().value()); - } - } - - if (!groupsToDelete.isEmpty()) { - prepareGroups = groupBusinessLogic.deleteGroups(resource, user, ComponentTypeEnum.RESOURCE, groupsToDelete); - if (prepareGroups.isRight()) { - return Either.right(prepareGroups.right().value()); - } - } - - if (groupsToUpdate != null && !groupsToUpdate.isEmpty()) { - prepareGroups = groupBusinessLogic.updateGroups(resource, ComponentTypeEnum.RESOURCE, groupsToUpdate); - if (prepareGroups.isRight()) { - return Either.right(prepareGroups.right().value()); - } - } - - } else { - return Either.left(resource); - } - - Either 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()); - } - - private Either createInputsOnResource(Resource resource, User user, Map inputs, boolean inTransaction) { - List resourceProperties = resource.getInputs(); - if ( (inputs != null && false == inputs.isEmpty()) || (resourceProperties != null && false == resourceProperties.isEmpty()) ) { - - Either, ResponseFormat> createInputs = inputsBusinessLogic.createInputsInGraph(inputs, resource, user, inTransaction); - if (createInputs.isRight()) { - return Either.right(createInputs.right().value()); - } - } else { - return Either.left(resource); - } - - Either 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()); - } - - private Either, ResponseFormat> updateGroupMembersUsingResource(Map groups, Resource component) { - - List result = new ArrayList<>(); - - List componentInstances = component.getComponentInstances(); - - if (groups != null) { - Either validateCyclicGroupsDependencies = validateCyclicGroupsDependencies(groups); - if (validateCyclicGroupsDependencies.isRight()) { - return FunctionalInterfaces.convertEitherRight(validateCyclicGroupsDependencies); - } - for (Entry entry : groups.entrySet()) { - String groupName = entry.getKey(); - - GroupDefinition groupDefinition = entry.getValue(); - - GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition); - updatedGroupDefinition.setMembers(null); - - // get the members of the group - Map members = groupDefinition.getMembers(); - if (members != null) { - Set compInstancesNames = members.keySet(); - - if (componentInstances == null || true == componentInstances.isEmpty()) { - String membersAstString = compInstancesNames.stream().collect(Collectors.joining(",")); - log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", membersAstString, groupName, component.getNormalizedName()); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, component.getNormalizedName(), getComponentTypeForResponse(component))); - } - // Find all component instances with the member names - Map memberNames = componentInstances.stream().collect(Collectors.toMap(ComponentInstance::getName, ComponentInstance::getUniqueId)); - memberNames.putAll(groups.keySet().stream().collect(Collectors.toMap(g -> g, g -> ""))); - Map relevantInstances = memberNames.entrySet().stream().filter(n -> compInstancesNames.contains(n.getKey())).collect(Collectors.toMap(n -> n.getKey(), n -> n.getValue())); - - if (relevantInstances == null || relevantInstances.size() != compInstancesNames.size()) { - - List foundMembers = new ArrayList<>(); - if (relevantInstances != null) { - foundMembers = relevantInstances.keySet().stream().collect(Collectors.toList()); - } - compInstancesNames.removeAll(foundMembers); - String membersAstString = compInstancesNames.stream().collect(Collectors.joining(",")); - log.debug("The members: {}, in group: {}, cannot be found in component: {}", membersAstString, groupName, component.getNormalizedName()); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, component.getNormalizedName(), getComponentTypeForResponse(component))); - } - - updatedGroupDefinition.setMembers(relevantInstances); - } - - result.add(updatedGroupDefinition); - } - } - return Either.left(result); - } - - /** - * This Method validates that there is no cyclic group dependencies. meaning group A as member in group B which is member in group A - * - * @param allGroups - * @return - */ - private Either validateCyclicGroupsDependencies(Map allGroups) { - - Either result = Either.left(true); - try { - Iterator> allGroupsItr = allGroups.entrySet().iterator(); - while (allGroupsItr.hasNext() && result.isLeft()) { - Entry groupAEntry = allGroupsItr.next(); - // Fetches a group member A - String groupAName = groupAEntry.getKey(); - // Finds all group members in group A - Set 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; - } - - /** - * This Method fills recursively the set groupMembers with all the members of the given group which are also of type group. - * - * @param groupName - * @param allGroups - * @param allGroupMembers - * @return - */ - private void fillAllGroupMemebersRecursivly(String groupName, Map allGroups, Set 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 currGroupMembers = groupDefinition.getMembers().keySet(); - // Filtered Members Of Current Group containing only members which - // are groups - List currGroupFilteredMembers = currGroupMembers.stream(). - // Keep Only Elements of type group and not Resource Instances - filter(innerGroupName -> allGroups.containsKey(innerGroupName)). - // Add Filtered Elements to main Set - peek(innerGroupName -> allGroupMembers.add(innerGroupName)). - // Collect results - collect(Collectors.toList()); - - // Recursively call the method for all the filtered group members - for (String innerGroupName : currGroupFilteredMembers) { - fillAllGroupMemebersRecursivly(innerGroupName, allGroups, allGroupMembers); - } - - } - - private boolean isfillGroupMemebersRecursivlyStopCondition(String groupName, Map allGroups, Set 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 allMembers = allGroups.get(groupName).getMembers().keySet(); - Set membersOfTypeGroup = allMembers.stream(). - // Filter In Only Group members - filter(innerGroupName -> allGroups.containsKey(innerGroupName)). - // Collect - collect(Collectors.toSet()); - stop = allGroupMembers.containsAll(membersOfTypeGroup); - } - return stop; - } - - private Either createRIAndRelationsFromYaml(String yamlName, Resource resource, Map uploadComponentInstanceInfoMap, AuditingActionEnum actionEnum, - String topologyTemplateYaml, List nodeTypesNewCreatedArtifacts, - Map nodeTypesInfo, CsarInfo csarInfo, Map>> nodeTypesArtifactsToCreate, - String nodeName) { - - Either result; - Either createResourcesInstancesEither; - - log.debug("************* Going to create all nodes {}", yamlName); - Either, ResponseFormat> createdResourcesFromdNodeTypeMap = this.handleNodeTypes(yamlName, resource, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeName); - if (createdResourcesFromdNodeTypeMap.isRight()) { - log.debug("failed to resources from node types status is {}", createdResourcesFromdNodeTypeMap.right().value()); - return Either.right(createdResourcesFromdNodeTypeMap.right().value()); - } - log.debug("************* Finished to create all nodes {}", yamlName); - - log.debug("************* Going to create all resource instances {}", yamlName); - createResourcesInstancesEither = createResourceInstances(csarInfo.getModifier(), yamlName, resource, uploadComponentInstanceInfoMap, true, false, csarInfo.getCreatedNodes()); - - if (createResourcesInstancesEither.isRight()) { - log.debug("failed to create resource instances status is {}", createResourcesInstancesEither.right().value()); - result = createResourcesInstancesEither; - return createResourcesInstancesEither; - } - log.debug("************* Finished to create all resource instances for {}", yamlName); - resource = createResourcesInstancesEither.left().value(); - log.debug("************* Going to create all relations {}", yamlName); - createResourcesInstancesEither = createResourceInstancesRelations(csarInfo.getModifier(), yamlName, resource, uploadComponentInstanceInfoMap); - - if (createResourcesInstancesEither.isRight()) { - log.debug("failed to create relation between resource instances status is {}", createResourcesInstancesEither.right().value()); - result = createResourcesInstancesEither; - return result; - } else { - resource = createResourcesInstancesEither.left().value(); - } - log.debug("************* Finished to create all relations {}", yamlName); - - log.debug("************* Going to create positions {}", yamlName); - Either, ResponseFormat> eitherSetPosition = compositionBusinessLogic.setPositionsForComponentInstances(resource, csarInfo.getModifier().getUserId()); - log.debug("************* Finished to set positions {}", yamlName); - result = eitherSetPosition.isRight() ? Either.right(eitherSetPosition.right().value()) : Either.left(resource); - - return result; - } - - private void handleAndAddExtractedVfcsArtifacts(List vfcArtifacts, List artifactsToAdd) { - List vfcArtifactNames = vfcArtifacts.stream().map(a -> a.getArtifactName()).collect(Collectors.toList()); - artifactsToAdd.stream().forEach(a -> { - if (!vfcArtifactNames.contains(a.getArtifactName())) { - vfcArtifacts.add(a); - } else { - log.error("Can't upload two artifact with the same name {}. ", a.getArtifactName()); - } - }); - - } - - @SuppressWarnings("unchecked") - private Either, ResponseFormat> handleNodeTypes(String yamlName, Resource resource, String topologyTemplateYaml, boolean needLock, - Map>> nodeTypesArtifactsToHandle, List nodeTypesNewCreatedArtifacts, - Map nodeTypesInfo, CsarInfo csarInfo, String nodeName) { - - Either, ResponseFormat> result = Either.left(csarInfo.getCreatedNodes()); - for (Entry nodeTypeEntry : nodeTypesInfo.entrySet()) { - if (nodeTypeEntry.getValue().isNested()) { - - Either createResourceFromYaml = handleNestedVfc(resource, nodeTypesArtifactsToHandle, - nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypeEntry.getKey()); - log.trace("************* finished to create node {}", nodeTypeEntry.getKey()); - if (createResourceFromYaml.isRight()) { - return Either.right(createResourceFromYaml.right().value()); - } - } - } - - Map mappedToscaTemplate = null; - if(StringUtils.isNotEmpty(nodeName) && MapUtils.isNotEmpty(nodeTypesInfo) && nodeTypesInfo.containsKey(nodeName)){ - mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate(); - } - if(MapUtils.isEmpty(mappedToscaTemplate)){ - mappedToscaTemplate = (Map) new Yaml().load(topologyTemplateYaml); - } - - Either, ResponseFormat> createdNodeTypeFromMainTemplateEither = createResourcesFromYamlNodeTypesList(yamlName, resource, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle, - nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo); - if (createdNodeTypeFromMainTemplateEither.isRight()) { - ResponseFormat responseFormat = createdNodeTypeFromMainTemplateEither.right().value(); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, "", "", AuditingActionEnum.IMPORT_RESOURCE, null); - return Either.right(responseFormat); - } - - // add the created node types to the cache although they are not in the - // graph. - csarInfo.getCreatedNodes().values().stream().forEach(p -> cacheManagerOperation.storeComponentInCache(p, NodeTypeEnum.Resource)); - - return result; - } - - private Either handleVfCsarArtifacts(Resource resource, CsarInfo csarInfo, List createdArtifacts, ArtifactOperationInfo artifactOperation, boolean shouldLock, - boolean inTransaction) { - - if (csarInfo.getCsar() != null) { - String vendorLicenseModelId = null; - String vfLicenseModelId = null; - - if (artifactOperation.getArtifactOperationEnum() == ArtifactOperationEnum.Update) { - Map deploymentArtifactsMap = resource.getDeploymentArtifacts(); - if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) { - for (Entry 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(); - } - } - - } - // Specific Behavior for license artifacts - 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, 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, shouldLock, inTransaction); - - Either eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, artifactOperation); - if (eitherCreateResult.isRight()) { - return Either.right(eitherCreateResult.right().value()); - } - - Either, 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 createArtifactsFromCsar = Either.left(resource); - if (artifactOperation.getArtifactOperationEnum() == ArtifactOperationEnum.Create) - createArtifactsFromCsar = createResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts, shouldLock, inTransaction); - else - createArtifactsFromCsar = updateResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts, shouldLock, inTransaction); - if (createArtifactsFromCsar.isRight()) { - log.debug("Couldn't create artifacts from artifacts.meta"); - return Either.right(createArtifactsFromCsar.right().value()); - } - - resource = createArtifactsFromCsar.left().value(); - } else { - List groupsToDelete = resource.getGroups(); - - if (groupsToDelete != null && !groupsToDelete.isEmpty()) { - Set artifactsToDelete = new HashSet(); - /* - * for (GroupDefinition group : groupsToDelete) { List artifacts = group.getArtifacts(); if (artifacts != null) { artifactsToDelete.addAll(artifacts); Either deleteGroupEither = - * groupOperation.deleteGroup(group.getUniqueId(), inTransaction); if (deleteGroupEither.isRight()) { StorageOperationStatus storageOperationStatus = deleteGroupEither.right().value(); ActionStatus actionStatus = - * componentsUtils.convertFromStorageResponse(storageOperationStatus); log.debug("Failed to delete group {} under component {}, error: {}", group.getUniqueId(), resource.getNormalizedName(), actionStatus.name()); return - * Either.right(componentsUtils.getResponseFormat(actionStatus)); } } } - */ - for (String artifactId : artifactsToDelete) { - Either, ResponseFormat> handleDelete = artifactsBusinessLogic.handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), AuditingActionEnum.ARTIFACT_DELETE, ComponentTypeEnum.RESOURCE, - resource, null, null, shouldLock, inTransaction); - if (handleDelete.isRight()) { - log.debug("Couldn't delete artifact {}", artifactId); - return Either.right(handleDelete.right().value()); - } - } - Either eitherGetResource = toscaOperationFacade.getToscaElement(resource.getUniqueId()); - if (eitherGetResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource); - - return Either.right(responseFormat); - - } - resource = eitherGetResource.left().value(); - } - } - } - return Either.left(resource); - } - - private Either createOrUpdateSingleNonMetaArtifact(Resource resource, CsarInfo csarInfo, String artifactPath, String artifactFileName, String artifactType, - ArtifactGroupTypeEnum artifactGroupType, String artifactLabel, String artifactDisplayName, String artifactDescription, String artifactId, ArtifactOperationInfo operation, List createdArtifacts, boolean shouldLock, - boolean inTransaction) { - byte[] artifactFileBytes = null; - - if (csarInfo.getCsar().containsKey(artifactPath)) { - artifactFileBytes = csarInfo.getCsar().get(artifactPath); - } - Either result = Either.left(true); - if (operation.getArtifactOperationEnum() == ArtifactOperationEnum.Update || operation.getArtifactOperationEnum() == ArtifactOperationEnum.Delete) { - if (artifactId != null && !artifactId.isEmpty() && artifactFileBytes == null) { - Either, ResponseFormat> handleDelete = artifactsBusinessLogic.handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), AuditingActionEnum.ARTIFACT_DELETE, ComponentTypeEnum.RESOURCE, resource, null, - null, shouldLock, inTransaction); - if (handleDelete.isRight()) { - result = Either.right(handleDelete.right().value()); - } - return result; - } - - if ((artifactId == null || artifactId.isEmpty()) && artifactFileBytes != null) { - operation = artifactsBusinessLogic.new ArtifactOperationInfo(false, false, ArtifactOperationEnum.Create); - } - - } - if (artifactFileBytes != null) { - Map vendorLicenseModelJson = buildJsonForUpdateArtifact(artifactId, artifactFileName, artifactType, artifactGroupType, artifactLabel, artifactDisplayName, artifactDescription, artifactFileBytes, null); - Either, ResponseFormat> eitherNonMetaArtifacts = createOrUpdateCsarArtifactFromJson(resource, csarInfo.getModifier(), vendorLicenseModelJson, operation, shouldLock, inTransaction); - addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts); - if (eitherNonMetaArtifacts.isRight()) { - BeEcompErrorManager.getInstance().logInternalFlowError("UploadLicenseArtifact", "Failed to upload license artifact: " + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(), ErrorSeverity.WARNING); - return Either.right(eitherNonMetaArtifacts.right().value()); - } - } - return result; - } - - private void addNonMetaCreatedArtifactsToSupportRollback(ArtifactOperationInfo operation, List createdArtifacts, Either, ResponseFormat> eitherNonMetaArtifacts) { - if (operation.getArtifactOperationEnum() == ArtifactOperationEnum.Create && createdArtifacts != null && eitherNonMetaArtifacts.isLeft()) { - Either eitherResult = eitherNonMetaArtifacts.left().value(); - if (eitherResult.isLeft()) { - createdArtifacts.add(eitherResult.left().value()); - } - } - } - - private Either, ResponseFormat> createOrUpdateCsarArtifactFromJson(Resource resource, User user, Map json, ArtifactOperationInfo operation, boolean shoudLock, boolean inTransaction) { - - String jsonStr = gson.toJson(json); - - String origMd5 = GeneralUtility.calculateMD5Base64EncodedByString(jsonStr); - ArtifactDefinition artifactDefinitionFromJson = RepresentationUtils.convertJsonToArtifactDefinition(jsonStr, ArtifactDefinition.class); - String artifactUniqueId = artifactDefinitionFromJson == null ? null : artifactDefinitionFromJson.getUniqueId(); - Either, ResponseFormat> uploadArtifactToService = artifactsBusinessLogic.validateAndHandleArtifact(resource.getUniqueId(), ComponentTypeEnum.RESOURCE, operation, artifactUniqueId, - artifactDefinitionFromJson, origMd5, jsonStr, null, null, null, user, resource, false, true, false); - if (uploadArtifactToService.isRight()) - return Either.right(uploadArtifactToService.right().value()); - - return Either.left(uploadArtifactToService.left().value()); - } - - public Either updateResourceArtifactsFromCsar(CsarInfo csarInfo, Resource resource, String artifactsMetaFile, String artifactsMetaFileName, List createdNewArtifacts, - boolean shouldLock, boolean inTransaction) { - - Either>, ResponseFormat> parseResourceInfoFromYamlEither = parseResourceArtifactsInfoFromFile(resource, artifactsMetaFile, artifactsMetaFileName, csarInfo.getModifier()); - if (parseResourceInfoFromYamlEither.isRight()) { - ResponseFormat responseFormat = parseResourceInfoFromYamlEither.right().value(); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, "", "", AuditingActionEnum.IMPORT_RESOURCE, null); - return Either.right(responseFormat); - } - - List groups = resource.getGroups(); - Map deplymentArtifact = resource.getDeploymentArtifacts(); - List createdDeplymentArtifactsAfterDelete = new ArrayList(); - if (deplymentArtifact != null && !deplymentArtifact.isEmpty()) { - for (Entry entry : deplymentArtifact.entrySet()) { - createdDeplymentArtifactsAfterDelete.add(entry.getValue()); - } - } - int labelCounter = createdDeplymentArtifactsAfterDelete.size(); - - if (deplymentArtifact == null || deplymentArtifact.isEmpty()) { - if (groups != null && !groups.isEmpty()) { - for (GroupDefinition group : groups) { - if (group.getArtifacts() != null && !group.getArtifacts().isEmpty()) { - log.debug("failed to update artifacts from csar. List of emty but group not empty"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); - return Either.right(responseFormat); - } - } - } - return createResourceArtifacts(csarInfo, resource, parseResourceInfoFromYamlEither.left().value(), AuditingActionEnum.CREATE_RESOURCE, createdNewArtifacts, shouldLock, inTransaction); - } - - // find master in group - Map>> groupArtifact = findMasterArtifactInGroup(groups, deplymentArtifact); - - ////////////////////////////////////// create set parsed - ////////////////////////////////////// artifacts/////////////////////////////////////////// - Map> parsedArtifactsMap = parseResourceInfoFromYamlEither.left().value(); - Collection> parsedArifactsCollection = parsedArtifactsMap.values(); - Map> parsedGroup = new HashMap>(); - - for (List parsedGroupTemplateList : parsedArifactsCollection) { - for (ArtifactTemplateInfo parsedGroupTemplate : parsedGroupTemplateList) { - parsedGroupTemplate.setGroupName(""); - Set parsedArtifactsNames = new HashSet(); - parsedArtifactsNames.add(parsedGroupTemplate); - List relatedGroupTemplateList = parsedGroupTemplate.getRelatedArtifactsInfo(); - if (relatedGroupTemplateList != null && !relatedGroupTemplateList.isEmpty()) { - createArtifactsGroupSet(parsedGroupTemplateList, parsedArtifactsNames); - } - parsedGroup.put(parsedGroupTemplate, parsedArtifactsNames); - } - } - - ///////////////////////////////// find artifacts to - ///////////////////////////////// delete//////////////////////////////////////////////////// - - Set artifactsToDelete = new HashSet(); - Map> groupToDelete = new HashMap>(); - - Set jsonMasterArtifacts = parsedGroup.keySet(); - Map mergedgroup = mergeGroupInUpdateFlow(groupArtifact, parsedGroup, artifactsToDelete, groupToDelete, jsonMasterArtifacts, createdDeplymentArtifactsAfterDelete); - - // Set deletedArtifactsName = new HashSet(); - Either, ResponseFormat> deletedArtifactsEither = deleteArtifactsInUpdateCsarFlow(resource, csarInfo.getModifier(), shouldLock, inTransaction, artifactsToDelete, groupToDelete); - if (deletedArtifactsEither.isRight()) { - log.debug("Failed to delete artifacts. Status is {} ", deletedArtifactsEither.right().value()); - - return Either.right(deletedArtifactsEither.right().value()); - - } - List deletedArtifacts = deletedArtifactsEither.left().value(); - - // need to update resource if we updated artifacts - if (deletedArtifacts != null && !deletedArtifacts.isEmpty()) { - for (ArtifactDefinition deletedArtifact : deletedArtifacts) { - ArtifactDefinition artToRemove = null; - for (ArtifactDefinition artFromResource : createdDeplymentArtifactsAfterDelete) { - if (deletedArtifact.getUniqueId().equalsIgnoreCase(artFromResource.getUniqueId())) { - artToRemove = artFromResource; - break; - } - } - if (artToRemove != null) - createdDeplymentArtifactsAfterDelete.remove(artToRemove); - - } - } - - ////////////// dissociate, associate or create - ////////////// artifacts//////////////////////////// - Either assDissotiateEither = associateAndDissociateArtifactsToGroup(csarInfo, resource, createdNewArtifacts, labelCounter, shouldLock, inTransaction, createdDeplymentArtifactsAfterDelete, - mergedgroup, deletedArtifacts); - - if (assDissotiateEither.isRight()) { - log.debug("Failed to delete artifacts. Status is {} ", assDissotiateEither.right().value()); - - return Either.right(assDissotiateEither.right().value()); - - } - resource = assDissotiateEither.left().value(); - deplymentArtifact = resource.getDeploymentArtifacts(); - createdDeplymentArtifactsAfterDelete.clear(); - if (deplymentArtifact != null && !deplymentArtifact.isEmpty()) { - for (Entry entry : deplymentArtifact.entrySet()) { - createdDeplymentArtifactsAfterDelete.add(entry.getValue()); - } - } - - groups = resource.getGroups(); - - // update vfModule names - Set groupForAssociateWithMembers = mergedgroup.keySet(); - if (groups != null && !groups.isEmpty()) { - Either, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNamesOnGraph(groups, resource, inTransaction); - if (validateUpdateVfGroupNamesRes.isRight()) { - return Either.right(validateUpdateVfGroupNamesRes.right().value()); - } - List heatGroups = null; - - heatGroups = groups.stream().filter(e -> e.getMembers() != null).collect(Collectors.toList()); - ; - - for (GroupDefinition updatedGroupDef : groupForAssociateWithMembers) { - - if (updatedGroupDef.getMembers() != null && !updatedGroupDef.getMembers().isEmpty()) { - updatedGroupDef.getMembers().clear(); - } - Map members = new HashMap(); - Set artifactsGroup = new HashSet(); - artifactsGroup.addAll(updatedGroupDef.getArtifacts()); - associateMembersToArtifacts(createdNewArtifacts, createdDeplymentArtifactsAfterDelete, heatGroups, artifactsGroup, members); - if (!members.isEmpty()) { - updatedGroupDef.setMembers(members); - - } - - - } - - - } - - //////////////// create new artifacts in update - //////////////// flow//////////////////////////// - List newArtifactsGroup = new ArrayList(); - - for (Entry> parsedGroupSetEntry : parsedGroup.entrySet()) { - ArtifactTemplateInfo parsedArtifactMaster = parsedGroupSetEntry.getKey(); - boolean isNewGroup = true; - for (Entry>> groupListEntry : groupArtifact.entrySet()) { - Map> groupArtifacts = groupListEntry.getValue(); - Set group = groupArtifacts.keySet(); - for (ArtifactDefinition artifactInfo : group) { - if (parsedArtifactMaster.getFileName().equalsIgnoreCase(artifactInfo.getArtifactName())) { - parsedArtifactMaster.setGroupName(groupListEntry.getKey().getName()); - isNewGroup = false; - } - } - } - if (isNewGroup) - newArtifactsGroup.add(parsedArtifactMaster); - - } - if (!newArtifactsGroup.isEmpty()) { - Collections.sort(newArtifactsGroup, (art1, art2) -> ArtifactTemplateInfo.compareByGroupName(art1, art2)); - int startGroupCounter = groupBusinessLogic.getNextVfModuleNameCounter(groups); - Either validateGroupNamesRes = groupBusinessLogic.validateGenerateVfModuleGroupNames(newArtifactsGroup, resource.getSystemName(), startGroupCounter); - if (validateGroupNamesRes.isRight()) { - return Either.right(validateGroupNamesRes.right().value()); - } - Either resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, resource, newArtifactsGroup, createdNewArtifacts, createdDeplymentArtifactsAfterDelete, labelCounter, shouldLock, inTransaction); - if (resStatus.isRight()) - return resStatus; - } - - // updatedGroup - if (!groupForAssociateWithMembers.isEmpty()) { - - List groupsId = groupForAssociateWithMembers.stream().map(e -> e).collect(Collectors.toList()); - - Either, ResponseFormat> updateVersionEither = groupBusinessLogic.updateGroups(resource, ComponentTypeEnum.RESOURCE, groupsId); - if (updateVersionEither.isRight()) { - log.debug("Failed to update groups version. Status is {} ", updateVersionEither.right().value()); - - return Either.right(updateVersionEither.right().value()); - - } - } - - Either eitherGerResource = toscaOperationFacade.getToscaElement(resource.getUniqueId()); - if (eitherGerResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource); - - return Either.right(responseFormat); - - } - return Either.left(eitherGerResource.left().value()); - - } - - private Either, ResponseFormat> deleteArtifactsInUpdateCsarFlow(Resource resource, User user, boolean shouldLock, boolean inTransaction, Set artifactsToDelete, - Map> groupToDelete) { - List deletedArtifacts = new ArrayList(); - String resourceId = resource.getUniqueId(); - if (!artifactsToDelete.isEmpty()) { - for (ArtifactDefinition artifact : artifactsToDelete) { - String artifactType = artifact.getArtifactType(); - ArtifactTypeEnum artifactTypeEnum = ArtifactTypeEnum.findType(artifactType); - if (artifactTypeEnum == ArtifactTypeEnum.HEAT_ENV) { - - /* - * Either removeArifactFromGraph = artifactOperation.removeArifactFromResource(resourceId, artifact.getUniqueId(), NodeTypeEnum.Resource, true, true); if - * (removeArifactFromGraph.isRight()) { StorageOperationStatus status = removeArifactFromGraph.right().value(); log.debug("Failed to delete heat env artifact {} . status is {}", artifact.getUniqueId(), status); ActionStatus - * actionStatus = componentsUtils.convertFromStorageResponse(status); return Either.right(componentsUtils.getResponseFormat(actionStatus)); } - * - * deletedArtifacts.add(removeArifactFromGraph.left().value()); - */ - - } else { - Either, ResponseFormat> handleDelete = artifactsBusinessLogic.handleDelete(resourceId, artifact.getUniqueId(), user, AuditingActionEnum.ARTIFACT_DELETE, ComponentTypeEnum.RESOURCE, resource, - null, null, shouldLock, inTransaction); - if (handleDelete.isRight()) { - return Either.right(handleDelete.right().value()); - } - - deletedArtifacts.add(handleDelete.left().value().left().value()); - } - - } - } - if (!groupToDelete.isEmpty()) { - log.debug("try to delete group"); - List groupDefinitionstoDelete = new ArrayList<>(); - List groups = resource.getGroups(); - for (Entry> deleteGroup : groupToDelete.entrySet()) { - Optional op = groups.stream().filter(gr -> gr.getUniqueId().equals(deleteGroup.getKey())).findAny(); - if (op.isPresent()) { - groupDefinitionstoDelete.add(op.get()); - } - - } - if (!groupDefinitionstoDelete.isEmpty()) { - Either, ResponseFormat> prepareGroups = groupBusinessLogic.deleteGroups(resource, user, ComponentTypeEnum.RESOURCE, groupDefinitionstoDelete); - if (prepareGroups.isRight()) { - return Either.right(prepareGroups.right().value()); - } - } - } - return Either.left(deletedArtifacts); - } - - private Either associateAndDissociateArtifactsToGroup(CsarInfo csarInfo, Resource resource, List createdNewArtifacts, int labelCounter, boolean shouldLock, - boolean inTransaction, List createdDeplymentArtifactsAfterDelete, Map mergedgroup, List deletedArtifacts) { - Map> artifactsToAssotiate = new HashMap>(); - Map>> artifactsToUpdateMap = new HashMap>>(); - Either resEither = Either.left(resource); - for (Entry entry : mergedgroup.entrySet()) { - List dissArtifactsInGroup = entry.getValue().getListToDissotiateArtifactFromGroup(deletedArtifacts); - GroupDefinition grDef = entry.getKey(); - if (dissArtifactsInGroup != null && !dissArtifactsInGroup.isEmpty()) { - for (ArtifactDefinition art : dissArtifactsInGroup) { - grDef.getArtifacts().remove(art.getUniqueId()); - grDef.getArtifactsUuid().remove(art.getArtifactUUID()); - } - } - - List newArtifactsInGroup = entry.getValue().getListToAssociateArtifactToGroup(); - if (newArtifactsInGroup != null && !newArtifactsInGroup.isEmpty()) - artifactsToAssotiate.put(entry.getKey(), newArtifactsInGroup); - - List> artifactsToUpdate = entry.getValue().getListToUpdateArtifactInGroup(); - if (artifactsToUpdate != null && !artifactsToUpdate.isEmpty()) - artifactsToUpdateMap.put(entry.getKey(), artifactsToUpdate); - } - - if (!artifactsToUpdateMap.isEmpty()) { - List updatedArtifacts = new ArrayList(); - for (Entry>> artifactsToUpdateEntry : artifactsToUpdateMap.entrySet()) { - List> artifactsToUpdateList = artifactsToUpdateEntry.getValue(); - GroupDefinition groupToUpdate = artifactsToUpdateEntry.getKey(); - - for (ImmutablePair artifact : artifactsToUpdateList) { - String prevUUID = artifact.getKey().getArtifactUUID(); - String prevId = artifact.getKey().getUniqueId(); - Either updateArtifactEither = updateDeploymentArtifactsFromCsar(csarInfo, resource, artifact.getKey(), artifact.getValue(), updatedArtifacts, - artifact.getRight().getRelatedArtifactsInfo(), shouldLock, inTransaction); - if (updateArtifactEither.isRight()) { - log.debug("failed to update artifacts. status is {}", updateArtifactEither.right().value()); - resEither = Either.right(updateArtifactEither.right().value()); - return resEither; - } - ArtifactDefinition artAfterUpdate = updateArtifactEither.left().value(); - if (!prevUUID.equals(artAfterUpdate.getArtifactUUID()) || !prevId.equals(artAfterUpdate.getUniqueId())) { - groupToUpdate.getArtifacts().remove(prevId); - groupToUpdate.getArtifactsUuid().remove(prevUUID); - groupToUpdate.getArtifacts().add(artAfterUpdate.getUniqueId()); - groupToUpdate.getArtifactsUuid().add(artAfterUpdate.getArtifactUUID()); - } - } - } - } - - for (Entry> associateEntry : artifactsToAssotiate.entrySet()) { - List associatedArtifact = associateEntry.getValue(); - Set arifactsUids = new HashSet(); - Set arifactsUuids = new HashSet(); - for (ArtifactTemplateInfo artifactTemplate : associatedArtifact) { // try - // to - // find - // artifact - // in - // resource - boolean isCreate = true; - for (ArtifactDefinition createdArtifact : createdDeplymentArtifactsAfterDelete) { - if (artifactTemplate.getFileName().equalsIgnoreCase(createdArtifact.getArtifactName())) { - arifactsUids.add(createdArtifact.getUniqueId()); - arifactsUuids.add(createdArtifact.getArtifactUUID()); - isCreate = false; - String heatEnvId = checkAndGetHeatEnvId(createdArtifact); - if (!heatEnvId.isEmpty()) { - arifactsUids.add(heatEnvId); - Optional op = createdDeplymentArtifactsAfterDelete.stream().filter(p -> p.getUniqueId().equals(heatEnvId)).findAny(); - if (op.isPresent()) { - this.artifactToscaOperation.updateHeatEnvPlaceholder(op.get(), resource.getUniqueId(), resource.getComponentType().getNodeType()); - - } - } - - break; - } - - } - if (isCreate) { // check if already created - for (ArtifactDefinition createdNewArtifact : createdNewArtifacts) { - if (artifactTemplate.getFileName().equalsIgnoreCase(createdNewArtifact.getArtifactName())) { - arifactsUids.add(createdNewArtifact.getUniqueId()); - arifactsUuids.add(createdNewArtifact.getArtifactUUID()); - isCreate = false; - String heatEnvId = checkAndGetHeatEnvId(createdNewArtifact); - if (!heatEnvId.isEmpty()) { - arifactsUids.add(heatEnvId); - } - break; - } - } - } - - if (isCreate) { - Either createArtifactEither = createDeploymentArtifact(csarInfo, resource, ARTIFACTS_PATH, artifactTemplate, createdNewArtifacts, labelCounter, shouldLock, inTransaction); - if (createArtifactEither.isRight()) { - resEither = Either.right(createArtifactEither.right().value()); - return resEither; - } - ArtifactDefinition createdArtifact = createArtifactEither.left().value(); - arifactsUids.add(createdArtifact.getUniqueId()); - arifactsUuids.add(createdArtifact.getArtifactUUID()); - ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(createdArtifact.getArtifactType()); - if (artifactType == ArtifactTypeEnum.HEAT || artifactType == ArtifactTypeEnum.HEAT_NET || artifactType == ArtifactTypeEnum.HEAT_VOL) { - Either createHeatEnvPlaceHolder = artifactsBusinessLogic.createHeatEnvPlaceHolder(createdArtifact, ArtifactsBusinessLogic.HEAT_VF_ENV_NAME, resource.getUniqueId(), NodeTypeEnum.Resource, - resource.getName(), csarInfo.getModifier(), resource, null); - if (createHeatEnvPlaceHolder.isRight()) { - return Either.right(createHeatEnvPlaceHolder.right().value()); - } - String heatEnvId = createHeatEnvPlaceHolder.left().value().getUniqueId(); - arifactsUids.add(heatEnvId); - } - } - - } - if (arifactsUids.size() > 0) { - List artifactsToAssociate = new ArrayList(); - artifactsToAssociate.addAll(arifactsUids); - GroupDefinition assotiateGroup = associateEntry.getKey(); - assotiateGroup.getArtifacts().addAll(arifactsUids); - assotiateGroup.getArtifactsUuid().addAll(arifactsUuids); - } - } - - /* - * if (!associateArtifactGroup.isEmpty()) { - * - * log.debug("Try to associate artifacts to groups."); - * - * Either, ResponseFormat> assotiateGroupEither = groupBusinessLogic.associateArtifactsToGroup(resource.getUniqueId(), user.getUserId(), ComponentTypeEnum.RESOURCE, associateArtifactGroup, shouldLock, inTransaction); if - * (assotiateGroupEither.isRight()) { log.debug("Failed to associate artifacts to groups. Status is {} ", assotiateGroupEither.right().value()); resEither = Either.right(assotiateGroupEither.right().value()); return resEither; - * - * } } - */ - - ComponentParametersView parametersView = new ComponentParametersView(); - parametersView.disableAll(); - parametersView.setIgnoreComponentInstances(false); - parametersView.setIgnoreUsers(false); - parametersView.setIgnoreArtifacts(false); - parametersView.setIgnoreGroups(false); - - Either eitherGerResource = toscaOperationFacade.getToscaElement(resource.getUniqueId(), parametersView); - - if (eitherGerResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource); - - resEither = Either.right(responseFormat); - return resEither; - - } - resEither = Either.left(eitherGerResource.left().value()); - return resEither; - } - - private Map mergeGroupInUpdateFlow(Map>> groupArtifact, Map> parsedGroup, - Set artifactsToDelete, Map> groupToDelete, Set jsonMasterArtifacts, List createdDeplymentArtifacts) { - Map mergedgroup = new HashMap(); - for (Entry>> groupListEntry : groupArtifact.entrySet()) { - Map> createdArtifactMap = groupListEntry.getValue(); - boolean isNeedToDeleteGroup = true; - List listToDelete = null; - for (ArtifactDefinition maserArtifact : createdArtifactMap.keySet()) { - listToDelete = createdArtifactMap.get(maserArtifact); - for (ArtifactDefinition artToDelete : listToDelete) { - findArtifactToDelete(parsedGroup, artifactsToDelete, groupListEntry.getKey().getUniqueId(), artToDelete, createdDeplymentArtifacts); - } - if (artifactsToDelete != null && !artifactsToDelete.isEmpty()) { - GroupDefinition group = groupListEntry.getKey(); - for (ArtifactDefinition artifactDefinition : artifactsToDelete) { - if (CollectionUtils.isNotEmpty(group.getArtifacts()) && group.getArtifacts().contains(artifactDefinition.getUniqueId())) { - group.getArtifacts().remove(artifactDefinition.getUniqueId()); - - } - if (CollectionUtils.isNotEmpty(group.getArtifactsUuid()) && group.getArtifactsUuid().contains(artifactDefinition.getArtifactUUID())) { - group.getArtifactsUuid().remove(artifactDefinition.getArtifactUUID()); - - } - } - - } - - for (ArtifactTemplateInfo jsonMasterArtifact : jsonMasterArtifacts) { - if (maserArtifact.getArtifactName().equalsIgnoreCase(jsonMasterArtifact.getFileName())) { - MergedArtifactInfo mergedGroup = new MergedArtifactInfo(); - mergedGroup.setJsonArtifactTemplate(jsonMasterArtifact); - mergedGroup.setCreatedArtifact(createdArtifactMap.get(maserArtifact)); - mergedgroup.put(groupListEntry.getKey(), mergedGroup); - isNeedToDeleteGroup = false; - - } - } - - } - if (isNeedToDeleteGroup) { - groupToDelete.put(groupListEntry.getKey().getUniqueId(), listToDelete); - } - - } - return mergedgroup; - } - - private void findArtifactToDelete(Map> parsedGroup, Set artifactsToDelete, String deleteGroupId, ArtifactDefinition artifact, - List createdDeplymentArtifacts) { - boolean isNeedToDeleteArtifact = true; - String artifactType = artifact.getArtifactType(); - ArtifactDefinition generatedFromArt = null; - if (artifact.getGeneratedFromId() != null && !artifact.getGeneratedFromId().isEmpty()) { - Optional op = createdDeplymentArtifacts.stream().filter(p -> p.getUniqueId().equals(artifact.getGeneratedFromId())).findAny(); - if (op.isPresent()) - generatedFromArt = op.get(); - - } - - for (Entry> parsedGroupSetEntry : parsedGroup.entrySet()) { - Set artifactsNames = parsedGroupSetEntry.getValue(); - for (ArtifactTemplateInfo template : artifactsNames) { - if (artifact.getArtifactName().equalsIgnoreCase(template.getFileName()) && artifactType.equalsIgnoreCase(template.getType())) { - isNeedToDeleteArtifact = false; - break; - - } else { - if (generatedFromArt != null) { - if (generatedFromArt.getArtifactName().equalsIgnoreCase(template.getFileName()) && generatedFromArt.getArtifactType().equalsIgnoreCase(template.getType())) { - isNeedToDeleteArtifact = false; - break; - } - } - } - } - - } - if (isNeedToDeleteArtifact) { - artifactsToDelete.add(artifact); - - } - } - - private Map>> findMasterArtifactInGroup(List groups, Map deplymentArtifact) { - Map>> groupArtifact = new HashMap>>(); - - for (GroupDefinition group : groups) { - Map> gupsMap = new HashMap>(); - List artifacts = new ArrayList(); - List artifactsList = group.getArtifacts(); - if (artifactsList != null && !artifactsList.isEmpty()) { - - ArtifactDefinition masterArtifact = ArtifactUtils.findMasterArtifact(deplymentArtifact, artifacts, artifactsList); - if (masterArtifact != null) - gupsMap.put(masterArtifact, artifacts); - groupArtifact.put(group, gupsMap); - - } - } - return groupArtifact; - } - - private void createArtifactsGroupSet(List parsedGroupTemplateList, Set parsedArtifactsName) { - - for (ArtifactTemplateInfo parsedGroupTemplate : parsedGroupTemplateList) { - parsedArtifactsName.add(parsedGroupTemplate); - List relatedArtifacts = parsedGroupTemplate.getRelatedArtifactsInfo(); - if (relatedArtifacts != null && !relatedArtifacts.isEmpty()) { - createArtifactsGroupSet(relatedArtifacts, parsedArtifactsName); - } - } - } - - public Either createResourceArtifactsFromCsar(CsarInfo csarInfo, Resource resource, String artifactsMetaFile, String artifactsMetaFileName, List createdArtifacts, - boolean shouldLock, boolean inTransaction) { - - log.debug("parseResourceArtifactsInfoFromFile start"); - Either>, ResponseFormat> parseResourceInfoFromYamlEither = parseResourceArtifactsInfoFromFile(resource, artifactsMetaFile, artifactsMetaFileName, csarInfo.getModifier()); - if (parseResourceInfoFromYamlEither.isRight()) { - ResponseFormat responseFormat = parseResourceInfoFromYamlEither.right().value(); - componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, "", "", AuditingActionEnum.IMPORT_RESOURCE, null); - return Either.right(responseFormat); - } - log.debug("parseResourceArtifactsInfoFromFile end"); - - log.debug("createResourceArtifacts start"); - Either respStatus = createResourceArtifacts(csarInfo, resource, parseResourceInfoFromYamlEither.left().value(), AuditingActionEnum.CREATE_RESOURCE, createdArtifacts, shouldLock, inTransaction); - if (respStatus.isRight()) { - return respStatus; - } - log.debug("createResourceArtifacts end"); - log.debug("getResource start"); - Either eitherGerResource = toscaOperationFacade.getToscaElement(resource.getUniqueId()); - log.debug("getResource end"); - if (eitherGerResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource); - - return Either.right(responseFormat); - - } - return Either.left(eitherGerResource.left().value()); - - } - - private Either createGroupDeploymentArtifactsFromCsar(CsarInfo csarInfo, Resource resource, List artifactsTemplateList, - List createdNewArtifacts, List artifactsFromResource, int labelCounter, boolean shouldLock, boolean inTransaction) { - Either resStatus = Either.left(resource); - List createdGroups = resource.getGroups(); - List heatGroups = null; - if (createdGroups != null && !createdGroups.isEmpty()) { - heatGroups = createdGroups.stream().filter(e -> e.getMembers() != null).collect(Collectors.toList()); - } - - List needToAdd = new ArrayList<>(); - for (ArtifactTemplateInfo groupTemplateInfo : artifactsTemplateList) { - String groupName = groupTemplateInfo.getGroupName(); - Set artifactsGroup = new HashSet(); - Set artifactsUUIDGroup = new HashSet(); - - resStatus = createDeploymentArtifactsFromCsar(csarInfo, resource, artifactsGroup, artifactsUUIDGroup, groupTemplateInfo, createdNewArtifacts, artifactsFromResource, labelCounter, shouldLock, inTransaction); - if (resStatus.isRight()) - return resStatus; - - Map members = new HashMap(); - associateMembersToArtifacts(createdNewArtifacts, artifactsFromResource, heatGroups, artifactsGroup, members); - - List artifactsList = new ArrayList(artifactsGroup); - List artifactsUUIDList = new ArrayList(artifactsUUIDGroup); - - GroupDefinition groupDefinition = new GroupDefinition(); - groupDefinition.setName(groupName); - groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE); - groupDefinition.setArtifacts(artifactsList); - groupDefinition.setArtifactsUuid(artifactsUUIDList); - - if (!members.isEmpty()) - groupDefinition.setMembers(members); - - List properties = new ArrayList(); - GroupProperty prop = new GroupProperty(); - prop.setName(Constants.IS_BASE); - prop.setValue(Boolean.toString(groupTemplateInfo.isBase())); - properties.add(prop); - - List createdArtifacts = new ArrayList<>(); - createdArtifacts.addAll(createdNewArtifacts); - createdArtifacts.addAll(artifactsFromResource); - Either getLatestGroupTypeRes = groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, true); - if (getLatestGroupTypeRes.isRight()) { - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getLatestGroupTypeRes.right().value()))); - } - properties = createVfModuleAdditionalProperties(groupTemplateInfo.isBase(), groupName, properties, createdArtifacts, artifactsList, getLatestGroupTypeRes.left().value()); - groupDefinition.convertFromGroupProperties(properties); - - // Either createGroup = groupBusinessLogic.createGroup(resource.getUniqueId(), user.getUserId(), ComponentTypeEnum.RESOURCE, groupDefinition, inTransaction); - // if (createGroup.isRight()) - // return Either.right(createGroup.right().value()); - needToAdd.add(groupDefinition); - } - ComponentParametersView componentParametersView = new ComponentParametersView(); - componentParametersView.disableAll(); - componentParametersView.setIgnoreArtifacts(false); - componentParametersView.setIgnoreGroups(false); - componentParametersView.setIgnoreComponentInstances(false); - - Either component = toscaOperationFacade.getToscaElement(resource.getUniqueId(), componentParametersView); - if (component.isRight()) { - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } - resource = component.left().value(); - - Either, ResponseFormat> addGroups = groupBusinessLogic.addGroups(resource, csarInfo.getModifier(), ComponentTypeEnum.RESOURCE, needToAdd); - if (addGroups.isRight()) - return Either.right(addGroups.right().value()); - - return resStatus; - } - - private Either createDeploymentArtifactsFromCsar(CsarInfo csarInfo, Resource resource, Set artifactsGroup, Set artifactsUUIDGroup, - ArtifactTemplateInfo artifactTemplateInfo, List createdArtifacts, List artifactsFromResource, int labelCounter, boolean shoudLock, boolean inTransaction) { - Either resStatus = Either.left(resource); - String artifactFileName = artifactTemplateInfo.getFileName(); - String artifactUid = ""; - String artifactUUID = ""; - String artifactEnvUid = ""; - boolean alreadyExist = false; - - // check if artifacts already exist - if (artifactsFromResource != null && !artifactsFromResource.isEmpty()) { - for (ArtifactDefinition artifactFromResource : artifactsFromResource) { - if (artifactFromResource.getArtifactName().equals(artifactFileName)) { - artifactUid = artifactFromResource.getUniqueId(); - artifactUUID = artifactFromResource.getArtifactUUID(); - if (!artifactFromResource.getArtifactType().equalsIgnoreCase(artifactTemplateInfo.getType())) { - log.debug("Artifact with name {} and type {} already exist with type {}", artifactFileName, artifactTemplateInfo.getType(), artifactFromResource.getArtifactType()); - BeEcompErrorManager.getInstance().logInternalDataError("Artifact file is not in expected formatr, fileName " + artifactFileName, "Artifact internals are invalid", ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_ALRADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, artifactFileName, artifactTemplateInfo.getType(), artifactFromResource.getArtifactType())); - } - alreadyExist = true; - artifactEnvUid = checkAndGetHeatEnvId(artifactFromResource); - break; - } - - } - - } - if (!alreadyExist) { - for (ArtifactDefinition createdArtifact : createdArtifacts) { - if (createdArtifact.getArtifactName().equals(artifactFileName)) { - artifactUid = createdArtifact.getUniqueId(); - artifactUUID = createdArtifact.getArtifactUUID(); - - if (!createdArtifact.getArtifactType().equalsIgnoreCase(artifactTemplateInfo.getType())) { - log.debug("Artifact with name {} and type {} already exist with type {}", artifactFileName, artifactTemplateInfo.getType(), createdArtifact.getArtifactType()); - BeEcompErrorManager.getInstance().logInternalDataError("Artifact file is not in expected formatr, fileName " + artifactFileName, "Artifact internals are invalid", ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_ALRADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, artifactFileName, artifactTemplateInfo.getType(), createdArtifact.getArtifactType())); - } - alreadyExist = true; - artifactEnvUid = checkAndGetHeatEnvId(createdArtifact); - break; - } - - } - } - // if not exist need to create - if (!alreadyExist) { - - Either newArtifactEither = createDeploymentArtifact(csarInfo, resource, ARTIFACTS_PATH, artifactTemplateInfo, createdArtifacts, labelCounter, shoudLock, inTransaction); - if (newArtifactEither.isRight()) { - resStatus = Either.right(newArtifactEither.right().value()); - return resStatus; - } - ArtifactDefinition newArtifact = newArtifactEither.left().value(); - artifactUid = newArtifact.getUniqueId(); - artifactUUID = newArtifact.getArtifactUUID(); - ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(newArtifact.getArtifactType()); - if (artifactType == ArtifactTypeEnum.HEAT || artifactType == ArtifactTypeEnum.HEAT_NET || artifactType == ArtifactTypeEnum.HEAT_VOL) { - Either createHeatEnvPlaceHolder = artifactsBusinessLogic.createHeatEnvPlaceHolder(newArtifact, ArtifactsBusinessLogic.HEAT_VF_ENV_NAME, resource.getUniqueId(), NodeTypeEnum.Resource, - resource.getName(), csarInfo.getModifier(), resource, null); - if (createHeatEnvPlaceHolder.isRight()) { - return Either.right(createHeatEnvPlaceHolder.right().value()); - } - artifactEnvUid = createHeatEnvPlaceHolder.left().value().getUniqueId(); - } - } - - artifactsGroup.add(artifactUid); - artifactsUUIDGroup.add(artifactUUID); - if (!artifactEnvUid.isEmpty()) { - artifactsGroup.add(artifactEnvUid); - } - - List relatedArtifacts = artifactTemplateInfo.getRelatedArtifactsInfo(); - if (relatedArtifacts != null) { - for (ArtifactTemplateInfo relatedArtifactTemplateInfo : relatedArtifacts) { - resStatus = createDeploymentArtifactsFromCsar(csarInfo, resource, artifactsGroup, artifactsUUIDGroup, relatedArtifactTemplateInfo, createdArtifacts, artifactsFromResource, labelCounter, shoudLock, inTransaction); - if (resStatus.isRight()) - return resStatus; - } - } - return resStatus; - } - - private Either createResourceArtifacts(CsarInfo csarInfo, Resource resource, Map> artifactsMap, AuditingActionEnum createResource, - List createdArtifacts, boolean shouldLock, boolean inTransaction) { - - Either resStatus = Either.left(resource); - - Collection> arifactsCollection = artifactsMap.values(); - - for (List groupTemplateList : arifactsCollection) { - if (groupTemplateList != null) { - resStatus = createGroupDeploymentArtifactsFromCsar(csarInfo, resource, groupTemplateList, createdArtifacts, 0, shouldLock, inTransaction); - if (resStatus.isRight()) - return resStatus; - } - } - - return resStatus; - - } - - private Either createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Resource resource, List createdArtifacts, boolean shouldLock, boolean inTransaction, ArtifactOperationInfo artifactOperation) { - - Either resStatus = null; - Map>> collectedWarningMessages = new HashMap<>(); - - try { - List artifactPathAndNameList = - // Stream of file paths contained in csar - csarInfo.getCsar().entrySet().stream() - // Filter in only VF artifact path location - .filter(e -> Pattern.compile(VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()).matches()) - // Validate and add warnings - .map(e -> CsarUtils.validateNonMetaArtifact(e.getKey(), e.getValue(), collectedWarningMessages)) - // Filter in Non Warnings - .filter(e -> e.isLeft()) - // Convert from Either to NonMetaArtifactInfo - .map(e -> e.left().value()) - // collect to List - .collect(Collectors.toList()); - - Either responseFormatEither = validateArtifactNames(artifactPathAndNameList); - if (responseFormatEither.isRight()) { - return Either.right(getComponentsUtils().getResponseFormatByArtifactId(ActionStatus.ARTIFACT_NAME_INVALID, responseFormatEither.right().value())); - } - - - EnumMap> vfCsarArtifactsToHandle = null; - - if (artifactOperation.getArtifactOperationEnum() == ArtifactOperationEnum.Create) { - vfCsarArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class); - vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList); - } else { - Either>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle(resource, artifactPathAndNameList, csarInfo.getModifier()); - - if (findVfCsarArtifactsToHandleRes.isRight()) { - resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value()); - } - if (resStatus == null) { - vfCsarArtifactsToHandle = findVfCsarArtifactsToHandleRes.left().value(); - } - } - if (resStatus == null && vfCsarArtifactsToHandle != null) { - for (Entry> currArtifactOperationPair : vfCsarArtifactsToHandle.entrySet()) { - - Optional optionalCreateInDBError = - // Stream of artifacts to be created - currArtifactOperationPair.getValue().stream() - // create each artifact - .map(e -> createOrUpdateSingleNonMetaArtifact(resource, csarInfo, e.getPath(), e.getArtifactName(), e.getArtifactType().getType(), e.getArtifactGroupType(), e.getArtifactLabel(), e.getDisplayName(), - CsarUtils.ARTIFACT_CREATED_FROM_CSAR, e.getArtifactUniqueId(), artifactsBusinessLogic.new ArtifactOperationInfo(false, false, currArtifactOperationPair.getKey()), createdArtifacts, shouldLock, - inTransaction)) - // filter in only error - .filter(e -> e.isRight()). - // Convert the error from either to ResponseFormat - map(e -> e.right().value()). - // Check if an error occurred - findAny(); - // Error found on artifact Creation - if (optionalCreateInDBError.isPresent()) { - resStatus = Either.right(optionalCreateInDBError.get()); - break; - } - } - } - 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; - } - - private Either validateArtifactNames(List artifactPathAndNameList) { - Pattern englishNumbersAndUnderScoresOnly = Pattern.compile(CsarUtils.VALID_ENGLISH_ARTIFACT_NAME); - for (NonMetaArtifactInfo nonMetaArtifactInfo : artifactPathAndNameList) { - if (!englishNumbersAndUnderScoresOnly.matcher(nonMetaArtifactInfo.getDisplayName()).matches()) { - return Either.right(nonMetaArtifactInfo.getArtifactName()); - } - } - return Either.left(true); - } - - - private Either>, ResponseFormat> findVfCsarArtifactsToHandle(Resource resource, List artifactPathAndNameList, User user) { - - List existingArtifacts = new ArrayList<>(); - // collect all Deployment and Informational artifacts of VF - 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 MANDATORY artifacts, LICENSE artifacts and artifacts was created from HEAT.meta - .filter(this::isNonMetaArtifact).collect(Collectors.toList()); - - List artifactsToIgnore = new ArrayList<>(); - // collect IDs of Artifacts of VF which belongs to any group - if (resource.getGroups() != null) { - resource.getGroups().stream().forEach(g -> { - if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) - artifactsToIgnore.addAll(g.getArtifacts()); - }); - } - existingArtifacts = existingArtifacts.stream() - // filter artifacts which belongs to any group - .filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(Collectors.toList()); - return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, resource, user); - } - - private 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()) == ArtifactTypeEnum.VENDOR_LICENSE || ArtifactTypeEnum.findType(artifact.getArtifactType()) == ArtifactTypeEnum.VF_LICENSE) { - result = false; - } - return result; - } - - private Either createGroupDeploymentArtifactsFromCsar(CsarInfo csarInfo, Resource resource, List artifactsTemplateList, List createdArtifacts, - int labelCounter, boolean shouldLock, boolean inTransaction) { - Either resStatus = Either.left(resource); - List createdGroups = resource.getGroups(); - List heatGroups = null; - if (createdGroups != null && !createdGroups.isEmpty()) { - - // List collect = resources.stream().flatMap( e -> - // e.getArtifacts().stream()).filter(p -> - // relevantArtifactTypes.contains(p.getArtifactType() - // )).collect(Collectors.toList()); - // List heatGroups = createdGroups.stream().filter( - // e -> e.getProperties().stream().filter(p -> - // p.getName().contains(Constants.HEAT_FILE_PROPS))).collect(Collectors.toList()); - heatGroups = createdGroups.stream().filter(e -> e.getMembers() != null).collect(Collectors.toList()); - ; - } - List needToCreate = new ArrayList<>(); - for (ArtifactTemplateInfo groupTemplateInfo : artifactsTemplateList) { - String groupName = groupTemplateInfo.getGroupName(); - Set artifactsGroup = new HashSet(); - Set artifactsUUIDGroup = new HashSet(); - - log.debug("createDeploymentArtifactsFromCsar start"); - resStatus = createDeploymentArtifactFromCsar(csarInfo, ARTIFACTS_PATH, resource, artifactsGroup, artifactsUUIDGroup, groupTemplateInfo, createdArtifacts, labelCounter, shouldLock, inTransaction); - log.debug("createDeploymentArtifactsFromCsar end"); - if (resStatus.isRight()) - return resStatus; - - Map members = new HashMap(); - associateMembersToArtifacts(createdArtifacts, null, heatGroups, artifactsGroup, members); - - List artifactsList = new ArrayList(artifactsGroup); - List artifactsUUIDList = new ArrayList(artifactsUUIDGroup); - - GroupDefinition groupDefinition = new GroupDefinition(); - groupDefinition.setName(groupName); - groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE); - groupDefinition.setArtifacts(artifactsList); - groupDefinition.setArtifactsUuid(artifactsUUIDList); - - if (!members.isEmpty()) - groupDefinition.setMembers(members); - List properties = new ArrayList(); - GroupProperty prop = new GroupProperty(); - prop.setName(Constants.IS_BASE); - prop.setValue(Boolean.toString(groupTemplateInfo.isBase())); - properties.add(prop); - Either getLatestGroupTypeRes = groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, true); - if (getLatestGroupTypeRes.isRight()) { - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getLatestGroupTypeRes.right().value()))); - } - properties = createVfModuleAdditionalProperties(groupTemplateInfo.isBase(), groupName, properties, createdArtifacts, artifactsList, getLatestGroupTypeRes.left().value()); - groupDefinition.convertFromGroupProperties(properties); - log.debug("createGroup start"); - - // Since in these groups we handle only artifacts, then no need to - // fetch component instances - - // Either createGroup = groupBusinessLogic.createGroup(comp, user, ComponentTypeEnum.RESOURCE, groupDefinition, inTransaction); - // log.debug("createGroup end"); - // if (createGroup.isRight()) - // return Either.right(createGroup.right().value()); - needToCreate.add(groupDefinition); - } - - ComponentParametersView componentParametersView = new ComponentParametersView(); - componentParametersView.disableAll(); - componentParametersView.setIgnoreUsers(false); - componentParametersView.setIgnoreArtifacts(false); - componentParametersView.setIgnoreGroups(false); - - componentParametersView.setIgnoreComponentInstances(false); - - Either component = toscaOperationFacade.getToscaElement(resource.getUniqueId(), componentParametersView); - if (component.isRight()) { - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } - - Either, ResponseFormat> createGroups = groupBusinessLogic.addGroups(component.left().value(), csarInfo.getModifier(), ComponentTypeEnum.RESOURCE, needToCreate); - if (createGroups.isRight()) { - return Either.right(createGroups.right().value()); - } - - return resStatus; - } - - public List createVfModuleAdditionalProperties(boolean isBase, String moduleName, List properties, List deploymentArtifacts, List artifactsInGroup, GroupTypeDefinition groupType) { - Map vfModuleProperties = ConfigurationManager.getConfigurationManager().getConfiguration().getVfModuleProperties(); - vfModuleProperties.entrySet().forEach(p -> { - GroupProperty prop = new GroupProperty(); - prop.setName(p.getKey()); - if (isBase) { - prop.setValue(p.getValue().getForBaseModule()); - prop.setDefaultValue(p.getValue().getForBaseModule()); - } else { - prop.setValue(p.getValue().getForNonBaseModule()); - prop.setDefaultValue(p.getValue().getForNonBaseModule()); - } - properties.add(prop); - - }); - GroupProperty proplabel = new GroupProperty(); - proplabel.setName("vf_module_label"); - - Matcher matcher = pattern.matcher(moduleName); - - if (matcher.find()) { - proplabel.setValue(matcher.group(1)); - proplabel.setDefaultValue(matcher.group(1)); - } else { - proplabel.setValue(moduleName); - proplabel.setDefaultValue(moduleName); - } - properties.add(proplabel); - - GroupProperty propvolume = new GroupProperty(); - propvolume.setName("volume_group"); - boolean isVolume = false; - for (String artifactId : artifactsInGroup) { - ArtifactDefinition artifactDef = null; - artifactDef = findArtifactInList(deploymentArtifacts, artifactId); - if (artifactDef != null && artifactDef.getArtifactType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType())) { - isVolume = true; - break; - } - } - propvolume.setValue(String.valueOf(isVolume)); - propvolume.setDefaultValue(String.valueOf(isVolume)); - properties.add(propvolume); - mergeWithGroupTypeProperties(properties, groupType.getProperties()); - return properties; - } - - private void mergeWithGroupTypeProperties(List properties, List groupTypeProperties) { - - Map propertiesMap = properties.stream().collect(Collectors.toMap(p -> p.getName(), p -> p)); - for (PropertyDefinition groupTypeProperty : groupTypeProperties) { - if (!propertiesMap.containsKey(groupTypeProperty.getName())) { - properties.add(new GroupProperty(groupTypeProperty)); - } - } - } - - private ArtifactDefinition findArtifactInList(List createdArtifacts, String artifactId) { - for (ArtifactDefinition artifact : createdArtifacts) { - if (artifact.getUniqueId().equals(artifactId)) { - return artifact; - } - } - return null; - } - - private void associateMembersToArtifacts(List createdArtifacts, List artifactsFromResource, List heatGroups, Set artifactsGroup, Map members) { - if (heatGroups != null && !heatGroups.isEmpty()) { - for (GroupDefinition heatGroup : heatGroups) { - List grpoupProps = heatGroup.convertToGroupProperties(); - if (grpoupProps != null) { - Optional op = grpoupProps.stream().filter(p -> p.getName().equals(Constants.HEAT_FILE_PROPS)).findAny(); - if (op.isPresent()) { - GroupProperty prop = op.get(); - String heatFileNAme = prop.getValue(); - if (null == heatFileNAme || heatFileNAme.isEmpty()) - continue; - List artifacts = new ArrayList(); - for (String artifactId : artifactsGroup) { - Optional opArt = createdArtifacts.stream().filter(p -> p.getUniqueId().equals(artifactId)).findAny(); - if (opArt.isPresent()) { - artifacts.add(opArt.get()); - } - if (artifactsFromResource != null) { - opArt = artifactsFromResource.stream().filter(p -> p.getUniqueId().equals(artifactId)).findAny(); - if (opArt.isPresent()) { - artifacts.add(opArt.get()); - } - } - } - Optional resOp = artifacts.stream().filter(p -> heatFileNAme.contains(p.getArtifactName())).findAny(); - if (resOp.isPresent()) { - members.putAll(heatGroup.getMembers()); - } - } - } - } - - } - } - - private Either createDeploymentArtifactFromCsar(CsarInfo csarInfo, String artifactPath, Resource resource, Set artifactsGroup, Set artifactsUUIDGroup, - ArtifactTemplateInfo artifactTemplateInfo, List createdArtifacts, int labelCounter, boolean shoudLock, boolean inTransaction) { - Either resStatus = Either.left(resource); - String artifactFileName = artifactTemplateInfo.getFileName(); - String artifactUid = ""; - String artifactEnvUid = ""; - String artifactUUID = ""; - boolean alreadyExist = false; - - // check if artifacts already exist - for (ArtifactDefinition createdArtifact : createdArtifacts) { - if (createdArtifact.getArtifactName().equals(artifactFileName)) { - artifactUid = createdArtifact.getUniqueId(); - artifactUUID = createdArtifact.getArtifactUUID(); - if (!createdArtifact.getArtifactType().equalsIgnoreCase(artifactTemplateInfo.getType())) { - log.debug("Artifact with name {} and type {} already exist with type {}", artifactFileName, artifactTemplateInfo.getType(), createdArtifact.getArtifactType()); - BeEcompErrorManager.getInstance().logInternalDataError("Artifact file is not in expected formatr, fileName " + artifactFileName, "Artifact internals are invalid", ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_ALRADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, artifactFileName, artifactTemplateInfo.getType(), createdArtifact.getArtifactType())); - } - alreadyExist = true; - artifactEnvUid = checkAndGetHeatEnvId(createdArtifact); - break; - } - } - // if not exist need to create - if (!alreadyExist) { - - Either newArtifactEither = createDeploymentArtifact(csarInfo, resource, artifactPath, artifactTemplateInfo, createdArtifacts, labelCounter, shoudLock, inTransaction); - if (newArtifactEither.isRight()) { - resStatus = Either.right(newArtifactEither.right().value()); - return resStatus; - } - ArtifactDefinition newArtifact = newArtifactEither.left().value(); - artifactUid = newArtifact.getUniqueId(); - artifactUUID = newArtifact.getArtifactUUID(); - - ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(newArtifact.getArtifactType()); - if (artifactType == ArtifactTypeEnum.HEAT || artifactType == ArtifactTypeEnum.HEAT_NET || artifactType == ArtifactTypeEnum.HEAT_VOL) { - Either createHeatEnvPlaceHolder = artifactsBusinessLogic.createHeatEnvPlaceHolder(newArtifact, ArtifactsBusinessLogic.HEAT_VF_ENV_NAME, resource.getUniqueId(), NodeTypeEnum.Resource, - resource.getName(), csarInfo.getModifier(), resource, null); - if (createHeatEnvPlaceHolder.isRight()) { - return Either.right(createHeatEnvPlaceHolder.right().value()); - } - artifactEnvUid = createHeatEnvPlaceHolder.left().value().getUniqueId(); - } - } - - artifactsGroup.add(artifactUid); - artifactsUUIDGroup.add(artifactUUID); - if (!artifactEnvUid.isEmpty()) { - artifactsGroup.add(artifactEnvUid); - } - - List relatedArtifacts = artifactTemplateInfo.getRelatedArtifactsInfo(); - if (relatedArtifacts != null) { - for (ArtifactTemplateInfo relatedArtifactTemplateInfo : relatedArtifacts) { - resStatus = createDeploymentArtifactFromCsar(csarInfo, artifactPath, resource, artifactsGroup, artifactsUUIDGroup, relatedArtifactTemplateInfo, createdArtifacts, labelCounter, shoudLock, inTransaction); - if (resStatus.isRight()) - return resStatus; - } - } - return resStatus; - } - - private String checkAndGetHeatEnvId(ArtifactDefinition createdArtifact) { - String artifactEnvUid = ""; - ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(createdArtifact.getArtifactType()); - if (artifactType == ArtifactTypeEnum.HEAT || artifactType == ArtifactTypeEnum.HEAT_NET || artifactType == ArtifactTypeEnum.HEAT_VOL) { - artifactEnvUid = createdArtifact.getUniqueId() + ArtifactsBusinessLogic.HEAT_ENV_SUFFIX; - } - return artifactEnvUid; - } - - private Either createDeploymentArtifact(CsarInfo csarInfo, Resource resource, String artifactPath, ArtifactTemplateInfo artifactTemplateInfo, - List createdArtifacts, int labelCounter, boolean shoudLock, boolean inTransaction) { - final String artifactFileName = artifactTemplateInfo.getFileName(); - Either, ResponseFormat> artifactContententStatus = CsarValidationUtils.getArtifactsContent(csarInfo.getCsarUUID(), csarInfo.getCsar(), artifactPath + artifactFileName, artifactFileName, componentsUtils); - if (artifactContententStatus.isRight()) { - return Either.right(artifactContententStatus.right().value()); - } - labelCounter += createdArtifacts.size(); - - Map json = buildJsonForArtifact(artifactTemplateInfo, artifactContententStatus.left().value().getValue(), labelCounter); - - Either, ResponseFormat> uploadArtifactToService = createOrUpdateCsarArtifactFromJson(resource, csarInfo.getModifier(), json, artifactsBusinessLogic.new ArtifactOperationInfo(false, false, ArtifactOperationEnum.Create), - shoudLock, inTransaction); - - if (uploadArtifactToService.isRight()) - return Either.right(uploadArtifactToService.right().value()); - - ArtifactDefinition currentInfo = uploadArtifactToService.left().value().left().value(); - if (currentInfo.getHeatParameters() != null) { - - Either updateEnvEither = updateHeatParamsFromCsar(resource, csarInfo, artifactTemplateInfo, currentInfo, false); - if (updateEnvEither.isRight()) { - log.debug("failed to update parameters to artifact {}", artifactFileName); - return Either.right(updateEnvEither.right().value()); - - } - currentInfo = updateEnvEither.left().value(); - - } - - createdArtifacts.add(currentInfo); - - return Either.left(currentInfo); - - } - - private Either updateDeploymentArtifactsFromCsar(CsarInfo csarInfo, Resource resource, ArtifactDefinition oldArtifact, ArtifactTemplateInfo artifactTemplateInfo, - List updatedArtifacts, List updatedRequiredArtifacts, boolean shouldLock, boolean inTransaction) { - - Either resStatus = null; - String artifactFileName = artifactTemplateInfo.getFileName(); - - // check if artifacts already exist - for (ArtifactDefinition updatedArtifact : updatedArtifacts) { - if (updatedArtifact.getArtifactName().equals(artifactFileName)) { - if (!updatedArtifact.getArtifactType().equalsIgnoreCase(artifactTemplateInfo.getType())) { - log.debug("Artifact with name {} and type {} already updated with type {}", artifactFileName, artifactTemplateInfo.getType(), updatedArtifact.getArtifactType()); - BeEcompErrorManager.getInstance().logInternalDataError("Artifact file is not in expected formatr, fileName " + artifactFileName, "Artifact internals are invalid", ErrorSeverity.ERROR); - resStatus = Either.right(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_ALRADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, artifactFileName, artifactTemplateInfo.getType(), updatedArtifact.getArtifactType())); - return resStatus; - } - resStatus = Either.left(updatedArtifact); - return resStatus; - } - - } - - Either, ResponseFormat> artifactContententStatus = CsarValidationUtils.getArtifactsContent(csarInfo.getCsarUUID(), csarInfo.getCsar(), CsarUtils.ARTIFACTS_PATH + artifactFileName, artifactFileName, componentsUtils); - if (artifactContententStatus.isRight()) { - resStatus = Either.right(artifactContententStatus.right().value()); - return resStatus; - } - - Map json = buildJsonForUpdateArtifact(oldArtifact.getUniqueId(), artifactFileName, oldArtifact.getArtifactType(), ArtifactGroupTypeEnum.DEPLOYMENT, oldArtifact.getArtifactLabel(), oldArtifact.getArtifactDisplayName(), - oldArtifact.getDescription(), artifactContententStatus.left().value().getRight(), updatedRequiredArtifacts); - - Either, ResponseFormat> uploadArtifactToService = createOrUpdateCsarArtifactFromJson(resource, csarInfo.getModifier(), json, artifactsBusinessLogic.new ArtifactOperationInfo(false, false, ArtifactOperationEnum.Update), - shouldLock, inTransaction); - - if (uploadArtifactToService.isRight()) { - resStatus = Either.right(uploadArtifactToService.right().value()); - return resStatus; - } - ArtifactDefinition currentInfo = uploadArtifactToService.left().value().left().value(); - - Either updateEnvEither = updateHeatParamsFromCsar(resource, csarInfo, artifactTemplateInfo, currentInfo, true); - if (updateEnvEither.isRight()) { - log.debug("failed to update parameters to artifact {}", artifactFileName); - resStatus = Either.right(updateEnvEither.right().value()); - return resStatus; - } - // TODO evg update env time ( must be separate US for this!!!!) - - updatedArtifacts.add(updateEnvEither.left().value()); - resStatus = Either.left(updateEnvEither.left().value()); - - return resStatus; - - } - - private Either updateHeatParamsFromCsar(Resource resource, CsarInfo csarInfo, ArtifactTemplateInfo artifactTemplateInfo, ArtifactDefinition currentInfo, boolean isUpdateEnv) { - Either resStatus = Either.left(currentInfo); - if (artifactTemplateInfo.getEnv() != null && !artifactTemplateInfo.getEnv().isEmpty()) { - - Either, ResponseFormat> artifactparamsStatus = CsarValidationUtils.getArtifactsContent(csarInfo.getCsarUUID(), csarInfo.getCsar(), CsarUtils.ARTIFACTS_PATH + artifactTemplateInfo.getEnv(), artifactTemplateInfo.getEnv(), - componentsUtils); - if (artifactparamsStatus.isRight()) { - resStatus = Either.right(artifactparamsStatus.right().value()); - return resStatus; - } - Either, ResponseFormat> propsStatus = extractHeatParameters(ArtifactTypeEnum.HEAT_ENV.getType(), artifactTemplateInfo.getEnv(), artifactparamsStatus.left().value().getValue(), false); - /* - * if (propsStatus.isRight()) { - * - * resStatus = Either.right(propsStatus.right().value()); return resStatus; } - */ - if (propsStatus.isLeft()) { - List updatedHeatEnvParams = propsStatus.left().value(); - List currentHeatEnvParams = currentInfo.getListHeatParameters(); - // List newHeatEnvParams = new ArrayList(); - - if (updatedHeatEnvParams != null && !updatedHeatEnvParams.isEmpty() && currentHeatEnvParams != null && !currentHeatEnvParams.isEmpty()) { - - String paramName; - for (HeatParameterDefinition heatEnvParam : updatedHeatEnvParams) { - - paramName = heatEnvParam.getName(); - for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) { - if (paramName.equalsIgnoreCase(currHeatParam.getName())) { - - String updatedParamValue = heatEnvParam.getCurrentValue(); - if (updatedParamValue == null) - updatedParamValue = heatEnvParam.getDefaultValue(); - HeatParameterType paramType = HeatParameterType.isValidType(currHeatParam.getType()); - if (!paramType.getValidator().isValid(updatedParamValue, null)) { - ActionStatus status = ActionStatus.INVALID_HEAT_PARAMETER_VALUE; - ResponseFormat responseFormat = componentsUtils.getResponseFormat(status, ArtifactTypeEnum.HEAT_ENV.getType(), paramType.getType(), paramName); - resStatus = Either.right(responseFormat); - return resStatus; - } - currHeatParam.setCurrentValue(paramType.getConverter().convert(updatedParamValue, null, null)); - // newHeatEnvParams.add(currHeatParam); - break; - } - } - } - currentInfo.setListHeatParameters(currentHeatEnvParams); - Either updateArifactOnResource = artifactToscaOperation.updateArifactOnResource(currentInfo, resource.getUniqueId(), currentInfo.getUniqueId(), null, null); - if (updateArifactOnResource.isRight()) { - log.debug("Failed to update heat paratemers of heat on CSAR flow for component {} artifact {} label {}", resource.getUniqueId(), currentInfo.getUniqueId(), currentInfo.getArtifactLabel()); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateArifactOnResource.right().value()))); - } - resStatus = Either.left(updateArifactOnResource.left().value()); - } - } - } - if (isUpdateEnv) { - Map artifacts = resource.getDeploymentArtifacts(); - Optional op = artifacts.values().stream().filter(p -> p.getGeneratedFromId() != null && p.getGeneratedFromId().equals(currentInfo.getUniqueId())).findAny(); - if (op.isPresent()) { - ArtifactDefinition artifactInfoHeatEnv = op.get(); - Either updateArifactOnResource = artifactToscaOperation.updateArifactOnResource(artifactInfoHeatEnv, resource.getUniqueId(), artifactInfoHeatEnv.getUniqueId(), null, null); - if (updateArifactOnResource.isRight()) { - log.debug("Failed to update heat env on CSAR flow for component {} artifact {} label {}", resource.getUniqueId(), artifactInfoHeatEnv.getUniqueId(), artifactInfoHeatEnv.getArtifactLabel()); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateArifactOnResource.right().value()))); - } - } - } - return resStatus; - } - - private Either, ResponseFormat> extractHeatParameters(String artifactType, String fileName, byte[] content, boolean is64Encoded) { - // extract heat parameters - String heatDecodedPayload = is64Encoded ? new String(Base64.decodeBase64(content)) : new String(content); - Either, ResultStatusEnum> heatParameters = ImportUtils.getHeatParamsWithoutImplicitTypes(heatDecodedPayload, artifactType); - if (heatParameters.isRight()) { - log.debug("File {} is not in expected key-value form in csar ", fileName); - BeEcompErrorManager.getInstance().logInternalDataError("File " + fileName + " is not in expected key-value form in csar ", "CSAR internals are invalid", ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_DEPLOYMENT_ARTIFACT_HEAT, fileName)); - - } - return Either.left(heatParameters.left().value()); - - } - - private Map buildJsonForArtifact(ArtifactTemplateInfo artifactTemplateInfo, byte[] artifactContentent, int atrifactLabelCounter) { - - Map json = new HashMap(); - String artifactName = artifactTemplateInfo.getFileName(); - - json.put(Constants.ARTIFACT_NAME, artifactTemplateInfo.getFileName()); - json.put(Constants.ARTIFACT_TYPE, artifactTemplateInfo.getType()); - json.put(Constants.ARTIFACT_DESCRIPTION, "created from csar"); - - // DE250204: There is no need to check if base64 encoding. - - // String encodedPayload = new String(artifactContentent); - // boolean isEncoded = GeneralUtility.isBase64Encoded(artifactContentent); - // if (!isEncoded) { - String encodedPayload = Base64.encodeBase64String(artifactContentent); - // } - json.put(Constants.ARTIFACT_PAYLOAD_DATA, encodedPayload); - String displayName = artifactName; - if (artifactName.lastIndexOf(".") > 0) - displayName = artifactName.substring(0, artifactName.lastIndexOf(".")); - json.put(Constants.ARTIFACT_DISPLAY_NAME, displayName); - String label = ValidationUtils.normalizeArtifactLabel(artifactTemplateInfo.getType() + atrifactLabelCounter); - json.put(Constants.ARTIFACT_LABEL, label); - json.put(Constants.ARTIFACT_GROUP_TYPE, ArtifactGroupTypeEnum.DEPLOYMENT.getType()); - List requiredArtifacts = artifactTemplateInfo.getRelatedArtifactsInfo(); - json.put(Constants.REQUIRED_ARTIFACTS, (requiredArtifacts == null || requiredArtifacts.isEmpty()) ? new ArrayList<>() - : requiredArtifacts.stream().filter(e -> e.getType().equals(ArtifactTypeEnum.HEAT_ARTIFACT.getType()) || e.getType().equals(ArtifactTypeEnum.HEAT_NESTED.getType())).map(e -> e.getFileName()).collect(Collectors.toList())); - return json; - } - - private Map buildJsonForUpdateArtifact(String artifactId, String artifactName, String artifactType, ArtifactGroupTypeEnum artifactGroupType, String label, String displayName, String description, byte[] artifactContentent, - List updatedRequiredArtifacts) { - - Map json = new HashMap(); - if (artifactId != null && !artifactId.isEmpty()) - json.put(Constants.ARTIFACT_ID, artifactId); - - json.put(Constants.ARTIFACT_NAME, artifactName); - json.put(Constants.ARTIFACT_TYPE, artifactType); - json.put(Constants.ARTIFACT_DESCRIPTION, description); - - String encodedPayload = new String(artifactContentent); - - // boolean isEncoded = GeneralUtility.isBase64Encoded(artifactContentent); - // if (!isEncoded) { - log.debug("payload is encoded. perform decode"); - encodedPayload = Base64.encodeBase64String(artifactContentent); - // } - - json.put(Constants.ARTIFACT_PAYLOAD_DATA, encodedPayload); - json.put(Constants.ARTIFACT_DISPLAY_NAME, displayName); - json.put(Constants.ARTIFACT_LABEL, label); - json.put(Constants.ARTIFACT_GROUP_TYPE, artifactGroupType.getType()); - json.put(Constants.REQUIRED_ARTIFACTS, (updatedRequiredArtifacts == null || updatedRequiredArtifacts.isEmpty()) ? new ArrayList<>() - : updatedRequiredArtifacts.stream().filter(e -> e.getType().equals(ArtifactTypeEnum.HEAT_ARTIFACT.getType()) || e.getType().equals(ArtifactTypeEnum.HEAT_NESTED.getType())).map(e -> e.getFileName()).collect(Collectors.toList())); - return json; - } - - @SuppressWarnings({ "unchecked", "static-access" }) - private Either>, ResponseFormat> parseResourceArtifactsInfoFromFile(Resource resource, String artifactsMetaFile, String artifactFileName, User user) { - - try { - JsonObject jsonElement = new JsonObject(); - jsonElement = gson.fromJson(artifactsMetaFile, jsonElement.getClass()); - - JsonElement importStructureElement = jsonElement.get(Constants.IMPORT_STRUCTURE); - if (importStructureElement == null || importStructureElement.isJsonNull()) { - log.debug("Artifact file is not in expected formatr, fileName {}", artifactFileName); - BeEcompErrorManager.getInstance().logInternalDataError("Artifact file is not in expected formatr, fileName " + artifactFileName, "Artifact internals are invalid", ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName)); - } - - Map>> artifactTemplateMap = new HashMap>>(); - artifactTemplateMap = componentsUtils.parseJsonToObject(importStructureElement.toString(), HashMap.class); - if (artifactTemplateMap.isEmpty()) { - log.debug("Artifact file is not in expected formatr, fileName {}", artifactFileName); - BeEcompErrorManager.getInstance().logInternalDataError("Artifact file is not in expected formatr, fileName " + artifactFileName, "Artifact internals are invalid", ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName)); - } - - Set artifactsTypeKeys = artifactTemplateMap.keySet(); - Map> artifactsMap = new HashMap>(); - List allGroups = new ArrayList<>(); - for (String artifactsTypeKey : artifactsTypeKeys) { - - List> o = artifactTemplateMap.get(artifactsTypeKey); - Either, ResponseFormat> artifactTemplateInfoListPairStatus = createArtifactTemplateInfoModule(artifactsTypeKey, o); - if (artifactTemplateInfoListPairStatus.isRight()) { - log.debug("Artifact file is not in expected formatr, fileName {}", artifactFileName); - BeEcompErrorManager.getInstance().logInternalDataError("Artifact file is not in expected format, fileName " + artifactFileName, "Artifact internals are invalid", ErrorSeverity.ERROR); - return Either.right(artifactTemplateInfoListPairStatus.right().value()); - } - List artifactTemplateInfoList = artifactTemplateInfoListPairStatus.left().value(); - if (artifactTemplateInfoList == null) { - log.debug("Artifact file is not in expected formatr, fileName {}", artifactFileName); - BeEcompErrorManager.getInstance().logInternalDataError("Artifact file is not in expected format, fileName " + artifactFileName, "Artifact internals are invalid", ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName)); - - } - allGroups.addAll(artifactTemplateInfoList); - artifactsMap.put(artifactsTypeKey, artifactTemplateInfoList); - } - int counter = groupBusinessLogic.getNextVfModuleNameCounter(resource.getGroups()); - Either validateGroupNamesRes = groupBusinessLogic.validateGenerateVfModuleGroupNames(allGroups, resource.getSystemName(), counter); - if (validateGroupNamesRes.isRight()) { - return Either.right(validateGroupNamesRes.right().value()); - } - return Either.left(artifactsMap); - } catch (Exception e) { - log.debug("Artifact file is not in expected format, fileName {}", artifactFileName); - log.debug("failed with exception.", e); - BeEcompErrorManager.getInstance().logInternalDataError("Artifact file is not in expected format, fileName " + artifactFileName, "Artifact internals are invalid", ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName)); - } - - } - - private Either, ResponseFormat> createArtifactTemplateInfoModule(String artifactsTypeKey, List> jsonObject) { - List artifactTemplateInfoList = new ArrayList(); - for (Map o : jsonObject) { - Either artifacttemplateInfoStatus = ArtifactTemplateInfo.createArtifactTemplateInfoFromJson(componentsUtils, artifactsTypeKey, o, artifactTemplateInfoList, null); - if (artifacttemplateInfoStatus.isRight()) { - return Either.right(artifacttemplateInfoStatus.right().value()); - } - - ArtifactTemplateInfo artifacttemplateInfo = artifacttemplateInfoStatus.left().value(); - if (artifacttemplateInfo != null) { - artifactTemplateInfoList.add(artifacttemplateInfo); - } - - } - return Either.left(artifactTemplateInfoList); - } - - private Either createResourceInstancesRelations(User user, String yamlName, Resource resource, Map uploadResInstancesMap) { - log.debug("createResourceInstancesRelations try to create relations "); - List componentInstancesList = resource.getComponentInstances(); - if (uploadResInstancesMap == null) { - log.debug("UploadComponentInstanceInfo is empty, fileName {}", yamlName); - BeEcompErrorManager.getInstance().logInternalDataError("UploadComponentInstanceInfo is emty, fileName {}", yamlName, ErrorSeverity.ERROR); - - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); - return Either.right(responseFormat); - } - - if (componentInstancesList == null || componentInstancesList.isEmpty()) { - log.debug("componentInstancesList is empty in resource {} ", resource.getUniqueId()); - BeEcompErrorManager.getInstance().logInternalDataError("componentInstancesList is empty in resource {} ", resource.getUniqueId(), ErrorSeverity.ERROR); - - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); - return Either.right(responseFormat); - } - - log.debug("Before validateAndUpdatePropertyValue"); - Either, TitanOperationStatus> allDataTypes = dataTypeCache.getAll(); - if (allDataTypes.isRight()) { - TitanOperationStatus status = allDataTypes.right().value(); - BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status, ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(status)), yamlName)); - - } - - Map> instProperties = new HashMap<>(); - Map>> instCapabilties = new HashMap<>(); - Map>> instRequirements = new HashMap<>(); - Map> instDeploymentArtifacts = new HashMap<>(); - Map> instArtifacts = new HashMap<>(); - Map> instAttributes = new HashMap<>(); - Map originCompMap = new HashMap<>(); - List relations = new ArrayList<>(); - - Map> instInputs = new HashMap<>(); - - for (Entry entry : uploadResInstancesMap.entrySet()) { - log.trace("Processing entry: {}", entry); - UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue(); - ComponentInstance currentCompInstance = null; - for (ComponentInstance compInstance : componentInstancesList) { - log.trace("Processing component instance: {}", compInstance); - 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(), ErrorSeverity.ERROR); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); - return Either.right(responseFormat); - } - String resourceInstanceId = currentCompInstance.getUniqueId(); - Resource originResource; - if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) { - Either getOriginResourceRes = toscaOperationFacade.getToscaFullElement(currentCompInstance.getComponentUid()); - if (getOriginResourceRes.isRight()) { - log.debug("failed to fetch resource with uniqueId {} and tosca component name {} status is {}", currentCompInstance.getComponentUid(), currentCompInstance.getToscaComponentName(), getOriginResourceRes); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()), yamlName); - return Either.right(responseFormat); - } - originResource = getOriginResourceRes.left().value(); - originCompMap.put(originResource.getUniqueId(), originResource); - } else { - originResource = originCompMap.get(currentCompInstance.getComponentUid()); - } - if (originResource.getCapabilities() != null && !originResource.getCapabilities().isEmpty()) - instCapabilties.put(currentCompInstance, originResource.getCapabilities()); - if (originResource.getRequirements() != null && !originResource.getRequirements().isEmpty()) - instRequirements.put(currentCompInstance, originResource.getRequirements()); - if (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.CVFC) { - ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, resource, originResource, currentCompInstance, yamlName, instProperties, allDataTypes.left().value()); - if (addPropertiesValueToRiRes.getStatus() != 200) { - return Either.right(addPropertiesValueToRiRes); - } - } else { - Either genericResourceEither = fetchAndSetDerivedFromGenericType(originResource); - if (genericResourceEither.isRight()) { - return genericResourceEither; - } - log.trace("************* Going to add inputs from from original resource {} to resource instance. ", originResource.getName()); - if (originResource.shouldGenerateInputs()) - generateInputsFromGenericTypeProperties(originResource, genericResourceEither.left().value()); - - ResponseFormat addInputValueToRiRes = addInputsValuesToRi(uploadComponentInstanceInfo, resource, originResource, currentCompInstance, yamlName, instInputs, allDataTypes.left().value()); - if (addInputValueToRiRes.getStatus() != 200) { - return Either.right(addInputValueToRiRes); - } - } - } - - Either>, 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()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addPropToInst.right().value()), yamlName); - return Either.right(responseFormat); - } - if (instInputs != null && !instInputs.isEmpty()) { - Either>, 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()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addInputToInst.right().value()), yamlName); - return Either.right(responseFormat); - } - } - StorageOperationStatus addArtToInst = toscaOperationFacade.associateDeploymentArtifactsToInstances(instDeploymentArtifacts, resource.getUniqueId(), user); - if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { - log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName); - return Either.right(responseFormat); - } - - addArtToInst = toscaOperationFacade.associateArtifactsToInstances(instArtifacts, resource.getUniqueId(), user); - if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { - log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName); - return Either.right(responseFormat); - } - - addArtToInst = toscaOperationFacade.associateCalculatedCapReq(instCapabilties, instRequirements, resource.getUniqueId()); - if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { - log.debug("failed to associate cap and req of resource {} status is {}", resource.getUniqueId(), addArtToInst); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName); - return Either.right(responseFormat); - } - - addArtToInst = toscaOperationFacade.associateInstAttributeToComponentToInstances(instAttributes, resource.getUniqueId()); - if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { - log.debug("failed to associate attributes of resource {} status is {}", resource.getUniqueId(), addArtToInst); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName); - return Either.right(responseFormat); - } - - ComponentParametersView parametersView = new ComponentParametersView(); - parametersView.disableAll(); - parametersView.setIgnoreComponentInstances(false); - parametersView.setIgnoreComponentInstancesProperties(false); - parametersView.setIgnoreCapabilities(false); - parametersView.setIgnoreRequirements(false); - - Either eitherGetResource = toscaOperationFacade.getToscaElement(resource.getUniqueId(), parametersView); - - if (eitherGetResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource); - - return Either.right(responseFormat); - - } - - resource = eitherGetResource.left().value(); - - for (Entry 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(), ErrorSeverity.ERROR); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); - return Either.right(responseFormat); - } - - ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, resource, entry.getValue(), relations); - if (addRelationToRiRes.getStatus() != 200) { - return Either.right(addRelationToRiRes); - } - } - - addArtToInst = toscaOperationFacade.associateResourceInstances(resource.getUniqueId(), relations); - if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) { - log.debug("failed to associate instances of resource {} status is {}", resource.getUniqueId(), addArtToInst); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName); - return Either.right(responseFormat); - } - - log.debug("************* in create relations, getResource start"); - - eitherGetResource = toscaOperationFacade.getToscaElement(resource.getUniqueId()); - log.debug("************* in create relations, getResource end"); - if (eitherGetResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource); - - return Either.right(responseFormat); - - } - resource = eitherGetResource.left().value(); - if(resource.getResourceType() == ResourceTypeEnum.CVFC){ - eitherGetResource = updateCalculatedCapReqWithSubstitutionMappings(resource, uploadResInstancesMap); - if (eitherGetResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource); - return Either.right(responseFormat); - } - } - return Either.left(eitherGetResource.left().value()); - } - - private Either updateCalculatedCapReqWithSubstitutionMappings(Resource resource, Map uploadResInstancesMap) { - Either updateRes = null; - Map>> updatedInstCapabilities = new HashMap<>(); - Map>> 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.associateCalculatedCapReq(updatedInstCapabilities, updatedInstRequirements, resource.getUniqueId()); - if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) { - log.debug("Failed to associate capabilities and requirementss of resource {}, updated according to a substitution mapping. Status is {}", resource.getUniqueId(), status); - updateRes = Either.right(status); - } - } - if(updateRes == null){ - updateRes = toscaOperationFacade.getToscaElement( resource.getUniqueId()); - } - return updateRes; - } - - - private void fillUpdatedInstCapabilitiesRequirements(List componentInstances, Map uploadResInstancesMap, - Map>> updatedInstCapabilities, Map>> updatedInstRequirements) { - - componentInstances.stream().forEach( i -> { - fillUpdatedInstCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate()); - fillUpdatedInstRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate()); - }); - } - - private void fillUpdatedInstRequirements(Map>> updatedInstRequirements, ComponentInstance instance, Map requirementsNamesToUpdate) { - Map> updatedRequirements = new HashMap<>(); - Set updatedReqNames = new HashSet<>(); - if(MapUtils.isNotEmpty(requirementsNamesToUpdate)){ - for (Map.Entry> 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(Collectors.toList())); - } - } - if(MapUtils.isNotEmpty(updatedRequirements)){ - updatedInstRequirements.put(instance, updatedRequirements); - } - } - - private void fillUpdatedInstCapabilities(Map>> updatedInstCapabilties, ComponentInstance instance, Map capabilitiesNamesToUpdate) { - Map> updatedCapabilities = new HashMap<>(); - Set updatedCapNames = new HashSet<>(); - if(MapUtils.isNotEmpty(capabilitiesNamesToUpdate)){ - for (Map.Entry> 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(Collectors.toList())); - } - } - if(MapUtils.isNotEmpty(updatedCapabilities)){ - updatedInstCapabilties.put(instance, updatedCapabilities); - } - } - - private ResponseFormat addRelationToRI(String yamlName, Resource resource, UploadComponentInstanceInfo nodesInfoValue, List relations) { - List componentInstancesList = resource.getComponentInstances(); - - UploadComponentInstanceInfo uploadComponentInstanceInfo = nodesInfoValue; - - 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(), ErrorSeverity.ERROR); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); - return responseFormat; - } - String resourceInstanceId = currentCompInstance.getUniqueId(); - - Map> regMap = uploadComponentInstanceInfo.getRequirements(); - - if (regMap != null) { - Iterator>> nodesRegValue = regMap.entrySet().iterator(); - - while (nodesRegValue.hasNext()) { - Entry> nodesRegInfoEntry = nodesRegValue.next(); - - List 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 eitherReqStatus = findAviableRequiremen(regName, yamlName, uploadComponentInstanceInfo, 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 reqAndRelationshipPairList = regCapRelDef.getRelationships(); - if (reqAndRelationshipPairList == null) - reqAndRelationshipPairList = new ArrayList(); - RequirementAndRelationshipPair reqAndRelationshipPair = new RequirementAndRelationshipPair(); - 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(), ErrorSeverity.ERROR); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); - return responseFormat; - } - regCapRelDef.setToNode(currentCapCompInstance.getUniqueId()); - log.debug("try to find aviable Capability req name is {} ", validReq.getName()); - CapabilityDefinition aviableCapForRel = findAvailableCapabilityByTypeOrName(validReq, currentCapCompInstance, uploadRegInfo); - if (aviableCapForRel == null) { - log.debug("aviable capability was not found. req name is {} component instance is {}", validReq.getName(), currentCapCompInstance.getUniqueId()); - BeEcompErrorManager.getInstance().logInternalDataError("aviable capability was not found. req name is " + validReq.getName() + " component instance is " + currentCapCompInstance.getUniqueId(), resource.getUniqueId(), - ErrorSeverity.ERROR); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); - return responseFormat; - } - reqAndRelationshipPair.setCapability(aviableCapForRel.getName()); - reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId()); - reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId()); - reqAndRelationshipPairList.add(reqAndRelationshipPair); - regCapRelDef.setRelationships(reqAndRelationshipPairList); - relations.add(regCapRelDef); - } - } - } else if (resource.getResourceType() != ResourceTypeEnum.CVFC) { - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK, yamlName); - return responseFormat; - } - return componentsUtils.getResponseFormat(ActionStatus.OK); - } - - private ResponseFormat addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource, ComponentInstance currentCompInstance, String yamlName, - Map> instInputs, Map allDataTypes) { - Map> propMap = uploadComponentInstanceInfo.getProperties(); - if (propMap != null && propMap.size() > 0) { - Map currPropertiesMap = new HashMap(); - - List listFromMap = originResource.getInputs(); - if (listFromMap == null || listFromMap.isEmpty()) { - log.debug("failed to find properties "); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND); - return responseFormat; - } - for (InputDefinition prop : listFromMap) { - String propName = prop.getName(); - if (!currPropertiesMap.containsKey(propName)) { - currPropertiesMap.put(propName, prop); - } - } - List instPropList = new ArrayList<>(); - for (List propertyList : propMap.values()) { - - UploadPropInfo propertyInfo = propertyList.get(0); - String propName = propertyInfo.getName(); - if (!currPropertiesMap.containsKey(propName)) { - log.debug("failed to find property {} ", propName); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName); - return responseFormat; - } - InputDefinition curPropertyDef = currPropertiesMap.get(propName); - ComponentInstanceInput property = null; - - String value = null; - List getInputs = null; - boolean isValidate = true; - if (propertyInfo.getValue() != null) { - getInputs = propertyInfo.getGet_input(); - isValidate = getInputs == null || getInputs.isEmpty(); - if (isValidate) { - value = ImportUtils.getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType()); - } else - value = ImportUtils.getPropertyJsonStringValue(propertyInfo.getValue(), ToscaTagNamesEnum.GET_INPUT.getElementName()); - } - String innerType = null; - property = new ComponentInstanceInput(curPropertyDef, value, null); - - Either validatevalueEiter = validatePropValueBeforeCreate(property, value, isValidate, innerType, allDataTypes); - if (validatevalueEiter.isRight()) { - return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(validatevalueEiter.right().value())); - } - - // String uniqueId = UniqueIdBuilder.buildResourceInstancePropertyValueUid(currentCompInstance.getComponentUid(), index++); - // property.setUniqueId(uniqueId); - property.setValue(validatevalueEiter.left().value()); - - if (getInputs != null && !getInputs.isEmpty()) { - List getInputValues = new ArrayList<>(); - for (GetInputValueDataDefinition getInput : getInputs) { - List inputs = resource.getInputs(); - if (inputs == null || inputs.isEmpty()) { - log.debug("Failed to add property {} to resource instance {}. Inputs list is empty ", property, currentCompInstance.getUniqueId()); - return componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); - } - - Optional optional = inputs.stream().filter(p -> p.getName().equals(getInput.getInputName())).findAny(); - if (!optional.isPresent()) { - log.debug("Failed to find input {} ", getInput.getInputName()); - // @@TODO error message - return componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); - } - InputDefinition input = optional.get(); - getInput.setInputId(input.getUniqueId()); - getInputValues.add(getInput); - - GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex(); - 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()); - // @@TODO error message - return componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); - } - InputDefinition inputIndex = optional.get(); - getInputIndex.setInputId(inputIndex.getUniqueId()); - getInputValues.add(getInputIndex); - } - } - property.setGetInputValues(getInputValues); - } - instPropList.add(property); - // delete overriden property - currPropertiesMap.remove(property.getName()); - } - // add rest of properties - if (!currPropertiesMap.isEmpty()) { - for (InputDefinition value : currPropertiesMap.values()) { - instPropList.add(new ComponentInstanceInput(value)); - } - } - instInputs.put(currentCompInstance.getUniqueId(), instPropList); - } - return componentsUtils.getResponseFormat(ActionStatus.OK); - } - - private ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource, ComponentInstance currentCompInstance, String yamlName, - Map> instProperties, Map allDataTypes) { - - Map> propMap = uploadComponentInstanceInfo.getProperties(); - if (propMap != null && propMap.size() > 0) { - Map currPropertiesMap = new HashMap(); - - List listFromMap = originResource.getProperties(); - if (listFromMap == null || listFromMap.isEmpty()) { - log.debug("failed to find properties "); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND); - return responseFormat; - } - for (PropertyDefinition prop : listFromMap) { - String propName = prop.getName(); - if (!currPropertiesMap.containsKey(propName)) { - currPropertiesMap.put(propName, prop); - } - } - List instPropList = new ArrayList<>(); - for (List propertyList : propMap.values()) { - - UploadPropInfo propertyInfo = propertyList.get(0); - String propName = propertyInfo.getName(); - if (!currPropertiesMap.containsKey(propName)) { - log.debug("failed to find property {} ", propName); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName); - return responseFormat; - } - PropertyDefinition curPropertyDef = currPropertiesMap.get(propName); - ComponentInstanceProperty property = null; - - String value = null; - List getInputs = null; - boolean isValidate = true; - if (propertyInfo.getValue() != null) { - getInputs = propertyInfo.getGet_input(); - isValidate = getInputs == null || getInputs.isEmpty(); - if (isValidate) { - value = ImportUtils.getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType()); - } else - value = ImportUtils.getPropertyJsonStringValue(propertyInfo.getValue(), ToscaTagNamesEnum.GET_INPUT.getElementName()); - } - String innerType = null; - property = new ComponentInstanceProperty(curPropertyDef, value, null); - - Either validatevalueEiter = validatePropValueBeforeCreate(property, value, isValidate, innerType, allDataTypes); - if (validatevalueEiter.isRight()) { - return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(validatevalueEiter.right().value())); - } - - // String uniqueId = UniqueIdBuilder.buildResourceInstancePropertyValueUid(currentCompInstance.getComponentUid(), index++); - // property.setUniqueId(uniqueId); - property.setValue(validatevalueEiter.left().value()); - - if (getInputs != null && !getInputs.isEmpty()) { - List getInputValues = new ArrayList<>(); - for (GetInputValueDataDefinition getInput : getInputs) { - List inputs = resource.getInputs(); - if (inputs == null || inputs.isEmpty()) { - log.debug("Failed to add property {} to resource instance {}. Inputs list is empty ", property, currentCompInstance.getUniqueId()); - return componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); - } - - Optional optional = inputs.stream().filter(p -> p.getName().equals(getInput.getInputName())).findAny(); - if (!optional.isPresent()) { - log.debug("Failed to find input {} ", getInput.getInputName()); - // @@TODO error message - return componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); - } - InputDefinition input = optional.get(); - getInput.setInputId(input.getUniqueId()); - getInputValues.add(getInput); - - GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex(); - 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()); - // @@TODO error message - return componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); - } - InputDefinition inputIndex = optional.get(); - getInputIndex.setInputId(inputIndex.getUniqueId()); - getInputValues.add(getInputIndex); - - } - - } - property.setGetInputValues(getInputValues); - } - instPropList.add(property); - // delete overriden property - currPropertiesMap.remove(property.getName()); - } - // add rest of properties - if (!currPropertiesMap.isEmpty()) { - for (PropertyDefinition value : currPropertiesMap.values()) { - instPropList.add(new ComponentInstanceProperty(value)); - } - } - instProperties.put(currentCompInstance.getUniqueId(), instPropList); - } - return componentsUtils.getResponseFormat(ActionStatus.OK); - } - - // US740820 Relate RIs according to capability name - private CapabilityDefinition findAvailableCapabilityByTypeOrName(RequirementDefinition validReq, ComponentInstance currentCapCompInstance, UploadReqInfo uploadReqInfo) { - if (null == uploadReqInfo.getCapabilityName() || validReq.getCapability().equals(uploadReqInfo.getCapabilityName())) {// get - // by - // capability - // type - return findAviableCapability(validReq, currentCapCompInstance); - } - return findAvailableCapability(validReq, currentCapCompInstance, uploadReqInfo); - } - - private CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance currentCapCompInstance, UploadReqInfo uploadReqInfo) { - CapabilityDefinition cap = null; - Map> capMap = currentCapCompInstance.getCapabilities(); - if (!capMap.containsKey(validReq.getCapability())) { - return null; - } - Optional capByName = capMap.get(validReq.getCapability()).stream().filter(p -> p.getName().equals(uploadReqInfo.getCapabilityName())).findAny(); - if (!capByName.isPresent()) { - return null; - } - cap = capByName.get(); - - if (cap.getMaxOccurrences() != null && !cap.getMaxOccurrences().equals(CapabilityDataDefinition.MAX_OCCURRENCES)) { - String leftOccurrences = cap.getLeftOccurrences(); - int left = Integer.parseInt(leftOccurrences); - if (left > 0) { - --left; - cap.setLeftOccurrences(String.valueOf(left)); - - } - - } - - // TODO temporary fix - remove specific capability node validation - - // String reqNode = validReq.getNode(); - // if (reqNode != null && !reqNode.isEmpty() && - // !cap.getCapabilitySources().contains(reqNode)) { - // return null; - // } - // RequirementAndRelationshipPair relationPair = getReqRelPair(cap); - // Either eitherStatus = componentInstanceOperation.isAvailableCapabilty(currentCapCompInstance, relationPair); - // if (eitherStatus.isRight() || eitherStatus.left().value() == false) { - // return null; - // } - return cap; - } - - private CapabilityDefinition findAviableCapability(RequirementDefinition validReq, ComponentInstance currentCapCompInstance) { - CapabilityDefinition aviableCapForRel = null; - Map> capMap = currentCapCompInstance.getCapabilities(); - if (capMap.containsKey(validReq.getCapability())) { - List capList = capMap.get(validReq.getCapability()); - - for (CapabilityDefinition cap : capList) { - // TODO temporary fix - remove specific capability node - // String reqNode = validReq.getNode(); - // if (reqNode != null && !reqNode.isEmpty()) { - // if (!cap.getCapabilitySources().contains(reqNode)) { - // continue; - // } - // } - if (cap.getMaxOccurrences() != null && !cap.getMaxOccurrences().equals(CapabilityDataDefinition.MAX_OCCURRENCES)) { - String leftOccurrences = cap.getLeftOccurrences(); - if (leftOccurrences == null) { - leftOccurrences = cap.getMaxOccurrences(); - } - int left = Integer.parseInt(leftOccurrences); - if (left > 0) { - --left; - cap.setLeftOccurrences(String.valueOf(left)); - aviableCapForRel = cap; - break; - } else { - continue; - } - } else { - aviableCapForRel = cap; - break; - } - } - } - return aviableCapForRel; - } - - private Either findAviableRequiremen(String regName, String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo, ComponentInstance currentCompInstance, String capName) { - Map> comInstRegDefMap = currentCompInstance.getRequirements(); - List list = comInstRegDefMap.get(capName); - RequirementDefinition validRegDef = null; - if (list == null) { - for (Entry> 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 Either parseResourceInfoFromYaml(String yamlFileName, Resource resource, String resourceYml, Map createdNodesToscaResourceNames, Map nodeTypesInfo, String nodeName) { - - Map mappedToscaTemplate; - if(nodeTypesInfo != null && nodeName != null && nodeTypesInfo.containsKey(nodeName)){ - mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate(); - } - else { - try { - //DE154502 Fail if duplicate key found in file - mappedToscaTemplate = ImportUtils.loadYamlAsStrictMap(resourceYml); - - } catch(ParserException e) { - log.error("Failed to load yaml file {}", yamlFileName, e); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.TOSCA_PARSE_ERROR, yamlFileName, e.getMessage()); - return Either.right(responseFormat); - } - } - Either toscaElementEither = ImportUtils.findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.TOPOLOGY_TEMPLATE, ToscaElementTypeEnum.ALL); - if (toscaElementEither.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE); - return Either.right(responseFormat); - } - - Either, ResponseFormat> createInputsEither = createInputsFromYaml(yamlFileName, mappedToscaTemplate, resource); - if (createInputsEither.isRight()) { - ResponseFormat responseFormat = createInputsEither.right().value(); - return Either.right(responseFormat); - } - - Either, ResponseFormat> uploadResInstancesEither = createResourcesInstanceInfoFromYaml(yamlFileName, mappedToscaTemplate, resource, createdNodesToscaResourceNames); - if (uploadResInstancesEither.isRight()) { - ResponseFormat responseFormat = uploadResInstancesEither.right().value(); - return Either.right(responseFormat); - } - - Either, ResponseFormat> createGroupsFromYaml = createGroupsFromYaml(yamlFileName, mappedToscaTemplate, resource); - if (createGroupsFromYaml.isRight()) { - ResponseFormat responseFormat = createGroupsFromYaml.right().value(); - return Either.right(responseFormat); - } - - ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo(); - parsedToscaYamlInfo.setInputs(createInputsEither.left().value()); - parsedToscaYamlInfo.setInstances(uploadResInstancesEither.left().value()); - parsedToscaYamlInfo.setGroups(createGroupsFromYaml.left().value()); - - return Either.left(parsedToscaYamlInfo); - } - - private Either createResourceInstances(User user, String yamlName, Resource resource, Map uploadResInstancesMap, boolean inTransaction, boolean needLock, - Map nodeNamespaceMap) { - - Either eitherResource = null; - log.debug("{} - going to create resource instanse from CSAR", yamlName); - if (uploadResInstancesMap == null || uploadResInstancesMap.isEmpty()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE); - - return Either.right(responseFormat); - - } - Map existingnodeTypeMap = new HashMap<>(); - if (nodeNamespaceMap != null && !nodeNamespaceMap.isEmpty()) { - nodeNamespaceMap.entrySet().stream().forEach(x -> existingnodeTypeMap.put(x.getValue().getToscaResourceName(), x.getValue())); - } - - Iterator> nodesInfoValue = uploadResInstancesMap.entrySet().iterator(); - Map resourcesInstancesMap = new HashMap<>(); - while (nodesInfoValue.hasNext()) { - log.debug("*************Going to create resource instances from {}", yamlName); - Entry uploadComponentInstanceInfoEntry = nodesInfoValue.next(); - UploadComponentInstanceInfo uploadComponentInstanceInfo = uploadComponentInstanceInfoEntry.getValue(); - - // updating type if the type is node type name - we need to take the - // updated name - log.debug("*************Going to create resource instance {}", uploadComponentInstanceInfo.getName()); - if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) { - uploadComponentInstanceInfo.setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName()); - } - - eitherResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo, existingnodeTypeMap); - if (eitherResource.isRight()) { - return eitherResource; - } - Resource refResource = eitherResource.left().value(); - - ComponentInstance componentInstance = new ComponentInstance(); - - componentInstance.setComponentUid(refResource.getUniqueId()); - - ComponentTypeEnum containerComponentType = resource.getComponentType(); - NodeTypeEnum containerNodeType = containerComponentType.getNodeType(); - //************ - if (containerNodeType.equals(NodeTypeEnum.Resource) && MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) && MapUtils.isNotEmpty(refResource.getCapabilities())) { - setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities()); - Either>, ResponseFormat> getValidComponentInstanceCapabilitiesRes = getValidComponentInstanceCapabilities(refResource.getUniqueId(), refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities()); - if (getValidComponentInstanceCapabilitiesRes.isRight()) { - return Either.right(getValidComponentInstanceCapabilitiesRes.right().value()); - } else { - componentInstance.setCapabilities(getValidComponentInstanceCapabilitiesRes.left().value()); - } - } - //*********************** - if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) { - log.debug("createResourceInstances - not found latest 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()); - return Either.right(responseFormat); - } - Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType()); - componentInstance.setName(uploadComponentInstanceInfo.getName()); - componentInstance.setIcon(origResource.getIcon()); - - resourcesInstancesMap.put(componentInstance, origResource); - - } - if (MapUtils.isNotEmpty(resourcesInstancesMap)) { - - StorageOperationStatus status = toscaOperationFacade.associateComponentInstancesToComponent(resource, resourcesInstancesMap, false); - if (status != null && status != StorageOperationStatus.OK) { - log.debug("Failed to add component instances to container component {}", resource.getName()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status)); - eitherResource = Either.right(responseFormat); - return eitherResource; - } - - } - - log.debug("*************Going to get resource {}", resource.getUniqueId()); - ComponentParametersView parametersView = new ComponentParametersView(); - parametersView.disableAll(); - parametersView.setIgnoreComponentInstances(false); - parametersView.setIgnoreUsers(false); - parametersView.setIgnoreInputs(false); // inputs are read when creating - // property values on instances - Either eitherGerResource = toscaOperationFacade.getToscaElement(resource.getUniqueId(), parametersView); - log.debug("*************finished to get resource {}", resource.getUniqueId()); - if (eitherGerResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource); - - return Either.right(responseFormat); - - } - - if (eitherGerResource.left().value().getComponentInstances() == null || eitherGerResource.left().value().getComponentInstances().isEmpty()) { - - log.debug("Error when create resource inctanse from csar. ComponentInstances list empty"); - BeEcompErrorManager.getInstance().logBeDaoSystemError("Error when create resource inctanse from csar. ComponentInstances list empty"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE); - return Either.right(responseFormat); - - } - - return Either.left(eitherGerResource.left().value()); - } - - private void setCapabilityNamesTypes(Map> originCapabilities, Map> uploadedCapabilities) { - for(Entry> currEntry : uploadedCapabilities.entrySet()){ - if(originCapabilities.containsKey(currEntry.getKey())){ - currEntry.getValue().stream().forEach(cap -> cap.setType(currEntry.getKey())); - } - } - - for(Map.Entry> capabilities : originCapabilities.entrySet()){ - capabilities.getValue().stream().forEach(cap -> {if(uploadedCapabilities.containsKey(cap.getName())){uploadedCapabilities.get(cap.getName()).stream().forEach(c -> {c.setName(cap.getName());c.setType(cap.getType());});};}); - } - } - - - - private Either validateResourceInstanceBeforeCreate(String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo, Map nodeNamespaceMap) { - log.debug("going to validate resource instance with name {} and type {} before create", uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); - Resource refResource = null; - if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) { - refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()); - } else { - Either findResourceEither = toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(uploadComponentInstanceInfo.getType()); - if (findResourceEither.isRight()) { - log.debug("not found lates version for resource instance with name {} and type {}", uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(findResourceEither.right().value())); - return Either.right(responseFormat); - } - refResource = findResourceEither.left().value(); - nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource); - } - String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState(); - if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) { - log.debug("component instance of component {} can not be created because the component is in an illegal state {}.", refResource.getName(), componentState); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.ILLEGAL_COMPONENT_STATE, refResource.getComponentType().getValue(), refResource.getName(), componentState); - return Either.right(responseFormat); - } - - if (!ToscaUtils.isAtomicType(refResource) && refResource.getResourceType() != ResourceTypeEnum.CVFC) { - log.debug("ref resource type is {}", refResource.getResourceType()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); - return Either.right(responseFormat); - } - log.debug("validate resource instance with name {} and type {} before create, successful",uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); - return Either.left(refResource); - } - - private Either, ResponseFormat> createResourcesInstanceInfoFromYaml(String yamlFileName, Map toscaJson, Resource resource, Map createdNodesToscaResourceNames) { - Map moduleComponentInstances = new HashMap(); - Map substitutionMappings = null; - Either, ResponseFormat> result = Either.left(moduleComponentInstances); - Either, ResultStatusEnum> eitherNodesTemlates = ImportUtils.findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.NODE_TEMPLATES); - Either, ResultStatusEnum> eitherSubstitutionMappings = ImportUtils.findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS); - if (eitherSubstitutionMappings.isLeft()) { - substitutionMappings = eitherSubstitutionMappings.left().value(); - } - if (eitherNodesTemlates.isLeft()) { - Map jsonNodeTemplates = eitherNodesTemlates.left().value(); - - Iterator> nodesNameValue = jsonNodeTemplates.entrySet().iterator(); - while (nodesNameValue.hasNext()) { - Entry nodeNameValue = nodesNameValue.next(); - Either eitherNode = createModuleComponentInstanceInfo(nodeNameValue, substitutionMappings, createdNodesToscaResourceNames); - if (eitherNode.isRight()) { - log.info("error when creating node template:{}, for resource:{}", nodeNameValue.getKey(), resource.getName()); - return Either.right(eitherNode.right().value()); - } else { - UploadComponentInstanceInfo uploadComponentInstanceInfo = eitherNode.left().value(); - moduleComponentInstances.put(nodeNameValue.getKey(), uploadComponentInstanceInfo); - } - - } - - } - if (moduleComponentInstances.isEmpty()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName); - return Either.right(responseFormat); - } - - return result; - } - - @SuppressWarnings("unchecked") - private Either createModuleComponentInstanceInfo(Entry nodeTemplateJsonEntry, Map substitutionMappings, Map createdNodesToscaResourceNames) { - - UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo(); - Either result = Either.left(nodeTemplateInfo); - nodeTemplateInfo.setName(nodeTemplateJsonEntry.getKey()); - try { - if (nodeTemplateJsonEntry.getValue() instanceof String) { - String nodeTemplateJsonString = (String) nodeTemplateJsonEntry.getValue(); - nodeTemplateInfo.setType(nodeTemplateJsonString); - } else if (nodeTemplateJsonEntry.getValue() instanceof Map) { - Map nodeTemplateJsonMap = (Map) nodeTemplateJsonEntry.getValue(); - // Type - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.TYPE.getElementName())) { - String toscaResourceType = (String) nodeTemplateJsonMap.get(ToscaTagNamesEnum.TYPE.getElementName()); - if (createdNodesToscaResourceNames.containsKey(toscaResourceType)) { - toscaResourceType = createdNodesToscaResourceNames.get(toscaResourceType); - } - nodeTemplateInfo.setType(toscaResourceType); - } - - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.REQUIREMENTS.getElementName())) { - Either>, ResponseFormat> regResponse = createReqModuleFromYaml(nodeTemplateInfo, nodeTemplateJsonMap); - if (regResponse.isRight()) - return Either.right(regResponse.right().value()); - if (regResponse.left().value().size() > 0) { - nodeTemplateInfo.setRequirements(regResponse.left().value()); - } - } - - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.CAPABILITIES.getElementName())) { - Either>, ResponseFormat> eitherCapRes = createCapModuleFromYaml(nodeTemplateInfo, nodeTemplateJsonMap); - if (eitherCapRes.isRight()) - return Either.right(eitherCapRes.right().value()); - if (eitherCapRes.left().value().size() > 0) { - nodeTemplateInfo.setCapabilities(eitherCapRes.left().value()); - } - } - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.PROPERTIES.getElementName())) { - Either>, ResponseFormat> regResponse = createPropModuleFromYaml(nodeTemplateJsonMap); - if (regResponse.isRight()) - return Either.right(regResponse.right().value()); - if (regResponse.left().value().size() > 0) { - nodeTemplateInfo.setProperties(regResponse.left().value()); - } - } - if (substitutionMappings != null) { - if (substitutionMappings.containsKey(ToscaTagNamesEnum.CAPABILITIES.getElementName())) { - Either, ResponseFormat> getCapNamesToUpdateRes = getNamesToUpdate(nodeTemplateInfo, (Map>) substitutionMappings.get(ToscaTagNamesEnum.CAPABILITIES.getElementName())); - if (getCapNamesToUpdateRes.isRight()) - return Either.right(getCapNamesToUpdateRes.right().value()); - if (getCapNamesToUpdateRes.left().value().size() > 0) { - nodeTemplateInfo.setCapabilitiesNamesToUpdate(getCapNamesToUpdateRes.left().value()); - } - } - if (substitutionMappings.containsKey(ToscaTagNamesEnum.REQUIREMENTS.getElementName())) { - Either, ResponseFormat> getReqNamesToUpdateRes = getNamesToUpdate(nodeTemplateInfo, (Map>) substitutionMappings.get(ToscaTagNamesEnum.REQUIREMENTS.getElementName())); - if (getReqNamesToUpdateRes.isRight()) - return Either.right(getReqNamesToUpdateRes.right().value()); - if (getReqNamesToUpdateRes.left().value().size() > 0) { - nodeTemplateInfo.setRequirementsNamesToUpdate(getReqNamesToUpdateRes.left().value()); - } - } - } - } else { - - result = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE)); - - } - } catch (Exception e) { - BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create capability"); - log.debug("error when creating capability, message:{}", e.getMessage(), e); - result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML)); - } - - return result; - } - - private Either, ResponseFormat> getNamesToUpdate(UploadComponentInstanceInfo nodeTemplateInfo, Map> elements) { - Either, ResponseFormat> response; - try { - Map namesToUpdate = elements.entrySet().stream().filter(e -> e.getValue().get(0).equalsIgnoreCase(nodeTemplateInfo.getName())).collect(Collectors.toMap(e -> e.getValue().get(1), e -> e.getKey())); - response = Either.left(namesToUpdate); - } catch (Exception e) { - log.debug("The exception {} occured upon adding names to update for instance {} . ", e.getMessage(), nodeTemplateInfo.getName()); - response = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } - return response; - } - - @SuppressWarnings("unchecked") - private Either>, ResponseFormat> createPropModuleFromYaml(Map nodeTemplateJsonMap) { - Map> moduleProp = new HashMap>(); - Either>, ResponseFormat> response = Either.left(moduleProp); - Either, ResultStatusEnum> toscaProperties = ImportUtils.findFirstToscaMapElement(nodeTemplateJsonMap, ToscaTagNamesEnum.PROPERTIES); - if (toscaProperties.isLeft()) { - Map jsonProperties = toscaProperties.left().value(); - for (Entry jsonPropObj : jsonProperties.entrySet()) { - // Property - String propName = jsonPropObj.getKey(); - Object propValue = jsonPropObj.getValue(); - - if (valueContainsPattern(STR_REPLACE_PATTERN, propValue)) { - log.trace("Ignore property value {}.", propName); - continue; - } - - if (valueContainsPattern(TOKEN_PATTERN, propValue)) { - log.trace("Ignore property value {}.", propName); - continue; - } - if (valueContainsPattern(GET_PROPERTY_PATTERN, propValue)) { - log.trace("Ignore property value {}.", propName); - continue; - } - - if (valueContainsPattern(CONCAT_PATTERN, propValue)) { - log.trace("Ignore property value {}.", propName); - continue; - } - - UploadPropInfo propertyDef = new UploadPropInfo(); - propertyDef.setValue(propValue); - propertyDef.setName(propName); - if (propValue instanceof Map) { - if (((Map) propValue).containsKey(ToscaTagNamesEnum.TYPE.getElementName())) { - propertyDef.setType(((Map) propValue).get(ToscaTagNamesEnum.TYPE.getElementName()).toString()); - } - - if (((Map) propValue).containsKey(ToscaTagNamesEnum.GET_INPUT.getElementName()) - || ImportUtils.getPropertyJsonStringValue(propValue, ToscaPropertyType.MAP.getType()).contains(ToscaTagNamesEnum.GET_INPUT.getElementName())) { - createGetInputModuleFromMap(propName, (Map) propValue, propertyDef); - } - - if (((Map) propValue).containsKey(ToscaTagNamesEnum.DESCRIPTION.getElementName())) { - propertyDef.setDescription(((Map) propValue).get(ToscaTagNamesEnum.DESCRIPTION.getElementName()).toString()); - } - if (((Map) propValue).containsKey(ToscaTagNamesEnum.DEFAULT_VALUE.getElementName())) { - propertyDef.setValue(((Map) propValue).get(ToscaTagNamesEnum.DEFAULT_VALUE.getElementName())); - } - if (((Map) propValue).containsKey(ToscaTagNamesEnum.IS_PASSWORD.getElementName())) { - propertyDef.setPassword(Boolean.getBoolean(((Map) propValue).get(ToscaTagNamesEnum.IS_PASSWORD.getElementName()).toString())); - } else { - propertyDef.setValue(propValue); - } - } else if (propValue instanceof List) { - List propValueList = (List) propValue; - - createInputPropList(propertyDef, propValueList); - propertyDef.setValue(propValue); - } - - if (moduleProp.containsKey(propName)) { - moduleProp.get(propName).add(propertyDef); - } else { - List list = new ArrayList(); - list.add(propertyDef); - moduleProp.put(propName, list); - } - } - } - return response; - } - - @SuppressWarnings("unchecked") - private void createInputPropList(UploadPropInfo propertyDef, List propValueList) { - for (Object objValue : propValueList) { - - if (objValue instanceof Map) { - Map objMap = (Map) objValue; - if (objMap.containsKey(ToscaTagNamesEnum.GET_INPUT.getElementName())) - createGetInputModuleFromMap(propertyDef.getName(), objMap, propertyDef); - else { - Set keys = objMap.keySet(); - for (String key : keys) { - Object value = objMap.get(key); - if (value instanceof Map) { - createGetInputModuleFromMap(key, (Map) value, propertyDef); - - } else if (value instanceof List) { - List propSubValueList = (List) value; - - createInputPropList(propertyDef, propSubValueList); - } - - } - } - - } else if (objValue instanceof List) { - List propSubValueList = (List) objValue; - - createInputPropList(propertyDef, propSubValueList); - - } - - } - } - - @SuppressWarnings("unchecked") - private void createGetInputModuleFromMap(String propName, Map propValue, UploadPropInfo propertyDef) { - - if (propValue.containsKey(ToscaTagNamesEnum.GET_INPUT.getElementName())) { - Object getInput = propValue.get(ToscaTagNamesEnum.GET_INPUT.getElementName()); - GetInputValueDataDefinition getInputInfo = new GetInputValueDataDefinition(); - List getInputs = propertyDef.getGet_input(); - if (getInputs == null) { - getInputs = new ArrayList(); - } - if (getInput instanceof String) { - - getInputInfo.setInputName((String) getInput); - getInputInfo.setPropName(propName); - - } else if (getInput instanceof List) { - List getInputList = (List) getInput; - getInputInfo.setPropName(propName); - getInputInfo.setInputName((String) getInputList.get(0)); - if (getInputList.size() > 1) { - Object indexObj = getInputList.get(1); - if (indexObj instanceof Integer) { - getInputInfo.setIndexValue((Integer) indexObj); - } else if (indexObj instanceof Float) { - int index = ((Float) indexObj).intValue(); - getInputInfo.setIndexValue(index); - } else if (indexObj instanceof Map && ((Map) indexObj).containsKey(ToscaTagNamesEnum.GET_INPUT.getElementName())) { - Object index = ((Map) indexObj).get(ToscaTagNamesEnum.GET_INPUT.getElementName()); - GetInputValueDataDefinition getInputInfoIndex = new GetInputValueDataDefinition(); - getInputInfoIndex.setInputName((String) index); - getInputInfoIndex.setPropName(propName); - getInputInfo.setGetInputIndex(getInputInfoIndex); - } - getInputInfo.setList(true); - } - - } - getInputs.add(getInputInfo); - propertyDef.setGet_input(getInputs); - propertyDef.setValue(propValue); - } else { - Set keys = propValue.keySet(); - for (String key : keys) { - Object value = propValue.get(key); - if (value instanceof Map) { - createGetInputModuleFromMap(key, (Map) value, propertyDef); - - } else if (value instanceof List) { - List valueList = (List) value; - for (Object o : valueList) { - if (o instanceof Map) { - createGetInputModuleFromMap(key, (Map) o, propertyDef); - - } - } - - } - - } - - } - } - - /* - * private boolean valueContainsStrReplace(Object propValue) { - * - * log.debug("valueContainsStrReplace value is {}", propValue); boolean result = false; if (propValue != null) { log.debug("valueContainsStrReplace value is {}", propValue.getClass()); Matcher matcher = - * STR_REPLACE_PATTERN.matcher(propValue.toString()); result = matcher.find(); } - * - * return result; } - * - * private boolean valueContainsToken(Object propValue) { - * - * log.debug("valueContainsToken value is {}", propValue); boolean result = false; if (propValue != null) { log.debug("valueContainsToken value is {}", propValue.getClass()); Matcher matcher = TOKEN_PATTERN.matcher(propValue.toString()); result = - * matcher.find(); } - * - * return result; } - */ - - private boolean valueContainsPattern(Pattern pattern, Object propValue) { - - log.debug("valueContainsToken value is {}", propValue); - boolean result = false; - if (propValue != null) { - log.trace("valueContainspattern value is {}", propValue.getClass()); - Matcher matcher = pattern.matcher(propValue.toString()); - result = matcher.find(); - } - - return result; - - } - - @SuppressWarnings("unchecked") - private Either>, ResponseFormat> createCapModuleFromYaml(UploadComponentInstanceInfo nodeTemplateInfo, Map nodeTemplateJsonMap) { - Map> moduleCap = new HashMap>(); - Either>, ResponseFormat> response = Either.left(moduleCap); - Either, ResultStatusEnum> toscaRequirements = ImportUtils.findFirstToscaListElement(nodeTemplateJsonMap, ToscaTagNamesEnum.CAPABILITIES); - if (toscaRequirements.isLeft()) { - List jsonCapabilities = toscaRequirements.left().value(); - - for (Object jsonCapObj : jsonCapabilities) { - // Requirement - Map capJsonWrapper = (Map) jsonCapObj; - String capName = capJsonWrapper.keySet().iterator().next(); - Either eitherCap = createModuleNodeTemplateCap(capJsonWrapper.get(capName)); - if (eitherCap.isRight()) { - log.info("error when creating Requirement:{}, for node:{}", capName, nodeTemplateInfo); - return Either.right(eitherCap.right().value()); - } else { - UploadCapInfo requirementDef = eitherCap.left().value(); - requirementDef.setName(capName); - if (moduleCap.containsKey(capName)) { - moduleCap.get(capName).add(requirementDef); - } else { - List list = new ArrayList(); - list.add(requirementDef); - moduleCap.put(capName, list); - } - } - } - } - return response; - } - - @SuppressWarnings("unchecked") - private Either>, ResponseFormat> createReqModuleFromYaml(UploadComponentInstanceInfo nodeTemplateInfo, Map nodeTemplateJsonMap) { - Map> moduleRequirements = new HashMap>(); - Either>, ResponseFormat> response = Either.left(moduleRequirements); - Either, ResultStatusEnum> toscaRequirements = ImportUtils.findFirstToscaListElement(nodeTemplateJsonMap, ToscaTagNamesEnum.REQUIREMENTS); - if (toscaRequirements.isLeft()) { - List jsonRequirements = toscaRequirements.left().value(); - - for (Object jsonRequirementObj : jsonRequirements) { - // Requirement - Map requirementJsonWrapper = (Map) jsonRequirementObj; - String requirementName = requirementJsonWrapper.keySet().iterator().next(); - Either eitherRequirement = createModuleNodeTemplateReg(requirementJsonWrapper.get(requirementName)); - if (eitherRequirement.isRight()) { - log.info("error when creating Requirement:{}, for node:{}", requirementName, nodeTemplateInfo); - return Either.right(eitherRequirement.right().value()); - } else { - UploadReqInfo requirementDef = eitherRequirement.left().value(); - requirementDef.setName(requirementName); - if (moduleRequirements.containsKey(requirementName)) { - moduleRequirements.get(requirementName).add(requirementDef); - } else { - List list = new ArrayList(); - list.add(requirementDef); - moduleRequirements.put(requirementName, list); - } - } - } - } - return response; - } - - @SuppressWarnings("unchecked") - private Either createModuleNodeTemplateCap(Object capObject) { - UploadCapInfo capTemplateInfo = new UploadCapInfo(); - Either result = Either.left(capTemplateInfo); - - if (capObject instanceof String) { - String nodeTemplateJsonString = (String) capObject; - capTemplateInfo.setNode(nodeTemplateJsonString); - } else if (capObject instanceof Map) { - Map nodeTemplateJsonMap = (Map) capObject; - // Type - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.NODE.getElementName())) { - capTemplateInfo.setNode((String) nodeTemplateJsonMap.get(ToscaTagNamesEnum.NODE.getElementName())); - } - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.TYPE.getElementName())) { - capTemplateInfo.setType((String) nodeTemplateJsonMap.get(ToscaTagNamesEnum.TYPE.getElementName())); - } - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) { - Either, ResultStatusEnum> validSourceTypesRes = ImportUtils.findFirstToscaListElement(nodeTemplateJsonMap, ToscaTagNamesEnum.VALID_SOURCE_TYPES); - if (validSourceTypesRes.isLeft()) { - capTemplateInfo.setValidSourceTypes(validSourceTypesRes.left().value().stream().map(o -> o.toString()).collect(Collectors.toList())); - } - } - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.PROPERTIES.getElementName())) { - Either>, ResponseFormat> regResponse = createPropModuleFromYaml(nodeTemplateJsonMap); - if (regResponse.isRight()) - return Either.right(regResponse.right().value()); - if (!regResponse.left().value().isEmpty()) { - List properties = new ArrayList(); - regResponse.left().value().values().forEach(list -> properties.addAll(list)); - if (!properties.isEmpty()) - capTemplateInfo.setProperties(properties); - } - } - } - return result; - } - - @SuppressWarnings("unchecked") - private Either createModuleNodeTemplateReg(Object regObject) { - - UploadReqInfo regTemplateInfo = new UploadReqInfo(); - Either result = Either.left(regTemplateInfo); - - if (regObject instanceof String) { - String nodeTemplateJsonString = (String) regObject; - regTemplateInfo.setNode(nodeTemplateJsonString); - } else if (regObject instanceof Map) { - Map nodeTemplateJsonMap = (Map) regObject; - // Type - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.NODE.getElementName())) { - regTemplateInfo.setNode((String) nodeTemplateJsonMap.get(ToscaTagNamesEnum.NODE.getElementName())); - } - // US740820 Relate RIs according to capability name - if (nodeTemplateJsonMap.containsKey(ToscaTagNamesEnum.CAPABILITY.getElementName())) { - regTemplateInfo.setCapabilityName((String) nodeTemplateJsonMap.get(ToscaTagNamesEnum.CAPABILITY.getElementName())); - } - } - return result; - } - - public Either propagateStateToCertified(User user, Resource resource, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, boolean needLock, boolean forceCertificationAllowed) { - - Either result = null; - try { - if(resource.getLifecycleState() != LifecycleStateEnum.CERTIFIED && forceCertificationAllowed && lifecycleBusinessLogic.isFirstCertification(resource.getVersion())){ - result = nodeForceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock); - if(result.isRight()){ - return result; - } - resource = result.left().value(); - } - if (resource.getLifecycleState() == LifecycleStateEnum.CERTIFIED) { - Either, ResponseFormat> eitherPopulated = populateToscaArtifacts(resource, user, false, inTransaction, needLock); - result = eitherPopulated.isLeft() ? Either.left(resource) : Either.right(eitherPopulated.right().value()); - return result; - } - return nodeFullCertification(resource.getUniqueId(), user, lifecycleChangeInfo, inTransaction, needLock); - } catch (Exception e) { - log.debug("The exception {} has occured upon certification of resource {}. ", e.getMessage(), resource.getName()); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } finally { - if (result == null || result.isRight()) { - BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify"); - if (inTransaction == false) { - log.debug("operation failed. do rollback"); - titanDao.rollback(); - } - } else if (inTransaction == false) { - log.debug("operation success. do commit"); - titanDao.commit(); - } - } - } - - private Either nodeFullCertification(String uniqueId, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, boolean needLock) { - Either result = lifecycleBusinessLogic.changeState(uniqueId, user, LifeCycleTransitionEnum.CERTIFICATION_REQUEST, lifecycleChangeInfo, inTransaction, needLock); - if (result.isLeft()) { - result = lifecycleBusinessLogic.changeState(uniqueId, user, LifeCycleTransitionEnum.START_CERTIFICATION, lifecycleChangeInfo, inTransaction, needLock); - } - if (result.isLeft()) { - result = lifecycleBusinessLogic.changeState(uniqueId, user, LifeCycleTransitionEnum.CERTIFY, lifecycleChangeInfo, inTransaction, needLock); - } - return result; - } - - private Either nodeForceCertification(Resource resource, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, boolean needLock) { - return lifecycleBusinessLogic.forceResourceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock); - } - - /* - * /** - * - * @deprecated Use {@link #createOrUpdateResourceByImport(Resource,User,boolean, boolean,boolean)} instead - */ - /* - * public Either, ResponseFormat> createOrUpdateResourceByImport(Resource resource, User user, AuditingActionEnum auditingEnum, boolean isNormative, boolean needLock) { return - * createOrUpdateResourceByImport(resource, user, isNormative, false, needLock); } - */ - - public Either, ResponseFormat> createOrUpdateResourceByImport(Resource resource, User user, boolean isNormative, boolean isInTransaction, boolean needLock, CsarInfo csarInfo) { - - // check if resource already exist - Either latestByName = toscaOperationFacade.getLatestByName(resource.getName()); - Either, ResponseFormat> result = null; - - // create - if (latestByName.isRight() && latestByName.right().value().equals(StorageOperationStatus.NOT_FOUND)) { - - Either latestByToscaName = toscaOperationFacade.getLatestByToscaResourceName(resource.getToscaResourceName()); - if (latestByToscaName.isRight() && latestByToscaName.right().value().equals(StorageOperationStatus.NOT_FOUND)) - result = createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo); - - else { - StorageOperationStatus status = latestByName.right().value(); - BeEcompErrorManager.getInstance().logBeComponentMissingError("Create / Update resource by import", ComponentTypeEnum.RESOURCE.getValue(), resource.getName()); - log.debug("resource already exist {}. status={}", resource.getName(), status); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_ALREADY_EXISTS); - componentsUtils.auditResource(responseFormat, user, resource, "", "", AuditingActionEnum.IMPORT_RESOURCE, null); - result = Either.right(responseFormat); - } - - } - - // update - else if (latestByName.isLeft()) { - result = updateExistingResourceByImport(resource, latestByName.left().value(), user, isNormative, needLock); - } - - // error - else { - StorageOperationStatus status = latestByName.right().value(); - log.debug("failed to get latest version of resource {}. status={}", resource.getName(), status); - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(latestByName.right().value()), resource); - componentsUtils.auditResource(responseFormat, user, resource, "", "", AuditingActionEnum.IMPORT_RESOURCE, null); - result = Either.right(responseFormat); - } - return result; - - } - - private Either, ResponseFormat> createResourceByImport(Resource resource, User user, boolean isNormative, boolean isInTransaction, CsarInfo csarInfo) { - log.debug("resource with name {} does not exist. create new resource", resource.getName()); - Either response = validateResourceBeforeCreate(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isInTransaction, csarInfo); - if (response.isRight()) { - return Either.right(response.right().value()); - } - Either createResponse = createResourceByDao(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isNormative, isInTransaction, null); - if (createResponse.isRight()) { - return Either.right(createResponse.right().value()); - } else { - ImmutablePair resourcePair = new ImmutablePair<>(createResponse.left().value(), ActionStatus.CREATED); - ASDCKpiApi.countImportResourcesKPI(); - return Either.left(resourcePair); - - } - } - - public boolean isResourceExist(String resourceName) { - Either latestByName = toscaOperationFacade.getLatestByName(resourceName); - return latestByName.isLeft(); - } - - private Either, ResponseFormat> updateExistingResourceByImport(Resource newResource, Resource oldResource, User user, boolean inTransaction, boolean needLock) { - String lockedResourceId = oldResource.getUniqueId(); - log.debug("found resource: name={}, id={}, version={}, state={}", oldResource.getName(), lockedResourceId, oldResource.getVersion(), oldResource.getLifecycleState()); - Either, ResponseFormat> result = null; - try { - if (needLock) { - Either lockResult = lockComponent(lockedResourceId, oldResource, "Update Resource by Import"); - if (lockResult.isRight()) { - return Either.right(lockResult.right().value()); - } - } - - Either prepareResourceForUpdate = prepareResourceForUpdate(oldResource, user, inTransaction, false); - if (prepareResourceForUpdate.isRight()) { - ResponseFormat responseFormat = prepareResourceForUpdate.right().value(); - log.info("resource {} cannot be updated. reason={}", lockedResourceId, responseFormat.getFormattedMessage()); - componentsUtils.auditResource(responseFormat, user, newResource, oldResource.getLifecycleState().name(), oldResource.getVersion(), AuditingActionEnum.IMPORT_RESOURCE, null); - result = Either.right(prepareResourceForUpdate.right().value()); - return result; - } - oldResource = prepareResourceForUpdate.left().value(); - - mergeOldResourceMetadataWithNew(oldResource, newResource); - - Either validateFieldsResponse = validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction); - if (validateFieldsResponse.isRight()) { - result = Either.right(validateFieldsResponse.right().value()); - return result; - } - - // contact info normalization - newResource.setContactId(newResource.getContactId().toLowerCase()); - // non-updatable fields - 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()); - } - newResource.setAbstract(oldResource.isAbstract()); - - if (newResource.getDerivedFrom() == null || newResource.getDerivedFrom().isEmpty()) { - newResource.setDerivedFrom(oldResource.getDerivedFrom()); - } - // TODO rhalili: handle artifacts here (delete from old resource and - // add for new) - // TODO rbetzer: remove after migration - in case of resources - // created without tosca artifacts - add the placeholders - if (newResource.getToscaArtifacts() == null || newResource.getToscaArtifacts().isEmpty()) { - setToscaArtifactsPlaceHolders(newResource, user); - } - Either overrideResource = toscaOperationFacade.overrideComponent(newResource, oldResource); - - if (overrideResource.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(overrideResource.right().value()), newResource); - componentsUtils.auditResource(responseFormat, user, newResource, newResource.getLifecycleState().name(), newResource.getVersion(), AuditingActionEnum.IMPORT_RESOURCE, null); - result = Either.right(responseFormat); - return result; - } - - log.debug("Resource updated successfully!!!"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK); - componentsUtils.auditResource(responseFormat, user, newResource, oldResource.getLifecycleState().name(), oldResource.getVersion(), AuditingActionEnum.IMPORT_RESOURCE, null); - - ImmutablePair resourcePair = new ImmutablePair<>(overrideResource.left().value(), ActionStatus.OK); - result = Either.left(resourcePair); - return result; - } finally { - if (result == null || result.isRight()) { - BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify"); - log.debug("operation failed. do rollback"); - titanDao.rollback(); - } else if (inTransaction == false) { - log.debug("operation success. do commit"); - titanDao.commit(); - } - if (needLock == true) { - log.debug("unlock resource {}", lockedResourceId); - graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource); - } - } - - } - - /** - * Merge old resource with new. Keep old category and vendor name without change - * - * @param oldResource - * @param newResource - */ - private void mergeOldResourceMetadataWithNew(Resource oldResource, Resource newResource) { - - // keep old category and vendor name without change - // merge the rest of the resource metadata - if (newResource.getTags() == null || newResource.getTags().isEmpty()) { - newResource.setTags(oldResource.getTags()); - } - - if (newResource.getDescription() == null) { - newResource.setDescription(oldResource.getDescription()); - } - - if (newResource.getVendorRelease() == null) { - newResource.setVendorRelease(oldResource.getVendorRelease()); - } - - if (newResource.getResourceVendorModelNumber() == null) { - newResource.setResourceVendorModelNumber(oldResource.getResourceVendorModelNumber()); - } - - if (newResource.getContactId() == null) { - newResource.setContactId(oldResource.getContactId()); - } - - newResource.setCategories(oldResource.getCategories()); - newResource.setVendorName(oldResource.getVendorName()); - } - - private Either prepareResourceForUpdate(Resource latestResource, User user, boolean inTransaction, boolean needLock) { - - Either result = Either.left(latestResource); - // check if user can edit resource - if (!ComponentValidationUtils.canWorkOnResource(latestResource, user.getUserId())) { - // checkout - Either changeState = lifecycleBusinessLogic.changeState(latestResource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, new LifecycleChangeInfoWithAction("update by import"), inTransaction, needLock); - result = changeState; - } - - return result; - } - - public Either validateResourceBeforeCreate(Resource resource, User user, AuditingActionEnum actionEnum, boolean inTransaction, CsarInfo csarInfo) { - - Either eitherValidation = validateResourceFieldsBeforeCreate(user, resource, actionEnum, inTransaction); - if (eitherValidation.isRight()) { - return Either.right(eitherValidation.right().value()); - } - - eitherValidation = validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), resource, actionEnum, inTransaction); - if (eitherValidation.isRight()) { - return Either.right(eitherValidation.right().value()); - } - eitherValidation = validateLifecycleTypesCreate(user, resource, actionEnum); - if (eitherValidation.isRight()) { - return Either.right(eitherValidation.right().value()); - } - eitherValidation = validateResourceType(user, resource, actionEnum); - if (eitherValidation.isRight()) { - return Either.right(eitherValidation.right().value()); - } - - resource.setCreatorUserId(user.getUserId()); - resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName()); - resource.setContactId(resource.getContactId().toLowerCase()); - if (StringUtils.isEmpty(resource.getToscaResourceName()) && !ToscaUtils.isAtomicType(resource)) { - String resourceSystemName; - if(csarInfo != null && 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 Either.left(resource); - } - - private Either validateResourceType(User user, Resource resource, AuditingActionEnum actionEnum) { - Either 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, null); - } - return eitherResult; - } - - private Either validateLifecycleTypesCreate(User user, Resource resource, AuditingActionEnum actionEnum) { - Either eitherResult = Either.left(true); - if (resource.getInterfaces() != null && resource.getInterfaces().size() > 0) { - log.debug("validate interface lifecycle Types Exist"); - Iterator intItr = resource.getInterfaces().values().iterator(); - while (intItr.hasNext() && eitherResult.isLeft()) { - InterfaceDefinition interfaceDefinition = intItr.next(); - String intType = interfaceDefinition.getUniqueId(); - Either 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, null); - } - - } - } - return eitherResult; - } - - private Either validateCapabilityTypesCreate(User user, ICapabilityTypeOperation capabilityTypeOperation, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) { - - Either eitherResult = Either.left(true); - if (resource.getCapabilities() != null && resource.getCapabilities().size() > 0) { - log.debug("validate capability Types Exist - capabilities section"); - - for (Entry> 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; - } - - // @param typeObject- the object to which the validation is done - private Either validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation, Resource resource, List validationObjects, AuditingActionEnum actionEnum, Either eitherResult, String type, - boolean inTransaction) { - Either eitherCapTypeFound = capabilityTypeOperation.getCapabilityType(type, inTransaction); - if (eitherCapTypeFound.isRight()) { - if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) { - BeEcompErrorManager.getInstance().logBeGraphObjectMissingError("Create Resource - validateCapabilityTypesCreate", "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 - validateCapabilityTypesCreate"); - } - 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, null); - } - return eitherResult; - } - - private Either validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation, Resource resource, AuditingActionEnum actionEnum, Either eitherResult, - Entry> typeEntry, boolean inTransaction) { - Either eitherCapTypeFound = capabilityTypeOperation.getCapabilityType(typeEntry.getKey(), inTransaction); - if (eitherCapTypeFound.isRight()) { - if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) { - BeEcompErrorManager.getInstance().logBeGraphObjectMissingError("Create Resource - validateCapabilityTypesCreate", "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 - validateCapabilityTypesCreate"); - } - 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, null); - } - CapabilityTypeDefinition capabilityTypeDefinition = eitherCapTypeFound.left().value(); - if (capabilityTypeDefinition.getProperties() != null) { - for (CapabilityDefinition capDef : typeEntry.getValue()) { - List properties = capDef.getProperties(); - if (properties == null || properties.isEmpty()) { - properties = new ArrayList(); - for (Entry prop : capabilityTypeDefinition.getProperties().entrySet()) { - ComponentInstanceProperty newProp = new ComponentInstanceProperty(prop.getValue()); - properties.add(newProp); - } - } else { - for (Entry prop : capabilityTypeDefinition.getProperties().entrySet()) { - PropertyDefinition porpFromDef = prop.getValue(); - List 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; - } - - public Either createResourceByDao(Resource resource, User user, AuditingActionEnum actionEnum, boolean isNormative, boolean inTransaction, EnumMap additionalParams) { - // create resource - - // lock new resource name in order to avoid creation resource with same - // name - if (inTransaction == false) { - Either lockResult = lockComponentByName(resource.getSystemName(), resource, "Create Resource"); - if (lockResult.isRight()) { - ResponseFormat responseFormat = lockResult.right().value(); - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, additionalParams); - return Either.right(responseFormat); - } - - log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult); - } - try { - if (resource.deriveFromGeneric()) { - Either genericResourceEither = fetchAndSetDerivedFromGenericType(resource); - if (genericResourceEither.isRight()) - return genericResourceEither; - if (resource.shouldGenerateInputs()) - generateInputsFromGenericTypeProperties(resource, genericResourceEither.left().value()); - } - - Either respStatus = createResourceTransaction(resource, user, isNormative, inTransaction); - if (respStatus.isLeft()) { - auditCreateResource(user, respStatus.left().value(), actionEnum, additionalParams); - ASDCKpiApi.countCreatedResourcesKPI(); - } else - componentsUtils.auditResource(respStatus.right().value(), user, resource, "", "", actionEnum, additionalParams); - return respStatus; - - } finally { - if (inTransaction == false) { - graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource); - } - } - } - - private void auditCreateResource(User user, Resource persistedResource, AuditingActionEnum actionEnum, EnumMap additionalParams) { - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED); - componentsUtils.auditResource(responseFormat, user, persistedResource, "", "", actionEnum, additionalParams); - } - - private Either createResourceTransaction(Resource resource, User user, boolean isNormative, boolean inTransaction) { - // validate resource name uniqueness - log.debug("validate resource name"); - Either eitherValidation = toscaOperationFacade.validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType()); - if (eitherValidation.isRight()) { - log.debug("Failed to validate component name {}. Status is {}. ", resource.getName(), eitherValidation.right().value()); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(eitherValidation.right().value())); - return Either.right(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()); - return Either.right(errorResponse); - } - - log.debug("send resource {} to dao for create", resource.getName()); - - createArtifactsPlaceHolderData(resource, user); - - // - - // enrich object - 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.CVFC) - resource.setAbstract(false); - } - - Either createToscaElement = toscaOperationFacade.createToscaComponent(resource); - if (createToscaElement.isLeft()) { - return Either.left(createToscaElement.left().value()); - } - - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(createToscaElement.right().value()), resource); - - return Either.right(responseFormat); - } - - private void createArtifactsPlaceHolderData(Resource resource, User user) { - // create mandatory artifacts - - // TODO it must be removed after that artifact uniqueId creation will be - // moved to ArtifactOperation - // String resourceUniqueId = - // UniqueIdBuilder.buildResourceUniqueId(resource.getResourceName(), - // resource.getResourceVersion()); - - setInformationalArtifactsPlaceHolder(resource, user); - setDeploymentArtifactsPlaceHolder(resource, user); - setToscaArtifactsPlaceHolders(resource, user); - } - - @SuppressWarnings("unchecked") - @Override - public void setDeploymentArtifactsPlaceHolder(Component component, User user) { - Resource resource = (Resource) component; - Map artifactMap = resource.getDeploymentArtifacts(); - if (artifactMap == null) { - artifactMap = new HashMap(); - } - Map deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration().getDeploymentResourceArtifacts(); - if (deploymentResourceArtifacts != null) { - Iterator> iterator = deploymentResourceArtifacts.entrySet().iterator(); - while (iterator.hasNext()) { - Entry currEntry = iterator.next(); - boolean shouldCreateArtifact = true; - Map artifactDetails = (Map) currEntry.getValue(); - Object object = artifactDetails.get(PLACE_HOLDER_RESOURCE_TYPES); - if (object != null) { - List artifactTypes = (List) object; - if (!artifactTypes.contains(resource.getResourceType().name())) { - shouldCreateArtifact = false; - continue; - } - } else { - log.info("resource types for artifact placeholder {} were not defined. default is all resources", currEntry.getKey()); - } - if (shouldCreateArtifact) { - if (artifactsBusinessLogic != null) { - ArtifactDefinition artifactDefinition = artifactsBusinessLogic.createArtifactPlaceHolderInfo(resource.getUniqueId(), currEntry.getKey(), (Map) currEntry.getValue(), user, ArtifactGroupTypeEnum.DEPLOYMENT); - if (artifactDefinition != null && !artifactMap.containsKey(artifactDefinition.getArtifactLabel())) - artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); - } - } - } - } - resource.setDeploymentArtifacts(artifactMap); - } - - @SuppressWarnings("unchecked") - private void setInformationalArtifactsPlaceHolder(Resource resource, User user) { - Map artifactMap = resource.getArtifacts(); - if (artifactMap == null) { - artifactMap = new HashMap(); - } - String resourceUniqueId = resource.getUniqueId(); - List exludeResourceCategory = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceCategory(); - List exludeResourceType = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceType(); - Map informationalResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration().getInformationalResourceArtifacts(); - List categories = resource.getCategories(); - boolean isCreateArtifact = true; - if (exludeResourceCategory != null) { - String category = categories.get(0).getName(); - for (String exlude : exludeResourceCategory) { - if (exlude.equalsIgnoreCase(category)) { - isCreateArtifact = false; - break; - } - } - - } - if (isCreateArtifact && exludeResourceType != null) { - String resourceType = resource.getResourceType().name(); - for (String type : exludeResourceType) { - if (type.equalsIgnoreCase(resourceType)) { - isCreateArtifact = false; - break; - } - } - - } - - if (informationalResourceArtifacts != null && isCreateArtifact) { - Set keys = informationalResourceArtifacts.keySet(); - for (String informationalResourceArtifactName : keys) { - Map artifactInfoMap = (Map) informationalResourceArtifacts.get(informationalResourceArtifactName); - ArtifactDefinition artifactDefinition = artifactsBusinessLogic.createArtifactPlaceHolderInfo(resourceUniqueId, informationalResourceArtifactName, artifactInfoMap, user, ArtifactGroupTypeEnum.INFORMATIONAL); - artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); - - } - } - resource.setArtifacts(artifactMap); - } - - /** - * deleteResource - * - * @param resourceId - * @param user - * @return - */ - public ResponseFormat deleteResource(String resourceId, User user) { - ResponseFormat responseFormat; - Either eitherCreator = validateUserExists(user, "Delete Resource", false); - if (eitherCreator.isRight()) { - return eitherCreator.right().value(); - } - - Either resourceStatus = toscaOperationFacade.getToscaElement(resourceId); - if (resourceStatus.isRight()) { - log.debug("failed to get resource {}", resourceId); - return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), ""); - } - - Resource resource = resourceStatus.left().value(); - - StorageOperationStatus result = StorageOperationStatus.OK; - Either lockResult = lockComponent(resourceId, resource, "Mark resource to delete"); - if (lockResult.isRight()) { - result = StorageOperationStatus.GENERAL_ERROR; - return componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); - } - - try { - - result = markComponentToDelete(resource); - if (result.equals(StorageOperationStatus.OK)) { - responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT); - } else { - ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result); - responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName()); - } - return responseFormat; - - } finally { - if (result == null || !result.equals(StorageOperationStatus.OK)) { - log.warn("operation failed. do rollback"); - titanDao.rollback(); - } else { - log.debug("operation success. do commit"); - titanDao.commit(); - } - graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource); - } - - } - - public ResponseFormat deleteResourceByNameAndVersion(String resourceName, String version, User user) { - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT); - Either eitherCreator = validateUserExists(user, "Delete Resource", false); - if (eitherCreator.isRight()) { - return eitherCreator.right().value(); - } - - // Resource resource = null; - Resource resource = null; - StorageOperationStatus result = StorageOperationStatus.OK; - try { - - Either resourceStatus = toscaOperationFacade.getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, version); - if (resourceStatus.isRight()) { - log.debug("failed to get resource {} version {}", resourceName, version); - return componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), resourceName); - } - - resource = resourceStatus.left().value(); - - } finally { - if (result == null || !result.equals(StorageOperationStatus.OK)) { - log.warn("operation failed. do rollback"); - titanDao.rollback(); - ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result); - responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resourceName); - } else { - log.debug("operation success. do commit"); - titanDao.commit(); - } - } - if (resource != null) { - Either lockResult = lockComponent(resource.getUniqueId(), resource, "Delete Resource"); - if (lockResult.isRight()) { - result = StorageOperationStatus.GENERAL_ERROR; - return componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); - } - try { - result = markComponentToDelete(resource); - if (!result.equals(StorageOperationStatus.OK)) { - ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result); - responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName()); - return responseFormat; - } - - } finally { - if (result == null || !result.equals(StorageOperationStatus.OK)) { - log.warn("operation failed. do rollback"); - titanDao.rollback(); - } else { - log.debug("operation success. do commit"); - titanDao.commit(); - } - graphLockOperation.unlockComponent(resource.getUniqueId(), NodeTypeEnum.Resource); - } - } - return responseFormat; - } - - public Either getResource(String resourceId, User user) { - - if (user != null) { - Either eitherCreator = validateUserExists(user, "Create Resource", false); - if (eitherCreator.isRight()) { - return Either.right(eitherCreator.right().value()); - } - } - - // IResourceOperation dataModel = getResourceOperation(); - Either storageStatus = toscaOperationFacade.getToscaElement(resourceId); - if (storageStatus.isRight()) { - log.debug("failed to get resource by id {}", resourceId); - return Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), resourceId)); - } - if(!(storageStatus.left().value() instanceof Resource)){ - return Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND), resourceId)); - } - return Either.left(storageStatus.left().value()); - - } - - public Either getResourceByNameAndVersion(String resourceName, String resourceVersion, String userId) { - - Either resp = validateUserExists(userId, "get Resource By Name And Version", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } - - Either getResource = toscaOperationFacade.getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, resourceVersion); - if (getResource.isRight()) { - log.debug("failed to get resource by name {} and version {}", resourceName, resourceVersion); - return Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResource.right().value()), resourceName)); - } - return Either.left(getResource.left().value()); - } - - /** - * updateResourceMetadata - * - * @param user - modifier data (userId) - * @param inTransaction TODO - * @param resourceIdToUpdate - the resource identifier - * @param newResource - * @return Either - */ - public Either updateResourceMetadata(String resourceIdToUpdate, Resource newResource, Resource currentResource, User user, boolean inTransaction) { - - Either resp = validateUserExists(user.getUserId(), "update Resource Metadata", false); - if (resp.isRight()) { - return Either.right(resp.right().value()); - } - - // IResourceOperation dataModel = getResourceOperation(); - log.debug("Get resource with id {}", resourceIdToUpdate); - boolean needToUnlock = false; - boolean rollbackNeeded = true; - - try { - // Either storageStatus = - // dataModel.getResource_tx(resourceIdToUpdate, false); - if (currentResource == null) { - Either storageStatus = toscaOperationFacade.getToscaElement(resourceIdToUpdate); - if (storageStatus.isRight()) { - return Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), "")); - } - - currentResource = storageStatus.left().value(); - } - // verify that resource is checked-out and the user is the last - // updater - if (!ComponentValidationUtils.canWorkOnResource(currentResource, user.getUserId())) { - return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)); - } - - // lock resource - StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceIdToUpdate, NodeTypeEnum.Resource); - if (!lockResult.equals(StorageOperationStatus.OK)) { - BeEcompErrorManager.getInstance().logBeFailedLockObjectError("Upload Artifact - lock ", NodeTypeEnum.Resource.getName(), resourceIdToUpdate); - log.debug("Failed to lock resource: {}, error - {}", resourceIdToUpdate, lockResult); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockResult)); - return Either.right(responseFormat); - } - - needToUnlock = true; - - // critical section starts here - // convert json to object - - // Update and updated resource must have a non-empty "derivedFrom" - // list - // This code is not called from import resources, because of root - // VF "derivedFrom" should be null (or ignored) - if (ToscaUtils.isAtomicType(currentResource)) { - Either derivedFromNotEmptyEither = validateDerivedFromNotEmpty(null, newResource, null); - if (derivedFromNotEmptyEither.isRight()) { - log.debug("for updated resource {}, derived from field is empty", newResource.getName()); - return Either.right(derivedFromNotEmptyEither.right().value()); - } - - derivedFromNotEmptyEither = validateDerivedFromNotEmpty(null, currentResource, null); - if (derivedFromNotEmptyEither.isRight()) { - log.debug("for current resource {}, derived from field is empty", currentResource.getName()); - return Either.right(derivedFromNotEmptyEither.right().value()); - } - } else { - newResource.setDerivedFrom(null); - } - - Either dataModelResponse = updateResourceMetadata(resourceIdToUpdate, newResource, user, currentResource, false, true); - if (dataModelResponse.isRight()) { - log.debug("failed to update resource metadata!!!"); - rollbackNeeded = true; - return Either.right(dataModelResponse.right().value()); - } - - log.debug("Resource metadata updated successfully!!!"); - rollbackNeeded = false; - return Either.left(dataModelResponse.left().value()); - - } finally { - if (!inTransaction) { - if (rollbackNeeded) { - titanDao.rollback(); - } else { - titanDao.commit(); - } - } - - if (needToUnlock) { - graphLockOperation.unlockComponent(resourceIdToUpdate, NodeTypeEnum.Resource); - } - } - } - - private Either,Boolean> updateComponentGroupName(String replacePattern , String with ,List oldGroup){ - if ( oldGroup==null || with==null || replacePattern==null || with.isEmpty() || replacePattern.isEmpty()){ - if (log.isInfoEnabled()) - log.info("cannot update group name , invalid args -> replacePattern:{} , with:{} , oldGroup:{}" , replacePattern, with, oldGroup == null ? null : " < size : "+oldGroup.size()+" >" ); - return Either.right(false); - } - List list = oldGroup.stream().map( group -> new GroupDefinition(group)).collect(Collectors.toList()); - for ( GroupDefinition group : list) { - if ( group!=null && group.isSamePrefix( replacePattern ) ){ - String prefix = group.getName().substring( 0, replacePattern.length() ); - String newGroupName = group.getName().replaceFirst(prefix , with); - group.setName(newGroupName); - //String newUid = group.getUniqueId().replaceFirst(prefix , with); //removing this will also change the unique id - //group.setUniqueId(newUid); - } - } - return Either.left(list); - } - - private boolean isComponentNameChanged(Resource newResource,Resource oldResource){ - if (newResource!=null && oldResource!=null){ //TODO - must protect all chain against null , use optional - String futureName = newResource.getComponentMetadataDefinition().getMetadataDataDefinition().getName(); - String oldName = oldResource.getComponentMetadataDefinition().getMetadataDataDefinition().getName(); - return !oldName.equals(futureName); - } - return false; - } - - private Either updateResourceMetadata(String resourceIdToUpdate, Resource newResource, User user, Resource currentResource, boolean shouldLock, boolean inTransaction) { - //region -> Update groups name for newResource - if ( isComponentNameChanged( currentResource , newResource) ){ - String replacePattern = Optional.ofNullable( //get currentResource name from metadata - Optional.ofNullable( Optional.ofNullable( currentResource ) - .orElse(null).getComponentMetadataDefinition() ) - .orElse(null).getMetadataDataDefinition() ) - .orElse(null).getName(); - String with = Optional.ofNullable( //get newResource name from metadata - Optional.ofNullable( Optional.ofNullable( newResource ) - .orElse(null).getComponentMetadataDefinition() ) - .orElse(null).getMetadataDataDefinition() ) - .orElse(null).getName(); - if ( with != null && replacePattern != null ){ - Either,Boolean> result = updateComponentGroupName( replacePattern , with ,currentResource.getGroups()); - if (result.isLeft()) - newResource.setGroups( (List)result.left().value() ); - } - } - //endregion - - Either validateResourceFields = validateResourceFieldsBeforeUpdate(currentResource, newResource, inTransaction); - if (validateResourceFields.isRight()) { - return Either.right(validateResourceFields.right().value()); - } - // Setting last updater and uniqueId - newResource.setContactId(newResource.getContactId().toLowerCase()); - newResource.setLastUpdaterUserId(user.getUserId()); - newResource.setUniqueId(resourceIdToUpdate); - // Cannot set highest version through UI - newResource.setHighestVersion(currentResource.isHighestVersion()); - newResource.setCreationDate(currentResource.getCreationDate()); - - Either processUpdateOfDerivedFrom = processUpdateOfDerivedFrom(currentResource, newResource, user.getUserId(), shouldLock, inTransaction); - - if (processUpdateOfDerivedFrom.isRight()) { - log.debug("Couldn't update derived from for resource {}", resourceIdToUpdate); - return Either.right(processUpdateOfDerivedFrom.right().value()); - } - - log.debug("send resource {} to dao for update", newResource.getUniqueId()); - if (newResource!=null && newResource.getGroups()!=null){ - for ( GroupDefinition group : newResource.getGroups() ){ - if (newResource.getComponentMetadataDefinition()!=null && newResource.getComponentMetadataDefinition().getMetadataDataDefinition()!=null) - groupBusinessLogic.validateAndUpdateGroupMetadata( - newResource.getComponentMetadataDefinition().getMetadataDataDefinition().getUniqueId() , - user, - ComponentTypeEnum.RESOURCE_INSTANCE, - group, - true , - false) ; - } - } - Either dataModelResponse = toscaOperationFacade.updateToscaElement(newResource); - - if (dataModelResponse.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()), newResource); - return Either.right(responseFormat); - } else if (dataModelResponse.left().value() == null) { - log.debug("No response from updateResource"); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); - } - return Either.left(dataModelResponse.left().value()); - } - - /** - * validateResourceFieldsBeforeCreate - * - * @param user - modifier data (userId) - * @param dataModel - IResourceOperation for resource crud - * @param resource - Resource object to validate - * @return Either - */ - private Either validateResourceFieldsBeforeCreate(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) { - Either componentsFieldsValidation = validateComponentFieldsBeforeCreate(user, resource, actionEnum); - if (componentsFieldsValidation.isRight()) { - return componentsFieldsValidation; - } - - // validate name - - /* - * log.debug("validate resource name"); Either eitherValidation = validateComponentName(user, resource, actionEnum); if (eitherValidation.isRight()) { return eitherValidation; } - * - * // validate description log.debug("validate description"); eitherValidation = validateDescriptionAndCleanup(user, resource, actionEnum); if (eitherValidation.isRight()) { return eitherValidation; } - */ - - // validate icon - /* - * log.debug("validate icon"); eitherValidation = validateIcon(user, resource, actionEnum); if (eitherValidation.isRight()) { return eitherValidation; } - */ - - // validate tags - /* - * log.debug("validate tags"); eitherValidation = validateTagsListAndRemoveDuplicates(user, resource, actionEnum); if (eitherValidation.isRight()) { return eitherValidation; } - */ - - // validate category - log.debug("validate category"); - Either eitherValidation = validateCategory(user, resource, actionEnum, inTransaction); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - // validate vendor name & release & model number - log.debug("validate vendor name"); - eitherValidation = validateVendorName(user, resource, actionEnum); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - log.debug("validate vendor release"); - eitherValidation = validateVendorReleaseName(user, resource, actionEnum); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - log.debug("validate resource vendor model number"); - eitherValidation = validateResourceVendorModelNumber(user, resource, actionEnum); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - // validate contact info - /* - * log.debug("validate contact info"); eitherValidation = validateContactIdContactId(user, resource, actionEnum); if (eitherValidation.isRight()) { return eitherValidation; } - */ - - // validate cost - log.debug("validate cost"); - eitherValidation = validateCost(user, resource, actionEnum); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - // validate licenseType - log.debug("validate licenseType"); - eitherValidation = validateLicenseType(user, resource, actionEnum); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - // validate template (derived from) - log.debug("validate derived from"); - if (!ToscaUtils.isAtomicType(resource) && resource.getResourceType() != ResourceTypeEnum.CVFC) { - resource.setDerivedFrom(null); - } - eitherValidation = validateDerivedFromExist(user, resource, actionEnum); - if (eitherValidation.isRight()) { - return Either.right(eitherValidation.right().value()); - } - - // warn about non-updatable fields - checkComponentFieldsForOverrideAttempt(resource); - String currentCreatorFullName = resource.getCreatorFullName(); - if (currentCreatorFullName != null) { - log.warn("Resource Creator fullname is automatically set and cannot be updated"); - } - - String currentLastUpdaterFullName = resource.getLastUpdaterFullName(); - if (currentLastUpdaterFullName != null) { - log.warn("Resource LastUpdater fullname is automatically set and cannot be updated"); - } - - Long currentLastUpdateDate = resource.getLastUpdateDate(); - if (currentLastUpdateDate != null) { - log.warn("Resource last update date is automatically set and cannot be updated"); - } - - Boolean currentAbstract = resource.isAbstract(); - if (currentAbstract != null) { - log.warn("Resource abstract is automatically set and cannot be updated"); - } - - return Either.left(true); - } - - /** - * validateResourceFieldsBeforeUpdate - * - * @param currentResource - Resource object to validate - * @return Either - */ - private Either validateResourceFieldsBeforeUpdate(Resource currentResource, Resource updateInfoResource, boolean inTransaction) { - - boolean hasBeenCertified = ValidationUtils.hasBeenCertified(currentResource.getVersion()); - - // validate resource name - log.debug("validate resource name before update"); - Either eitherValidation = validateResourceName(currentResource, updateInfoResource, hasBeenCertified); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - // validate description - log.debug("validate description before update"); - eitherValidation = validateDescriptionAndCleanup(null, updateInfoResource, null); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - log.debug("validate icon before update"); - eitherValidation = validateIcon(currentResource, updateInfoResource, hasBeenCertified); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - log.debug("validate tags before update"); - eitherValidation = validateTagsListAndRemoveDuplicates(null, updateInfoResource, null); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - log.debug("validate vendor name before update"); - eitherValidation = validateVendorName(currentResource, updateInfoResource, hasBeenCertified); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - log.debug("validate resource vendor model number before update"); - eitherValidation = validateResourceVendorModelNumber(currentResource, updateInfoResource); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - - log.debug("validate vendor release before update"); - eitherValidation = validateVendorReleaseName(null, updateInfoResource, null); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - log.debug("validate contact info before update"); - eitherValidation = validateContactId(null, updateInfoResource, null); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - log.debug("validate derived before update"); - eitherValidation = validateDerivedFromDuringUpdate(currentResource, updateInfoResource, hasBeenCertified); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - log.debug("validate category before update"); - eitherValidation = validateCategory(currentResource, updateInfoResource, hasBeenCertified, inTransaction); - if (eitherValidation.isRight()) { - return eitherValidation; - } - - // warn about non-updatable fields - String currentResourceVersion = currentResource.getVersion(); - String updatedResourceVersion = updateInfoResource.getVersion(); - - if ((updatedResourceVersion != null) && (!updatedResourceVersion.equals(currentResourceVersion))) { - log.warn("Resource version is automatically set and cannot be updated"); - } - - String currentCreatorUserId = currentResource.getCreatorUserId(); - String updatedCreatorUserId = updateInfoResource.getCreatorUserId(); - - if ((updatedCreatorUserId != null) && (!updatedCreatorUserId.equals(currentCreatorUserId))) { - log.warn("Resource Creator UserId is automatically set and cannot be updated"); - } - - String currentCreatorFullName = currentResource.getCreatorFullName(); - String updatedCreatorFullName = updateInfoResource.getCreatorFullName(); - - if ((updatedCreatorFullName != null) && (!updatedCreatorFullName.equals(currentCreatorFullName))) { - log.warn("Resource Creator fullname is automatically set and cannot be updated"); - } - - String currentLastUpdaterUserId = currentResource.getLastUpdaterUserId(); - String updatedLastUpdaterUserId = updateInfoResource.getLastUpdaterUserId(); - - if ((updatedLastUpdaterUserId != null) && (!updatedLastUpdaterUserId.equals(currentLastUpdaterUserId))) { - log.warn("Resource LastUpdater userId is automatically set and cannot be updated"); - } - - String currentLastUpdaterFullName = currentResource.getLastUpdaterFullName(); - String updatedLastUpdaterFullName = updateInfoResource.getLastUpdaterFullName(); - - if ((updatedLastUpdaterFullName != null) && (!updatedLastUpdaterFullName.equals(currentLastUpdaterFullName))) { - log.warn("Resource LastUpdater fullname is automatically set and cannot be updated"); - } - - Long currentCreationDate = currentResource.getCreationDate(); - Long updatedCreationDate = updateInfoResource.getCreationDate(); - - if ((updatedCreationDate != null) && (!updatedCreationDate.equals(currentCreationDate))) { - log.warn("Resource Creation date is automatically set and cannot be updated"); - } - - Long currentLastUpdateDate = currentResource.getLastUpdateDate(); - Long updatedLastUpdateDate = updateInfoResource.getLastUpdateDate(); - - if ((updatedLastUpdateDate != null) && (!updatedLastUpdateDate.equals(currentLastUpdateDate))) { - log.warn("Resource last update date is automatically set and cannot be updated"); - } - - LifecycleStateEnum currentLifecycleState = currentResource.getLifecycleState(); - LifecycleStateEnum updatedLifecycleState = updateInfoResource.getLifecycleState(); - - if ((updatedLifecycleState != null) && (!updatedLifecycleState.equals(currentLifecycleState))) { - log.warn("Resource lifecycle state date is automatically set and cannot be updated"); - } - - Boolean currentAbstract = currentResource.isAbstract(); - Boolean updatedAbstract = updateInfoResource.isAbstract(); - - if ((updatedAbstract != null) && (!updatedAbstract.equals(currentAbstract))) { - log.warn("Resource abstract is automatically set and cannot be updated"); - } - - Boolean currentHighestVersion = currentResource.isHighestVersion(); - Boolean updatedHighestVersion = updateInfoResource.isHighestVersion(); - - if ((updatedHighestVersion != null) && (!updatedHighestVersion.equals(currentHighestVersion))) { - log.warn("Resource highest version is automatically set and cannot be updated"); - } - - String currentUuid = currentResource.getUUID(); - String updatedUuid = updateInfoResource.getUUID(); - - if ((updatedUuid != null) && (!updatedUuid.equals(currentUuid))) { - log.warn("Resource UUID is automatically set and cannot be updated"); - } - - ResourceTypeEnum currentResourceType = currentResource.getResourceType(); - ResourceTypeEnum updatedResourceType = updateInfoResource.getResourceType(); - - if ((updatedResourceType != null) && (!updatedResourceType.equals(currentResourceType))) { - log.warn("Resource Type cannot be updated"); - - } - updateInfoResource.setResourceType(currentResource.getResourceType()); - - String currentInvariantUuid = currentResource.getInvariantUUID(); - String updatedInvariantUuid = updateInfoResource.getInvariantUUID(); - - if ((updatedInvariantUuid != null) && (!updatedInvariantUuid.equals(currentInvariantUuid))) { - log.warn("Resource invariant UUID is automatically set and cannot be updated"); - updateInfoResource.setInvariantUUID(currentInvariantUuid); - } - return Either.left(true); - } - - /* - * private Either validateResourceName(User user, Resource resource, AuditingActionEnum actionEnum) { log.debug("validate resource name is not empty"); String resourceName = resource.getResourceName(); - * - * if (!ValidationUtils.validateStringNotEmpty(resourceName)) { log.debug("Resource name is empty"); ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_COMPONENT_NAME, ComponentTypeEnum.RESOURCE.getValue()); - * componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); return Either.right(responseFormat); } - * - * if (!ValidationUtils.validateResourceNameLength(resourceName)) { log.debug("Resource name is exceeds max length {} ", ValidationUtils.RESOURCE_NAME_MAX_LENGTH); ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus. - * COMPONENT_NAME_EXCEEDS_LIMIT, ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.RESOURCE_NAME_MAX_LENGTH); componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); return Either.right(responseFormat); - * } - * - * if (!ValidationUtils.validateResourceName(resourceName)) { log.debug("Resource name {} has invalid format", resourceName); ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_COMPONENT_NAME, - * ComponentTypeEnum.RESOURCE.getValue()); componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); return Either.right(responseFormat); } resource.setNormalizedName(ValidationUtils.normaliseComponentName( - * resourceName)); resource.setSystemName(ValidationUtils.convertToSystemName(resourceName)) ; - * - * return Either.left(true); } - */ - - private Either validateResourceName(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) { - String resourceNameUpdated = updateInfoResource.getName(); - String resourceNameCurrent = currentResource.getName(); - if (!resourceNameCurrent.equals(resourceNameUpdated)) { - if (!hasBeenCertified) { - Either validateResourceNameResponse = validateComponentName(null, updateInfoResource, null); - if (validateResourceNameResponse.isRight()) { - ResponseFormat errorResponse = validateResourceNameResponse.right().value(); - return Either.right(errorResponse); - } - validateResourceNameResponse = validateResourceNameExists(updateInfoResource); - if (validateResourceNameResponse.isRight()) { - ResponseFormat errorResponse = validateResourceNameResponse.right().value(); - return Either.right(errorResponse); - } - 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); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED); - return Either.right(errorResponse); - } - } - return Either.left(true); - } - - private Either validateIcon(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) { - String iconUpdated = updateInfoResource.getIcon(); - String iconCurrent = currentResource.getIcon(); - if (!iconCurrent.equals(iconUpdated)) { - if (!hasBeenCertified) { - Either validateIcon = validateIcon(null, updateInfoResource, null); - if (validateIcon.isRight()) { - ResponseFormat errorResponse = validateIcon.right().value(); - return Either.right(errorResponse); - } - } else { - log.info("Icon {} cannot be updated once the resource has been certified once.", iconUpdated); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_ICON_CANNOT_BE_CHANGED); - return Either.right(errorResponse); - } - } - return Either.left(true); - } - - private Either validateVendorName(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) { - String vendorNameUpdated = updateInfoResource.getVendorName(); - String vendorNameCurrent = currentResource.getVendorName(); - if (!vendorNameCurrent.equals(vendorNameUpdated)) { - if (!hasBeenCertified) { - Either validateVendorName = validateVendorName(null, updateInfoResource, null); - if (validateVendorName.isRight()) { - ResponseFormat errorResponse = validateVendorName.right().value(); - return Either.right(errorResponse); - } - } else { - log.info("Vendor name {} cannot be updated once the resource has been certified once.", vendorNameUpdated); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_VENDOR_NAME_CANNOT_BE_CHANGED); - return Either.right(errorResponse); - } - } - return Either.left(true); - } - - private Either validateResourceVendorModelNumber(Resource currentResource, Resource updateInfoResource) { - String updatedResourceVendorModelNumber = updateInfoResource.getResourceVendorModelNumber(); - String currentResourceVendorModelNumber = currentResource.getResourceVendorModelNumber(); - if (!currentResourceVendorModelNumber.equals(updatedResourceVendorModelNumber)) { - Either validateResourceVendorModelNumber = validateResourceVendorModelNumber(null, updateInfoResource, null); - if (validateResourceVendorModelNumber.isRight()) { - ResponseFormat errorResponse = validateResourceVendorModelNumber.right().value(); - return Either.right(errorResponse); - } - } - return Either.left(true); - } - - private Either validateCategory(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified, boolean inTransaction) { - Either validateCategoryName = validateCategory(null, updateInfoResource, null, inTransaction); - if (validateCategoryName.isRight()) { - ResponseFormat errorResponse = validateCategoryName.right().value(); - return Either.right(errorResponse); - } - if (hasBeenCertified) { - CategoryDefinition currentCategory = currentResource.getCategories().get(0); - SubCategoryDefinition currentSubCategory = currentCategory.getSubcategories().get(0); - CategoryDefinition updateCategory = updateInfoResource.getCategories().get(0); - SubCategoryDefinition updtaeSubCategory = updateCategory.getSubcategories().get(0); - if (!currentCategory.getName().equals(updateCategory.getName()) || !currentSubCategory.getName().equals(updtaeSubCategory.getName())) { - log.info("Category {} cannot be updated once the resource has been certified once.", currentResource.getCategories()); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CATEGORY_CANNOT_BE_CHANGED); - return Either.right(errorResponse); - } - } - return Either.left(true); - } - - private Either validateDerivedFromDuringUpdate(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) { - - List currentDerivedFrom = currentResource.getDerivedFrom(); - List 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) { - Either validateDerivedFromExistsEither = validateDerivedFromExist(null, updateInfoResource, null); - if (validateDerivedFromExistsEither.isRight()) { - return validateDerivedFromExistsEither; - } - } else { - Either 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; - } - } - } else { - // For derived from, we must know whether it was actually changed, - // otherwise we must do no action. - // Due to changes it inflicts on data model (remove artifacts, - // properties...), it's not like a flat field which can be - // overwritten if not changed. - // So we must indicate that derived from is not changed - updateInfoResource.setDerivedFrom(null); - } - return Either.left(true); - } - - private Either validateDerivedFromExist(User user, Resource resource, AuditingActionEnum actionEnum) { - - if (resource.getDerivedFrom() == null || resource.getDerivedFrom().isEmpty()) { - return Either.left(true); - } - - // IResourceOperation resourceOperation = getResourceOperation(); - - String templateName = resource.getDerivedFrom().get(0); - - Either 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.name()); - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus), resource); - log.trace("audit before sending response"); - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); - return Either.right(responseFormat); - } 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, null); - - return Either.right(responseFormat); - - } - return Either.left(true); - } - - // Tal G for extending inheritance US815447 - private Either validateDerivedFromExtending(User user, Resource currentResource, Resource updateInfoResource, AuditingActionEnum actionEnum) { - // If updated resource is not deriving, should fail validation - /* - * if (currentResource.getDerivedFrom() == null || currentResource.getDerivedFrom().isEmpty()) { return Either.left(false); } - */ - // If current resource is deriving from certain type and it is updated to not deriving, should fail validation - /* - * if (updateInfoResource.getDerivedFrom() == null || updateInfoResource.getDerivedFrom().isEmpty()) { return Either.left(false); } - */ - String currentTemplateName = currentResource.getDerivedFrom().get(0); - String updatedTemplateName = updateInfoResource.getDerivedFrom().get(0); - - Either dataModelResponse = toscaOperationFacade.validateToscaResourceNameExtends(currentTemplateName, updatedTemplateName); - if (dataModelResponse.isRight()) { - StorageOperationStatus storageStatus = dataModelResponse.right().value(); - BeEcompErrorManager.getInstance().logBeDaoSystemError("Create/Update Resource - validateDerivingFromExtendingType"); - log.debug("request to data model failed with error: {}", storageStatus.name()); - ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus), currentResource); - log.trace("audit before sending response"); - componentsUtils.auditResource(responseFormat, user, currentResource, "", "", actionEnum, null); - 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, null); - - return Either.right(responseFormat); - - } - return Either.left(true); - } - - public Either 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, null); - - return Either.right(responseFormat); - } - return Either.left(true); - } - - private Either validateResourceNameExists(Resource resource) { - - Either resourceOperationResponse = toscaOperationFacade.validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType()); - if (resourceOperationResponse.isLeft()) { - if (!resourceOperationResponse.left().value()) { - return Either.left(false); - } else { - log.debug("resource with name: {}, already exists", resource.getName()); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(), resource.getName()); - return Either.right(errorResponse); - } - } - log.debug("error while validateResourceNameExists for resource: {}", resource.getName()); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceOperationResponse.right().value())); - return Either.right(errorResponse); - } - - /* - * private Either validateTagsListAndRemoveDuplicates(User user, Resource resource, AuditingActionEnum actionEnum) { List tagsList = resource.getTags(); - * - * Either validateTags = validateResourceTags(tagsList, resource.getResourceName()); if (validateTags.isRight()) { ResponseFormat responseFormat = validateTags.right().value(); - * componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); return Either.right(responseFormat); } ValidationUtils.removeDuplicateFromList(tagsList); return Either.left(true); - * - * } - * - * private Either validateResourceTags(List tags, String resourceName) { log.debug("validate resource tags"); boolean includesResourceName = false; int tagListSize = 0; if (tags != null && !tags.isEmpty()) { for - * (String tag : tags) { if (!ValidationUtils.validateTagLength(tag)) { log.debug("tag length exceeds limit {}", ValidationUtils.TAG_MAX_LENGTH); return Either.right(componentsUtils.getResponseFormat(ActionStatus. - * COMPONENT_SINGLE_TAG_EXCEED_LIMIT, "" + ValidationUtils.TAG_MAX_LENGTH)); } if (ValidationUtils.validateComponentNamePattern(tag)) { if (!includesResourceName) { includesResourceName = resourceName.equals(tag); } } else { - * log.debug("invalid tag {}", tag); return Either.right(componentsUtils.getResponseFormat(ActionStatus. COMPONENT_INVALID_TAG)); } tagListSize += tag.length() + 1; } if (!includesResourceName) { log.debug( "tags must include resource name"); - * return Either.right(componentsUtils.getResponseFormat(ActionStatus. COMPONENT_INVALID_TAGS_NO_COMP_NAME)); } if (!ValidationUtils.validateTagListLength(tagListSize)) { log.debug( "overall tags length {}, exceeds limit {}", tagListSize, - * ValidationUtils.TAG_LIST_MAX_LENGTH); return Either.right(componentsUtils.getResponseFormat(ActionStatus. COMPONENT_TAGS_EXCEED_LIMIT, "" + ValidationUtils.TAG_LIST_MAX_LENGTH)); } return Either.left(true); } - * - * return Either.right(componentsUtils.getResponseFormat(ActionStatus. COMPONENT_MISSING_TAGS)); } - */ - - private Either validateCategory(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) { - - List categories = resource.getCategories(); - if (categories == null || categories.size() == 0) { - log.debug("Resource category is empty"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue()); - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); - return Either.right(responseFormat); - } - if (categories.size() > 1) { - log.debug("Must be only one category for resource"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_TOO_MUCH_CATEGORIES, ComponentTypeEnum.RESOURCE.getValue()); - return Either.right(responseFormat); - } - CategoryDefinition category = categories.get(0); - List subcategories = category.getSubcategories(); - if (subcategories == null || subcategories.size() == 0) { - log.debug("Missinig subcategory for resource"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_SUBCATEGORY); - return Either.right(responseFormat); - } - if (subcategories.size() > 1) { - log.debug("Must be only one sub category for resource"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_TOO_MUCH_SUBCATEGORIES); - return Either.right(responseFormat); - } - - SubCategoryDefinition subcategory = subcategories.get(0); - - if (!ValidationUtils.validateStringNotEmpty(category.getName())) { - log.debug("Resource category is empty"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue()); - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); - return Either.right(responseFormat); - } - if (!ValidationUtils.validateStringNotEmpty(subcategory.getName())) { - log.debug("Resource category is empty"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue()); - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); - return Either.right(responseFormat); - } - - Either validateCategory = validateCategoryListed(category, subcategory, inTransaction); - if (validateCategory.isRight()) { - ResponseFormat responseFormat = validateCategory.right().value(); - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); - return Either.right(responseFormat); - } - - return Either.left(true); - } - - private Either validateCategoryListed(CategoryDefinition category, SubCategoryDefinition subcategory, boolean inTransaction) { - if (category != null && subcategory != null) { - log.debug("validating resource category {} against valid categories list", category); - Either, ActionStatus> categories = elementDao.getAllCategories(NodeTypeEnum.ResourceNewCategory, inTransaction); - if (categories.isRight()) { - log.debug("failed to retrive resource categories from Titan"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(categories.right().value()); - return Either.right(responseFormat); - } - List categoryList = categories.left().value(); - for (CategoryDefinition cat : categoryList) { - if (cat.getName().equals(category.getName())) { - for (SubCategoryDefinition subcat : cat.getSubcategories()) { - if (subcat.getName().equals(subcategory.getName())) { - return Either.left(true); - } - } - log.debug("SubCategory {} is not part of resource category group. Resource subcategory valid values are {}", subcategory, cat.getSubcategories()); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue())); - } - } - log.debug("Category {} is not part of resource category group. Resource category valid values are {}", category, categoryList); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue())); - } - return Either.left(false); - } - - public Either 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, null); - return Either.right(errorResponse); - } - - Either validateVendorReleaseResponse = validateVendorReleaseName(vendorRelease); - if (validateVendorReleaseResponse.isRight()) { - ResponseFormat responseFormat = validateVendorReleaseResponse.right().value(); - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); - } - return validateVendorReleaseResponse; - } - - public Either validateVendorReleaseName(String vendorRelease) { - 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); - return Either.right(errorResponse); - } - - if (!ValidationUtils.validateVendorRelease(vendorRelease)) { - log.info("vendor release is not valid."); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_RELEASE); - return Either.right(errorResponse); - } - return Either.left(true); - } - return Either.left(false); - - } - - private Either 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, null); - return Either.right(errorResponse); - } - - Either validateVendorNameResponse = validateVendorName(vendorName); - if (validateVendorNameResponse.isRight()) { - ResponseFormat responseFormat = validateVendorNameResponse.right().value(); - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); - } - return validateVendorNameResponse; - - } - - private Either validateResourceVendorModelNumber(User user, Resource resource, AuditingActionEnum actionEnum) { - String resourceVendorModelNumber = resource.getResourceVendorModelNumber(); - Either validateResourceVendorModelNumber = validateResourceVendorModelNumber(resourceVendorModelNumber); - if (validateResourceVendorModelNumber.isRight()) { - ResponseFormat responseFormat = validateResourceVendorModelNumber.right().value(); - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); - } - return validateResourceVendorModelNumber; - - } - - - private Either validateVendorName(String vendorName) { - 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); - return Either.right(errorResponse); - } - - if (!ValidationUtils.validateVendorName(vendorName)) { - log.info("vendor name is not valid."); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_NAME); - return Either.right(errorResponse); - } - return Either.left(true); - - } - return Either.left(false); - - } - - private Either validateResourceVendorModelNumber(String resourceVendorModelNumber) { - if (resourceVendorModelNumber.equals("")) { - return Either.left(true); - } else { - 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); - return Either.right(errorResponse); - } - // 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); - return Either.right(errorResponse); - } - return Either.left(true); - } - } - - - /* - * private Either validateDescriptionAndCleanup(User user, Resource resource, AuditingActionEnum actionEnum) { String description = resource.getDescription(); if (!ValidationUtils.validateStringNotEmpty(description)) { - * log.debug("Resource description is empty"); ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus. COMPONENT_MISSING_DESCRIPTION, ComponentTypeEnum.RESOURCE.getValue()); componentsUtils.auditResource(errorResponse, - * user, resource, "", "", actionEnum, null); return Either.right(errorResponse); } - * - * description = ValidationUtils.removeNoneUtf8Chars(description); description = ValidationUtils.removeHtmlTags(description); description = ValidationUtils.normaliseWhitespace(description); description = ValidationUtils.stripOctets(description); - * - * Either validatDescription = validateResourceDescription(description); if (validatDescription.isRight()) { ResponseFormat responseFormat = validatDescription.right().value(); - * componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); return Either.right(responseFormat); } resource.setDescription(description); return Either.left(true); } - * - * private Either validateResourceDescription(String description) { if (description != null) { if (!ValidationUtils.validateDescriptionLength(description)) { return - * Either.right(componentsUtils.getResponseFormat(ActionStatus. COMPONENT_DESCRIPTION_EXCEEDS_LIMIT, ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH)); } - * - * if (!ValidationUtils.validateIsEnglish(description)) { return Either.right(componentsUtils.getResponseFormat(ActionStatus. COMPONENT_INVALID_DESCRIPTION, ComponentTypeEnum.RESOURCE.getValue())); } return Either.left(true); } return - * Either.left(false); } - */ - - /* - * private Either validateContactId(User user, Resource resource, AuditingActionEnum actionEnum) { String contactId = resource.getContactId(); - * - * if (!ValidationUtils.validateStringNotEmpty(contactId)) { log.info("contact info is missing."); ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus. COMPONENT_MISSING_CONTACT, ComponentTypeEnum.RESOURCE.getValue()); - * componentsUtils.auditResource(errorResponse, user, resource, "", "", actionEnum, null); return Either.right(errorResponse); } - * - * Either validateContactIdResponse = validateContactId(contactId); if (validateContactIdResponse.isRight()) { ResponseFormat responseFormat = validateContactIdResponse.right().value(); - * componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); } return validateContactIdResponse; } - * - * private Either validateContactId(String contactId) { if (contactId != null) { if (!ValidationUtils.validateContactId(contactId)) { log.debug("contact {} is invalid.", contactId); ResponseFormat errorResponse = - * componentsUtils.getResponseFormat(ActionStatus. COMPONENT_INVALID_CONTACT, ComponentTypeEnum.RESOURCE.getValue()); return Either.right(errorResponse); } return Either.left(true); } return Either.left(false); - * - * } - */ - - /* - * private Either validateIcon(User user, Resource resource, AuditingActionEnum actionEnum) { String icon = resource.getIcon(); - * - * if (!ValidationUtils.validateStringNotEmpty(icon)) { log.debug("icon is missing."); ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_MISSING_ICON, ComponentTypeEnum.RESOURCE.getValue()); - * componentsUtils.auditResource(errorResponse, user, resource, "", "", actionEnum, null); return Either.right(errorResponse); } - * - * Either validateIcon = validateIcon(icon); if (validateIcon.isRight()) { ResponseFormat responseFormat = validateIcon.right().value(); componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, - * null); } return validateIcon; - * - * } - * - * public Either validateIcon(String icon) { if (icon != null) { if (!ValidationUtils.validateIconLength(icon)) { log.debug("icon exceeds max length"); return Either.right(componentsUtils.getResponseFormat(ActionStatus. - * COMPONENT_ICON_EXCEEDS_LIMIT, ComponentTypeEnum.RESOURCE.getValue(), "" + ValidationUtils.ICON_MAX_LENGTH)); } if (!ValidationUtils.validateIcon(icon)) { log.debug("icon is invalid." ); ResponseFormat errorResponse = - * componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.RESOURCE.getValue()); return Either.right(errorResponse); } return Either.left(true); } return Either.left(false); - * - * } - */ - - private Either validateCost(User user, Resource resource, AuditingActionEnum actionEnum) { - String cost = resource.getCost(); - if (cost != null) { - - if (!ValidationUtils.validateCost(cost)) { - log.debug("resource cost is invalid."); - ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); - return Either.right(errorResponse); - } - } - return Either.left(true); - } - - private Either validateLicenseType(User user, Resource resource, AuditingActionEnum actionEnum) { - log.debug("validate licenseType"); - String licenseType = resource.getLicenseType(); - if (licenseType != null) { - List licenseTypes = ConfigurationManager.getConfigurationManager().getConfiguration().getLicenseTypes(); - if (!licenseTypes.contains(licenseType)) { - log.debug("License type {} isn't configured"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT); - if (actionEnum != null) { - // In update case, no audit is required - componentsUtils.auditResource(responseFormat, user, resource, "", "", actionEnum, null); - } - return Either.right(responseFormat); - } - } - return Either.left(true); - } - - private Either processUpdateOfDerivedFrom(Resource currentResource, Resource updatedResource, String userId, boolean shouldLock, boolean inTransaction) { - Either deleteArtifactByInterface = null; - if (updatedResource.getDerivedFrom() != null) { - log.debug("Starting derived from update for resource {}", updatedResource.getUniqueId()); - log.debug("1. Removing interface artifacts from graph"); - // Remove all interface artifacts of resource - String resourceId = updatedResource.getUniqueId(); - Map interfaces = currentResource.getInterfaces(); - - if (interfaces != null) { - Collection values = interfaces.values(); - for (InterfaceDefinition interfaceDefinition : values) { - String interfaceType = interfaceTypeOperation.getShortInterfaceName(interfaceDefinition); - - log.trace("Starting interface artifacts removal for interface type {}", interfaceType); - Map operations = interfaceDefinition.getOperationsMap(); - if (operations != null) { - for (Entry operationEntry : operations.entrySet()) { - Operation operation = operationEntry.getValue(); - ArtifactDefinition implementation = operation.getImplementationArtifact(); - if (implementation != null) { - String uniqueId = implementation.getUniqueId(); - log.debug("Removing interface artifact definition {}, operation {}, interfaceType {}", uniqueId, operationEntry.getKey(), interfaceType); - // only thing that transacts and locks here - deleteArtifactByInterface = artifactsBusinessLogic.deleteArtifactByInterface(resourceId, interfaceType, operationEntry.getKey(), userId, uniqueId, null, shouldLock, true); - if (deleteArtifactByInterface.isRight()) { - log.debug("Couldn't remove artifact definition with id {}", uniqueId); - if (!inTransaction) { - titanDao.rollback(); - } - return Either.right(deleteArtifactByInterface.right().value()); - } - } else { - log.trace("No implementation found for operation {} - nothing to delete", operationEntry.getKey()); - } - } - } else { - log.trace("No operations found for interface type {}", interfaceType); - } - } - } - log.debug("2. Removing properties"); - Either, StorageOperationStatus> findPropertiesOfNode = propertyOperation.deleteAllPropertiesAssociatedToNode(NodeTypeEnum.Resource, resourceId); - - if (findPropertiesOfNode.isRight() && !findPropertiesOfNode.right().value().equals(StorageOperationStatus.OK)) { - log.debug("Failed to remove all properties of resource"); - if (!inTransaction) - titanDao.rollback(); - return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(findPropertiesOfNode.right().value()))); - } - - } else { - log.debug("Derived from wasn't changed during update"); - } - - if (!inTransaction) - titanDao.commit(); - return Either.left(true); - - } - - /**** Auditing *******************/ - - protected static IElementOperation getElementDao(Class class1, ServletContext context) { - WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR); - - WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context); - - return webApplicationContext.getBean(class1); - } - - public ICapabilityTypeOperation getCapabilityTypeOperation() { - return capabilityTypeOperation; - } - - public void setCapabilityTypeOperation(ICapabilityTypeOperation capabilityTypeOperation) { - this.capabilityTypeOperation = capabilityTypeOperation; - } - - public Either validatePropertiesDefaultValues(Resource resource) { - log.debug("validate resource properties default values"); - Either eitherResult = Either.left(true); - List properties = resource.getProperties(); - String type = null; - String innerType = null; - if (properties != null) { - for (PropertyDefinition property : properties) { - if (!propertyOperation.isPropertyTypeValid(property)) { - log.info("Invalid type for property"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName()); - eitherResult = Either.right(responseFormat); - break; - } - - Either, ResponseFormat> allDataTypes = getAllDataTypes(applicationDataTypeCache); - if (allDataTypes.isRight()) { - return Either.right(allDataTypes.right().value()); - } - - type = property.getType(); - if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) { - ImmutablePair propertyInnerTypeValid = propertyOperation.isPropertyInnerTypeValid(property, allDataTypes.left().value()); - innerType = propertyInnerTypeValid.getLeft(); - if (!propertyInnerTypeValid.getRight().booleanValue()) { - log.info("Invalid inner type for property"); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_INNER_TYPE, innerType, property.getName()); - eitherResult = Either.right(responseFormat); - break; - } - } - - if (!propertyOperation.isPropertyDefaultValueValid(property, allDataTypes.left().value())) { - log.info("Invalid default value for property"); - ResponseFormat responseFormat; - if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) { - responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, property.getName(), type, innerType, property.getDefaultValue()); - } else { - responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEFAULT_VALUE, property.getName(), type, property.getDefaultValue()); - } - eitherResult = Either.right(responseFormat); - break; - - } - } - } - return eitherResult; - } - - @Override - public Either, ResponseFormat> deleteMarkedComponents() { - return deleteMarkedComponents(ComponentTypeEnum.RESOURCE); - } - - @Override - public ComponentInstanceBusinessLogic getComponentInstanceBL() { - return vfComponentInstanceBusinessLogic; - } - - private String getComponentTypeForResponse(Component component) { - String componentTypeForResponse = "SERVICE"; - if (component instanceof Resource) { - componentTypeForResponse = ((Resource) component).getResourceType().name(); - } - return componentTypeForResponse; - } - - private Either, ResponseFormat> createGroupsFromYaml(String yamlFileName, Map toscaJson, Resource resource) { - - Map groups = new HashMap(); - Either, ResponseFormat> result = Either.left(groups); - - Either, ResultStatusEnum> eitherNodesTemlates = ImportUtils.findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.GROUPS); - if (eitherNodesTemlates.isLeft()) { - Map jsonNodeTemplates = eitherNodesTemlates.left().value(); - - if (jsonNodeTemplates != null && false == jsonNodeTemplates.isEmpty()) { - Iterator> nodesNameValue = jsonNodeTemplates.entrySet().iterator(); - while (nodesNameValue.hasNext()) { - Entry groupNameValue = nodesNameValue.next(); - - String groupName = groupNameValue.getKey(); - Either eitherNode = createGroupInfo(groupName, groupNameValue.getValue()); - if (eitherNode.isRight()) { - String message = "Failed when creating group: " + groupNameValue.getKey() + " for resource:" + resource.getName(); - BeEcompErrorManager.getInstance().logInternalFlowError("ImportResource", message, ErrorSeverity.INFO); - return Either.right(eitherNode.right().value()); - } else { - GroupDefinition groupDefinition = eitherNode.left().value(); - groups.put(groupName, groupDefinition); - } - } - } - } - - return result; - } - - private Either, ResponseFormat> createInputsFromYaml(String yamlFileName, Map toscaJson, Resource resource) { - - Either, ResultStatusEnum> inputs = ImportUtils.getInputs(toscaJson); - if (inputs.isRight()) { - String message = "Failed when creating inputs: for resource:" + resource.getName(); - BeEcompErrorManager.getInstance().logInternalFlowError("ImportResource", message, ErrorSeverity.INFO); - Map resultMap = new HashMap<>(); - return Either.left(resultMap); - - } - - Either, ResponseFormat> result = Either.left(inputs.left().value()); - - return result; - } - - @SuppressWarnings("unchecked") - private Either createGroupInfo(String groupName, Object groupTemplateJson) { - - GroupDefinition groupInfo = new GroupDefinition(); - groupInfo.setName(groupName); - Either result = Either.left(groupInfo); - - try { - if (groupTemplateJson != null && groupTemplateJson instanceof Map) { - Map groupTemplateJsonMap = (Map) groupTemplateJson; - String groupType = (String) groupTemplateJsonMap.get(ToscaTagNamesEnum.TYPE.getElementName()); - if (!StringUtils.isEmpty(groupType)) { - groupInfo.setType(groupType); - } else { - log.debug("The 'type' member is not found under group {}", groupName); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_MISSING_GROUP_TYPE, groupName)); - } - - if (groupTemplateJsonMap.containsKey(ToscaTagNamesEnum.DESCRIPTION.getElementName())) { - groupInfo.setDescription((String) groupTemplateJsonMap.get(ToscaTagNamesEnum.DESCRIPTION.getElementName())); - } - - if (groupTemplateJsonMap.containsKey(ToscaTagNamesEnum.MEMBERS.getElementName())) { - Object members = groupTemplateJsonMap.get(ToscaTagNamesEnum.MEMBERS.getElementName()); - if (members != null) { - if (members instanceof List) { - Map membersLoaded = new HashMap<>(); - List membersAsList = (List) members; - for (Object member : membersAsList) { - membersLoaded.put(member.toString(), ""); - } - groupInfo.setMembers(membersLoaded); - } else { - log.debug("The 'members' member is not of type list under group {}", groupName); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE)); - } - } - } - - if (groupTemplateJsonMap.containsKey(ToscaTagNamesEnum.PROPERTIES.getElementName())) { - Object properties = groupTemplateJsonMap.get(ToscaTagNamesEnum.PROPERTIES.getElementName()); - - Either, ResponseFormat> regResponse = createPropertiesValueModuleFromYaml(properties, groupName, groupType); - if (regResponse.isRight()) - return Either.right(regResponse.right().value()); - if (regResponse.left().value().size() > 0) { - groupInfo.convertFromGroupProperties(regResponse.left().value()); - } - } - - } else { - result = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE)); - } - } catch (Exception e) { - BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create group"); - log.debug("error when creating group, message:{}", e.getMessage(), e); - result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_YAML)); - } - - return result; - } - - @SuppressWarnings("unchecked") - private Either, ResponseFormat> createPropertiesValueModuleFromYaml(Object properties, String groupName, String groupType) { - - List result = new ArrayList<>(); - - if (properties == null) { - return Either.left(result); - } - - Either groupTypeRes = groupTypeOperation.getLatestGroupTypeByType(groupType, true); - - if (groupTypeRes.isRight()) { - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_TYPE_IS_INVALID, groupType)); - } - - Map gtProperties = new HashMap<>(); - GroupTypeDefinition groupTypeDefinition = groupTypeRes.left().value(); - - List propertiesDef = groupTypeDefinition.getProperties(); - - if (propertiesDef != null) { - gtProperties = propertiesDef.stream().collect(Collectors.toMap(p -> p.getName(), p -> p)); - } - - if (properties != null) { - - if (properties instanceof Map) { - - Map props = (Map) properties; - for (Entry entry : props.entrySet()) { - - String propName = entry.getKey(); - Object value = entry.getValue(); - - PropertyDefinition gtDefinition = gtProperties.get(propName); - if (gtDefinition == null) { - return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_PROPERTY_NOT_FOUND, propName, groupName, groupType)); - } - - ToscaPropertyType type = ToscaPropertyType.isValidType(gtDefinition.getType()); - - String convertedValue = null; - if (value != null) { - if (type == null || value instanceof Map || value instanceof List) { - convertedValue = gson.toJson(value); - } else { - convertedValue = value.toString(); - } - } - - GroupProperty groupProperty = new GroupProperty(); - groupProperty.setValue(convertedValue); - groupProperty.setName(propName); - - log.trace("After building group property {}", groupProperty); - - result.add(groupProperty); - } - - } - - } - - return Either.left(result); - } - - public Either getLatestResourceFromCsarUuid(String csarUuid, User user) { - - // validate user - if (user != null) { - Either userValidation = validateUserExists(user, "Get resource from csar UUID", false); - if (userValidation.isRight()) { - return Either.right(userValidation.right().value()); - } - } - - // get resource from csar uuid - Either either = toscaOperationFacade.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUuid, ""); - if (either.isRight()) { - ResponseFormat resp = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_FROM_CSAR_NOT_FOUND, csarUuid); - return Either.right(resp); - } - - return Either.left(either.left().value()); - } - - @Override - public Either, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs(String componentId, ComponentTypeEnum componentTypeEnum, String userId, String searchText) { - return null; - } - - private Either>, ResponseFormat> getValidComponentInstanceCapabilities(String resourceId, Map> defaultCapabilities, Map> uploadedCapabilities) { - ResponseFormat responseFormat; - Map> validCapabilitiesMap = new HashMap<>(); - - for (Entry> uploadedCapabilitiesEntry : uploadedCapabilities.entrySet()) { - String capabilityType = uploadedCapabilitiesEntry.getValue().get(0).getType(); - if (!defaultCapabilities.containsKey(capabilityType)) { - responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, capabilityType); - return Either.right(responseFormat); - } else { - CapabilityDefinition defaultCapability; - if(CollectionUtils.isNotEmpty(defaultCapabilities.get(capabilityType).get(0).getProperties())){ - defaultCapability = defaultCapabilities.get(capabilityType).get(0); - } else { - Either getFullComponentRes = toscaOperationFacade.getToscaFullElement(resourceId); - if(getFullComponentRes.isRight()){ - log.debug("Failed to get full component {}. Status is {}. ", resourceId, getFullComponentRes.right().value()); - responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_FOUND, resourceId); - return Either.right(responseFormat); - } - defaultCapability = getFullComponentRes.left().value().getCapabilities().get(capabilityType).get(0); - } - if(CollectionUtils.isEmpty(defaultCapability.getProperties()) && CollectionUtils.isNotEmpty(uploadedCapabilitiesEntry.getValue().get(0).getProperties())){ - log.debug("Failed to validate capability {} of component {}. Property list is empty. ", defaultCapability.getName(), resourceId); - log.debug("Failed to update capability property values. Property list of fetched capability {} is empty. ", defaultCapability.getName()); - responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, resourceId); - return Either.right(responseFormat); - } - if(CollectionUtils.isNotEmpty(defaultCapability.getProperties()) && CollectionUtils.isNotEmpty(uploadedCapabilitiesEntry.getValue().get(0).getProperties())){ - Either validationRes = validateUniquenessUpdateUploadedComponentInstanceCapability(defaultCapability, uploadedCapabilitiesEntry.getValue().get(0)); - if (validationRes.isRight()) { - responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NAME_ALREADY_EXISTS, validationRes.right().value()); - return Either.right(responseFormat); - } - } - List validCapabilityList = new ArrayList<>(); - validCapabilityList.add(defaultCapability); - validCapabilitiesMap.put(uploadedCapabilitiesEntry.getKey(), validCapabilityList); - } - } - return Either.left(validCapabilitiesMap); - } - - private Either validateUniquenessUpdateUploadedComponentInstanceCapability(CapabilityDefinition defaultCapability, UploadCapInfo uploadedCapability) { - List validProperties = new ArrayList<>(); - Map defaultProperties = defaultCapability.getProperties().stream().collect(Collectors.toMap(PropertyDefinition::getName, Function.identity())); - List uploadedProperties = uploadedCapability.getProperties(); - for (UploadPropInfo property : uploadedProperties) { - String propertyName = property.getName().toLowerCase(); - String propertyType = property.getType(); - ComponentInstanceProperty validProperty; - if (defaultProperties.containsKey(propertyName)) { - if (propertyType != null && !defaultProperties.get(propertyName).getType().equals(propertyType)) { - return Either.right(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); - return Either.left(true); - } - - private Either>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation(List artifactPathAndNameList, List existingArtifactsToHandle, - Resource resource, User user) { - - EnumMap> nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class); - Wrapper responseWrapper = new Wrapper<>(); - Either>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either.left(nodeTypeArtifactsToHandle); - ; - try { - // add all found Csar artifacts to list to upload - List artifactsToUpload = new ArrayList<>(artifactPathAndNameList); - List artifactsToUpdate = new ArrayList<>(); - List artifactsToDelete = new ArrayList<>(); - for (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()) == currNewArtifact.getArtifactType()) { - if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) { - currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId()); - // if current artifact already exists, but has different content, add him to the list to update - artifactsToUpdate.add(currNewArtifact); - } - // remove found artifact from the list of existing artifacts to handle, because it was already handled - existingArtifactsToHandle.remove(foundArtifact); - // and remove found artifact from the list to upload, because it should either be updated or be ignored - artifactsToUpload.remove(currNewArtifact); - } else { - log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName()); - ResponseFormat responseFormat = ResponseFormatManager.getInstance().getResponseFormat(ActionStatus.ARTIFACT_ALRADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, currNewArtifact.getArtifactName(), - currNewArtifact.getArtifactType().name(), foundArtifact.getArtifactType()); - AuditingActionEnum auditingAction = artifactsBusinessLogic.detectAuditingType(artifactsBusinessLogic.new ArtifactOperationInfo(false, false, ArtifactOperationEnum.Create), foundArtifact.getArtifactChecksum()); - artifactsBusinessLogic.handleAuditing(auditingAction, resource, resource.getUniqueId(), user, null, null, foundArtifact.getUniqueId(), responseFormat, resource.getComponentType(), null); - responseWrapper.setInnerElement(responseFormat); - break; - } - } - } - } - if (responseWrapper.isEmpty()) { - existingArtifactsToHandle.stream() - // add all artifacts which was not detected as artifact to update or to ignore to the list to delete - .forEach(a -> artifactsToDelete.add(new NonMetaArtifactInfo(a.getArtifactName(), null, ArtifactTypeEnum.findType(a.getArtifactType()), a.getArtifactGroupType(), null, a.getUniqueId()))); - } - if (responseWrapper.isEmpty()) { - if (!artifactsToUpload.isEmpty()) - nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.Create, artifactsToUpload); - if (!artifactsToUpdate.isEmpty()) - nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.Update, artifactsToUpdate); - if (!artifactsToDelete.isEmpty()) - nodeTypeArtifactsToHandle.put(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); - } - return nodeTypeArtifactsToHandleRes; - } - - - private 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; - } - StringBuilder toscaResourceName = new StringBuilder(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX); - String 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)).append('.').append(Constants.ABSTRACT); - } - return toscaResourceName.append(actualName.toLowerCase()).toString(); - } - - public ICacheMangerOperation getCacheManagerOperation() { - return cacheManagerOperation; - } - - public void setCacheManagerOperation(ICacheMangerOperation cacheManagerOperation) { - this.cacheManagerOperation = cacheManagerOperation; - } - - ///////////////////////////////////////// DataModel refactoring///////////////////////////////////////////// - @Override - public Either getUiComponentDataTransferByComponentId(String resourceId, List dataParamsToReturn) { - - ComponentParametersView paramsToRetuen = new ComponentParametersView(dataParamsToReturn); - Either resourceResultEither = toscaOperationFacade.getToscaElement(resourceId, paramsToRetuen); - - if (resourceResultEither.isRight()) { - if (resourceResultEither.right().value().equals(StorageOperationStatus.NOT_FOUND)) { - log.debug("Failed to found resource with id {} ", resourceId); - Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId)); - } - - log.debug("failed to get resource by id {} with filters {}", resourceId, dataParamsToReturn.toString()); - return Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceResultEither.right().value()), "")); - } - - Resource resource = resourceResultEither.left().value(); - UiComponentDataTransfer dataTransfer = UiComponentDataConverter.getUiDataTransferFromResourceByParams(resource, dataParamsToReturn); - return Either.left(dataTransfer); - } - + private static final String DELETE_RESOURCE = "Delete Resource"; + private static final String IN_RESOURCE = " in resource {} "; + private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes"; + private static final String INITIAL_VERSION = "0.1"; + private static final Logger log = Logger.getLogger(ResourceBusinessLogic.class); + private static final String CERTIFICATION_ON_IMPORT = "certification on import"; + private static final String CREATE_RESOURCE = "Create Resource"; + private static final String VALIDATE_DERIVED_BEFORE_UPDATE = "validate derived before update"; + private static final String CATEGORY_IS_EMPTY = "Resource category is empty"; + private static final String CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES = "Create Resource - validateCapabilityTypesCreate"; + 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 VALID_CHARACTERS_ARTIFACT_NAME = "'A-Z', 'a-z', '0-9', '.', '_', '-', '@' and space"; + private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(ResourceBusinessLogic.class.getName()); + private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic; + private final ResourceImportManager resourceImportManager; + private final InputsBusinessLogic inputsBusinessLogic; + private final OutputsBusinessLogic outputsBusinessLogic; + private final CompositionBusinessLogic compositionBusinessLogic; + private final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic; + private final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic; + private final UiComponentDataConverter uiComponentDataConverter; + private final CsarBusinessLogic csarBusinessLogic; + private final PropertyBusinessLogic propertyBusinessLogic; + private final PolicyBusinessLogic policyBusinessLogic; + private final ModelBusinessLogic modelBusinessLogic; + private final DataTypeBusinessLogic dataTypeBusinessLogic; + private final PolicyTypeBusinessLogic policyTypeBusinessLogic; + private final ModelOperation modelOperation; + private IInterfaceLifecycleOperation interfaceTypeOperation; + private LifecycleBusinessLogic lifecycleBusinessLogic; + @Autowired + private ICapabilityTypeOperation capabilityTypeOperation; + @Autowired + private TopologyComparator topologyComparator; + @Autowired + private ComponentValidator componentValidator; + @Autowired + private PropertyDataValueMergeBusinessLogic propertyDataValueMergeBusinessLogic; + @Autowired + private SoftwareInformationBusinessLogic softwareInformationBusinessLogic; + + + @Autowired + public ResourceBusinessLogic(final IElementOperation elementDao, final IGroupOperation groupOperation, + final IGroupInstanceOperation groupInstanceOperation, final IGroupTypeOperation groupTypeOperation, + final GroupBusinessLogic groupBusinessLogic, final InterfaceOperation interfaceOperation, + final InterfaceLifecycleOperation interfaceLifecycleTypeOperation, + final ArtifactsBusinessLogic artifactsBusinessLogic, + final ComponentInstanceBusinessLogic componentInstanceBusinessLogic, + final @Lazy ResourceImportManager resourceImportManager, final InputsBusinessLogic inputsBusinessLogic, + final OutputsBusinessLogic outputsBusinessLogic, final CompositionBusinessLogic compositionBusinessLogic, + final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic, + final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic, + final UiComponentDataConverter uiComponentDataConverter, final CsarBusinessLogic csarBusinessLogic, + final ArtifactsOperations artifactToscaOperation, final PropertyBusinessLogic propertyBusinessLogic, + final ComponentContactIdValidator componentContactIdValidator, final ComponentNameValidator componentNameValidator, + final ComponentTagsValidator componentTagsValidator, final ComponentValidator componentValidator, + final ComponentIconValidator componentIconValidator, + final ComponentProjectCodeValidator componentProjectCodeValidator, + final ComponentDescriptionValidator componentDescriptionValidator, final PolicyBusinessLogic policyBusinessLogic, + final ModelBusinessLogic modelBusinessLogic, final DataTypeBusinessLogic dataTypeBusinessLogic, + final PolicyTypeBusinessLogic policyTypeBusinessLogic, final ModelOperation modelOperation) { + super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic, interfaceOperation, + interfaceLifecycleTypeOperation, artifactsBusinessLogic, artifactToscaOperation, componentContactIdValidator, componentNameValidator, + componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator); + this.componentInstanceBusinessLogic = componentInstanceBusinessLogic; + this.resourceImportManager = resourceImportManager; + this.inputsBusinessLogic = inputsBusinessLogic; + this.outputsBusinessLogic = outputsBusinessLogic; + this.compositionBusinessLogic = compositionBusinessLogic; + this.resourceDataMergeBusinessLogic = resourceDataMergeBusinessLogic; + this.csarArtifactsAndGroupsBusinessLogic = csarArtifactsAndGroupsBusinessLogic; + this.uiComponentDataConverter = uiComponentDataConverter; + this.csarBusinessLogic = csarBusinessLogic; + this.propertyBusinessLogic = propertyBusinessLogic; + this.policyBusinessLogic = policyBusinessLogic; + this.modelBusinessLogic = modelBusinessLogic; + this.dataTypeBusinessLogic = dataTypeBusinessLogic; + this.policyTypeBusinessLogic = policyTypeBusinessLogic; + this.modelOperation = modelOperation; + } + + static Either rollbackWithEither(final JanusGraphDao janusGraphDao, final ActionStatus actionStatus, + final String... params) { + if (janusGraphDao != null) { + janusGraphDao.rollback(); + } + return Either.right(new ByActionStatusComponentException(actionStatus, params)); + } + + public LifecycleBusinessLogic getLifecycleBusinessLogic() { + return lifecycleBusinessLogic; + } + + @Autowired + public void setLifecycleManager(LifecycleBusinessLogic lifecycleBusinessLogic) { + this.lifecycleBusinessLogic = lifecycleBusinessLogic; + } + + @VisibleForTesting + protected void setComponentValidator(ComponentValidator componentValidator) { + this.componentValidator = componentValidator; + } + + public IElementOperation getElementDao() { + return elementDao; + } + + public void setElementDao(IElementOperation elementDao) { + this.elementDao = elementDao; + } + + public UserBusinessLogic getUserAdmin() { + return this.userAdmin; + } + + @Autowired + @Override + public void setUserAdmin(UserBusinessLogic userAdmin) { + this.userAdmin = userAdmin; + } + + public ComponentsUtils getComponentsUtils() { + return this.componentsUtils; + } + + @Autowired + @Override + public void setComponentsUtils(ComponentsUtils componentsUtils) { + this.componentsUtils = componentsUtils; + } + + public ArtifactsBusinessLogic getArtifactsManager() { + return artifactsBusinessLogic; + } + + public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) { + this.artifactsBusinessLogic = artifactsManager; + } + + @Autowired + @Override + public void setApplicationDataTypeCache(ApplicationDataTypeCache applicationDataTypeCache) { + this.applicationDataTypeCache = applicationDataTypeCache; + } + + @Autowired + public void setInterfaceTypeOperation(IInterfaceLifecycleOperation interfaceTypeOperation) { + this.interfaceTypeOperation = interfaceTypeOperation; + } + + /** + * the method returns a list of all the resources that are certified, the returned resources are only abstract or only none abstract according to + * the given param + * + * @param getAbstract + * @param userId TODO + * @return + */ + public List getAllCertifiedResources(boolean getAbstract, HighestFilterEnum highestFilter, String userId) { + User user = validateUserExists(userId); + Boolean isHighest = null; + switch (highestFilter) { + case ALL: + break; + case HIGHEST_ONLY: + isHighest = true; + break; + case NON_HIGHEST_ONLY: + isHighest = false; + break; + default: + break; + } + Either, StorageOperationStatus> getResponse = toscaOperationFacade.getAllCertifiedResources(getAbstract, isHighest); + if (getResponse.isRight()) { + throw new StorageException(getResponse.right().value()); + } + return getResponse.left().value(); + } + + public Either, ResponseFormat> validateResourceNameExists(String resourceName, ResourceTypeEnum resourceTypeEnum, + String userId) { + validateUserExists(userId); + Either dataModelResponse = toscaOperationFacade + .validateComponentNameUniqueness(resourceName, resourceTypeEnum, ComponentTypeEnum.RESOURCE); + // DE242223 + janusGraphDao.commit(); + if (dataModelResponse.isLeft()) { + Map result = new HashMap<>(); + result.put("isValid", dataModelResponse.left().value()); + log.debug("validation was successfully performed."); + return Either.left(result); + } + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value())); + return Either.right(responseFormat); + } + + public Resource createResource(Resource resource, AuditingActionEnum auditingAction, User user, Map csarUIPayload, + String payloadName) { + validateResourceBeforeCreate(resource, user, false); + String csarUUID = payloadName == null ? resource.getCsarUUID() : payloadName; + loggerSupportability.log(LoggerSupportabilityActions.CREATE_RESOURCE, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, + "Starting to create resource from CSAR by user {} ", user.getUserId()); + if (StringUtils.isNotEmpty(csarUUID)) { + csarBusinessLogic.validateCsarBeforeCreate(resource, auditingAction, user, csarUUID); + log.debug("CsarUUID is {} - going to create resource from CSAR", csarUUID); + Resource createResourceFromCsar = createResourceFromCsar(resource, user, csarUIPayload, csarUUID); + return updateCatalog(createResourceFromCsar, ChangeTypeEnum.LIFECYCLE).left().map(Resource.class::cast).left().value(); + } + final Resource createResourceByDao = createResourceByDao(resource, user, auditingAction, false, false); + return updateCatalog(createResourceByDao, ChangeTypeEnum.LIFECYCLE).left().map(Resource.class::cast).left().value(); + } + + public Resource validateAndUpdateResourceFromCsar(Resource resource, User user, Map csarUIPayload, String payloadName, + String resourceUniqueId) { + String csarUUID = payloadName; + String csarVersion = null; + Resource updatedResource = null; + if (payloadName == null) { + csarUUID = resource.getCsarUUID(); + csarVersion = resource.getCsarVersion(); + } + if (csarUUID != null && !csarUUID.isEmpty()) { + Resource oldResource = getResourceByUniqueId(resourceUniqueId); + validateCsarUuidMatching(oldResource, resource, csarUUID, resourceUniqueId, user); + validateCsarIsNotAlreadyUsed(oldResource, resource, csarUUID, user); + if (oldResource != null && ValidationUtils.hasBeenCertified(oldResource.getVersion())) { + overrideImmutableMetadata(oldResource, resource); + } + validateResourceBeforeCreate(resource, user, false); + String oldCsarVersion = oldResource != null ? oldResource.getCsarVersion() : null; + log.debug("CsarUUID is {} - going to update resource with UniqueId {} from CSAR", csarUUID, resourceUniqueId); + // (on boarding flow): If the update includes same csarUUID and + + // same csarVersion as already in the VF - no need to import the + + // csar (do only metadata changes if there are). + if (csarVersion != null && oldCsarVersion != null && oldCsarVersion.equals(csarVersion)) { + updatedResource = updateResourceMetadata(resourceUniqueId, resource, oldResource, user, false); + } else { + updatedResource = updateResourceFromCsar(oldResource, resource, user, AuditingActionEnum.UPDATE_RESOURCE_METADATA, false, + csarUIPayload, csarUUID); + } + } else { + log.debug("Failed to update resource {}, csarUUID or payload name is missing", resource.getSystemName()); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CSAR_UUID, resource.getName()); + componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.CREATE_RESOURCE); + throw new ByActionStatusComponentException(ActionStatus.MISSING_CSAR_UUID, resource.getName()); + } + return updatedResource; + } + + private void validateCsarIsNotAlreadyUsed(Resource oldResource, Resource resource, String csarUUID, User user) { + // (on boarding flow): If the update includes a csarUUID: verify this + + // csarUUID is not in use by another VF, If it is - use same error as + + // above: + + // "Error: The VSP with UUID %1 was already imported for VF %2. Please + + // select another or update the existing VF." %1 - csarUUID, %2 - VF + + // name + Either resourceLinkedToCsarRes = toscaOperationFacade + .getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUUID, resource.getSystemName()); + if (resourceLinkedToCsarRes.isRight()) { + if (StorageOperationStatus.NOT_FOUND != resourceLinkedToCsarRes.right().value()) { + log.debug("Failed to find previous resource by CSAR {} and system name {}", csarUUID, resource.getSystemName()); + throw new StorageException(resourceLinkedToCsarRes.right().value()); + } + } else if (!resourceLinkedToCsarRes.left().value().getUniqueId().equals(oldResource.getUniqueId()) && !resourceLinkedToCsarRes.left().value() + .getName().equals(oldResource.getName())) { + ResponseFormat errorResponse = componentsUtils + .getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, csarUUID, resourceLinkedToCsarRes.left().value().getName()); + componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA); + throw new ByActionStatusComponentException(ActionStatus.VSP_ALREADY_EXISTS, csarUUID, resourceLinkedToCsarRes.left().value().getName()); + } + } + + private void validateCsarUuidMatching(Resource resource, Resource oldResource, String csarUUID, String resourceUniqueId, User user) { + // (on boarding flow): If the update includes csarUUID which is + + // different from the csarUUID of the VF - fail with + + // error: "Error: Resource %1 cannot be updated using since it is linked + + // to a different VSP" %1 - VF name + String oldCsarUUID = oldResource.getCsarUUID(); + if (oldCsarUUID != null && !oldCsarUUID.isEmpty() && !csarUUID.equals(oldCsarUUID)) { + log.debug("Failed to update resource with UniqueId {} using Csar {}, since the resource is linked to a different VSP {}", + resourceUniqueId, csarUUID, oldCsarUUID); + ResponseFormat errorResponse = componentsUtils + .getResponseFormat(ActionStatus.RESOURCE_LINKED_TO_DIFFERENT_VSP, resource.getName(), csarUUID, oldCsarUUID); + componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA); + throw new ByActionStatusComponentException(ActionStatus.RESOURCE_LINKED_TO_DIFFERENT_VSP, resource.getName(), csarUUID, oldCsarUUID); + } + } + + private Resource getResourceByUniqueId(String resourceUniqueId) { + Either oldResourceRes = toscaOperationFacade.getToscaFullElement(resourceUniqueId); + if (oldResourceRes.isRight()) { + log.debug("Failed to find previous resource by UniqueId {}, status: {}", resourceUniqueId, oldResourceRes.right().value()); + throw new StorageException(oldResourceRes.right().value()); + } + return oldResourceRes.left().value(); + } + + private void overrideImmutableMetadata(Resource oldResource, Resource resource) { + resource.setName(oldResource.getName()); + resource.setIcon(oldResource.getIcon()); + resource.setTags(oldResource.getTags()); + resource.setCategories(oldResource.getCategories()); + resource.setDerivedFrom(oldResource.getDerivedFrom()); + } + + private Resource updateResourceFromCsar(Resource oldResource, Resource newResource, User user, AuditingActionEnum updateResource, + boolean inTransaction, Map csarUIPayload, String csarUUID) { + Resource updatedResource = null; + validateLifecycleState(oldResource, user); + String lockedResourceId = oldResource.getUniqueId(); + List createdArtifacts = new ArrayList<>(); + CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(newResource, oldResource, user, csarUIPayload, csarUUID, null); + lockComponent(lockedResourceId, oldResource, "update Resource From Csar"); + Map nodeTypesInfo = csarInfo.extractTypesInfo(); + Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle( + nodeTypesInfo, csarInfo, oldResource); + if (findNodeTypesArtifactsToHandleRes.isRight()) { + log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID()); + throw new ByResponseFormatComponentException(findNodeTypesArtifactsToHandleRes.right().value()); + } + Map>> nodeTypesArtifactsToHandle = findNodeTypesArtifactsToHandleRes.left() + .value(); + try { + updatedResource = updateResourceFromYaml(oldResource, newResource, updateResource, createdArtifacts, csarInfo.getMainTemplateName(), + csarInfo.getMainTemplateContent(), csarInfo, nodeTypesInfo, nodeTypesArtifactsToHandle, null, false); + } catch (ComponentException | StorageException e) { + rollback(inTransaction, newResource, createdArtifacts, null); + throw e; + } finally { + janusGraphDao.commit(); + log.debug("unlock resource {}", lockedResourceId); + graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource); + } + return updatedResource; + } + + private void validateLifecycleState(Resource oldResource, User user) { + if (LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT == oldResource.getLifecycleState() && !oldResource.getLastUpdaterUserId() + .equals(user.getUserId())) { + log.debug("#validateLifecycleState - Current user is not last updater, last updater userId: {}, current user userId: {}", + oldResource.getLastUpdaterUserId(), user.getUserId()); + throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION); + } + } + + private Resource updateResourceFromYaml(Resource oldResource, Resource newResource, AuditingActionEnum actionEnum, + List createdArtifacts, String yamlFileName, String yamlFileContent, CsarInfo csarInfo, + Map nodeTypesInfo, + Map>> nodeTypesArtifactsToHandle, + String nodeName, boolean isNested) { + boolean inTransaction = true; + boolean shouldLock = false; + Resource preparedResource = null; + ParsedToscaYamlInfo uploadComponentInstanceInfoMap; + try { + uploadComponentInstanceInfoMap = csarBusinessLogic + .getParsedToscaYamlInfo(yamlFileContent, yamlFileName, nodeTypesInfo, csarInfo, nodeName, oldResource); + Map instances = uploadComponentInstanceInfoMap.getInstances(); + if (MapUtils.isEmpty(instances) && newResource.getResourceType() != ResourceTypeEnum.PNF) { + throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName); + } + preparedResource = updateExistingResourceByImport(newResource, oldResource, csarInfo.getModifier(), inTransaction, shouldLock, + isNested).left; + log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent); + handleResourceGenericType(preparedResource, yamlFileContent, uploadComponentInstanceInfoMap, + uploadComponentInstanceInfoMap.getSubstitutionMappingNodeType()); + handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo, + csarInfo, nodeName, newResource.getModel()); + preparedResource = createInputsOnResource(preparedResource, uploadComponentInstanceInfoMap.getInputs()); + Map existingNodeTypesByResourceNames = new HashMap<>(); + final Map instancesToCreate = getInstancesToCreate(uploadComponentInstanceInfoMap, + newResource.getModel()); + preparedResource = createResourceInstances(yamlFileName, preparedResource, oldResource, instancesToCreate, csarInfo.getCreatedNodes(), + existingNodeTypesByResourceNames); + preparedResource = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, oldResource, + instancesToCreate, + existingNodeTypesByResourceNames); + } catch (ComponentException e) { + ResponseFormat responseFormat = + e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat(); + log.debug("#updateResourceFromYaml - failed to update newResource from yaml {} .The error is {}", yamlFileName, responseFormat); + componentsUtils + .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldResource : preparedResource, actionEnum); + throw e; + } catch (StorageException e) { + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus())); + log.debug("#updateResourceFromYaml - failed to update newResource from yaml {} .The error is {}", yamlFileName, responseFormat); + componentsUtils + .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldResource : preparedResource, actionEnum); + throw e; + } + Either, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic + .validateUpdateVfGroupNames(uploadComponentInstanceInfoMap.getGroups(), preparedResource.getSystemName()); + if (validateUpdateVfGroupNamesRes.isRight()) { + throw new ByResponseFormatComponentException(validateUpdateVfGroupNamesRes.right().value()); + } + // add groups to newResource + Map groups; + if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) { + groups = validateUpdateVfGroupNamesRes.left().value(); + } else { + groups = uploadComponentInstanceInfoMap.getGroups(); + } + handleGroupsProperties(preparedResource, groups); + Either isTopologyChanged = topologyComparator.isTopologyChanged(oldResource, preparedResource); + preparedResource = updateGroupsOnResource(preparedResource, groups); + NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToHandle); + Either updateArtifactsEither = createOrUpdateArtifacts(ArtifactOperationEnum.UPDATE, createdArtifacts, yamlFileName, + csarInfo, preparedResource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock); + if (updateArtifactsEither.isRight()) { + log.debug("failed to update artifacts {}", updateArtifactsEither.right().value()); + throw new ByResponseFormatComponentException(updateArtifactsEither.right().value()); + } + preparedResource = getResourceWithGroups(updateArtifactsEither.left().value().getUniqueId()); + updateGroupsName(oldResource, preparedResource, isTopologyChanged.left().value()); + updateResourceInstancesNames(oldResource, csarInfo, preparedResource, isTopologyChanged.left().value()); + final String preparedResourceId = preparedResource != null ? preparedResource.getUniqueId() : ""; + preparedResource = getResourceWithGroups(preparedResourceId); + updateVolumeGroup(preparedResource); + ActionStatus mergingPropsAndInputsStatus = resourceDataMergeBusinessLogic.mergeResourceEntities(oldResource, preparedResource); + if (mergingPropsAndInputsStatus != ActionStatus.OK) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(mergingPropsAndInputsStatus, preparedResource); + throw new ByResponseFormatComponentException(responseFormat); + } + compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId()); + return preparedResource; + } + + protected void updateVolumeGroup(Resource preparedResource) { + List groups = preparedResource.safeGetGroups(); + for (GroupDefinition group : groups) { + Map createdNewArtifacts = preparedResource.getDeploymentArtifacts(); + if (DEFAULT_GROUP_VF_MODULE.equals(group.getType())) { + List volumePropList = group.getProperties().stream().filter(p -> "volume_group".equals(p.getName())) + .collect(Collectors.toList()); + if (!volumePropList.isEmpty()) { + PropertyDataDefinition volumeProp = volumePropList.get(0); + if (volumeProp != null) { + boolean isVolumeGroup = isVolumeGroup(group.getArtifacts(), new ArrayList<>(createdNewArtifacts.values())); + if (!volumePropList.get(0).getValue().equals(String.valueOf(isVolumeGroup))) { + volumeProp.setValue(String.valueOf(isVolumeGroup)); + volumeProp.setDefaultValue(String.valueOf(isVolumeGroup)); + } + } + } + } + } + } + + private void updateGroupsName(Resource oldResource, Resource preparedResource, boolean isTopologyChanged) { + if (oldResource == null || preparedResource == null) { + log.debug("Failed to update groups name : oldResource or preparedResource is null"); + } else if (CollectionUtils.isNotEmpty(oldResource.getGroups()) && CollectionUtils.isNotEmpty(preparedResource.getGroups())) { + Map oldGroups = oldResource.getGroups().stream() + .collect(toMap(GroupDataDefinition::getInvariantName, GroupDataDefinition::getName)); + List updatedGroups = preparedResource.getGroups().stream() + .filter(group -> oldGroups.containsKey(group.getInvariantName()) && !group.getName().equals(oldGroups.get(group.getInvariantName()))) + .collect(toList()); + if (CollectionUtils.isNotEmpty(updatedGroups)) { + if (isTopologyChanged) { + updatedGroups.stream().filter(group -> !group.isVspOriginated()) + .forEach(group -> group.setName(oldGroups.get(group.getInvariantName()))); + } else { + updatedGroups.forEach(group -> group.setName(oldGroups.get(group.getInvariantName()))); + } + groupBusinessLogic.updateGroups(preparedResource, updatedGroups, false); + } + } + } + + private void updateResourceInstancesNames(Resource oldResource, CsarInfo csarInfo, Resource preparedResource, boolean isTopologyChanged) { + if (oldResource == null || preparedResource == null) { + log.debug("Failed to update resource instances names : oldResource or preparedResource is null"); + } else { + if (CollectionUtils.isNotEmpty(oldResource.getComponentInstances())) { + Map oldInstances = oldResource.getComponentInstances().stream() + .collect(toMap(ComponentInstance::getInvariantName, ComponentInstance::getName)); + List updatedInstances = preparedResource.getComponentInstances().stream() + .filter(i -> oldInstances.containsKey(i.getInvariantName()) && !i.getName().equals(oldInstances.get(i.getInvariantName()))) + .collect(toList()); + if (CollectionUtils.isNotEmpty(updatedInstances)) { + if (isTopologyChanged) { + updatedInstances.stream().filter(i -> !i.isCreatedFromCsar()).forEach(i -> i.setName(oldInstances.get(i.getInvariantName()))); + } else { + updatedInstances.forEach(i -> i.setName(oldInstances.get(i.getInvariantName()))); + } + } + } + componentInstanceBusinessLogic.updateComponentInstance(ComponentTypeEnum.RESOURCE_PARAM_NAME, null, preparedResource.getUniqueId(), + csarInfo.getModifier().getUserId(), preparedResource.getComponentInstances(), false); + } + } + + private Either createOrUpdateArtifacts(ArtifactOperationEnum operation, List createdArtifacts, + String yamlFileName, CsarInfo csarInfo, Resource preparedResource, + NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts, + boolean inTransaction, boolean shouldLock) { + String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName(); + Resource resource = preparedResource; + Map>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts + .getNodeTypesArtifactsToHandle(); + if (preparedResource.getResourceType() == ResourceTypeEnum.CVFC) { + if (nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()) { + Either, ResponseFormat> handleNodeTypeArtifactsRes = handleNodeTypeArtifacts(preparedResource, + nodeTypesArtifactsToHandle.get(nodeName), createdArtifacts, csarInfo.getModifier(), inTransaction, true); + if (handleNodeTypeArtifactsRes.isRight()) { + return Either.right(handleNodeTypeArtifactsRes.right().value()); + } + } + } else { + Either 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); + } + + private Resource handleResourceGenericType(Resource resource) { + Resource genericResource = fetchAndSetDerivedFromGenericType(resource); + + if (resource.shouldGenerateInputs()) { + generateAndAddInputsFromGenericTypeProperties(resource, genericResource); + } + return genericResource; + } + + private Resource handleResourceGenericType(final Resource resource, final String topologyTemplateYaml, + final ParsedToscaYamlInfo parsedToscaYamlInfo, final String substitutionMappingNodeType) { + if (processSubstitutableAsNodeType(resource, parsedToscaYamlInfo)) { + final Map substitutableAsNodeType = getSubstitutableAsNodeTypeFromTemplate( + (Map) new Yaml().load(topologyTemplateYaml), substitutionMappingNodeType); + final Resource genericResource = fetchAndSetDerivedFromGenericType(resource, + (String) substitutableAsNodeType.get(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName())); + + generatePropertiesFromGenericType(resource, genericResource); + generatePropertiesFromNodeType(resource, substitutableAsNodeType); + final String resourceId = resource.getUniqueId(); + resource.getProperties().forEach(propertyDefinition -> propertyDefinition.setUniqueId( + UniqueIdBuilder.buildPropertyUniqueId(resourceId, propertyDefinition.getName()))); + createResourcePropertiesOnGraph(resource); + return genericResource; + } + return handleResourceGenericType(resource); + } + + private Either>>, ResponseFormat> findNodeTypesArtifactsToHandle( + final Map nodeTypesInfo, final CsarInfo csarInfo, final Resource oldResource) { + final Map>> nodeTypesArtifactsToHandle = new HashMap<>(); + Either>>, ResponseFormat> nodeTypesArtifactsToHandleRes = Either + .left(nodeTypesArtifactsToHandle); + try { + final Map> extractedVfcsArtifacts = CsarUtils.extractVfcsArtifactsFromCsar(csarInfo.getCsar()); + final Map> 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) { + final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + nodeTypesArtifactsToHandleRes = Either.right(responseFormat); + log.debug("Exception occurred when findNodeTypesUpdatedArtifacts, error is:{}", e.getMessage(), e); + } + return nodeTypesArtifactsToHandleRes; + } + + private void findAddNodeTypeArtifactsToHandle(CsarInfo csarInfo, + Map>> nodeTypesArtifactsToHandle, + Resource resource, Map> extractedVfcsArtifacts, String namespace, + ImmutablePair vfcToscaNames) { + EnumMap> 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 (!isEmpty(extractedVfcsArtifacts)) { + List 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 + curNodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class); + List 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(ArtifactOperationEnum.DELETE, artifactsToDelete); + } + } + if (isNotEmpty(curNodeTypeArtifactsToHandle)) { + nodeTypesArtifactsToHandle.put(namespace, curNodeTypeArtifactsToHandle); + } + } + + private Resource findVfcResource(CsarInfo csarInfo, Resource resource, String currVfcToscaName, String previousVfcToscaName, + StorageOperationStatus status) { + if (status != null && status != StorageOperationStatus.NOT_FOUND) { + log.debug("Error occurred during fetching node type with tosca name {}, error: {}", currVfcToscaName, status); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(status), csarInfo.getCsarUUID()); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.CREATE_RESOURCE); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status), csarInfo.getCsarUUID()); + } else if (StringUtils.isNotEmpty(currVfcToscaName)) { + return (Resource) toscaOperationFacade.getLatestByToscaResourceName(currVfcToscaName, resource.getModel()).left() + .on(st -> findVfcResource(csarInfo, resource, previousVfcToscaName, null, st)); + } + return null; + } + + private EnumMap> findNodeTypeArtifactsToHandle(Resource curNodeType, + List extractedArtifacts) { + try { + List artifactsToUpload = new ArrayList<>(extractedArtifacts); + List artifactsToUpdate = new ArrayList<>(); + List artifactsToDelete = new ArrayList<>(); + processExistingNodeTypeArtifacts(extractedArtifacts, artifactsToUpload, artifactsToUpdate, artifactsToDelete, + collectExistingArtifacts(curNodeType)); + return putFoundArtifacts(artifactsToUpload, artifactsToUpdate, artifactsToDelete); + } catch (Exception e) { + log.debug("Exception occurred when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e); + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); + } + } + + private EnumMap> putFoundArtifacts(List artifactsToUpload, + List artifactsToUpdate, + List artifactsToDelete) { + EnumMap> nodeTypeArtifactsToHandle = null; + if (!artifactsToUpload.isEmpty() || !artifactsToUpdate.isEmpty() || !artifactsToDelete.isEmpty()) { + nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class); + if (!artifactsToUpload.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, artifactsToUpload); + } + if (!artifactsToUpdate.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, artifactsToUpdate); + } + if (!artifactsToDelete.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete); + } + } + return nodeTypeArtifactsToHandle; + } + + private void processExistingNodeTypeArtifacts(List extractedArtifacts, List artifactsToUpload, + List artifactsToUpdate, List artifactsToDelete, + Map existingArtifacts) { + if (!existingArtifacts.isEmpty()) { + extractedArtifacts.forEach(a -> processNodeTypeArtifact(artifactsToUpload, artifactsToUpdate, existingArtifacts, a)); + artifactsToDelete.addAll(existingArtifacts.values()); + } + } + + private void processNodeTypeArtifact(List artifactsToUpload, List artifactsToUpdate, + Map existingArtifacts, ArtifactDefinition currNewArtifact) { + Optional 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 ByActionStatusComponentException(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, + currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(), foundArtifact.get().getArtifactType()); + } + } + } + + private void updateFoundArtifact(List 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); + } + } + + private Map collectExistingArtifacts(Resource curNodeType) { + Map 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; + } + + /** + * Changes resource life cycle state to checked out + * + * @param resource + * @param user + * @param inTransaction + * @return + */ + private Either checkoutResource(Resource resource, User user, boolean inTransaction) { + Either checkoutResourceRes; + try { + if (!resource.getComponentMetadataDefinition().getMetadataDataDefinition().getState() + .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) { + log.debug("************* Going to change life cycle state of resource {} to not certified checked out. ", resource.getName()); + Either checkoutRes = lifecycleBusinessLogic + .changeComponentState(resource.getComponentType(), resource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, + new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT, LifecycleChanceActionEnum.CREATE_FROM_CSAR), inTransaction, true); + if (checkoutRes.isRight()) { + log.debug("Could not change state of component {} with uid {} to checked out. Status is {}. ", + resource.getComponentType().getNodeType(), resource.getUniqueId(), checkoutRes.right().value().getStatus()); + 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 occurred when checkoutResource {} , error is:{}", resource.getName(), e.getMessage(), e); + } + return checkoutResourceRes; + } + + /** + * Handles Artifacts of NodeType + * + * @param nodeTypeResource + * @param nodeTypeArtifactsToHandle + * @param user + * @param inTransaction + * @return + */ + public Either, ResponseFormat> handleNodeTypeArtifacts(Resource nodeTypeResource, + Map> nodeTypeArtifactsToHandle, + List createdArtifacts, User user, + boolean inTransaction, boolean ignoreLifecycleState) { + List handleNodeTypeArtifactsRequestRes; + Either, ResponseFormat> handleNodeTypeArtifactsRes = null; + Either changeStateResponse; + try { + changeStateResponse = checkoutResource(nodeTypeResource, user, inTransaction); + if (changeStateResponse.isRight()) { + return Either.right(changeStateResponse.right().value()); + } + nodeTypeResource = changeStateResponse.left().value(); + List handledNodeTypeArtifacts = new ArrayList<>(); + log.debug("************* Going to handle artifacts of node type resource {}. ", nodeTypeResource.getName()); + for (Entry> curOperationEntry : nodeTypeArtifactsToHandle.entrySet()) { + ArtifactOperationEnum curOperation = curOperationEntry.getKey(); + List curArtifactsToHandle = curOperationEntry.getValue(); + if (curArtifactsToHandle != null && !curArtifactsToHandle.isEmpty()) { + log.debug("************* Going to {} artifact to vfc {}", curOperation.name(), nodeTypeResource.getName()); + handleNodeTypeArtifactsRequestRes = artifactsBusinessLogic + .handleArtifactsRequestForInnerVfcComponent(curArtifactsToHandle, nodeTypeResource, user, createdArtifacts, + new ArtifactOperationInfo(false, ignoreLifecycleState, curOperation), false, inTransaction); + if (ArtifactOperationEnum.isCreateOrLink(curOperation)) { + createdArtifacts.addAll(handleNodeTypeArtifactsRequestRes); + } + handledNodeTypeArtifacts.addAll(handleNodeTypeArtifactsRequestRes); + } + } + handleNodeTypeArtifactsRes = Either.left(handledNodeTypeArtifacts); + } catch (Exception e) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR); + handleNodeTypeArtifactsRes = Either.right(responseFormat); + log.debug("Exception occurred when handleVfcArtifacts, error is:{}", e.getMessage(), e); + } + return handleNodeTypeArtifactsRes; + } + + private Map> extractVfcToscaNames(final Map nodeTypesInfo, + final String vfResourceName, final CsarInfo csarInfo) { + final Map> vfcToscaNames = new HashMap<>(); + final Map nodes = extractAllNodes(nodeTypesInfo, csarInfo); + if (!nodes.isEmpty()) { + for (Entry nodeType : nodes.entrySet()) { + final ImmutablePair toscaResourceName = buildNestedToscaResourceName(ResourceTypeEnum.VFC.name(), vfResourceName, + nodeType.getKey()); + vfcToscaNames.put(nodeType.getKey(), toscaResourceName); + } + } + for (final NodeTypeInfo cvfc : nodeTypesInfo.values()) { + vfcToscaNames.put(cvfc.getType(), buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), vfResourceName, cvfc.getType())); + } + return vfcToscaNames; + } + + private Map extractAllNodes(Map nodeTypesInfo, CsarInfo csarInfo) { + Map nodes = new HashMap<>(); + for (NodeTypeInfo nodeTypeInfo : nodeTypesInfo.values()) { + extractNodeTypes(nodes, nodeTypeInfo.getMappedToscaTemplate()); + } + extractNodeTypes(nodes, csarInfo.getMappedToscaMainTemplate()); + return nodes; + } + + private void extractNodeTypes(Map nodes, Map mappedToscaTemplate) { + Either, ResultStatusEnum> eitherNodeTypes = ImportUtils + .findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES); + if (eitherNodeTypes.isLeft()) { + nodes.putAll(eitherNodeTypes.left().value()); + } + } + + public Resource createResourceFromCsar(Resource resource, User user, Map csarUIPayload, String csarUUID) { + log.trace("************* created successfully from YAML, resource TOSCA "); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.STARTED, "Starting to create Resource From Csar by user {}", + user.getUserId()); + OnboardedCsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, csarUIPayload, csarUUID, null); + + Map nodeTypesInfo = csarInfo.extractTypesInfo(); + final String model = resource.getModel(); + if (StringUtils.isNotEmpty(model)) { + final Map dataTypesToCreate = getDatatypesToCreate(model, csarInfo.getDataTypes()); + final Map policyTypesToCreate = getPolicytypesToCreate(model, csarInfo.getPolicyTypes()); + if (MapUtils.isNotEmpty(dataTypesToCreate) || MapUtils.isNotEmpty(policyTypesToCreate)) { + createModel(resource, csarInfo.getVfResourceName()); + } + if (MapUtils.isNotEmpty(dataTypesToCreate)) { + dataTypeBusinessLogic.createDataTypeFromYaml(new Yaml().dump(dataTypesToCreate), model, true); + } + if (MapUtils.isNotEmpty(policyTypesToCreate)) { + policyTypeBusinessLogic.createPolicyTypeFromYaml(new Yaml().dump(policyTypesToCreate), model, true); + } + } + Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle( + nodeTypesInfo, csarInfo, resource); + if (findNodeTypesArtifactsToHandleRes.isRight()) { + log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID()); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "error: {}", findNodeTypesArtifactsToHandleRes.right().value()); + throw new ByResponseFormatComponentException(findNodeTypesArtifactsToHandleRes.right().value()); + } + Resource vfResource = createResourceFromYaml(resource, csarInfo.getMainTemplateContent(), csarInfo.getMainTemplateName(), nodeTypesInfo, + csarInfo, findNodeTypesArtifactsToHandleRes.left().value(), true, false, null); + log.trace("*************VF Resource created successfully from YAML, resource TOSCA name: {}", vfResource.getToscaResourceName()); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.COMPLETE, "Ended create Resource From Csar by user {}", + user.getUserId()); + return vfResource; + } + + private Resource validateResourceBeforeCreate(Resource resource, User user, boolean inTransaction) { + log.trace("validating resource before create"); + user.copyData(validateUser(user, CREATE_RESOURCE, resource, AuditingActionEnum.CREATE_RESOURCE, false)); + // validate user role + validateUserRole(user, resource, new ArrayList<>(), AuditingActionEnum.CREATE_RESOURCE, null); + // VF / PNF "derivedFrom" should be null (or ignored) + if (ModelConverter.isAtomicComponent(resource)) { + validateDerivedFromNotEmpty(user, resource, AuditingActionEnum.CREATE_RESOURCE); + } + return validateResourceBeforeCreate(resource, user, AuditingActionEnum.CREATE_RESOURCE, inTransaction, null); + } + + private Resource createResourceFromYaml(Resource resource, String topologyTemplateYaml, String yamlName, Map nodeTypesInfo, + CsarInfo csarInfo, + Map>> nodeTypesArtifactsToCreate, + boolean shouldLock, boolean inTransaction, String nodeName) { + List 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 ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", resource.getName()); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, ""); + 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()); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, + "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; + } + + public Map createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, Map mappedToscaTemplate, + boolean needLock, + Map>> nodeTypesArtifactsToHandle, + List nodeTypesNewCreatedArtifacts, + Map nodeTypesInfo, CsarInfo csarInfo, + final String substitutableAsNodeType) { + Either toscaVersion = findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION); + if (toscaVersion.isRight()) { + throw new ByActionStatusComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE); + } + Map mapToConvert = new HashMap<>(); + mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value()); + final Map nodeTypes = getNodeTypesFromTemplate(mappedToscaTemplate, substitutableAsNodeType); + createNodeTypes(yamlName, resource, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert, + nodeTypes); + return csarInfo.getCreatedNodes(); + } + + private Map getNodeTypesFromTemplate(final Map mappedToscaTemplate, final String substitutableAsNodeType) { + final Map nodeTypes = getAllNodeTypesInTemplate(mappedToscaTemplate); + if (StringUtils.isNotEmpty(substitutableAsNodeType)) { + nodeTypes.remove(substitutableAsNodeType); + } + return nodeTypes; + } + + @SuppressWarnings("unchecked") + private Map getSubstitutableAsNodeTypeFromTemplate(final Map mappedToscaTemplate, + final String substitutableAsNodeType) { + return (Map) getAllNodeTypesInTemplate(mappedToscaTemplate).get(substitutableAsNodeType); + } + + private Map getAllNodeTypesInTemplate(final Map mappedToscaTemplate) { + return ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES).left().orValue(HashMap::new); + } + + private void createModel(final Resource resource, final String vfResourcename) { + final String nameForGeneratedModel = resource.getModel() + "_" + vfResourcename + resource.getCsarVersion(); + Model model = new Model(nameForGeneratedModel, resource.getModel(), ModelTypeEnum.NORMATIVE_EXTENSION); + modelBusinessLogic.createModel(model); + resource.setModel(nameForGeneratedModel); + } + + private Map getDatatypesToCreate(final String model, final Map dataTypes) { + final Map dataTypesToCreate = new HashMap<>(); + for (final String dataType : dataTypes.keySet()) { + final Either result = + propertyOperation.getDataTypeByName(dataType, model); + if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) { + dataTypesToCreate.put(dataType, dataTypes.get(dataType)); + } + } + return dataTypesToCreate; + } + + private Map getPolicytypesToCreate(final String model, final Map policyTypes) { + final Map policyTypesToCreate = new HashMap<>(); + for (final String policyType : policyTypes.keySet()) { + final Either result = + policyTypeOperation.getLatestPolicyTypeByType(policyType, model); + if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) { + policyTypesToCreate.put(policyType, policyTypes.get(policyType)); + } + } + return policyTypesToCreate; + } + + private void createNodeTypes(String yamlName, Resource resource, boolean needLock, + Map>> nodeTypesArtifactsToHandle, + List nodeTypesNewCreatedArtifacts, Map nodeTypesInfo, CsarInfo csarInfo, + Map mapToConvert, Map nodeTypes) { + Iterator> nodesNameValueIter = nodeTypes.entrySet().iterator(); + Resource vfcCreated = null; + while (nodesNameValueIter.hasNext()) { + Entry nodeType = nodesNameValueIter.next(); + Map> 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 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()); + } + } + + private Resource handleNestedVfc(Resource resource, Map>> nodesArtifactsToHandle, + List createdArtifacts, Map nodesInfo, CsarInfo csarInfo, + String nodeName) { + String yamlName = nodesInfo.get(nodeName).getTemplateFileName(); + Map nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate(); + log.debug("************* Going to create node types from yaml {}", yamlName); + createResourcesFromYamlNodeTypesList(yamlName, resource, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts, + Collections.emptyMap(), csarInfo, resource.getModel()); + 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 = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName); + } + return resource; + } + + private Resource handleComplexVfc(final Resource resource, + final Map>> nodesArtifactsToHandle, + final List createdArtifacts, Map nodesInfo, CsarInfo csarInfo, + final String nodeName, final String yamlName) { + Resource oldComplexVfc = null; + Resource newComplexVfc = buildValidComplexVfc(resource, csarInfo, nodeName, nodesInfo); + Either oldComplexVfcRes = toscaOperationFacade + .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName()); + if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) { + oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName( + buildNestedToscaResourceName(ResourceTypeEnum.CVFC.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 ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); + } else if (oldComplexVfcRes.isLeft()) { + log.debug(VALIDATE_DERIVED_BEFORE_UPDATE); + final Either eitherValidation = 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()); + final LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT, + LifecycleChanceActionEnum.CREATE_FROM_CSAR); + log.debug("Going to certify cvfc {}. ", newComplexVfc.getName()); + final Resource result = propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, true); + csarInfo.getCreatedNodes().put(nodeName, result); + csarInfo.removeNodeFromQueue(); + return result; + } + + private Resource handleComplexVfc(Map>> nodesArtifactsToHandle, + List createdArtifacts, Map nodesInfo, CsarInfo csarInfo, + String nodeName, String yamlName, Resource oldComplexVfc, Resource newComplexVfc) { + Resource handleComplexVfcRes; + Map mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate(); + String yamlContent = new String(csarInfo.getCsar().get(yamlName)); + Map newNodeTypesInfo = nodesInfo.entrySet().stream().collect(toMap(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; + } + + private Resource buildValidComplexVfc(Resource resource, CsarInfo csarInfo, String nodeName, Map 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); + } + + private String getNodeTypeActualName(final String nodeTypefullName, final String nodeTypeNamePrefix) { + final String nameWithouNamespacePrefix = nodeTypefullName.substring(nodeTypeNamePrefix.length()); + final String[] findTypes = nameWithouNamespacePrefix.split("\\."); + if (findTypes.length > 1) { + final String resourceType = findTypes[0]; + return nameWithouNamespacePrefix.substring(resourceType.length()); + } + return nameWithouNamespacePrefix; + } + + private ImmutablePair createNodeTypeResourceFromYaml(final String yamlName, final Entry nodeNameValue, + User user, final Map mapToConvert, + final Resource resourceVf, final boolean needLock, + final Map> nodeTypeArtifactsToHandle, + final List nodeTypesNewCreatedArtifacts, + final boolean forceCertificationAllowed, final CsarInfo csarInfo, + final boolean isNested) { + final UploadResourceInfo resourceMetaData = fillResourceMetadata(yamlName, resourceVf, nodeNameValue.getKey(), user); + final String singleVfcYaml = buildNodeTypeYaml(nodeNameValue, mapToConvert, resourceMetaData.getResourceType(), csarInfo); + user = validateUser(user, "CheckIn Resource", resourceVf, AuditingActionEnum.CHECKIN_RESOURCE, true); + return createResourceFromNodeType(singleVfcYaml, resourceMetaData, user, true, needLock, nodeTypeArtifactsToHandle, + nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeNameValue.getKey(), isNested); + } + + private String buildNodeTypeYaml(final Entry nodeNameValue, final Map mapToConvert, final String nodeResourceType, + final 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. + final DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + final Yaml yaml = new Yaml(options); + final Map 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); + } + + public Boolean validateResourceCreationFromNodeType(Resource resource, User creator) { + validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE); + return true; + } + + public ImmutablePair createResourceFromNodeType(String nodeTypeYaml, UploadResourceInfo resourceMetaData, User creator, + boolean isInTransaction, boolean needLock, + Map> nodeTypeArtifactsToHandle, + List nodeTypesNewCreatedArtifacts, + boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName, + boolean isNested) { + LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT, + LifecycleChanceActionEnum.CREATE_FROM_CSAR); + Function validator = resource -> validateResourceCreationFromNodeType(resource, creator); + return resourceImportManager + .importCertifiedResource(nodeTypeYaml, resourceMetaData, creator, validator, lifecycleChangeInfo, isInTransaction, true, needLock, + nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeName, isNested); + } + + /** + * Validates if a given node type name has a valid prefix. + * + * @param nodeName node name from definition file + * @param definedResourceNamespaceList is a list of all node type name prefix allowed + * @return a valid node type name prefix if it`s found + */ + public Optional validateNodeTypeNamePrefix(final String nodeName, final List definedResourceNamespaceList) { + for (final String validNamespace : definedResourceNamespaceList) { + if (nodeName.startsWith(validNamespace)) { + return Optional.of(validNamespace); + } + } + return Optional.empty(); + } + + private List getDefinedNodeTypeNamespaceList() { + return ConfigurationManager.getConfigurationManager().getConfiguration().getDefinedResourceNamespace(); + } + + private UploadResourceInfo fillResourceMetadata(final String yamlName, final Resource resourceVf, final String nodeName, final User user) { + final UploadResourceInfo resourceMetaData = new UploadResourceInfo(); + final String nodeTypeNamePrefix = getNodeTypeNamePrefix(nodeName); + log.debug("Node type Name prefix {}", nodeTypeNamePrefix); + if (!nodeName.startsWith(nodeTypeNamePrefix)) { + log.debug("invalid nodeName:{} does not start with {}.", nodeName, getDefinedNodeTypeNamespaceList()); + throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName); + } + final String actualName = this.getNodeTypeActualName(nodeName, nodeTypeNamePrefix); + final String namePrefix = nodeName.replace(actualName, ""); + String resourceType = namePrefix.substring(nodeTypeNamePrefix.length()); + log.debug("initial namePrefix:{} resourceType {}. nodeName {} , actualName {} prefix {}", namePrefix, resourceType, nodeName, actualName, + nodeTypeNamePrefix); + // 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(); + } + if (!ResourceTypeEnum.containsIgnoreCase(resourceType)) { + 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 ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName); + } + // Setting name + resourceMetaData.setName(resourceVf.getSystemName() + actualName); + // Setting type from name + final 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.setTenant(resourceVf.getTenant()); + resourceMetaData.setVendorRelease(resourceVf.getVendorRelease()); + resourceMetaData.setModel(resourceVf.getModel()); + // Setting tag + final List tags = new ArrayList<>(); + tags.add(resourceMetaData.getName()); + resourceMetaData.setTags(tags); + // Setting category + final CategoryDefinition category = new CategoryDefinition(); + category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME); + final SubCategoryDefinition subCategory = new SubCategoryDefinition(); + subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY); + category.addSubCategory(subCategory); + final List categories = new ArrayList<>(); + categories.add(category); + resourceMetaData.setCategories(categories); + return resourceMetaData; + } + + private Resource buildComplexVfcMetadata(final Resource resourceVf, final CsarInfo csarInfo, final String nodeName, + final Map nodesInfo) { + final Resource cvfc = new Resource(); + final 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.CVFC); + 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.setTenant(resourceVf.getTenant()); + cvfc.setVendorRelease(resourceVf.getVendorRelease()); + cvfc.setModel(resourceVf.getModel()); + cvfc.setResourceVendorModelNumber(resourceVf.getResourceVendorModelNumber()); + cvfc.setToscaResourceName(buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), csarInfo.getVfResourceName(), nodeName).getLeft()); + cvfc.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID()); + final List tags = new ArrayList<>(); + tags.add(cvfc.getName()); + cvfc.setTags(tags); + final CategoryDefinition category = new CategoryDefinition(); + category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME); + SubCategoryDefinition subCategory = new SubCategoryDefinition(); + subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY); + category.addSubCategory(subCategory); + final List 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(final String resourceVfName, final 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 Resource createResourceAndRIsFromYaml(final String yamlName, Resource resource, final ParsedToscaYamlInfo parsedToscaYamlInfo, + final AuditingActionEnum actionEnum, final boolean isNormative, + final List createdArtifacts, final String topologyTemplateYaml, + final Map nodeTypesInfo, final CsarInfo csarInfo, + final Map>> nodeTypesArtifactsToCreate, + final boolean shouldLock, final boolean inTransaction, final String nodeName) { + final List nodeTypesNewCreatedArtifacts = new ArrayList<>(); + if (shouldLock) { + final Either lockResult = lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE); + if (lockResult.isRight()) { + rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ByResponseFormatComponentException(lockResult.right().value()); + } + log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult); + } + try { + log.trace("************* createResourceFromYaml before full create resource {}", yamlName); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, + "Starting to add inputs from yaml: {}", yamlName); + if (processSubstitutableAsNodeType(resource, parsedToscaYamlInfo)) { + final Map substitutableAsNodeType = getSubstitutableAsNodeTypeFromTemplate( + (Map) new Yaml().load(topologyTemplateYaml), parsedToscaYamlInfo.getSubstitutionMappingNodeType()); + resource.setToscaResourceName(parsedToscaYamlInfo.getSubstitutionMappingNodeType()); + final Resource genericResource = fetchAndSetDerivedFromGenericType(resource, + (String) substitutableAsNodeType.get(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName())); + resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative); + generatePropertiesFromGenericType(resource, genericResource); + generatePropertiesFromNodeType(resource, substitutableAsNodeType); + final String resourceId = resource.getUniqueId(); + resource.getProperties().forEach(propertyDefinition -> propertyDefinition.setUniqueId( + UniqueIdBuilder.buildPropertyUniqueId(resourceId, propertyDefinition.getName()))); + + createResourcePropertiesOnGraph(resource); + final Map instancesToCreate = getInstancesToCreate(parsedToscaYamlInfo, resource.getModel()); + + if (MapUtils.isNotEmpty(instancesToCreate)) { + log.trace("************* Going to create nodes, RI's and Relations from yaml {}", yamlName); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, + "Start create nodes, RI and Relations from yaml: {}", yamlName); + resource = createRIAndRelationsFromYaml(yamlName, resource, instancesToCreate, topologyTemplateYaml, + nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName, + parsedToscaYamlInfo.getSubstitutionMappingNodeType()); + } + } else { + final Resource genericResource = fetchAndSetDerivedFromGenericType(resource, null); + resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative); + log.trace("************* createResourceFromYaml after full create resource {}", yamlName); + log.trace("************* Going to add inputs from yaml {}", yamlName); + if (resource.shouldGenerateInputs()) { + generateAndAddInputsFromGenericTypeProperties(resource, genericResource); + } + final Map inputs = parsedToscaYamlInfo.getInputs(); + resource = createInputsOnResource(resource, inputs); + + log.trace("************* Finish to add inputs from yaml {}", yamlName); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, + "Finish to add inputs from yaml: {}", yamlName); + if (resource.getResourceType() == ResourceTypeEnum.PNF) { + log.trace("************* Adding generic properties to PNF"); + resource = (Resource) propertyBusinessLogic.copyPropertyToComponent(resource, genericResource.getProperties()); + log.trace("************* Adding software information to PNF"); + softwareInformationBusinessLogic.setSoftwareInformation(resource, csarInfo); + log.trace("************* Removing non-mano software information file from PNF"); + if (csarInfo.getSoftwareInformationPath().isPresent() && !softwareInformationBusinessLogic.removeSoftwareInformationFile( + csarInfo)) { + log.warn(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(), "catalog-be", + "Could not remove the software information file."); + } + } + final Map instancesToCreate = getInstancesToCreate(parsedToscaYamlInfo); + + log.trace("************* Going to create nodes, RI's and Relations from yaml {}", yamlName); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, + "Start create nodes, RI and Relations from yaml: {}", yamlName); + resource = createRIAndRelationsFromYaml(yamlName, resource, instancesToCreate, topologyTemplateYaml, + nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName, null); + } + log.trace("************* Finished to create nodes, RI and Relation from yaml {}", yamlName); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, + "Finished to create nodes, RI and Relation from yaml: {}", yamlName); + // validate update vf module group names + Optional> asdGroups = checkAndCreateAsdTypeVfModules(parsedToscaYamlInfo.getInstances()); + Map parsedGroups = parsedToscaYamlInfo.getGroups(); + if (asdGroups.isPresent()) { + parsedGroups.putAll(asdGroups.get()); + } + final Either, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic + .validateUpdateVfGroupNames(parsedGroups, resource.getSystemName()); + if (validateUpdateVfGroupNamesRes.isRight()) { + rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ByResponseFormatComponentException(validateUpdateVfGroupNamesRes.right().value()); + } + // add groups to resource + final Map groups; + log.trace("************* Going to add groups from yaml {}", yamlName); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, + "Start to add groups from yaml: {}", yamlName); + if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) { + groups = validateUpdateVfGroupNamesRes.left().value(); + } else { + groups = parsedGroups; + } + final Either createGroupsOnResource = createGroupsOnResource(resource, groups); + if (createGroupsOnResource.isRight()) { + rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR while adding groups from yaml: {}", yamlName); + throw new ByResponseFormatComponentException(createGroupsOnResource.right().value()); + } + resource = createGroupsOnResource.left().value(); + log.trace("************* Finished to add groups from yaml {}", yamlName); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, + "Finished to add groups from yaml: {}", yamlName); + log.trace("************* Going to add artifacts from yaml {}", yamlName); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, + "Started to add artifacts from yaml: {}", yamlName); + log.trace("************* Starting to add policies from yaml {}", yamlName); + Map policies = parsedToscaYamlInfo.getPolicies(); + if (MapUtils.isNotEmpty(policies)) { + resource = createPoliciesOnResource(resource, policies); + } + log.trace("************* Finished to add policies from yaml {}", yamlName); + final NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, + nodeTypesArtifactsToCreate); + final Either createArtifactsEither = createOrUpdateArtifacts(ArtifactOperationEnum.CREATE, createdArtifacts, + yamlName, csarInfo, resource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock); + if (createArtifactsEither.isRight()) { + rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "error happened {}", createArtifactsEither.right().value()); + throw new ByResponseFormatComponentException(createArtifactsEither.right().value()); + } + loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, + "Finished to add artifacts from yaml: " + resource.getToscaResourceName()); + final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED); + componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, actionEnum); + ASDCKpiApi.countCreatedResourcesKPI(); + return resource; + } catch (final BusinessLogicException e) { + log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(), + "An error has occurred during resource and resource instance creation", e); + rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ByResponseFormatComponentException(e.getResponseFormat()); + } catch (final ComponentException e) { + log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ResourceBusinessLogic.class.getName(), + "An error has occurred during resource and resource instance creation", e); + rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ByResponseFormatComponentException(e.getResponseFormat()); + } catch (final Exception e) { + log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(), + "An error has occurred during resource and resource instance creation", e); + rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts); + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); + } finally { + if (!inTransaction) { + janusGraphDao.commit(); + } + if (shouldLock) { + graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource); + } + } + } + + private Optional> checkAndCreateAsdTypeVfModules(Map instances) { + Map addAsdGroups = new HashMap<>(); + if (isNotEmpty(instances) || instances != null) { + for (Map.Entry instance : instances.entrySet()) { + if (isNotEmpty(instance.getValue().getArtifacts()) || instance.getValue().getArtifacts() != null) { + Map artifactsMap = instance.getValue().getArtifacts() + .get(ToscaTagNamesEnum.ARTIFACTS.getElementName()); + if (isNotEmpty(artifactsMap) || artifactsMap != null) { + for (Map.Entry artifact : artifactsMap.entrySet()) { + if (artifact.getValue().getType().equals(Constants.ASD_DEPLOYMENT_ITEM)) { + GroupDefinition groupDefinition = new GroupDefinition(); + groupDefinition.setName(artifact.getKey()); + groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE); + addAsdTypeProperties(groupDefinition); + addAsdGroups.put(groupDefinition.getName(), groupDefinition); + } + } + } + } + } + } + return Optional.of(addAsdGroups); + } + + private void addAsdTypeProperties(GroupDefinition groupDefinition) { + List properties = new ArrayList<>(); + GroupProperty propIsBase = new GroupProperty(); + propIsBase.setName(Constants.IS_BASE); + propIsBase.setValue("true"); + properties.add(propIsBase); + GroupProperty propVfModuleLabel = new GroupProperty(); + propVfModuleLabel.setName(Constants.VF_MODULE_LABEL); + propVfModuleLabel.setValue(groupDefinition.getName()); + properties.add(propVfModuleLabel); + GroupProperty propVfModuleDescription = new GroupProperty(); + propVfModuleDescription.setName(Constants.VF_MODULE_DESCRIPTION); + propVfModuleDescription.setValue("VF Module representing deployment item " + groupDefinition.getName()); + properties.add(propVfModuleDescription); + GroupProperty propMinVfModuleInstances = new GroupProperty(); + propMinVfModuleInstances.setName(Constants.MIN_VF_MODULE_INSTANCES); + propMinVfModuleInstances.setValue("1"); + properties.add(propMinVfModuleInstances); + GroupProperty propMaxVfModuleInstances = new GroupProperty(); + propMaxVfModuleInstances.setName(Constants.MAX_VF_MODULE_INSTANCES); + propMaxVfModuleInstances.setValue("1"); + properties.add(propMaxVfModuleInstances); + GroupProperty propInitialCount = new GroupProperty(); + propInitialCount.setName(Constants.INITIAL_COUNT); + propInitialCount.setValue("1"); + properties.add(propInitialCount); + GroupProperty propVfModuleType = new GroupProperty(); + propVfModuleType.setName(Constants.VF_MODULE_TYPE); + propVfModuleType.setValue("Base"); + properties.add(propVfModuleType); + GroupProperty propVolumeGroup = new GroupProperty(); + propVolumeGroup.setName(Constants.VOLUME_GROUP); + propVolumeGroup.setValue("false"); + properties.add(propVolumeGroup); + groupDefinition.convertFromGroupProperties(properties); + } + + private boolean processSubstitutableAsNodeType(final Resource resource, final ParsedToscaYamlInfo parsedToscaYamlInfo) { + return !resource.getResourceType().isAtomicType() && StringUtils.isNotEmpty(resource.getModel()) + && parsedToscaYamlInfo.getSubstitutionMappingNodeType() != null; + } + + private Map getInstancesToCreate(final ParsedToscaYamlInfo parsedToscaYamlInfo) { + return getInstancesToCreate(parsedToscaYamlInfo, null); + } + + private Map getInstancesToCreate(final ParsedToscaYamlInfo parsedToscaYamlInfo, final String model) { + if (StringUtils.isEmpty(model) || StringUtils.isEmpty(parsedToscaYamlInfo.getSubstitutionMappingNodeType())) { + return parsedToscaYamlInfo.getInstances(); + } + return parsedToscaYamlInfo.getInstances().entrySet().stream() + .filter(entry -> !parsedToscaYamlInfo.getSubstitutionMappingNodeType().equals(entry.getValue().getType())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } + + private void rollback(boolean inTransaction, Resource resource, List createdArtifacts, + List nodeTypesNewCreatedArtifacts) { + if (!inTransaction) { + 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()); + } + } + + private Resource getResourceWithGroups(String resourceId) { + ComponentParametersView filter = new ComponentParametersView(); + filter.setIgnoreGroups(false); + Either updatedResource = toscaOperationFacade.getToscaElement(resourceId, filter); + if (updatedResource.isRight()) { + rollbackWithException(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resourceId); + } + return updatedResource.left().value(); + } + + private Either createGroupsOnResource(Resource resource, Map groups) { + if (groups != null && !groups.isEmpty()) { + List groupsAsList = updateGroupsMembersUsingResource(groups, resource); + handleGroupsProperties(resource, groups); + fillGroupsFinalFields(groupsAsList); + Either, ResponseFormat> createGroups = groupBusinessLogic.createGroups(resource, groupsAsList, true); + if (createGroups.isRight()) { + return Either.right(createGroups.right().value()); + } + } + return Either.left(resource); + } + + private void handleGroupsProperties(Resource resource, Map groups) { + List 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)); + } + } + + private Resource createPoliciesOnResource(Resource resource, Map policies) { + policyBusinessLogic.createPolicies(resource, policies); + return resource; + } + + private void handleGetInputs(PropertyDataDefinition property, List inputs) { + if (isNotEmpty(property.getGetInputValues())) { + if (inputs == null || inputs.isEmpty()) { + log.debug("Failed to add property {} to group. Inputs list is empty ", property); + rollbackWithException(ActionStatus.INPUTS_NOT_FOUND, + property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString()); + } + ListIterator getInputValuesIter = property.getGetInputValues().listIterator(); + while (getInputValuesIter.hasNext()) { + GetInputValueDataDefinition getInput = getInputValuesIter.next(); + Either inputEither = findInputByName(inputs, getInput); + if (inputEither.isRight()) { + throw inputEither.right().value(); + } else { + InputDefinition input = inputEither.left().value(); + getInput.setInputId(input.getUniqueId()); + if (getInput.getGetInputIndex() != null) { + GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex(); + Either newInputEither = findInputByName(inputs, getInputIndex); + if (newInputEither.isRight()) { + throw newInputEither.right().value(); + } else { + InputDefinition newInput = newInputEither.left().value(); + getInputIndex.setInputId(newInput.getUniqueId()); + } + getInputValuesIter.add(getInputIndex); + } + } + } + } + } + + Either rollbackWithEither(final ActionStatus actionStatus, final String... params) { + return rollbackWithEither(janusGraphDao, actionStatus, params); + } + + private Either findInputByName(List inputs, GetInputValueDataDefinition getInput) { + final String inputName = getInput != null ? getInput.getInputName() : ""; + if (inputs == null || inputs.isEmpty()) { + log.debug("#findInputByName - Inputs list is empty"); + return rollbackWithEither(ActionStatus.INPUTS_NOT_FOUND, inputName); + } else { + Optional inputOpt = inputs.stream().filter(p -> p.getName().equals(inputName)).findFirst(); + if (inputOpt.isEmpty()) { + log.debug("#findInputByName - Failed to find the input {} ", inputName); + return rollbackWithEither(ActionStatus.INPUTS_NOT_FOUND, inputName); + } else { + return Either.left(inputOpt.get()); + } + } + } + + private void fillGroupsFinalFields(List groupsAsList) { + groupsAsList.forEach(groupDefinition -> { + groupDefinition.setInvariantName(groupDefinition.getName()); + groupDefinition.setCreatedFrom(CreatedFrom.CSAR); + }); + } + + private Resource updateGroupsOnResource(Resource resource, Map groups) { + if (isEmpty(groups)) { + return resource; + } + return updateOrCreateGroups(resource, groups); + } + + private Resource updateOrCreateGroups(Resource resource, Map groups) { + List groupsFromResource = resource.getGroups(); + List groupsAsList = updateGroupsMembersUsingResource(groups, resource); + List groupsToUpdate = new ArrayList<>(); + List groupsToDelete = new ArrayList<>(); + List 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)) { + groupBusinessLogic.addGroups(resource, groupsToCreate, true).left().on(this::throwComponentException); + } else { + groupBusinessLogic.createGroups(resource, groupsToCreate, true).left().on(this::throwComponentException); + } + } + if (isNotEmpty(groupsToDelete)) { + groupBusinessLogic.deleteGroups(resource, groupsToDelete).left().on(this::throwComponentException); + } + if (isNotEmpty(groupsToUpdate)) { + groupBusinessLogic.updateGroups(resource, groupsToUpdate, true).left().on(this::throwComponentException); + } + return resource; + } + + private void addGroupsToDelete(List groupsFromResource, List groupsAsList, + List groupsToDelete) { + for (GroupDefinition group : groupsFromResource) { + Optional op = groupsAsList.stream().filter(p -> p.getInvariantName().equalsIgnoreCase(group.getInvariantName())) + .findAny(); + if (op.isEmpty() && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) { + groupsToDelete.add(group); + } + } + } + + private void addGroupsToCreateOrUpdate(List groupsFromResource, List groupsAsList, + List groupsToUpdate, List groupsToCreate) { + for (GroupDefinition group : groupsAsList) { + Optional 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); + } + } + } + + private Resource createInputsOnResource(Resource resource, Map inputs) { + List resourceProperties = resource.getInputs(); + if (MapUtils.isNotEmpty(inputs) || isNotEmpty(resourceProperties)) { + Either, ResponseFormat> createInputs = inputsBusinessLogic.createInputsInGraph(inputs, resource); + if (createInputs.isRight()) { + loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "failed to add inputs from yaml: {}", createInputs.right().value()); + throw new ByResponseFormatComponentException(createInputs.right().value()); + } + resource.setInputs(createInputs.left().value()); + } + return resource; + } + + private Resource generatePropertiesFromNodeType(final Resource resource, final Map nodeType) { + final Either, ResultStatusEnum> properties = ImportUtils.getProperties(nodeType); + if (properties.isLeft()) { + final List propertiesList = new ArrayList<>(); + final Map value = properties.left().value(); + if (value != null) { + for (Entry entry : value.entrySet()) { + final String name = entry.getKey(); + final PropertyDefinition propertyDefinition = entry.getValue(); + propertyDefinition.setName(name); + propertiesList.add(propertyDefinition); + resource.getProperties().removeIf(p -> p.getName().equals(name)); + } + } + resource.getProperties().addAll(propertiesList); + } + return resource; + } + + private Resource createResourcePropertiesOnGraph(final Resource resource) { + final List resourceProperties = resource.getProperties(); + for (PropertyDefinition propertyDefinition : resourceProperties) { + final Either addPropertyEither = toscaOperationFacade + .addPropertyToComponent(propertyDefinition, resource); + + if (addPropertyEither.isRight()) { + final String error = String.format("failed to add properties from yaml: %s", addPropertyEither.right().value()); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_PROPERTIES, resource.getComponentMetadataForSupportLog(), + StatusCode.ERROR, + error); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addPropertyEither.right().value()), error); + } + } + return resource; + } + + private List updateGroupsMembersUsingResource(Map groups, Resource component) { + List result = new ArrayList<>(); + List componentInstances = component.getComponentInstances(); + if (groups != null) { + for (Entry entry : groups.entrySet()) { + String groupName = entry.getKey(); + GroupDefinition groupDefinition = entry.getValue(); + GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition); + + updatedGroupDefinition.setMembers(null); + Map members = groupDefinition.getMembers(); + if (members != null) { + updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members); + } + result.add(updatedGroupDefinition); + } + } + return result; + } + + private void updateGroupMembers(Map groups, GroupDefinition updatedGroupDefinition, Resource component, + List componentInstances, String groupName, Map members) { + Set compInstancesNames = members.keySet(); + if (CollectionUtils.isEmpty(componentInstances)) { + String membersAstString = String.join(",", compInstancesNames); + log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", membersAstString, + groupName, component.getNormalizedName()); + throw new ByActionStatusComponentException(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, + component.getNormalizedName(), getComponentTypeForResponse(component)); + } + // Find all component instances with the member names + Map memberNames = componentInstances.stream().collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId)); + memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> ""))); + Map relevantInstances = memberNames.entrySet().stream().filter(n -> compInstancesNames.contains(n.getKey())) + .collect(toMap(Entry::getKey, Entry::getValue)); + if (relevantInstances.size() != compInstancesNames.size()) { + List foundMembers = new ArrayList<>(relevantInstances.keySet()); + foundMembers.forEach(compInstancesNames::remove); + String membersAstString = String.join(",", compInstancesNames); + log.debug("The members: {}, in group: {}, cannot be found in component: {}", membersAstString, groupName, component.getNormalizedName()); + throw new ByActionStatusComponentException(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, + component.getNormalizedName(), getComponentTypeForResponse(component)); + } + updatedGroupDefinition.setMembers(relevantInstances); + } + + private Resource createRIAndRelationsFromYaml(String yamlName, Resource resource, + Map uploadComponentInstanceInfoMap, + String topologyTemplateYaml, List nodeTypesNewCreatedArtifacts, + Map nodeTypesInfo, CsarInfo csarInfo, + Map>> nodeTypesArtifactsToCreate, + String nodeName, final String substitutableAsNodeType) { + log.debug("************* Going to create all nodes {}", yamlName); + handleNodeTypes(yamlName, resource, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, nodeTypesInfo, + csarInfo, nodeName, substitutableAsNodeType); + log.debug("************* Finished to create all nodes {}", yamlName); + log.debug("************* Going to create all resource instances {}", yamlName); + Map existingNodeTypesByResourceNames = new HashMap<>(); + resource = createResourceInstances(yamlName, resource, null, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes(), + existingNodeTypesByResourceNames); + log.debug("************* Finished to create all resource instances {}", yamlName); + log.debug("************* Going to create all relations {}", yamlName); + resource = createResourceInstancesRelations(csarInfo.getModifier(), yamlName, resource, null, uploadComponentInstanceInfoMap, + existingNodeTypesByResourceNames); + log.debug("************* Finished to create all relations {}", yamlName); + log.debug("************* Going to create positions {}", yamlName); + compositionBusinessLogic.setPositionsForComponentInstances(resource, csarInfo.getModifier().getUserId()); + log.debug("************* Finished to set positions {}", yamlName); + return resource; + } + + private void handleAndAddExtractedVfcsArtifacts(List vfcArtifacts, List artifactsToAdd) { + List vfcArtifactNames = vfcArtifacts.stream().map(ArtifactDataDefinition::getArtifactName).collect(toList()); + artifactsToAdd.forEach(a -> { + if (!vfcArtifactNames.contains(a.getArtifactName())) { + vfcArtifacts.add(a); + } else { + log.debug("Can't upload two artifact with the same name {}. ", a.getArtifactName()); + } + }); + } + + @SuppressWarnings("unchecked") + private void handleNodeTypes(String yamlName, Resource resource, String topologyTemplateYaml, boolean needLock, + Map>> nodeTypesArtifactsToHandle, + List nodeTypesNewCreatedArtifacts, Map nodeTypesInfo, CsarInfo csarInfo, + String nodeName, String substitutableAsNodeType) { + try { + for (Entry nodeTypeEntry : nodeTypesInfo.entrySet()) { + if (nodeTypeEntry.getValue().isNested() && !nodeTypeAlreadyExists(nodeTypeEntry.getKey(), resource.getModel())) { + handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, + nodeTypeEntry.getKey()); + log.trace("************* finished to create node {}", nodeTypeEntry.getKey()); + } + } + Map mappedToscaTemplate = null; + if (StringUtils.isNotEmpty(nodeName) && isNotEmpty(nodeTypesInfo) && nodeTypesInfo.containsKey(nodeName)) { + mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate(); + } + if (isEmpty(mappedToscaTemplate)) { + mappedToscaTemplate = (Map) new Yaml().load(topologyTemplateYaml); + } + createResourcesFromYamlNodeTypesList(yamlName, resource, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle, + nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, substitutableAsNodeType); + } 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; + } + } + + private boolean nodeTypeAlreadyExists(final String toscaResourceName, String modelName) { + return toscaOperationFacade.getLatestByToscaResourceName(toscaResourceName, modelName).isLeft(); + } + + private Either handleVfCsarArtifacts(Resource resource, CsarInfo csarInfo, List createdArtifacts, + ArtifactOperationInfo artifactOperation, boolean shouldLock, + boolean inTransaction) { + if (csarInfo.getCsar() != null) { + String vendorLicenseModelId = null; + String vfLicenseModelId = null; + if (artifactOperation.isUpdate()) { + Map deploymentArtifactsMap = resource.getDeploymentArtifacts(); + if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) { + for (Entry 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(); + } + } + } + } + // Specific Behavior for license artifacts + 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); + Either eitherCreateResult + = createOrUpdateNonMetaArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, artifactOperation); + if (eitherCreateResult.isRight()) { + return Either.right(eitherCreateResult.right().value()); + } + Either, 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 createArtifactsFromCsar; + if (artifactOperation.isCreateOrLink()) { + createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic + .createResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts); + } else { + Either 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()); + } + } + return Either.left(resource); + } + + private Either createOrUpdateSingleNonMetaArtifact(Resource resource, CsarInfo csarInfo, String artifactPath, + String artifactFileName, String artifactType, + ArtifactGroupTypeEnum artifactGroupType, String artifactLabel, + String artifactDisplayName, String artifactDescription, + String artifactId, ArtifactOperationInfo operation, + List createdArtifacts, boolean isFromCsar, + boolean shouldLock, boolean inTransaction) { + byte[] artifactFileBytes = null; + if (csarInfo.getCsar().containsKey(artifactPath)) { + artifactFileBytes = csarInfo.getCsar().get(artifactPath); + } + Either result = Either.left(true); + if (operation.isUpdate() || operation.isDelete()) { + if (isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar)) { + Either handleDelete = artifactsBusinessLogic + .handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), resource, shouldLock, inTransaction); + if (handleDelete.isRight()) { + result = Either.right(handleDelete.right().value()); + } else { + ArtifactDefinition value = handleDelete.left().value(); + String updatedArtifactId = value.getUniqueId(); + if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) { + resource.getDeploymentArtifacts().remove(updatedArtifactId); + } else { + resource.getArtifacts().remove(updatedArtifactId); + } + } + return result; + } + if (StringUtils.isEmpty(artifactId) && artifactFileBytes != null) { + operation = new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE); + } + } + if (artifactFileBytes != null) { + Map vendorLicenseModelJson = ArtifactUtils + .buildJsonForUpdateArtifact(artifactId, artifactFileName, artifactType, artifactGroupType, artifactLabel, artifactDisplayName, + artifactDescription, artifactFileBytes, null, isFromCsar); + Either, ResponseFormat> eitherNonMetaArtifacts = csarArtifactsAndGroupsBusinessLogic + .createOrUpdateCsarArtifactFromJson(resource, csarInfo.getModifier(), vendorLicenseModelJson, operation); + addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts); + if (eitherNonMetaArtifacts.isRight()) { + BeEcompErrorManager.getInstance().logInternalFlowError("UploadLicenseArtifact", + "Failed to upload license artifact: " + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(), ErrorSeverity.WARNING); + return Either.right(eitherNonMetaArtifacts.right().value()); + } + ArtifactDefinition artifactDefinition = eitherNonMetaArtifacts.left().value().left().value(); + createOrUpdateResourceWithUpdatedArtifact(artifactDefinition, resource, artifactGroupType); + } + return result; + } + + private void createOrUpdateResourceWithUpdatedArtifact(ArtifactDefinition artifact, Resource resource, ArtifactGroupTypeEnum groupTypeEnum) { + if (groupTypeEnum == ArtifactGroupTypeEnum.DEPLOYMENT) { + resource.getDeploymentArtifacts().put(artifact.getArtifactLabel(), artifact); + } else { + resource.getArtifacts().put(artifact.getArtifactLabel(), artifact); + } + } + + private boolean isArtifactDeletionRequired(String artifactId, byte[] artifactFileBytes, boolean isFromCsar) { + return !StringUtils.isEmpty(artifactId) && artifactFileBytes == null && isFromCsar; + } + + private void addNonMetaCreatedArtifactsToSupportRollback(ArtifactOperationInfo operation, List createdArtifacts, + Either, ResponseFormat> eitherNonMetaArtifacts) { + if (operation.isCreateOrLink() && createdArtifacts != null && eitherNonMetaArtifacts.isLeft()) { + Either eitherResult = eitherNonMetaArtifacts.left().value(); + if (eitherResult.isLeft()) { + createdArtifacts.add(eitherResult.left().value()); + } + } + } + + private Either createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Resource resource, + List createdArtifacts, boolean shouldLock, + boolean inTransaction, ArtifactOperationInfo artifactOperation) { + Either resStatus = null; + Map>> collectedWarningMessages = new HashMap<>(); + try { + Either, String> artifactPathAndNameList = getValidArtifactNames(csarInfo, collectedWarningMessages); + if (artifactPathAndNameList.isRight()) { + return Either.right( + getComponentsUtils().getResponseFormatByArtifactId(ActionStatus.ARTIFACT_NAME_INVALID, artifactPathAndNameList.right().value(), + VALID_CHARACTERS_ARTIFACT_NAME)); + } + EnumMap> vfCsarArtifactsToHandle = null; + if (artifactOperation.isCreateOrLink()) { + vfCsarArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class); + vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value()); + } else { + Either>, 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 occurred in createNonMetaArtifacts, message:{}", e.getMessage(), e); + } finally { + CsarUtils.handleWarningMessages(collectedWarningMessages); + } + return resStatus; + } + + private Either processCsarArtifacts(CsarInfo csarInfo, Resource resource, List createdArtifacts, + boolean shouldLock, boolean inTransaction, + Either resStatus, + EnumMap> vfCsarArtifactsToHandle) { + for (Entry> currArtifactOperationPair : vfCsarArtifactsToHandle.entrySet()) { + Optional optionalCreateInDBError = + // Stream of artifacts to be created + currArtifactOperationPair.getValue().stream() + // create each artifact + .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 in only error + .filter(Either::isRight). + // Convert the error from either to + + // ResponseFormat + map(e -> e.right().value()). + // Check if an error occurred + findAny(); + // Error found on artifact Creation + if (optionalCreateInDBError.isPresent()) { + resStatus = Either.right(optionalCreateInDBError.get()); + break; + } + } + return resStatus; + } + + private Either, String> getValidArtifactNames(CsarInfo csarInfo, + Map>> collectedWarningMessages) { + List artifactPathAndNameList = + // Stream of file paths contained in csar + csarInfo.getCsar().entrySet().stream() + // Filter in only VF artifact path location + .filter(e -> Pattern.compile(VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()).matches()) + // Validate and add warnings + .map(e -> CsarUtils.validateNonMetaArtifact(e.getKey(), e.getValue(), collectedWarningMessages)) + // Filter in Non Warnings + .filter(Either::isLeft) + // Convert from Either to NonMetaArtifactInfo + .map(e -> e.left().value()) + // collect to List + .collect(toList()); + Pattern englishNumbersAndUnderScoresOnly = Pattern.compile(CsarUtils.VALID_ENGLISH_ARTIFACT_NAME); + for (NonMetaArtifactInfo nonMetaArtifactInfo : artifactPathAndNameList) { + if (!englishNumbersAndUnderScoresOnly.matcher(nonMetaArtifactInfo.getDisplayName()).matches()) { + return Either.right(nonMetaArtifactInfo.getArtifactName()); + } + } + return Either.left(artifactPathAndNameList); + } + + private Either>, ResponseFormat> findVfCsarArtifactsToHandle(Resource resource, + List artifactPathAndNameList, + User user) { + List existingArtifacts = new ArrayList<>(); + // collect all Deployment and Informational artifacts of VF + 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 MANDATORY artifacts, LICENSE artifacts and artifacts + + // was created from HEAT.meta + .filter(this::isNonMetaArtifact).collect(toList()); + List artifactsToIgnore = new ArrayList<>(); + // collect IDs of Artifacts of VF which belongs to any group + if (resource.getGroups() != null) { + resource.getGroups().forEach(g -> { + if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) { + artifactsToIgnore.addAll(g.getArtifacts()); + } + }); + } + existingArtifacts = existingArtifacts.stream() + // filter artifacts which belongs to any group + .filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(toList()); + return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, resource, user); + } + + private boolean isNonMetaArtifact(ArtifactDefinition artifact) { + return !artifact.getMandatory() && artifact.getArtifactName() != null && isValidArtifactType(artifact); + } + + private boolean isValidArtifactType(ArtifactDefinition artifact) { + return artifact.getArtifactType() != null && ArtifactTypeEnum.parse(artifact.getArtifactType()) != ArtifactTypeEnum.VENDOR_LICENSE + && ArtifactTypeEnum.parse(artifact.getArtifactType()) != ArtifactTypeEnum.VF_LICENSE; + } + + private Resource createResourceInstancesRelations(User user, String yamlName, Resource resource, Resource oldResource, + Map uploadResInstancesMap, + Map existingNodeTypesByResourceNames) { + log.debug("#createResourceInstancesRelations - Going to create relations "); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, + "Start to create relations"); + List componentInstancesList = resource.getComponentInstances(); + if (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); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "No instances found in the resource: {}, is empty, yaml template file name: {}", resource.getName(), yamlName); + BeEcompErrorManager.getInstance() + .logInternalDataError("createResourceInstancesRelations", "No instances found in a resource or nn yaml template. ", + ErrorSeverity.ERROR); + throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + Map> instProperties = new HashMap<>(); + Map>> instCapabilities = new HashMap<>(); + Map>> instRequirements = new HashMap<>(); + Map> instDeploymentArtifacts = new HashMap<>(); + Map> instArtifacts = new HashMap<>(); + Map> instAttributes = new HashMap<>(); + List relations = new ArrayList<>(); + Map> instInputs = new HashMap<>(); + Resource finalResource = resource; + uploadResInstancesMap.values().forEach( + i -> processComponentInstance(yamlName, finalResource, componentInstancesList, + componentsUtils.getAllDataTypes(applicationDataTypeCache, resource.getModel()), instProperties, instCapabilities, + instRequirements, instDeploymentArtifacts, instArtifacts, instAttributes, existingNodeTypesByResourceNames, instInputs, i)); + resource.getComponentInstances().stream().filter(i -> !i.isCreatedFromCsar()).forEach( + i -> processUiComponentInstance(oldResource, i, instCapabilities, instRequirements, instDeploymentArtifacts, instArtifacts, + instProperties, instInputs, instAttributes)); + associateComponentInstancePropertiesToComponent(yamlName, resource, instProperties); + associateComponentInstanceInputsToComponent(yamlName, resource, instInputs); + associateDeploymentArtifactsToInstances(user, yamlName, resource, instDeploymentArtifacts); + associateArtifactsToInstances(yamlName, resource, instArtifacts); + associateOrAddCalculatedCapReq(yamlName, resource, instCapabilities, instRequirements); + associateInstAttributeToComponentToInstances(yamlName, resource, instAttributes); + addRelationsToRI(yamlName, resource, uploadResInstancesMap, componentInstancesList, relations); + associateResourceInstances(yamlName, resource, relations); + handleSubstitutionMappings(resource, uploadResInstancesMap); + log.debug("************* in create relations, getResource start"); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, "create relations"); + Either eitherGetResource = toscaOperationFacade.getToscaFullElement(resource.getUniqueId()); + log.debug("************* in create relations, getResource end"); + if (eitherGetResource.isRight()) { + loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR while create relations"); + throw new ByResponseFormatComponentException( + componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource)); + } + return eitherGetResource.left().value(); + } + + private void processUiComponentInstance(Resource oldResource, ComponentInstance instance, + Map>> instCapabilities, + Map>> instRequirements, + Map> instDeploymentArtifacts, + Map> instArtifacts, + Map> instProperties, + Map> instInputs, + Map> instAttributes) { + Optional foundInstance = findInstance(oldResource, instance); + if (foundInstance.isPresent()) { + if (MapUtils.isNotEmpty(foundInstance.get().getCapabilities())) { + instCapabilities.put(instance, foundInstance.get().getCapabilities()); + } + if (MapUtils.isNotEmpty(foundInstance.get().getRequirements())) { + instRequirements.put(instance, foundInstance.get().getRequirements()); + } + if (MapUtils.isNotEmpty(foundInstance.get().getDeploymentArtifacts())) { + instDeploymentArtifacts.put(instance.getUniqueId(), foundInstance.get().getDeploymentArtifacts()); + } + if (MapUtils.isNotEmpty(foundInstance.get().getArtifacts())) { + instArtifacts.put(instance.getUniqueId(), foundInstance.get().getArtifacts()); + } + if (MapUtils.isNotEmpty(oldResource.getComponentInstancesProperties()) && CollectionUtils + .isNotEmpty(oldResource.getComponentInstancesProperties().get(foundInstance.get().getUniqueId()))) { + instProperties.put(instance.getUniqueId(), oldResource.getComponentInstancesProperties().get(foundInstance.get().getUniqueId())); + } + if (MapUtils.isNotEmpty(oldResource.getComponentInstancesInputs()) && CollectionUtils + .isNotEmpty(oldResource.getComponentInstancesInputs().get(foundInstance.get().getUniqueId()))) { + instInputs.put(instance.getUniqueId(), oldResource.getComponentInstancesInputs().get(foundInstance.get().getUniqueId())); + } + if (MapUtils.isNotEmpty(oldResource.getComponentInstancesAttributes()) && CollectionUtils + .isNotEmpty(oldResource.getComponentInstancesAttributes().get(foundInstance.get().getUniqueId()))) { + instAttributes.put(instance.getUniqueId(), + oldResource.getComponentInstancesAttributes().get(foundInstance.get().getUniqueId()).stream().map(AttributeDefinition::new) + .collect(toList())); + } + } + } + + private Optional findInstance(Resource oldResource, ComponentInstance instance) { + if (oldResource != null && CollectionUtils.isNotEmpty(oldResource.getComponentInstances())) { + return oldResource.getComponentInstances().stream().filter(i -> i.getName().equals(instance.getName())).findFirst(); + } + return Optional.empty(); + } + + private void associateResourceInstances(String yamlName, Resource resource, List relations) { + Either, 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 ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status), yamlName); + } else { + setResourceInstanceRelationsOnComponent(resource, relationsEither.left().value()); + } + } + + private void associateInstAttributeToComponentToInstances(String yamlName, Resource resource, + Map> 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 ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName); + } + } + + private void associateOrAddCalculatedCapReq(String yamlName, Resource resource, + Map>> instCapabilities, + Map>> 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 ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName); + } + } + + private void associateArtifactsToInstances(String yamlName, Resource resource, Map> 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 ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName); + } + } + + private void associateDeploymentArtifactsToInstances(User user, String yamlName, Resource resource, + Map> 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 ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName); + } + } + + private void associateComponentInstanceInputsToComponent(String yamlName, Resource resource, + Map> instInputs) { + if (MapUtils.isNotEmpty(instInputs)) { + Either>, StorageOperationStatus> addInputToInst = toscaOperationFacade + .associateComponentInstanceInputsToComponent(instInputs, resource.getUniqueId()); + if (addInputToInst.isRight()) { + StorageOperationStatus addInputToInstError = addInputToInst.right().value(); + log.debug("failed to associate inputs value of resource {} status is {}", resource.getUniqueId(), addInputToInstError); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addInputToInstError), yamlName); + } + setComponentInstanceInputsOnComponent(resource, instInputs); + } + } + + private void setComponentInstanceInputsOnComponent(Resource resource, Map> instInputs) { + Map> componentInstancesInputs = resource.getComponentInstancesInputs(); + if (componentInstancesInputs == null) { + componentInstancesInputs = new HashMap<>(); + } + componentInstancesInputs.putAll(instInputs); + resource.setComponentInstancesInputs(componentInstancesInputs); + } + + private void associateComponentInstancePropertiesToComponent(String yamlName, Resource resource, + Map> instProperties) { + Either>, StorageOperationStatus> addPropToInst = toscaOperationFacade + .associateComponentInstancePropertiesToComponent(instProperties, resource.getUniqueId()); + if (addPropToInst.isRight()) { + loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR while associate compnent insatnce properties of resource: {} status is: {}", resource.getName(), + addPropToInst.right().value()); + StorageOperationStatus storageOperationStatus = addPropToInst.right().value(); + log.debug("failed to associate properties of resource {} status is {}", resource.getUniqueId(), storageOperationStatus); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(storageOperationStatus), yamlName); + } + setComponentInstancePropertiesOnComponent(resource, instProperties); + } + + private void setComponentInstancePropertiesOnComponent(Resource resource, Map> instProperties) { + Map> componentInstanceProps = resource.getComponentInstancesProperties(); + if (componentInstanceProps == null) { + componentInstanceProps = new HashMap<>(); + } + componentInstanceProps.putAll(instProperties); + resource.setComponentInstancesProperties(componentInstanceProps); + } + + private void handleSubstitutionMappings(Resource resource, Map uploadResInstancesMap) { + Either getResourceRes = null; + if (resource.getResourceType() == ResourceTypeEnum.CVFC) { + getResourceRes = updateCalculatedCapReqWithSubstitutionMappings(resource, uploadResInstancesMap); + } else if (StringUtils.isNotEmpty(resource.getModel()) && resource.getResourceType() == ResourceTypeEnum.VF) { + getResourceRes = updateCalculatedCapReqWithSubstitutionMappingsForVf(resource, uploadResInstancesMap); + } + if (getResourceRes != null && getResourceRes.isRight()) { + ResponseFormat responseFormat = componentsUtils + .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource); + throw new ByResponseFormatComponentException(responseFormat); + } + + } + + private void addRelationsToRI(String yamlName, Resource resource, Map uploadResInstancesMap, + List componentInstancesList, List relations) { + for (Entry 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(), + ErrorSeverity.ERROR); + throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, resource, entry.getValue(), relations); + if (addRelationToRiRes.getStatus() != 200) { + throw new ByResponseFormatComponentException(addRelationToRiRes); + } + } + } + + private void setResourceInstanceRelationsOnComponent(Resource resource, List relations) { + if (resource.getComponentInstancesRelations() != null) { + resource.getComponentInstancesRelations().addAll(relations); + } else { + resource.setComponentInstancesRelations(relations); + } + } + + private void processComponentInstance(String yamlName, Resource resource, List componentInstancesList, + Map allDataTypes, + Map> instProperties, + Map>> instCapabilties, + Map>> instRequirements, + Map> instDeploymentArtifacts, + Map> instArtifacts, + Map> instAttributes, Map originCompMap, + Map> instInputs, + UploadComponentInstanceInfo uploadComponentInstanceInfo) { + Optional currentCompInstanceOpt = componentInstancesList.stream() + .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName())).findFirst(); + if (currentCompInstanceOpt.isEmpty()) { + 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(), + ErrorSeverity.ERROR); + throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + ComponentInstance currentCompInstance = currentCompInstanceOpt.get(); + String resourceInstanceId = currentCompInstance.getUniqueId(); + Resource originResource = getOriginResource(originCompMap, currentCompInstance); + if (isNotEmpty(originResource.getRequirements())) { + instRequirements.put(currentCompInstance, originResource.getRequirements()); + } + if (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.CVFC) { + ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, resource, originResource, + currentCompInstance, instProperties, allDataTypes); + if (addPropertiesValueToRiRes.getStatus() != 200) { + throw new ByResponseFormatComponentException(addPropertiesValueToRiRes); + } + } else { + addInputsValuesToRi(uploadComponentInstanceInfo, resource, originResource, currentCompInstance, instInputs, allDataTypes); + } + } + + private Resource getOriginResource(Map originCompMap, ComponentInstance currentCompInstance) { + Resource originResource; + if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) { + Either getOriginResourceRes = toscaOperationFacade + .getToscaFullElement(currentCompInstance.getComponentUid()); + if (getOriginResourceRes.isRight()) { + log.debug("failed to fetch resource with uniqueId {} and tosca component name {} status is {}", currentCompInstance.getComponentUid(), + currentCompInstance.getToscaComponentName(), getOriginResourceRes); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()), + currentCompInstance.getComponentUid()); + } + originResource = getOriginResourceRes.left().value(); + originCompMap.put(originResource.getUniqueId(), originResource); + } else { + originResource = originCompMap.get(currentCompInstance.getComponentUid()); + } + return originResource; + } + + private void processComponentInstanceCapabilities(Map allDataTypes, + Map>> instCapabilties, + UploadComponentInstanceInfo uploadComponentInstanceInfo, ComponentInstance currentCompInstance, + Resource originResource) { + Map> originCapabilities; + if (isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) { + originCapabilities = new HashMap<>(); + Map> newPropertiesMap = new HashMap<>(); + originResource.getCapabilities().forEach((k, v) -> addCapabilities(originCapabilities, k, v)); + uploadComponentInstanceInfo.getCapabilities().values().forEach(l -> addCapabilitiesProperties(newPropertiesMap, l)); + updateCapabilityPropertiesValues(originCapabilities, newPropertiesMap, allDataTypes); + } else { + originCapabilities = originResource.getCapabilities(); + } + instCapabilties.put(currentCompInstance, originCapabilities); + } + + private void updateCapabilityPropertiesValues(Map> originCapabilities, + Map> newPropertiesMap, + Map allDataTypes) { + originCapabilities.values().stream().flatMap(Collection::stream).filter(c -> newPropertiesMap.containsKey(c.getName())) + .forEach(c -> updatePropertyValues(c.getProperties(), newPropertiesMap.get(c.getName()), allDataTypes)); + } + + private void addCapabilitiesProperties(Map> newPropertiesMap, List capabilities) { + for (UploadCapInfo capability : capabilities) { + if (isNotEmpty(capability.getProperties())) { + newPropertiesMap.put(capability.getName(), capability.getProperties().stream().collect(toMap(UploadInfo::getName, p -> p))); + } + } + } + + private void addCapabilities(Map> originCapabilities, String type, List capabilities) { + List list = capabilities.stream().map(CapabilityDefinition::new).collect(toList()); + originCapabilities.put(type, list); + } + + private void updatePropertyValues(List properties, Map newProperties, + Map allDataTypes) { + properties.forEach(p -> updatePropertyValue(p, newProperties.get(p.getName()), allDataTypes)); + } + + private String updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo, + Map allDataTypes) { + String value = null; + List 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 validatePropValueBeforeCreate(property, value, isValidate, allDataTypes); + } + + private Either updateCalculatedCapReqWithSubstitutionMappings(Resource resource, + Map uploadResInstancesMap) { + Either updateRes = null; + Map>> updatedInstCapabilities = new HashMap<>(); + Map>> 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) { + log.debug( + "Failed to associate capabilities and requirementss of resource {}, updated according to a substitution mapping. Status is {}", + resource.getUniqueId(), status); + updateRes = Either.right(status); + } + } + if (updateRes == null) { + updateRes = Either.left(resource); + } + return updateRes; + } + + private Either updateCalculatedCapReqWithSubstitutionMappingsForVf(final Resource resource, + final Map uploadResInstancesMap) { + Either updateRes = null; + final Map>> updatedInstCapabilities = new HashMap<>(); + final Map>> updatedInstRequirements = new HashMap<>(); + + resource.getComponentInstances().forEach(i -> { + setExternalCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate()); + setExternalRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate()); + }); + + final StorageOperationStatus status = toscaOperationFacade.updateCalculatedCapabilitiesRequirements(updatedInstCapabilities, + updatedInstRequirements, resource); + if (status != StorageOperationStatus.OK) { + log.debug( + "Failed to update capabilities and requirements of resource {}. Status is {}", + resource.getUniqueId(), status); + updateRes = Either.right(status); + } + + if (updateRes == null) { + updateRes = Either.left(resource); + } + return updateRes; + } + + private void fillUpdatedInstCapabilitiesRequirements(List componentInstances, + Map uploadResInstancesMap, + Map>> updatedInstCapabilities, + Map>> updatedInstRequirements) { + componentInstances.forEach(i -> { + fillUpdatedInstCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate()); + fillUpdatedInstRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate()); + }); + } + + private void fillUpdatedInstRequirements(Map>> updatedInstRequirements, + ComponentInstance instance, Map requirementsNamesToUpdate) { + Map> updatedRequirements = new HashMap<>(); + Set updatedReqNames = new HashSet<>(); + if (isNotEmpty(requirementsNamesToUpdate)) { + for (Map.Entry> 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 (isNotEmpty(updatedRequirements)) { + updatedInstRequirements.put(instance, updatedRequirements); + } + } + + private void setExternalRequirements( + final Map>> updatedInstRequirements, + final ComponentInstance instance, final Map requirementsNamesToUpdate) { + final Map> updatedRequirements = new HashMap<>(); + final Set updatedReqNames = new HashSet<>(); + if (isNotEmpty(requirementsNamesToUpdate)) { + for (Map.Entry> 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.setExternal(true); + r.setExternalName(requirementsNamesToUpdate.get(r.getName())); + updatedReqNames.add(r.getName()); + return r; + }).collect(toList())); + } + } + if (isNotEmpty(updatedRequirements)) { + updatedInstRequirements.put(instance, updatedRequirements); + } + } + + private void setExternalCapabilities( + final Map>> updatedInstCapabilties, + final ComponentInstance instance, Map capabilitiesNamesToUpdate) { + final Map> updatedCapabilities = new HashMap<>(); + final Set updatedCapNames = new HashSet<>(); + if (isNotEmpty(capabilitiesNamesToUpdate)) { + for (Map.Entry> 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.setExternal(true); + c.setExternalName(capabilitiesNamesToUpdate.get(c.getName())); + updatedCapNames.add(c.getName()); + return c; + }).collect(toList())); + } + } + if (isNotEmpty(updatedCapabilities)) { + updatedInstCapabilties.put(instance, updatedCapabilities); + } + } + + private void fillUpdatedInstCapabilities(Map>> updatedInstCapabilties, + ComponentInstance instance, Map capabilitiesNamesToUpdate) { + Map> updatedCapabilities = new HashMap<>(); + Set updatedCapNames = new HashSet<>(); + if (isNotEmpty(capabilitiesNamesToUpdate)) { + for (Map.Entry> 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 (isNotEmpty(updatedCapabilities)) { + updatedInstCapabilties.put(instance, updatedCapabilities); + } + } + + private ResponseFormat addRelationToRI(String yamlName, Resource resource, UploadComponentInstanceInfo nodesInfoValue, + List relations) { + List 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(), + ErrorSeverity.ERROR); + return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + String resourceInstanceId = currentCompInstance.getUniqueId(); + Map> regMap = nodesInfoValue.getRequirements(); + if (regMap != null) { + for (Entry> nodesRegInfoEntry : regMap.entrySet()) { + List uploadRegInfoList = nodesRegInfoEntry.getValue(); + for (UploadReqInfo uploadRegInfo : uploadRegInfoList) { + log.debug("Going to create relation {}", uploadRegInfo.getName()); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, + "Started to create relations on instance: {}", uploadRegInfo.getName()); + String regName = uploadRegInfo.getName(); + RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef(); + regCapRelDef.setFromNode(resourceInstanceId); + log.debug("try to find available requirement {} ", regName); + Either eitherReqStatus = findAviableRequiremen(regName, yamlName, nodesInfoValue, + currentCompInstance, uploadRegInfo.getCapabilityName()); + if (eitherReqStatus.isRight()) { + log.debug("failed to find available requirement {} status is {}", regName, eitherReqStatus.right().value()); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR while search available requirement {} status is: {}", regName, eitherReqStatus.right().value()); + return eitherReqStatus.right().value(); + } + RequirementDefinition validReq = eitherReqStatus.left().value(); + List 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()); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR 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(), + 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); + if (aviableCapForRel == null) { + log.debug("aviable capability was not found. req name is {} component instance is {}", validReq.getName(), + currentCapCompInstance.getUniqueId()); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR available capability was not found. req name is: {} component instance is: {}", validReq.getName(), + currentCapCompInstance.getUniqueId()); + BeEcompErrorManager.getInstance().logInternalDataError( + "aviable capability was not found. req name is " + validReq.getName() + " component instance is " + currentCapCompInstance + .getUniqueId(), resource.getUniqueId(), ErrorSeverity.ERROR); + return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName); + } + reqAndRelationshipPair.setCapability(aviableCapForRel.getName()); + reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId()); + reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId()); + CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship(); + capReqRel.setRelation(reqAndRelationshipPair); + reqAndRelationshipPairList.add(capReqRel); + regCapRelDef.setRelationships(reqAndRelationshipPairList); + relations.add(regCapRelDef); + } + } + } else if (resource.getResourceType() != ResourceTypeEnum.CVFC) { + return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName); + } + return componentsUtils.getResponseFormat(ActionStatus.OK); + } + + private void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource, + ComponentInstance currentCompInstance, Map> instInputs, + Map allDataTypes) { + Map> propMap = uploadComponentInstanceInfo.getProperties(); + if (MapUtils.isNotEmpty(propMap)) { + Map currPropertiesMap = new HashMap<>(); + List instPropList = new ArrayList<>(); + if (CollectionUtils.isEmpty(originResource.getInputs())) { + log.debug("failed to find properties "); + loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR while try to find properties"); + throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND); + } + originResource.getInputs().forEach(p -> addInput(currPropertiesMap, p)); + for (List propertyList : propMap.values()) { + processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList); + } + currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p))); + instInputs.put(currentCompInstance.getUniqueId(), instPropList); + } + } + + private void processProperty(Resource resource, ComponentInstance currentCompInstance, Map allDataTypes, + Map currPropertiesMap, List instPropList, + List propertyList) { + UploadPropInfo propertyInfo = propertyList.get(0); + String propName = propertyInfo.getName(); + if (!currPropertiesMap.containsKey(propName)) { + loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR failed to find property: {}", propName); + log.debug("failed to find property {} ", propName); + throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, propName); + } + InputDefinition curPropertyDef = currPropertiesMap.get(propName); + ComponentInstanceInput property = null; + String value = null; + List getInputs = null; + boolean isValidate = true; + if (propertyInfo.getValue() != null) { + getInputs = propertyInfo.getGet_input(); + isValidate = getInputs == null || getInputs.isEmpty(); + if (isValidate) { + value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType()); + } else { + value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()); + } + } + property = new ComponentInstanceInput(curPropertyDef, value, null); + String validPropertyVAlue = validatePropValueBeforeCreate(property, value, isValidate, allDataTypes); + property.setValue(validPropertyVAlue); + if (isNotEmpty(getInputs)) { + List getInputValues = new ArrayList<>(); + for (GetInputValueDataDefinition getInput : getInputs) { + List inputs = resource.getInputs(); + if (CollectionUtils.isEmpty(inputs)) { + loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR Failed to add property: " + propName + " to resource instance: {}. Inputs list is empty ", + currentCompInstance.getUniqueId()); + log.debug("Failed to add property {} to resource instance {}. Inputs list is empty ", property, + currentCompInstance.getUniqueId()); + throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT); + } + Optional optional = inputs.stream().filter(p -> p.getName().equals(getInput.getInputName())).findAny(); + if (optional.isEmpty()) { + loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR Failed to find input: " + getInput.getInputName()); + log.debug("Failed to find input {} ", getInput.getInputName()); + // @@TODO error message + throw new ByActionStatusComponentException(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); + // delete overriden property + currPropertiesMap.remove(property.getName()); + } + + private void processGetInput(List getInputValues, List inputs, + GetInputValueDataDefinition getInputIndex) { + Optional optional; + if (getInputIndex != null) { + optional = inputs.stream().filter(p -> p.getName().equals(getInputIndex.getInputName())).findAny(); + if (optional.isEmpty()) { + log.debug("Failed to find input {} ", getInputIndex.getInputName()); + // @@TODO error message + throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT); + } + InputDefinition inputIndex = optional.get(); + getInputIndex.setInputId(inputIndex.getUniqueId()); + getInputValues.add(getInputIndex); + } + } + + private void addInput(Map currPropertiesMap, InputDefinition prop) { + String propName = prop.getName(); + if (!currPropertiesMap.containsKey(propName)) { + currPropertiesMap.put(propName, prop); + } + } + + private ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource, + ComponentInstance currentCompInstance, Map> instProperties, + Map allDataTypes) { + Map> propMap = uploadComponentInstanceInfo.getProperties(); + Map currPropertiesMap = new HashMap<>(); + List listFromMap = originResource.getProperties(); + if ((propMap != null && !propMap.isEmpty()) && (listFromMap == null || listFromMap.isEmpty())) { + loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR Failed to find properties"); + 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 instPropList = new ArrayList<>(); + if (propMap != null && propMap.size() > 0) { + for (List propertyList : propMap.values()) { + UploadPropInfo propertyInfo = propertyList.get(0); + String propName = propertyInfo.getName(); + if (!currPropertiesMap.containsKey(propName)) { + log.debug("failed to find property {} ", propName); + loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR 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 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()); + } + } + property = new ComponentInstanceProperty(curPropertyDef, value, null); + String validatePropValue = validatePropValueBeforeCreate(property, value, isValidate, allDataTypes); + property.setValue(validatePropValue); + if (getInputs != null && !getInputs.isEmpty()) { + List getInputValues = new ArrayList<>(); + for (GetInputValueDataDefinition getInput : getInputs) { + List inputs = resource.getInputs(); + if (inputs == null || inputs.isEmpty()) { + log.debug("Failed to add property {} to instance. Inputs list is empty ", property); + loggerSupportability + .log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "Failed to add property: {} to instance. Inputs list is empty", propName); + rollbackWithException(ActionStatus.INPUTS_NOT_FOUND, + property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString()); + } + Either inputEither = findInputByName(inputs, getInput); + if (inputEither.isRight()) { + throw inputEither.right().value(); + } else { + InputDefinition input = inputEither.left().value(); + getInput.setInputId(input.getUniqueId()); + getInputValues.add(getInput); + GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex(); + if (getInputIndex != null) { + Either newInputEither = findInputByName(inputs, getInputIndex); + if (inputEither.isRight()) { + throw newInputEither.right().value(); + } else { + InputDefinition newInput = newInputEither.left().value(); + getInputIndex.setInputId(newInput.getUniqueId()); + } + getInputValues.add(getInputIndex); + } + } + } + property.setGetInputValues(getInputValues); + } + instPropList.add(property); + // delete overriden property + currPropertiesMap.remove(property.getName()); + } + } + // add rest of properties + if (!currPropertiesMap.isEmpty()) { + for (PropertyDefinition value : currPropertiesMap.values()) { + instPropList.add(new ComponentInstanceProperty(value)); + } + } + instProperties.put(currentCompInstance.getUniqueId(), instPropList); + return componentsUtils.getResponseFormat(ActionStatus.OK); + } + + // US740820 Relate RIs according to capability name + private CapabilityDefinition findAvailableCapabilityByTypeOrName(RequirementDefinition validReq, ComponentInstance currentCapCompInstance, + UploadReqInfo uploadReqInfo) { + if (null == uploadReqInfo.getCapabilityName() || validReq.getCapability() + .equals(uploadReqInfo.getCapabilityName())) {// get + + // by + + // capability + + // type + return findAvailableCapability(validReq, currentCapCompInstance); + } + return findAvailableCapability(validReq, currentCapCompInstance, uploadReqInfo); + } + + private CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance currentCapCompInstance, + UploadReqInfo uploadReqInfo) { + CapabilityDefinition cap = null; + Map> capMap = currentCapCompInstance.getCapabilities(); + if (!capMap.containsKey(validReq.getCapability())) { + return null; + } + Optional capByName = capMap.get(validReq.getCapability()).stream() + .filter(p -> p.getName().equals(uploadReqInfo.getCapabilityName())).findAny(); + if (capByName.isEmpty()) { + 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 CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance instance) { + Map> capMap = instance.getCapabilities(); + if (capMap.containsKey(validReq.getCapability())) { + List 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; + } + + private boolean isBoundedByOccurrences(CapabilityDefinition cap) { + return cap.getMaxOccurrences() != null && !cap.getMaxOccurrences().equals(CapabilityDataDefinition.MAX_OCCURRENCES); + } + + private Either findAviableRequiremen(String regName, String yamlName, + UploadComponentInstanceInfo uploadComponentInstanceInfo, + ComponentInstance currentCompInstance, String capName) { + Map> comInstRegDefMap = currentCompInstance.getRequirements(); + List list = comInstRegDefMap.get(capName); + RequirementDefinition validRegDef = null; + if (list == null) { + for (Entry> 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); + } + + private Resource createResourceInstances(String yamlName, Resource resource, Resource oldResource, + Map uploadResInstancesMap, Map nodeNamespaceMap, + Map existingNodeTypesByResourceNames) { + Either eitherResource; + log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName); + if (isEmpty(uploadResInstancesMap) && resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE); + throw new ByResponseFormatComponentException(responseFormat); + } + if (MapUtils.isNotEmpty(nodeNamespaceMap)) { + nodeNamespaceMap.forEach((k, v) -> existingNodeTypesByResourceNames.put(v.getToscaResourceName(), v)); + } + Map resourcesInstancesMap = new HashMap<>(); + uploadResInstancesMap.values().forEach( + i -> createAndAddResourceInstance(i, yamlName, resource, nodeNamespaceMap, existingNodeTypesByResourceNames, resourcesInstancesMap)); + if (oldResource != null && oldResource.getResourceType() != ResourceTypeEnum.CVFC && oldResource.getComponentInstances() != null) { + Map existingNodeTypesByUids = existingNodeTypesByResourceNames.values().stream() + .collect(toMap(Resource::getUniqueId, r -> r)); + oldResource.getComponentInstances().stream().filter(i -> !i.isCreatedFromCsar()) + .forEach(uiInst -> resourcesInstancesMap.put(uiInst, getOriginResource(existingNodeTypesByUids, uiInst))); + } + if (isNotEmpty(resourcesInstancesMap)) { + try { + toscaOperationFacade.associateComponentInstancesToComponent(resource, resourcesInstancesMap, false, oldResource != null); + } 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()); + } + } + } + if (CollectionUtils.isEmpty(resource.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 ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE)); + } + return resource; + } + + private void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName, Resource resource, + Map nodeNamespaceMap, Map existingnodeTypeMap, + Map resourcesInstancesMap) { + Either eitherResource; + log.debug("*************Going to create resource instances {}", yamlName); + // updating type if the type is node type name - we need to take the + + // updated name + log.debug("*************Going to create resource instances {}", uploadComponentInstanceInfo.getName()); + if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) { + uploadComponentInstanceInfo.setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName()); + } + Resource refResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo, existingnodeTypeMap, resource); + ComponentInstance componentInstance = new ComponentInstance(); + componentInstance.setComponentUid(refResource.getUniqueId()); + Collection 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 == NodeTypeEnum.Resource && isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) && isNotEmpty( + refResource.getCapabilities())) { + setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities()); + Map> validComponentInstanceCapabilities = getValidComponentInstanceCapabilities( + refResource.getUniqueId(), refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities()); + componentInstance.setCapabilities(validComponentInstanceCapabilities); + } + if (isNotEmpty(uploadComponentInstanceInfo.getArtifacts())) { + Map> artifacts = uploadComponentInstanceInfo.getArtifacts(); + Map toscaArtifacts = new HashMap<>(); + Map> arts = artifacts.entrySet().stream() + .filter(e -> e.getKey().contains(TypeUtils.ToscaTagNamesEnum.ARTIFACTS.getElementName())) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + Map artifact = arts.get(TypeUtils.ToscaTagNamesEnum.ARTIFACTS.getElementName()); + for (Map.Entry entry : artifact.entrySet()) { + ToscaArtifactDataDefinition to = new ToscaArtifactDataDefinition(); + to.setFile(entry.getValue().getFile()); + to.setType(entry.getValue().getType()); + if (isNotEmpty(entry.getValue().getProperties())) { + Map newPropertiesMap = new HashMap<>(); + List artifactPropsInfo = entry.getValue().getProperties(); + for (UploadPropInfo propInfo : artifactPropsInfo) { + newPropertiesMap.put(propInfo.getName(), propInfo.getValue()); + } + to.setProperties(newPropertiesMap); + } + toscaArtifacts.put(entry.getKey(), to); + } + componentInstance.setToscaArtifacts(toscaArtifacts); + } + if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) { + log.debug("createResourceInstances - not found lates version for resource instance with name {} and type {}", + uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); + throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(), + uploadComponentInstanceInfo.getType()); + } + Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType()); + componentInstance.setName(uploadComponentInstanceInfo.getName()); + componentInstance.setIcon(origResource.getIcon()); + componentInstance.setCreatedFrom(CreatedFrom.CSAR); + resourcesInstancesMap.put(componentInstance, origResource); + } + + private void setCapabilityNamesTypes(Map> originCapabilities, + Map> uploadedCapabilities) { + for (Entry> currEntry : uploadedCapabilities.entrySet()) { + if (originCapabilities.containsKey(currEntry.getKey())) { + currEntry.getValue().forEach(cap -> cap.setType(currEntry.getKey())); + } + } + for (Map.Entry> capabilities : originCapabilities.entrySet()) { + capabilities.getValue().forEach(cap -> { + if (uploadedCapabilities.containsKey(cap.getName())) { + uploadedCapabilities.get(cap.getName()).forEach(c -> { + c.setName(cap.getName()); + c.setType(cap.getType()); + }); + } + }); + } + } + + private Resource validateResourceInstanceBeforeCreate(String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo, + Map nodeNamespaceMap, Resource resource) { + log.debug("validateResourceInstanceBeforeCreate - going to validate resource instance with name {} and type {} before create", + uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); + Resource refResource; + if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) { + refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()); + } else { + Either findResourceEither = StringUtils.isEmpty(resource.getModel()) ? + toscaOperationFacade.getByToscaResourceNameMatchingVendorRelease(uploadComponentInstanceInfo.getType(), + ((ResourceMetadataDataDefinition) resource.getComponentMetadataDefinition().getMetadataDataDefinition()).getVendorRelease()) : + toscaOperationFacade.getLatestByToscaResourceNameAndModel(uploadComponentInstanceInfo.getType(), resource.getModel()); + if (findResourceEither.isRight()) { + log.debug("validateResourceInstanceBeforeCreate - not found latest version for resource instance with name {} and type {}", + uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType()); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(findResourceEither.right().value())); + } + refResource = findResourceEither.left().value(); + nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource); + } + String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState(); + if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) { + log.debug( + "validateResourceInstanceBeforeCreate - component instance of component {} can not be created because the component is in an illegal state {}.", + refResource.getName(), componentState); + throw new ByActionStatusComponentException(ActionStatus.ILLEGAL_COMPONENT_STATE, refResource.getComponentType().getValue(), + refResource.getName(), componentState); + } + if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.CVFC) { + log.debug("validateResourceInstanceBeforeCreate - ref resource type is {} ", refResource.getResourceType()); + throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(), + uploadComponentInstanceInfo.getType()); + } + return refResource; + } + + public Resource propagateStateToCertified(User user, Resource resource, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, + boolean needLock, boolean forceCertificationAllowed) { + boolean failed = false; + try { + if (resource.getLifecycleState() != LifecycleStateEnum.CERTIFIED && forceCertificationAllowed && lifecycleBusinessLogic + .isFirstCertification(resource.getVersion())) { + nodeForceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock); + } + if (resource.getLifecycleState() == LifecycleStateEnum.CERTIFIED) { + populateToscaArtifacts(resource, user, false, inTransaction, needLock, false); + return resource; + } + return nodeFullCertification(resource.getUniqueId(), user, lifecycleChangeInfo, inTransaction, needLock); + } catch (ComponentException e) { + failed = true; + log.debug("The exception has occurred upon certification of resource {}. ", resource.getName(), e); + throw e; + } finally { + if (failed) { + BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify"); + if (!inTransaction) { + janusGraphDao.rollback(); + } + } else if (!inTransaction) { + janusGraphDao.commit(); + } + } + } + + private Resource nodeFullCertification(String uniqueId, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, + boolean needLock) { + Either resourceResponse = lifecycleBusinessLogic + .changeState(uniqueId, user, LifeCycleTransitionEnum.CERTIFY, lifecycleChangeInfo, inTransaction, needLock); + if (resourceResponse.isRight()) { + throw new ByResponseFormatComponentException(resourceResponse.right().value()); + } + return resourceResponse.left().value(); + } + + private Resource nodeForceCertification(Resource resource, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, + boolean needLock) { + return lifecycleBusinessLogic.forceResourceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock); + } + + public ImmutablePair createOrUpdateResourceByImport(final Resource resource, final User user, final boolean isNormative, + final boolean isInTransaction, final boolean needLock, + final CsarInfo csarInfo, final String nodeName, + final boolean isNested) { + ImmutablePair result = null; + // check if resource already exists (search by tosca name = type) + final boolean isNestedResource = isNestedResourceUpdate(csarInfo, nodeName); + final String resourceName = resource.getToscaResourceName(); + final Either latestByToscaName = toscaOperationFacade + .getLatestByToscaResourceNameAndModel(resourceName, resource.getModel()); + if (latestByToscaName.isLeft() && Objects.nonNull(latestByToscaName.left().value())) { + final Resource foundResource = latestByToscaName.left().value(); + // we don't allow updating names of top level types + if (!isNestedResource && !StringUtils.equals(resource.getName(), foundResource.getName())) { + BeEcompErrorManager.getInstance() + .logBeComponentMissingError("Create / Update resource by import", ComponentTypeEnum.RESOURCE.getValue(), resource.getName()); + log.debug("resource already exist new name={} old name={} same type={}", resource.getName(), foundResource.getName(), + resource.getToscaResourceName()); + final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_ALREADY_EXISTS); + componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE); + throwComponentException(responseFormat); + } + result = updateExistingResourceByImport(resource, foundResource, user, isNormative, needLock, isNested); + } else if (isNotFound(latestByToscaName)) { + if (isNestedResource) { + result = createOrUpdateNestedResource(resource, user, isNormative, isInTransaction, needLock, csarInfo, isNested, nodeName); + } else { + result = createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo); + } + } else { + StorageOperationStatus status = latestByToscaName.right().value(); + log.debug("failed to get latest version of resource {}. status={}", resource.getName(), status); + ResponseFormat responseFormat = componentsUtils + .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(latestByToscaName.right().value()), resource); + componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE); + throwComponentException(responseFormat); + } + return result; + } + + private boolean isNestedResourceUpdate(CsarInfo csarInfo, String nodeName) { + return csarInfo != null && csarInfo.isUpdate() && nodeName != null; + } + + private ImmutablePair createOrUpdateNestedResource(final Resource resource, final User user, final boolean isNormative, + final boolean isInTransaction, final boolean needLock, + final CsarInfo csarInfo, final boolean isNested, + final String nodeName) { + final Either latestByToscaName = toscaOperationFacade.getLatestByToscaResourceName( + buildNestedToscaResourceName(resource.getResourceType().name(), csarInfo.getVfResourceName(), nodeName).getRight(), resource.getModel()); + if (latestByToscaName.isLeft()) { + final Resource nestedResource = (Resource) latestByToscaName.left().value(); + log.debug(VALIDATE_DERIVED_BEFORE_UPDATE); + final Either eitherValidation = validateNestedDerivedFromDuringUpdate(nestedResource, resource, + ValidationUtils.hasBeenCertified(nestedResource.getVersion())); + if (eitherValidation.isRight()) { + return createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo); + } + return updateExistingResourceByImport(resource, nestedResource, user, isNormative, needLock, isNested); + } else { + return createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo); + } + } + + private boolean isNotFound(Either getResourceEither) { + return getResourceEither.isRight() && getResourceEither.right().value() == StorageOperationStatus.NOT_FOUND; + } + + private ImmutablePair createResourceByImport(Resource resource, User user, boolean isNormative, boolean isInTransaction, + CsarInfo csarInfo) { + log.debug("resource with name {} does not exist. create new resource", resource.getName()); + validateResourceBeforeCreate(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isInTransaction, csarInfo); + final Resource createResourceByDao = createResourceByDao(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isNormative, isInTransaction); + Resource createdResource = updateCatalog(createResourceByDao, ChangeTypeEnum.LIFECYCLE).left().map(r -> (Resource) r).left().value(); + ImmutablePair resourcePair = new ImmutablePair<>(createdResource, ActionStatus.CREATED); + ASDCKpiApi.countImportResourcesKPI(); + return resourcePair; + } + + public boolean isResourceExist(String resourceName) { + Either latestByName = toscaOperationFacade.getLatestByName(resourceName, null); + return latestByName.isLeft(); + } + + private ImmutablePair 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 resourcePair = null; + try { + lockComponent(lockedResourceId, oldResource, needLock, "Update Resource by Import"); + oldResource = prepareResourceForUpdate(oldResource, newResource, user, inTransaction, false); + mergeOldResourceMetadataWithNew(oldResource, newResource); + validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction, isNested); + validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), newResource, AuditingActionEnum.IMPORT_RESOURCE, inTransaction); + // contact info normalization + newResource.setContactId(newResource.getContactId().toLowerCase()); + PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource); + // non-updatable fields + 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()); + newResource.setModel(oldResource.getModel()); + if (oldResource.getCsarUUID() != null) { + newResource.setCsarUUID(oldResource.getCsarUUID()); + } + if (oldResource.getImportedToscaChecksum() != null) { + newResource.setImportedToscaChecksum(oldResource.getImportedToscaChecksum()); + } + newResource.setAbstract(oldResource.isAbstract()); + if (CollectionUtils.isEmpty(newResource.getDerivedFrom())) { + newResource.setDerivedFrom(oldResource.getDerivedFrom()); + } + if (CollectionUtils.isEmpty(newResource.getDataTypes())) { + newResource.setDataTypes(oldResource.getDataTypes()); + } + if (StringUtils.isEmpty(newResource.getDerivedFromGenericType())) { + newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType()); + } + if (StringUtils.isEmpty(newResource.getDerivedFromGenericVersion())) { + newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion()); + } + // add for new) + + // created without tosca artifacts - add the placeholders + if (MapUtils.isEmpty(newResource.getToscaArtifacts())) { + setToscaArtifactsPlaceHolders(newResource, user); + } + + if (CollectionUtils.isEmpty(newResource.getAttributes())) { + newResource.setAttributes(oldResource.getAttributes()); + } + if (CollectionUtils.isEmpty(newResource.getProperties())) { + newResource.setProperties(oldResource.getProperties()); + } + Either overrideResource = toscaOperationFacade.overrideComponent(newResource, oldResource); + if (overrideResource.isRight()) { + ResponseFormat responseFormat = componentsUtils + .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(overrideResource.right().value()), newResource); + componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.IMPORT_RESOURCE); + throwComponentException(responseFormat); + } + updateCatalog(overrideResource.left().value(), ChangeTypeEnum.LIFECYCLE); + log.debug("Resource updated successfully!!!"); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK); + componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.IMPORT_RESOURCE, + ResourceVersionInfo.newBuilder().state(oldResource.getLifecycleState().name()).version(oldResource.getVersion()).build()); + resourcePair = new ImmutablePair<>(overrideResource.left().value(), ActionStatus.OK); + return resourcePair; + } finally { + if (resourcePair == null) { + BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify"); + janusGraphDao.rollback(); + } else if (!inTransaction) { + janusGraphDao.commit(); + } + if (needLock) { + log.debug("unlock resource {}", lockedResourceId); + graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource); + } + } + } + + /** + * Merge old resource with new. Keep old category and vendor name without change + * + * @param oldResource + * @param newResource + */ + private void mergeOldResourceMetadataWithNew(Resource oldResource, Resource newResource) { + // keep old category and vendor name without change + + // merge the rest of the resource metadata + if (newResource.getTags() == null || newResource.getTags().isEmpty()) { + newResource.setTags(oldResource.getTags()); + } + if (newResource.getDescription() == null) { + newResource.setDescription(oldResource.getDescription()); + } + if (newResource.getVendorRelease() == null) { + newResource.setVendorRelease(oldResource.getVendorRelease()); + } + if (newResource.getResourceVendorModelNumber() == null) { + newResource.setResourceVendorModelNumber(oldResource.getResourceVendorModelNumber()); + } + if (newResource.getModel() == null) { + newResource.setModel(oldResource.getModel()); + } + if (newResource.getContactId() == null) { + newResource.setContactId(oldResource.getContactId()); + } + newResource.setIcon(oldResource.getIcon()); + newResource.setCategories(oldResource.getCategories()); + if (newResource.getVendorName() == null) { + newResource.setVendorName(oldResource.getVendorName()); + } + List oldForUpdate = oldResource.getGroups(); + if (CollectionUtils.isNotEmpty(oldForUpdate)) { + List groupForUpdate = oldForUpdate.stream().map(GroupDefinition::new).collect(Collectors.toList()); + groupForUpdate.stream().filter(GroupDataDefinition::isVspOriginated).forEach(group -> group.setName(group.getInvariantName())); + newResource.setGroups(groupForUpdate); + } + if (newResource.getResourceType().isAtomicType() && !newResource.getName().equals("Root") + && newResource.getResourceType() != ResourceTypeEnum.CVFC) { + ResourceTypeEnum updatedResourceType = newResource.getResourceType(); + Optional derivedFromResourceOptional = getParentComponent(newResource); + if (derivedFromResourceOptional.isPresent() && derivedFromResourceOptional.get().getComponentType() == ComponentTypeEnum.RESOURCE) { + Resource parentResource = (Resource) derivedFromResourceOptional.get(); + if (!(parentResource.isAbstract() && (ResourceTypeEnum.VFC == parentResource.getResourceType() + || ResourceTypeEnum.ABSTRACT == parentResource.getResourceType())) && parentResource.getResourceType() != updatedResourceType + && oldResource.getResourceType() != updatedResourceType) { + BeEcompErrorManager.getInstance().logInternalDataError("mergeOldResourceMetadataWithNew", + "resource type of the resource does not match to derived from resource type", ErrorSeverity.ERROR); + log.debug( + "#mergeOldResourceMetadataWithNew - resource type {} of the resource {} does not match to derived from resource type {}", + newResource.getResourceType(), newResource.getToscaResourceName(), parentResource.getResourceType()); + throw new ByActionStatusComponentException(ActionStatus.INVALID_RESOURCE_TYPE); + } + } + } + } + + private Optional getParentComponent(Resource newResource) { + if (newResource.getDerivedFrom() == null) { + return Optional.empty(); + } + String toscaResourceNameDerivedFrom = newResource.getDerivedFrom().get(0); + Either latestByToscaResourceName = toscaOperationFacade + .getLatestByToscaResourceName(toscaResourceNameDerivedFrom, newResource.getModel()); + if (latestByToscaResourceName.isRight()) { + BeEcompErrorManager.getInstance() + .logInternalDataError("mergeOldResourceMetadataWithNew", "derived from resource not found", ErrorSeverity.ERROR); + log.debug("#mergeOldResourceMetadataWithNew - derived from resource {} not found", toscaResourceNameDerivedFrom); + throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, toscaResourceNameDerivedFrom); + } + return Optional.of(latestByToscaResourceName.left().value()); + } + + private 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; + } + + private 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 ByResponseFormatComponentException(response); + } + + 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 (StringUtils.isEmpty(resource.getToscaResourceName()) && !ModelConverter.isAtomicComponent(resource)) { + String resourceSystemName; + if (csarInfo != null && 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; + } + + private Either validateResourceType(User user, Resource resource, AuditingActionEnum actionEnum) { + Either 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; + } + + private Either validateLifecycleTypesCreate(User user, Resource resource, AuditingActionEnum actionEnum) { + Either eitherResult = Either.left(true); + if (resource.getInterfaces() != null && resource.getInterfaces().size() > 0) { + log.debug("validate interface lifecycle Types Exist"); + Iterator intItr = resource.getInterfaces().values().iterator(); + while (intItr.hasNext() && eitherResult.isLeft()) { + InterfaceDefinition interfaceDefinition = intItr.next(); + String intType = interfaceDefinition.getUniqueId(); + Either eitherCapTypeFound = interfaceTypeOperation.getInterface(UniqueIdBuilder.buildInterfaceTypeUid(resource.getModel(), 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; + } + + private Either validateCapabilityTypesCreate(User user, ICapabilityTypeOperation capabilityTypeOperation, + Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) { + Either eitherResult = Either.left(true); + if (resource.getCapabilities() != null && resource.getCapabilities().size() > 0) { + log.debug("validate capability Types Exist - capabilities section"); + for (Entry> 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; + } + + // @param typeObject- the object to which the validation is done + private Either validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation, + Resource resource, List validationObjects, AuditingActionEnum actionEnum, + Either eitherResult, String type, + boolean inTransaction) { + Either eitherCapTypeFound = capabilityTypeOperation.getCapabilityType( + UniqueIdBuilder.buildCapabilityTypeUid(resource.getModel(), 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); + } + return eitherResult; + } + + private Either validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation, + Resource resource, AuditingActionEnum actionEnum, + Either eitherResult, + Entry> typeEntry, boolean inTransaction) { + Either eitherCapTypeFound = capabilityTypeOperation + .getCapabilityType(UniqueIdBuilder.buildCapabilityTypeUid(resource.getModel(), 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()); + componentsUtils.auditResource(errorResponse, user, resource, actionEnum); + return Either.right(errorResponse); + } + CapabilityTypeDefinition capabilityTypeDefinition = eitherCapTypeFound.left().value(); + if (capabilityTypeDefinition.getProperties() != null) { + for (CapabilityDefinition capDef : typeEntry.getValue()) { + List properties = capDef.getProperties(); + List changedProperties = new ArrayList<>(); + if (properties == null || properties.isEmpty()) { + for (Entry prop : capabilityTypeDefinition.getProperties().entrySet()) { + ComponentInstanceProperty newProp = new ComponentInstanceProperty(prop.getValue()); + changedProperties.add(newProp); + } + } else { + List propsToAdd = new ArrayList<>(); + for (Entry prop : capabilityTypeDefinition.getProperties().entrySet()) { + PropertyDefinition propFromDef = prop.getValue(); + boolean propFound = false; + for (ComponentInstanceProperty cip : properties) { + if (propFromDef.getName().equals(cip.getName())) { + //merge property value and property description only, ignore other fields + if (cip.getDescription() != null && !cip.getDescription().equals(propFromDef.getDescription())) { + propFromDef.setDescription(cip.getDescription()); + } + propertyDataValueMergeBusinessLogic.mergePropertyValue(propFromDef, cip, new ArrayList<>()); + if (cip.getValue() != null) { + propFromDef.setValue(cip.getValue()); + } + propsToAdd.add(new ComponentInstanceProperty(propFromDef)); + propFound = true; + properties.remove(cip); + break; + } + } + if (!propFound) { + propsToAdd.add(new ComponentInstanceProperty(propFromDef)); + } + } + if (!propsToAdd.isEmpty()) { + changedProperties.addAll(propsToAdd); + } + } + capDef.setProperties(changedProperties); + } + } + return eitherResult; + } + + public Resource createResourceByDao(Resource resource, User user, AuditingActionEnum actionEnum, boolean isNormative, boolean inTransaction) { + // create resource + + // lock new resource name in order to avoid creation resource with same + + // name + Resource createdResource = null; + if (!inTransaction) { + Either lockResult = lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE); + if (lockResult.isRight()) { + ResponseFormat responseFormat = lockResult.right().value(); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw new ByResponseFormatComponentException(responseFormat); + } + log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult); + } + try { + if (resource.deriveFromGeneric()) { + handleResourceGenericType(resource); + } + createdResource = createResourceTransaction(resource, user, isNormative); + componentsUtils.auditResource(componentsUtils.getResponseFormat(ActionStatus.CREATED), user, createdResource, actionEnum); + ASDCKpiApi.countCreatedResourcesKPI(); + } catch (ComponentException e) { + janusGraphDao.rollback(); + ResponseFormat responseFormat = + e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat(); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw e; + } catch (StorageException e) { + janusGraphDao.rollback(); + ResponseFormat responseFormat = componentsUtils + .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus())); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw e; + } finally { + if (!inTransaction) { + graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource); + } + } + return createdResource; + } + + private Resource createResourceTransaction(Resource resource, User user, boolean isNormative) { + final String resourceName = resource.getName(); + final String modelName = resource.getModel(); + final ResourceTypeEnum resourceType = resource.getResourceType(); + final ComponentTypeEnum componentType = resource.getComponentType(); + final Either eitherValidation = toscaOperationFacade + .validateComponentNameAndModelExists(resourceName, modelName, resourceType, componentType); + if (eitherValidation.isRight()) { + loggerSupportability.log(LoggerSupportabilityActions.VALIDATE_NAME, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "ERROR while validate component name {} Status is: {}", resource.getName(), eitherValidation.right().value()); + log.debug("Failed to validate component name {}. Status is {}. ", resource.getName(), eitherValidation.right().value()); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(eitherValidation.right().value())); + } + if (eitherValidation.left().value()) { + log.debug("resource with name: {}, already exists", resource.getName()); + loggerSupportability + .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR, + "resource with name: {} already exists", resource.getName()); + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(), + resource.getName()); + } + log.debug("send resource {} to dao for create", resource.getName()); + createArtifactsPlaceHolderData(resource, user); + // enrich object + 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.CVFC) { + resource.setAbstract(false); + } + } + return toscaOperationFacade.createToscaComponent(resource).left().on(r -> throwComponentExceptionByResource(r, resource)); + } + + private Resource throwComponentExceptionByResource(StorageOperationStatus status, Resource resource) { + ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(status), resource); + throw new ByResponseFormatComponentException(responseFormat); + } + + private void createArtifactsPlaceHolderData(Resource resource, User user) { + // create mandatory artifacts + + // TODO it must be removed after that artifact uniqueId creation will be + + // moved to ArtifactOperation + setInformationalArtifactsPlaceHolder(resource, user); + setDeploymentArtifactsPlaceHolder(resource, user); + setToscaArtifactsPlaceHolders(resource, user); + } + + @SuppressWarnings("unchecked") + @Override + public void setDeploymentArtifactsPlaceHolder(Component component, User user) { + Resource resource = (Resource) component; + Map artifactMap = resource.getDeploymentArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + Map deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration() + .getDeploymentResourceArtifacts(); + if (deploymentResourceArtifacts != null) { + Map finalArtifactMap = artifactMap; + deploymentResourceArtifacts.forEach((k, v) -> processDeploymentResourceArtifacts(user, resource, finalArtifactMap, k, v)); + } + resource.setDeploymentArtifacts(artifactMap); + } + + private void processDeploymentResourceArtifacts(User user, Resource resource, Map artifactMap, String k, Object v) { + Map artifactDetails = (Map) v; + Object object = artifactDetails.get(PLACE_HOLDER_RESOURCE_TYPES); + if (object != null) { + List artifactTypes = (List) 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) v, user, ArtifactGroupTypeEnum.DEPLOYMENT); + if (artifactDefinition != null && !artifactMap.containsKey(artifactDefinition.getArtifactLabel())) { + artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); + } + } + } + + @SuppressWarnings("unchecked") + private void setInformationalArtifactsPlaceHolder(Resource resource, User user) { + Map artifactMap = resource.getArtifacts(); + if (artifactMap == null) { + artifactMap = new HashMap<>(); + } + String resourceUniqueId = resource.getUniqueId(); + List exludeResourceCategory = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceCategory(); + List exludeResourceType = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceType(); + Map informationalResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration() + .getInformationalResourceArtifacts(); + List 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 keys = informationalResourceArtifacts.keySet(); + for (String informationalResourceArtifactName : keys) { + Map artifactInfoMap = (Map) informationalResourceArtifacts.get(informationalResourceArtifactName); + ArtifactDefinition artifactDefinition = artifactsBusinessLogic + .createArtifactPlaceHolderInfo(resourceUniqueId, informationalResourceArtifactName, artifactInfoMap, user, + ArtifactGroupTypeEnum.INFORMATIONAL); + artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition); + } + } + resource.setArtifacts(artifactMap); + } + + /** + * deleteResource + * + * @param resourceId + * @param user + * @return + */ + public ResponseFormat deleteResource(String resourceId, User user) { + ResponseFormat responseFormat; + validateUserExists(user); + Either resourceStatus = toscaOperationFacade.getToscaElement(resourceId); + if (resourceStatus.isRight()) { + log.debug("failed to get resource {}", resourceId); + return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), ""); + } + Resource resource = resourceStatus.left().value(); + StorageOperationStatus result = StorageOperationStatus.OK; + lockComponent(resourceId, resource, "Mark resource to delete"); + try { + result = markComponentToDelete(resource); + if (result == StorageOperationStatus.OK) { + responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT); + } else { + ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result); + responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName()); + } + return responseFormat; + } finally { + if (!StorageOperationStatus.OK.equals(result)) { + janusGraphDao.rollback(); + } else { + janusGraphDao.commit(); + } + graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource); + } + } + + private boolean isComponentSystemDeployed(Resource resource) { + return resource.getComponentMetadataDefinition().getMetadataDataDefinition().isNormative(); + } + + /** + * Deletes every version of the provided resource + * + * @param resourceId the resource identifier + * @param user the user that performs the deletion + * @return + * @throws ComponentException if there is any error in the deletion of the resource operation + */ + public void deleteResourceAllVersions(String resourceId, User user) { + validateUserExists(user); + Either resourceStatus = toscaOperationFacade.getToscaElement(resourceId); + if (resourceStatus.isRight()) { + log.debug("Failed to get resource {}", resourceId); + componentException(resourceStatus.right().value()); + } + Resource resource = resourceStatus.left().value(); + if (isComponentSystemDeployed(resource)) { + throw new ByActionStatusComponentException(ActionStatus.CANNOT_DELETE_SYSTEM_DEPLOYED_RESOURCES, ComponentTypeEnum.RESOURCE.getValue(), + resource.getName()); + } + if (Boolean.FALSE.equals(resource.isArchived())) { + log.debug("The resource, {}, requested for delete has not been archived.", resourceId); + throw new ComponentException(ActionStatus.COMPONENT_NOT_ARCHIVED, resourceId); + } + try { + String model = resource.getModel(); + final Optional modelOptional = modelOperation.findModelByName(model); + List deletedResourceList = toscaOperationFacade.deleteComponent(resource.getInvariantUUID(), NodeTypeEnum.Resource, true); + if (log.isDebugEnabled()) { + deletedResourceList.forEach(deletedR -> log.debug("Component {} was deleted.", deletedR)); + } + if (modelOptional.isPresent() && modelOptional.get().getModelType() == ModelTypeEnum.NORMATIVE_EXTENSION) { + modelOperation.deleteModel(modelOptional.get(), true); + } + toscaOperationFacade.commitAndCheck(resource.getUniqueId()); + updateCatalog(resource, ChangeTypeEnum.DELETE); + } catch (ComponentException exception) { + log.debug("Failed to delete resource, {} ", resourceId); + janusGraphDao.rollback(); + throw exception; + } + } + + public ResponseFormat deleteResourceByNameAndVersion(String resourceName, String version, User user) { + ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT); + validateUserExists(user); + Resource resource = null; + StorageOperationStatus result = StorageOperationStatus.OK; + boolean failed = false; + try { + Either resourceStatus = toscaOperationFacade + .getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, version); + if (resourceStatus.isRight()) { + log.debug("failed to get resource {} version {}", resourceName, version); + return componentsUtils + .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), resourceName); + } + resource = resourceStatus.left().value(); + } finally { + janusGraphDao.commit(); + } + if (resource != null) { + lockComponent(resource.getUniqueId(), resource, DELETE_RESOURCE); + try { + result = markComponentToDelete(resource); + if (result != StorageOperationStatus.OK) { + ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result); + responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName()); + return responseFormat; + } + } catch (ComponentException e) { + failed = true; + throw e; + } finally { + if (failed || !StorageOperationStatus.OK.equals(result)) { + janusGraphDao.rollback(); + } else { + janusGraphDao.commit(); + } + graphLockOperation.unlockComponent(resource.getUniqueId(), NodeTypeEnum.Resource); + } + } + return responseFormat; + } + + public Either getResource(String resourceId, User user) { + if (user != null) { + validateUserExists(user); + } + Either storageStatus = toscaOperationFacade.getToscaElement(resourceId); + if (storageStatus.isRight()) { + log.debug("failed to get resource by id {}", resourceId); + return Either.right( + componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), resourceId)); + } + if (storageStatus.left().value() == null) { + return Either.right(componentsUtils + .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND), resourceId)); + } + return Either.left(storageStatus.left().value()); + } + + public Either getResourceByNameAndVersion(String resourceName, String resourceVersion, String userId) { + validateUserExists(userId); + Either getResource = toscaOperationFacade + .getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, resourceVersion); + if (getResource.isRight()) { + log.debug("failed to get resource by name {} and version {}", resourceName, resourceVersion); + return Either.right( + componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResource.right().value()), resourceName)); + } + return Either.left(getResource.left().value()); + } + + /** + * updateResourceMetadata + * + * @param user - modifier data (userId) + * @param inTransaction TODO + * @param resourceIdToUpdate - the resource identifier + * @param newResource + * @return Either + */ + public Resource updateResourceMetadata(String resourceIdToUpdate, Resource newResource, Resource currentResource, User user, + boolean inTransaction) { + validateUserExists(user.getUserId()); + log.debug("Get resource with id {}", resourceIdToUpdate); + boolean needToUnlock = false; + try { + if (currentResource == null) { + Either storageStatus = toscaOperationFacade.getToscaElement(resourceIdToUpdate); + if (storageStatus.isRight()) { + throw new ByResponseFormatComponentException( + componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), "")); + } + currentResource = storageStatus.left().value(); + } + // verify that resource is checked-out and the user is the last + + // updater + if (!ComponentValidationUtils.canWorkOnResource(currentResource, user.getUserId())) { + throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)); + } + // lock resource + StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceIdToUpdate, NodeTypeEnum.Resource); + if (lockResult != StorageOperationStatus.OK) { + BeEcompErrorManager.getInstance() + .logBeFailedLockObjectError("Upload Artifact - lock ", NodeTypeEnum.Resource.getName(), resourceIdToUpdate); + log.debug("Failed to lock resource: {}, error - {}", resourceIdToUpdate, lockResult); + ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockResult)); + throw new ByResponseFormatComponentException(responseFormat); + } + needToUnlock = true; + // critical section starts here + + // convert json to object + + // Update and updated resource must have a non-empty "derivedFrom" + + // list + + // This code is not called from import resources, because of root + + // VF "derivedFrom" should be null (or ignored) + if (ModelConverter.isAtomicComponent(currentResource)) { + validateDerivedFromNotEmpty(null, newResource, null); + validateDerivedFromNotEmpty(null, currentResource, null); + } else { + newResource.setDerivedFrom(null); + } + Either dataModelResponse = updateResourceMetadata(resourceIdToUpdate, newResource, user, currentResource, + true); + if (dataModelResponse.isRight()) { + log.debug("failed to update resource metadata!!!"); + throw new ByResponseFormatComponentException(dataModelResponse.right().value()); + } + log.debug("Resource metadata updated successfully!!!"); + return dataModelResponse.left().value(); + } catch (ComponentException | StorageException e) { + rollback(inTransaction, newResource, null, null); + throw e; + } finally { + if (!inTransaction) { + janusGraphDao.commit(); + } + if (needToUnlock) { + graphLockOperation.unlockComponent(resourceIdToUpdate, NodeTypeEnum.Resource); + } + } + } + + private Either updateResourceMetadata(String resourceIdToUpdate, Resource newResource, User user, + Resource currentResource, boolean inTransaction) { + updateVfModuleGroupsNames(currentResource, newResource); + validateResourceFieldsBeforeUpdate(currentResource, newResource, inTransaction, false); + // Setting last updater and uniqueId + newResource.setContactId(newResource.getContactId().toLowerCase()); + newResource.setLastUpdaterUserId(user.getUserId()); + newResource.setUniqueId(resourceIdToUpdate); + // Cannot set highest version through UI + newResource.setHighestVersion(currentResource.isHighestVersion()); + newResource.setCreationDate(currentResource.getCreationDate()); + Either processUpdateOfDerivedFrom = processUpdateOfDerivedFrom(currentResource, newResource, user.getUserId(), + inTransaction); + if (processUpdateOfDerivedFrom.isRight()) { + log.debug("Couldn't update derived from for resource {}", resourceIdToUpdate); + return Either.right(processUpdateOfDerivedFrom.right().value()); + } + log.debug("send resource {} to dao for update", newResource.getUniqueId()); + if (isNotEmpty(newResource.getGroups())) { + for (GroupDefinition group : newResource.getGroups()) { + if (DEFAULT_GROUP_VF_MODULE.equals(group.getType())) { + groupBusinessLogic + .validateAndUpdateGroupMetadata(newResource.getComponentMetadataDefinition().getMetadataDataDefinition().getUniqueId(), user, + newResource.getComponentType(), group, true, false); + } + } + } + Either dataModelResponse = toscaOperationFacade.updateToscaElement(newResource); + if (dataModelResponse.isRight()) { + ResponseFormat responseFormat = componentsUtils + .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()), newResource); + return Either.right(responseFormat); + } else if (dataModelResponse.left().value() == null) { + log.debug("No response from updateResource"); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + } + return Either.left(dataModelResponse.left().value()); + } + + private void updateVfModuleGroupsNames(Resource currentResource, Resource newResource) { + if (currentResource.getGroups() != null && !currentResource.getName().equals(newResource.getName())) { + List updatedGroups = currentResource.getGroups().stream() + .map(group -> getUpdatedGroup(group, currentResource.getName(), newResource.getName())).collect(toList()); + newResource.setGroups(updatedGroups); + } + } + + private GroupDefinition getUpdatedGroup(GroupDefinition currGroup, String replacePattern, String with) { + GroupDefinition updatedGroup = new GroupDefinition(currGroup); + if (updatedGroup.isSamePrefix(replacePattern) && updatedGroup.getType().equals(DEFAULT_GROUP_VF_MODULE)) { + String prefix = updatedGroup.getName().substring(0, replacePattern.length()); + String newGroupName = updatedGroup.getName().replaceFirst(prefix, with); + updatedGroup.setName(newGroupName); + } + return updatedGroup; + } + + /** + * validateResourceFieldsBeforeCreate + * + * @param user - modifier data (userId) + */ + private void validateResourceFieldsBeforeCreate(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) { + componentValidator.validate(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.CVFC) { + resource.setDerivedFrom(null); + } + validateDerivedFromExist(user, resource, actionEnum); + // warn about non-updatable fields + 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"); + } + } + + /** + * validateResourceFieldsBeforeUpdate + * + * @param currentResource - Resource object to validate + * @param isNested + */ + private void validateResourceFieldsBeforeUpdate(Resource currentResource, Resource updateInfoResource, boolean inTransaction, boolean isNested) { + validateFields(currentResource, updateInfoResource, inTransaction, isNested); + warnNonEditableFields(currentResource, updateInfoResource); + } + + private void warnNonEditableFields(Resource currentResource, Resource updateInfoResource) { + String currentResourceVersion = currentResource.getVersion(); + String updatedResourceVersion = updateInfoResource.getVersion(); + if ((updatedResourceVersion != null) && (!updatedResourceVersion.equals(currentResourceVersion))) { + log.debug("Resource version is automatically set and cannot be updated"); + } + String currentCreatorUserId = currentResource.getCreatorUserId(); + String updatedCreatorUserId = updateInfoResource.getCreatorUserId(); + if ((updatedCreatorUserId != null) && (!updatedCreatorUserId.equals(currentCreatorUserId))) { + log.debug("Resource Creator UserId is automatically set and cannot be updated"); + } + String currentCreatorFullName = currentResource.getCreatorFullName(); + String updatedCreatorFullName = updateInfoResource.getCreatorFullName(); + if ((updatedCreatorFullName != null) && (!updatedCreatorFullName.equals(currentCreatorFullName))) { + log.debug("Resource Creator fullname is automatically set and cannot be updated"); + } + String currentLastUpdaterUserId = currentResource.getLastUpdaterUserId(); + String updatedLastUpdaterUserId = updateInfoResource.getLastUpdaterUserId(); + if ((updatedLastUpdaterUserId != null) && (!updatedLastUpdaterUserId.equals(currentLastUpdaterUserId))) { + log.debug("Resource LastUpdater userId is automatically set and cannot be updated"); + } + String currentLastUpdaterFullName = currentResource.getLastUpdaterFullName(); + String updatedLastUpdaterFullName = updateInfoResource.getLastUpdaterFullName(); + if ((updatedLastUpdaterFullName != null) && (!updatedLastUpdaterFullName.equals(currentLastUpdaterFullName))) { + log.debug("Resource LastUpdater fullname is automatically set and cannot be updated"); + } + Long currentCreationDate = currentResource.getCreationDate(); + Long updatedCreationDate = updateInfoResource.getCreationDate(); + if ((updatedCreationDate != null) && (!updatedCreationDate.equals(currentCreationDate))) { + log.debug("Resource Creation date is automatically set and cannot be updated"); + } + Long currentLastUpdateDate = currentResource.getLastUpdateDate(); + Long updatedLastUpdateDate = updateInfoResource.getLastUpdateDate(); + if ((updatedLastUpdateDate != null) && (!updatedLastUpdateDate.equals(currentLastUpdateDate))) { + log.debug("Resource last update date is automatically set and cannot be updated"); + } + LifecycleStateEnum currentLifecycleState = currentResource.getLifecycleState(); + LifecycleStateEnum updatedLifecycleState = updateInfoResource.getLifecycleState(); + if ((updatedLifecycleState != null) && (!updatedLifecycleState.equals(currentLifecycleState))) { + log.debug("Resource lifecycle state date is automatically set and cannot be updated"); + } + Boolean currentAbstract = currentResource.isAbstract(); + Boolean updatedAbstract = updateInfoResource.isAbstract(); + if ((updatedAbstract != null) && (!updatedAbstract.equals(currentAbstract))) { + log.debug("Resource abstract is automatically set and cannot be updated"); + } + Boolean currentHighestVersion = currentResource.isHighestVersion(); + Boolean updatedHighestVersion = updateInfoResource.isHighestVersion(); + if ((updatedHighestVersion != null) && (!updatedHighestVersion.equals(currentHighestVersion))) { + log.debug("Resource highest version is automatically set and cannot be updated"); + } + String currentUuid = currentResource.getUUID(); + String updatedUuid = updateInfoResource.getUUID(); + if ((updatedUuid != null) && (!updatedUuid.equals(currentUuid))) { + log.debug("Resource UUID is automatically set and cannot be updated"); + } + log.debug("Resource Type cannot be updated"); + String currentInvariantUuid = currentResource.getInvariantUUID(); + String updatedInvariantUuid = updateInfoResource.getInvariantUUID(); + if ((updatedInvariantUuid != null) && (!updatedInvariantUuid.equals(currentInvariantUuid))) { + log.debug("Resource invariant UUID is automatically set and cannot be updated"); + updateInfoResource.setInvariantUUID(currentInvariantUuid); + } + } + + 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"); + componentDescriptionValidator.validateAndCorrectField(null, updateInfoResource, null); + log.debug("validate icon before update"); + validateIcon(currentResource, updateInfoResource, hasBeenCertified); + log.debug("validate tags before update"); + componentTagsValidator.validateAndCorrectField(null, updateInfoResource, null); + log.debug("validate vendor name before update"); + validateVendorName(null, updateInfoResource, null); + log.debug("validate resource vendor model number before update"); + validateResourceVendorModelNumber(currentResource, updateInfoResource); + log.debug("validate vendor release before update"); + validateVendorReleaseName(null, updateInfoResource, null); + log.debug("validate contact info before update"); + componentContactIdValidator.validateAndCorrectField(null, updateInfoResource, null); + log.debug(VALIDATE_DERIVED_BEFORE_UPDATE); + validateDerivedFromDuringUpdate(currentResource, updateInfoResource, hasBeenCertified); + log.debug("validate category before update"); + validateCategory(currentResource, updateInfoResource, hasBeenCertified, inTransaction); + } + + private boolean isResourceNameEquals(Resource currentResource, Resource updateInfoResource) { + String resourceNameUpdated = updateInfoResource.getName(); + String resourceNameCurrent = currentResource.getName(); + if (resourceNameCurrent.equals(resourceNameUpdated)) { + return true; + } + // In case of CVFC type we should support the case of old VF with CVFC + + // instances that were created without the "Cvfc" suffix + return currentResource.getResourceType() == ResourceTypeEnum.CVFC && resourceNameUpdated + .equals(addCvfcSuffixToResourceName(resourceNameCurrent)); + } + + private String addCvfcSuffixToResourceName(String resourceName) { + return resourceName + "Cvfc"; + } + + private void validateResourceName(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified, boolean isNested) { + String resourceNameUpdated = updateInfoResource.getName(); + if (!isResourceNameEquals(currentResource, updateInfoResource)) { + if (isNested || !hasBeenCertified) { + componentNameValidator.validateAndCorrectField(null, updateInfoResource, null); + validateResourceNameUniqueness(updateInfoResource); + 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 ByActionStatusComponentException(ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED); + } + } + } + + private void validateIcon(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) { + String iconUpdated = updateInfoResource.getIcon(); + String iconCurrent = currentResource.getIcon(); + if (!iconCurrent.equals(iconUpdated)) { + if (!hasBeenCertified) { + componentIconValidator.validateAndCorrectField(null, updateInfoResource, null); + } else { + log.info("Icon {} cannot be updated once the resource has been certified once.", iconUpdated); + throw new ByActionStatusComponentException(ActionStatus.RESOURCE_ICON_CANNOT_BE_CHANGED); + } + } + } + + private void validateResourceVendorModelNumber(Resource currentResource, Resource updateInfoResource) { + String updatedResourceVendorModelNumber = updateInfoResource.getResourceVendorModelNumber(); + String currentResourceVendorModelNumber = currentResource.getResourceVendorModelNumber(); + if (!currentResourceVendorModelNumber.equals(updatedResourceVendorModelNumber)) { + validateResourceVendorModelNumber(null, updateInfoResource, null); + } + } + + private Either validateCategory(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified, + boolean inTransaction) { + validateCategory(null, updateInfoResource, null, inTransaction); + if (hasBeenCertified) { + CategoryDefinition currentCategory = currentResource.getCategories().get(0); + SubCategoryDefinition currentSubCategory = currentCategory.getSubcategories().get(0); + CategoryDefinition updateCategory = updateInfoResource.getCategories().get(0); + SubCategoryDefinition updtaeSubCategory = updateCategory.getSubcategories().get(0); + if (!currentCategory.getName().equals(updateCategory.getName()) || !currentSubCategory.getName().equals(updtaeSubCategory.getName())) { + log.info("Category {} cannot be updated once the resource has been certified once.", currentResource.getCategories()); + ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CATEGORY_CANNOT_BE_CHANGED); + return Either.right(errorResponse); + } + } + return Either.left(true); + } + + private Either validateDerivedFromDuringUpdate(Resource currentResource, Resource updateInfoResource, + boolean hasBeenCertified) { + List currentDerivedFrom = currentResource.getDerivedFrom(); + List 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 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; + } + } + } else { + // For derived from, we must know whether it was actually changed, + + // otherwise we must do no action. + + // Due to changes it inflicts on data model (remove artifacts, + + // properties...), it's not like a flat field which can be + + // overwritten if not changed. + + // So we must indicate that derived from is not changed + updateInfoResource.setDerivedFrom(null); + } + return Either.left(true); + } + + private Either validateNestedDerivedFromDuringUpdate(Resource currentResource, Resource updateInfoResource, + boolean hasBeenCertified) { + List currentDerivedFrom = currentResource.getDerivedFrom(); + List 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 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); + } + + private void validateDerivedFromExist(User user, Resource resource, AuditingActionEnum actionEnum) { + if (resource.getDerivedFrom() == null || resource.getDerivedFrom().isEmpty()) { + return; + } + String templateName = resource.getDerivedFrom().get(0); + Either 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 ByActionStatusComponentException(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 ByActionStatusComponentException(ActionStatus.PARENT_RESOURCE_NOT_FOUND); + } + } + + // Tal G for extending inheritance US815447 + private Either validateDerivedFromExtending(User user, Resource currentResource, Resource updateInfoResource, + AuditingActionEnum actionEnum) { + String currentTemplateName = currentResource.getDerivedFrom().get(0); + String updatedTemplateName = updateInfoResource.getDerivedFrom().get(0); + Either dataModelResponse = toscaOperationFacade + .validateToscaResourceNameExtends(currentTemplateName, updatedTemplateName, currentResource.getModel()); + 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 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 ByActionStatusComponentException(ActionStatus.MISSING_DERIVED_FROM_TEMPLATE); + } + } + + private void validateResourceNameUniqueness(Resource resource) { + Either resourceOperationResponse = toscaOperationFacade + .validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType()); + if (resourceOperationResponse.isLeft() && resourceOperationResponse.left().value()) { + log.debug("resource with name: {}, already exists", resource.getName()); + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(), + resource.getName()); + } else if (resourceOperationResponse.isRight()) { + log.debug("error while validateResourceNameExists for resource: {}", resource.getName()); + throw new StorageException(resourceOperationResponse.right().value()); + } + } + + private void validateCategory(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) { + List 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 ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue()); + } + if (categories.size() > 1) { + log.debug("Must be only one category for resource"); + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_TOO_MUCH_CATEGORIES, ComponentTypeEnum.RESOURCE.getValue()); + } + CategoryDefinition category = categories.get(0); + List subcategories = category.getSubcategories(); + if (CollectionUtils.isEmpty(subcategories)) { + log.debug("Missinig subcategory for resource"); + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY); + } + if (subcategories.size() > 1) { + log.debug("Must be only one sub category for resource"); + throw new ByActionStatusComponentException(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 ByActionStatusComponentException(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 ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue()); + } + validateCategoryListed(category, subcategory, user, resource, actionEnum, inTransaction); + } + + private void validateCategoryListed(CategoryDefinition category, SubCategoryDefinition subcategory, User user, Resource resource, + AuditingActionEnum actionEnum, boolean inTransaction) { + ResponseFormat responseFormat; + if (category != null && subcategory != null) { + log.debug("validating resource category {} against valid categories list", category); + Either, ActionStatus> categories = elementDao.getAllCategories(NodeTypeEnum.ResourceNewCategory, inTransaction); + if (categories.isRight()) { + log.debug("failed to retrieve resource categories from JanusGraph"); + responseFormat = componentsUtils.getResponseFormat(categories.right().value()); + componentsUtils.auditResource(responseFormat, user, resource, actionEnum); + throw new ByActionStatusComponentException(categories.right().value()); + } + List categoryList = categories.left().value(); + Optional foundCategory = categoryList.stream().filter(cat -> cat.getName().equals(category.getName())).findFirst(); + if (foundCategory.isEmpty()) { + log.debug("Category {} is not part of resource category group. Resource category valid values are {}", category, categoryList); + failOnInvalidCategory(user, resource, actionEnum); + return; // explisite output even if failOnInvalidCategory throw an exception + } + Optional foundSubcategory = foundCategory.get().getSubcategories().stream() + .filter(subcat -> subcat.getName().equals(subcategory.getName())).findFirst(); + if (foundSubcategory.isEmpty()) { + log.debug("SubCategory {} is not part of resource category group. Resource subcategory valid values are {}", subcategory, + foundCategory.get().getSubcategories()); + failOnInvalidCategory(user, resource, actionEnum); + } + } + } + + private 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 ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue()); + } + + 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 ByActionStatusComponentException(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 ByActionStatusComponentException(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 ByActionStatusComponentException(ActionStatus.INVALID_VENDOR_RELEASE, vendorRelease); + } + } + } + + private 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 ByActionStatusComponentException(ActionStatus.MISSING_VENDOR_NAME); + } + validateVendorName(vendorName, user, resource, actionEnum); + } + + private 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 ByActionStatusComponentException(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 ByActionStatusComponentException(ActionStatus.INVALID_VENDOR_NAME, vendorName); + } + } + } + + private void validateResourceVendorModelNumber(User user, Resource resource, AuditingActionEnum actionEnum) { + String resourceVendorModelNumber = resource.getResourceVendorModelNumber(); + if (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 ByActionStatusComponentException(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 ByActionStatusComponentException(ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER); + } + } + } + + private void validateCost(Resource resource) { + String cost = resource.getCost(); + if (cost != null) { + if (!ValidationUtils.validateCost(cost)) { + log.debug("resource cost is invalid."); + throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT); + } + } + } + + private void validateLicenseType(User user, Resource resource, AuditingActionEnum actionEnum) { + log.debug("validate licenseType"); + String licenseType = resource.getLicenseType(); + if (licenseType != null) { + List 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 ByActionStatusComponentException(ActionStatus.INVALID_CONTENT); + } + } + } + + private Either processUpdateOfDerivedFrom(Resource currentResource, Resource updatedResource, String userId, + boolean inTransaction) { + if (updatedResource.getDerivedFrom() != null) { + log.debug("Starting derived from update for resource {}", updatedResource.getUniqueId()); + log.debug("1. Removing interface artifacts from graph"); + // Remove all interface artifacts of resource + String resourceId = updatedResource.getUniqueId(); + Map interfaces = currentResource.getInterfaces(); + if (interfaces != null) { + Collection values = interfaces.values(); + for (InterfaceDefinition interfaceDefinition : values) { + String interfaceType = interfaceTypeOperation.getShortInterfaceName(interfaceDefinition); + log.trace("Starting interface artifacts removal for interface type {}", interfaceType); + Map operations = interfaceDefinition.getOperationsMap(); + if (operations != null) { + for (Entry operationEntry : operations.entrySet()) { + Operation operation = operationEntry.getValue(); + ArtifactDefinition implementation = operation.getImplementationArtifact(); + if (implementation != null) { + String uniqueId = implementation.getUniqueId(); + log.debug("Removing interface artifact definition {}, operation {}, interfaceType {}", uniqueId, + operationEntry.getKey(), interfaceType); + // only thing that transacts and locks here + Either deleteArtifactByInterface = artifactsBusinessLogic + .deleteArtifactByInterface(resourceId, userId, uniqueId, true); + if (deleteArtifactByInterface.isRight()) { + log.debug("Couldn't remove artifact definition with id {}", uniqueId); + if (!inTransaction) { + janusGraphDao.rollback(); + } + return Either.right(deleteArtifactByInterface.right().value()); + } + } else { + log.trace("No implementation found for operation {} - nothing to delete", operationEntry.getKey()); + } + } + } else { + log.trace("No operations found for interface type {}", interfaceType); + } + } + } + log.debug("2. Removing properties"); + Either, StorageOperationStatus> findPropertiesOfNode = propertyOperation + .deleteAllPropertiesAssociatedToNode(NodeTypeEnum.Resource, resourceId); + if (findPropertiesOfNode.isRight() && findPropertiesOfNode.right().value() != StorageOperationStatus.OK) { + log.debug("Failed to remove all properties of resource"); + if (!inTransaction) { + janusGraphDao.rollback(); + } + return Either + .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(findPropertiesOfNode.right().value()))); + } + } else { + log.debug("Derived from wasn't changed during update"); + } + if (inTransaction) { + return Either.left(true); + } + janusGraphDao.commit(); + return Either.left(true); + } + + public ICapabilityTypeOperation getCapabilityTypeOperation() { + return capabilityTypeOperation; + } + + @Autowired + public void setCapabilityTypeOperation(ICapabilityTypeOperation capabilityTypeOperation) { + this.capabilityTypeOperation = capabilityTypeOperation; + } + + public Boolean validatePropertiesDefaultValues(Resource resource) { + log.debug("validate resource properties default values"); + List properties = resource.getProperties(); + if (properties != null) { + iterateOverProperties(properties, resource.getModel()); + } + return true; + } + + public void iterateOverProperties(List properties, String model) { + for (PropertyDefinition property : properties) { + if (!propertyOperation.isPropertyTypeValid(property, model)) { + log.info("Invalid type for property {}", property); + throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName()); + } + Map allDataTypes = componentsUtils.getAllDataTypes(applicationDataTypeCache, model); + String type = property.getType(); + if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) { + ResponseFormat responseFormat = validateMapOrListPropertyType(property, allDataTypes); + if (responseFormat != null) { + break; + } + } + validateDefaultPropertyValue(property, allDataTypes, type); + } + } + + private void validateDefaultPropertyValue(PropertyDefinition property, Map allDataTypes, String type) { + if (!propertyOperation.isPropertyDefaultValueValid(property, allDataTypes)) { + log.info("Invalid default value for property {}", property); + ResponseFormat responseFormat; + if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) { + throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, property.getName(), type, + property.getDefaultValue()); + } + throw new ByActionStatusComponentException(ActionStatus.INVALID_DEFAULT_VALUE, property.getName(), type, property.getDefaultValue()); + } + } + + private ResponseFormat validateMapOrListPropertyType(PropertyDefinition property, + Map allDataTypes) { + ResponseFormat responseFormat = null; + ImmutablePair propertyInnerTypeValid = propertyOperation.isPropertyInnerTypeValid(property, allDataTypes); + String innerType = propertyInnerTypeValid.getLeft(); + if (Boolean.FALSE.equals(propertyInnerTypeValid.getRight())) { + log.info("Invalid inner type for property {}", property); + responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_INNER_TYPE, innerType, property.getName()); + } + return responseFormat; + } + + @Override + public Either, ResponseFormat> deleteMarkedComponents() { + return deleteMarkedComponents(ComponentTypeEnum.RESOURCE); + } + + @Override + public ComponentInstanceBusinessLogic getComponentInstanceBL() { + return componentInstanceBusinessLogic; + } + + private String getComponentTypeForResponse(Component component) { + String componentTypeForResponse = "SERVICE"; + if (component instanceof Resource) { + componentTypeForResponse = ((Resource) component).getResourceType().name(); + } + return componentTypeForResponse; + } + + public Either getLatestResourceFromCsarUuid(String csarUuid, User user) { + // validate user + if (user != null) { + validateUserExists(user); + } + // get resource from csar uuid + Either either = toscaOperationFacade + .getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUuid, ""); + if (either.isRight()) { + ResponseFormat resp = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_FROM_CSAR_NOT_FOUND, csarUuid); + return Either.right(resp); + } + return Either.left(either.left().value()); + } + + @Override + public Either, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs(String componentId, String userId) { + return null; + } + + private Map> getValidComponentInstanceCapabilities(String resourceId, + Map> defaultCapabilities, + Map> uploadedCapabilities) { + Map> validCapabilitiesMap = new HashMap<>(); + uploadedCapabilities.forEach((k, v) -> addValidComponentInstanceCapabilities(k, v, resourceId, defaultCapabilities, validCapabilitiesMap)); + return validCapabilitiesMap; + } + + private void addValidComponentInstanceCapabilities(String key, List capabilities, String resourceId, + Map> defaultCapabilities, + Map> validCapabilitiesMap) { + String capabilityType = capabilities.get(0).getType(); + if (defaultCapabilities.containsKey(capabilityType)) { + CapabilityDefinition defaultCapability = getCapability(resourceId, defaultCapabilities, capabilityType); + validateCapabilityProperties(capabilities, resourceId, defaultCapability); + List validCapabilityList = new ArrayList<>(); + validCapabilityList.add(defaultCapability); + validCapabilitiesMap.put(key, validCapabilityList); + } else { + throw new ByActionStatusComponentException(ActionStatus.MISSING_CAPABILITY_TYPE, capabilityType); + } + } + + private void validateCapabilityProperties(List 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 ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, resourceId); + } else if (isNotEmpty(capabilities.get(0).getProperties())) { + validateUniquenessUpdateUploadedComponentInstanceCapability(defaultCapability, capabilities.get(0)); + } + } + + private CapabilityDefinition getCapability(String resourceId, Map> defaultCapabilities, + String capabilityType) { + CapabilityDefinition defaultCapability; + if (isNotEmpty(defaultCapabilities.get(capabilityType).get(0).getProperties())) { + defaultCapability = defaultCapabilities.get(capabilityType).get(0); + } else { + Either getFullComponentRes = toscaOperationFacade.getToscaFullElement(resourceId); + if (getFullComponentRes.isRight()) { + log.debug("Failed to get full component {}. Status is {}. ", resourceId, getFullComponentRes.right().value()); + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NOT_FOUND, resourceId); + } + defaultCapability = getFullComponentRes.left().value().getCapabilities().get(capabilityType).get(0); + } + return defaultCapability; + } + + private void validateUniquenessUpdateUploadedComponentInstanceCapability(CapabilityDefinition defaultCapability, + UploadCapInfo uploadedCapability) { + List validProperties = new ArrayList<>(); + Map defaultProperties = defaultCapability.getProperties().stream() + .collect(toMap(PropertyDefinition::getName, Function.identity())); + List 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 ByActionStatusComponentException(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 defaultProperties, String propertyName, String propertyType) { + return propertyType != null && !defaultProperties.get(propertyName).getType().equals(propertyType); + } + + private Either>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation( + List artifactPathAndNameList, List existingArtifactsToHandle, Resource resource, User user) { + EnumMap> nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class); + Wrapper responseWrapper = new Wrapper<>(); + Either>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either + .left(nodeTypeArtifactsToHandle); + try { + // add all found Csar artifacts to list to upload + List artifactsToUpload = new ArrayList<>(artifactPathAndNameList); + List artifactsToUpdate = new ArrayList<>(); + List artifactsToDelete = new ArrayList<>(); + for (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 (foundArtifact.getArtifactType().equals(currNewArtifact.getArtifactType())) { + if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) { + currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId()); + // if current artifact already exists, but has + + // different content, add him to the list to + + // update + artifactsToUpdate.add(currNewArtifact); + } + // remove found artifact from the list of existing + + // artifacts to handle, because it was already + + // handled + existingArtifactsToHandle.remove(foundArtifact); + // and remove found artifact from the list to + + // upload, because it should either be updated or be + + // ignored + 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 = artifactsBusinessLogic + .detectAuditingType(new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE), + foundArtifact.getArtifactChecksum()); + 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 NonMetaArtifactInfo(currArtifact.getArtifactName(), null, currArtifact.getArtifactType(), + currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar())); + } else { + artifactsToUpdate.add(new NonMetaArtifactInfo(currArtifact.getArtifactName(), null, currArtifact.getArtifactType(), + currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar())); + } + } + } + if (responseWrapper.isEmpty()) { + if (!artifactsToUpload.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, artifactsToUpload); + } + if (!artifactsToUpdate.isEmpty()) { + nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, artifactsToUpdate); + } + if (!artifactsToDelete.isEmpty()) { + nodeTypeArtifactsToHandle.put(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 occurred when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e); + } + return nodeTypeArtifactsToHandleRes; + } + + ImmutablePair buildNestedToscaResourceName(final String nodeResourceType, final String vfResourceName, + final 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 { + final String nodeTypeNamePrefix = getNodeTypeNamePrefix(nodeTypeFullName); + log.debug("####### buildNestedToscaResourceName nodeResourceType {}, vfResourceName {}, " + + "nodeTypeFullName {}, actualType {}, vfResourceName {} ", nodeResourceType, vfResourceName, nodeTypeFullName, actualType, + vfResourceName); + final StringBuilder toscaResourceName = new StringBuilder(nodeTypeNamePrefix); + if (!nodeTypeFullName.contains(nodeTypeNamePrefix)) { + nameWithouNamespacePrefix = nodeTypeFullName; + } else { + nameWithouNamespacePrefix = nodeTypeFullName.substring(nodeTypeNamePrefix.length()); + } + final String[] findTypes = nameWithouNamespacePrefix.split("\\."); + String actualName; + if (nodeResourceType.equalsIgnoreCase(findTypes[0])) { + actualName = nameWithouNamespacePrefix.substring(nodeResourceType.length()); + } else { + actualName = "." + nameWithouNamespacePrefix; + } + if (actualName.startsWith(Constants.ABSTRACT)) { + toscaResourceName.append(nodeResourceType.toLowerCase()).append('.').append(ValidationUtils.convertToSystemName(actualVfName)); + } else { + toscaResourceName.append(actualType.toLowerCase()).append('.').append(ValidationUtils.convertToSystemName(actualVfName)).append('.') + .append(Constants.ABSTRACT); + } + final StringBuilder previousToscaResourceName = new StringBuilder(toscaResourceName); + final String[] actualNames = actualName.split("\\."); + if (actualNames.length < 3) { + return new ImmutablePair<>(toscaResourceName.append(actualName.toLowerCase()).toString(), + previousToscaResourceName.append(actualName).toString()); + } + return new ImmutablePair<>(toscaResourceName.append(actualName.toLowerCase()).toString(), + previousToscaResourceName.append(actualName.substring(actualNames[1].length() + 1).toLowerCase()).toString()); + } catch (final Exception e) { + log.debug("Exception occured when buildNestedToscaResourceName, error is:{}", e.getMessage(), e); + throw new ByActionStatusComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE, vfResourceName); + } + } + + /** + * Extracts a Node Type Name prefix from the given Node Type Name. + * + * @param fullName Node Type Name + * @return Node Type Name Prefix + */ + private String getNodeTypeNamePrefix(final String fullName) { + String tempPrefix = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX; + final List definedNodeTypeNamespaceList = getDefinedNodeTypeNamespaceList(); + log.debug("************* getPrefiX fullName {} FROM {}", fullName, definedNodeTypeNamespaceList); + final Optional validNameSpace = validateNodeTypeNamePrefix(fullName, definedNodeTypeNamespaceList); + if (validNameSpace.isPresent()) { + tempPrefix = validNameSpace.get(); + } + log.debug("************* getNodeTypeNamePrefix return fullName {} ", tempPrefix); + return tempPrefix; + } + + @Override + public Either getUiComponentDataTransferByComponentId(String resourceId, + List dataParamsToReturn) { + ComponentParametersView paramsToReturn = new ComponentParametersView(dataParamsToReturn); + Either resourceResultEither = toscaOperationFacade.getToscaElement(resourceId, paramsToReturn); + if (resourceResultEither.isRight()) { + if (resourceResultEither.right().value() == StorageOperationStatus.NOT_FOUND) { + log.debug("Failed to found resource with id {} ", resourceId); + Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId)); + } + log.debug("failed to get resource by id {} with filters {}", resourceId, dataParamsToReturn); + return Either.right( + componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceResultEither.right().value()), "")); + } + Resource resource = resourceResultEither.left().value(); + if (dataParamsToReturn.contains(ComponentFieldsEnum.INPUTS.getValue())) { + ListUtils.emptyIfNull(resource.getInputs()).forEach(input -> input.setConstraints(setInputConstraint(input))); + } + UiComponentDataTransfer dataTransfer = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resource, dataParamsToReturn); + return Either.left(dataTransfer); + } + + @Override + public Either shouldUpgradeToLatestDerived(Component clonedComponent) { + Resource resource = (Resource) clonedComponent; + if (ModelConverter.isAtomicComponent(resource.getResourceType())) { + Either shouldUpgradeToLatestDerived = toscaOperationFacade.shouldUpgradeToLatestDerived(resource); + if (shouldUpgradeToLatestDerived.isRight()) { + return Either.right(componentsUtils.convertFromStorageResponse(shouldUpgradeToLatestDerived.right().value())); + } + return Either.left(shouldUpgradeToLatestDerived.left().value()); + } else { + return super.shouldUpgradeToLatestDerived(clonedComponent); + } + } }