+ public List<String> deleteService(String invariantUUID, final boolean inTransaction) {
+ return deleteComponent(invariantUUID, NodeTypeEnum.Service, inTransaction);
+ }
+
+ public List<String> deleteComponent(String invariantUUID, NodeTypeEnum componentType, final boolean inTransaction) {
+ final List<GraphVertex> allServiceVerticesToDelete = findVertexListByInvariantUuid(invariantUUID);
+ List<String> affectedComponentIds = new ArrayList<>();
+ try {
+ checkNotUsed(allServiceVerticesToDelete);
+ lockAllVerticesByNodeType(allServiceVerticesToDelete, componentType);
+ for (GraphVertex elementV : allServiceVerticesToDelete) {
+ Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
+ if (deleteToscaElement.isRight()) {
+ log.debug("Failed to delete element UniqueID {}, Name {}, error {}", elementV.getUniqueId(),
+ elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value());
+ throwStorageException(deleteToscaElement.right().value());
+ }
+ affectedComponentIds.add(elementV.getUniqueId());
+ }
+ if (!inTransaction) {
+ janusGraphDao.commit();
+ }
+ } catch (Exception exception) {
+ if (!inTransaction) {
+ janusGraphDao.rollback();
+ }
+ throw exception;
+ } finally {
+ unlockAllVerticesByNodeType(allServiceVerticesToDelete, componentType);
+ }
+ return affectedComponentIds;
+ }
+
+ private void checkNotUsed(List<GraphVertex> vertices) {
+ boolean isInUse = isAnyComponentInUse(vertices);
+ if (isInUse) {
+ Set<GraphVertex> listOfVertices = getComponentsUsingComponents(vertices);
+ List<String> 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();
+ }
+ }
+
+ public List<GraphVertex> 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<GraphVertex> getComponentsUsingComponents(List<GraphVertex> componentVertices) {
+ Set<GraphVertex> inUseBy = new TreeSet<>(Comparator.comparing(GraphVertex::getUniqueId));
+ for (final GraphVertex elementV : componentVertices) {
+ List<GraphVertex> inUseByVertex = isInUse(elementV);
+ if (!inUseByVertex.isEmpty()) {
+ inUseBy.addAll(inUseByVertex);
+ }
+ }
+ return inUseBy;
+ }
+
+ private boolean isAnyComponentInUse(List<GraphVertex> 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<GraphVertex> isInUse(GraphVertex elementV) {