2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.model.operations.utils;
23 import java.util.HashMap;
24 import java.util.Iterator;
26 import java.util.Map.Entry;
28 import org.apache.tinkerpop.gremlin.structure.Direction;
29 import org.apache.tinkerpop.gremlin.structure.Edge;
30 import org.apache.tinkerpop.gremlin.structure.Element;
31 import org.apache.tinkerpop.gremlin.structure.Property;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
34 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
35 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class GraphDeleteUtil {
41 private static Logger log = LoggerFactory.getLogger(GraphDeleteUtil.class.getName());
43 public TitanOperationStatus deleteChildrenNodes(Vertex rootVertex, GraphEdgeLabels edgeType) {
45 // Iterable<Edge> edgesCreatorIterable =
46 // rootVertex.getEdges(Direction.OUT,
48 Iterator<Edge> edgesCreatorIterator = rootVertex.edges(Direction.OUT, edgeType.getProperty());
50 while (edgesCreatorIterator.hasNext()) {
51 Edge edge = edgesCreatorIterator.next();
52 Vertex incomingVertex = edge.inVertex();
53 Iterator<Edge> outEdges = incomingVertex.edges(Direction.OUT);
55 if (outEdges.hasNext()) {
56 return TitanOperationStatus.CANNOT_DELETE_NON_LEAF_NODE;
58 Map<String, Object> properties = null;
59 if (log.isDebugEnabled()) {
60 properties = getProperties(incomingVertex);
61 log.debug("Going to delete vertex {}",properties);
63 incomingVertex.remove();
64 if (log.isDebugEnabled()) {
65 log.debug("After deleting vertex {}",properties);
71 return TitanOperationStatus.OK;
75 public Map<String, Object> getProperties(Element element) {
77 Map<String, Object> result = null;
79 if (element.keys() != null && element.keys().size() > 0) {
80 Map<String, Property> propertyMap = ElementHelper.propertyMap(element,
81 element.keys().toArray(new String[element.keys().size()]));
82 result = new HashMap<String, Object>();
84 for (Entry<String, Property> entry : propertyMap.entrySet()) {
85 String key = entry.getKey();
86 Object value = entry.getValue().value();
88 result.put(key, value);