summaryrefslogtreecommitdiffstats
path: root/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/impl/Neo4jUsersDAO.java
blob: 14489ef04b3b8e136d84513ab307184c9c51afcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.api.IUsersDAO;
import org.openecomp.sdc.be.dao.graph.datatype.GraphElement;
import org.openecomp.sdc.be.dao.graph.datatype.GraphElementTypeEnum;
import org.openecomp.sdc.be.dao.neo4j.Neo4jClient;
import org.openecomp.sdc.be.dao.neo4j.Neo4jOperationStatus;
import org.openecomp.sdc.be.dao.neo4j.filters.MatchFilter;
import org.openecomp.sdc.be.dao.neo4j.filters.UpdateFilter;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.resources.data.UserData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import fj.data.Either;

//@Component("users-dao")
public class Neo4jUsersDAO implements IUsersDAO {

	// @Resource
	Neo4jClient neo4jClient;

	private static Logger logger = LoggerFactory.getLogger(Neo4jUsersDAO.class.getName());

	public Neo4jUsersDAO() {

	}

	@PostConstruct
	public void init() {
	}

	private void createIndexesAndConstraints() {
		Either<Map<String, List<String>>, Neo4jOperationStatus> statusInd = neo4jClient
				.getIndexes(NodeTypeEnum.User.getName());
		if (statusInd.isRight()) {
			logger.error("Failed to get indexes from Neo4j graph");
			throw new RuntimeException("Failed to initialize Neo4jUsersDAO - Failed to get indexes from Neo4j graph");
		}
		Map<String, List<String>> indexes = statusInd.left().value();
		if (indexes == null || indexes.isEmpty()) {
			logger.info("Define users indexes in Neo4j");
			List<String> propertyNames = new ArrayList<String>();
			propertyNames.add("firstName");
			propertyNames.add("lastName");
			propertyNames.add("email");
			propertyNames.add("role");
			logger.info("Start create Users indexes in Neo4jGraph");
			Neo4jOperationStatus createIndexStatus = neo4jClient.createIndex(NodeTypeEnum.User.getName(),
					propertyNames);
			if (createIndexStatus.equals(Neo4jOperationStatus.OK)) {
				logger.info("Users indexes created in Neo4j");
				List<String> propertyUnique = new ArrayList<String>();
				propertyUnique.add("userId");

				logger.info("Start create Users constraints in Neo4jGraph");
				Neo4jOperationStatus createUniquenessStatus = neo4jClient
						.createUniquenessConstraints(NodeTypeEnum.User.getName(), propertyUnique);
				if (createUniquenessStatus.equals(Neo4jOperationStatus.OK)) {
					logger.info("Users constraints creatyed in Neo4j");
				} else {
					logger.error("Failed to create constraints in Neo4j graph [{}]", createUniquenessStatus);
					throw new RuntimeException(
							"Failed to initialize Neo4jUsersDAO - Failed to create constraints in Neo4j graph");
				}
			} else {
				logger.error("Failed to create indexes in Neo4j graph [{}]", createIndexStatus);
				throw new RuntimeException(
						"Failed to initialize Neo4jUsersDAO - Failed to create indexes in Neo4j graph");
			}
		} else {
			logger.info("Users indexes already defined in Neo4j");
		}
	}

	@Override
	public Either<UserData, ActionStatus> getUserData(String id) {
		MatchFilter filter = new MatchFilter();
		filter.addToMatch("userId", id);
		Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
				NodeTypeEnum.User.getName(), filter);
		if (status.isRight()) {
			return Either.right(ActionStatus.GENERAL_ERROR);
		} else {
			List<GraphElement> value = status.left().value();
			if (value == null || value.isEmpty()) {
				return Either.right(ActionStatus.USER_NOT_FOUND);
			} else {
				return Either.left((UserData) value.get(0));
			}
		}
	}

	@Override
	public ActionStatus saveUserData(UserData userData) {
		Neo4jOperationStatus status = neo4jClient.createElement(userData);
		if (status.equals(Neo4jOperationStatus.OK)) {
			return ActionStatus.OK;
		} else {
			return ActionStatus.GENERAL_ERROR;
		}
	}

	@Override
	public ActionStatus updateUserData(UserData userData) {
		UpdateFilter filter = new UpdateFilter();
		filter.addToMatch("userId", userData.getUserId());
		filter.setToUpdate(userData.toGraphMap());
		Neo4jOperationStatus status = neo4jClient.updateElement(GraphElementTypeEnum.Node, NodeTypeEnum.User.getName(),
				filter);
		if (status.equals(Neo4jOperationStatus.OK)) {
			return ActionStatus.OK;
		} else {
			return ActionStatus.GENERAL_ERROR;
		}
	}

	@Override
	public ActionStatus deleteUserData(String id) {
		MatchFilter filter = new MatchFilter();
		filter.addToMatch("userId", id);
		Neo4jOperationStatus status = neo4jClient.deleteElement(GraphElementTypeEnum.Node, NodeTypeEnum.User.getName(),
				filter);
		if (status.equals(Neo4jOperationStatus.OK)) {
			return ActionStatus.OK;
		} else {
			return ActionStatus.GENERAL_ERROR;
		}
	}

	public Neo4jClient getNeo4jClient() {
		return neo4jClient;
	}

	public void setNeo4jClient(Neo4jClient neo4jClient) {
		this.neo4jClient = neo4jClient;
	}

}