aboutsummaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/UpdatePropertyOnVertex.java
blob: 9911fb73f36ba89ca68c72a6dd44ecb09f2caa28 (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
170
/*-
 * ============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.asdctool.impl;

import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphQuery;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.openecomp.sdc.asdctool.Utils;
import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.common.log.wrappers.Logger;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class UpdatePropertyOnVertex {

	private static Logger log = Logger.getLogger(UpdatePropertyOnVertex.class.getName());

	public Integer updatePropertyOnServiceAtLeastCertified(String janusGraphFile, Map<String, Object> keyValueToSet,
			List<Map<String, Object>> orCriteria) {

		JanusGraph graph = null;

		Integer numberOfUpdatedVertexes = 0;

		try {
			graph = openGraph(janusGraphFile);

			if (orCriteria != null && false == orCriteria.isEmpty()) {

				for (Map<String, Object> criteria : orCriteria) {

					JanusGraphQuery<? extends JanusGraphQuery> query = graph.query();

					if (criteria != null && !criteria.isEmpty()) {
						for (Map.Entry<String, Object> entry : criteria.entrySet()) {
							query = query.has(entry.getKey(), entry.getValue());
						}
					}

					Iterator iterator = query
							.has(GraphPropertiesDictionary.STATE.getProperty(), LifecycleStateEnum.CERTIFIED.name())
							.vertices().iterator();

					boolean isFoundAtLeastOneCertifiedService = false;
					while (iterator.hasNext()) {
						Vertex vertex = (Vertex) iterator.next();

						Map<String, Object> leftProps = Utils.getProperties(vertex);
						boolean vertexLeftContainsRightProps = Utils.vertexLeftContainsRightProps(leftProps, criteria);
						if (false == vertexLeftContainsRightProps) {
							log.debug("Ignore vertex since properties it does not contains properties {}. Vertex properties are {}",criteria,leftProps);
							continue;
						}

						isFoundAtLeastOneCertifiedService = true;
						break;
					}

					if (true == isFoundAtLeastOneCertifiedService) {

						Integer currentNumberOfUpdates = updateVertexes(keyValueToSet, graph, criteria);

						if (currentNumberOfUpdates != null) {
							numberOfUpdatedVertexes += currentNumberOfUpdates;
						}

					} else {
						log.debug("No certified service was found for criteria {}",criteria);
					}
				}

			}

			graph.tx().commit();

			return numberOfUpdatedVertexes;

		} catch (Exception e) {
			log.info("update Property On Service At Least Certified failed -{}" , e);
			graph.tx().rollback();

			return null;

		} finally {
			if (graph != null) {
				graph.close();
			}
		}

	}

	private Integer updateVertexes(Map<String, Object> keyValueToSet, JanusGraph graph, Map<String, Object> criteria) {
		Integer numberOfUpdatedVertexesPerService = 0;

		JanusGraphQuery<? extends JanusGraphQuery> updateQuery = graph.query();

		if (criteria != null && !criteria.isEmpty()) {
			for (Map.Entry<String, Object> entry : criteria.entrySet()) {
				updateQuery = updateQuery.has(entry.getKey(), entry.getValue());
			}
		}
		Iterator updateIterator = updateQuery.vertices().iterator();

		while (updateIterator.hasNext()) {

			Vertex vertex = (Vertex) updateIterator.next();

			Map<String, Object> leftProps = Utils.getProperties(vertex);

			boolean vertexLeftContainsRightProps = Utils.vertexLeftContainsRightProps(leftProps, criteria);
			if (false == vertexLeftContainsRightProps) {
				log.debug("Ignore vertex since properties it does not contains properties {}. Vertex properties are {}",criteria,leftProps);
				continue;
			}

			if (keyValueToSet != null) {
				for (Entry<String, Object> entry : keyValueToSet.entrySet()) {
					String key = entry.getKey();
					Object value = entry.getValue();

					vertex.property(key, value);
					
					if (log.isDebugEnabled()){
						log.debug("After setting vertex {} {} with key value {},{}",  
							vertex.property(GraphPropertiesDictionary.NAME.getProperty()),
							vertex.property(GraphPropertiesDictionary.VERSION.getProperty()),key,value);
					}
					numberOfUpdatedVertexesPerService++;
				}
			}

		}

		log.info(
				"The number of updated services for criteria " + criteria + " is " + numberOfUpdatedVertexesPerService);
		return numberOfUpdatedVertexesPerService;
	}

	public JanusGraph openGraph(String janusGraphFileLocation) {

		JanusGraph graph = JanusGraphFactory.open(janusGraphFileLocation);

		return graph;

	}

}