CSIT Fix for SDC-2585
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / impl / Neo4jUsersDAO.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.IUsersDAO;
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.Neo4jClient;
29 import org.openecomp.sdc.be.dao.neo4j.Neo4jOperationStatus;
30 import org.openecomp.sdc.be.dao.neo4j.filters.MatchFilter;
31 import org.openecomp.sdc.be.dao.neo4j.filters.UpdateFilter;
32 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
33 import org.openecomp.sdc.be.resources.data.UserData;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
35
36 import javax.annotation.PostConstruct;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Map;
40
41 //@Component("users-dao")
42 public class Neo4jUsersDAO implements IUsersDAO {
43
44         private static final String USER_ID = "userId";
45
46     // @Resource
47         Neo4jClient neo4jClient;
48
49         private static Logger logger = Logger.getLogger(Neo4jUsersDAO.class.getName());
50
51         public Neo4jUsersDAO() {
52
53         }
54
55         @PostConstruct
56         public void init() {
57         }
58
59         private void createIndexesAndConstraints() {
60                 Either<Map<String, List<String>>, Neo4jOperationStatus> statusInd = neo4jClient
61                                 .getIndexes(NodeTypeEnum.User.getName());
62                 if (statusInd.isRight()) {
63                         logger.error("Failed to get indexes from Neo4j graph");
64                         throw new RuntimeException("Failed to initialize Neo4jUsersDAO - Failed to get indexes from Neo4j graph");
65                 }
66                 Map<String, List<String>> indexes = statusInd.left().value();
67                 if (indexes == null || indexes.isEmpty()) {
68                         logger.info("Define users indexes in Neo4j");
69                         List<String> propertyNames = new ArrayList<>();
70                         propertyNames.add("firstName");
71                         propertyNames.add("lastName");
72                         propertyNames.add("email");
73                         propertyNames.add("role");
74                         logger.info("Start create Users indexes in Neo4jGraph");
75                         Neo4jOperationStatus createIndexStatus = neo4jClient.createIndex(NodeTypeEnum.User.getName(),
76                                         propertyNames);
77                         if (createIndexStatus.equals(Neo4jOperationStatus.OK)) {
78                                 logger.info("Users indexes created in Neo4j");
79                                 List<String> propertyUnique = new ArrayList<>();
80                                 propertyUnique.add(USER_ID);
81
82                                 logger.info("Start create Users constraints in Neo4jGraph");
83                                 Neo4jOperationStatus createUniquenessStatus = neo4jClient
84                                                 .createUniquenessConstraints(NodeTypeEnum.User.getName(), propertyUnique);
85                                 if (createUniquenessStatus.equals(Neo4jOperationStatus.OK)) {
86                                         logger.info("Users constraints creatyed in Neo4j");
87                                 } else {
88                                         logger.error("Failed to create constraints in Neo4j graph [{}]", createUniquenessStatus);
89                                         throw new RuntimeException(
90                                                         "Failed to initialize Neo4jUsersDAO - Failed to create constraints in Neo4j graph");
91                                 }
92                         } else {
93                                 logger.error("Failed to create indexes in Neo4j graph [{}]", createIndexStatus);
94                                 throw new RuntimeException(
95                                                 "Failed to initialize Neo4jUsersDAO - Failed to create indexes in Neo4j graph");
96                         }
97                 } else {
98                         logger.info("Users indexes already defined in Neo4j");
99                 }
100         }
101
102         @Override
103         public Either<UserData, ActionStatus> getUserData(String id) {
104                 MatchFilter filter = new MatchFilter();
105                 filter.addToMatch(USER_ID, id);
106                 Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
107                                 NodeTypeEnum.User.getName(), filter);
108                 if (status.isRight()) {
109                         return Either.right(ActionStatus.GENERAL_ERROR);
110                 } else {
111                         List<GraphElement> value = status.left().value();
112                         if (value == null || value.isEmpty()) {
113                                 return Either.right(ActionStatus.USER_NOT_FOUND);
114                         } else {
115                                 return Either.left((UserData) value.get(0));
116                         }
117                 }
118         }
119
120         @Override
121         public ActionStatus saveUserData(UserData userData) {
122                 Neo4jOperationStatus status = neo4jClient.createElement(userData);
123                 if (status.equals(Neo4jOperationStatus.OK)) {
124                         return ActionStatus.OK;
125                 } else {
126                         return ActionStatus.GENERAL_ERROR;
127                 }
128         }
129
130         @Override
131         public ActionStatus updateUserData(UserData userData) {
132                 UpdateFilter filter = new UpdateFilter();
133                 filter.addToMatch(USER_ID, userData.getUserId());
134                 filter.setToUpdate(userData.toGraphMap());
135                 Neo4jOperationStatus status = neo4jClient.updateElement(GraphElementTypeEnum.Node, NodeTypeEnum.User.getName(),
136                                 filter);
137                 if (status.equals(Neo4jOperationStatus.OK)) {
138                         return ActionStatus.OK;
139                 } else {
140                         return ActionStatus.GENERAL_ERROR;
141                 }
142         }
143
144         @Override
145         public ActionStatus deleteUserData(String id) {
146                 MatchFilter filter = new MatchFilter();
147                 filter.addToMatch(USER_ID, id);
148                 Neo4jOperationStatus status = neo4jClient.deleteElement(GraphElementTypeEnum.Node, NodeTypeEnum.User.getName(),
149                                 filter);
150                 if (status.equals(Neo4jOperationStatus.OK)) {
151                         return ActionStatus.OK;
152                 } else {
153                         return ActionStatus.GENERAL_ERROR;
154                 }
155         }
156
157         public Neo4jClient getNeo4jClient() {
158                 return neo4jClient;
159         }
160
161         public void setNeo4jClient(Neo4jClient neo4jClient) {
162                 this.neo4jClient = neo4jClient;
163         }
164
165 }