re base code
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / UpdatePropertyOnVertex.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.asdctool.impl;
22
23 import com.thinkaurelius.titan.core.TitanFactory;
24 import com.thinkaurelius.titan.core.TitanGraph;
25 import com.thinkaurelius.titan.core.TitanGraphQuery;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.openecomp.sdc.asdctool.Utils;
28 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
29 import org.openecomp.sdc.be.model.LifecycleStateEnum;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36
37 public class UpdatePropertyOnVertex {
38
39         private static Logger log = Logger.getLogger(UpdatePropertyOnVertex.class.getName());
40
41         public Integer updatePropertyOnServiceAtLeastCertified(String titanFile, Map<String, Object> keyValueToSet,
42                         List<Map<String, Object>> orCriteria) {
43
44                 TitanGraph graph = null;
45
46                 Integer numberOfUpdatedVertexes = 0;
47
48                 try {
49                         graph = openGraph(titanFile);
50
51                         if (orCriteria != null && false == orCriteria.isEmpty()) {
52
53                                 for (Map<String, Object> criteria : orCriteria) {
54
55                                         TitanGraphQuery<? extends TitanGraphQuery> query = graph.query();
56
57                                         if (criteria != null && !criteria.isEmpty()) {
58                                                 for (Map.Entry<String, Object> entry : criteria.entrySet()) {
59                                                         query = query.has(entry.getKey(), entry.getValue());
60                                                 }
61                                         }
62
63                                         Iterator iterator = query
64                                                         .has(GraphPropertiesDictionary.STATE.getProperty(), LifecycleStateEnum.CERTIFIED.name())
65                                                         .vertices().iterator();
66
67                                         boolean isFoundAtLeastOneCertifiedService = false;
68                                         while (iterator.hasNext()) {
69                                                 Vertex vertex = (Vertex) iterator.next();
70
71                                                 Map<String, Object> leftProps = Utils.getProperties(vertex);
72                                                 boolean vertexLeftContainsRightProps = Utils.vertexLeftContainsRightProps(leftProps, criteria);
73                                                 if (false == vertexLeftContainsRightProps) {
74                                                         log.debug("Ignore vertex since properties it does not contains properties {}. Vertex properties are {}",criteria,leftProps);
75                                                         continue;
76                                                 }
77
78                                                 isFoundAtLeastOneCertifiedService = true;
79                                                 break;
80                                         }
81
82                                         if (true == isFoundAtLeastOneCertifiedService) {
83
84                                                 Integer currentNumberOfUpdates = updateVertexes(keyValueToSet, graph, criteria);
85
86                                                 if (currentNumberOfUpdates != null) {
87                                                         numberOfUpdatedVertexes += currentNumberOfUpdates;
88                                                 }
89
90                                         } else {
91                                                 log.debug("No certified service was found for criteria {}",criteria);
92                                         }
93                                 }
94
95                         }
96
97                         // graph.commit();
98                         graph.tx().commit();
99
100                         return numberOfUpdatedVertexes;
101
102                 } catch (Exception e) {
103                         log.info("update Property On Service At Least Certified failed -{}" , e);
104                         // graph.rollback();
105                         graph.tx().rollback();
106
107                         return null;
108
109                 } finally {
110                         if (graph != null) {
111                                 // graph.shutdown();
112                                 graph.close();
113                         }
114                 }
115
116         }
117
118         private Integer updateVertexes(Map<String, Object> keyValueToSet, TitanGraph graph, Map<String, Object> criteria) {
119                 Integer numberOfUpdatedVertexesPerService = 0;
120
121                 TitanGraphQuery<? extends TitanGraphQuery> updateQuery = graph.query();
122
123                 if (criteria != null && !criteria.isEmpty()) {
124                         for (Map.Entry<String, Object> entry : criteria.entrySet()) {
125                                 updateQuery = updateQuery.has(entry.getKey(), entry.getValue());
126                         }
127                 }
128                 Iterator updateIterator = updateQuery.vertices().iterator();
129
130                 while (updateIterator.hasNext()) {
131
132                         Vertex vertex = (Vertex) updateIterator.next();
133
134                         Map<String, Object> leftProps = Utils.getProperties(vertex);
135
136                         boolean vertexLeftContainsRightProps = Utils.vertexLeftContainsRightProps(leftProps, criteria);
137                         if (false == vertexLeftContainsRightProps) {
138                                 log.debug("Ignore vertex since properties it does not contains properties {}. Vertex properties are {}",criteria,leftProps);
139                                 continue;
140                         }
141
142                         if (keyValueToSet != null) {
143                                 for (Entry<String, Object> entry : keyValueToSet.entrySet()) {
144                                         String key = entry.getKey();
145                                         Object value = entry.getValue();
146
147                                         // vertex.setProperty(key, value);
148                                         vertex.property(key, value);
149                                         
150                                         if (log.isDebugEnabled()){
151                                                 log.debug("After setting vertex {} {} with key value {},{}",  
152                                                         vertex.property(GraphPropertiesDictionary.NAME.getProperty()),
153                                                         vertex.property(GraphPropertiesDictionary.VERSION.getProperty()),key,value);
154                                         }
155                                         numberOfUpdatedVertexesPerService++;
156                                 }
157                         }
158
159                 }
160
161                 log.info(
162                                 "The number of updated services for criteria " + criteria + " is " + numberOfUpdatedVertexesPerService);
163                 return numberOfUpdatedVertexesPerService;
164         }
165
166         public TitanGraph openGraph(String titanFileLocation) {
167
168                 TitanGraph graph = TitanFactory.open(titanFileLocation);
169
170                 return graph;
171
172         }
173
174 }