CSIT Fix for SDC-2585
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / api / BasicDao.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.api;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.graph.datatype.GraphElement;
25 import org.openecomp.sdc.be.dao.graph.datatype.GraphElementTypeEnum;
26 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
27 import org.openecomp.sdc.be.dao.impl.Neo4jResourceDAO;
28 import org.openecomp.sdc.be.dao.neo4j.*;
29 import org.openecomp.sdc.be.dao.neo4j.filters.MatchFilter;
30 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Map.Entry;
37
38 public abstract class BasicDao implements IBasicDAO {
39
40         Neo4jGraphBatchBuilder graphBatchBuilder = new Neo4jGraphBatchBuilder();
41
42         Neo4jClient neo4jClient;
43
44         private static Logger logger = Logger.getLogger(Neo4jResourceDAO.class.getName());
45
46         public <T extends GraphNode> Either<T, Neo4jOperationStatus> create(GraphNeighbourTable graphNeighbourTable,
47                         Class<T> clazz, NodeTypeEnum nodeType) {
48
49                 if (graphNeighbourTable != null) {
50
51                         Either<BatchBuilder, Neo4jOperationStatus> bbResult = graphBatchBuilder
52                                         .buildBatchBuilderFromTable(graphNeighbourTable);
53
54                         if (bbResult.isLeft()) {
55
56                                 BatchBuilder batchBuilder = bbResult.left().value();
57                                 // Neo4jOperationStatus neo4jOperationStatus =
58                                 // neo4jClient.execute(batchBuilder);
59                                 Either<List<List<GraphElement>>, Neo4jOperationStatus> executeResult = neo4jClient
60                                                 .execute(batchBuilder);
61
62                                 if (executeResult.isRight()) {
63                                         return Either.right(executeResult.right().value());
64                                 }
65
66                                 T result = null;
67                                 List<List<GraphElement>> listOfResults = executeResult.left().value();
68                                 if (listOfResults != null) {
69                                         for (List<GraphElement> listOfElements : listOfResults) {
70                                                 if (listOfElements != null && !listOfElements.isEmpty()) {
71                                                         for (GraphElement element : listOfElements) {
72                                                                 logger.debug("element {} was returned after running batch operation {}",
73                                                                                 element, batchBuilder);
74                                                                 if (element instanceof GraphNode) {
75                                                                         GraphNode neo4jNode = (GraphNode) element;
76                                                                         if (NodeTypeEnum.getByName(neo4jNode.getLabel()) == nodeType) {
77                                                                                 result = clazz.cast(neo4jNode);
78                                                                         }
79                                                                 }
80                                                         }
81                                                 }
82                                         }
83                                 }
84
85                                 return Either.left(result);
86
87                         } else {
88                                 return Either.right(bbResult.right().value());
89                         }
90
91                 } else {
92                         logger.error("The table sent in order to create resource is empty.");
93                         return Either.right(Neo4jOperationStatus.BAD_REQUEST);
94                 }
95
96         }
97
98         @Override
99         public <T extends GraphNode> Either<T, Neo4jOperationStatus> getNodeData(String uniqueid, Class<T> clazz,
100                         NodeTypeEnum nodeTypeEnum) {
101
102                 MatchFilter filter = new MatchFilter();
103                 filter.addToMatch(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), uniqueid);
104
105                 return getNodeData(filter, clazz, nodeTypeEnum);
106
107         }
108
109         @Override
110         public <T extends GraphNode> Either<T, Neo4jOperationStatus> getNodeData(String keyName, String uniqueid,
111                         Class<T> clazz, NodeTypeEnum nodeTypeEnum) {
112
113                 MatchFilter filter = new MatchFilter();
114                 filter.addToMatch(keyName, uniqueid);
115
116                 return getNodeData(filter, clazz, nodeTypeEnum);
117
118         }
119
120         private <T extends GraphNode> Either<T, Neo4jOperationStatus> getNodeData(MatchFilter filter, Class<T> clazz,
121                         NodeTypeEnum nodeTypeEnum) {
122
123                 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
124                                 nodeTypeEnum.getName(), filter);
125
126                 if (status.isRight()) {
127                         return Either.right(status.right().value());
128                 } else {
129                         List<GraphElement> value = status.left().value();
130                         if (value == null || value.isEmpty()) {
131                                 return Either.right(Neo4jOperationStatus.NOT_FOUND);
132                         } else {
133                                 return Either.left(clazz.cast(value.get(0)));
134                         }
135                 }
136         }
137
138         @Override
139         public <T extends GraphNode> Either<List<T>, Neo4jOperationStatus> getNodesData(
140                         Map<String, Object> propertiesToMatch, Class<T> clazz, NodeTypeEnum nodeTypeEnum) {
141
142                 MatchFilter filter = new MatchFilter();
143                 if (propertiesToMatch != null) {
144                         for (Entry<String, Object> property : propertiesToMatch.entrySet()) {
145                                 filter.addToMatch(property.getKey(), property.getValue());
146                         }
147                 }
148
149                 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
150                                 nodeTypeEnum.getName(), filter);
151
152                 if (status.isRight()) {
153                         return Either.right(status.right().value());
154                 } else {
155                         List<GraphElement> value = status.left().value();
156                         if (value == null || value.isEmpty()) {
157                                 return Either.right(Neo4jOperationStatus.NOT_FOUND);
158                         } else {
159                                 List<T> list = new ArrayList<>();
160                                 for (GraphElement element : value) {
161                                         list.add(clazz.cast(element));
162                                 }
163                                 return Either.left(list);
164                         }
165                 }
166         }
167
168         public Neo4jClient getNeo4jClient() {
169                 return neo4jClient;
170         }
171
172         public void setNeo4jClient(Neo4jClient neo4jClient) {
173                 this.neo4jClient = neo4jClient;
174         }
175
176 }