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.api;
23 import java.util.ArrayList;
24 import java.util.List;
26 import java.util.Map.Entry;
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;
43 import fj.data.Either;
45 public abstract class BasicDao implements IBasicDAO {
47 Neo4jGraphBatchBuilder graphBatchBuilder = new Neo4jGraphBatchBuilder();
49 Neo4jClient neo4jClient;
51 private static Logger logger = LoggerFactory.getLogger(Neo4jResourceDAO.class.getName());
53 public <T extends GraphNode> Either<T, Neo4jOperationStatus> create(GraphNeighbourTable graphNeighbourTable,
54 Class<T> clazz, NodeTypeEnum nodeType) {
56 if (graphNeighbourTable != null) {
58 Either<BatchBuilder, Neo4jOperationStatus> bbResult = graphBatchBuilder
59 .buildBatchBuilderFromTable(graphNeighbourTable);
61 if (bbResult.isLeft()) {
63 BatchBuilder batchBuilder = bbResult.left().value();
64 // Neo4jOperationStatus neo4jOperationStatus =
65 // neo4jClient.execute(batchBuilder);
66 Either<List<List<GraphElement>>, Neo4jOperationStatus> executeResult = neo4jClient
67 .execute(batchBuilder);
69 if (executeResult.isRight()) {
70 return Either.right(executeResult.right().value());
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);
92 return Either.left(result);
95 return Either.right(bbResult.right().value());
99 logger.error("The table sent in order to create resource is empty.");
100 return Either.right(Neo4jOperationStatus.BAD_REQUEST);
106 public <T extends GraphNode> Either<T, Neo4jOperationStatus> getNodeData(String uniqueid, Class<T> clazz,
107 NodeTypeEnum nodeTypeEnum) {
109 MatchFilter filter = new MatchFilter();
110 filter.addToMatch(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), uniqueid);
112 return getNodeData(filter, clazz, nodeTypeEnum);
117 public <T extends GraphNode> Either<T, Neo4jOperationStatus> getNodeData(String keyName, String uniqueid,
118 Class<T> clazz, NodeTypeEnum nodeTypeEnum) {
120 MatchFilter filter = new MatchFilter();
121 filter.addToMatch(keyName, uniqueid);
123 return getNodeData(filter, clazz, nodeTypeEnum);
127 private <T extends GraphNode> Either<T, Neo4jOperationStatus> getNodeData(MatchFilter filter, Class<T> clazz,
128 NodeTypeEnum nodeTypeEnum) {
130 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
131 nodeTypeEnum.getName(), filter);
133 if (status.isRight()) {
134 return Either.right(status.right().value());
136 List<GraphElement> value = status.left().value();
137 if (value == null || value.isEmpty()) {
138 return Either.right(Neo4jOperationStatus.NOT_FOUND);
140 return Either.left(clazz.cast(value.get(0)));
146 public <T extends GraphNode> Either<List<T>, Neo4jOperationStatus> getNodesData(
147 Map<String, Object> propertiesToMatch, Class<T> clazz, NodeTypeEnum nodeTypeEnum) {
149 MatchFilter filter = new MatchFilter();
150 if (propertiesToMatch != null) {
151 for (Entry<String, Object> property : propertiesToMatch.entrySet()) {
152 filter.addToMatch(property.getKey(), property.getValue());
156 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
157 nodeTypeEnum.getName(), filter);
159 if (status.isRight()) {
160 return Either.right(status.right().value());
162 List<GraphElement> value = status.left().value();
163 if (value == null || value.isEmpty()) {
164 return Either.right(Neo4jOperationStatus.NOT_FOUND);
166 List<T> list = new ArrayList<T>();
167 for (GraphElement element : value) {
168 list.add(clazz.cast(element));
170 return Either.left(list);
175 public Neo4jClient getNeo4jClient() {
179 public void setNeo4jClient(Neo4jClient neo4jClient) {
180 this.neo4jClient = neo4jClient;