CSIT Fix for SDC-2585
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / impl / Neo4jResourceDAO.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.impl;
22
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;
33
34 import javax.annotation.PostConstruct;
35 import java.util.List;
36 import java.util.Map;
37
38 //@Component("neo4j-resource-dao")
39 public class Neo4jResourceDAO extends BasicDao implements IResourceDAO {
40
41         // @Resource
42         Neo4jClient neo4jClient;
43
44         private static Logger logger = Logger.getLogger(Neo4jResourceDAO.class.getName());
45
46         Neo4jGraphBatchBuilder graphBatchBuilder = new Neo4jGraphBatchBuilder();
47
48         public Neo4jResourceDAO() {
49
50         }
51
52         @PostConstruct
53         public void init() {
54                 super.setNeo4jClient(neo4jClient);
55         }
56
57         private String findResourceDataIdFromNodes(List<GraphNode> nodes) {
58
59                 if (nodes != null) {
60
61                         for (GraphNode neo4jNode : nodes) {
62                                 String label = neo4jNode.getLabel();
63                                 if (label.equals(NodeTypeEnum.Resource.getName())) {
64                                         return neo4jNode.getUniqueId().toString();
65                                 }
66                         }
67                 }
68
69                 return null;
70         }
71
72         private GraphRelation addStateRelation(RelationEndPoint from, RelationEndPoint to, GraphEdgeLabels edgeLabel,
73                         String value) {
74
75                 GraphRelation relationState = new GraphRelation();
76                 relationState.setFrom(from);
77                 relationState.setTo(to);
78                 relationState.setType(edgeLabel.name());
79                 relationState.setAction(ActionEnum.Create);
80                 return relationState;
81         }
82
83         // private ActionStatus convertNeo4jOperationStatusToActionStatus(
84         // Neo4jOperationStatus value) {
85         //
86         // if (value == null) {
87         // return ActionStatus.GENERAL_ERROR;
88         // }
89         //
90         // switch (value) {
91         // case NOT_FOUND:
92         // return ActionStatus.RESOURCE_NOT_FOUND;
93         // case ERROR:
94         // return ActionStatus.GENERAL_ERROR;
95         // case NOT_SUPPORTED:
96         // return ActionStatus.INVALID_CONTENT;
97         // case WRONG_INPUT:
98         // return ActionStatus.INVALID_CONTENT;
99         // case OK:
100         // return ActionStatus.OK;
101         // default:
102         // return ActionStatus.GENERAL_ERROR;
103         // }
104         //
105         // }
106
107         @Override
108         public Either<ResourceMetadataData, Neo4jOperationStatus> getResourceData(String id) {
109
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);
114
115                 if (status.isRight()) {
116                         return Either.right(status.right().value());
117                 } else {
118                         List<GraphElement> value = status.left().value();
119                         if (value == null || value.isEmpty()) {
120                                 return Either.right(Neo4jOperationStatus.NOT_FOUND);
121                         } else {
122                                 return Either.left((ResourceMetadataData) value.get(0));
123                         }
124                 }
125         }
126
127         @Override
128         public Either<Integer, Neo4jOperationStatus> getNumberOfResourcesByName(String name) {
129
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);
134
135                 if (status.isRight() || (status.left().value() == null)) {
136                         return Either.right(Neo4jOperationStatus.GENERAL_ERROR);
137                 } else {
138                         List<GraphElement> value = status.left().value();
139                         return Either.left(value.size());
140                 }
141         }
142
143         @Override
144         public void setNeo4jClient(Neo4jClient client) {
145                 this.neo4jClient = client;
146                 super.setNeo4jClient(client);
147         }
148
149         @Override
150         public Either<List<ResourceMetadataData>, Neo4jOperationStatus> getAllResourcesData(
151                         Map<String, Object> propertiesToMatch) {
152
153                 RecursiveFilter filter = new RecursiveFilter(NodeTypeEnum.Resource);
154                 // filter.addRelationType("typeof").addRelationType("belong").setProperties(propertiesToMatch);
155
156                 Either<List<List<GraphElement>>, Neo4jOperationStatus> ret = neo4jClient.executeGet(filter);
157                 if (ret.isRight()) {
158                         return Either.right(ret.right().value());
159                 }
160                 List<List<GraphElement>> listOfListOfNeo4jElement = ret.left().value();
161
162                 for (List<GraphElement> row : listOfListOfNeo4jElement) {
163
164                         for (GraphElement elem : row) {
165
166                         }
167                 }
168                 return Either.right(null);
169
170                 /*
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); } }
185                  */
186         }
187
188         // @Override
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;
198         // } else {
199         // return ActionStatus.GENERAL_ERROR;
200         // }
201         // }
202         //
203         // @Override
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;
212         // } else {
213         // return ActionStatus.GENERAL_ERROR;
214         // }
215         // }
216
217 }