CSIT Fix for SDC-2585
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / neo4j / Neo4jGraphBatchBuilder.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.dao.neo4j;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.graph.datatype.*;
25 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
26 import org.openecomp.sdc.common.log.wrappers.Logger;
27
28 import java.util.*;
29
30 public class Neo4jGraphBatchBuilder {
31
32         private static Logger logger = Logger.getLogger(Neo4jGraphBatchBuilder.class.getName());
33
34         public Either<BatchBuilder, Neo4jOperationStatus> buildBatchBuilderFromTable(
35                         GraphNeighbourTable graphNeighbourTable) {
36
37                 logger.debug("The table sent in order to build BatchBuilder is {}", graphNeighbourTable);
38
39                 List<GraphNode> nodes = graphNeighbourTable.getNodes();
40                 if (nodes != null && nodes.size() > 0) {
41                         List<NodeRelation> directedEdges = graphNeighbourTable.getDirectedEdges();
42
43                         List<RelationEndPoint> relationEndPoints = new ArrayList<>(nodes.size());
44                         Set<Integer> nodesInRelations = findDistinctNodesIndex(directedEdges);
45
46                         buildRelationEndPoints(nodes, nodesInRelations, relationEndPoints);
47
48                         BatchBuilder batchBuilder = BatchBuilder.getBuilder();
49
50                         for (GraphElement neo4jElement : nodes) {
51                                 if (neo4jElement.getAction() != ActionEnum.Delete) {
52                                         logger.debug("Goint to add node {} to batch builder.", neo4jElement);
53                                         batchBuilder.add(neo4jElement);
54                                 }
55                         }
56
57                         if (directedEdges != null) {
58                                 for (NodeRelation nodeRelation : directedEdges) {
59                                         GraphRelation relation = buildNeo4jRelation(relationEndPoints, nodeRelation);
60                                         logger.debug("Goint to add relation {} to batch builder.", relation);
61                                         batchBuilder.add(relation);
62                                 }
63                         }
64
65                         for (GraphElement neo4jElement : nodes) {
66                                 if (neo4jElement.getAction() == ActionEnum.Delete) {
67                                         logger.debug("Goint to add node {} to batch builder.", neo4jElement);
68                                         batchBuilder.add(neo4jElement);
69                                 }
70                         }
71
72                         return Either.left(batchBuilder);
73
74                 } else {
75                         logger.error("No node was sent in order to create the resource.");
76                         return Either.right(Neo4jOperationStatus.BAD_REQUEST);
77                 }
78         }
79
80         private Pair<String, String> getUniqueIdKeyValue(GraphNode neo4jNode) {
81
82                 // String label = neo4jNode.getLabel();
83                 // NodeTypeEnum nodeTypeEnum = NodeTypeEnum.getByName(label);
84                 //
85                 return Pair.createPair(neo4jNode.getUniqueIdKey(), neo4jNode.getUniqueId().toString());
86         }
87
88         private Set<Integer> findDistinctNodesIndex(List<NodeRelation> directedEdges) {
89
90                 HashSet<Integer> nodesIndex = new HashSet<>();
91
92                 if (directedEdges != null) {
93                         for (NodeRelation nodeRelation : directedEdges) {
94                                 nodesIndex.add(nodeRelation.getFromIndex());
95                                 nodesIndex.add(nodeRelation.getToIndex());
96                         }
97                 }
98
99                 return nodesIndex;
100         }
101
102         private String findResourceDataIdFromNodes(List<GraphNode> nodes) {
103
104                 if (nodes != null) {
105
106                         for (GraphNode neo4jNode : nodes) {
107                                 String label = neo4jNode.getLabel();
108                                 if (label.equals(NodeTypeEnum.Resource.getName())) {
109                                         return neo4jNode.getUniqueId().toString();
110                                 }
111                         }
112                 }
113
114                 return null;
115         }
116
117         private GraphRelation buildNeo4jRelation(List<RelationEndPoint> relationEndPoints, NodeRelation nodeRelation) {
118                 GraphRelation relation = new GraphRelation();
119                 int fromIndex = nodeRelation.getFromIndex();
120                 int toIndex = nodeRelation.getToIndex();
121                 Neo4jEdge neo4jEdge = nodeRelation.getEdge();
122                 relation.setFrom(relationEndPoints.get(fromIndex));
123                 relation.setTo(relationEndPoints.get(toIndex));
124                 relation.setType(neo4jEdge.getEdgeType().getProperty());
125
126                 // TODO: fix it after change
127                 Map<String, Object> edgeProps = neo4jEdge.getProperties();
128                 if (edgeProps != null && !edgeProps.isEmpty()) {
129                         relation.addPropertis(edgeProps);
130                 }
131
132                 relation.setAction(neo4jEdge.getAction());
133                 return relation;
134         }
135
136         private void buildRelationEndPoints(List<GraphNode> nodes, Set<Integer> nodesInRelations,
137                         List<RelationEndPoint> relationEndPoints) {
138
139                 if (nodesInRelations != null) {
140                         for (Integer nodeIndex : nodesInRelations) {
141
142                                 GraphElement neo4jElement = nodes.get(nodeIndex);
143                                 GraphNode neo4jNode = (GraphNode) neo4jElement;
144                                 String label = neo4jNode.getLabel();
145                                 Pair<String, String> uniqueKeyValue = getUniqueIdKeyValue(neo4jNode);
146
147                                 RelationEndPoint endPoint = new RelationEndPoint(NodeTypeEnum.getByName(label), uniqueKeyValue.getKey(),
148                                                 uniqueKeyValue.getValue());
149                                 relationEndPoints.add(nodeIndex, endPoint);
150
151                         }
152                 }
153
154         }
155
156         public static class Pair<K, V> {
157
158                 private final K key;
159                 private final V value;
160
161                 public static <K, V> Pair<K, V> createPair(K key, V value) {
162                         return new Pair<>(key, value);
163                 }
164
165                 public Pair(K key, V value) {
166                         this.key = key;
167                         this.value = value;
168                 }
169
170                 public K getKey() {
171                         return key;
172                 }
173
174                 public V getValue() {
175                         return value;
176                 }
177
178         }
179 }