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