VoltE fix
[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 java.util.List;
24 import java.util.Map;
25
26 import javax.annotation.PostConstruct;
27
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;
47
48 import fj.data.Either;
49
50 //@Component("neo4j-resource-dao")
51 public class Neo4jResourceDAO extends BasicDao implements IResourceDAO {
52
53         // @Resource
54         Neo4jClient neo4jClient;
55
56         private static Logger logger = LoggerFactory.getLogger(Neo4jResourceDAO.class.getName());
57
58         Neo4jGraphBatchBuilder graphBatchBuilder = new Neo4jGraphBatchBuilder();
59
60         public Neo4jResourceDAO() {
61
62         }
63
64         @PostConstruct
65         public void init() {
66                 super.setNeo4jClient(neo4jClient);
67         }
68
69         private String findResourceDataIdFromNodes(List<GraphNode> nodes) {
70
71                 if (nodes != null) {
72
73                         for (GraphNode neo4jNode : nodes) {
74                                 String label = neo4jNode.getLabel();
75                                 if (label.equals(NodeTypeEnum.Resource.getName())) {
76                                         return neo4jNode.getUniqueId().toString();
77                                 }
78                         }
79                 }
80
81                 return null;
82         }
83
84         private GraphRelation addStateRelation(RelationEndPoint from, RelationEndPoint to, GraphEdgeLabels edgeLabel,
85                         String value) {
86
87                 GraphRelation relationState = new GraphRelation();
88                 relationState.setFrom(from);
89                 relationState.setTo(to);
90                 relationState.setType(edgeLabel.name());
91                 relationState.setAction(ActionEnum.Create);
92                 return relationState;
93         }
94
95         // private ActionStatus convertNeo4jOperationStatusToActionStatus(
96         // Neo4jOperationStatus value) {
97         //
98         // if (value == null) {
99         // return ActionStatus.GENERAL_ERROR;
100         // }
101         //
102         // switch (value) {
103         // case NOT_FOUND:
104         // return ActionStatus.RESOURCE_NOT_FOUND;
105         // case ERROR:
106         // return ActionStatus.GENERAL_ERROR;
107         // case NOT_SUPPORTED:
108         // return ActionStatus.INVALID_CONTENT;
109         // case WRONG_INPUT:
110         // return ActionStatus.INVALID_CONTENT;
111         // case OK:
112         // return ActionStatus.OK;
113         // default:
114         // return ActionStatus.GENERAL_ERROR;
115         // }
116         //
117         // }
118
119         @Override
120         public Either<ResourceMetadataData, Neo4jOperationStatus> getResourceData(String id) {
121
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);
126
127                 if (status.isRight()) {
128                         return Either.right(status.right().value());
129                 } else {
130                         List<GraphElement> value = status.left().value();
131                         if (value == null || value.isEmpty()) {
132                                 return Either.right(Neo4jOperationStatus.NOT_FOUND);
133                         } else {
134                                 return Either.left((ResourceMetadataData) value.get(0));
135                         }
136                 }
137         }
138
139         @Override
140         public Either<Integer, Neo4jOperationStatus> getNumberOfResourcesByName(String name) {
141
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);
146
147                 if (status.isRight() || (status.left().value() == null)) {
148                         return Either.right(Neo4jOperationStatus.GENERAL_ERROR);
149                 } else {
150                         List<GraphElement> value = status.left().value();
151                         return Either.left(value.size());
152                 }
153         }
154
155         @Override
156         public void setNeo4jClient(Neo4jClient client) {
157                 this.neo4jClient = client;
158                 super.setNeo4jClient(client);
159         }
160
161         @Override
162         public Either<List<ResourceMetadataData>, Neo4jOperationStatus> getAllResourcesData(
163                         Map<String, Object> propertiesToMatch) {
164
165                 RecursiveFilter filter = new RecursiveFilter(NodeTypeEnum.Resource);
166                 // filter.addRelationType("typeof").addRelationType("belong").setProperties(propertiesToMatch);
167
168                 Either<List<List<GraphElement>>, Neo4jOperationStatus> ret = neo4jClient.executeGet(filter);
169                 if (ret.isRight()) {
170                         return Either.right(ret.right().value());
171                 }
172                 List<List<GraphElement>> listOfListOfNeo4jElement = ret.left().value();
173
174                 for (List<GraphElement> row : listOfListOfNeo4jElement) {
175
176                         for (GraphElement elem : row) {
177
178                         }
179                 }
180                 return Either.right(null);
181
182                 /*
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); } }
197                  */
198         }
199
200         // @Override
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;
210         // } else {
211         // return ActionStatus.GENERAL_ERROR;
212         // }
213         // }
214         //
215         // @Override
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;
224         // } else {
225         // return ActionStatus.GENERAL_ERROR;
226         // }
227         // }
228
229 }