Refactoring Consolidation Service
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / utils / GraphDeleteUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.operations.utils;
22
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
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;
38
39 public class GraphDeleteUtil {
40
41         private static Logger log = LoggerFactory.getLogger(GraphDeleteUtil.class.getName());
42
43         public TitanOperationStatus deleteChildrenNodes(Vertex rootVertex, GraphEdgeLabels edgeType) {
44
45                 // Iterable<Edge> edgesCreatorIterable =
46                 // rootVertex.getEdges(Direction.OUT,
47                 // edgeType.name());
48                 Iterator<Edge> edgesCreatorIterator = rootVertex.edges(Direction.OUT, edgeType.getProperty());
49
50                 while (edgesCreatorIterator.hasNext()) {
51                         Edge edge = edgesCreatorIterator.next();
52                         Vertex incomingVertex = edge.inVertex();
53                         Iterator<Edge> outEdges = incomingVertex.edges(Direction.OUT);
54
55                         if (outEdges.hasNext()) {
56                                 return TitanOperationStatus.CANNOT_DELETE_NON_LEAF_NODE;
57                         } else {
58                                 Map<String, Object> properties = null;
59                                 if (log.isDebugEnabled()) {
60                                         properties = getProperties(incomingVertex);
61                                         log.debug("Going to delete vertex {}",properties);
62                                 }
63                                 incomingVertex.remove();
64                                 if (log.isDebugEnabled()) {
65                                         log.debug("After deleting vertex {}",properties);
66                                 }
67                         }
68
69                 }
70
71                 return TitanOperationStatus.OK;
72
73         }
74
75         public Map<String, Object> getProperties(Element element) {
76
77                 Map<String, Object> result = null;
78
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>();
83
84                         for (Entry<String, Property> entry : propertyMap.entrySet()) {
85                                 String key = entry.getKey();
86                                 Object value = entry.getValue().value();
87
88                                 result.put(key, value);
89                         }
90                 }
91                 return result;
92         }
93
94 }