CSIT Fix for SDC-2585
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / impl / Neo4jElementDAO.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.ActionStatus;
25 import org.openecomp.sdc.be.dao.api.IElementDAO;
26 import org.openecomp.sdc.be.dao.graph.datatype.GraphElement;
27 import org.openecomp.sdc.be.dao.graph.datatype.GraphElementTypeEnum;
28 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
29 import org.openecomp.sdc.be.dao.neo4j.Neo4jClient;
30 import org.openecomp.sdc.be.dao.neo4j.Neo4jOperationStatus;
31 import org.openecomp.sdc.be.dao.neo4j.filters.MatchFilter;
32 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34
35 import java.util.List;
36
37 //@Component("elements-dao")
38 public class Neo4jElementDAO implements IElementDAO {
39
40         // @Resource
41         Neo4jClient neo4jClient;
42
43         private static Logger logger = Logger.getLogger(Neo4jElementDAO.class.getName());
44
45         @Override
46         public Either<List<GraphElement>, ActionStatus> getAllCategories() {
47                 MatchFilter filter = new MatchFilter();
48                 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
49                                 NodeTypeEnum.ResourceCategory.getName(), filter);
50                 if (status.isRight()) {
51                         return Either.right(ActionStatus.GENERAL_ERROR);
52                 } else {
53                         List<GraphElement> value = status.left().value();
54                         if (value == null || value.isEmpty()) {
55                                 return Either.right(ActionStatus.GENERAL_ERROR);
56                         } else {
57                                 return Either.left(value);
58                         }
59                 }
60         }
61
62         @Override
63         public Either<List<GraphElement>, ActionStatus> getAllTags() {
64                 MatchFilter filter = new MatchFilter();
65                 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
66                                 NodeTypeEnum.Tag.getName(), filter);
67                 if (status.isRight()) {
68                         return Either.right(ActionStatus.GENERAL_ERROR);
69                 } else {
70                         List<GraphElement> value = status.left().value();
71                         if (value == null) {
72                                 return Either.right(ActionStatus.GENERAL_ERROR);
73                         } else {
74                                 return Either.left(value);
75                         }
76                 }
77         }
78
79         @Override
80         public Either<GraphElement, ActionStatus> getCategory(String name) {
81                 MatchFilter filter = new MatchFilter();
82                 filter.addToMatch(GraphPropertiesDictionary.NAME.getProperty(), name);
83                 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
84                                 NodeTypeEnum.ResourceCategory.getName(), filter);
85                 if (status.isRight()) {
86                         return Either.right(ActionStatus.GENERAL_ERROR);
87                 } else {
88                         List<GraphElement> value = status.left().value();
89                         if (value == null) {
90                                 return Either.right(ActionStatus.GENERAL_ERROR);
91                         } else {
92                                 return Either.left(value.get(0));
93                         }
94                 }
95
96         }
97
98         /**
99          * FOR TEST ONLY
100          * 
101          * @param neo4jClient
102          */
103         public void setNeo4jClient(Neo4jClient neo4jClient) {
104                 this.neo4jClient = neo4jClient;
105         }
106
107 }