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 {
45 Neo4jClient neo4jClient;
47 private static Logger logger = Logger.getLogger(Neo4jUsersDAO.class.getName());
49 public Neo4jUsersDAO() {
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");
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(),
75 if (createIndexStatus.equals(Neo4jOperationStatus.OK)) {
76 logger.info("Users indexes created in Neo4j");
77 List<String> propertyUnique = new ArrayList<>();
78 propertyUnique.add("userId");
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");
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");
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");
96 logger.info("Users indexes already defined in Neo4j");
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);
109 List<GraphElement> value = status.left().value();
110 if (value == null || value.isEmpty()) {
111 return Either.right(ActionStatus.USER_NOT_FOUND);
113 return Either.left((UserData) value.get(0));
119 public ActionStatus saveUserData(UserData userData) {
120 Neo4jOperationStatus status = neo4jClient.createElement(userData);
121 if (status.equals(Neo4jOperationStatus.OK)) {
122 return ActionStatus.OK;
124 return ActionStatus.GENERAL_ERROR;
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(),
135 if (status.equals(Neo4jOperationStatus.OK)) {
136 return ActionStatus.OK;
138 return ActionStatus.GENERAL_ERROR;
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(),
148 if (status.equals(Neo4jOperationStatus.OK)) {
149 return ActionStatus.OK;
151 return ActionStatus.GENERAL_ERROR;
155 public Neo4jClient getNeo4jClient() {
159 public void setNeo4jClient(Neo4jClient neo4jClient) {
160 this.neo4jClient = neo4jClient;