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