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 java.util.List;
26 import javax.annotation.PostConstruct;
28 import org.openecomp.sdc.be.dao.api.BasicDao;
29 import org.openecomp.sdc.be.dao.api.IResourceDAO;
30 import org.openecomp.sdc.be.dao.graph.datatype.ActionEnum;
31 import org.openecomp.sdc.be.dao.graph.datatype.GraphElement;
32 import org.openecomp.sdc.be.dao.graph.datatype.GraphElementTypeEnum;
33 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
34 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
35 import org.openecomp.sdc.be.dao.graph.datatype.RelationEndPoint;
36 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
37 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
38 import org.openecomp.sdc.be.dao.neo4j.Neo4jClient;
39 import org.openecomp.sdc.be.dao.neo4j.Neo4jGraphBatchBuilder;
40 import org.openecomp.sdc.be.dao.neo4j.Neo4jOperationStatus;
41 import org.openecomp.sdc.be.dao.neo4j.filters.MatchFilter;
42 import org.openecomp.sdc.be.dao.neo4j.filters.RecursiveFilter;
43 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
44 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import fj.data.Either;
50 //@Component("neo4j-resource-dao")
51 public class Neo4jResourceDAO extends BasicDao implements IResourceDAO {
54 Neo4jClient neo4jClient;
56 private static Logger logger = LoggerFactory.getLogger(Neo4jResourceDAO.class.getName());
58 Neo4jGraphBatchBuilder graphBatchBuilder = new Neo4jGraphBatchBuilder();
60 public Neo4jResourceDAO() {
66 super.setNeo4jClient(neo4jClient);
69 private String findResourceDataIdFromNodes(List<GraphNode> nodes) {
73 for (GraphNode neo4jNode : nodes) {
74 String label = neo4jNode.getLabel();
75 if (label.equals(NodeTypeEnum.Resource.getName())) {
76 return neo4jNode.getUniqueId().toString();
84 private GraphRelation addStateRelation(RelationEndPoint from, RelationEndPoint to, GraphEdgeLabels edgeLabel,
87 GraphRelation relationState = new GraphRelation();
88 relationState.setFrom(from);
89 relationState.setTo(to);
90 relationState.setType(edgeLabel.name());
91 relationState.setAction(ActionEnum.Create);
95 // private ActionStatus convertNeo4jOperationStatusToActionStatus(
96 // Neo4jOperationStatus value) {
98 // if (value == null) {
99 // return ActionStatus.GENERAL_ERROR;
104 // return ActionStatus.RESOURCE_NOT_FOUND;
106 // return ActionStatus.GENERAL_ERROR;
107 // case NOT_SUPPORTED:
108 // return ActionStatus.INVALID_CONTENT;
110 // return ActionStatus.INVALID_CONTENT;
112 // return ActionStatus.OK;
114 // return ActionStatus.GENERAL_ERROR;
120 public Either<ResourceMetadataData, Neo4jOperationStatus> getResourceData(String id) {
122 MatchFilter filter = new MatchFilter();
123 filter.addToMatch(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), id);
124 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
125 NodeTypeEnum.Resource.getName(), filter);
127 if (status.isRight()) {
128 return Either.right(status.right().value());
130 List<GraphElement> value = status.left().value();
131 if (value == null || value.isEmpty()) {
132 return Either.right(Neo4jOperationStatus.NOT_FOUND);
134 return Either.left((ResourceMetadataData) value.get(0));
140 public Either<Integer, Neo4jOperationStatus> getNumberOfResourcesByName(String name) {
142 MatchFilter filter = new MatchFilter();
143 filter.addToMatch(GraphPropertiesDictionary.NAME.getProperty(), name);
144 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
145 NodeTypeEnum.Resource.getName(), filter);
147 if (status.isRight() || (status.left().value() == null)) {
148 return Either.right(Neo4jOperationStatus.GENERAL_ERROR);
150 List<GraphElement> value = status.left().value();
151 return Either.left(value.size());
156 public void setNeo4jClient(Neo4jClient client) {
157 this.neo4jClient = client;
158 super.setNeo4jClient(client);
162 public Either<List<ResourceMetadataData>, Neo4jOperationStatus> getAllResourcesData(
163 Map<String, Object> propertiesToMatch) {
165 RecursiveFilter filter = new RecursiveFilter(NodeTypeEnum.Resource);
166 // filter.addRelationType("typeof").addRelationType("belong").setProperties(propertiesToMatch);
168 Either<List<List<GraphElement>>, Neo4jOperationStatus> ret = neo4jClient.executeGet(filter);
170 return Either.right(ret.right().value());
172 List<List<GraphElement>> listOfListOfNeo4jElement = ret.left().value();
174 for (List<GraphElement> row : listOfListOfNeo4jElement) {
176 for (GraphElement elem : row) {
180 return Either.right(null);
183 * MatchFilter filter = new MatchFilter(); if(propertiesToMatch !=
184 * null){ for (Entry<String,Object> propertie :
185 * propertiesToMatch.entrySet()){ filter.addToMatch(propertie.getKey(),
186 * propertie.getValue()); } } Either<List<GraphElement>,
187 * Neo4jOperationStatus> status =
188 * neo4jClient.getByFilter(GraphElementTypeEnum.Node,
189 * NodeTypeEnum.Resource.getName(), filter); if (status.isRight()) {
190 * return Either.right(status.right().value()); } else {
191 * List<GraphElement> value = status.left().value(); if (value == null
192 * || value.isEmpty()) { return
193 * Either.right(Neo4jOperationStatus.NOT_FOUND); } else {
194 * List<ResourceData> result=new ArrayList<>(); for(GraphElement element
195 * : value ){ result.add((ResourceData)element); } return
196 * Either.left(result); } }
201 // public ActionStatus updateUserData(UserData userData) {
202 // UpdateFilter filter = new UpdateFilter();
203 // filter.addToMatch("userId", userData.getUserId());
204 // filter.setToUpdate(userData.toMap());
205 // Neo4jOperationStatus status =
206 // neo4jClient.updateElement(Neo4JElementTypeEnum.Node,
207 // NodeTypeEnum.User.getName(), filter);
208 // if (status.equals(Neo4jOperationStatus.OK)) {
209 // return ActionStatus.OK;
211 // return ActionStatus.GENERAL_ERROR;
216 // public ActionStatus deleteUserData(String id) {
217 // MatchFilter filter = new MatchFilter();
218 // filter.addToMatch("userId", id);
219 // Neo4jOperationStatus status =
220 // neo4jClient.deleteElement(Neo4JElementTypeEnum.Node,
221 // NodeTypeEnum.User.getName(), filter);
222 // if (status.equals(Neo4jOperationStatus.OK)) {
223 // return ActionStatus.OK;
225 // return ActionStatus.GENERAL_ERROR;