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.impl;
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.api.BasicDao;
25 import org.openecomp.sdc.be.dao.api.IResourceDAO;
26 import org.openecomp.sdc.be.dao.graph.datatype.*;
27 import org.openecomp.sdc.be.dao.neo4j.*;
28 import org.openecomp.sdc.be.dao.neo4j.filters.MatchFilter;
29 import org.openecomp.sdc.be.dao.neo4j.filters.RecursiveFilter;
30 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
31 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import javax.annotation.PostConstruct;
35 import java.util.List;
38 //@Component("neo4j-resource-dao")
39 public class Neo4jResourceDAO extends BasicDao implements IResourceDAO {
42 Neo4jClient neo4jClient;
44 private static Logger logger = Logger.getLogger(Neo4jResourceDAO.class.getName());
46 Neo4jGraphBatchBuilder graphBatchBuilder = new Neo4jGraphBatchBuilder();
48 public Neo4jResourceDAO() {
54 super.setNeo4jClient(neo4jClient);
57 private String findResourceDataIdFromNodes(List<GraphNode> nodes) {
61 for (GraphNode neo4jNode : nodes) {
62 String label = neo4jNode.getLabel();
63 if (label.equals(NodeTypeEnum.Resource.getName())) {
64 return neo4jNode.getUniqueId().toString();
72 private GraphRelation addStateRelation(RelationEndPoint from, RelationEndPoint to, GraphEdgeLabels edgeLabel,
75 GraphRelation relationState = new GraphRelation();
76 relationState.setFrom(from);
77 relationState.setTo(to);
78 relationState.setType(edgeLabel.name());
79 relationState.setAction(ActionEnum.Create);
83 // private ActionStatus convertNeo4jOperationStatusToActionStatus(
84 // Neo4jOperationStatus value) {
86 // if (value == null) {
87 // return ActionStatus.GENERAL_ERROR;
92 // return ActionStatus.RESOURCE_NOT_FOUND;
94 // return ActionStatus.GENERAL_ERROR;
95 // case NOT_SUPPORTED:
96 // return ActionStatus.INVALID_CONTENT;
98 // return ActionStatus.INVALID_CONTENT;
100 // return ActionStatus.OK;
102 // return ActionStatus.GENERAL_ERROR;
108 public Either<ResourceMetadataData, Neo4jOperationStatus> getResourceData(String id) {
110 MatchFilter filter = new MatchFilter();
111 filter.addToMatch(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), id);
112 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
113 NodeTypeEnum.Resource.getName(), filter);
115 if (status.isRight()) {
116 return Either.right(status.right().value());
118 List<GraphElement> value = status.left().value();
119 if (value == null || value.isEmpty()) {
120 return Either.right(Neo4jOperationStatus.NOT_FOUND);
122 return Either.left((ResourceMetadataData) value.get(0));
128 public Either<Integer, Neo4jOperationStatus> getNumberOfResourcesByName(String name) {
130 MatchFilter filter = new MatchFilter();
131 filter.addToMatch(GraphPropertiesDictionary.NAME.getProperty(), name);
132 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
133 NodeTypeEnum.Resource.getName(), filter);
135 if (status.isRight() || (status.left().value() == null)) {
136 return Either.right(Neo4jOperationStatus.GENERAL_ERROR);
138 List<GraphElement> value = status.left().value();
139 return Either.left(value.size());
144 public void setNeo4jClient(Neo4jClient client) {
145 this.neo4jClient = client;
146 super.setNeo4jClient(client);
150 public Either<List<ResourceMetadataData>, Neo4jOperationStatus> getAllResourcesData(
151 Map<String, Object> propertiesToMatch) {
153 RecursiveFilter filter = new RecursiveFilter(NodeTypeEnum.Resource);
154 // filter.addRelationType("typeof").addRelationType("belong").setProperties(propertiesToMatch);
156 Either<List<List<GraphElement>>, Neo4jOperationStatus> ret = neo4jClient.executeGet(filter);
158 return Either.right(ret.right().value());
160 List<List<GraphElement>> listOfListOfNeo4jElement = ret.left().value();
162 for (List<GraphElement> row : listOfListOfNeo4jElement) {
164 for (GraphElement elem : row) {
168 return Either.right(null);
171 * MatchFilter filter = new MatchFilter(); if(propertiesToMatch !=
172 * null){ for (Entry<String,Object> propertie :
173 * propertiesToMatch.entrySet()){ filter.addToMatch(propertie.getKey(),
174 * propertie.getValue()); } } Either<List<GraphElement>,
175 * Neo4jOperationStatus> status =
176 * neo4jClient.getByFilter(GraphElementTypeEnum.Node,
177 * NodeTypeEnum.Resource.getName(), filter); if (status.isRight()) {
178 * return Either.right(status.right().value()); } else {
179 * List<GraphElement> value = status.left().value(); if (value == null
180 * || value.isEmpty()) { return
181 * Either.right(Neo4jOperationStatus.NOT_FOUND); } else {
182 * List<ResourceData> result=new ArrayList<>(); for(GraphElement element
183 * : value ){ result.add((ResourceData)element); } return
184 * Either.left(result); } }
189 // public ActionStatus updateUserData(UserData userData) {
190 // UpdateFilter filter = new UpdateFilter();
191 // filter.addToMatch("userId", userData.getUserId());
192 // filter.setToUpdate(userData.toMap());
193 // Neo4jOperationStatus status =
194 // neo4jClient.updateElement(Neo4JElementTypeEnum.Node,
195 // NodeTypeEnum.User.getName(), filter);
196 // if (status.equals(Neo4jOperationStatus.OK)) {
197 // return ActionStatus.OK;
199 // return ActionStatus.GENERAL_ERROR;
204 // public ActionStatus deleteUserData(String id) {
205 // MatchFilter filter = new MatchFilter();
206 // filter.addToMatch("userId", id);
207 // Neo4jOperationStatus status =
208 // neo4jClient.deleteElement(Neo4JElementTypeEnum.Node,
209 // NodeTypeEnum.User.getName(), filter);
210 // if (status.equals(Neo4jOperationStatus.OK)) {
211 // return ActionStatus.OK;
213 // return ActionStatus.GENERAL_ERROR;