X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-model%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fmodel%2Fjsonjanusgraph%2Foperations%2FToscaOperationFacade.java;h=951dc5aaed1c874f27a7bda34f8dcf347d4e44c5;hb=c469756a1092194adedd590d35f0f1f8feac3a36;hp=0d39bb264f1b997a947ac0a32aadb59cdc5a6513;hpb=15fabcb9618c6acd6fa604e17777df4d494eca1b;p=sdc.git diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java index 0d39bb264f..951dc5aaed 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java @@ -17,64 +17,148 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.be.model.jsonjanusgraph.operations; +import com.vdurmont.semver4j.Semver; +import com.vdurmont.semver4j.Semver.SemverType; import fj.data.Either; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Edge; +import org.janusgraph.graphdb.query.JanusGraphPredicate; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.dao.api.exception.JanusGraphException; +import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.jsongraph.HealingJanusGraphDao; 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.datatypes.elements.*; +import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; +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.MapAttributesDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.MapCapabilityProperty; import org.openecomp.sdc.be.datatypes.elements.MapInterfaceDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.*; -import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.datatypes.elements.MapListCapabilityDataDefinition; +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.OriginTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.AttributeDefinition; +import org.openecomp.sdc.be.model.CapabilityDefinition; +import org.openecomp.sdc.be.model.CatalogUpdateTimestamp; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceAttribute; +import org.openecomp.sdc.be.model.ComponentInstanceInput; +import org.openecomp.sdc.be.model.ComponentInstanceInterface; +import org.openecomp.sdc.be.model.ComponentInstanceOutput; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.DistributionStatusEnum; +import org.openecomp.sdc.be.model.GroupDefinition; +import org.openecomp.sdc.be.model.GroupInstance; +import org.openecomp.sdc.be.model.InputDefinition; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.OutputDefinition; +import org.openecomp.sdc.be.model.PolicyDefinition; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.RelationshipInfo; +import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; +import org.openecomp.sdc.be.model.RequirementDefinition; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.catalog.CatalogComponent; +import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData; import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate; import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ToscaOperationExceptionSupplier; import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.StorageException; +import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; 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.model.utils.GroupUtils; import org.openecomp.sdc.be.resources.data.ComponentMetadataData; import org.openecomp.sdc.common.jsongraph.util.CommonUtility; import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.common.util.ValidationUtils; import org.springframework.beans.factory.annotation.Autowired; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.TreeSet; import java.util.function.BiPredicate; import java.util.stream.Collectors; import static java.util.Objects.requireNonNull; import static org.apache.commons.collections.CollectionUtils.isEmpty; import static org.apache.commons.collections.CollectionUtils.isNotEmpty; - +import static org.janusgraph.core.attribute.Text.REGEX; +import static org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum.TOPOLOGY_TEMPLATE; @org.springframework.stereotype.Component("tosca-operation-facade") public class ToscaOperationFacade { + // region - ToscaElement - GetById + public static final String PROXY_SUFFIX = "_proxy"; // region - Fields - private static final String COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch a component with and UniqueId {}, error: {}"; private static final String FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS = "Failed to find recently added property {} on the resource {}. Status is {}. "; private static final String FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS = "Failed to get updated resource {}. Status is {}. "; private static final String FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS = "Failed to add the property {} to the resource {}. Status is {}. "; private static final String SERVICE = "service"; + private static final String VF = "VF"; private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}"; private static final String COMPONENT_CREATED_SUCCESSFULLY = "Component created successfully!!!"; + private static final String INPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY = "Inputs associated to component successfully!"; + private static final String OUTPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY = "Outputs associated to component successfully!"; private static final String COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch component with and unique id {}, error: {}"; + private static final Logger log = Logger.getLogger(ToscaOperationFacade.class.getName()); + @Autowired + private IGraphLockOperation graphLockOperation; @Autowired private NodeTypeOperation nodeTypeOperation; @Autowired @@ -85,46 +169,49 @@ public class ToscaOperationFacade { private GroupsOperation groupsOperation; @Autowired private HealingJanusGraphDao janusGraphDao; - - private static final Logger log = Logger.getLogger(ToscaOperationFacade.class.getName()); // endregion + @Autowired + private ContainerInstanceTypesData containerInstanceTypesData; - // region - ToscaElement - GetById - public static final String PROXY_SUFFIX = "_proxy"; + private static Optional getPropertyCapability(String propertyParentUniqueId, Component containerComponent) { + Map> componentCapabilities = containerComponent.getCapabilities(); + if (MapUtils.isEmpty(componentCapabilities)) { + return Optional.empty(); + } + List capabilityDefinitionList = componentCapabilities.values().stream().flatMap(Collection::stream) + .collect(Collectors.toList()); + if (CollectionUtils.isEmpty(capabilityDefinitionList)) { + return Optional.empty(); + } + return capabilityDefinitionList.stream().filter(capabilityDefinition -> capabilityDefinition.getUniqueId().equals(propertyParentUniqueId)) + .findAny(); + } public Either getToscaFullElement(String componentId) { ComponentParametersView filters = new ComponentParametersView(); filters.setIgnoreCapabiltyProperties(false); - filters.setIgnoreForwardingPath(false); + filters.setIgnoreServicePath(false); return getToscaElement(componentId, filters); } public Either getToscaElement(String componentId) { - return getToscaElement(componentId, JsonParseFlagEnum.ParseAll); - } public Either getToscaElement(String componentId, ComponentParametersView filters) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, filters.detectParseFlag()); + Either getVertexEither = janusGraphDao.getVertexById(componentId, filters.detectParseFlag()); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - } return getToscaElementByOperation(getVertexEither.left().value(), filters); } public Either getToscaElement(String componentId, JsonParseFlagEnum parseFlag) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, parseFlag); + Either getVertexEither = janusGraphDao.getVertexById(componentId, parseFlag); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - } return getToscaElementByOperation(getVertexEither.left().value()); } @@ -134,9 +221,7 @@ public class ToscaOperationFacade { } public Either validateComponentExists(String componentId) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { JanusGraphOperationStatus status = getVertexEither.right().value(); if (status == JanusGraphOperationStatus.NOT_FOUND) { @@ -148,47 +233,49 @@ public class ToscaOperationFacade { } return Either.left(true); } + // endregion public Either findLastCertifiedToscaElementByUUID(T component) { Map props = new EnumMap<>(GraphPropertyEnum.class); props.put(GraphPropertyEnum.UUID, component.getUUID()); props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); - Either, JanusGraphOperationStatus> getVertexEither = janusGraphDao .getByCriteria(ModelConverter.getVertexType(component), props); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - } return getToscaElementByOperation(getVertexEither.left().value().get(0)); } - // endregion // region - ToscaElement - GetByOperation 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); - log.debug("getToscaElementByOperation: toscaOperation={}", toscaOperation.getClass()); - Either toscaElement; - String componentId = componentV.getUniqueId(); - if (toscaOperation != null) { - log.debug("Need to fetch tosca element for id {}", componentId); - toscaElement = toscaOperation.getToscaElement(componentV, filters); + private Either getToscaElementByOperation(GraphVertex componentV, + ComponentParametersView filters) { + if (componentV == null) { + log.debug("Unexpected null value for `componentV`"); + return Either.right(StorageOperationStatus.GENERAL_ERROR); } 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()); + VertexTypeEnum label = componentV.getLabel(); + ToscaElementOperation toscaOperation = getToscaElementOperation(componentV); + if (toscaOperation != null) { + log.debug("getToscaElementByOperation: toscaOperation={}", toscaOperation.getClass()); + } + 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); + } + return toscaElement.left().map(ModelConverter::convertFromToscaElement); } - return Either.left(ModelConverter.convertFromToscaElement(toscaElement.left().value())); } // endregion @@ -206,7 +293,6 @@ public class ToscaOperationFacade { public Either createToscaComponent(T resource) { ToscaElement toscaElement = ModelConverter.convertToToscaElement(resource); - ToscaElementOperation toscaElementOperation = getToscaElementOperation(resource); Either createToscaElement = toscaElementOperation.createToscaElement(toscaElement); if (createToscaElement.isLeft()) { @@ -219,21 +305,17 @@ public class ToscaOperationFacade { // region - ToscaElement Delete public StorageOperationStatus markComponentToDelete(Component componentToDelete) { - - if ((componentToDelete.getIsDeleted() != null) && componentToDelete.getIsDeleted() && !componentToDelete.isHighestVersion()) { + if (Boolean.TRUE.equals(componentToDelete.getIsDeleted()) && Boolean.FALSE.equals(componentToDelete.isHighestVersion())) { // component already marked for delete return StorageOperationStatus.OK; } else { - Either getResponse = janusGraphDao .getVertexById(componentToDelete.getUniqueId(), JsonParseFlagEnum.ParseAll); if (getResponse.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentToDelete.getUniqueId(), getResponse.right().value()); return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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()) { @@ -244,13 +326,10 @@ public class ToscaOperationFacade { } public Either deleteToscaComponent(String componentId) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.ParseAll); + Either getVertexEither = janusGraphDao.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.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - } Either deleteElement = deleteToscaElement(getVertexEither.left().value()); if (deleteElement.isRight()) { @@ -258,7 +337,6 @@ public class ToscaOperationFacade { return Either.right(deleteElement.right().value()); } T dataModel = ModelConverter.convertFromToscaElement(deleteElement.left().value()); - return Either.left(dataModel); } @@ -282,36 +360,69 @@ public class ToscaOperationFacade { } return toscaElement; } - // endregion + // endregion private ToscaElementOperation getToscaElementOperation(Component component) { return ModelConverter.isAtomicComponent(component) ? nodeTypeOperation : topologyTemplateOperation; } - public Either getLatestByToscaResourceName(String toscaResourceName) { - return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); + public Either getLatestByToscaResourceNameAndModel(final String toscaResourceName, + final String model) { + return getLatestByNameAndModel(toscaResourceName, JsonParseFlagEnum.ParseMetadata, new ComponentParametersView(), model); + } + + private Either getLatestByNameAndModel(final String nodeName, + final JsonParseFlagEnum parseFlag, + final ComponentParametersView filter, + final String model) { + Either result; + final Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); + final Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); + propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, nodeName); + propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); + final Either, JanusGraphOperationStatus> highestResources = janusGraphDao + .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, parseFlag, model); + if (highestResources.isRight()) { + final JanusGraphOperationStatus status = highestResources.right().value(); + log.debug("failed to find resource with name {}. status={} ", nodeName, status); + result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); + return result; + } + final List resources = highestResources.left().value(); + double version = 0.0; + GraphVertex highestResource = null; + for (final GraphVertex vertex : resources) { + final Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION); + double resourceVersion = Double.parseDouble((String) versionObj); + if (resourceVersion > version) { + version = resourceVersion; + highestResource = vertex; + } + } + return getToscaElementByOperation(highestResource, filter); + } + + public Either getLatestByToscaResourceName(String toscaResourceName, String modelName) { + return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName, modelName); } public Either getFullLatestComponentByToscaResourceName(String toscaResourceName) { ComponentParametersView fetchAllFilter = new ComponentParametersView(); - fetchAllFilter.setIgnoreForwardingPath(true); + fetchAllFilter.setIgnoreServicePath(true); fetchAllFilter.setIgnoreCapabiltyProperties(false); - return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName, JsonParseFlagEnum.ParseAll, fetchAllFilter); + return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName, JsonParseFlagEnum.ParseAll, fetchAllFilter, null); } - public Either getLatestByName(String resourceName) { - return getLatestByName(GraphPropertyEnum.NAME, resourceName); - + public Either getLatestByName(String resourceName, String modelName) { + return getLatestByName(GraphPropertyEnum.NAME, resourceName, modelName); } public StorageOperationStatus validateCsarUuidUniqueness(String csarUUID) { - Map properties = new EnumMap<>(GraphPropertyEnum.class); properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID); - Either, JanusGraphOperationStatus> resources = janusGraphDao .getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata); - if (resources.isRight()) { if (resources.right().value() == JanusGraphOperationStatus.NOT_FOUND) { return StorageOperationStatus.OK; @@ -321,17 +432,17 @@ public class ToscaOperationFacade { } } return StorageOperationStatus.ENTITY_ALREADY_EXISTS; - } - public Either, StorageOperationStatus> getFollowed(String userId, Set lifecycleStates, Set lastStateStates, ComponentTypeEnum componentType) { + 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()); @@ -347,23 +458,175 @@ public class ToscaOperationFacade { } public Either getLatestCertifiedNodeTypeByToscaResourceName(String toscaResourceName) { - return getLatestCertifiedByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata); } - public Either getLatestCertifiedByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag) { + public Either getByToscaResourceNameMatchingVendorRelease(final String toscaResourceName, + final String vendorVersion) { + return getByToscaResourceNameMatchingVendorRelease(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata, + vendorVersion); + } - Either result = null; + public Either getByToscaResourceNameMatchingVendorRelease(String toscaResourceName, VertexTypeEnum vertexType, + JsonParseFlagEnum parseFlag, String vendorRelease) { Map props = new EnumMap<>(GraphPropertyEnum.class); props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); - props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + Map> predicateCriteria = getVendorVersionPredicate(vendorRelease); Either, JanusGraphOperationStatus> getLatestRes = janusGraphDao - .getByCriteria(vertexType, props, parseFlag); + .getByCriteria(vertexType, props, null, predicateCriteria, parseFlag, null); + if (getLatestRes.isRight() || CollectionUtils.isEmpty(getLatestRes.left().value())) { + getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag); + } + return getLatestRes.right().map(status -> { + CommonUtility + .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status); + return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status); + }).left().bind(resources -> { + double version = 0.0; + GraphVertex highestResource = null; + for (GraphVertex resource : resources) { + double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION)); + if (resourceVersion > version && isValidForVendorRelease(resource, vendorRelease)) { + version = resourceVersion; + highestResource = resource; + } + } + if (highestResource != null) { + return getToscaFullElement(highestResource.getUniqueId()); + } else { + log.debug("The vertex with the highest version could not be found for {}", toscaResourceName); + return Either.right(StorageOperationStatus.GENERAL_ERROR); + } + }); + } + + public Either getByToscaResourceNameAndVersion(final String toscaResourceName, + final String version, final String model) { + Either result; + + Map hasProperties = new EnumMap<>(GraphPropertyEnum.class); + Map hasNotProperties = new EnumMap<>(GraphPropertyEnum.class); + + hasProperties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); + hasProperties.put(GraphPropertyEnum.VERSION, version); + hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true); + + Either, JanusGraphOperationStatus> getResourceRes = janusGraphDao + .getByCriteria(VertexTypeEnum.NODE_TYPE, hasProperties, hasNotProperties, JsonParseFlagEnum.ParseAll, model); + if (getResourceRes.isRight()) { + JanusGraphOperationStatus status = getResourceRes.right().value(); + log.debug("failed to find resource with toscaResourceName {}, version {}. Status is {} ", toscaResourceName, version, status); + result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); + return result; + } + return getToscaElementByOperation(getResourceRes.left().value().get(0)); + } + + private Map> getVendorVersionPredicate(final String vendorRelease) { + Map> predicateCriteria = new HashMap<>(); + if (!"1.0".equals(vendorRelease)) { + String[] vendorReleaseElements = vendorRelease.split("\\."); + if (vendorReleaseElements.length > 0) { + String regex = ".*\"vendorRelease\":\""; + for (int i = 0; i < vendorReleaseElements.length; i++) { + regex += vendorReleaseElements[i]; + regex += i < vendorReleaseElements.length - 1 ? "\\." : "\".*"; + } + predicateCriteria.put("metadata", new HashMap.SimpleEntry<>(REGEX, regex)); + } + } + return predicateCriteria; + } + + public boolean isNodeAssociatedToModel(final String model, final Resource resource) { + final List modelElementVertices = getResourceModelElementVertices(resource); + if (model == null) { + return modelElementVertices.isEmpty(); + } + return modelElementVertices.stream().anyMatch(graphVertex -> graphVertex.getMetadataProperty(GraphPropertyEnum.NAME).equals(model)); + } + + public List getResourceModelElementVertices(final Resource resource) { + final Either vertex = + janusGraphDao.getVertexById(resource.getUniqueId(), JsonParseFlagEnum.NoParse); + if (vertex.isRight() || Objects.isNull(vertex.left().value())) { + return Collections.emptyList(); + } + final Either, JanusGraphOperationStatus> nodeModelVertices = + janusGraphDao.getParentVertices(vertex.left().value(), EdgeLabelEnum.MODEL_ELEMENT, JsonParseFlagEnum.NoParse); + if (nodeModelVertices.isRight() || nodeModelVertices.left().value() == null) { + return Collections.emptyList(); + } + return nodeModelVertices.left().value(); + } + + private boolean isValidForVendorRelease(final GraphVertex resource, final String vendorRelease) { + if (!vendorRelease.equals("1.0")) { + try { + Semver resourceSemVer = new Semver((String) resource.getJsonMetadataField(JsonPresentationFields.VENDOR_RELEASE), SemverType.NPM); + Semver packageSemVer = new Semver(vendorRelease, SemverType.NPM); + return !resourceSemVer.isGreaterThan(packageSemVer); + } catch (Exception exception) { + log.debug("Error in comparing vendor release", exception); + return true; + } + } + return true; + } + + public Either getLatestCertifiedByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType, + JsonParseFlagEnum parseFlag) { + Map props = new EnumMap<>(GraphPropertyEnum.class); + props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); + props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + Either, JanusGraphOperationStatus> getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag); + return getLatestRes.right().map(status -> { + CommonUtility + .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status); + return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status); + }).left().bind(resources -> { + 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; + } + } + if (highestResource != null) { + return getToscaFullElement(highestResource.getUniqueId()); + } else { + log.debug("The vertex with the highest version could not be found for {}", toscaResourceName); + return Either.right(StorageOperationStatus.GENERAL_ERROR); + } + }); + } + + public Either getLatestResourceByToscaResourceName(String toscaResourceName) { + if (toscaResourceName != null && toscaResourceName.contains("org.openecomp.resource.vf")) { + return getLatestResourceByToscaResourceName(toscaResourceName, TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseMetadata); + } else { + return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata); + } + } + public Either getLatestResourceByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType, + JsonParseFlagEnum parseFlag) { + Either result = null; + Map props = new EnumMap<>(GraphPropertyEnum.class); + props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); + props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + if (!toscaResourceName.contains("org.openecomp.resource.vf")) { + props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + } + Either, JanusGraphOperationStatus> getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag); if (getLatestRes.isRight()) { JanusGraphOperationStatus status = getLatestRes.right().value(); - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status); + CommonUtility + .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status); result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); } if (result == null) { @@ -377,7 +640,12 @@ public class ToscaOperationFacade { highestResource = resource; } } - result = getToscaFullElement(highestResource.getUniqueId()); + if (highestResource != null) { + result = getToscaFullElement(highestResource.getUniqueId()); + } else { + log.debug("The vertex with the highest version could not be found for {}", toscaResourceName); + result = Either.right(StorageOperationStatus.GENERAL_ERROR); + } } return result; } @@ -390,40 +658,44 @@ public class ToscaOperationFacade { return validateUniquenessRes; } - public Either dissociateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) { + public Either dissociateResourceInstances(String componentId, + RequirementCapabilityRelDef requirementDef) { return nodeTemplateOperation.dissociateResourceInstances(componentId, requirementDef); } /** * Allows to get fulfilled requirement by relation and received predicate */ - public Either getFulfilledRequirementByRelation(String componentId, String instanceId, RequirementCapabilityRelDef relation, BiPredicate predicate) { + public Either getFulfilledRequirementByRelation(String componentId, String instanceId, + RequirementCapabilityRelDef relation, + BiPredicate predicate) { return nodeTemplateOperation.getFulfilledRequirementByRelation(componentId, instanceId, relation, predicate); } /** * Allows to get fulfilled capability by relation and received predicate */ - public Either getFulfilledCapabilityByRelation(String componentId, String instanceId, RequirementCapabilityRelDef relation, BiPredicate predicate) { + public Either getFulfilledCapabilityByRelation(String componentId, String instanceId, + RequirementCapabilityRelDef relation, + BiPredicate predicate) { return nodeTemplateOperation.getFulfilledCapabilityByRelation(componentId, instanceId, relation, predicate); } - public StorageOperationStatus associateResourceInstances(String componentId, List relations) { - Either, StorageOperationStatus> status = nodeTemplateOperation.associateResourceInstances(componentId, relations); - if (status.isRight()) { - return status.right().value(); + public Either, StorageOperationStatus> associateResourceInstances(Component component, String componentId, + List relations) { + Either, StorageOperationStatus> reqAndCapListEither = nodeTemplateOperation + .associateResourceInstances(componentId, relations); + if (component != null) { + updateInstancesCapAndReqOnComponentFromDB(component); } - return StorageOperationStatus.OK; + return reqAndCapListEither; } protected Either validateToscaResourceNameUniqueness(String name) { - Map properties = new EnumMap<>(GraphPropertyEnum.class); properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, name); - Either, JanusGraphOperationStatus> resources = janusGraphDao .getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata); - if (resources.isRight() && resources.right().value() != JanusGraphOperationStatus.NOT_FOUND) { log.debug("failed to get resources from graph with property name: {}", name); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resources.right().value())); @@ -442,53 +714,48 @@ public class ToscaOperationFacade { log.debug("resources with property name:{} does not exists in graph", name); return Either.left(true); } - } // region - Component Update - public Either overrideComponent(Resource newComponent, Resource oldComponent) { - copyArtifactsToNewComponent(newComponent, oldComponent); - Either componentVEither = janusGraphDao .getVertexById(oldComponent.getUniqueId(), JsonParseFlagEnum.NoParse); if (componentVEither.isRight()) { - log.debug("Falied to fetch component {} error {}", oldComponent.getUniqueId(), componentVEither.right().value()); + log.debug("Failed to fetch component {} error {}", oldComponent.getUniqueId(), componentVEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(componentVEither.right().value())); } GraphVertex componentv = componentVEither.left().value(); Either parentVertexEither = janusGraphDao .getParentVertex(componentv, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse); if (parentVertexEither.isRight() && parentVertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) { - log.debug("Falied to fetch parent version for component {} error {}", oldComponent.getUniqueId(), parentVertexEither.right().value()); + log.debug("Failed to fetch parent version for component {} error {}", oldComponent.getUniqueId(), parentVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertexEither.right().value())); } - Either deleteToscaComponent = deleteToscaElement(componentv); if (deleteToscaComponent.isRight()) { - log.debug("Falied to remove old component {} error {}", oldComponent.getUniqueId(), deleteToscaComponent.right().value()); + log.debug("Failed 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()); + log.debug("Failed to create tosca element component {} error {}", newComponent.getUniqueId(), createToscaComponent.right().value()); return Either.right(createToscaComponent.right().value()); } Resource newElement = createToscaComponent.left().value(); Either newVersionEither = janusGraphDao .getVertexById(newElement.getUniqueId(), JsonParseFlagEnum.NoParse); if (newVersionEither.isRight()) { - log.debug("Falied to fetch new tosca element component {} error {}", newComponent.getUniqueId(), newVersionEither.right().value()); + log.debug("Failed to fetch new tosca element component {} error {}", newComponent.getUniqueId(), newVersionEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(newVersionEither.right().value())); } if (parentVertexEither.isLeft()) { GraphVertex previousVersionV = parentVertexEither.left().value(); - JanusGraphOperationStatus - createEdge = janusGraphDao + JanusGraphOperationStatus createEdge = janusGraphDao .createEdge(previousVersionV, newVersionEither.left().value(), EdgeLabelEnum.VERSION, null); if (createEdge != JanusGraphOperationStatus.OK) { - log.debug("Falied to associate to previous version {} new version {} error {}", previousVersionV.getUniqueId(), newVersionEither.right().value(), createEdge); + log.debug("Failed to associate to previous version {} new version {} error {}", previousVersionV.getUniqueId(), + newVersionEither.right().value(), createEdge); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge)); } } @@ -502,20 +769,16 @@ public class ToscaOperationFacade { toscaArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE)); } newComponent.setToscaArtifacts(toscaArtifacts); - Map artifacts = oldComponent.getArtifacts(); if (artifacts != null && !artifacts.isEmpty()) { artifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE)); } newComponent.setArtifacts(artifacts); - Map depArtifacts = oldComponent.getDeploymentArtifacts(); if (depArtifacts != null && !depArtifacts.isEmpty()) { depArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE)); } newComponent.setDeploymentArtifacts(depArtifacts); - - newComponent.setGroups(oldComponent.getGroups()); newComponent.setLastUpdateDate(null); newComponent.setHighestVersion(true); } @@ -524,50 +787,51 @@ public class ToscaOperationFacade { return updateToscaElement(componentToUpdate, new ComponentParametersView()); } - public Either updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) { + private Either updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) { String componentId = componentToUpdate.getUniqueId(); - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.ParseAll); + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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()); + Either updateToscaElement = null; + if (toscaElementOperation != null) { + updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult); + } else { + log.debug("Null value returned by `getToscaElementOperation` with value {}", elementV); + updateToscaElement = Either.right(StorageOperationStatus.GENERAL_ERROR); } - return Either.left(ModelConverter.convertFromToscaElement(updateToscaElement.left().value())); + return updateToscaElement.bimap(ModelConverter::convertFromToscaElement, status -> { + log.debug("Failed to update tosca element {} error {}", componentId, status); + return status; + }); } - private Either getLatestByName(GraphPropertyEnum property, String nodeName, JsonParseFlagEnum parseFlag) { - return getLatestByName(property, nodeName, parseFlag, new ComponentParametersView()); + private Either getLatestByName(GraphPropertyEnum property, String nodeName, + JsonParseFlagEnum parseFlag, String modelName) { + return getLatestByName(property, nodeName, parseFlag, new ComponentParametersView(), modelName); } - private Either getLatestByName(GraphPropertyEnum property, String nodeName, JsonParseFlagEnum parseFlag, ComponentParametersView filter) { + private Either getLatestByName(GraphPropertyEnum property, String nodeName, + JsonParseFlagEnum parseFlag, ComponentParametersView filter, + String model) { 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, JanusGraphOperationStatus> highestResources = janusGraphDao - .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, parseFlag); + .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, parseFlag, model); if (highestResources.isRight()) { JanusGraphOperationStatus status = highestResources.right().value(); log.debug("failed to find resource with name {}. status={} ", nodeName, status); result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); return result; } - List resources = highestResources.left().value(); double version = 0.0; GraphVertex highestResource = null; @@ -582,64 +846,42 @@ public class ToscaOperationFacade { return getToscaElementByOperation(highestResource, filter); } - // endregion // region - Component Get By .. - private Either getLatestByName(GraphPropertyEnum property, String nodeName) { - return getLatestByName(property, nodeName, JsonParseFlagEnum.ParseMetadata); + private Either getLatestByName(GraphPropertyEnum property, String nodeName, String modelName) { + return getLatestByName(property, nodeName, JsonParseFlagEnum.ParseMetadata, modelName); } - 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); - + public Either getBySystemNameAndVersion(final ComponentTypeEnum componentType, + final String systemName, + final String version) { + final Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); + final Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName); - if (componentType != null) + propertiesToMatch.put(GraphPropertyEnum.VERSION, version); + if (componentType != null) { propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); - + } propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - Either, JanusGraphOperationStatus> getComponentsRes = janusGraphDao - .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); - if (getComponentsRes.isRight()) { - JanusGraphOperationStatus status = getComponentsRes.right().value(); - log.debug("Failed to fetch the component with system name {}. Status is {} ", systemName, status); - result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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); + final Either, JanusGraphOperationStatus> getResourceResult + = janusGraphDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); + if (getResourceResult.isRight()) { + final JanusGraphOperationStatus status = getResourceResult.right().value(); + log.debug("Failed to find resource with systemName {}, version {}. Status is {} ", systemName, version, status); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); } - return result; + return getToscaElementByOperation(getResourceResult.left().value().get(0)); } - public Either getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version) { + 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; - + public Either getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, + String version, JsonParseFlagEnum parseFlag) { 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); @@ -651,26 +893,51 @@ public class ToscaOperationFacade { if (getResourceRes.isRight()) { JanusGraphOperationStatus status = getResourceRes.right().value(); log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status); - result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); - return result; + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); } return getToscaElementByOperation(getResourceRes.left().value().get(0)); } - public Either, StorageOperationStatus> getCatalogOrArchiveComponents(boolean isCatalog, List excludeTypes) { - List excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream().filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name())) - .collect(Collectors.toList()); + public Either getComponentByNameAndVendorRelease(final ComponentTypeEnum componentType, + final String name, final String vendorRelease, + final JsonParseFlagEnum parseFlag, + final String modelName) { + Map hasProperties = new EnumMap<>(GraphPropertyEnum.class); + Map hasNotProperties = new EnumMap<>(GraphPropertyEnum.class); + hasProperties.put(GraphPropertyEnum.NAME, name); + hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true); + if (componentType != null) { + hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); + } + Map> predicateCriteria = getVendorVersionPredicate(vendorRelease); + Either, JanusGraphOperationStatus> getResourceRes = janusGraphDao.getByCriteria(null, hasProperties, hasNotProperties, + predicateCriteria, parseFlag, modelName); + if (getResourceRes.isRight()) { + JanusGraphOperationStatus status = getResourceRes.right().value(); + log.debug("failed to find resource with name {}, version {}. Status is {} ", name, predicateCriteria, status); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); + } + return getToscaElementByOperation(getResourceRes.left().value().get(0)); + } + + public Either, StorageOperationStatus> getCatalogOrArchiveComponents(boolean isCatalog, + List excludeTypes) { + List excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream() + .filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name())) + .collect(Collectors.toList()); return topologyTemplateOperation.getElementCatalogData(isCatalog, excludedResourceTypes); } // endregion - public Either, StorageOperationStatus> getCatalogComponents(ComponentTypeEnum componentType, List excludeTypes, boolean isHighestVersions) { + public Either, StorageOperationStatus> getCatalogComponents(ComponentTypeEnum componentType, + List excludeTypes, + boolean isHighestVersions) { List components = new ArrayList<>(); Either, StorageOperationStatus> catalogDataResult; List toscaElements = new ArrayList<>(); - List excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream().filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name())) - .collect(Collectors.toList()); - + List excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream() + .filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name())) + .collect(Collectors.toList()); switch (componentType) { case RESOURCE: catalogDataResult = nodeTypeOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludedResourceTypes, isHighestVersions); @@ -721,93 +988,218 @@ public class ToscaOperationFacade { return Either.left(checkIfInUseAndDelete(allMarked)); } - private List checkIfInUseAndDelete(List allMarked) { - final List forbiddenEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF); - List deleted = new ArrayList<>(); - - for (GraphVertex elementV : allMarked) { - boolean isAllowedToDelete = true; - - for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) { - Either belongingEdgeByCriteria = janusGraphDao - .getBelongingEdgeByCriteria(elementV, edgeLabelEnum, null); - if (belongingEdgeByCriteria.isLeft()){ - log.debug("Marked element {} in use. don't delete it", elementV.getUniqueId()); - isAllowedToDelete = false; - break; - } - } + public List deleteService(String invariantUUID, final boolean inTransaction) { + return deleteComponent(invariantUUID, NodeTypeEnum.Service, inTransaction); + } - if (isAllowedToDelete) { + public List deleteComponent(String invariantUUID, NodeTypeEnum componentType, final boolean inTransaction) { + final List allServiceVerticesToDelete = findVertexListByInvariantUuid(invariantUUID); + List affectedComponentIds = new ArrayList<>(); + try { + checkNotUsed(allServiceVerticesToDelete); + lockAllVerticesByNodeType(allServiceVerticesToDelete, componentType); + for (GraphVertex elementV : allServiceVerticesToDelete) { Either deleteToscaElement = deleteToscaElement(elementV); if (deleteToscaElement.isRight()) { - log.debug("Failed to delete marked element UniqueID {}, Name {}, error {}", elementV.getUniqueId(), elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value()); - continue; + log.debug("Failed to delete element UniqueID {}, Name {}, error {}", elementV.getUniqueId(), + elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value()); + throwStorageException(deleteToscaElement.right().value()); } - deleted.add(elementV.getUniqueId()); + affectedComponentIds.add(elementV.getUniqueId()); + } + if (!inTransaction) { + janusGraphDao.commit(); + } + } catch (Exception exception) { + if (!inTransaction) { + janusGraphDao.rollback(); } + throw exception; + } finally { + unlockAllVerticesByNodeType(allServiceVerticesToDelete, componentType); } - return deleted; + return affectedComponentIds; } - 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); + private void checkNotUsed(List vertices) { + boolean isInUse = isAnyComponentInUse(vertices); + if (isInUse) { + Set listOfVertices = getComponentsUsingComponents(vertices); + List listOfStringComponents = new ArrayList<>(); + for (GraphVertex componentVertex : listOfVertices) { + listOfStringComponents.add( + componentVertex.getMetadataJson().get(GraphPropertyEnum.COMPONENT_TYPE.getProperty()) + " " + + componentVertex.getMetadataJson().get(GraphPropertyEnum.NAME.getProperty()) + ); + } + String stringOfComponents = String.join(", ", listOfStringComponents); + throw ToscaOperationExceptionSupplier.componentInUse(stringOfComponents).get(); } - if (allComponentsMarkedForDeletion.isRight()) { + } + + public List findVertexListByInvariantUuid(final String invariantUuid) { + try { + return janusGraphDao.findAllVertexByInvariantUuid(invariantUuid, Collections.emptyMap()); + } catch (final JanusGraphException e) { + log.error(EcompLoggerErrorCode.DATA_ERROR, this.getClass().getName(), e.getMessage()); + throw new StorageException(e.getStatus()); + } + } + + public void commitAndCheck(String componentId) { + JanusGraphOperationStatus status = janusGraphDao.commit(); + if (!status.equals(JanusGraphOperationStatus.OK)) { + log.debug("error occurred when trying to DELETE {}. Return code is: {}", componentId, status); + throwStorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); + } + } + + private Set getComponentsUsingComponents(List componentVertices) { + Set inUseBy = new TreeSet<>(Comparator.comparing(GraphVertex::getUniqueId)); + for (final GraphVertex elementV : componentVertices) { + List inUseByVertex = isInUse(elementV); + if (!inUseByVertex.isEmpty()) { + inUseBy.addAll(inUseByVertex); + } + } + return inUseBy; + } + + private boolean isAnyComponentInUse(List componentVertices) { + boolean isComponentInUse = false; + if (log.isDebugEnabled()) { + for (final GraphVertex graphVertex : componentVertices) { + if (!isInUse(graphVertex).isEmpty()) { + isComponentInUse = true; + } + } + } else { + isComponentInUse = componentVertices.stream().anyMatch(vertex -> !isInUse(vertex).isEmpty()); + } + return isComponentInUse; + } + + private List isInUse(GraphVertex elementV) { + final List forbiddenEdgeLabelEnums = Arrays + .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF); + for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) { + Either, JanusGraphOperationStatus> inUseBy = + janusGraphDao.getParentVertices(elementV, edgeLabelEnum, JsonParseFlagEnum.ParseAll); + if (inUseBy.isLeft()) { + if (log.isDebugEnabled()) { + log.debug("Element {} in use.", elementV.getUniqueId()); + } + return inUseBy.left().value(); + } + } + return Collections.emptyList(); + } + + private List checkIfInUseAndDelete(List allMarked) { + List deleted = new ArrayList<>(); + for (GraphVertex elementV : allMarked) { + boolean isAllowedToDelete = !isInUse(elementV).isEmpty(); + if (isAllowedToDelete) { + Either deleteToscaElement = deleteToscaElement(elementV); + if (deleteToscaElement.isRight()) { + log.debug("Failed to delete marked element UniqueID {}, Name {}, error {}", elementV.getUniqueId(), + elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value()); + continue; + } + deleted.add(elementV.getUniqueId()); + } + } + return deleted; + } + + private void lockAllVerticesByNodeType(List allVerticesToLock, NodeTypeEnum nodeType) { + for (GraphVertex graphVertex : allVerticesToLock) { + StorageOperationStatus storageOperationStatus = graphLockOperation.lockComponent(graphVertex.getUniqueId(), nodeType); + if (!storageOperationStatus.equals(StorageOperationStatus.OK)) { + throwStorageException(storageOperationStatus); + } + } + } + + private void unlockAllVerticesByNodeType(List allVerticesToUnlock, NodeTypeEnum nodeType) { + for (GraphVertex graphVertex : allVerticesToUnlock) { + graphLockOperation.unlockComponent(graphVertex.getUniqueId(), nodeType); + } + } + + 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(GraphVertex::getUniqueId).collect(Collectors.toList())); } // region - Component Update - public Either, StorageOperationStatus> addComponentInstanceToTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance, boolean allowDeleted, User user) { - + public Either, StorageOperationStatus> addComponentInstanceToTopologyTemplate(Component containerComponent, + Component origComponent, + ComponentInstance componentInstance, + boolean allowDeleted, User user) { Either, StorageOperationStatus> result = null; Either updateContainerComponentRes = null; if (StringUtils.isEmpty(componentInstance.getIcon())) { componentInstance.setIcon(origComponent.getIcon()); } - String nameToFindForCounter = componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy ? ValidationUtils.normaliseComponentName(componentInstance.getSourceModelName()) + PROXY_SUFFIX : origComponent.getName(); + String nameToFindForCounter; + switch (componentInstance.getOriginType()) { + case ServiceProxy: + nameToFindForCounter = ValidationUtils.normaliseComponentName(componentInstance.getSourceModelName()) + PROXY_SUFFIX; + break; + case ServiceSubstitution: + nameToFindForCounter = ValidationUtils.normaliseComponentName(componentInstance.getSourceModelName()); + break; + default: + nameToFindForCounter = origComponent.getName(); + } String nextComponentInstanceCounter = getNextComponentInstanceCounter(containerComponent, nameToFindForCounter); - Either, StorageOperationStatus> addResult = nodeTemplateOperation.addComponentInstanceToTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), + Either, StorageOperationStatus> addResult = nodeTemplateOperation + .addComponentInstanceToTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), ModelConverter.convertToToscaElement(origComponent), nextComponentInstanceCounter, componentInstance, allowDeleted, user); - if (addResult.isRight()) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the component instance {} to container component {}. ", componentInstance.getName(), containerComponent.getName()); + 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()); + 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()); + 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; + public void associateComponentInstancesToComponent(Component containerComponent, Map resourcesInstancesMap, + boolean allowDeleted, boolean isUpdateCsar) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Going to add component instances to component {}", containerComponent.getUniqueId()); - Either metadataVertex = janusGraphDao .getVertexById(containerComponent.getUniqueId(), JsonParseFlagEnum.ParseAll); if (metadataVertex.isRight()) { @@ -815,30 +1207,36 @@ public class ToscaOperationFacade { if (status == JanusGraphOperationStatus.NOT_FOUND) { status = JanusGraphOperationStatus.INVALID_ID; } - result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status); - } - if (result == null) { - result = nodeTemplateOperation.associateComponentInstancesToComponent(containerComponent, resourcesInstancesMap, metadataVertex.left().value(), allowDeleted); + throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); } - return result; + Map compnentInstancesMap = nodeTemplateOperation + .associateComponentInstancesToComponent(containerComponent, resourcesInstancesMap, metadataVertex.left().value(), allowDeleted, + isUpdateCsar); + containerComponent.setComponentInstances(ModelConverter.getComponentInstancesFromMapObject(compnentInstancesMap, containerComponent)); } - public Either, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance) { - + 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()); + 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), + 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()); + 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()); + 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; @@ -848,41 +1246,45 @@ public class ToscaOperationFacade { return updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, new ComponentParametersView()); } - public Either updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, ComponentParametersView filter) { - + public Either updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, + ComponentParametersView filter) { 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), filter); + CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata belonging to container component {}. ", + containerComponent.getName()); + Either updateResult = nodeTemplateOperation + .updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), filter); if (updateResult.isRight()) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata belonging to container component {}. ", containerComponent.getName()); + 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()); + CommonUtility + .addRecordToLog(log, LogLevelEnum.TRACE, "The metadata has been updated to container component {}. ", updatedComponent.getName()); result = Either.left(updatedComponent); } return result; } - // endregion - - public Either, StorageOperationStatus> deleteComponentInstanceFromTopologyTemplate(Component containerComponent, String resourceInstanceId) { + // endregion + 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); + 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()); + 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()); + 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; @@ -903,16 +1305,22 @@ public class ToscaOperationFacade { /** * @return max counter of component instance Id's, null if not found */ - private Integer getMaxCounterFromNamesAndIds(Component containerComponent, String normalizedName) { - List countersInNames = containerComponent.getComponentInstances().stream() - .filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName)) - .map(ci -> ci.getNormalizedName().split(normalizedName)[1]) - .collect(Collectors.toList()); - List countersInIds = containerComponent.getComponentInstances().stream() - .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName)) - .map(ci -> ci.getUniqueId().split(normalizedName)[1]) - .collect(Collectors.toList()); - List namesAndIdsList = new ArrayList<>(countersInNames); + private Integer getMaxCounterFromNamesAndIds(final Component containerComponent, final String normalizedName) { + final Pattern COUNTER_PATTERN = Pattern.compile(normalizedName + "[\\s_:-]?\\d+$"); + final List countersInNames = containerComponent.getComponentInstances().stream() + .filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName)) + .filter(ci -> !ci.getNormalizedName().equals(normalizedName)) + .map(ComponentInstance::getNormalizedName) + .map(COUNTER_PATTERN::matcher).filter(Matcher::find).map(matcher -> matcher.group(0)) + .map(nn -> nn.replaceAll("\\D", "")) + .collect(Collectors.toList()); + final List countersInIds = containerComponent.getComponentInstances().stream() + .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName)) + .map(ComponentInstance::getUniqueId) + .map(COUNTER_PATTERN::matcher).filter(Matcher::find).map(matcher -> matcher.group(0)) + .map(nn -> nn.replaceAll("\\D", "")) + .collect(Collectors.toList()); + final List namesAndIdsList = new ArrayList<>(countersInNames); namesAndIdsList.addAll(countersInIds); return getMaxInteger(namesAndIdsList); } @@ -927,61 +1335,70 @@ public class ToscaOperationFacade { maxCounter = currCounter; } } catch (NumberFormatException e) { - continue; } } return currCounter == null ? null : maxCounter; } - public Either associateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) { + public Either associateResourceInstances(Component component, String componentId, + RequirementCapabilityRelDef requirementDef) { return nodeTemplateOperation.associateResourceInstances(componentId, requirementDef); - } public Either, StorageOperationStatus> createAndAssociateInputs(Map inputs, String componentId) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - } - GraphVertex vertex = getVertexEither.left().value(); - Map inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue()))); - + 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); + log.debug(INPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY); List inputsResList = null; if (inputsMap != null && !inputsMap.isEmpty()) { - inputsResList = inputsMap.values().stream() - .map(InputDefinition::new) - .collect(Collectors.toList()); + inputsResList = inputsMap.values().stream().map(InputDefinition::new).collect(Collectors.toList()); } return Either.left(inputsResList); } return Either.right(status); + } + public Either, StorageOperationStatus> createAndAssociateOutputs(final Map outputs, + final String componentId) { + final Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getVertexEither.isRight()) { + log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); + } + final GraphVertex vertex = getVertexEither.left().value(); + final Map outputsMap = outputs.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> new OutputDefinition(e.getValue()))); + final StorageOperationStatus status = topologyTemplateOperation.associateOutputsToComponent(vertex, outputsMap, componentId); + if (StorageOperationStatus.OK == status) { + log.debug(OUTPUTS_ASSOCIATED_TO_COMPONENT_SUCCESSFULLY); + List outputsResList = null; + if (MapUtils.isNotEmpty(outputsMap)) { + outputsResList = outputsMap.values().stream().map(OutputDefinition::new).collect(Collectors.toList()); + } + return Either.left(outputsResList); + } + return Either.right(status); } public Either, StorageOperationStatus> addInputsToComponent(Map inputs, String componentId) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - } - GraphVertex vertex = getVertexEither.left().value(); - Map inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDefinition(e.getValue()))); - - StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsMap, JsonPresentationFields.NAME); - + Map inputsMap = inputs.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDefinition(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; @@ -991,7 +1408,28 @@ public class ToscaOperationFacade { return Either.left(inputsResList); } return Either.right(status); + } + public Either, StorageOperationStatus> addOutputsToComponent(Map outputs, String componentId) { + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getVertexEither.isRight()) { + log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); + } + GraphVertex vertex = getVertexEither.left().value(); + Map outputsMap = outputs.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> new AttributeDefinition(e.getValue()))); + StorageOperationStatus status = topologyTemplateOperation + .addToscaDataToToscaElement(vertex, EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputsMap, JsonPresentationFields.NAME); + if (StorageOperationStatus.OK == status) { + log.debug(COMPONENT_CREATED_SUCCESSFULLY); + List outputsResList = null; + if (outputsMap != null && !outputsMap.isEmpty()) { + outputsResList = outputsMap.values().stream().map(OutputDefinition::new).collect(Collectors.toList()); + } + return Either.left(outputsResList); + } + return Either.right(status); } /** @@ -1001,10 +1439,9 @@ public class ToscaOperationFacade { * @param componentId unique ID of Component. * @return list of data types. */ - public Either, StorageOperationStatus> addDataTypesToComponent(Map dataTypes, String componentId) { - + public Either, StorageOperationStatus> addDataTypesToComponent(Map dataTypes, + String componentId) { log.trace("#addDataTypesToComponent - enter, componentId={}", componentId); - /* get component vertex */ Either getVertexEither = janusGraphDao .getVertexById(componentId, JsonParseFlagEnum.NoParse); @@ -1015,16 +1452,14 @@ public class ToscaOperationFacade { } GraphVertex vertex = getVertexEither.left().value(); log.trace("#addDataTypesToComponent - get vertex ok"); - // convert DataTypeDefinition to DataTypeDataDefinition Map dataTypeDataMap = dataTypes.entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, e -> convertDataTypeToDataTypeData(e.getValue()))); - + .collect(Collectors.toMap(Map.Entry::getKey, e -> convertDataTypeToDataTypeData(e.getValue()))); // add datatype(s) to the Component. - // if child vertex does not exist, it will be created. - StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex, - EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeDataMap, JsonPresentationFields.NAME); + // if child vertex does not exist, it will be created. + StorageOperationStatus status = topologyTemplateOperation + .addToscaDataToToscaElement(vertex, EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeDataMap, JsonPresentationFields.NAME); if (StorageOperationStatus.OK == status) { log.debug(COMPONENT_CREATED_SUCCESSFULLY); List inputsResList = null; @@ -1033,7 +1468,6 @@ public class ToscaOperationFacade { } return Either.left(inputsResList); } - log.trace("#addDataTypesToComponent - leave"); return Either.right(status); } @@ -1041,122 +1475,117 @@ public class ToscaOperationFacade { private DataTypeDataDefinition convertDataTypeToDataTypeData(DataTypeDefinition dataType) { DataTypeDataDefinition dataTypeData = new DataTypeDataDefinition(dataType); if (CollectionUtils.isNotEmpty(dataType.getProperties())) { - List propertyDataList = dataType.getProperties().stream() - .map(PropertyDataDefinition::new).collect(Collectors.toList()); + List propertyDataList = dataType.getProperties().stream().map(PropertyDataDefinition::new) + .collect(Collectors.toList()); dataTypeData.setPropertiesData(propertyDataList); } - // if "derivedFrom" data_type exists, copy the name to "derivedFromName" if (dataType.getDerivedFrom() != null && StringUtils.isNotEmpty(dataType.getDerivedFrom().getName())) { // if names are different, log it if (!StringUtils.equals(dataTypeData.getDerivedFromName(), dataType.getDerivedFrom().getName())) { - log.debug("#convertDataTypeToDataTypeData - derivedFromName(={}) overwritten by derivedFrom.name(={})", - dataType.getDerivedFromName(), dataType.getDerivedFrom().getName()); + log.debug("#convertDataTypeToDataTypeData - derivedFromName(={}) overwritten by derivedFrom.name(={})", dataType.getDerivedFromName(), + dataType.getDerivedFrom().getName()); } dataTypeData.setDerivedFromName(dataType.getDerivedFrom().getName()); } - // supply "name" field to toscaPresentationValue in each datatype object for DAO operations dataTypeData.setToscaPresentationValue(JsonPresentationFields.NAME, dataType.getName()); return dataTypeData; } - public Either, StorageOperationStatus> getComponentInputs(String componentId) { - - Either getVertexEither = janusGraphDao - .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.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - - } - - Either toscaElement = - topologyTemplateOperation.getToscaElement(componentId); - if(toscaElement.isRight()) { - return Either.right(toscaElement.right().value()); - } - - TopologyTemplate topologyTemplate = (TopologyTemplate) toscaElement.left().value(); - - Map inputsMap = topologyTemplate.getInputs(); - - List inputs = new ArrayList<>(); - if(MapUtils.isNotEmpty(inputsMap)) { - inputs = - inputsMap.values().stream().map(p -> new InputDefinition(p)).collect(Collectors.toList()); - } - - return Either.left(inputs); - } - - public Either, StorageOperationStatus> updateInputsToComponent(List inputs, String componentId) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - } + Either toscaElement = topologyTemplateOperation.getToscaElement(componentId); + if (toscaElement.isRight()) { + return Either.right(toscaElement.right().value()); + } + TopologyTemplate topologyTemplate = (TopologyTemplate) toscaElement.left().value(); + Map inputsMap = topologyTemplate.getInputs(); + List inputs = new ArrayList<>(); + if (MapUtils.isNotEmpty(inputsMap)) { + inputs = inputsMap.values().stream().map(p -> new InputDefinition(p)).collect(Collectors.toList()); + } + return Either.left(inputs); + } + public Either, StorageOperationStatus> updateInputsToComponent(List inputs, String componentId) { + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getVertexEither.isRight()) { + log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); + } GraphVertex vertex = getVertexEither.left().value(); List inputsAsDataDef = inputs.stream().map(PropertyDataDefinition::new).collect(Collectors.toList()); - - StorageOperationStatus status = topologyTemplateOperation.updateToscaDataOfToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsAsDataDef, JsonPresentationFields.NAME); - + StorageOperationStatus status = topologyTemplateOperation + .updateToscaDataOfToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsAsDataDef, JsonPresentationFields.NAME); if (StorageOperationStatus.OK == status) { log.debug(COMPONENT_CREATED_SUCCESSFULLY); List inputsResList = null; - if (inputsAsDataDef != null && !inputsAsDataDef.isEmpty()) { + if (CollectionUtils.isNotEmpty(inputsAsDataDef)) { inputsResList = inputsAsDataDef.stream().map(InputDefinition::new).collect(Collectors.toList()); } return Either.left(inputsResList); } return Either.right(status); + } + public Either, StorageOperationStatus> updateOutputsToComponent(List outputs, String componentId) { + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getVertexEither.isRight()) { + log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); + } + GraphVertex vertex = getVertexEither.left().value(); + List outputsAsDataDef = outputs.stream().map(AttributeDataDefinition::new).collect(Collectors.toList()); + StorageOperationStatus status = topologyTemplateOperation + .updateToscaDataOfToscaElement(vertex, EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputsAsDataDef, JsonPresentationFields.NAME); + if (StorageOperationStatus.OK == status) { + log.debug(COMPONENT_CREATED_SUCCESSFULLY); + List outputsResList = null; + if (!outputsAsDataDef.isEmpty()) { + outputsResList = outputsAsDataDef.stream().map(OutputDefinition::new).collect(Collectors.toList()); + } + return Either.left(outputsResList); + } + return Either.right(status); } // region - ComponentInstance - public Either>, StorageOperationStatus> associateComponentInstancePropertiesToComponent(Map> instProperties, String componentId) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + public Either>, StorageOperationStatus> associateComponentInstancePropertiesToComponent( + Map> instProperties, String componentId) { + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e))); - + propertiesMap.setMapToscaDataDefinition( + entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::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); - } /** * saves the instInputs as the updated instance inputs of the component container in DB */ - public Either>, StorageOperationStatus> updateComponentInstanceInputsToComponent(Map> instInputs, String componentId) { + public Either>, StorageOperationStatus> updateComponentInstanceInputsToComponent( + Map> instInputs, String componentId) { if (instInputs == null || instInputs.isEmpty()) { return Either.left(instInputs); } @@ -1165,20 +1594,23 @@ public class ToscaOperationFacade { List toscaDataListPerInst = inputsPerIntance.getValue(); List pathKeysPerInst = new ArrayList<>(); pathKeysPerInst.add(inputsPerIntance.getKey()); - status = topologyTemplateOperation.updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, toscaDataListPerInst, pathKeysPerInst, JsonPresentationFields.NAME); + status = topologyTemplateOperation + .updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, toscaDataListPerInst, + pathKeysPerInst, JsonPresentationFields.NAME); if (status != StorageOperationStatus.OK) { - log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}", inputsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_INPUTS, status); + log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}", + inputsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_INPUTS, status); return Either.right(status); } } - return Either.left(instInputs); } /** * saves the instProps as the updated instance properties of the component container in DB */ - public Either>, StorageOperationStatus> updateComponentInstancePropsToComponent(Map> instProps, String componentId) { + public Either>, StorageOperationStatus> updateComponentInstancePropsToComponent( + Map> instProps, String componentId) { if (instProps == null || instProps.isEmpty()) { return Either.left(instProps); } @@ -1187,50 +1619,89 @@ public class ToscaOperationFacade { List toscaDataListPerInst = propsPerIntance.getValue(); List pathKeysPerInst = new ArrayList<>(); pathKeysPerInst.add(propsPerIntance.getKey()); - status = topologyTemplateOperation.updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, toscaDataListPerInst, pathKeysPerInst, JsonPresentationFields.NAME); + status = topologyTemplateOperation + .updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, + toscaDataListPerInst, pathKeysPerInst, JsonPresentationFields.NAME); if (status != StorageOperationStatus.OK) { - log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}", propsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_PROPERTIES, status); + log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}", + propsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_PROPERTIES, status); return Either.right(status); } } - return Either.left(instProps); } - public Either>, StorageOperationStatus> associateComponentInstanceInputsToComponent(Map> instInputs, String componentId) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + public Either>, StorageOperationStatus> associateComponentInstanceInputsToComponent( + Map> instInputs, String componentId) { + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - } GraphVertex vertex = getVertexEither.left().value(); Map instPropsMap = new HashMap<>(); if (instInputs != null) { - MapPropertiesDataDefinition propertiesMap; for (Entry> entry : instInputs.entrySet()) { propertiesMap = new MapPropertiesDataDefinition(); - - propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e))); - + propertiesMap.setMapToscaDataDefinition( + entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e))); instPropsMap.put(entry.getKey(), propertiesMap); } } - StorageOperationStatus status = topologyTemplateOperation.associateInstInputsToComponent(vertex, instPropsMap); - if (StorageOperationStatus.OK == status) { log.debug(COMPONENT_CREATED_SUCCESSFULLY); return Either.left(instInputs); } return Either.right(status); + } + + public Either, StorageOperationStatus> associateComponentInstanceInterfacesToComponent( + Map> instInterfaces, + String componentId + ) { + Either getVertexEither = janusGraphDao.getVertexById( + componentId, + JsonParseFlagEnum.NoParse + ); + if (getVertexEither.isRight()) { + log.debug( + COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, + componentId, + getVertexEither.right().value() + ); + return Either.right( + DaoStatusConverter.convertJanusGraphStatusToStorageStatus( + getVertexEither.right().value() + ) + ); + } + GraphVertex vertex = getVertexEither.left().value(); + Map instInterfacesMap = new HashMap<>(); + if (instInterfaces != null) { + for (Map.Entry> entryInstances : instInterfaces.entrySet()) { + Map incomingInterfacesMap = entryInstances.getValue().entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); + MapInterfaceDataDefinition interfacesMap = new MapInterfaceDataDefinition(); + interfacesMap.setMapToscaDataDefinition(incomingInterfacesMap); + instInterfacesMap.put(entryInstances.getKey(), interfacesMap); + } + } + StorageOperationStatus status = topologyTemplateOperation.associateInstInterfacesToComponent( + vertex, + instInterfacesMap + ); + if (StorageOperationStatus.OK == status) { + log.debug(COMPONENT_CREATED_SUCCESSFULLY); + return Either.left(instInterfacesMap); + } + return Either.right(status); } - public Either>, StorageOperationStatus> addComponentInstanceInputsToComponent(Component containerComponent, Map> instProperties) { + public Either>, StorageOperationStatus> addComponentInstanceInputsToComponent( + Component containerComponent, Map> instProperties) { requireNonNull(instProperties); StorageOperationStatus status; for (Entry> entry : instProperties.entrySet()) { @@ -1240,8 +1711,7 @@ public class ToscaOperationFacade { for (ComponentInstanceInput property : props) { List componentInstancesInputs = containerComponent.getComponentInstancesInputs().get(componentInstanceId); Optional instanceProperty = componentInstancesInputs.stream() - .filter(p -> p.getName().equals(property.getName())) - .findAny(); + .filter(p -> p.getName().equals(property.getName())).findAny(); if (instanceProperty.isPresent()) { status = updateComponentInstanceInput(containerComponent, componentInstanceId, property); } else { @@ -1259,32 +1729,60 @@ public class ToscaOperationFacade { return Either.left(instProperties); } - public Either>, StorageOperationStatus> addComponentInstancePropertiesToComponent(Component containerComponent, Map> instProperties) { + public Either>, StorageOperationStatus> addComponentInstanceOutputsToComponent( + Component containerComponent, Map> instOutputs) { + requireNonNull(instOutputs); + StorageOperationStatus status; + for (final Entry> entry : instOutputs.entrySet()) { + final List outputs = entry.getValue(); + final String componentInstanceId = entry.getKey(); + if (!isEmpty(outputs)) { + for (final ComponentInstanceOutput output : outputs) { + final List componentInstanceOutputs = containerComponent.getComponentInstancesOutputs() + .get(componentInstanceId); + final Optional componentInstanceOutput = componentInstanceOutputs.stream() + .filter(p -> p.getName().equals(output.getName())).findAny(); + if (componentInstanceOutput.isPresent()) { + status = updateComponentInstanceOutput(containerComponent, componentInstanceId, output); + } else { + status = addComponentInstanceOutput(containerComponent, componentInstanceId, output); + } + if (status != StorageOperationStatus.OK) { + log.debug("Failed to update instance output {} for instance {} error {} ", output, componentInstanceId, status); + return Either.right(status); + } else { + log.trace("instance output {} for instance {} updated", output, componentInstanceId); + } + } + } + } + return Either.left(instOutputs); + } + + public Either>, StorageOperationStatus> addComponentInstancePropertiesToComponent( + Component containerComponent, Map> instProperties) { requireNonNull(instProperties); for (Entry> entry : instProperties.entrySet()) { List props = entry.getValue(); String componentInstanceId = entry.getKey(); - List originalComponentInstProps = - containerComponent.getComponentInstancesProperties().get(componentInstanceId); + List originalComponentInstProps = containerComponent.getComponentInstancesProperties() + .get(componentInstanceId); Map> containerComponentCapabilities = containerComponent.getCapabilities(); - - if(isEmpty(props)) { + if (isEmpty(props)) { continue; } for (ComponentInstanceProperty property : props) { StorageOperationStatus status = null; String propertyParentUniqueId = property.getParentUniqueId(); - Optional - capPropDefinition = getPropertyCapability(propertyParentUniqueId, containerComponent); - if(capPropDefinition.isPresent() && MapUtils.isNotEmpty(containerComponentCapabilities)) { - status = populateAndUpdateInstanceCapProperty(containerComponent, componentInstanceId, - containerComponentCapabilities, property, capPropDefinition.get()); + Optional capPropDefinition = getPropertyCapability(propertyParentUniqueId, containerComponent); + if (capPropDefinition.isPresent() && MapUtils.isNotEmpty(containerComponentCapabilities)) { + status = populateAndUpdateInstanceCapProperty(containerComponent, componentInstanceId, containerComponentCapabilities, property, + capPropDefinition.get()); } - if(status == null) { - status = updateOrAddComponentInstanceProperty(containerComponent, componentInstanceId, - originalComponentInstProps, property); + if (status == null) { + status = updateOrAddComponentInstanceProperty(containerComponent, componentInstanceId, originalComponentInstProps, property); } - if(status != StorageOperationStatus.OK) { + if (status != StorageOperationStatus.OK) { return Either.right(status); } } @@ -1292,87 +1790,105 @@ public class ToscaOperationFacade { return Either.left(instProperties); } + public Either>, StorageOperationStatus> addComponentInstanceAttributesToComponent( + final Component containerComponent, final Map> componentInstanceAttribute) { + requireNonNull(componentInstanceAttribute); + for (final Entry> entry : componentInstanceAttribute.entrySet()) { + final List attributes = entry.getValue(); + if (isEmpty(attributes)) { + continue; + } + final String componentInstanceId = entry.getKey(); + final List componentInstanceAttributes = containerComponent.getComponentInstancesAttributes() + .get(componentInstanceId); + for (final ComponentInstanceAttribute attribute : attributes) { + final StorageOperationStatus status = updateOrAddComponentInstanceAttribute(containerComponent, componentInstanceId, + componentInstanceAttributes, attribute); + if (status != StorageOperationStatus.OK) { + return Either.right(status); + } + } + } + return Either.left(componentInstanceAttribute); + } + private StorageOperationStatus populateAndUpdateInstanceCapProperty(Component containerComponent, String componentInstanceId, Map> containerComponentCapabilities, ComponentInstanceProperty property, CapabilityDefinition capabilityDefinition) { List capabilityDefinitions = containerComponentCapabilities.get(capabilityDefinition.getType()); - if(CollectionUtils.isEmpty(capabilityDefinitions)) { + if (CollectionUtils.isEmpty(capabilityDefinitions)) { return null; } Optional capDefToGetProp = capabilityDefinitions.stream() - .filter(cap -> cap.getUniqueId().equals(capabilityDefinition.getUniqueId()) && cap.getPath().size() == 1).findAny(); - if(capDefToGetProp.isPresent()) { + .filter(cap -> cap.getUniqueId().equals(capabilityDefinition.getUniqueId()) && cap.getPath().size() == 1).findAny(); + if (capDefToGetProp.isPresent()) { return updateInstanceCapabilityProperty(containerComponent, componentInstanceId, property, capDefToGetProp.get()); } return null; } - private static Optional getPropertyCapability(String propertyParentUniqueId, - Component containerComponent) { - - Map> componentCapabilities = containerComponent.getCapabilities(); - if(MapUtils.isEmpty(componentCapabilities)){ - return Optional.empty(); - } - List capabilityDefinitionList = componentCapabilities.values() - .stream().flatMap(Collection::stream).collect(Collectors.toList()); - if(CollectionUtils.isEmpty(capabilityDefinitionList)){ - return Optional.empty(); - } - return capabilityDefinitionList.stream() - .filter(capabilityDefinition -> capabilityDefinition.getUniqueId().equals(propertyParentUniqueId)) - .findAny(); - } - - private StorageOperationStatus updateOrAddComponentInstanceProperty(Component containerComponent, - String componentInstanceId, List originalComponentInstProps, - ComponentInstanceProperty property) - { + private StorageOperationStatus updateOrAddComponentInstanceProperty(Component containerComponent, String componentInstanceId, + List originalComponentInstProps, + ComponentInstanceProperty property) { StorageOperationStatus status; // check if the property already exists or not Optional instanceProperty = originalComponentInstProps.stream() - .filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny(); + .filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny(); if (instanceProperty.isPresent()) { status = updateComponentInstanceProperty(containerComponent, componentInstanceId, property); } else { status = addComponentInstanceProperty(containerComponent, componentInstanceId, property); } if (status != StorageOperationStatus.OK) { - log.debug("Failed to update instance property {} for instance {} error {} ", - property, componentInstanceId, status); + log.debug("Failed to update instance property {} for instance {} error {} ", property, componentInstanceId, status); + } + return status; + } + + private StorageOperationStatus updateOrAddComponentInstanceAttribute(Component containerComponent, String componentInstanceId, + List componentInstanceAttributes, + ComponentInstanceAttribute attribute) { + StorageOperationStatus status; + // check if the attribute already exists or not + Optional instanceProperty = componentInstanceAttributes.stream() + .filter(p -> p.getUniqueId().equals(attribute.getUniqueId())).findAny(); + if (instanceProperty.isPresent()) { + status = updateComponentInstanceAttribute(containerComponent, componentInstanceId, attribute); + } else { + status = addComponentInstanceAttribute(containerComponent, componentInstanceId, attribute); + } + if (status != StorageOperationStatus.OK) { + log.debug("Failed to update instance attribute {} for instance {} error {} ", attribute, componentInstanceId, status); } return status; } public StorageOperationStatus updateInstanceCapabilityProperty(Component containerComponent, String componentInstanceId, - ComponentInstanceProperty property, - CapabilityDefinition capabilityDefinition) { + ComponentInstanceProperty property, CapabilityDefinition capabilityDefinition) { Optional fetchedCIOptional = containerComponent.getComponentInstanceById(componentInstanceId); - if(!fetchedCIOptional.isPresent()) { + if (!fetchedCIOptional.isPresent()) { return StorageOperationStatus.GENERAL_ERROR; } - Either getComponentRes = - getToscaFullElement(fetchedCIOptional.get().getComponentUid()); - if(getComponentRes.isRight()) { + Either getComponentRes = getToscaFullElement(fetchedCIOptional.get().getComponentUid()); + if (getComponentRes.isRight()) { return StorageOperationStatus.GENERAL_ERROR; } Optional componentOptional = isNodeServiceProxy(getComponentRes.left().value()); String propOwner; - if(!componentOptional.isPresent()) { + if (!componentOptional.isPresent()) { propOwner = componentInstanceId; } else { propOwner = fetchedCIOptional.get().getSourceModelUid(); } StorageOperationStatus status; StringBuilder sb = new StringBuilder(componentInstanceId); - sb.append(ModelConverter.CAP_PROP_DELIM).append(propOwner).append(ModelConverter.CAP_PROP_DELIM) - .append(capabilityDefinition.getType()).append(ModelConverter.CAP_PROP_DELIM).append(capabilityDefinition.getName()); + sb.append(ModelConverter.CAP_PROP_DELIM).append(propOwner).append(ModelConverter.CAP_PROP_DELIM).append(capabilityDefinition.getType()) + .append(ModelConverter.CAP_PROP_DELIM).append(capabilityDefinition.getName()); String capKey = sb.toString(); status = updateComponentInstanceCapabiltyProperty(containerComponent, componentInstanceId, capKey, property); if (status != StorageOperationStatus.OK) { - log.debug("Failed to update instance capability property {} for instance {} error {} ", property, - componentInstanceId, status); + log.debug("Failed to update instance capability property {} for instance {} error {} ", property, componentInstanceId, status); return status; } return StorageOperationStatus.OK; @@ -1384,199 +1900,289 @@ public class ToscaOperationFacade { } Resource resource = (Resource) component; ResourceTypeEnum resType = resource.getResourceType(); - if(resType.equals(ResourceTypeEnum.ServiceProxy)) { + if (resType.equals(ResourceTypeEnum.ServiceProxy)) { return Optional.of(component); } return Optional.empty(); } - public StorageOperationStatus associateDeploymentArtifactsToInstances(Map> instDeploymentArtifacts, String componentId, User user) { - - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + public StorageOperationStatus associateCapabilitiesToService(Map capabilities, String componentId) { + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()); + } + GraphVertex vertex = getVertexEither.left().value(); + if (MapUtils.isNotEmpty(capabilities)) { + Either associateElementToData = topologyTemplateOperation + .associateElementToData(vertex, VertexTypeEnum.CAPABILITIES, EdgeLabelEnum.CAPABILITIES, capabilities); + if (associateElementToData.isRight()) { + return associateElementToData.right().value(); + } + } + return StorageOperationStatus.OK; + } + public StorageOperationStatus associateRequirementsToService(Map requirements, String componentId) { + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getVertexEither.isRight()) { + log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); + return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()); + } + GraphVertex vertex = getVertexEither.left().value(); + if (MapUtils.isNotEmpty(requirements)) { + Either associateElementToData = topologyTemplateOperation + .associateElementToData(vertex, VertexTypeEnum.REQUIREMENTS, EdgeLabelEnum.REQUIREMENTS, requirements); + if (associateElementToData.isRight()) { + return associateElementToData.right().value(); + } } + return StorageOperationStatus.OK; + } + public StorageOperationStatus associateDeploymentArtifactsToInstances(Map> instDeploymentArtifacts, + Component component, User user) { + Either getVertexEither = janusGraphDao + .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse); + if (getVertexEither.isRight()) { + log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value()); + return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()); + } GraphVertex vertex = getVertexEither.left().value(); Map instArtMap = new HashMap<>(); if (instDeploymentArtifacts != null) { - MapArtifactDataDefinition artifactsMap; for (Entry> entry : instDeploymentArtifacts.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); - + 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); } } - + ModelConverter.setComponentInstancesDeploymentArtifactsToComponent(instArtMap, component); return topologyTemplateOperation.associateInstDeploymentArtifactsToComponent(vertex, instArtMap); - } - public StorageOperationStatus associateArtifactsToInstances(Map> instArtifacts, String componentId) { - + public StorageOperationStatus associateArtifactsToInstances(Map> instArtifacts, Component component) { Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { - log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); + log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value()); return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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()))); + Map artifacts = artList.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); artifactsMap = new MapArtifactDataDefinition(artifacts); - instArtMap.put(entry.getKey(), artifactsMap); } } - + ModelConverter.setComponentInstancesInformationalArtifactsToComponent(instArtMap, component); return topologyTemplateOperation.associateInstArtifactsToComponent(vertex, instArtMap); - } - public StorageOperationStatus associateInstAttributeToComponentToInstances(Map> instArttributes, String componentId) { - + public StorageOperationStatus associateInstAttributeToComponentToInstances(Map> instArttributes, + Component component) { Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { - log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); + log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value()); return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()); - } - GraphVertex vertex = getVertexEither.left().value(); - Map instAttr = new HashMap<>(); + Map instAttr = new HashMap<>(); if (instArttributes != null) { - - MapPropertiesDataDefinition attributesMap; - for (Entry> entry : instArttributes.entrySet()) { - attributesMap = new MapPropertiesDataDefinition(); - attributesMap.setMapToscaDataDefinition(entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e))); + MapAttributesDataDefinition attributesMap; + for (Entry> entry : instArttributes.entrySet()) { + final List value = entry.getValue(); + attributesMap = new MapAttributesDataDefinition(); + attributesMap.setMapToscaDataDefinition( + value.stream().map(AttributeDefinition::new).collect(Collectors.toMap(AttributeDefinition::getName, e -> e))); instAttr.put(entry.getKey(), attributesMap); } } - + setComponentInstanceAttributesOnComponent(component, instAttr); return topologyTemplateOperation.associateInstAttributeToComponent(vertex, instAttr); - } + // endregion + private void setComponentInstanceAttributesOnComponent(Component resource, Map instAttr) { + Map> componentInstancesAttributes = resource.getComponentInstancesAttributes(); + if (componentInstancesAttributes == null) { + componentInstancesAttributes = new HashMap<>(); + } + componentInstancesAttributes.putAll(ModelConverter.getComponentInstancesAttributes(instAttr)); + resource.setComponentInstancesAttributes(componentInstancesAttributes); + } - public StorageOperationStatus associateOrAddCalculatedCapReq(Map>> instCapabilties, Map>> instReg, String componentId) { + public StorageOperationStatus associateOrAddCalculatedCapReq(Map>> instCapabilties, + Map>> instReg, + Component component) { Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { - log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); + log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value()); return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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(CapabilityDataDefinition::new).collect(Collectors.toList()))); + mapToscaDataDefinition.put(instCapability.getKey(), new ListCapabilityDataDefinition( + instCapability.getValue().stream().map(CapabilityDataDefinition::new).collect(Collectors.toList()))); } - ComponentInstanceDataDefinition componentInstance = new ComponentInstanceDataDefinition(entry.getKey()); - MapListCapabilityDataDefinition capMap = nodeTemplateOperation.prepareCalculatedCapabiltyForNodeType(mapToscaDataDefinition, componentInstance); - - MapCapabilityProperty mapCapabilityProperty = ModelConverter.convertToMapOfMapCapabiltyProperties(caps, componentInstance.getUniqueId(), true); - + MapListCapabilityDataDefinition capMap = nodeTemplateOperation + .prepareCalculatedCapabiltyForNodeType(mapToscaDataDefinition, componentInstance); + MapCapabilityProperty mapCapabilityProperty = ModelConverter + .convertToMapOfMapCapabiltyProperties(caps, componentInstance.getUniqueId(), true); calcCapabilty.put(entry.getKey().getUniqueId(), capMap); calculatedCapabilitiesProperties.put(entry.getKey().getUniqueId(), mapCapabilityProperty); } } - 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(RequirementDataDefinition::new).collect(Collectors.toList()))); + mapToscaDataDefinition.put(instReq.getKey(), new ListRequirementDataDefinition( + instReq.getValue().stream().map(RequirementDataDefinition::new).collect(Collectors.toList()))); } + MapListRequirementDataDefinition reqMap = nodeTemplateOperation + .prepareCalculatedRequirementForNodeType(mapToscaDataDefinition, new ComponentInstanceDataDefinition(entry.getKey())); + String componentInstanceId = entry.getKey().getUniqueId(); + calcRequirements.put(componentInstanceId, reqMap); + } + } + StorageOperationStatus storageOperationStatus = topologyTemplateOperation + .associateOrAddCalcCapReqToComponent(vertex, calcRequirements, calcCapabilty, calculatedCapabilitiesProperties); + updateInstancesCapAndReqOnComponentFromDB(component); + return storageOperationStatus; + } - MapListRequirementDataDefinition capMap = nodeTemplateOperation.prepareCalculatedRequirementForNodeType(mapToscaDataDefinition, new ComponentInstanceDataDefinition(entry.getKey())); - - calcRequirements.put(entry.getKey().getUniqueId(), capMap); + public StorageOperationStatus updateCalculatedCapabilitiesRequirements( + final Map>> instCapabilties, + final Map>> instReg, + final Component component) { + StorageOperationStatus storageOperationStatus = StorageOperationStatus.OK; + if (instCapabilties != null) { + for (Entry>> entry : instCapabilties.entrySet()) { + final Map> cap = entry.getValue(); + for (List capabilityList : cap.values()) { + for (CapabilityDefinition capability : capabilityList) { + nodeTemplateOperation.updateComponentInstanceCapabilities(component.getUniqueId(), entry.getKey().getUniqueId(), capability); + } + } + } + } + if (instReg != null) { + for (Entry>> entry : instReg.entrySet()) { + final Map> req = entry.getValue(); + for (List requirementList : req.values()) { + for (RequirementDefinition requirement : requirementList) { + storageOperationStatus = nodeTemplateOperation.updateComponentInstanceRequirement(component.getUniqueId(), + entry.getKey().getUniqueId(), requirement); + if (storageOperationStatus != StorageOperationStatus.OK) { + return storageOperationStatus; + } + } + } } } + return storageOperationStatus; + } - return topologyTemplateOperation.associateOrAddCalcCapReqToComponent(vertex, calcRequirements, calcCapabilty, calculatedCapabilitiesProperties); + private void updateInstancesCapAndReqOnComponentFromDB(Component component) { + ComponentParametersView componentParametersView = new ComponentParametersView(true); + componentParametersView.setIgnoreCapabilities(false); + componentParametersView.setIgnoreRequirements(false); + componentParametersView.setIgnoreCapabiltyProperties(false); + componentParametersView.setIgnoreComponentInstances(false); + Either componentEither = getToscaElement(component.getUniqueId(), componentParametersView); + if (componentEither.isRight()) { + throw new StorageException(StorageOperationStatus.NOT_FOUND); + } + Component updatedComponent = componentEither.left().value(); + component.setCapabilities(updatedComponent.getCapabilities()); + component.setRequirements(updatedComponent.getRequirements()); + component.setComponentInstancesRelations(updatedComponent.getComponentInstancesRelations()); + component.setComponentInstances(updatedComponent.getComponentInstances()); } - private Either, StorageOperationStatus> getLatestVersionNonCheckoutServicesMetadataOnly(Map hasProps, Map hasNotProps) { + private Either, StorageOperationStatus> getLatestVersionNonCheckoutServicesMetadataOnly(Map hasProps, + Map hasNotProps, + String modelName) { List services = new ArrayList<>(); List states = new ArrayList<>(); // include props hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); - + if (modelName != null) { + hasProps.put(GraphPropertyEnum.MODEL, modelName); + } // exclude props states.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); hasNotProps.put(GraphPropertyEnum.STATE, states); hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true); - return fetchServicesByCriteria(services, hasProps, hasNotProps); + return fetchServicesByCriteria(services, hasProps, hasNotProps, modelName); } - private Either, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(boolean isAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType, VertexTypeEnum vertexType) { + private Either, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(final boolean isAbstract, + final ComponentTypeEnum componentTypeEnum, + final String internalComponentType, + final VertexTypeEnum vertexType, + final String modelName, + final boolean includeNormativeExtensionModels) { List services = null; Map hasProps = new EnumMap<>(GraphPropertyEnum.class); Map hasNotProps = new EnumMap<>(GraphPropertyEnum.class); - fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType); + fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType, modelName); Either, JanusGraphOperationStatus> getRes = janusGraphDao - .getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata); - if (getRes.isRight()) { - if (getRes.right().value().equals(JanusGraphOperationStatus.NOT_FOUND)) { - return Either.left(new ArrayList<>()); - } else { - return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value())); - } + .getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata, modelName, includeNormativeExtensionModels); + if (getRes.isRight() && !JanusGraphOperationStatus.NOT_FOUND.equals(getRes.right().value())) { + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value())); } // region -> Fetch non checked-out services if (internalComponentType != null && internalComponentType.toLowerCase().trim().equals(SERVICE) && VertexTypeEnum.NODE_TYPE == vertexType) { - Either, StorageOperationStatus> result = getLatestVersionNonCheckoutServicesMetadataOnly(new EnumMap<>(GraphPropertyEnum.class), new EnumMap<>(GraphPropertyEnum.class)); + Either, StorageOperationStatus> result = getLatestVersionNonCheckoutServicesMetadataOnly( + new EnumMap<>(GraphPropertyEnum.class), new EnumMap<>(GraphPropertyEnum.class), modelName); if (result.isRight()) { log.debug("Failed to fetch services for"); return Either.right(result.right().value()); } services = result.left().value(); - if (log.isTraceEnabled() && isEmpty(services)) + if (log.isTraceEnabled() && isEmpty(services)) { log.trace("No relevant services available"); + } } // endregion 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 light 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); + if (getRes.isLeft()) { + for (GraphVertex vertexComponent : getRes.left().value()) { + Either componentRes = topologyTemplateOperation + .getLightComponent(vertexComponent, componentTypeEnum, params); + if (componentRes.isRight()) { + log.debug("Failed to fetch light 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); + } } } if (CollectionUtils.isNotEmpty(services)) { @@ -1585,8 +2191,8 @@ public class ToscaOperationFacade { return Either.left(nonAbstractLatestComponents); } - public Either getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag, Boolean isHighest) { - + public Either getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag, + Boolean isHighest) { Either result; Map hasProperties = new EnumMap<>(GraphPropertyEnum.class); hasProperties.put(GraphPropertyEnum.UUID, componentUuid); @@ -1596,15 +2202,16 @@ public class ToscaOperationFacade { Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683 - Either, JanusGraphOperationStatus> getRes = janusGraphDao .getByCriteria(null, hasProperties, propertiesNotToMatch, parseFlag); if (getRes.isRight()) { result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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(); + 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; @@ -1612,8 +2219,7 @@ public class ToscaOperationFacade { public Either getComponentMetadata(String componentId) { Either result; - Either getRes = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.ParseMetadata); + Either getRes = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata); if (getRes.isRight()) { result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value())); } else { @@ -1623,9 +2229,10 @@ public class ToscaOperationFacade { return result; } - public Either, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract, ComponentTypeEnum componentTypeEnum, - String internalComponentType, List componentUids) { - + public Either, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract, + ComponentTypeEnum componentTypeEnum, + String internalComponentType, + List componentUids) { List components = new ArrayList<>(); if (componentUids == null) { Either, StorageOperationStatus> componentUidsRes = getComponentUids(isAbstract, componentTypeEnum, internalComponentType); @@ -1641,7 +2248,8 @@ public class ToscaOperationFacade { componentParametersView.setIgnoreCapabilities(false); componentParametersView.setIgnoreRequirements(false); } - Either getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum).getLightComponent(componentUid, componentTypeEnum, componentParametersView); + Either getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum) + .getLightComponent(componentUid, componentTypeEnum, componentParametersView); if (getToscaElementRes.isRight()) { log.debug("Failed to fetch resource for error is {}", getToscaElementRes.right().value()); return Either.right(getToscaElementRes.right().value()); @@ -1665,9 +2273,10 @@ public class ToscaOperationFacade { component.setNormalizedName(null); } - private Either, StorageOperationStatus> getComponentUids(boolean isAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType) { - - Either, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, componentTypeEnum, internalComponentType); + private Either, StorageOperationStatus> getComponentUids(boolean isAbstract, ComponentTypeEnum componentTypeEnum, + String internalComponentType) { + Either, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, componentTypeEnum, + internalComponentType, null, false); if (getToscaElementsRes.isRight()) { return Either.right(getToscaElementsRes.right().value()); } @@ -1676,9 +2285,7 @@ public class ToscaOperationFacade { if (collection == null) { componentUids = new ArrayList<>(); } else { - componentUids = collection.stream() - .map(Component::getUniqueId) - .collect(Collectors.toList()); + componentUids = collection.stream().map(Component::getUniqueId).collect(Collectors.toList()); } return Either.left(componentUids); } @@ -1691,7 +2298,8 @@ public class ToscaOperationFacade { return componentParametersView; } - public Either validateComponentNameExists(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) { + public Either validateComponentNameExists(String name, ResourceTypeEnum resourceType, + ComponentTypeEnum componentType) { Either result = validateComponentNameUniqueness(name, resourceType, componentType); if (result.isLeft()) { result = Either.left(!result.left().value()); @@ -1699,47 +2307,79 @@ public class ToscaOperationFacade { return result; } - public Either validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) { - VertexTypeEnum vertexType = ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE; + public Either validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType, + ComponentTypeEnum 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, JanusGraphOperationStatus> vertexEither = janusGraphDao - .getByCriteria(vertexType, properties, JsonParseFlagEnum.NoParse); + .getByCriteria(getVertexTypeEnum(resourceType), propertiesToMatch(normalizedName, componentType), JsonParseFlagEnum.NoParse); if (vertexEither.isRight() && vertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) { log.debug("failed to get vertex from graph with property normalizedName: {}", normalizedName); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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); + return Either.left(CollectionUtils.isEmpty(vertexEither.isLeft() ? vertexEither.left().value() : null)); + } + + public Either validateComponentNameAndModelExists(final String resourceName, final String model, + final ResourceTypeEnum resourceType, + final ComponentTypeEnum componentType) { + Either result = validateComponentNameAndModelUniqueness(resourceName, model, resourceType, componentType); + if (result.isLeft()) { + result = Either.left(!result.left().value()); } + return result; } - private void fillNodeTypePropsMap(Map hasProps, Map hasNotProps, String internalComponentType) { - switch (internalComponentType.toLowerCase()) { - case "vf": - case "cvfc": - hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, Arrays.asList(ResourceTypeEnum.VFCMT.name(), ResourceTypeEnum.Configuration.name())); - break; - case SERVICE: - case "pnf": - case "cr": - hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, Arrays.asList(ResourceTypeEnum.VFC.name(), ResourceTypeEnum.VFCMT.name())); - break; - case "vl": - hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VL.name()); - break; - default: - break; + private Either validateComponentNameAndModelUniqueness(final String resourceName, final String modelName, + final ResourceTypeEnum resourceType, + final ComponentTypeEnum componentType) { + final String normalizedName = ValidationUtils.normaliseComponentName(resourceName); + final Either, JanusGraphOperationStatus> vertexEither = janusGraphDao + .getByCriteria(getVertexTypeEnum(resourceType), propertiesToMatch(normalizedName, componentType), null, null, JsonParseFlagEnum.NoParse, + modelName); + if (vertexEither.isRight() && vertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) { + log.debug("failed to get vertex from graph with property normalizedName: {} and model: {}", normalizedName, modelName); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value())); + } + return Either.left(CollectionUtils.isEmpty(vertexEither.isLeft() ? vertexEither.left().value().stream() + .collect(Collectors.toList()) : null)); + } + + private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) { + return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : TOPOLOGY_TEMPLATE; + } + + private Map propertiesToMatch(final String normalizedName, final ComponentTypeEnum componentType) { + final Map properties = new EnumMap<>(GraphPropertyEnum.class); + properties.put(GraphPropertyEnum.NORMALIZED_NAME, normalizedName); + properties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); + return properties; + } + + private void fillNodeTypePropsMap(final Map hasProps, final Map hasNotProps, + final String internalComponentType, String modelName) { + final Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration(); + final List allowedTypes; + if (ComponentTypeEnum.SERVICE.getValue().equalsIgnoreCase(internalComponentType)) { + allowedTypes = containerInstanceTypesData.getServiceAllowedList(modelName); + } else { + final ResourceTypeEnum resourceType = ResourceTypeEnum.getTypeIgnoreCase(internalComponentType); + allowedTypes = containerInstanceTypesData.getComponentAllowedList(ComponentTypeEnum.RESOURCE, resourceType); + } + final List allResourceTypes = configuration.getResourceTypes(); + if (allowedTypes == null) { + hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, allResourceTypes); + return; + } + if (ResourceTypeEnum.VL.getValue().equalsIgnoreCase(internalComponentType)) { + hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, allowedTypes); + } else { + final List notAllowedTypes = allResourceTypes.stream().filter(s -> !allowedTypes.contains(s)).collect(Collectors.toList()); + hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, notAllowedTypes); } } - private void fillTopologyTemplatePropsMap(Map hasProps, Map hasNotProps, ComponentTypeEnum componentTypeEnum) { + private void fillTopologyTemplatePropsMap(Map hasProps, Map hasNotProps, + ComponentTypeEnum componentTypeEnum) { switch (componentTypeEnum) { case RESOURCE: hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); @@ -1753,16 +2393,17 @@ public class ToscaOperationFacade { hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name()); } - private void fillPropsMap(Map hasProps, Map hasNotProps, String internalComponentType, ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType) { + private void fillPropsMap(Map hasProps, Map hasNotProps, String internalComponentType, + ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType, String modelName) { hasNotProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name()); - hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, 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); + fillNodeTypePropsMap(hasProps, hasNotProps, internalComponentType, modelName); } } else { fillTopologyTemplatePropsMap(hasProps, hasNotProps, componentTypeEnum); @@ -1774,27 +2415,33 @@ public class ToscaOperationFacade { if (ComponentTypeEnum.RESOURCE == componentTypeEnum) { internalVertexTypes.add(VertexTypeEnum.NODE_TYPE); } - if (ComponentTypeEnum.SERVICE == componentTypeEnum || SERVICE.equalsIgnoreCase(internalComponentType)) { - internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE); + if (ComponentTypeEnum.SERVICE == componentTypeEnum || SERVICE.equalsIgnoreCase(internalComponentType) || VF.equalsIgnoreCase( + internalComponentType)) { + internalVertexTypes.add(TOPOLOGY_TEMPLATE); } return internalVertexTypes; } - public Either, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType) { + public Either, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract, + final ComponentTypeEnum componentTypeEnum, + final String internalComponentType, + final String modelName, + final boolean includeNormativeExtensionModels) { List internalVertexTypes = getInternalVertexTypes(componentTypeEnum, internalComponentType); List result = new ArrayList<>(); for (VertexTypeEnum vertexType : internalVertexTypes) { - Either, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract, componentTypeEnum, internalComponentType, vertexType); + Either, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract, + componentTypeEnum, internalComponentType, vertexType, modelName, includeNormativeExtensionModels); if (listByVertexType.isRight()) { return listByVertexType; } result.addAll(listByVertexType.left().value()); } return Either.left(result); - } - private Either, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid, Map additionalPropertiesToMatch) { + private Either, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid, + Map additionalPropertiesToMatch) { Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); if (additionalPropertiesToMatch != null) { propertiesToMatch.putAll(additionalPropertiesToMatch); @@ -1805,10 +2452,8 @@ public class ToscaOperationFacade { public Either getComponentByUuidAndVersion(String componentUuid, String version) { Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); - propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid); propertiesToMatch.put(GraphPropertyEnum.VERSION, version); - Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); Either, JanusGraphOperationStatus> vertexEither = janusGraphDao @@ -1816,65 +2461,52 @@ public class ToscaOperationFacade { if (vertexEither.isRight()) { return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value())); } - List vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null; if (vertexList == null || vertexList.isEmpty() || vertexList.size() > 1) { return Either.right(StorageOperationStatus.NOT_FOUND); } - return getToscaElementByOperation(vertexList.get(0)); } - public Either, StorageOperationStatus> getComponentListByUuid(String componentUuid, Map additionalPropertiesToMatch) { - + 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); propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683 - Either, JanusGraphOperationStatus> vertexEither = janusGraphDao .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); - if (vertexEither.isRight()) { log.debug("Couldn't fetch metadata for component with uuid {}, error: {}", componentUuid, vertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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()) { + if (Boolean.TRUE.equals(component.isHighestVersion())) { LinkedList highestComponent = new LinkedList<>(); highestComponent.add(component); return Either.left(highestComponent); } } } - return Either.left(latestComponents); } @@ -1888,30 +2520,26 @@ public class ToscaOperationFacade { return getLatestComponentByUuid(componentUuid, null); } - public Either getLatestComponentByUuid(String componentUuid, Map propertiesToMatch) { - + public Either getLatestComponentByUuid(String componentUuid, + Map propertiesToMatch) { Either, StorageOperationStatus> latestVersionListEither = getLatestComponentListByUuid(componentUuid, propertiesToMatch); - 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(); - + 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); @@ -1919,10 +2547,8 @@ public class ToscaOperationFacade { propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - Either, JanusGraphOperationStatus> getResourcesRes = janusGraphDao .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.convertJanusGraphStatusToStorageStatus(getResourcesRes.right().value())); @@ -1938,20 +2564,17 @@ public class ToscaOperationFacade { return Either.left(resources); } - public Either getLatestByNameAndVersion(String name, String version, JsonParseFlagEnum parseFlag) { + public Either getLatestByNameAndVersion(String name, String version, + JsonParseFlagEnum parseFlag, String model) { 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, JanusGraphOperationStatus> getResourceRes = janusGraphDao - .getByCriteria(null, hasProperties, hasNotProperties, parseFlag); + .getByCriteria(null, hasProperties, hasNotProperties, parseFlag, model); if (getResourceRes.isRight()) { JanusGraphOperationStatus status = getResourceRes.right().value(); log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status); @@ -1961,11 +2584,13 @@ public class ToscaOperationFacade { return getToscaElementByOperation(getResourceRes.left().value().get(0)); } - public Either getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName) { + public Either getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, + String systemName) { return getLatestComponentByCsarOrName(componentType, csarUUID, systemName, JsonParseFlagEnum.ParseAll); } - public Either getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName, JsonParseFlagEnum parseFlag) { + public Either getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, + String systemName, JsonParseFlagEnum parseFlag) { Map props = new EnumMap<>(GraphPropertyEnum.class); Map propsHasNot = new EnumMap<>(GraphPropertyEnum.class); props.put(GraphPropertyEnum.CSAR_UUID, csarUUID); @@ -1974,7 +2599,6 @@ public class ToscaOperationFacade { props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); } propsHasNot.put(GraphPropertyEnum.IS_DELETED, true); - GraphVertex resourceMetadataData = null; List resourceMetadataDataList = null; Either, JanusGraphOperationStatus> byCsar = janusGraphDao @@ -1983,21 +2607,22 @@ public class ToscaOperationFacade { if (JanusGraphOperationStatus.NOT_FOUND == byCsar.right().value()) { // Fix Defect DE256036 if (StringUtils.isEmpty(systemName)) { - return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus( - JanusGraphOperationStatus.NOT_FOUND)); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.NOT_FOUND)); } - props.clear(); props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); props.put(GraphPropertyEnum.SYSTEM_NAME, systemName); Either, JanusGraphOperationStatus> bySystemname = janusGraphDao .getByCriteria(null, props, JsonParseFlagEnum.ParseMetadata); if (bySystemname.isRight()) { - log.debug("getLatestResourceByCsarOrName - Failed to find by system name {} error {} ", systemName, bySystemname.right().value()); + log.debug("getLatestResourceByCsarOrName - Failed to find by system name {} error {} ", systemName, + bySystemname.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(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()); + 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(); @@ -2015,18 +2640,22 @@ public class ToscaOperationFacade { 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); + final Object csarUuid = resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID); + if (csarUuid != null && !csarUuid.equals(csarUUID)) { + log.debug("getLatestResourceByCsarOrName - same system name {} but different csarUUID. exist {} and new {} ", systemName, + csarUuid, csarUUID); // correct error will be returned from create flow. with all + // correct audit records!!!!! return Either.right(StorageOperationStatus.NOT_FOUND); } - return getToscaElement((String) resourceMetadataData.getUniqueId()); + return getToscaElement(resourceMetadataData.getUniqueId()); } } 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()); + 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) { @@ -2048,27 +2677,24 @@ public class ToscaOperationFacade { return null; } - public Either validateToscaResourceNameExtends(String templateNameCurrent, String templateNameExtends) { - + public Either validateToscaResourceNameExtends(String templateNameCurrent, String templateNameExtends, + String model) { String currentTemplateNameChecked = templateNameExtends; - while (currentTemplateNameChecked != null && !currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) { - Either latestByToscaResourceName = getLatestByToscaResourceName(currentTemplateNameChecked); - + Either latestByToscaResourceName = getLatestByToscaResourceName(currentTemplateNameChecked, model); if (latestByToscaResourceName.isRight()) { - return latestByToscaResourceName.right().value() == StorageOperationStatus.NOT_FOUND ? Either.left(false) : Either.right(latestByToscaResourceName.right().value()); + 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); + return (currentTemplateNameChecked != null && currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) ? Either.left(true) + : Either.left(false); } public Either, StorageOperationStatus> fetchMetaDataByResourceType(String resourceType, ComponentParametersView filterBy) { @@ -2079,18 +2705,14 @@ public class ToscaOperationFacade { propsHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); Either, JanusGraphOperationStatus> resourcesByTypeEither = janusGraphDao .getByCriteria(null, props, propsHasNotToMatch, JsonParseFlagEnum.ParseMetadata); - if (resourcesByTypeEither.isRight()) { return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resourcesByTypeEither.right().value())); } - List vertexList = resourcesByTypeEither.left().value(); List components = new ArrayList<>(); - for (GraphVertex vertex : vertexList) { components.add(getToscaElementByOperation(vertex, filterBy).left().value()); } - return Either.left(components); } @@ -2099,7 +2721,8 @@ public class ToscaOperationFacade { } public Either updateDistributionStatus(Service service, User user, DistributionStatusEnum distributionStatus) { - Either updateDistributionStatus = topologyTemplateOperation.updateDistributionStatus(service.getUniqueId(), user, distributionStatus); + Either updateDistributionStatus = topologyTemplateOperation + .updateDistributionStatus(service.getUniqueId(), user, distributionStatus); if (updateDistributionStatus.isRight()) { return Either.right(updateDistributionStatus.right().value()); } @@ -2110,12 +2733,10 @@ public class ToscaOperationFacade { } public Either updateComponentLastUpdateDateOnGraph(Component component) { - Either result = null; GraphVertex serviceVertex; Either updateRes = null; - Either getRes = janusGraphDao - .getVertexById(component.getUniqueId(), JsonParseFlagEnum.ParseMetadata); + Either getRes = janusGraphDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.ParseMetadata); if (getRes.isRight()) { JanusGraphOperationStatus status = getRes.right().value(); log.error("Failed to fetch component {}. status is {}", component.getUniqueId(), status); @@ -2144,29 +2765,24 @@ public class ToscaOperationFacade { 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) { - + 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()) { + if (CollectionUtils.isNotEmpty(distStatus)) { for (DistributionStatusEnum state : distStatus) { propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name()); - Either, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch); + Either, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch, + propertiesNotToMatch, null); if (fetchServicesByCriteria.isRight()) { return fetchServicesByCriteria; } else { @@ -2175,24 +2791,34 @@ public class ToscaOperationFacade { } return Either.left(servicesAll); } else { - return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch); + return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch, null); } } - private Either, StorageOperationStatus> fetchServicesByCriteria(List servicesAll, Map propertiesToMatch, Map propertiesNotToMatch) { - Either, JanusGraphOperationStatus> getRes = janusGraphDao - .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); + private Either, StorageOperationStatus> fetchServicesByCriteria(List servicesAll, + Map propertiesToMatch, + Map propertiesNotToMatch, + String modelName) { + Either, JanusGraphOperationStatus> getRes; + if (StringUtils.isEmpty(modelName)) { + getRes = janusGraphDao.getByCriteria(TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll); + } else { + getRes = janusGraphDao.getByCriteria(TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll, modelName); + } if (getRes.isRight()) { if (getRes.right().value() != JanusGraphOperationStatus.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()); + 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.convertJanusGraphStatusToStorageStatus(getRes.right().value())); } } else { - for (GraphVertex vertex : getRes.left().value()) { - Either getServiceRes = topologyTemplateOperation.getLightComponent(vertex, ComponentTypeEnum.SERVICE, new ComponentParametersView(true)); - + for (final GraphVertex vertex : getRes.left().value()) { + 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()); + 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())); @@ -2206,16 +2832,19 @@ public class ToscaOperationFacade { janusGraphDao.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()))); - + 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 addInformationalArtifactsToInstance(String componentId, ComponentInstance componentInstance, Map artifacts) { + public StorageOperationStatus addInformationalArtifactsToInstance(String componentId, ComponentInstance componentInstance, + Map artifacts) { StorageOperationStatus status = StorageOperationStatus.OK; if (MapUtils.isNotEmpty(artifacts)) { - Map instDeplArtifacts = artifacts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); + Map instDeplArtifacts = artifacts.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); status = nodeTemplateOperation.addInformationalArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts); } return status; @@ -2229,160 +2858,258 @@ public class ToscaOperationFacade { return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances); } - public Either addPropertyToComponent(String propertyName, - PropertyDefinition newPropertyDefinition, - Component component) { - newPropertyDefinition.setName(propertyName); - - StorageOperationStatus status = getToscaElementOperation(component) - .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME); - if (status != StorageOperationStatus.OK) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the component {}. Status is {}. ", propertyName, component.getName(), status); + public Either addPropertyToComponent(PropertyDefinition newPropertyDefinition, + Component component) { + final String propertyName = newPropertyDefinition.getName(); + StorageOperationStatus status = getToscaElementOperation(component) + .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, + JsonPresentationFields.NAME); + if (status != StorageOperationStatus.OK) { + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the component {}. Status is {}. ", propertyName, + component.getName(), status); return Either.right(status); - } - + } ComponentParametersView filter = new ComponentParametersView(true); filter.setIgnoreProperties(false); filter.setIgnoreInputs(false); Either getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter); if (getUpdatedComponentRes.isRight()) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value()); + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(), + getUpdatedComponentRes.right().value()); return Either.right(status); } - PropertyDefinition newProperty = null; - List properties = - (getUpdatedComponentRes.left().value()).getProperties(); + List properties = (getUpdatedComponentRes.left().value()).getProperties(); if (CollectionUtils.isNotEmpty(properties)) { - Optional propertyOptional = properties.stream().filter( - propertyEntry -> propertyEntry.getName().equals(propertyName)).findAny(); + Optional propertyOptional = properties.stream().filter(propertyEntry -> propertyEntry.getName().equals(propertyName)) + .findAny(); if (propertyOptional.isPresent()) { newProperty = propertyOptional.get(); } } if (newProperty == null) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the component {}. Status is {}. ", propertyName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND); + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the component {}. Status is {}. ", + propertyName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND); return Either.right(StorageOperationStatus.NOT_FOUND); } - return Either.left(newProperty); - } + } + + public Either addInputToComponent(String inputName, InputDefinition newInputDefinition, + Component component) { + newInputDefinition.setName(inputName); + StorageOperationStatus status = getToscaElementOperation(component) + .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition, + JsonPresentationFields.NAME); + if (status != StorageOperationStatus.OK) { + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the input {} to the component {}. Status is {}. ", inputName, + component.getName(), status); + return Either.right(status); + } + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreProperties(false); + filter.setIgnoreInputs(false); + Either getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter); + if (getUpdatedComponentRes.isRight()) { + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(), + getUpdatedComponentRes.right().value()); + return Either.right(status); + } + InputDefinition newInput = null; + List inputs = (getUpdatedComponentRes.left().value()).getInputs(); + if (CollectionUtils.isNotEmpty(inputs)) { + Optional inputOptional = inputs.stream().filter(inputEntry -> inputEntry.getName().equals(inputName)).findAny(); + if (inputOptional.isPresent()) { + newInput = inputOptional.get(); + } + } + if (newInput == null) { + CommonUtility + .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added input {} " + "on the component {}. Status is {}. ", inputs, + component.getUniqueId(), StorageOperationStatus.NOT_FOUND); + return Either.right(StorageOperationStatus.NOT_FOUND); + } + return Either.left(newInput); + } - public StorageOperationStatus deletePropertyOfComponent(Component component, String propertyName) { - return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, propertyName, JsonPresentationFields.NAME); - } + public StorageOperationStatus deletePropertyOfComponent(Component component, String propertyName) { + return getToscaElementOperation(component) + .deleteToscaDataElement(component.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 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); + return getToscaElementOperation(resource) + .deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputName, JsonPresentationFields.NAME); + } + + public StorageOperationStatus deleteOutputOfResource(final Component resource, final String outputName) { + return getToscaElementOperation(resource) + .deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputName, JsonPresentationFields.NAME); } /** * Deletes a data type from a component. - * @param component the container which has the data type + * + * @param component the container which has the data type * @param dataTypeName the data type name to be deleted * @return Operation result. */ public StorageOperationStatus deleteDataTypeOfComponent(Component component, String dataTypeName) { - return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeName, JsonPresentationFields.NAME); - } - - public Either updatePropertyOfComponent(Component component, - PropertyDefinition newPropertyDefinition) { - - Either getUpdatedComponentRes = null; - Either result = null; - StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.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(), component.getName(), status); - result = Either.right(status); - } - if (result == null) { - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreProperties(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 = (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(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND); - result = Either.right(StorageOperationStatus.NOT_FOUND); - } - } - return result; - } - - - - public Either addAttributeOfResource(Component component, PropertyDefinition newAttributeDef) { + return getToscaElementOperation(component) + .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeName, + JsonPresentationFields.NAME); + } + public Either updatePropertyOfComponent(Component component, + PropertyDefinition newPropertyDefinition) { Either getUpdatedComponentRes = null; Either result = null; + StorageOperationStatus status = getToscaElementOperation(component) + .updateToscaDataOfToscaElement(component.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(), + component.getName(), status); + result = Either.right(status); + } + if (result == null) { + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreProperties(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 = (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(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND); + result = Either.right(StorageOperationStatus.NOT_FOUND); + } + } + return result; + } + + public Either updateAttributeOfComponent(Component component, + AttributeDefinition newPropertyDefinition) { + Either getUpdatedComponentRes = null; + Either result = null; + StorageOperationStatus status = getToscaElementOperation(component) + .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newPropertyDefinition, + JsonPresentationFields.NAME); + if (status != StorageOperationStatus.OK) { + CommonUtility + .addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newPropertyDefinition.getName(), + component.getName(), status); + result = Either.right(status); + } + if (result == null) { + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreProperties(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 = (getUpdatedComponentRes.left().value()).getAttributes().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(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND); + result = Either.right(StorageOperationStatus.NOT_FOUND); + } + } + return result; + } + + public Either addAttributeOfResource(Component component, AttributeDefinition 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); + newAttributeDef.setOwnerId(component.getUniqueId()); } - - StorageOperationStatus status = getToscaElementOperation(component).addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME); + 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); + 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); + filter.setIgnoreAttributes(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()); + 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(); + 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); + 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) { - + public Either updateAttributeOfResource(Component component, AttributeDefinition newAttributeDef) { Either getUpdatedComponentRes = null; - Either result = null; - StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME); + 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); + 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); + filter.setIgnoreAttributes(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()); + 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(); + 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); + 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); } } @@ -2390,12 +3117,14 @@ public class ToscaOperationFacade { } 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); + 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); + 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) { @@ -2403,16 +3132,19 @@ public class ToscaOperationFacade { 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()); + 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(); + 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); + 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); } } @@ -2427,76 +3159,116 @@ public class ToscaOperationFacade { * @param componentInstanceId - id * @return - successfull/failed status **/ - public Either cleanAndAddGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, String componentInstanceId) { + public Either cleanAndAddGroupInstancesToComponentInstance(Component containerComponent, + ComponentInstance componentInstance, + String componentInstanceId) { String uniqueId = componentInstance.getUniqueId(); - StorageOperationStatus status = nodeTemplateOperation.deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS, uniqueId); + StorageOperationStatus status = nodeTemplateOperation + .deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, + uniqueId); if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete group instances for container {}. error {] ", componentInstanceId, status); + CommonUtility + .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete group instances for container {}. error {] ", componentInstanceId, status); return Either.right(status); } if (componentInstance.getGroupInstances() != null) { status = addGroupInstancesToComponentInstance(containerComponent, componentInstance, componentInstance.getGroupInstances()); if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) { - CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add group instances for container {}. error {] ", componentInstanceId, status); + CommonUtility + .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add group instances for container {}. error {] ", componentInstanceId, + status); return Either.right(status); } } return Either.left(status); } - public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List groups, Map> groupInstancesArtifacts) { + public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, + List groups, + Map> groupInstancesArtifacts) { return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groups, groupInstancesArtifacts); } - public Either, StorageOperationStatus> updateGroupsOnComponent(Component component, List updatedGroups) { - return groupsOperation.updateGroups(component, updatedGroups, true); + public Either, StorageOperationStatus> updateGroupsOnComponent(Component component, + List updatedGroups) { + return groupsOperation.updateGroups(component, updatedGroups, PromoteVersionEnum.MINOR); } - public Either, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, String instanceId, List updatedGroupInstances) { + public Either, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, String instanceId, + List updatedGroupInstances) { return groupsOperation.updateGroupInstances(component, instanceId, updatedGroupInstances); } - public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List groupInstances) { + public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, + List groupInstances) { return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groupInstances); } - public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance, Map deploymentArtifacts) { + public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance, + Map deploymentArtifacts) { return nodeTemplateOperation.addDeploymentArtifactsToComponentInstance(containerComponent, componentInstance, deploymentArtifacts); } - public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) { + public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId, + ComponentInstanceProperty property) { return nodeTemplateOperation.updateComponentInstanceProperty(containerComponent, componentInstanceId, property); } - public StorageOperationStatus updateComponentInstanceProperties(Component containerComponent, String componentInstanceId, List properties) { + public StorageOperationStatus updateComponentInstanceProperties(Component containerComponent, String componentInstanceId, + List properties) { return nodeTemplateOperation.updateComponentInstanceProperties(containerComponent, componentInstanceId, properties); } + public StorageOperationStatus updateComponentInstanceAttributes(final Component containerComponent, final String componentInstanceId, + final List attributes) { + return nodeTemplateOperation.updateComponentInstanceAttributes(containerComponent, componentInstanceId, attributes); + } - public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) { + public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId, + ComponentInstanceProperty property) { return nodeTemplateOperation.addComponentInstanceProperty(containerComponent, componentInstanceId, property); } - public StorageOperationStatus updateComponentInstanceAttribute(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property){ - return nodeTemplateOperation.updateComponentInstanceAttribute(containerComponent, componentInstanceId, property); + public StorageOperationStatus updateComponentInstanceAttribute(final Component containerComponent, final String componentInstanceId, + final ComponentInstanceAttribute attribute) { + return nodeTemplateOperation.updateComponentInstanceAttribute(containerComponent, componentInstanceId, attribute); } - public StorageOperationStatus addComponentInstanceAttribute(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property){ - return nodeTemplateOperation.addComponentInstanceAttribute(containerComponent, componentInstanceId, property); + public StorageOperationStatus addComponentInstanceAttribute(Component containerComponent, String componentInstanceId, + ComponentInstanceAttribute attribute) { + return nodeTemplateOperation.addComponentInstanceAttribute(containerComponent, componentInstanceId, attribute); } - public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) { + public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId, + ComponentInstanceInput property) { return nodeTemplateOperation.updateComponentInstanceInput(containerComponent, componentInstanceId, property); } - public StorageOperationStatus updateComponentInstanceInputs(Component containerComponent, String componentInstanceId, List instanceInputs) { + public StorageOperationStatus updateComponentInstanceOutput(Component containerComponent, String componentInstanceId, + ComponentInstanceOutput property) { + return nodeTemplateOperation.updateComponentInstanceOutput(containerComponent, componentInstanceId, property); + } + + public StorageOperationStatus updateComponentInstanceInputs(Component containerComponent, String componentInstanceId, + List instanceInputs) { return nodeTemplateOperation.updateComponentInstanceInputs(containerComponent, componentInstanceId, instanceInputs); } - public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) { + public StorageOperationStatus updateComponentInstanceOutputs(Component containerComponent, String componentInstanceId, + List instanceInputs) { + return nodeTemplateOperation.updateComponentInstanceOutputs(containerComponent, componentInstanceId, instanceInputs); + } + + public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId, + ComponentInstanceInput property) { return nodeTemplateOperation.addComponentInstanceInput(containerComponent, componentInstanceId, property); } + public StorageOperationStatus addComponentInstanceOutput(Component containerComponent, String componentInstanceId, + ComponentInstanceOutput property) { + return nodeTemplateOperation.addComponentInstanceOutput(containerComponent, componentInstanceId, property); + } + public void setNodeTypeOperation(NodeTypeOperation nodeTypeOperation) { this.nodeTypeOperation = nodeTypeOperation; } @@ -2505,61 +3277,130 @@ public class ToscaOperationFacade { this.topologyTemplateOperation = topologyTemplateOperation; } - public StorageOperationStatus deleteComponentInstanceInputsFromTopologyTemplate(Component containerComponent, List inputsToDelete) { - return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.INPUTS, inputsToDelete.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList())); + public StorageOperationStatus deleteComponentInstanceInputsFromTopologyTemplate(Component containerComponent, + List inputsToDelete) { + return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.INPUTS, + inputsToDelete.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList())); } - public StorageOperationStatus updateComponentInstanceCapabiltyProperty(Component containerComponent, String componentInstanceUniqueId, String capabilityUniqueId, ComponentInstanceProperty property) { - return nodeTemplateOperation.updateComponentInstanceCapabilityProperty(containerComponent, componentInstanceUniqueId, capabilityUniqueId, property); + public StorageOperationStatus deleteComponentInstanceOutputsFromTopologyTemplate(final Component containerComponent, + final List outputsToDelete) { + return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.OUTPUTS, + outputsToDelete.stream().map(AttributeDataDefinition::getName).collect(Collectors.toList())); + } + + public StorageOperationStatus updateComponentInstanceCapabiltyProperty(Component containerComponent, String componentInstanceUniqueId, + String capabilityPropertyKey, ComponentInstanceProperty property) { + return nodeTemplateOperation + .updateComponentInstanceCapabilityProperty(containerComponent, componentInstanceUniqueId, capabilityPropertyKey, property); } public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceUniqueId) { - return convertComponentInstanceProperties(containerComponent, componentInstanceUniqueId) - .map(instanceCapProps -> topologyTemplateOperation.updateComponentInstanceCapabilityProperties(containerComponent, componentInstanceUniqueId, instanceCapProps)) - .orElse(StorageOperationStatus.NOT_FOUND); + return convertComponentInstanceProperties(containerComponent, componentInstanceUniqueId).map(instanceCapProps -> topologyTemplateOperation + .updateComponentInstanceCapabilityProperties(containerComponent, componentInstanceUniqueId, instanceCapProps)) + .orElse(StorageOperationStatus.NOT_FOUND); + } + + public StorageOperationStatus updateComponentInstanceRequirement(String containerComponentId, String componentInstanceUniqueId, + RequirementDataDefinition requirementDataDefinition) { + return nodeTemplateOperation.updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition); + } + + public CapabilityDataDefinition updateComponentInstanceCapability(final String containerComponentId, final String componentInstanceUniqueId, + final CapabilityDataDefinition capabilityDataDefinition) { + + return nodeTemplateOperation.updateComponentInstanceCapabilities(containerComponentId, componentInstanceUniqueId, capabilityDataDefinition); } public StorageOperationStatus updateComponentInstanceInterfaces(Component containerComponent, String componentInstanceUniqueId) { - MapInterfaceDataDefinition mapInterfaceDataDefinition = - convertComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId); - return topologyTemplateOperation - .updateComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId, mapInterfaceDataDefinition); + MapInterfaceDataDefinition mapInterfaceDataDefinition = convertComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId); + return topologyTemplateOperation.updateComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId, mapInterfaceDataDefinition); + } + + public StorageOperationStatus updateComponentInterfaces(final Component component, final String componentInterfaceUpdatedKey) { + MapInterfaceDataDefinition mapInterfaceDataDefinition = convertComponentInterfaces(component.getInterfaces()); + return topologyTemplateOperation.updateComponentInterfaces(component.getUniqueId(), mapInterfaceDataDefinition, componentInterfaceUpdatedKey); + } + + public Either addInterfaceToComponent(final String interfaceName, + final InterfaceDefinition interfaceDefinition, + final Component component) { + + final boolean match = component.getInterfaces().keySet().stream().anyMatch(s -> s.equals(interfaceName)); + StorageOperationStatus status; + final ToscaElementOperation toscaElementOperation = getToscaElementOperation(component); + if (match) { + status = toscaElementOperation.updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS, + VertexTypeEnum.INTERFACE_ARTIFACTS, interfaceDefinition, JsonPresentationFields.TYPE); + } else { + status = toscaElementOperation.addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS, + VertexTypeEnum.INTERFACE_ARTIFACTS, interfaceDefinition, JsonPresentationFields.TYPE); + } + + if (status != StorageOperationStatus.OK) { + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the interface {} to the component {}. Status is {}. ", + interfaceName, component.getName(), status); + return Either.right(status); + } + final ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreInterfaces(false); + filter.setIgnoreInterfaceInstances(false); + final Either getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter); + if (getUpdatedComponentRes.isRight()) { + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", + component.getUniqueId(), getUpdatedComponentRes.right().value()); + return Either.right(getUpdatedComponentRes.right().value()); + } + InterfaceDefinition newInterfaceDefinition = null; + final Map interfaces = (getUpdatedComponentRes.left().value()).getInterfaces(); + if (MapUtils.isNotEmpty(interfaces)) { + final Optional interfaceNameOptional = interfaces.keySet().stream().filter(key -> key.equals(interfaceName)).findAny(); + if (interfaceNameOptional.isPresent()) { + newInterfaceDefinition = interfaces.get(interfaceNameOptional.get()); + } + } + if (newInterfaceDefinition == null) { + CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added interface {} on the component {}. Status is {}. ", + interfaceName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND); + return Either.right(StorageOperationStatus.NOT_FOUND); + } + return Either.left(newInterfaceDefinition); } - public StorageOperationStatus updateComponentCalculatedCapabilitiesProperties(Component containerComponent) { - Map mapCapabiltyPropertyMap = - convertComponentCapabilitiesProperties(containerComponent); - return nodeTemplateOperation.overrideComponentCapabilitiesProperties(containerComponent, mapCapabiltyPropertyMap); - } + public StorageOperationStatus updateComponentCalculatedCapabilitiesProperties(Component containerComponent) { + Map mapCapabiltyPropertyMap = convertComponentCapabilitiesProperties(containerComponent); + return nodeTemplateOperation.overrideComponentCapabilitiesProperties(containerComponent, mapCapabiltyPropertyMap); + } public StorageOperationStatus deleteAllCalculatedCapabilitiesRequirements(String topologyTemplateId) { - StorageOperationStatus status = topologyTemplateOperation.removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES); + StorageOperationStatus status = topologyTemplateOperation + .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES); if (status == StorageOperationStatus.OK) { - status = topologyTemplateOperation.removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS); + status = topologyTemplateOperation + .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS); } if (status == StorageOperationStatus.OK) { - status = topologyTemplateOperation.removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES); + status = topologyTemplateOperation + .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES); } return status; } public Either shouldUpgradeToLatestDerived(Resource clonedResource) { String componentId = clonedResource.getUniqueId(); - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); - } GraphVertex nodeTypeV = getVertexEither.left().value(); - ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(clonedResource); - - Either shouldUpdateDerivedVersion = nodeTypeOperation.shouldUpdateDerivedVersion(toscaElementToUpdate, nodeTypeV); + Either shouldUpdateDerivedVersion = nodeTypeOperation + .shouldUpdateDerivedVersion(toscaElementToUpdate, nodeTypeV); if (shouldUpdateDerivedVersion.isRight() && StorageOperationStatus.OK != shouldUpdateDerivedVersion.right().value()) { - log.debug("Failed to update derived version for node type {} derived {}, error: {}", componentId, clonedResource.getDerivedFrom().get(0), shouldUpdateDerivedVersion.right().value()); - return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); + log.debug("Failed to update derived version for node type {} derived {}, error: {}", componentId, clonedResource.getDerivedFrom().get(0), + shouldUpdateDerivedVersion.right().value()); + return Either.right(shouldUpdateDerivedVersion.right().value()); } if (shouldUpdateDerivedVersion.isLeft()) { return Either.left(ModelConverter.convertFromToscaElement(shouldUpdateDerivedVersion.left().value())); @@ -2570,35 +3411,44 @@ public class ToscaOperationFacade { /** * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId */ - public Either, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityName, String capabilityType, String ownerId) { + public Either, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId, + String instanceId, + String capabilityName, + String capabilityType, + String ownerId) { return topologyTemplateOperation.getComponentInstanceCapabilityProperties(componentId, instanceId, capabilityName, capabilityType, ownerId); } - private MapInterfaceDataDefinition convertComponentInstanceInterfaces(Component currComponent, - String componentInstanceId) { - MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition(); - List componentInterface = currComponent.getComponentInstancesInterfaces().get(componentInstanceId); - - if(CollectionUtils.isNotEmpty(componentInterface)) { - componentInterface.stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put - (interfaceDef.getUniqueId(), interfaceDef)); - } + private MapInterfaceDataDefinition convertComponentInstanceInterfaces(Component currComponent, String componentInstanceId) { + MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition(); + List componentInterface = currComponent.getComponentInstancesInterfaces().get(componentInstanceId); + if (CollectionUtils.isNotEmpty(componentInterface)) { + componentInterface.stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put(interfaceDef.getUniqueId(), interfaceDef)); + } + return mapInterfaceDataDefinition; + } - return mapInterfaceDataDefinition; - } + private MapInterfaceDataDefinition convertComponentInterfaces(final Map interfaces) { + final MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition(); + if (MapUtils.isNotEmpty(interfaces)) { + interfaces.values().stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put(interfaceDef.getType(), interfaceDef)); + } + return mapInterfaceDataDefinition; + } - private Map convertComponentCapabilitiesProperties(Component currComponent) { - Map map = ModelConverter.extractCapabilityPropertiesFromGroups(currComponent.getGroups(), true); - map.putAll(ModelConverter.extractCapabilityProperteisFromInstances(currComponent.getComponentInstances(), true)); - return map; - } + private Map convertComponentCapabilitiesProperties(Component currComponent) { + Map map = ModelConverter.extractCapabilityPropertiesFromGroups(currComponent.getGroups(), true); + map.putAll(ModelConverter.extractCapabilityProperteisFromInstances(currComponent.getComponentInstances(), true)); + return map; + } private Optional convertComponentInstanceProperties(Component component, String instanceId) { return component.fetchInstanceById(instanceId) - .map(ci -> ModelConverter.convertToMapOfMapCapabiltyProperties(ci.getCapabilities(), instanceId)); + .map(ci -> ModelConverter.convertToMapOfMapCapabilityProperties(ci.getCapabilities(), instanceId, ci.getOriginType().isAtomicType())); } - public Either associatePolicyToComponent(String componentId, PolicyDefinition policyDefinition, int counter) { + public Either associatePolicyToComponent(String componentId, PolicyDefinition policyDefinition, + int counter) { Either result = null; Either getVertexEither; getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata); @@ -2606,13 +3456,14 @@ public class ToscaOperationFacade { log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); } else { - if (getVertexEither.left().value().getLabel() != VertexTypeEnum.TOPOLOGY_TEMPLATE) { + if (getVertexEither.left().value().getLabel() != TOPOLOGY_TEMPLATE) { log.error("Policy association to component of Tosca type {} is not allowed. ", getVertexEither.left().value().getLabel()); result = Either.right(StorageOperationStatus.BAD_REQUEST); } } if (result == null) { - StorageOperationStatus status = topologyTemplateOperation.addPolicyToToscaElement(getVertexEither.left().value(), policyDefinition, counter); + StorageOperationStatus status = topologyTemplateOperation + .addPolicyToToscaElement(getVertexEither.left().value(), policyDefinition, counter); if (status != StorageOperationStatus.OK) { return Either.right(status); } @@ -2626,11 +3477,12 @@ public class ToscaOperationFacade { public StorageOperationStatus associatePoliciesToComponent(String componentId, List policies) { log.debug("#associatePoliciesToComponent - associating policies for component {}.", componentId); return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata) - .either(containerVertex -> topologyTemplateOperation.addPoliciesToToscaElement(containerVertex, policies), - DaoStatusConverter::convertJanusGraphStatusToStorageStatus); + .either(containerVertex -> topologyTemplateOperation.addPoliciesToToscaElement(containerVertex, policies), + DaoStatusConverter::convertJanusGraphStatusToStorageStatus); } - public Either updatePolicyOfComponent(String componentId, PolicyDefinition policyDefinition) { + public Either updatePolicyOfComponent(String componentId, PolicyDefinition policyDefinition, + PromoteVersionEnum promoteVersionEnum) { Either result = null; Either getVertexEither; getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); @@ -2639,6 +3491,7 @@ public class ToscaOperationFacade { result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value())); } if (result == null) { + policyDefinition.setVersion(GroupUtils.updateVersion(promoteVersionEnum, policyDefinition.getVersion())); StorageOperationStatus status = topologyTemplateOperation.updatePolicyOfToscaElement(getVertexEither.left().value(), policyDefinition); if (status != StorageOperationStatus.OK) { return Either.right(status); @@ -2652,17 +3505,14 @@ public class ToscaOperationFacade { public StorageOperationStatus updatePoliciesOfComponent(String componentId, List policyDefinition) { log.debug("#updatePoliciesOfComponent - updating policies for component {}", componentId); - return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse) - .right() - .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus) - .either(containerVertex -> topologyTemplateOperation.updatePoliciesOfToscaElement(containerVertex, policyDefinition), - err -> err); + return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse).right() + .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus) + .either(containerVertex -> topologyTemplateOperation.updatePoliciesOfToscaElement(containerVertex, policyDefinition), err -> err); } public StorageOperationStatus removePolicyFromComponent(String componentId, String policyId) { StorageOperationStatus status = null; - Either getVertexEither = janusGraphDao - .getVertexById(componentId, JsonParseFlagEnum.NoParse); + Either getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value()); status = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()); @@ -2674,32 +3524,42 @@ public class ToscaOperationFacade { } public boolean canAddGroups(String componentId) { - GraphVertex vertex = janusGraphDao.getVertexById(componentId) - .left() - .on(this::onJanusGraphError); + GraphVertex vertex = janusGraphDao.getVertexById(componentId).left().on(this::onJanusGraphError); return topologyTemplateOperation.hasEdgeOfType(vertex, EdgeLabelEnum.GROUPS); } GraphVertex onJanusGraphError(JanusGraphOperationStatus toe) { - throw new StorageException( - DaoStatusConverter.convertJanusGraphStatusToStorageStatus(toe)); + throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(toe)); } - public void updateNamesOfCalculatedCapabilitiesRequirements(String componentId){ - topologyTemplateOperation - .updateNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId)); + public CatalogUpdateTimestamp updateCatalogTimes() { + long now = System.currentTimeMillis(); + GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT).left().on(this::onJanusGraphError); + Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME); + catalogRoot.addMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME, currentTime); + catalogRoot.addMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME, now); + janusGraphDao.updateVertex(catalogRoot).left().on(this::onJanusGraphError); + return new CatalogUpdateTimestamp(currentTime, now); + } + + public CatalogUpdateTimestamp getCatalogTimes() { + GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT).left().on(this::onJanusGraphError); + Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME); + Long prevTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME); + return new CatalogUpdateTimestamp(prevTime == null ? 0 : prevTime.longValue(), currentTime == null ? 0 : currentTime.longValue()); + } + + public void updateNamesOfCalculatedCapabilitiesRequirements(String componentId) { + topologyTemplateOperation.updateNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId)); } public void revertNamesOfCalculatedCapabilitiesRequirements(String componentId) { - topologyTemplateOperation - .revertNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId)); + topologyTemplateOperation.revertNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId)); } private TopologyTemplate getTopologyTemplate(String componentId) { - return (TopologyTemplate)topologyTemplateOperation - .getToscaElement(componentId, getFilterComponentWithCapProperties()) - .left() - .on(this::throwStorageException); + return (TopologyTemplate) topologyTemplateOperation.getToscaElement(componentId, getFilterComponentWithCapProperties()).left() + .on(this::throwStorageException); } private ComponentParametersView getFilterComponentWithCapProperties() { @@ -2713,12 +3573,13 @@ public class ToscaOperationFacade { } public Either isComponentInUse(String componentId) { - final List forbiddenEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF); + final List forbiddenEdgeLabelEnums = Arrays + .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF); Either vertexById = janusGraphDao.getVertexById(componentId); if (vertexById.isLeft()) { for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) { Iterator edgeItr = vertexById.left().value().getVertex().edges(Direction.IN, edgeLabelEnum.name()); - if(edgeItr != null && edgeItr.hasNext()){ + if (edgeItr != null && edgeItr.hasNext()) { return Either.left(true); } } @@ -2726,41 +3587,36 @@ public class ToscaOperationFacade { return Either.left(false); } - public Either, StorageOperationStatus> getComponentListByInvariantUuid - (String componentInvariantUuid, Map additionalPropertiesToMatch) { - - Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); - if (MapUtils.isNotEmpty(additionalPropertiesToMatch)) { - propertiesToMatch.putAll(additionalPropertiesToMatch); - } - propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, componentInvariantUuid); - - Either, JanusGraphOperationStatus> vertexEither = janusGraphDao - .getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata); - - if (vertexEither.isRight()) { - log.debug("Couldn't fetch metadata for component with type {} and invariantUUId {}, error: {}", componentInvariantUuid, vertexEither.right().value()); - return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value())); - } - List vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null; - - if (vertexList == null || vertexList.isEmpty()) { - log.debug("Component with invariantUUId {} was not found", componentInvariantUuid); - return Either.right(StorageOperationStatus.NOT_FOUND); - } - - ArrayList components = new ArrayList<>(); - for (GraphVertex vertex : vertexList) { - Either toscaElementByOperation = getToscaElementByOperation(vertex); - if (toscaElementByOperation.isRight()) { - log.debug("Could not fetch the following Component by Invariant UUID {}", vertex.getUniqueId()); - return Either.right(toscaElementByOperation.right().value()); - } - components.add(toscaElementByOperation.left().value()); - } - - return Either.left(components); - } + public Either, StorageOperationStatus> getComponentListByInvariantUuid(String componentInvariantUuid, + Map additionalPropertiesToMatch) { + Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); + if (MapUtils.isNotEmpty(additionalPropertiesToMatch)) { + propertiesToMatch.putAll(additionalPropertiesToMatch); + } + propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, componentInvariantUuid); + Either, JanusGraphOperationStatus> vertexEither = janusGraphDao + .getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata); + if (vertexEither.isRight()) { + log.debug("Couldn't fetch metadata for component with type {} and invariantUUId {}, error: {}", componentInvariantUuid, + vertexEither.right().value()); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value())); + } + List vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null; + if (vertexList == null || vertexList.isEmpty()) { + log.debug("Component with invariantUUId {} was not found", componentInvariantUuid); + return Either.right(StorageOperationStatus.NOT_FOUND); + } + ArrayList components = new ArrayList<>(); + for (GraphVertex vertex : vertexList) { + Either toscaElementByOperation = getToscaElementByOperation(vertex); + if (toscaElementByOperation.isRight()) { + log.debug("Could not fetch the following Component by Invariant UUID {}", vertex.getUniqueId()); + return Either.right(toscaElementByOperation.right().value()); + } + components.add(toscaElementByOperation.left().value()); + } + return Either.left(components); + } public Either, StorageOperationStatus> getParentComponents(String componentId) { List parentComponents = new ArrayList<>(); @@ -2770,9 +3626,9 @@ public class ToscaOperationFacade { for (EdgeLabelEnum edgeLabelEnum : relationEdgeLabelEnums) { Either parentVertexEither = janusGraphDao .getParentVertex(vertexById.left().value(), edgeLabelEnum, JsonParseFlagEnum.ParseJson); - if(parentVertexEither.isLeft()){ + if (parentVertexEither.isLeft()) { Either componentEither = getToscaElement(parentVertexEither.left().value().getUniqueId()); - if(componentEither.isLeft()){ + if (componentEither.isLeft()) { parentComponents.add(componentEither.left().value()); } } @@ -2780,8 +3636,12 @@ public class ToscaOperationFacade { } return Either.left(parentComponents); } + public void updateCapReqPropertiesOwnerId(String componentId) { - topologyTemplateOperation - .updateCapReqPropertiesOwnerId(componentId, getTopologyTemplate(componentId)); + topologyTemplateOperation.updateCapReqPropertiesOwnerId(componentId, getTopologyTemplate(componentId)); + } + + public Either getLatestByServiceName(String serviceName) { + return getLatestByName(GraphPropertyEnum.NAME, serviceName, null); } }