X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-be%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fcomponents%2Fimpl%2FResourceImportManager.java;h=99dea8f04d363564939aa33df116082159b6b0f4;hb=7c61c7bb0369bc27d05c8801c8cb0cca41e494bf;hp=79543ce5cf1bd4ac91a33c65d461ae7dacdd2f2a;hpb=8a5065a44e64cd9eabbb2c16d262eb88be3d0c43;p=sdc.git diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java index 79543ce5cf..99dea8f04d 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java @@ -19,11 +19,32 @@ * Modifications copyright (c) 2019 Nokia * ================================================================================ */ - package org.openecomp.sdc.be.components.impl; +import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataType; +import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataTypeDefinitionWithName; + import fj.data.Either; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; +import java.util.function.Function; +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.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.auditing.api.AuditEventFactory; @@ -33,33 +54,49 @@ import org.openecomp.sdc.be.components.csar.CsarInfo; import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum; import org.openecomp.sdc.be.components.impl.ImportUtils.Constants; 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.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity; import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; 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.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.ComponentInstanceProperty; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.DefaultUploadResourceInfo; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.NodeTypesMetadataList; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.RequirementDefinition; import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.UploadComponentInstanceInfo; +import org.openecomp.sdc.be.model.UploadInterfaceInfo; import org.openecomp.sdc.be.model.UploadResourceInfo; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.category.SubCategoryDefinition; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; +import org.openecomp.sdc.be.model.mapper.NodeTypeMetadataMapper; import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation; @@ -68,54 +105,49 @@ import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData; import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo; import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo; import org.openecomp.sdc.be.utils.TypeUtils; +import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.common.util.ThreadLocalsHolder; import org.openecomp.sdc.common.util.ValidationUtils; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import org.springframework.web.context.WebApplicationContext; import org.yaml.snakeyaml.Yaml; -import javax.servlet.ServletContext; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.function.Function; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -@Component("resourceImportManager") +@org.springframework.stereotype.Component("resourceImportManager") public class ResourceImportManager { - static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern.compile("[\\w\\-\\_\\d\\:]+"); - private static final String IMPLEMENTATION = "implementation"; + static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern.compile("['\\w\\s\\-\\.\\:]+"); + private static final Logger log = Logger.getLogger(ResourceImportManager.class); + private final InterfaceDefinitionHandler interfaceDefinitionHandler; + private final ComponentsUtils componentsUtils; + private final CapabilityTypeOperation capabilityTypeOperation; + private final JanusGraphDao janusGraphDao; private ServletContext servletContext; - private AuditingManager auditingManager; private ResourceBusinessLogic resourceBusinessLogic; - private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic; - + @Autowired + private ServiceBusinessLogic serviceBusinessLogic; private IGraphLockOperation graphLockOperation; - protected ToscaOperationFacade toscaOperationFacade; - - protected final ComponentsUtils componentsUtils; - private final CapabilityTypeOperation capabilityTypeOperation; - + private ToscaOperationFacade toscaOperationFacade; private ResponseFormatManager responseFormatManager; - private static final Logger log = Logger.getLogger(ResourceImportManager.class); - @Autowired - public ResourceImportManager(ComponentsUtils componentsUtils, CapabilityTypeOperation capabilityTypeOperation) { + public ResourceImportManager(final ComponentsUtils componentsUtils, final CapabilityTypeOperation capabilityTypeOperation, + final InterfaceDefinitionHandler interfaceDefinitionHandler, final JanusGraphDao janusGraphDao) { this.componentsUtils = componentsUtils; this.capabilityTypeOperation = capabilityTypeOperation; + this.interfaceDefinitionHandler = interfaceDefinitionHandler; + this.janusGraphDao = janusGraphDao; + } + + public ServiceBusinessLogic getServiceBusinessLogic() { + return serviceBusinessLogic; + } + + public void setServiceBusinessLogic(ServiceBusinessLogic serviceBusinessLogic) { + this.serviceBusinessLogic = serviceBusinessLogic; } @Autowired @@ -123,82 +155,157 @@ public class ResourceImportManager { this.toscaOperationFacade = toscaOperationFacade; } - public ImmutablePair importNormativeResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, boolean createNewVersion, boolean needLock) { - + public ImmutablePair importNormativeResource(final String resourceYml, final UploadResourceInfo resourceMetaData, + final Map instancesFromCsar, + final User creator, final boolean createNewVersion, final boolean needLock, + final boolean isInTransaction) { LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(); lifecycleChangeInfo.setUserRemarks("certification on import"); Function validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource); + return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, isInTransaction, createNewVersion, + needLock, null, null, false, null, null, false, instancesFromCsar); + } - return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock, null, null, false, null, null, false); + public void importAllNormativeResource(final String resourcesYaml, final NodeTypesMetadataList nodeTypesMetadataList, final User user, + final boolean createNewVersion, final boolean needLock) { + final Map nodeTypesYamlMap; + try { + nodeTypesYamlMap = new Yaml().load(resourcesYaml); + } catch (final Exception e) { + log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceImportManager.class.getName(), "Could not parse node types YAML", e); + throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TYPES_YAML); + } + if (!nodeTypesYamlMap.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName())) { + return; + } + final Map nodeTypesMap = (Map) nodeTypesYamlMap.get(ToscaTagNamesEnum.NODE_TYPES.getElementName()); + importAllNormativeResource(nodeTypesMap, nodeTypesMetadataList, null, user, "", createNewVersion,needLock); } - public ImmutablePair importNormativeResourceFromCsar(String resourceYml, UploadResourceInfo resourceMetaData, User creator, boolean createNewVersion, boolean needLock) { + public void importAllNormativeResource(final Map nodeTypesMap, final NodeTypesMetadataList nodeTypesMetadataList, + Map instancesFromCsar, final User user, String model, final boolean createNewVersion, final boolean needLock) { + try { + nodeTypesMetadataList.getNodeMetadataList().forEach(nodeTypeMetadata -> { + final String nodeTypeToscaName = nodeTypeMetadata.getToscaName(); + final Map nodeTypeMap = (Map) nodeTypesMap.get(nodeTypeToscaName); + if (nodeTypeMap == null) { + log.warn(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceImportManager.class.getName(), + "Could not find given node type '{}'. The node will not be created.", nodeTypeToscaName); + } else { + final Map>> nodeTypeDefinitionMap = + Map.of(ToscaTagNamesEnum.NODE_TYPES.getElementName(), + Map.of(nodeTypeToscaName, nodeTypeMap) + ); + final String nodeTypeYaml = new Yaml().dump(nodeTypeDefinitionMap); + UploadResourceInfo uploadResourceInfo = NodeTypeMetadataMapper.mapTo(nodeTypeMetadata); + if (uploadResourceInfo instanceof DefaultUploadResourceInfo) { + uploadResourceInfo.setModel(model); + uploadResourceInfo.setContactId(user.getUserId()); + } + importNormativeResource(nodeTypeYaml, uploadResourceInfo, instancesFromCsar, user, createNewVersion, needLock, true); + } + }); + janusGraphDao.commit(); + } catch (final Exception e) { + janusGraphDao.rollback(); + throw e; + } + } + public ImmutablePair importNormativeResourceFromCsar(String resourceYml, UploadResourceInfo resourceMetaData, + User creator, boolean createNewVersion, boolean needLock) { LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(); lifecycleChangeInfo.setUserRemarks("certification on import"); Function validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource); - - return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock, null, null, false, null, null, false); + return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock, + null, null, false, null, null, false, null); } public ImmutablePair importCertifiedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, Function validationFunction, - LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean isInTransaction, boolean createNewVersion, boolean needLock, Map> nodeTypeArtifactsToHandle, List nodeTypesNewCreatedArtifacts, boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName, boolean isNested) { + LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean isInTransaction, + boolean createNewVersion, boolean needLock, + Map> nodeTypeArtifactsToHandle, + List nodeTypesNewCreatedArtifacts, + boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName, + boolean isNested, Map instancesFromCsar) { Resource resource = new Resource(); ImmutablePair responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED); Either, ResponseFormat> response = Either.left(responsePair); - String latestCertifiedResourceId = null; try { boolean shouldBeCertified = nodeTypeArtifactsToHandle == null || nodeTypeArtifactsToHandle.isEmpty(); setConstantMetaData(resource, shouldBeCertified); - setMetaDataFromJson(resourceMetaData, resource); - - populateResourceFromYaml(resourceYml, resource); - - Boolean isValidResource = validationFunction.apply(resource); - if (!createNewVersion) { - Either latestByName = toscaOperationFacade.getLatestByName(resource.getName()); - if (latestByName.isLeft()) { - throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, resource.getName()); - } - } - resource = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, true, isInTransaction, needLock, csarInfo, nodeName, isNested).left; - Resource changeStateResponse; - - if (nodeTypeArtifactsToHandle != null && !nodeTypeArtifactsToHandle.isEmpty()) { - Either, ResponseFormat> handleNodeTypeArtifactsRes = - resourceBusinessLogic.handleNodeTypeArtifacts(resource, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, creator, isInTransaction, false); - if (handleNodeTypeArtifactsRes.isRight()) { - //TODO: should be used more correct action - throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); - } + setResourceMetaData(resource, resourceYml, resourceMetaData); + populateResourceFromYaml(resourceYml, resource, instancesFromCsar); + validationFunction.apply(resource); + resource.getComponentMetadataDefinition().getMetadataDataDefinition().setNormative(resourceMetaData.isNormative()); + checkResourceExists(createNewVersion, csarInfo, resource); + resource = resourceBusinessLogic + .createOrUpdateResourceByImport(resource, creator, true, isInTransaction, needLock, csarInfo, nodeName, isNested).left; + Resource changeStateResponse; + if (nodeTypeArtifactsToHandle != null && !nodeTypeArtifactsToHandle.isEmpty()) { + Either, ResponseFormat> handleNodeTypeArtifactsRes = resourceBusinessLogic + .handleNodeTypeArtifacts(resource, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, creator, isInTransaction, false); + if (handleNodeTypeArtifactsRes.isRight()) { + //TODO: should be used more correct action + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); } - latestCertifiedResourceId = getLatestCertifiedResourceId(resource); - changeStateResponse = resourceBusinessLogic.propagateStateToCertified(creator, resource, lifecycleChangeInfo, isInTransaction, needLock, forceCertificationAllowed); - responsePair = new ImmutablePair<>(changeStateResponse, response.left() - .value().right); - } - catch (RuntimeException e) { + } + latestCertifiedResourceId = getLatestCertifiedResourceId(resource); + changeStateResponse = resourceBusinessLogic + .propagateStateToCertified(creator, resource, lifecycleChangeInfo, isInTransaction, needLock, forceCertificationAllowed); + responsePair = new ImmutablePair<>(changeStateResponse, response.left().value().right); + } catch (RuntimeException e) { handleImportResourceException(resourceMetaData, creator, true, e); - } - finally { + } finally { if (latestCertifiedResourceId != null && needLock) { log.debug("unlock resource {}", latestCertifiedResourceId); graphLockOperation.unlockComponent(latestCertifiedResourceId, NodeTypeEnum.Resource); } } - return responsePair; } - private ResponseFormat getResponseFormatFromComponentException(RuntimeException e) { - if(e instanceof ComponentException){ - return ((ComponentException) e).getResponseFormat() == null ? - componentsUtils.getResponseFormat(((ComponentException) e).getActionStatus(), ((ComponentException) e).getParams()) : - ((ComponentException) e).getResponseFormat(); + private void checkResourceExists(final boolean isCreate, final CsarInfo csarInfo, final Resource resource) { + if (isCreate) { + checkResourceExistsOnCreate(resource, csarInfo); + } else { + checkResourceExistsOnUpdate(resource); } - return null; + } + + private void checkResourceExistsOnCreate(final Resource resource, final CsarInfo csarInfo) { + if (isCsarPresent(csarInfo)) { + return; + } + final Either resourceEither = + toscaOperationFacade.getComponentByNameAndVendorRelease(resource.getComponentType(), resource.getName(), + resource.getVendorRelease(), JsonParseFlagEnum.ParseAll, resource.getModel()); + if (resourceEither.isLeft() && toscaOperationFacade.isNodeAssociatedToModel(resource.getModel(), resource)) { + if (resource.getModel() == null) { + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_VENDOR_RELEASE_ALREADY_EXISTS, + resource.getName(), resource.getVendorRelease()); + } + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_VENDOR_RELEASE_ALREADY_EXISTS_IN_MODEL, + resource.getName(), resource.getVendorRelease(), resource.getModel()); + } + } + + private void checkResourceExistsOnUpdate(final Resource resource) { + final String model = resource.getModel(); + final Either latestByName = toscaOperationFacade.getLatestByName(resource.getName(), model); + if (latestByName.isLeft() && toscaOperationFacade.isNodeAssociatedToModel(model, resource)) { + if (model == null) { + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, + resource.getResourceType().name(), resource.getName()); + } + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_MODEL_ALREADY_EXIST, resource.getName(), model); + } + } + + private boolean isCsarPresent(final CsarInfo csarInfo) { + return csarInfo != null && StringUtils.isNotEmpty(csarInfo.getCsarUUID()); } private String getLatestCertifiedResourceId(Resource resource) { @@ -212,8 +319,7 @@ public class ResourceImportManager { } } return allVersions.get(String.valueOf(latestCertifiedVersion)); - } - else { + } else { return null; } } @@ -228,212 +334,214 @@ public class ResourceImportManager { resource.setIcon(resourceMetaData.getResourceIconPath()); resource.setResourceVendorModelNumber(resourceMetaData.getResourceVendorModelNumber()); resource.setResourceType(ResourceTypeEnum.valueOf(resourceMetaData.getResourceType())); + resource.setTenant(resourceMetaData.getTenant()); if (resourceMetaData.getVendorName() != null) { resource.setVendorName(resourceMetaData.getVendorName()); } if (resourceMetaData.getVendorRelease() != null) { resource.setVendorRelease(resourceMetaData.getVendorRelease()); } + if (resourceMetaData.getModel() != null) { + resource.setModel(resourceMetaData.getModel()); + } } } - public ImmutablePair importUserDefinedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, boolean isInTransaction) { - + public ImmutablePair importUserDefinedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, + boolean isInTransaction) { Resource resource = new Resource(); ImmutablePair responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED); - try { setMetaDataFromJson(resourceMetaData, resource); - - populateResourceFromYaml(resourceYml, resource); - - // currently import VF isn't supported. In future will be supported - // import VF only with CSAR file!! + populateResourceFromYaml(resourceYml, resource, null); + // currently import VF isn't supported. In future will be supported import VF only with CSAR file!! if (ResourceTypeEnum.VF == resource.getResourceType()) { log.debug("Now import VF isn't supported. It will be supported in future with CSAR file only"); throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION); } - resourceBusinessLogic.validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE); - Boolean validatePropertiesTypes = resourceBusinessLogic.validatePropertiesDefaultValues(resource); - - responsePair = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, - false, isInTransaction, true, null, null, false); - - } - catch (RuntimeException e) { + resourceBusinessLogic.validatePropertiesDefaultValues(resource); + responsePair = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, false, isInTransaction, true, null, null, false); + } catch (RuntimeException e) { handleImportResourceException(resourceMetaData, creator, false, e); } return responsePair; - } - void populateResourceFromYaml(String resourceYml, Resource resource) { - @SuppressWarnings("unchecked") -// Either eitherResult = Either.left(true); - Object ymlObj = new Yaml().load(resourceYml); + private void populateResourceFromYaml(final String resourceYml, Resource resource, Map instancesFromCsar) { + @SuppressWarnings("unchecked") Object ymlObj = new Yaml().load(resourceYml); if (ymlObj instanceof Map) { - Map toscaJsonAll = (Map) ymlObj; + final Either existingResource = getExistingResource(resource); + final Map toscaJsonAll = (Map) ymlObj; Map toscaJson = toscaJsonAll; - - // Checks if exist and builds the node_types map - if (toscaJsonAll.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName()) && resource.getResourceType() != ResourceTypeEnum.CVFC) { + if (toscaJsonAll.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName()) && resource.getResourceType() != ResourceTypeEnum.CVFC) { toscaJson = new HashMap<>(); - toscaJson.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(), toscaJsonAll.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName())); + toscaJson.put(ToscaTagNamesEnum.NODE_TYPES.getElementName(), toscaJsonAll.get(ToscaTagNamesEnum.NODE_TYPES.getElementName())); + } + final List foundElements = new ArrayList<>(); + final Either, ResultStatusEnum> toscaElements = ImportUtils + .findToscaElements(toscaJsonAll, ToscaTagNamesEnum.DATA_TYPES.getElementName(), ToscaElementTypeEnum.MAP, foundElements); + if (toscaElements.isLeft()) { + final Map toscaAttributes = (Map) foundElements.get(0); + if (MapUtils.isNotEmpty(toscaAttributes)) { + resource.setDataTypes(extractDataTypeFromJson(resourceBusinessLogic, toscaAttributes, resource.getModel())); + } } - // Derived From - Resource parentResource = setDerivedFrom(toscaJson, resource); + final Resource parentResource = setDerivedFrom(toscaJson, resource); if (StringUtils.isEmpty(resource.getToscaResourceName())) { setToscaResourceName(toscaJson, resource); } - setAttributes(toscaJson, resource); setCapabilities(toscaJson, resource, parentResource); - setProperties(toscaJson, resource); + setProperties(toscaJson, resource, existingResource); + setAttributes(toscaJson, resource); setRequirements(toscaJson, resource, parentResource); - setInterfaceLifecycle(toscaJson, resource); - } - else { + setInterfaceLifecycle(toscaJson, resource, existingResource, instancesFromCsar); + } else { throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); } + } + private Either getExistingResource(final Resource resource) { + final Either, JanusGraphOperationStatus> byCriteria = janusGraphDao.getByCriteria( + getVertexTypeEnum(resource.getResourceType()), propertiesToMatch(resource), propertiesToNotMatch(), + JsonParseFlagEnum.ParseAll, resource.getModel(), false); + if (byCriteria.isLeft() && CollectionUtils.isNotEmpty(byCriteria.left().value())) { + final List graphVertexList = byCriteria.left().value(); + if (graphVertexList.size() == 1) { + return toscaOperationFacade.getToscaElement(graphVertexList.get(0).getUniqueId()); + } else { + final Optional vertex = graphVertexList.stream() + .max(Comparator.comparing(graphVertex -> (String) graphVertex.getMetadataProperties().get(GraphPropertyEnum.VERSION))); + if (vertex.isPresent()) { + return toscaOperationFacade.getToscaElement(vertex.get().getUniqueId()); + } + } + } + return Either.right(StorageOperationStatus.NOT_FOUND); + } + + private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) { + return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE; + } + + private Map propertiesToMatch(final Resource resource) { + final Map graphProperties = new EnumMap<>(GraphPropertyEnum.class); + graphProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normaliseComponentName(resource.getName())); + graphProperties.put(GraphPropertyEnum.COMPONENT_TYPE, resource.getComponentType().name()); + graphProperties.put(GraphPropertyEnum.RESOURCE_TYPE, resource.getResourceType().name()); + graphProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + return graphProperties; + } + + private Map propertiesToNotMatch() { + final Map graphProperties = new EnumMap<>(GraphPropertyEnum.class); + graphProperties.put(GraphPropertyEnum.IS_DELETED, true); + graphProperties.put(GraphPropertyEnum.IS_ARCHIVED, true); + return graphProperties; } private void setToscaResourceName(Map toscaJson, Resource resource) { - Either, ResultStatusEnum> toscaElement = ImportUtils.findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.NODE_TYPES); - if (toscaElement.isLeft() || toscaElement.left().value().size() == 1) { - String toscaResourceName = toscaElement.left().value().keySet().iterator().next(); - resource.setToscaResourceName(toscaResourceName); + resource.setToscaResourceName(getToscaResourceName(toscaJson)); + } + + private String getToscaResourceName(Map toscaJson) { + Either, ResultStatusEnum> toscaElement = ImportUtils + .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.NODE_TYPES); + if (toscaElement.isLeft() && toscaElement.left().value().size() == 1) { + return toscaElement.left().value().keySet().iterator().next(); } + return null; } - private void setInterfaceLifecycle(Map toscaJson, Resource resource) { - Either, ResultStatusEnum> toscaInterfaces = ImportUtils.findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.INTERFACES); + private void setInterfaceLifecycle(Map toscaJson, Resource resource, Either existingResource, + Map instancesFromCsar) { + final Either, ResultStatusEnum> toscaInterfaces = ImportUtils + .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.INTERFACES); + final Map moduleInterfaces = new HashMap<>(); + final Map map; + List interfaceInfoList = null; + if (MapUtils.isNotEmpty(instancesFromCsar)) { + interfaceInfoList = instancesFromCsar.values().stream().filter(i -> MapUtils.isNotEmpty(i.getInterfaces())) + .flatMap(i -> i.getInterfaces().values().stream()).collect(Collectors.toList()); + } if (toscaInterfaces.isLeft()) { - Map jsonInterfaces = toscaInterfaces.left().value(); - Map moduleInterfaces = new HashMap<>(); - Iterator> interfacesNameValue = jsonInterfaces.entrySet().iterator(); - while (interfacesNameValue.hasNext()) { - Entry interfaceNameValue = interfacesNameValue.next(); - Either eitherInterface = createModuleInterface(interfaceNameValue - .getValue(), resource); + map = toscaInterfaces.left().value(); + for (final Entry interfaceNameValue : map.entrySet()) { + final Either eitherInterface = + createModuleInterface(interfaceNameValue.getValue(), resource.getModel()); if (eitherInterface.isRight()) { log.info("error when creating interface:{}, for resource:{}", interfaceNameValue.getKey(), resource.getName()); + } else { + final InterfaceDefinition interfaceDefinition = eitherInterface.left().value(); + if (CollectionUtils.isNotEmpty(interfaceInfoList)) { + updateInterfaceDefinition(interfaceDefinition, interfaceInfoList); + } + moduleInterfaces.put(interfaceDefinition.getType(), interfaceDefinition); } - else { - moduleInterfaces.put(interfaceNameValue.getKey(), eitherInterface.left().value()); - } - } - if (moduleInterfaces.size() > 0) { - resource.setInterfaces(moduleInterfaces); + } else { + map = Collections.emptyMap(); + } + if (existingResource.isLeft()) { + final Map interfaces = existingResource.left().value().getInterfaces(); + if (MapUtils.isNotEmpty(interfaces)) { + final Map userCreatedInterfaceDefinitions = + interfaces.entrySet().stream() + .filter(i -> i.getValue().isUserCreated()) + .filter(i -> !map.containsKey(i.getValue().getType())) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + if (MapUtils.isNotEmpty(userCreatedInterfaceDefinitions)) { + moduleInterfaces.putAll(userCreatedInterfaceDefinitions); + } } } + + if (MapUtils.isNotEmpty(moduleInterfaces)) { + resource.setInterfaces(moduleInterfaces); + } } - private Either createModuleInterface(Object interfaceJson, Resource resource) { - InterfaceDefinition interf = new InterfaceDefinition(); - Either result = Either.left(interf); + private void updateInterfaceDefinition(InterfaceDefinition interfaceDefinition, List interfaceInfoList) { + Map operations = new HashMap<>(); + interfaceInfoList.stream().filter(i -> interfaceDefinition.getType().endsWith(i.getKey())).forEach(i -> { + i.getOperations().values().forEach(o -> { + o.setImplementation(null); + }); + operations.putAll(i.getOperations()); + }); + interfaceDefinition.setOperations(operations); + } + private Either createModuleInterface(final Object interfaceJson, final String model) { try { if (interfaceJson instanceof String) { - String requirementJsonString = (String) interfaceJson; - interf.setType(requirementJsonString); - } - else if (interfaceJson instanceof Map && ResourceTypeEnum.VFC.equals(resource.getResourceType())) { - Map requirementJsonMap = (Map) interfaceJson; - Map operations = new HashMap<>(); - - for (final Entry entry : requirementJsonMap.entrySet()) { - if (entryIsInterfaceType(entry)) { - String type = (String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName()); - interf.setType(type); - interf.setUniqueId(type.toLowerCase()); - } else if (entryContainsImplementationForAKnownOperation(entry, interf.getType())){ - - OperationDataDefinition operation = new OperationDataDefinition(); - operation.setName(entry.getKey()); - - ArtifactDataDefinition implementation = new ArtifactDataDefinition(); - // Adding the artifact name in quotes to indicate that this is a literal value, rather than a reference to - // an SDC artifact - implementation.setArtifactName(Constants.ESCAPED_DOUBLE_QUOTE + ((Map)entry.getValue()).get(IMPLEMENTATION) + Constants.ESCAPED_DOUBLE_QUOTE); - operation.setImplementation(implementation); - - operations.put(entry.getKey(), operation); - } - } - if (!operations.isEmpty()) { - interf.setOperations(operations); - } + final InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); + interfaceDefinition.setType((String) interfaceJson); + return Either.left(interfaceDefinition); } - else { - result = Either.right(ResultStatusEnum.GENERAL_ERROR); + if (interfaceJson instanceof Map) { + final Map interfaceJsonMap = (Map) interfaceJson; + final InterfaceDefinition interfaceDefinition = interfaceDefinitionHandler.create(interfaceJsonMap, model); + return Either.left(interfaceDefinition); } - - } - catch (Exception e) { + return Either.right(ResultStatusEnum.GENERAL_ERROR); + } catch (final Exception e) { BeEcompErrorManager.getInstance().logBeSystemError("Import Resource- create interface"); log.debug("error when creating interface, message:{}", e.getMessage(), e); - result = Either.right(ResultStatusEnum.GENERAL_ERROR); - } - - return result; - } - - private boolean entryIsInterfaceType(final Entry entry) { - if(entry.getKey().equals(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) { - if (entry.getValue() instanceof String) { - return true; - } - throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML); - } - return false; - } - - private boolean entryContainsImplementationForAKnownOperation(final Entry entry, final String interfaceType) { - if (entry.getValue() instanceof Map && ((Map)entry.getValue()).containsKey(IMPLEMENTATION)) { - if (isAKnownOperation(interfaceType, entry.getKey())){ - return true; - } - throw new ByActionStatusComponentException(ActionStatus.INTERFACE_OPERATION_NOT_FOUND); - } - return false; - } - - private boolean isAKnownOperation(String interfaceType, String operation) { - Either, ResponseFormat> interfaceLifecycleTypes = interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes(); - if (interfaceLifecycleTypes.isRight() || interfaceLifecycleTypes.left().value() == null) { - return false; - } - - for (Entry interfaceLifecycleType : interfaceLifecycleTypes.left().value().entrySet()) { - if (interfaceTypeAndOperationMatches(interfaceLifecycleType, interfaceType, operation)) { - return true; - } - } - return false; - } - - private boolean interfaceTypeAndOperationMatches(Entry interfaceLifecycleType, String interfaceType, String operation) { - if (interfaceLifecycleType.getKey().equalsIgnoreCase(interfaceType) && interfaceLifecycleType.getValue().getOperations() != null) { - for (String interfaceLifecycleTypeOperation : interfaceLifecycleType.getValue().getOperations().keySet()) { - if (interfaceLifecycleTypeOperation != null && interfaceLifecycleTypeOperation.equalsIgnoreCase(operation)){ - return true; - } - } - } - return false; - } - - private void setRequirements(Map toscaJson, Resource resource, Resource parentResource) {// Note that parentResource can be null - Either, ResultStatusEnum> toscaRequirements = ImportUtils.findFirstToscaListElement(toscaJson, TypeUtils.ToscaTagNamesEnum.REQUIREMENTS); + return Either.right(ResultStatusEnum.GENERAL_ERROR); + } + } + + private void setRequirements(Map toscaJson, Resource resource, + Resource parentResource) {// Note that parentResource can be null + Either, ResultStatusEnum> toscaRequirements = ImportUtils + .findFirstToscaListElement(toscaJson, ToscaTagNamesEnum.REQUIREMENTS); if (toscaRequirements.isLeft()) { List jsonRequirements = toscaRequirements.left().value(); Map> moduleRequirements = new HashMap<>(); // Checking for name duplication Set reqNames = new HashSet<>(); - // Getting flattened list of capabilities of parent node - cap name - // to cap type + // Getting flattened list of capabilities of parent node - cap name to cap type Map reqName2TypeMap = getReqName2Type(parentResource); for (Object jsonRequirementObj : jsonRequirements) { // Requirement @@ -445,128 +553,148 @@ public class ResourceImportManager { throw new ByActionStatusComponentException(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "requirement", reqNameLowerCase); } reqNames.add(reqNameLowerCase); - RequirementDefinition requirementDef = createRequirementFromImportFile(requirementJsonWrapper - .get(requirementName)); + RequirementDefinition requirementDef = createRequirementFromImportFile(requirementJsonWrapper.get(requirementName)); requirementDef.setName(requirementName); if (moduleRequirements.containsKey(requirementDef.getCapability())) { moduleRequirements.get(requirementDef.getCapability()).add(requirementDef); - } - else { + } else { List list = new ArrayList<>(); list.add(requirementDef); moduleRequirements.put(requirementDef.getCapability(), list); } - // Validating against req/cap of "derived from" node - Boolean validateVsParentCap = validateCapNameVsDerived(reqName2TypeMap, requirementDef - .getCapability(), requirementDef.getName()); + Boolean validateVsParentCap = validateCapNameVsDerived(reqName2TypeMap, requirementDef.getCapability(), requirementDef.getName()); if (!validateVsParentCap) { String parentResourceName = parentResource != null ? parentResource.getName() : ""; log.debug("Requirement with name {} already exists in parent {}", requirementDef.getName(), parentResourceName); - throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "requirement", requirementDef - .getName() - .toLowerCase(), parentResourceName); + throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "requirement", + requirementDef.getName().toLowerCase(), parentResourceName); } } if (moduleRequirements.size() > 0) { resource.setRequirements(moduleRequirements); } - } - } + } private RequirementDefinition createRequirementFromImportFile(Object requirementJson) { RequirementDefinition requirement = new RequirementDefinition(); - if (requirementJson instanceof String) { String requirementJsonString = (String) requirementJson; requirement.setCapability(requirementJsonString); - } - else if (requirementJson instanceof Map) { + } else if (requirementJson instanceof Map) { Map requirementJsonMap = (Map) requirementJson; - if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.CAPABILITY.getElementName())) { - requirement.setCapability((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.CAPABILITY.getElementName())); + if (requirementJsonMap.containsKey(ToscaTagNamesEnum.CAPABILITY.getElementName())) { + requirement.setCapability((String) requirementJsonMap.get(ToscaTagNamesEnum.CAPABILITY.getElementName())); } - - if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.NODE.getElementName())) { - requirement.setNode((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.NODE.getElementName())); + if (requirementJsonMap.containsKey(ToscaTagNamesEnum.NODE.getElementName())) { + requirement.setNode((String) requirementJsonMap.get(ToscaTagNamesEnum.NODE.getElementName())); } - - if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.RELATIONSHIP.getElementName())) { - requirement.setRelationship((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.RELATIONSHIP.getElementName())); + if (requirementJsonMap.containsKey(ToscaTagNamesEnum.RELATIONSHIP.getElementName())) { + requirement.setRelationship((String) requirementJsonMap.get(ToscaTagNamesEnum.RELATIONSHIP.getElementName())); } - if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) { - List occurrencesList = (List) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName()); + if (requirementJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) { + List occurrencesList = (List) requirementJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName()); validateOccurrences(occurrencesList); requirement.setMinOccurrences(occurrencesList.get(0).toString()); requirement.setMaxOccurrences(occurrencesList.get(1).toString()); } - } - else { + } else { throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML); } return requirement; } - private void setProperties(Map toscaJson, Resource resource) { - Map reducedToscaJson = new HashMap<>(toscaJson); + private void setProperties(final Map toscaJson, final Resource resource, + final Either existingResource) { + final Map reducedToscaJson = new HashMap<>(toscaJson); ImportUtils.removeElementFromJsonMap(reducedToscaJson, "capabilities"); - Either, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson); + final Either, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson); if (properties.isLeft()) { - List propertiesList = new ArrayList<>(); - Map value = properties.left().value(); - if (value != null) { - for (Entry entry : value.entrySet()) { - String name = entry.getKey(); - if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) { - log.debug("The property with invalid name {} occured upon import resource {}. ", name, resource.getName()); - throw new ByActionStatusComponentException(componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY)); + final Map propertyDefinitionMap = properties.left().value(); + if (MapUtils.isNotEmpty(propertyDefinitionMap)) { + final List propertiesList = new ArrayList<>(); + for (final Entry entry : propertyDefinitionMap.entrySet()) { + addPropertyToList(resource.getName(), propertiesList, entry); + } + if (existingResource.isLeft() && CollectionUtils.isNotEmpty(existingResource.left().value().getProperties())) { + final List userCreatedResourceProperties = + existingResource.left().value().getProperties().stream() + .filter(PropertyDataDefinition::isUserCreated) + .filter(propertyDefinition -> !propertyDefinitionMap.containsKey(propertyDefinition.getName())) + .collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(userCreatedResourceProperties)) { + propertiesList.addAll(userCreatedResourceProperties); } - PropertyDefinition propertyDefinition = entry.getValue(); - propertyDefinition.setName(name); - propertiesList.add(propertyDefinition); } + + resource.setProperties(propertiesList); } - resource.setProperties(propertiesList); + } else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) { + throw new ByActionStatusComponentException( + componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY)); } - else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) { - throw new ByActionStatusComponentException(componentsUtils.convertFromResultStatusEnum(properties - .right() - .value(), JsonPresentationFields.PROPERTY)); + } + + private void addPropertyToList(final String resourceName, + final List propertiesList, + final Entry entry) { + final String propertyName = entry.getKey(); + if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(propertyName).matches()) { + log.debug("The property with invalid name {} occured upon import resource {}. ", propertyName, resourceName); + throw new ByActionStatusComponentException( + componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY)); } + final PropertyDefinition propertyDefinition = entry.getValue(); + propertyDefinition.setName(propertyName); + propertiesList.add(propertyDefinition); } - private ResultStatusEnum setAttributes(Map toscaJson, Resource resource) { - ResultStatusEnum result = ResultStatusEnum.OK; - Either, ResultStatusEnum> attributes = ImportUtils.getAttributes(toscaJson); - if (attributes.isLeft()) { - List attributeList = new ArrayList<>(); - Map value = attributes.left().value(); - if (value != null) { - for (Entry entry : value.entrySet()) { - String name = entry.getKey(); - PropertyDefinition attributeDef = entry.getValue(); - attributeDef.setName(name); - attributeList.add(attributeDef); - } + private void setAttributes(final Map originalToscaJsonMap, final Resource resource) { + final Map toscaJsonMap = new HashMap<>(originalToscaJsonMap); + ImportUtils.removeElementFromJsonMap(toscaJsonMap, "capabilities"); + final Either, ResultStatusEnum> getAttributeEither = ImportUtils.getAttributes(toscaJsonMap); + if (getAttributeEither.isRight()) { + final ResultStatusEnum resultStatus = getAttributeEither.right().value(); + if (resultStatus == ResultStatusEnum.ELEMENT_NOT_FOUND) { + return; } - resource.setAttributes(attributeList); + throw new ByActionStatusComponentException(componentsUtils.convertFromResultStatusEnum(resultStatus, JsonPresentationFields.ATTRIBUTES)); + } + final List attributeDefinitionList = new ArrayList<>(); + final Map attributeMap = getAttributeEither.left().value(); + if (MapUtils.isEmpty(attributeMap)) { + return; } - else { - result = attributes.right().value(); + for (final Entry entry : attributeMap.entrySet()) { + final String name = entry.getKey(); + if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) { + log.debug("Detected attribute with invalid name '{}' during resource '{}' import. ", name, resource.getName()); + throw new ByActionStatusComponentException( + componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_ATTRIBUTE_NAME, JsonPresentationFields.ATTRIBUTES)); + } + final AttributeDefinition attributeDefinition = entry.getValue(); + attributeDefinition.setName(name); + if (attributeDefinition.getEntry_schema() != null && attributeDefinition.getEntry_schema().getType() != null) { + attributeDefinition.setSchema(new SchemaDefinition()); + attributeDefinition.getSchema().setProperty(new PropertyDataDefinition()); + attributeDefinition.getSchema().getProperty().setType(entry.getValue().getEntry_schema().getType()); + } + attributeDefinitionList.add(attributeDefinition); } - return result; + resource.setAttributes(attributeDefinitionList); } private Resource setDerivedFrom(Map toscaJson, Resource resource) { - Either toscaDerivedFromElement = ImportUtils.findFirstToscaStringElement(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM); + Either toscaDerivedFromElement = ImportUtils + .findFirstToscaStringElement(toscaJson, ToscaTagNamesEnum.DERIVED_FROM); Resource derivedFromResource = null; if (toscaDerivedFromElement.isLeft()) { String derivedFrom = toscaDerivedFromElement.left().value(); log.debug("Derived from TOSCA name is {}", derivedFrom); resource.setDerivedFrom(Arrays.asList(new String[]{derivedFrom})); - Either latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom); - + Either latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom, + resource.getModel()); if (latestByToscaResourceName.isRight()) { StorageOperationStatus operationStatus = latestByToscaResourceName.right().value(); if (operationStatus == StorageOperationStatus.NOT_FOUND) { @@ -574,8 +702,7 @@ public class ResourceImportManager { } log.debug("Error when fetching parent resource {}, error: {}", derivedFrom, operationStatus); ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus); - BeEcompErrorManager.getInstance() - .logBeComponentMissingError("Import TOSCA YAML", "resource", derivedFrom); + BeEcompErrorManager.getInstance().logBeComponentMissingError("Import TOSCA YAML", "resource", derivedFrom); throw new ByActionStatusComponentException(convertFromStorageResponse, derivedFrom); } derivedFromResource = latestByToscaResourceName.left().value(); @@ -583,19 +710,21 @@ public class ResourceImportManager { return derivedFromResource; } - private void setCapabilities(Map toscaJson, Resource resource, Resource parentResource) {// Note that parentResource can be null - Either, ResultStatusEnum> toscaCapabilities = ImportUtils.findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.CAPABILITIES); + private void setCapabilities(Map toscaJson, Resource resource, + Resource parentResource) {// Note that parentResource can be null + Either, ResultStatusEnum> toscaCapabilities = ImportUtils + .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.CAPABILITIES); if (toscaCapabilities.isLeft()) { Map jsonCapabilities = toscaCapabilities.left().value(); Map> moduleCapabilities = new HashMap<>(); Iterator> capabilitiesNameValue = jsonCapabilities.entrySet().iterator(); Set capNames = new HashSet<>(); // Getting flattened list of capabilities of parent node - cap name + // to cap type Map capName2TypeMap = getCapName2Type(parentResource); while (capabilitiesNameValue.hasNext()) { Entry capabilityNameValue = capabilitiesNameValue.next(); - // Validating that no req/cap duplicates exist in imported YAML String capNameLowerCase = capabilityNameValue.getKey().toLowerCase(); if (capNames.contains(capNameLowerCase)) { @@ -603,32 +732,27 @@ public class ResourceImportManager { throw new ByActionStatusComponentException(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "capability", capNameLowerCase); } capNames.add(capNameLowerCase); - - CapabilityDefinition capabilityDef = createCapabilityFromImportFile(capabilityNameValue - .getValue()); + CapabilityDefinition capabilityDef = createCapabilityFromImportFile(capabilityNameValue.getValue()); capabilityDef.setName(capabilityNameValue.getKey()); if (moduleCapabilities.containsKey(capabilityDef.getType())) { moduleCapabilities.get(capabilityDef.getType()).add(capabilityDef); - } - else { + } else { List list = new ArrayList<>(); list.add(capabilityDef); moduleCapabilities.put(capabilityDef.getType(), list); } - // Validating against req/cap of "derived from" node - Boolean validateVsParentCap = validateCapNameVsDerived(capName2TypeMap, capabilityDef - .getType(), capabilityDef.getName()); - + Boolean validateVsParentCap = validateCapNameVsDerived(capName2TypeMap, capabilityDef.getType(), capabilityDef.getName()); if (!validateVsParentCap) { // Here parentResource is for sure not null, so it's + // null-safe + // Check added to avoid sonar warning String parentResourceName = parentResource != null ? parentResource.getName() : ""; log.debug("Capability with name {} already exists in parent {}", capabilityDef.getName(), parentResourceName); - throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "capability", capabilityDef - .getName() - .toLowerCase(), parentResourceName); + throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "capability", + capabilityDef.getName().toLowerCase(), parentResourceName); } } if (moduleCapabilities.size() > 0) { @@ -647,9 +771,11 @@ public class ResourceImportManager { String nameLowerCase = capDefinition.getName().toLowerCase(); if (capName2type.get(nameLowerCase) != null) { String parentResourceName = parentResource.getName(); - log.debug("Resource with name {} has more than one capability with name {}, ignoring case", parentResourceName, nameLowerCase); - BeEcompErrorManager.getInstance() - .logInternalDataError("Import resource", "Parent resource " + parentResourceName + " of imported resource has one or more capabilities with name " + nameLowerCase, ErrorSeverity.ERROR); + log.debug("Resource with name {} has more than one capability with name {}, ignoring case", parentResourceName, + nameLowerCase); + BeEcompErrorManager.getInstance().logInternalDataError("Import resource", + "Parent resource " + parentResourceName + " of imported resource has one or more capabilities with name " + + nameLowerCase, ErrorSeverity.ERROR); throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); } capName2type.put(nameLowerCase, capDefinition.getType()); @@ -670,9 +796,11 @@ public class ResourceImportManager { String nameLowerCase = reqDefinition.getName().toLowerCase(); if (reqName2type.get(nameLowerCase) != null) { String parentResourceName = parentResource.getName(); - log.debug("Resource with name {} has more than one requirement with name {}, ignoring case", parentResourceName, nameLowerCase); - BeEcompErrorManager.getInstance() - .logInternalDataError("Import resource", "Parent resource " + parentResourceName + " of imported resource has one or more requirements with name " + nameLowerCase, ErrorSeverity.ERROR); + log.debug("Resource with name {} has more than one requirement with name {}, ignoring case", parentResourceName, + nameLowerCase); + BeEcompErrorManager.getInstance().logInternalDataError("Import resource", + "Parent resource " + parentResourceName + " of imported resource has one or more requirements with name " + + nameLowerCase, ErrorSeverity.ERROR); throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); } reqName2type.put(nameLowerCase, reqDefinition.getCapability()); @@ -689,15 +817,15 @@ public class ResourceImportManager { String parentCapType = parentCapName2Type.get(capNameLowerCase); if (parentCapType != null) { if (childCapabilityType.equals(parentCapType)) { - log.debug("Capability with name {} is of same type {} for imported resource and its parent - this is OK", capNameLowerCase, childCapabilityType); + log.debug("Capability with name {} is of same type {} for imported resource and its parent - this is OK", capNameLowerCase, + childCapabilityType); return true; } - Either capabilityTypeDerivedFrom = capabilityTypeOperation.isCapabilityTypeDerivedFrom(childCapabilityType, parentCapType); + Either capabilityTypeDerivedFrom = capabilityTypeOperation + .isCapabilityTypeDerivedFrom(childCapabilityType, parentCapType); if (capabilityTypeDerivedFrom.isRight()) { log.debug("Couldn't check whether imported resource capability derives from its parent's capability"); - throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(capabilityTypeDerivedFrom - .right() - .value())); + throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(capabilityTypeDerivedFrom.right().value())); } return capabilityTypeDerivedFrom.left().value(); } @@ -705,58 +833,43 @@ public class ResourceImportManager { } private CapabilityDefinition createCapabilityFromImportFile(Object capabilityJson) { - CapabilityDefinition capabilityDefinition = new CapabilityDefinition(); - if (capabilityJson instanceof String) { String capabilityJsonString = (String) capabilityJson; capabilityDefinition.setType(capabilityJsonString); - } - else if (capabilityJson instanceof Map) { + } else if (capabilityJson instanceof Map) { Map capabilityJsonMap = (Map) capabilityJson; // Type - if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) { - capabilityDefinition.setType((String) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())); + if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.TYPE.getElementName())) { + capabilityDefinition.setType((String) capabilityJsonMap.get(ToscaTagNamesEnum.TYPE.getElementName())); } // ValidSourceTypes - if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) { - capabilityDefinition.setValidSourceTypes((List) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES - .getElementName())); + if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) { + capabilityDefinition + .setValidSourceTypes((List) capabilityJsonMap.get(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())); } // ValidSourceTypes - if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName())) { - capabilityDefinition.setDescription((String) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName())); + if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.DESCRIPTION.getElementName())) { + capabilityDefinition.setDescription((String) capabilityJsonMap.get(ToscaTagNamesEnum.DESCRIPTION.getElementName())); } - if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) { - List occurrencesList = (List) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName()); + if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) { + List occurrencesList = (List) capabilityJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName()); validateOccurrences(occurrencesList); capabilityDefinition.setMinOccurrences(occurrencesList.get(0).toString()); capabilityDefinition.setMaxOccurrences(occurrencesList.get(1).toString()); } - if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.PROPERTIES.getElementName())) { - + if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.PROPERTIES.getElementName())) { Either, ResultStatusEnum> propertiesRes = ImportUtils.getProperties(capabilityJsonMap); if (propertiesRes.isRight()) { throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND); - } - else { - propertiesRes.left() - .value() - .entrySet() - .stream() - .forEach(e -> e.getValue().setName(e.getKey().toLowerCase())); - List capabilityProperties = propertiesRes.left() - .value() - .values() - .stream() - .map(p -> new ComponentInstanceProperty(p, p - .getDefaultValue(), null)) - .collect(Collectors.toList()); + } else { + propertiesRes.left().value().entrySet().stream().forEach(e -> e.getValue().setName(e.getKey().toLowerCase())); + List capabilityProperties = propertiesRes.left().value().values().stream() + .map(p -> new ComponentInstanceProperty(p, p.getDefaultValue(), null)).collect(Collectors.toList()); capabilityDefinition.setProperties(capabilityProperties); } } - } - else if (!(capabilityJson instanceof List)) { + } else if (!(capabilityJson instanceof List)) { throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML); } return capabilityDefinition; @@ -766,14 +879,13 @@ public class ResourceImportManager { ResponseFormat responseFormat; ComponentException newException; if (e instanceof ComponentException) { - ComponentException componentException = (ComponentException)e; + ComponentException componentException = (ComponentException) e; responseFormat = componentException.getResponseFormat(); if (responseFormat == null) { responseFormat = getResponseFormatManager().getResponseFormat(componentException.getActionStatus(), componentException.getParams()); } newException = componentException; - } - else{ + } else { responseFormat = getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR); newException = new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); } @@ -789,67 +901,95 @@ public class ResourceImportManager { if (isNormative) { version = TypeUtils.getFirstCertifiedVersionVersion(); lifeCycleState = LifecycleStateEnum.CERTIFIED.name(); - } - else { + } else { version = ""; lifeCycleState = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name(); - } - String message = ""; if (errorResponseWrapper.getMessageId() != null) { message = errorResponseWrapper.getMessageId() + ": "; } message += errorResponseWrapper.getFormattedMessage(); - - AuditEventFactory factory = new AuditImportResourceAdminEventFactory( - CommonAuditData.newBuilder() - .status(errorResponseWrapper.getStatus()) - .description(message) - .requestId(ThreadLocalsHolder.getUuid()) - .build(), - new ResourceCommonInfo(resourceMetaData.getName(), ComponentTypeEnum.RESOURCE.getValue()), - ResourceVersionInfo.newBuilder() - .state(lifeCycleState) - .version(version) - .build(), - ResourceVersionInfo.newBuilder() - .state("") - .version("") - .build(), - "", user, ""); + CommonAuditData.newBuilder().status(errorResponseWrapper.getStatus()).description(message).requestId(ThreadLocalsHolder.getUuid()) + .build(), new ResourceCommonInfo(resourceMetaData.getName(), ComponentTypeEnum.RESOURCE.getValue()), + ResourceVersionInfo.newBuilder().state(lifeCycleState).version(version).build(), + ResourceVersionInfo.newBuilder().state("").version("").build(), "", user, ""); getAuditingManager().auditEvent(factory); + } + private void setResourceMetaData(Resource resource, String resourceYml, UploadResourceInfo resourceMetaData) { + Map ymlObj = new Yaml().load(resourceYml); + String toscaName = getToscaResourceName(ymlObj); + final Either latestByToscaName = toscaOperationFacade + .getLatestByToscaResourceName(toscaName, resourceMetaData.getModel()); + if (latestByToscaName.isLeft() && resourceMetaData instanceof DefaultUploadResourceInfo) { + setMetaDataFromLatestResource(resource, latestByToscaName.left().value()); + } else { + setMetaDataFromJson(resourceMetaData, resource); + } } private void setMetaDataFromJson(final UploadResourceInfo resourceMetaData, final Resource resource) { this.populateResourceMetadata(resourceMetaData, resource); resource.setCreatorUserId(resourceMetaData.getContactId()); - final String payloadData = resourceMetaData.getPayloadData(); if (payloadData != null) { resource.setToscaVersion(getToscaVersion(payloadData)); } - final List categories = resourceMetaData.getCategories(); calculateResourceIsAbstract(resource, categories); } - private String getToscaVersion(final String payloadData) { + private void setMetaDataFromLatestResource(Resource resource, Resource latestResource) { + if (resource != null && latestResource != null) { + resource.setCreatorUserId(latestResource.getContactId()); + resource.setDescription(latestResource.getDescription()); + resource.setTags(latestResource.getTags()); + resource.setCategories(latestResource.getCategories()); + resource.setContactId(latestResource.getContactId()); + resource.setName(latestResource.getName()); + resource.setIcon(latestResource.getIcon()); + resource.setResourceVendorModelNumber(latestResource.getResourceVendorModelNumber()); + resource.setResourceType(latestResource.getResourceType()); + if (latestResource.getVendorName() != null) { + resource.setVendorName(latestResource.getVendorName()); + } + if (latestResource.getVendorRelease() != null) { + resource.setVendorRelease(latestResource.getVendorRelease()); + } + if (latestResource.getModel() != null) { + resource.setModel(latestResource.getModel()); + } + if (latestResource.getToscaVersion() != null) { + resource.setToscaVersion(latestResource.getToscaVersion()); + } + final List categories = latestResource.getCategories(); + calculateResourceIsAbstract(resource, categories); + } + } + + private Map decodePayload(final String payloadData) { final String decodedPayload = new String(Base64.decodeBase64(payloadData)); - final Map mappedToscaTemplate = (Map) new Yaml().load(decodedPayload); - final Either findFirstToscaStringElement = - ImportUtils.findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION); - return findFirstToscaStringElement.left().value(); + return (Map) new Yaml().load(decodedPayload); + } + + private String getToscaVersion(final String payloadData) { + final Map mappedToscaTemplate = decodePayload(payloadData); + final Either findFirstToscaStringElement = ImportUtils + .findFirstToscaStringElement(mappedToscaTemplate, ToscaTagNamesEnum.TOSCA_VERSION); + if (findFirstToscaStringElement.isLeft()) { + return findFirstToscaStringElement.left().value(); + } else { + return null; + } } private void calculateResourceIsAbstract(Resource resource, List categories) { if (categories != null && !categories.isEmpty()) { CategoryDefinition categoryDef = categories.get(0); resource.setAbstract(false); - if (categoryDef != null && categoryDef.getName() != null && categoryDef.getName() - .equals(Constants.ABSTRACT_CATEGORY_NAME)) { + if (categoryDef != null && categoryDef.getName() != null && categoryDef.getName().equals(Constants.ABSTRACT_CATEGORY_NAME)) { SubCategoryDefinition subCategoryDef = categoryDef.getSubcategories().get(0); if (subCategoryDef != null && subCategoryDef.getName().equals(Constants.ABSTRACT_SUBCATEGORY)) { resource.setAbstract(true); @@ -864,8 +1004,7 @@ public class ResourceImportManager { if (shouldBeCertified) { version = TypeUtils.getFirstCertifiedVersionVersion(); state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE; - } - else { + } else { version = ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION; state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT; } @@ -874,16 +1013,13 @@ public class ResourceImportManager { resource.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION); resource.setVendorName(ImportUtils.Constants.VENDOR_NAME); resource.setVendorRelease(ImportUtils.Constants.VENDOR_RELEASE); - } private void validateOccurrences(List occurrensesList) { - if (!ValidationUtils.validateListNotEmpty(occurrensesList)) { log.debug("Occurrenses list empty"); throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES); } - if (occurrensesList.size() < 2) { log.debug("Occurrenses list size not 2"); throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES); @@ -894,8 +1030,7 @@ public class ResourceImportManager { Integer maxOccurrences; if (minObj instanceof Integer) { minOccurrences = (Integer) minObj; - } - else { + } else { log.debug("Invalid occurrenses format. low_bound occurrense must be Integer {}", minObj); throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES); } @@ -903,23 +1038,19 @@ public class ResourceImportManager { log.debug("Invalid occurrenses format.low_bound occurrense negative {}", minOccurrences); throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES); } - - if (maxObj instanceof String){ - if(!"UNBOUNDED".equals(maxObj)) { + if (maxObj instanceof String) { + if (!"UNBOUNDED".equals(maxObj)) { log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj); throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES); } - } - else { + } else { if (maxObj instanceof Integer) { maxOccurrences = (Integer) maxObj; - } - else { + } else { log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj); throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES); } - - if (maxOccurrences <= 0 || maxOccurrences < minOccurrences) { + if (maxOccurrences < 0 || maxOccurrences < minOccurrences) { log.debug("Invalid occurrenses format. min occurrence is {}, Max occurrence is {}", minOccurrences, maxOccurrences); throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES); } @@ -939,7 +1070,8 @@ public class ResourceImportManager { } private ResourceBusinessLogic getResourceBL(ServletContext context) { - WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(org.openecomp.sdc.common.api.Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR); + WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context + .getAttribute(org.openecomp.sdc.common.api.Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR); WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context); return webApplicationContext.getBean(ResourceBusinessLogic.class); } @@ -948,10 +1080,19 @@ public class ResourceImportManager { return servletContext; } + public void setServletContext(ServletContext servletContext) { + this.servletContext = servletContext; + } + public AuditingManager getAuditingManager() { return auditingManager; } + @Autowired + public void setAuditingManager(AuditingManager auditingManager) { + this.auditingManager = auditingManager; + } + public ResponseFormatManager getResponseFormatManager() { return responseFormatManager; } @@ -968,11 +1109,6 @@ public class ResourceImportManager { public void setResourceBusinessLogic(ResourceBusinessLogic resourceBusinessLogic) { this.resourceBusinessLogic = resourceBusinessLogic; } - - @Autowired - public void setInterfaceOperationBusinessLogic(InterfaceOperationBusinessLogic interfaceOperationBusinessLogic) { - this.interfaceOperationBusinessLogic = interfaceOperationBusinessLogic; - } public IGraphLockOperation getGraphLockOperation() { return graphLockOperation; @@ -983,14 +1119,28 @@ public class ResourceImportManager { this.graphLockOperation = graphLockOperation; } - public void setServletContext(ServletContext servletContext) { - this.servletContext = servletContext; - } - - @Autowired - public void setAuditingManager(AuditingManager auditingManager) { - this.auditingManager = auditingManager; + private List extractDataTypeFromJson(final ResourceBusinessLogic resourceBusinessLogic, + final Map foundElements, + final String model) { + final List dataTypeDefinitionList = new ArrayList<>(); + if (MapUtils.isNotEmpty(foundElements)) { + final Either, JanusGraphOperationStatus> dataTypeCacheAll = + resourceBusinessLogic.applicationDataTypeCache.getAll(model); + if (dataTypeCacheAll.isLeft()) { + for (final Entry attributeNameValue : foundElements.entrySet()) { + final Object value = attributeNameValue.getValue(); + if (value instanceof Map) { + final DataTypeDefinition dataTypeDefinition = createDataTypeDefinitionWithName(attributeNameValue); + final DataTypeDefinition dataTypeDefinitionParent = dataTypeCacheAll.left().value() + .get(dataTypeDefinition.getDerivedFromName()); + dataTypeDefinition.setDerivedFrom(dataTypeDefinitionParent); + dataTypeDefinitionList.add(dataTypeDefinition); + } else { + dataTypeDefinitionList.add(createDataType(String.valueOf(value))); + } + } + } + } + return dataTypeDefinitionList; } - - }