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 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;
33 import java.util.ArrayList;
34 import java.util.List;
36 import java.util.Map.Entry;
38 public abstract class BasicDao implements IBasicDAO {
40 Neo4jGraphBatchBuilder graphBatchBuilder = new Neo4jGraphBatchBuilder();
42 Neo4jClient neo4jClient;
44 private static Logger logger = Logger.getLogger(Neo4jResourceDAO.class.getName());
46 public <T extends GraphNode> Either<T, Neo4jOperationStatus> create(GraphNeighbourTable graphNeighbourTable,
47 Class<T> clazz, NodeTypeEnum nodeType) {
49 if (graphNeighbourTable != null) {
51 Either<BatchBuilder, Neo4jOperationStatus> bbResult = graphBatchBuilder
52 .buildBatchBuilderFromTable(graphNeighbourTable);
54 if (bbResult.isLeft()) {
56 BatchBuilder batchBuilder = bbResult.left().value();
57 // Neo4jOperationStatus neo4jOperationStatus =
58 // neo4jClient.execute(batchBuilder);
59 Either<List<List<GraphElement>>, Neo4jOperationStatus> executeResult = neo4jClient
60 .execute(batchBuilder);
62 if (executeResult.isRight()) {
63 return Either.right(executeResult.right().value());
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);
85 return Either.left(result);
88 return Either.right(bbResult.right().value());
92 logger.error("The table sent in order to create resource is empty.");
93 return Either.right(Neo4jOperationStatus.BAD_REQUEST);
99 public <T extends GraphNode> Either<T, Neo4jOperationStatus> getNodeData(String uniqueid, Class<T> clazz,
100 NodeTypeEnum nodeTypeEnum) {
102 MatchFilter filter = new MatchFilter();
103 filter.addToMatch(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), uniqueid);
105 return getNodeData(filter, clazz, nodeTypeEnum);
110 public <T extends GraphNode> Either<T, Neo4jOperationStatus> getNodeData(String keyName, String uniqueid,
111 Class<T> clazz, NodeTypeEnum nodeTypeEnum) {
113 MatchFilter filter = new MatchFilter();
114 filter.addToMatch(keyName, uniqueid);
116 return getNodeData(filter, clazz, nodeTypeEnum);
120 private <T extends GraphNode> Either<T, Neo4jOperationStatus> getNodeData(MatchFilter filter, Class<T> clazz,
121 NodeTypeEnum nodeTypeEnum) {
123 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
124 nodeTypeEnum.getName(), filter);
126 if (status.isRight()) {
127 return Either.right(status.right().value());
129 List<GraphElement> value = status.left().value();
130 if (value == null || value.isEmpty()) {
131 return Either.right(Neo4jOperationStatus.NOT_FOUND);
133 return Either.left(clazz.cast(value.get(0)));
139 public <T extends GraphNode> Either<List<T>, Neo4jOperationStatus> getNodesData(
140 Map<String, Object> propertiesToMatch, Class<T> clazz, NodeTypeEnum nodeTypeEnum) {
142 MatchFilter filter = new MatchFilter();
143 if (propertiesToMatch != null) {
144 for (Entry<String, Object> property : propertiesToMatch.entrySet()) {
145 filter.addToMatch(property.getKey(), property.getValue());
149 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
150 nodeTypeEnum.getName(), filter);
152 if (status.isRight()) {
153 return Either.right(status.right().value());
155 List<GraphElement> value = status.left().value();
156 if (value == null || value.isEmpty()) {
157 return Either.right(Neo4jOperationStatus.NOT_FOUND);
159 List<T> list = new ArrayList<>();
160 for (GraphElement element : value) {
161 list.add(clazz.cast(element));
163 return Either.left(list);
168 public Neo4jClient getNeo4jClient() {
172 public void setNeo4jClient(Neo4jClient neo4jClient) {
173 this.neo4jClient = neo4jClient;