2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.dao.impl;
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;
36 import javax.annotation.PostConstruct;
37 import java.util.ArrayList;
38 import java.util.List;
41 //@Component("users-dao")
42 public class Neo4jUsersDAO implements IUsersDAO {
44 private static final String USER_ID = "userId";
47 Neo4jClient neo4jClient;
49 private static Logger logger = Logger.getLogger(Neo4jUsersDAO.class.getName());
51 public Neo4jUsersDAO() {
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");
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(),
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);
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");
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");
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");
98 logger.info("Users indexes already defined in Neo4j");
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);
111 List<GraphElement> value = status.left().value();
112 if (value == null || value.isEmpty()) {
113 return Either.right(ActionStatus.USER_NOT_FOUND);
115 return Either.left((UserData) value.get(0));
121 public ActionStatus saveUserData(UserData userData) {
122 Neo4jOperationStatus status = neo4jClient.createElement(userData);
123 if (status.equals(Neo4jOperationStatus.OK)) {
124 return ActionStatus.OK;
126 return ActionStatus.GENERAL_ERROR;
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(),
137 if (status.equals(Neo4jOperationStatus.OK)) {
138 return ActionStatus.OK;
140 return ActionStatus.GENERAL_ERROR;
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(),
150 if (status.equals(Neo4jOperationStatus.OK)) {
151 return ActionStatus.OK;
153 return ActionStatus.GENERAL_ERROR;
157 public Neo4jClient getNeo4jClient() {
161 public void setNeo4jClient(Neo4jClient neo4jClient) {
162 this.neo4jClient = neo4jClient;