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.dao.neo4j;
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;
30 public class Neo4jGraphBatchBuilder {
32 private static Logger logger = Logger.getLogger(Neo4jGraphBatchBuilder.class.getName());
34 public Either<BatchBuilder, Neo4jOperationStatus> buildBatchBuilderFromTable(
35 GraphNeighbourTable graphNeighbourTable) {
37 logger.debug("The table sent in order to build BatchBuilder is {}", graphNeighbourTable);
39 List<GraphNode> nodes = graphNeighbourTable.getNodes();
40 if (nodes != null && nodes.size() > 0) {
41 List<NodeRelation> directedEdges = graphNeighbourTable.getDirectedEdges();
43 List<RelationEndPoint> relationEndPoints = new ArrayList<>(nodes.size());
44 Set<Integer> nodesInRelations = findDistinctNodesIndex(directedEdges);
46 buildRelationEndPoints(nodes, nodesInRelations, relationEndPoints);
48 BatchBuilder batchBuilder = BatchBuilder.getBuilder();
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);
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);
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);
72 return Either.left(batchBuilder);
75 logger.error("No node was sent in order to create the resource.");
76 return Either.right(Neo4jOperationStatus.BAD_REQUEST);
80 private Pair<String, String> getUniqueIdKeyValue(GraphNode neo4jNode) {
82 // String label = neo4jNode.getLabel();
83 // NodeTypeEnum nodeTypeEnum = NodeTypeEnum.getByName(label);
85 return Pair.createPair(neo4jNode.getUniqueIdKey(), neo4jNode.getUniqueId().toString());
88 private Set<Integer> findDistinctNodesIndex(List<NodeRelation> directedEdges) {
90 HashSet<Integer> nodesIndex = new HashSet<>();
92 if (directedEdges != null) {
93 for (NodeRelation nodeRelation : directedEdges) {
94 nodesIndex.add(nodeRelation.getFromIndex());
95 nodesIndex.add(nodeRelation.getToIndex());
102 private String findResourceDataIdFromNodes(List<GraphNode> nodes) {
106 for (GraphNode neo4jNode : nodes) {
107 String label = neo4jNode.getLabel();
108 if (label.equals(NodeTypeEnum.Resource.getName())) {
109 return neo4jNode.getUniqueId().toString();
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());
126 // TODO: fix it after change
127 Map<String, Object> edgeProps = neo4jEdge.getProperties();
128 if (edgeProps != null && !edgeProps.isEmpty()) {
129 relation.addPropertis(edgeProps);
132 relation.setAction(neo4jEdge.getAction());
136 private void buildRelationEndPoints(List<GraphNode> nodes, Set<Integer> nodesInRelations,
137 List<RelationEndPoint> relationEndPoints) {
139 if (nodesInRelations != null) {
140 for (Integer nodeIndex : nodesInRelations) {
142 GraphElement neo4jElement = nodes.get(nodeIndex);
143 GraphNode neo4jNode = (GraphNode) neo4jElement;
144 String label = neo4jNode.getLabel();
145 Pair<String, String> uniqueKeyValue = getUniqueIdKeyValue(neo4jNode);
147 RelationEndPoint endPoint = new RelationEndPoint(NodeTypeEnum.getByName(label), uniqueKeyValue.getKey(),
148 uniqueKeyValue.getValue());
149 relationEndPoints.add(nodeIndex, endPoint);
156 public static class Pair<K, V> {
159 private final V value;
161 public static <K, V> Pair<K, V> createPair(K key, V value) {
162 return new Pair<>(key, value);
165 public Pair(K key, V value) {
174 public V getValue() {