package org.openecomp.sdc.be.model.jsontitan.operations; import java.util.*; import java.util.Map.Entry; import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.dao.jsongraph.TitanDao; import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; import org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition; 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.ComponentInstanceDataDefinition; import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition; import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty; import org.openecomp.sdc.be.datatypes.elements.MapListCapabiltyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition; import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition; 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.model.ArtifactDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; 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.DistributionStatusEnum; import org.openecomp.sdc.be.model.GroupDefinition; import org.openecomp.sdc.be.model.GroupInstance; import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.LifecycleStateEnum; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; import org.openecomp.sdc.be.model.RequirementDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate; import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement; import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum; import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.resources.data.ComponentMetadataData; import org.openecomp.sdc.be.utils.CommonBeUtils; import org.openecomp.sdc.common.jsongraph.util.CommonUtility; import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum; import org.openecomp.sdc.common.util.ValidationUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import fj.data.Either; @org.springframework.stereotype.Component("tosca-operation-facade") public class ToscaOperationFacade { @Autowired private NodeTypeOperation nodeTypeOperation; @Autowired private TopologyTemplateOperation topologyTemplateOperation; @Autowired private NodeTemplateOperation nodeTemplateOperation; @Autowired private GroupsOperation groupsOperation; @Autowired private TitanDao titanDao; private static Logger log = LoggerFactory.getLogger(ToscaOperationFacade.class.getName()); public Either getToscaElement(String componentId) { return getToscaElement(componentId, JsonParseFlagEnum.ParseAll); } public Either getToscaFullElement(String componentId) { ComponentParametersView filters = new ComponentParametersView(); filters.setIgnoreCapabiltyProperties(false); return getToscaElement(componentId, filters); } public Either getToscaElement(String componentId, ComponentParametersView filters) { Either getVertexEither = titanDao.getVertexById(componentId, filters.detectParseFlag()); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value())); } return getToscaElementByOperation(getVertexEither.left().value(), filters); } public Either getToscaElement(String componentId, JsonParseFlagEnum parseFlag) { Either getVertexEither = titanDao.getVertexById(componentId, parseFlag); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value())); } return getToscaElementByOperation(getVertexEither.left().value()); } public Either getToscaElement(GraphVertex componentVertex) { return getToscaElementByOperation(componentVertex); } public Either validateComponentExists(String componentId) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { TitanOperationStatus status = getVertexEither.right().value(); if (status == TitanOperationStatus.NOT_FOUND) { return Either.left(false); } else { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value())); } } return Either.left(true); } public Either findLastCertifiedToscaElementByUUID(T component) { Map props = new HashMap<>(); props.put(GraphPropertyEnum.UUID, component.getUUID()); props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); Either, TitanOperationStatus> getVertexEither = titanDao.getByCriteria(ModelConverter.getVertexType(component), props); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", component.getUniqueId(), getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value())); } return getToscaElementByOperation(getVertexEither.left().value().get(0)); } private Either getToscaElementByOperation(GraphVertex componentV) { return getToscaElementByOperation(componentV, new ComponentParametersView()); } private Either getToscaElementByOperation(GraphVertex componentV, ComponentParametersView filters) { VertexTypeEnum label = componentV.getLabel(); ToscaElementOperation toscaOperation = getToscaElementOperation(componentV); Either toscaElement; String componentId = componentV.getUniqueId(); if (toscaOperation != null) { log.debug("Need to fetch tosca element for id {}", componentId); toscaElement = toscaOperation.getToscaElement(componentV, filters); } else { log.debug("not supported tosca type {} for id {}", label, componentId); toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST); } if (toscaElement.isRight()) { return Either.right(toscaElement.right().value()); } return Either.left(ModelConverter.convertFromToscaElement(toscaElement.left().value())); } private ToscaElementOperation getToscaElementOperation(GraphVertex componentV) { VertexTypeEnum label = componentV.getLabel(); switch (label) { case NODE_TYPE: return nodeTypeOperation; case TOPOLOGY_TEMPLATE: return topologyTemplateOperation; default: return null; } } /** * * @param resource * @return */ public Either createToscaComponent(T resource) { ToscaElement toscaElement = ModelConverter.convertToToscaElement(resource); ToscaElementOperation toscaElementOperation = getToscaElementOperation(toscaElement); Either createToscaElement = toscaElementOperation.createToscaElement(toscaElement); if (createToscaElement.isLeft()) { log.debug("Component created successfully!!!"); T dataModel = ModelConverter.convertFromToscaElement(createToscaElement.left().value()); return Either.left(dataModel); } return Either.right(createToscaElement.right().value()); } /** * * @param componentToDelete * @return */ public StorageOperationStatus markComponentToDelete(Component componentToDelete) { if ((componentToDelete.getIsDeleted() != null) && componentToDelete.getIsDeleted() && !componentToDelete.isHighestVersion()) { // component already marked for delete return StorageOperationStatus.OK; } else { Either getResponse = titanDao.getVertexById(componentToDelete.getUniqueId(), JsonParseFlagEnum.ParseAll); if (getResponse.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentToDelete.getUniqueId(), getResponse.right().value()); return DaoStatusConverter.convertTitanStatusToStorageStatus(getResponse.right().value()); } GraphVertex componentV = getResponse.left().value(); // same operation for node type and topology template operations Either result = nodeTypeOperation.markComponentToDelete(componentV); if (result.isRight()) { return result.right().value(); } return StorageOperationStatus.OK; } } /** * * @param componentId * @return */ public Either deleteToscaComponent(String componentId) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component vertex with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value())); } Either deleteElement = deleteToscaElement(getVertexEither.left().value()); if (deleteElement.isRight()) { log.debug("Failed to delete component with and unique id {}, error: {}", componentId, deleteElement.right().value()); return Either.right(deleteElement.right().value()); } T dataModel = ModelConverter.convertFromToscaElement(deleteElement.left().value()); return Either.left(dataModel); } private Either deleteToscaElement(GraphVertex componentV) { VertexTypeEnum label = componentV.getLabel(); Either toscaElement; Object componentId = componentV.getUniqueId(); switch (label) { case NODE_TYPE: log.debug("Need to fetch node type for id {}", componentId); toscaElement = nodeTypeOperation.deleteToscaElement(componentV); break; case TOPOLOGY_TEMPLATE: log.debug("Need to fetch topology template for id {}", componentId); toscaElement = topologyTemplateOperation.deleteToscaElement(componentV); break; default: log.debug("not supported tosca type {} for id {}", label, componentId); toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST); break; } return toscaElement; } private ToscaElementOperation getToscaElementOperation(ToscaElement toscaElement) { ComponentTypeEnum componentType = toscaElement.getComponentType(); switch (componentType) { case RESOURCE: ResourceTypeEnum resourceType = toscaElement.getResourceType(); if (resourceType == ResourceTypeEnum.VF) { return topologyTemplateOperation; } else { return nodeTypeOperation; } case SERVICE: case PRODUCT: return topologyTemplateOperation; default: break; } return null; } private ToscaElementOperation getToscaElementOperation(Component component) { switch (component.getComponentType()) { case RESOURCE: ResourceTypeEnum resourceType = ((Resource) component).getResourceType(); if (resourceType != null && resourceType == ResourceTypeEnum.VF) { return topologyTemplateOperation; } else { return nodeTypeOperation; } case SERVICE: case PRODUCT: return topologyTemplateOperation; default: break; } return null; } public Either getLatestByToscaResourceName(String toscaResourceName) { return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); } public Either getLatestByName(String resourceName) { return getLatestByName(GraphPropertyEnum.NAME, resourceName); } public Either validateCsarUuidUniqueness(String csarUUID) { Either, StorageOperationStatus> byCsar = null; Map properties = new HashMap(); properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID); Either, TitanOperationStatus> resources = titanDao.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata); if (resources.isRight()) { if (resources.right().value() == TitanOperationStatus.NOT_FOUND) { return Either.left(new Integer(0)); } else { log.debug("failed to get resources from graph with property name: {}", csarUUID); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value())); } } List resourceList = (resources.isLeft() ? resources.left().value() : null); return Either.left(new Integer(resourceList.size())); } public Either, StorageOperationStatus> getFollowed(String userId, Set lifecycleStates, Set lastStateStates, ComponentTypeEnum componentType) { Either, StorageOperationStatus> followedResources; if (componentType == ComponentTypeEnum.RESOURCE) { followedResources = nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType); } else { followedResources = topologyTemplateOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType); } Set components = new HashSet<>(); if (followedResources.isRight() && followedResources.right().value() != StorageOperationStatus.NOT_FOUND) { return Either.right(followedResources.right().value()); } if (followedResources.isLeft()) { List toscaElements = followedResources.left().value(); toscaElements.forEach(te -> { T component = ModelConverter.convertFromToscaElement(te); components.add(component); }); } return Either.left(components); } public Either getLatestCertifiedNodeTypeByToscaResourceName(String toscaResourceName) { return getLatestCertifiedByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata); } public Either getLatestCertifiedByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag) { Either result = null; Map props = new HashMap(); props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); Either, TitanOperationStatus> getLatestRes = titanDao.getByCriteria(vertexType, props, parseFlag); if (getLatestRes.isRight()) { TitanOperationStatus status = getLatestRes.right().value(); CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status); result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); } if (result == null) { List resources = getLatestRes.left().value(); double version = 0.0; GraphVertex highestResource = null; for (GraphVertex resource : resources) { double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION)); if (resourceVersion > version) { version = resourceVersion; highestResource = resource; } } result = getToscaElement(highestResource.getUniqueId()); } return result; } public Either validateToscaResourceNameExists(String templateName) { Either validateUniquenessRes = validateToscaResourceNameUniqueness(templateName); if (validateUniquenessRes.isLeft()) { return Either.left(!validateUniquenessRes.left().value()); } return validateUniquenessRes; } public Either dissociateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) { return nodeTemplateOperation.dissociateResourceInstances(componentId, requirementDef); } public StorageOperationStatus associateResourceInstances(String componentId, List relations) { Either, StorageOperationStatus> status = nodeTemplateOperation.associateResourceInstances(componentId, relations); if (status.isRight()) { return status.right().value(); } return StorageOperationStatus.OK; } protected Either validateToscaResourceNameUniqueness(String name) { Map properties = new HashMap(); properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, name); Either, TitanOperationStatus> resources = titanDao.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata); if (resources.isRight() && resources.right().value() != TitanOperationStatus.NOT_FOUND) { log.debug("failed to get resources from graph with property name: {}", name); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value())); } List resourceList = (resources.isLeft() ? resources.left().value() : null); if (resourceList != null && resourceList.size() > 0) { if (log.isDebugEnabled()) { StringBuilder builder = new StringBuilder(); for (GraphVertex resourceData : resourceList) { builder.append(resourceData.getUniqueId() + "|"); } log.debug("resources with property name:{} exists in graph. found {}", name, builder.toString()); } return Either.left(false); } else { log.debug("resources with property name:{} does not exists in graph", name); return Either.left(true); } } /** * * @param newComponent * @param oldComponent * @return */ public Either overrideComponent(T newComponent, T oldComponent) { // TODO // newComponent.setInterfaces(oldComponent.getInterfaces); newComponent.setArtifacts(oldComponent.getArtifacts()); newComponent.setDeploymentArtifacts(oldComponent.getDeploymentArtifacts()); newComponent.setGroups(oldComponent.getGroups()); newComponent.setInputs(null); newComponent.setLastUpdateDate(null); newComponent.setHighestVersion(true); Either componentVEither = titanDao.getVertexById(oldComponent.getUniqueId(), JsonParseFlagEnum.NoParse); if (componentVEither.isRight()) { log.debug("Falied to fetch component {} error {}", oldComponent.getUniqueId(), componentVEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(componentVEither.right().value())); } GraphVertex componentv = componentVEither.left().value(); Either parentVertexEither = titanDao.getParentVertex(componentv, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse); if (parentVertexEither.isRight() && parentVertexEither.right().value() != TitanOperationStatus.NOT_FOUND) { log.debug("Falied to fetch parent version for component {} error {}", oldComponent.getUniqueId(), parentVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(parentVertexEither.right().value())); } Either deleteToscaComponent = deleteToscaElement(componentv); if (deleteToscaComponent.isRight()) { log.debug("Falied to remove old component {} error {}", oldComponent.getUniqueId(), deleteToscaComponent.right().value()); return Either.right(deleteToscaComponent.right().value()); } Either createToscaComponent = createToscaComponent(newComponent); if (createToscaComponent.isRight()) { log.debug("Falied to create tosca element component {} error {}", newComponent.getUniqueId(), createToscaComponent.right().value()); return Either.right(createToscaComponent.right().value()); } T newElement = createToscaComponent.left().value(); Either newVersionEither = titanDao.getVertexById(newElement.getUniqueId(), JsonParseFlagEnum.NoParse); if (newVersionEither.isRight()) { log.debug("Falied to fetch new tosca element component {} error {}", newComponent.getUniqueId(), newVersionEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(newVersionEither.right().value())); } if (parentVertexEither.isLeft()) { GraphVertex previousVersionV = parentVertexEither.left().value(); TitanOperationStatus createEdge = titanDao.createEdge(previousVersionV, newVersionEither.left().value(), EdgeLabelEnum.VERSION, null); if (createEdge != TitanOperationStatus.OK) { log.debug("Falied to associate to previous version {} new version {} error {}", previousVersionV.getUniqueId(), newVersionEither.right().value(), createEdge); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge)); } } return Either.left(newElement); } /** * * @param componentToUpdate * @return */ public Either updateToscaElement(T componentToUpdate) { return updateToscaElement(componentToUpdate, new ComponentParametersView()); } /** * * @param componentToUpdate * @param type * @param filterResult * @return */ public Either updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) { String componentId = componentToUpdate.getUniqueId(); Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value())); } GraphVertex elementV = getVertexEither.left().value(); ToscaElementOperation toscaElementOperation = getToscaElementOperation(elementV); ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(componentToUpdate); Either updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult); if (updateToscaElement.isRight()) { log.debug("Failed to update tosca element {} error {}", componentId, updateToscaElement.right().value()); return Either.right(updateToscaElement.right().value()); } return Either.left(ModelConverter.convertFromToscaElement(updateToscaElement.left().value())); } private Either getLatestByName(GraphPropertyEnum property, String nodeName) { Either result; Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(property, nodeName); propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); Either, TitanOperationStatus> highestResources = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata); if (highestResources.isRight()) { TitanOperationStatus status = highestResources.right().value(); log.debug("failed to find resource with name {}. status={} ", nodeName, status); result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); return result; } List resources = highestResources.left().value(); double version = 0.0; GraphVertex highestResource = null; for (GraphVertex vertex : resources) { Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION); double resourceVersion = Double.valueOf((String) versionObj); if (resourceVersion > version) { version = resourceVersion; highestResource = vertex; } } return getToscaElementByOperation(highestResource); } public Either, StorageOperationStatus> getBySystemName(ComponentTypeEnum componentType, String systemName) { Either, StorageOperationStatus> result = null; Either getComponentRes; List components = new ArrayList<>(); List componentVertices; Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName); if (componentType != null) propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); Either, TitanOperationStatus> getComponentsRes = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); if (getComponentsRes.isRight()) { TitanOperationStatus status = getComponentsRes.right().value(); log.debug("Failed to fetch the component with system name {}. Status is {} ", systemName, status); result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); } if (result == null) { componentVertices = getComponentsRes.left().value(); for (GraphVertex componentVertex : componentVertices) { getComponentRes = getToscaElementByOperation(componentVertex); if (getComponentRes.isRight()) { log.debug("Failed to get the component {}. Status is {} ", componentVertex.getJsonMetadataField(JsonPresentationFields.NAME), getComponentRes.right().value()); result = Either.right(getComponentRes.right().value()); break; } T componentBySystemName = getComponentRes.left().value(); log.debug("Found component, id: {}", componentBySystemName.getUniqueId()); components.add(componentBySystemName); } } if (result == null) { result = Either.left(components); } return result; } public Either getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version) { return getComponentByNameAndVersion(componentType, name, version, JsonParseFlagEnum.ParseAll); } public Either getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version, JsonParseFlagEnum parseFlag) { Either result; Map hasProperties = new EnumMap<>(GraphPropertyEnum.class); Map hasNotProperties = new EnumMap<>(GraphPropertyEnum.class); hasProperties.put(GraphPropertyEnum.NAME, name); hasProperties.put(GraphPropertyEnum.VERSION, version); hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true); if (componentType != null) { hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); } Either, TitanOperationStatus> getResourceRes = titanDao.getByCriteria(null, hasProperties, hasNotProperties, parseFlag); if (getResourceRes.isRight()) { TitanOperationStatus status = getResourceRes.right().value(); log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status); result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); return result; } return getToscaElementByOperation(getResourceRes.left().value().get(0)); } public Either, StorageOperationStatus> getCatalogComponents(ComponentTypeEnum componentType, boolean isHighestVersions) { List components = new ArrayList<>(); Either, StorageOperationStatus> catalogDataResult; List toscaElements; switch (componentType) { case RESOURCE: catalogDataResult = nodeTypeOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, ToscaElementTypeEnum.NodeType, isHighestVersions); if (catalogDataResult.isRight()) { return Either.right(catalogDataResult.right().value()); } toscaElements = catalogDataResult.left().value(); Either, StorageOperationStatus> resourceCatalogData = topologyTemplateOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, ToscaElementTypeEnum.TopologyTemplate, isHighestVersions); if (resourceCatalogData.isRight()) { return Either.right(resourceCatalogData.right().value()); } toscaElements.addAll(resourceCatalogData.left().value()); break; case SERVICE: catalogDataResult = topologyTemplateOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, ToscaElementTypeEnum.TopologyTemplate, isHighestVersions); if (catalogDataResult.isRight()) { return Either.right(catalogDataResult.right().value()); } toscaElements = catalogDataResult.left().value(); break; default: log.debug("Not supported component type {}", componentType); return Either.right(StorageOperationStatus.BAD_REQUEST); } toscaElements.forEach(te -> { T component = ModelConverter.convertFromToscaElement(te); components.add(component); }); return Either.left(components); } public Either, StorageOperationStatus> deleteMarkedElements(ComponentTypeEnum componentType) { Either, StorageOperationStatus> allComponentsMarkedForDeletion; List deleted = new ArrayList<>(); switch (componentType) { case RESOURCE: allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType); break; case SERVICE: case PRODUCT: allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType); break; default: log.debug("Not supported component type {}", componentType); return Either.right(StorageOperationStatus.BAD_REQUEST); } if (allComponentsMarkedForDeletion.isRight()) { return Either.right(allComponentsMarkedForDeletion.right().value()); } List allMarked = allComponentsMarkedForDeletion.left().value(); Either, TitanOperationStatus> allNotDeletedElements = topologyTemplateOperation.getAllNotDeletedElements(); if (allNotDeletedElements.isRight()) { return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(allNotDeletedElements.right().value())); } List allNonMarked = allNotDeletedElements.left().value(); for (GraphVertex elementV : allMarked) { if (topologyTemplateOperation.isInUse(elementV, allNonMarked) == false) { Either deleteToscaElement = deleteToscaElement(elementV); if (deleteToscaElement.isRight()) { log.debug("Failed to delete marked element {} error {}", elementV.getUniqueId(), deleteToscaElement.right().value()); } } else { deleted.add(elementV.getUniqueId()); log.debug("Marked element {} in use. don't delete it", elementV.getUniqueId()); } } return Either.left(deleted); } public Either, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) { Either, StorageOperationStatus> allComponentsMarkedForDeletion; switch (componentType) { case RESOURCE: allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType); break; case SERVICE: case PRODUCT: allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType); break; default: log.debug("Not supported component type {}", componentType); return Either.right(StorageOperationStatus.BAD_REQUEST); } if (allComponentsMarkedForDeletion.isRight()) { return Either.right(allComponentsMarkedForDeletion.right().value()); } return Either.left(allComponentsMarkedForDeletion.left().value().stream().map(v -> v.getUniqueId()).collect(Collectors.toList())); } public Either isComponentInUse(String componentId) { Either result; Either, TitanOperationStatus> allNotDeletedElements = topologyTemplateOperation.getAllNotDeletedElements(); if (allNotDeletedElements.isRight()) { result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(allNotDeletedElements.right().value())); } else { result = Either.left(topologyTemplateOperation.isInUse(componentId, allNotDeletedElements.left().value())); } return result; } public Either, StorageOperationStatus> addComponentInstanceToTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance, boolean allowDeleted, User user) { Either, StorageOperationStatus> result = null; Either updateContainerComponentRes = null; componentInstance.setIcon(origComponent.getIcon()); Either, StorageOperationStatus> addResult = nodeTemplateOperation.addComponentInstanceToTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), ModelConverter.convertToToscaElement(origComponent), getNextComponentInstanceCounter(containerComponent, origComponent.getName()), componentInstance, allowDeleted, user); if (addResult.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the component instance {} to container component {}. ", componentInstance.getName(), containerComponent.getName()); result = Either.right(addResult.right().value()); } if (result == null) { updateContainerComponentRes = topologyTemplateOperation.getToscaElement(containerComponent.getUniqueId()); if (updateContainerComponentRes.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch updated topology template {} with updated component instance {}. ", containerComponent.getName(), componentInstance.getName()); result = Either.right(updateContainerComponentRes.right().value()); } } if (result == null) { Component updatedComponent = ModelConverter.convertFromToscaElement(updateContainerComponentRes.left().value()); String createdInstanceId = addResult.left().value().getRight(); CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been added to container component {}. ", createdInstanceId, updatedComponent.getName()); result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId)); } return result; } public StorageOperationStatus associateComponentInstancesToComponent(Component containerComponent, Map resourcesInstancesMap, boolean allowDeleted) { StorageOperationStatus result = null; CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Going to add component instances to component {}", containerComponent.getUniqueId()); Either metadataVertex = titanDao.getVertexById(containerComponent.getUniqueId(), JsonParseFlagEnum.ParseAll); if (metadataVertex.isRight()) { TitanOperationStatus status = metadataVertex.right().value(); if (status == TitanOperationStatus.NOT_FOUND) { status = TitanOperationStatus.INVALID_ID; } result = DaoStatusConverter.convertTitanStatusToStorageStatus(status); } if (result == null) { result = nodeTemplateOperation.associateComponentInstancesToComponent(containerComponent, resourcesInstancesMap, metadataVertex.left().value(), allowDeleted); } return result; } public Either, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance) { Either, StorageOperationStatus> result = null; CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName()); componentInstance.setIcon(origComponent.getIcon()); Either, StorageOperationStatus> updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), ModelConverter.convertToToscaElement(origComponent), componentInstance); if (updateResult.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName()); result = Either.right(updateResult.right().value()); } if (result == null) { Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft()); String createdInstanceId = updateResult.left().value().getRight(); CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata of the component instance {} has been updated to container component {}. ", createdInstanceId, updatedComponent.getName()); result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId)); } return result; } public Either updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent) { Either result = null; CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata belonging to container component {}. ", containerComponent.getName()); Either updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent)); if (updateResult.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata belonging to container component {}. ", containerComponent.getName()); result = Either.right(updateResult.right().value()); } if (result == null) { Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value()); CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata has been updated to container component {}. ", updatedComponent.getName()); result = Either.left(updatedComponent); } return result; } public Either, StorageOperationStatus> deleteComponentInstanceFromTopologyTemplate(Component containerComponent, String resourceInstanceId) { Either, StorageOperationStatus> result = null; CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName()); Either, StorageOperationStatus> updateResult = nodeTemplateOperation.deleteComponentInstanceFromTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), resourceInstanceId); if (updateResult.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName()); result = Either.right(updateResult.right().value()); } if (result == null) { Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft()); String deletedInstanceId = updateResult.left().value().getRight(); CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been deleted from container component {}. ", deletedInstanceId, updatedComponent.getName()); result = Either.left(new ImmutablePair<>(updatedComponent, deletedInstanceId)); } return result; } private String getNextComponentInstanceCounter(Component containerComponent, String originResourceName) { Integer nextCounter = 0; if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) { String normalizedName = ValidationUtils.normalizeComponentInstanceName(originResourceName); Integer maxCounterFromNames = getMaxCounterFromNames(containerComponent, normalizedName); Integer maxCounterFromIds = getMaxCounterFromIds(containerComponent, normalizedName); if (maxCounterFromNames == null && maxCounterFromIds != null) { nextCounter = maxCounterFromIds + 1; } else if (maxCounterFromIds == null && maxCounterFromNames != null) { nextCounter = maxCounterFromNames + 1; } else if (maxCounterFromIds != null && maxCounterFromNames != null) { nextCounter = maxCounterFromNames > maxCounterFromIds ? maxCounterFromNames + 1 : maxCounterFromIds + 1; } } return nextCounter.toString(); } private Integer getMaxCounterFromNames(Component containerComponent, String normalizedName) { Integer maxCounter = 0; List countersStr = containerComponent.getComponentInstances().stream().filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName)).map(ci -> ci.getNormalizedName().split(normalizedName)[1]) .collect(Collectors.toList()); if (CollectionUtils.isEmpty(countersStr)) { return null; } Integer currCounter = null; for (String counter : countersStr) { if (StringUtils.isEmpty(counter)) { continue; } try { currCounter = Integer.parseInt(counter); } catch (Exception e) { continue; } maxCounter = maxCounter < currCounter ? currCounter : maxCounter; } if (currCounter == null) { return null; } return maxCounter; } private Integer getMaxCounterFromIds(Component containerComponent, String normalizedName) { Integer maxCounter = 0; List countersStr = containerComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName)).map(ci -> ci.getUniqueId().split(normalizedName)[1]) .collect(Collectors.toList()); if (CollectionUtils.isEmpty(countersStr)) { return null; } Integer currCounter = null; for (String counter : countersStr) { if (StringUtils.isEmpty(counter)) { continue; } try { currCounter = Integer.parseInt(counter); } catch (Exception e) { continue; } maxCounter = maxCounter < currCounter ? currCounter : maxCounter; } if (currCounter == null) { return null; } return maxCounter; } public Either associateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) { return nodeTemplateOperation.associateResourceInstances(componentId, requirementDef); } public Either, StorageOperationStatus> createAndAssociateInputs(Map inputs, String componentId) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value())); } GraphVertex vertex = getVertexEither.left().value(); Map inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue()))); StorageOperationStatus status = topologyTemplateOperation.associateInputsToComponent(vertex, inputsMap, componentId); if (StorageOperationStatus.OK == status) { log.debug("Component created successfully!!!"); List inputsResList = null; if (inputsMap != null && !inputsMap.isEmpty()) { inputsResList = inputsMap.values().stream().map(i -> new InputDefinition(i)).collect(Collectors.toList()); } return Either.left(inputsResList); } return Either.right(status); } public Either, StorageOperationStatus> addInputsToComponent(Map inputs, String componentId) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value())); } GraphVertex vertex = getVertexEither.left().value(); Map inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue()))); StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsMap, JsonPresentationFields.NAME); if (StorageOperationStatus.OK == status) { log.debug("Component created successfully!!!"); List inputsResList = null; if (inputsMap != null && !inputsMap.isEmpty()) { inputsResList = inputsMap.values().stream().map(i -> new InputDefinition(i)).collect(Collectors.toList()); } return Either.left(inputsResList); } return Either.right(status); } public Either>, StorageOperationStatus> associateComponentInstancePropertiesToComponent(Map> instProperties, String componentId) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value())); } GraphVertex vertex = getVertexEither.left().value(); Map instPropsMap = new HashMap<>(); if (instProperties != null) { MapPropertiesDataDefinition propertiesMap; for (Entry> entry : instProperties.entrySet()) { propertiesMap = new MapPropertiesDataDefinition(); propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e))); instPropsMap.put(entry.getKey(), propertiesMap); } } StorageOperationStatus status = topologyTemplateOperation.associateInstPropertiesToComponent(vertex, instPropsMap); if (StorageOperationStatus.OK == status) { log.debug("Component created successfully!!!"); return Either.left(instProperties); } return Either.right(status); } public Either>, StorageOperationStatus> addComponentInstanceInputsToComponent(Component containerComponent, Map> instProperties) { StorageOperationStatus status = StorageOperationStatus.OK; if (instProperties != null) { for (Entry> entry : instProperties.entrySet()) { List props = entry.getValue(); String componentInstanseId = entry.getKey(); if (props != null && !props.isEmpty()) { for (ComponentInstanceInput property : props) { List componentInstancesInputs = containerComponent.getComponentInstancesInputs().get(componentInstanseId); Optional instanceProperty = componentInstancesInputs.stream().filter(p -> p.getName().equals(property.getName())).findAny(); if (instanceProperty.isPresent()) { status = updateComponentInstanceInput(containerComponent, componentInstanseId, property); } else { status = addComponentInstanceInput(containerComponent, componentInstanseId, property); } if (status != StorageOperationStatus.OK) { log.debug("Failed to update instance input {} for instance {} error {} ", property, componentInstanseId, status); return Either.right(status); } else { log.trace("instance input {} for instance {} updated", property, componentInstanseId); } } } } } return Either.left(instProperties); } public StorageOperationStatus deleteComponentInstanceInputsToComponent(Map> instProperties, String componentId) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()); } GraphVertex vertex = getVertexEither.left().value(); Map instPropsMap = new HashMap<>(); if (instProperties != null) { MapPropertiesDataDefinition propertiesMap; for (Entry> entry : instProperties.entrySet()) { propertiesMap = new MapPropertiesDataDefinition(); propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e))); instPropsMap.put(entry.getKey(), propertiesMap); } } return topologyTemplateOperation.deleteInstInputsToComponent(vertex, instPropsMap); } public Either>, StorageOperationStatus> addComponentInstancePropertiesToComponent(Component containerComponent, Map> instProperties, String componentId) { StorageOperationStatus status = StorageOperationStatus.OK; if (instProperties != null) { for (Entry> entry : instProperties.entrySet()) { List props = entry.getValue(); String componentInstanseId = entry.getKey(); List instanceProperties = containerComponent.getComponentInstancesProperties().get(componentInstanseId); if (props != null && !props.isEmpty()) { for (ComponentInstanceProperty property : props) { Optional instanceProperty = instanceProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny(); if (instanceProperty.isPresent()) { status = updateComponentInstanceProperty(containerComponent, componentInstanseId, property); } else { status = addComponentInstanceProperty(containerComponent, componentInstanseId, property); } } } } } return Either.left(instProperties); } public StorageOperationStatus associateArtifactToInstances(Map> instArtifacts, String componentId, User user) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()); } GraphVertex vertex = getVertexEither.left().value(); Map instArtMap = new HashMap<>(); if (instArtifacts != null) { MapArtifactDataDefinition artifactsMap; for (Entry> entry : instArtifacts.entrySet()) { Map artList = entry.getValue(); Map artifacts = artList.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); artifactsMap = nodeTemplateOperation.prepareInstDeploymentArtifactPerInstance(artifacts, entry.getKey(), user, NodeTemplateOperation.HEAT_VF_ENV_NAME); instArtMap.put(entry.getKey(), artifactsMap); } } return topologyTemplateOperation.associateInstArtifactToComponent(vertex, instArtMap); } public StorageOperationStatus associateInstAttributeToComponentToInstances(Map> instArttributes, String componentId) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()); } GraphVertex vertex = getVertexEither.left().value(); Map instAttr = new HashMap<>(); if (instArttributes != null) { MapPropertiesDataDefinition attributesMap; for (Entry> entry : instArttributes.entrySet()) { attributesMap = new MapPropertiesDataDefinition(); attributesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e))); instAttr.put(entry.getKey(), attributesMap); } } return topologyTemplateOperation.associateInstAttributeToComponent(vertex, instAttr); } public StorageOperationStatus associateCalculatedCapReq(Map>> instCapabilties, Map>> instReg, String componentId) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()); } GraphVertex vertex = getVertexEither.left().value(); Map calcRequirements = new HashMap<>(); Map calcCapabilty = new HashMap<>(); Map calculatedCapabilitiesProperties = new HashMap<>(); ; if (instCapabilties != null) { for (Entry>> entry : instCapabilties.entrySet()) { Map> caps = entry.getValue(); Map mapToscaDataDefinition = new HashMap<>(); for (Entry> instCapability : caps.entrySet()) { mapToscaDataDefinition.put(instCapability.getKey(), new ListCapabilityDataDefinition(instCapability.getValue().stream().map(iCap -> new CapabilityDataDefinition(iCap)).collect(Collectors.toList()))); } ComponentInstanceDataDefinition componentInstance = new ComponentInstanceDataDefinition(entry.getKey()); MapListCapabiltyDataDefinition capMap = nodeTemplateOperation.prepareCalculatedCapabiltyForNodeType(mapToscaDataDefinition, componentInstance); MapCapabiltyProperty mapCapabiltyProperty = ModelConverter.convertToMapOfMapCapabiltyProperties(caps, componentInstance.getUniqueId(), true); calcCapabilty.put(entry.getKey().getUniqueId(), capMap); calculatedCapabilitiesProperties.put(entry.getKey().getUniqueId(), mapCapabiltyProperty); } } if (instReg != null) { for (Entry>> entry : instReg.entrySet()) { Map> req = entry.getValue(); Map mapToscaDataDefinition = new HashMap<>(); for (Entry> instReq : req.entrySet()) { mapToscaDataDefinition.put(instReq.getKey(), new ListRequirementDataDefinition(instReq.getValue().stream().map(iCap -> new RequirementDataDefinition(iCap)).collect(Collectors.toList()))); } MapListRequirementDataDefinition capMap = nodeTemplateOperation.prepareCalculatedRequirementForNodeType(mapToscaDataDefinition, new ComponentInstanceDataDefinition(entry.getKey())); calcRequirements.put(entry.getKey().getUniqueId(), capMap); } } StorageOperationStatus status = topologyTemplateOperation.associateCalcCapReqToComponent(vertex, calcRequirements, calcCapabilty, calculatedCapabilitiesProperties); return status; } private Either, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, VertexTypeEnum vertexType) { Map hasProps = new EnumMap<>(GraphPropertyEnum.class); Map hasNotProps = new EnumMap<>(GraphPropertyEnum.class); fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType); Either, TitanOperationStatus> getRes = titanDao.getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata); if (getRes.isRight()) { if (getRes.right().value().equals(TitanOperationStatus.NOT_FOUND)) { return Either.left(new ArrayList<>()); } else { return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value())); } } else { List nonAbstractLatestComponents = new ArrayList<>(); ComponentParametersView params = new ComponentParametersView(true); params.setIgnoreAllVersions(false); for (GraphVertex vertexComponent : getRes.left().value()) { Either componentRes = topologyTemplateOperation.getLightComponent(vertexComponent, componentTypeEnum, params); if (componentRes.isRight()) { log.debug("Failed to fetch ligth element for {} error {}", vertexComponent.getUniqueId(), componentRes.right().value()); return Either.right(componentRes.right().value()); } else { Component component = ModelConverter.convertFromToscaElement(componentRes.left().value()); nonAbstractLatestComponents.add(component); } } return Either.left(nonAbstractLatestComponents); } } public Either getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag, Boolean isHighest) { Either result; Map hasProperties = new EnumMap<>(GraphPropertyEnum.class); hasProperties.put(GraphPropertyEnum.UUID, componentUuid); if (isHighest != null) { hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest.booleanValue()); } Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); Either, TitanOperationStatus> getRes = titanDao.getByCriteria(null, hasProperties, propertiesNotToMatch, parseFlag); if (getRes.isRight()) { result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value())); } else { List latestVersionList = getRes.left().value().stream().map(ModelConverter::convertToComponentMetadata).collect(Collectors.toList()); ComponentMetadataData latestVersion = latestVersionList.size() == 1 ? latestVersionList.get(0) : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getMetadataDataDefinition().getVersion()), Double.parseDouble(c2.getMetadataDataDefinition().getVersion()))).get(); result = Either.left(latestVersion); } return result; } public Either getComponentMetadata(String componentId) { Either result; Either getRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata); if (getRes.isRight()) { result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value())); } else { ComponentMetadataData componentMetadata = ModelConverter.convertToComponentMetadata(getRes.left().value()); result = Either.left(componentMetadata); } return result; } private Map findLatestVersion(List resourceDataList) { Map, ComponentMetadataData> latestVersionMap = new HashMap, ComponentMetadataData>(); for (ComponentMetadataData resourceData : resourceDataList) { ComponentMetadataData latestVersionData = resourceData; ComponentMetadataDataDefinition metadataDataDefinition = resourceData.getMetadataDataDefinition(); Pair pair = createKeyPair(latestVersionData); if (latestVersionMap.containsKey(pair)) { latestVersionData = latestVersionMap.get(pair); String currentVersion = latestVersionData.getMetadataDataDefinition().getVersion(); String newVersion = metadataDataDefinition.getVersion(); if (CommonBeUtils.compareAsdcComponentVersions(newVersion, currentVersion)) { latestVersionData = resourceData; } } if (log.isDebugEnabled()) log.debug("last certified version of resource = {} version is {}", latestVersionData.getMetadataDataDefinition().getName(), latestVersionData.getMetadataDataDefinition().getVersion()); latestVersionMap.put(pair, latestVersionData); } Map resVersionMap = new HashMap(); for (ComponentMetadataData resourceData : latestVersionMap.values()) { ComponentMetadataData latestVersionData = resourceData; ComponentMetadataDataDefinition metadataDataDefinition = resourceData.getMetadataDataDefinition(); if (resVersionMap.containsKey(metadataDataDefinition.getUUID())) { latestVersionData = resVersionMap.get(metadataDataDefinition.getUUID()); String currentVersion = latestVersionData.getMetadataDataDefinition().getVersion(); String newVersion = metadataDataDefinition.getVersion(); if (CommonBeUtils.compareAsdcComponentVersions(newVersion, currentVersion)) { latestVersionData = resourceData; } } if (log.isDebugEnabled()) log.debug("last uuid version of resource = {} version is {}", latestVersionData.getMetadataDataDefinition().getName(), latestVersionData.getMetadataDataDefinition().getVersion()); resVersionMap.put(latestVersionData.getMetadataDataDefinition().getUUID(), latestVersionData); } return resVersionMap; } private Pair createKeyPair(ComponentMetadataData metadataData) { Pair pair; NodeTypeEnum label = NodeTypeEnum.getByName(metadataData.getLabel()); switch (label) { case Resource: pair = new ImmutablePair<>(metadataData.getMetadataDataDefinition().getName(), ((ResourceMetadataDataDefinition) metadataData.getMetadataDataDefinition()).getResourceType().name()); break; default: pair = new ImmutablePair<>(metadataData.getMetadataDataDefinition().getName(), metadataData.getLabel()); break; } return pair; } public Either, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, List componentUids) { Either, StorageOperationStatus> result = null; List components = new ArrayList<>(); if (componentUids == null) { Either, StorageOperationStatus> componentUidsRes = getComponentUids(isAbstract, isHighest, componentTypeEnum, internalComponentType, componentUids); if (componentUidsRes.isRight()) { result = Either.right(componentUidsRes.right().value()); } else { componentUids = componentUidsRes.left().value(); } } if (!componentUids.isEmpty()) { for (String componentUid : componentUids) { ComponentParametersView componentParametersView = buildComponentViewForNotAbstract(); if (internalComponentType != null && "vl".equalsIgnoreCase(internalComponentType)) { componentParametersView.setIgnoreCapabilities(false); componentParametersView.setIgnoreRequirements(false); } Either getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum).getLightComponent(componentUid, componentTypeEnum, componentParametersView); if (getToscaElementRes.isRight()) { if (log.isDebugEnabled()) log.debug("Failed to fetch resource for error is {}", getToscaElementRes.right().value()); result = Either.right(getToscaElementRes.right().value()); break; } Component component = ModelConverter.convertFromToscaElement(getToscaElementRes.left().value()); component.setContactId(null); component.setCreationDate(null); component.setCreatorUserId(null); component.setCreatorFullName(null); component.setLastUpdateDate(null); component.setLastUpdaterUserId(null); component.setLastUpdaterFullName(null); component.setNormalizedName(null); components.add(component); } } if (result == null) { result = Either.left(components); } return result; } private Either, StorageOperationStatus> getComponentUids(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, List componentUids) { Either, StorageOperationStatus> result = null; Either, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, isHighest, componentTypeEnum, internalComponentType); if (getToscaElementsRes.isRight()) { result = Either.right(getToscaElementsRes.right().value()); } else { List collection = getToscaElementsRes.left().value(); if (collection == null) { componentUids = new ArrayList<>(); } else { componentUids = collection.stream().map(p -> p.getUniqueId()).collect(Collectors.toList()); } } if (result == null) { result = Either.left(componentUids); } return result; } private ComponentParametersView buildComponentViewForNotAbstract() { ComponentParametersView componentParametersView = new ComponentParametersView(); componentParametersView.disableAll(); componentParametersView.setIgnoreCategories(false); componentParametersView.setIgnoreAllVersions(false); return componentParametersView; } public Either validateComponentNameExists(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) { Either result = validateComponentNameUniqueness(name, resourceType, componentType); if (result.isLeft()) { result = Either.left(!result.left().value()); } return result; } public Either validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) { VertexTypeEnum vertexType = getVertexTypeByComponentAndResourceTypeEnum(resourceType, componentType); String normalizedName = ValidationUtils.normaliseComponentName(name); Map properties = new EnumMap<>(GraphPropertyEnum.class); properties.put(GraphPropertyEnum.NORMALIZED_NAME, normalizedName); properties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); Either, TitanOperationStatus> vertexEither = titanDao.getByCriteria(vertexType, properties, JsonParseFlagEnum.NoParse); if (vertexEither.isRight() && vertexEither.right().value() != TitanOperationStatus.NOT_FOUND) { log.debug("failed to get vertex from graph with property normalizedName: {}", normalizedName); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value())); } List vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null; if (vertexList != null && !vertexList.isEmpty()) { return Either.left(false); } else { return Either.left(true); } } // UI query parameter is either VFC/CP/VL(for yaml upload/update) or VF (for CSAR upload/user composed VF) // TODO implementation of topology template VFCs may require updating this method - depending on UI implementation private VertexTypeEnum getVertexTypeByComponentAndResourceTypeEnum(ResourceTypeEnum resourceType, ComponentTypeEnum componentType) { VertexTypeEnum vertexType = VertexTypeEnum.TOPOLOGY_TEMPLATE; if (ComponentTypeEnum.RESOURCE == componentType && ResourceTypeEnum.VF != resourceType) { vertexType = VertexTypeEnum.NODE_TYPE; } return vertexType; } private void fillNodeTypePropsMap(Map hasProps, Map hasNotProps, String internalComponentType) { switch (internalComponentType.toLowerCase()) { case "vf": hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name()); hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VFCMT.name()); break; case "service": hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VFC.name()); hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VFCMT.name()); break; case "vl": hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VL.name()); break; default: break; } } private void fillTopologyTemplatePropsMap(Map hasProps, ComponentTypeEnum componentTypeEnum) { switch (componentTypeEnum) { case RESOURCE: hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); break; case SERVICE: hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); break; default: break; } } private void fillPropsMap(Map hasProps, Map hasNotProps, String internalComponentType, ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType) { hasNotProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name()); hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); if (VertexTypeEnum.NODE_TYPE == internalVertexType) { hasProps.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract); if (internalComponentType != null) { fillNodeTypePropsMap(hasProps, hasNotProps, internalComponentType); } } else { fillTopologyTemplatePropsMap(hasProps, componentTypeEnum); } } private List getInternalVertexTypes(ComponentTypeEnum componentTypeEnum, String internalComponentType) { List internalVertexTypes = new ArrayList<>(); if (ComponentTypeEnum.RESOURCE == componentTypeEnum) { internalVertexTypes.add(VertexTypeEnum.NODE_TYPE); } if (ComponentTypeEnum.SERVICE == componentTypeEnum || "service".equalsIgnoreCase(internalComponentType)) { internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE); } return internalVertexTypes; } public Either, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType) { List internalVertexTypes = getInternalVertexTypes(componentTypeEnum, internalComponentType); List result = new ArrayList<>(); for (VertexTypeEnum vertexType : internalVertexTypes) { Either, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract, isHighest, componentTypeEnum, internalComponentType, vertexType); if (listByVertexType.isRight()) { return listByVertexType; } result.addAll(listByVertexType.left().value()); } return Either.left(result); } public Either, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid) { Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); Either, StorageOperationStatus> componentListByUuid = getComponentListByUuid(componentUuid, propertiesToMatch); return componentListByUuid; } public Either, StorageOperationStatus> getComponentListByUuid(String componentUuid, Map additionalPropertiesToMatch) { Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); if (additionalPropertiesToMatch != null) { propertiesToMatch.putAll(additionalPropertiesToMatch); } propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid); Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); Either, TitanOperationStatus> vertexEither = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); if (vertexEither.isRight()) { log.debug("Couldn't fetch metadata for component with type {} and uuid {}, error: {}", componentUuid, vertexEither.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value())); } List vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null; if (vertexList == null || vertexList.isEmpty()) { log.debug("Component with uuid {} was not found", componentUuid); return Either.right(StorageOperationStatus.NOT_FOUND); } ArrayList latestComponents = new ArrayList<>(); for (GraphVertex vertex : vertexList) { Either toscaElementByOperation = getToscaElementByOperation(vertex); if(toscaElementByOperation.isRight()){ log.debug("Could not fetch the following Component by UUID {}", vertex.getUniqueId()); return Either.right(toscaElementByOperation.right().value()); } latestComponents.add(toscaElementByOperation.left().value()); } if(latestComponents.size() > 1) { for (Component component : latestComponents) { if(component.isHighestVersion()){ LinkedList highestComponent = new LinkedList<>(); highestComponent.add(component); return Either.left(highestComponent); } } } return Either.left(latestComponents); } public Either getLatestComponentByUuid(String componentUuid) { Either, StorageOperationStatus> latestVersionListEither = getLatestComponentListByUuid(componentUuid); if (latestVersionListEither.isRight()) { return Either.right(latestVersionListEither.right().value()); } List latestVersionList = latestVersionListEither.left().value(); if (latestVersionList.isEmpty()) { return Either.right(StorageOperationStatus.NOT_FOUND); } Component component = latestVersionList.size() == 1 ? latestVersionList.get(0) : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getVersion()), Double.parseDouble(c2.getVersion()))).get(); return Either.left(component); } public Either, StorageOperationStatus> getAllCertifiedResources(boolean isAbstract, Boolean isHighest) { List resources = new ArrayList<>(); Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract); if (isHighest != null) { propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest.booleanValue()); } propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); Either, TitanOperationStatus> getResourcesRes = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); if (getResourcesRes.isRight()) { log.debug("Failed to fetch all certified resources. Status is {}", getResourcesRes.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getResourcesRes.right().value())); } List resourceVerticies = getResourcesRes.left().value(); for (GraphVertex resourceV : resourceVerticies) { Either getResourceRes = getToscaElement(resourceV); if (getResourceRes.isRight()) { return Either.right(getResourceRes.right().value()); } resources.add(getResourceRes.left().value()); } return Either.left(resources); } public Either getLatestByNameAndVersion(String name, String version, JsonParseFlagEnum parseFlag) { Either result; Map hasProperties = new EnumMap<>(GraphPropertyEnum.class); Map hasNotProperties = new EnumMap<>(GraphPropertyEnum.class); hasProperties.put(GraphPropertyEnum.NAME, name); hasProperties.put(GraphPropertyEnum.VERSION, version); hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true); Either, TitanOperationStatus> getResourceRes = titanDao.getByCriteria(null, hasProperties, hasNotProperties, parseFlag); if (getResourceRes.isRight()) { TitanOperationStatus status = getResourceRes.right().value(); log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status); result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); return result; } return getToscaElementByOperation(getResourceRes.left().value().get(0)); } public Either getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName) { return getLatestComponentByCsarOrName(componentType, csarUUID, systemName, false, JsonParseFlagEnum.ParseAll); } public Either getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName, boolean allowDeleted, JsonParseFlagEnum parseFlag) { Map props = new EnumMap<>(GraphPropertyEnum.class); props.put(GraphPropertyEnum.CSAR_UUID, csarUUID); props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); if (componentType != null) { props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); } Map propsHasNot = new EnumMap<>(GraphPropertyEnum.class); propsHasNot.put(GraphPropertyEnum.IS_DELETED, true); GraphVertex resourceMetadataData = null; List resourceMetadataDataList = null; Either, TitanOperationStatus> byCsar = titanDao.getByCriteria(null, props, propsHasNot, JsonParseFlagEnum.ParseMetadata); if (byCsar.isRight()) { if (TitanOperationStatus.NOT_FOUND == byCsar.right().value()) { // Fix Defect DE256036 if (StringUtils.isEmpty(systemName)) { return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.NOT_FOUND)); } props.clear(); props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); props.put(GraphPropertyEnum.SYSTEM_NAME, systemName); Either, TitanOperationStatus> bySystemname = titanDao.getByCriteria(null, props, JsonParseFlagEnum.ParseMetadata); if (bySystemname.isRight()) { log.debug("getLatestResourceByCsarOrName - Failed to find by system name {} error {} ", systemName, bySystemname.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(bySystemname.right().value())); } if (bySystemname.left().value().size() > 2) { log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) must return only 2 latest version, but was returned - {}", bySystemname.left().value().size()); return Either.right(StorageOperationStatus.GENERAL_ERROR); } resourceMetadataDataList = bySystemname.left().value(); if (resourceMetadataDataList.size() == 1) { resourceMetadataData = resourceMetadataDataList.get(0); } else { for (GraphVertex curResource : resourceMetadataDataList) { if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) { resourceMetadataData = curResource; break; } } } if (resourceMetadataData == null) { log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) returned 2 latest CERTIFIED versions"); return Either.right(StorageOperationStatus.GENERAL_ERROR); } if (resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID) != null && !((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID)).equals(csarUUID)) { log.debug("getLatestResourceByCsarOrName - same system name {} but different csarUUID. exist {} and new {} ", systemName, resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID), csarUUID); // correct error will be returned from create flow. with all // correct audit records!!!!! return Either.right(StorageOperationStatus.NOT_FOUND); } Either resource = getToscaElement((String) resourceMetadataData.getUniqueId()); return resource; } } else { resourceMetadataDataList = byCsar.left().value(); if (resourceMetadataDataList.size() > 2) { log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) must return only 2 latest version, but was returned - {}", byCsar.left().value().size()); return Either.right(StorageOperationStatus.GENERAL_ERROR); } if (resourceMetadataDataList.size() == 1) { resourceMetadataData = resourceMetadataDataList.get(0); } else { for (GraphVertex curResource : resourceMetadataDataList) { if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) { resourceMetadataData = curResource; break; } } } if (resourceMetadataData == null) { log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) returned 2 latest CERTIFIED versions"); return Either.right(StorageOperationStatus.GENERAL_ERROR); } Either resource = getToscaElement((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID), parseFlag); return resource; } return null; } public Either validateToscaResourceNameExtends(String templateNameCurrent, String templateNameExtends) { String currentTemplateNameChecked = templateNameExtends; while (currentTemplateNameChecked != null && !currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) { Either latestByToscaResourceName = getLatestByToscaResourceName(currentTemplateNameChecked); if (latestByToscaResourceName.isRight()) { return latestByToscaResourceName.right().value() == StorageOperationStatus.NOT_FOUND ? Either.left(false) : Either.right(latestByToscaResourceName.right().value()); } Resource value = latestByToscaResourceName.left().value(); if (value.getDerivedFrom() != null) { currentTemplateNameChecked = value.getDerivedFrom().get(0); } else { currentTemplateNameChecked = null; } } return (currentTemplateNameChecked != null && currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) ? Either.left(true) : Either.left(false); } public Either, StorageOperationStatus> fetchByResourceType(String resourceType) { Map props = new EnumMap<>(GraphPropertyEnum.class); props.put(GraphPropertyEnum.RESOURCE_TYPE, resourceType); props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); Either, TitanOperationStatus> resourcesByTypeEither = titanDao.getByCriteria(null, props); if (resourcesByTypeEither.isRight()) { return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resourcesByTypeEither.right().value())); } List vertexList = resourcesByTypeEither.left().value(); List components = new ArrayList<>(); for (GraphVertex vertex : vertexList) { components.add(getToscaElementByOperation(vertex).left().value()); } return Either.left(components); } public void commit() { titanDao.commit(); } public Either updateDistributionStatus(Service service, User user, DistributionStatusEnum distributionStatus) { Either updateDistributionStatus = topologyTemplateOperation.updateDistributionStatus(service.getUniqueId(), user, distributionStatus); if (updateDistributionStatus.isRight()) { return Either.right(updateDistributionStatus.right().value()); } GraphVertex serviceV = updateDistributionStatus.left().value(); service.setDistributionStatus(distributionStatus); service.setLastUpdateDate((Long) serviceV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE)); return Either.left(service); } public Either updateComponentLastUpdateDateOnGraph(Component component, Long modificationTime) { Either result = null; GraphVertex serviceVertex; Either updateRes = null; Either getRes = titanDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.ParseMetadata); if (getRes.isRight()) { TitanOperationStatus status = getRes.right().value(); log.error("Failed to fetch component {}. status is {}", component.getUniqueId(), status); result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); } if (result == null) { serviceVertex = getRes.left().value(); long lastUpdateDate = System.currentTimeMillis(); serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate); component.setLastUpdateDate(lastUpdateDate); updateRes = titanDao.updateVertex(serviceVertex); if (updateRes.isRight()) { result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value())); } } if (result == null) { result = Either.left(ModelConverter.convertToComponentMetadata(updateRes.left().value())); } return result; } public TitanDao getTitanDao() { return titanDao; } public Either, StorageOperationStatus> getCertifiedServicesWithDistStatus(Set distStatus) { Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); return getServicesWithDistStatus(distStatus, propertiesToMatch); } public Either, StorageOperationStatus> getServicesWithDistStatus(Set distStatus, Map additionalPropertiesToMatch) { List servicesAll = new ArrayList<>(); Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); if (additionalPropertiesToMatch != null && !additionalPropertiesToMatch.isEmpty()) { propertiesToMatch.putAll(additionalPropertiesToMatch); } propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); if (distStatus != null && !distStatus.isEmpty()) { for (DistributionStatusEnum state : distStatus) { propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name()); Either, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch); if (fetchServicesByCriteria.isRight()) { return fetchServicesByCriteria; } else { servicesAll = fetchServicesByCriteria.left().value(); } } return Either.left(servicesAll); } else { return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch); } } // private Either, StorageOperationStatus> fetchServicesByCriteria(List servicesAll, Map propertiesToMatch, Map propertiesNotToMatch) { // Either, TitanOperationStatus> getRes = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); // if (getRes.isRight()) { // if (getRes.right().value() != TitanOperationStatus.NOT_FOUND) { // CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch, propertiesNotToMatch, getRes.right().value()); // return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value())); // } // } else { // for (GraphVertex vertex : getRes.left().value()) { // Either getServiceRes = getToscaElementByOperation(vertex); // if (getServiceRes.isRight()) { // CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ", vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value()); // return Either.right(getServiceRes.right().value()); // } else { // servicesAll.add((Service) getToscaElementByOperation(vertex).left().value()); // } // } // } // return Either.left(servicesAll); // } private Either, StorageOperationStatus> fetchServicesByCriteria(List servicesAll, Map propertiesToMatch, Map propertiesNotToMatch) { Either, TitanOperationStatus> getRes = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); if (getRes.isRight()) { if (getRes.right().value() != TitanOperationStatus.NOT_FOUND) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch, propertiesNotToMatch, getRes.right().value()); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value())); } } else { for (GraphVertex vertex : getRes.left().value()) { // Either getServiceRes = getToscaElementByOperation(vertex); Either getServiceRes = topologyTemplateOperation.getLightComponent(vertex, ComponentTypeEnum.SERVICE, new ComponentParametersView(true)); if (getServiceRes.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ", vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value()); return Either.right(getServiceRes.right().value()); } else { servicesAll.add(ModelConverter.convertFromToscaElement(getServiceRes.left().value())); } } } return Either.left(servicesAll); } public void rollback() { titanDao.rollback(); } public StorageOperationStatus addDeploymentArtifactsToInstance(String componentId, ComponentInstance componentInstance, Map finalDeploymentArtifacts) { Map instDeplArtifacts = finalDeploymentArtifacts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); return nodeTemplateOperation.addDeploymentArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts); } public StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId) { return nodeTemplateOperation.generateCustomizationUUIDOnInstance(componentId, instanceId); } public StorageOperationStatus generateCustomizationUUIDOnInstanceGroup(String componentId, String instanceId, List groupInstances) { return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances); } public Either addPropertyToResource(String propertyName, PropertyDefinition newPropertyDefinition, Resource resource) { Either result = null; Either getUpdatedComponentRes = null; newPropertyDefinition.setName(propertyName); newPropertyDefinition.setParentUniqueId(resource.getUniqueId()); StorageOperationStatus status = getToscaElementOperation(resource).addToscaDataToToscaElement(resource.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME); if (status != StorageOperationStatus.OK) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", propertyName, resource.getName(), status); result = Either.right(status); } if (result == null) { ComponentParametersView filter = new ComponentParametersView(true); filter.setIgnoreProperties(false); getUpdatedComponentRes = getToscaElement(resource.getUniqueId(), filter); if (getUpdatedComponentRes.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", resource.getUniqueId(), getUpdatedComponentRes.right().value()); result = Either.right(status); } } if (result == null) { PropertyDefinition newProperty = null; List properties = ((Resource) getUpdatedComponentRes.left().value()).getProperties(); if (CollectionUtils.isNotEmpty(properties)) { Optional newPropertyOptional = properties.stream().filter(p -> p.getName().equals(propertyName)).findAny(); if (newPropertyOptional.isPresent()) { newProperty = newPropertyOptional.get(); } } if (newProperty == null) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", propertyName, resource.getUniqueId(), StorageOperationStatus.NOT_FOUND); result = Either.right(StorageOperationStatus.NOT_FOUND); } else { result = Either.left(newProperty); } } return result; } public StorageOperationStatus deletePropertyOfResource(Resource resource, String propertyName) { return getToscaElementOperation(resource).deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, propertyName, JsonPresentationFields.NAME); } public StorageOperationStatus deleteAttributeOfResource(Component component, String attributeName) { return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, attributeName, JsonPresentationFields.NAME); } public StorageOperationStatus deleteInputOfResource(Component resource, String inputName) { return getToscaElementOperation(resource).deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputName, JsonPresentationFields.NAME); } public Either updatePropertyOfResource(Resource resource, PropertyDefinition newPropertyDefinition) { Either getUpdatedComponentRes = null; Either result = null; StorageOperationStatus status = getToscaElementOperation(resource).updateToscaDataOfToscaElement(resource.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME); if (status != StorageOperationStatus.OK) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newPropertyDefinition.getName(), resource.getName(), status); result = Either.right(status); } if (result == null) { ComponentParametersView filter = new ComponentParametersView(true); filter.setIgnoreProperties(false); getUpdatedComponentRes = getToscaElement(resource.getUniqueId(), filter); if (getUpdatedComponentRes.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", resource.getUniqueId(), getUpdatedComponentRes.right().value()); result = Either.right(status); } } if (result == null) { Optional newProperty = ((Resource) getUpdatedComponentRes.left().value()).getProperties().stream().filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny(); if (newProperty.isPresent()) { result = Either.left(newProperty.get()); } else { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newPropertyDefinition.getName(), resource.getUniqueId(), StorageOperationStatus.NOT_FOUND); result = Either.right(StorageOperationStatus.NOT_FOUND); } } return result; } public Either addAttributeOfResource(Component component, PropertyDefinition newAttributeDef) { Either getUpdatedComponentRes = null; Either result = null; if (newAttributeDef.getUniqueId() == null || newAttributeDef.getUniqueId().isEmpty()) { String attUniqueId = UniqueIdBuilder.buildAttributeUid(component.getUniqueId(), newAttributeDef.getName()); newAttributeDef.setUniqueId(attUniqueId); } StorageOperationStatus status = getToscaElementOperation(component).addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME); if (status != StorageOperationStatus.OK) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newAttributeDef.getName(), component.getName(), status); result = Either.right(status); } if (result == null) { ComponentParametersView filter = new ComponentParametersView(true); filter.setIgnoreAttributesFrom(false); getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter); if (getUpdatedComponentRes.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value()); result = Either.right(status); } } if (result == null) { Optional newAttribute = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny(); if (newAttribute.isPresent()) { result = Either.left(newAttribute.get()); } else { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND); result = Either.right(StorageOperationStatus.NOT_FOUND); } } return result; } public Either updateAttributeOfResource(Component component, PropertyDefinition newAttributeDef) { Either getUpdatedComponentRes = null; Either result = null; StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME); if (status != StorageOperationStatus.OK) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newAttributeDef.getName(), component.getName(), status); result = Either.right(status); } if (result == null) { ComponentParametersView filter = new ComponentParametersView(true); filter.setIgnoreAttributesFrom(false); getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter); if (getUpdatedComponentRes.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value()); result = Either.right(status); } } if (result == null) { Optional newProperty = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny(); if (newProperty.isPresent()) { result = Either.left(newProperty.get()); } else { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND); result = Either.right(StorageOperationStatus.NOT_FOUND); } } return result; } public Either updateInputOfComponent(Component component, InputDefinition newInputDefinition) { Either getUpdatedComponentRes = null; Either result = null; StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition, JsonPresentationFields.NAME); if (status != StorageOperationStatus.OK) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the input {} to the component {}. Status is {}. ", newInputDefinition.getName(), component.getName(), status); result = Either.right(status); } if (result == null) { ComponentParametersView filter = new ComponentParametersView(true); filter.setIgnoreInputs(false); getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter); if (getUpdatedComponentRes.isRight()) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value()); result = Either.right(status); } } if (result == null) { Optional updatedInput = getUpdatedComponentRes.left().value().getInputs().stream().filter(p -> p.getName().equals(newInputDefinition.getName())).findAny(); if (updatedInput.isPresent()) { result = Either.left(updatedInput.get()); } else { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently updated inputs {} on the resource {}. Status is {}. ", newInputDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND); result = Either.right(StorageOperationStatus.NOT_FOUND); } } return result; } public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List groups, Map> groupInstancesArtifacts) { return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groups, groupInstancesArtifacts); } public Either, StorageOperationStatus> updateGroupsOnComponent(Component component, ComponentTypeEnum componentType, List updatedGroups) { return groupsOperation.updateGroups(component, componentType, updatedGroups); } public Either, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, ComponentTypeEnum componentType, String instanceId, List updatedGroupInstances) { return groupsOperation.updateGroupInstances(component, componentType, instanceId, updatedGroupInstances); } public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List groupInstances) { return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groupInstances); } public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance, Map deploymentArtifacts) { return nodeTemplateOperation.addDeploymentArtifactsToComponentInstance(containerComponent, componentInstance, deploymentArtifacts); } public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) { return nodeTemplateOperation.updateComponentInstanceProperty(containerComponent, componentInstanceId, property); } public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) { return nodeTemplateOperation.addComponentInstanceProperty(containerComponent, componentInstanceId, property); } public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) { return nodeTemplateOperation.updateComponentInstanceInput(containerComponent, componentInstanceId, property); } public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) { return nodeTemplateOperation.addComponentInstanceInput(containerComponent, componentInstanceId, property); } public void setNodeTypeOperation(NodeTypeOperation nodeTypeOperation) { this.nodeTypeOperation = nodeTypeOperation; } public void setTopologyTemplateOperation(TopologyTemplateOperation topologyTemplateOperation) { this.topologyTemplateOperation = topologyTemplateOperation; } }