Catalog alignment
[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 org.apache.tinkerpop.gremlin.structure.Vertex;
24 import org.janusgraph.core.JanusGraph;
25 import org.janusgraph.core.JanusGraphFactory;
26 import org.janusgraph.core.JanusGraphQuery;
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 janusGraphFile, Map<String, Object> keyValueToSet,
42                         List<Map<String, Object>> orCriteria) {
43
44                 JanusGraph graph = null;
45
46                 Integer numberOfUpdatedVertexes = 0;
47
48                 try {
49                         graph = openGraph(janusGraphFile);
50
51                         if (orCriteria != null && false == orCriteria.isEmpty()) {
52
53                                 for (Map<String, Object> criteria : orCriteria) {
54
55                                         JanusGraphQuery<? extends JanusGraphQuery> 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.tx().commit();
98
99                         return numberOfUpdatedVertexes;
100
101                 } catch (Exception e) {
102                         e.printStackTrace();
103                         graph.tx().rollback();
104
105                         return null;
106
107                 } finally {
108                         if (graph != null) {
109                                 graph.close();
110                         }
111                 }
112
113         }
114
115         private Integer updateVertexes(Map<String, Object> keyValueToSet, JanusGraph graph, Map<String, Object> criteria) {
116                 Integer numberOfUpdatedVertexesPerService = 0;
117
118                 JanusGraphQuery<? extends JanusGraphQuery> updateQuery = graph.query();
119
120                 if (criteria != null && !criteria.isEmpty()) {
121                         for (Map.Entry<String, Object> entry : criteria.entrySet()) {
122                                 updateQuery = updateQuery.has(entry.getKey(), entry.getValue());
123                         }
124                 }
125                 Iterator updateIterator = updateQuery.vertices().iterator();
126
127                 while (updateIterator.hasNext()) {
128
129                         Vertex vertex = (Vertex) updateIterator.next();
130
131                         Map<String, Object> leftProps = Utils.getProperties(vertex);
132
133                         boolean vertexLeftContainsRightProps = Utils.vertexLeftContainsRightProps(leftProps, criteria);
134                         if (false == vertexLeftContainsRightProps) {
135                                 log.debug("Ignore vertex since properties it does not contains properties {}. Vertex properties are {}",criteria,leftProps);
136                                 continue;
137                         }
138
139                         if (keyValueToSet != null) {
140                                 for (Entry<String, Object> entry : keyValueToSet.entrySet()) {
141                                         String key = entry.getKey();
142                                         Object value = entry.getValue();
143
144                                         vertex.property(key, value);
145                                         
146                                         if (log.isDebugEnabled()){
147                                                 log.debug("After setting vertex {} {} with key value {},{}",  
148                                                         vertex.property(GraphPropertiesDictionary.NAME.getProperty()),
149                                                         vertex.property(GraphPropertiesDictionary.VERSION.getProperty()),key,value);
150                                         }
151                                         numberOfUpdatedVertexesPerService++;
152                                 }
153                         }
154
155                 }
156
157                 log.info(
158                                 "The number of updated services for criteria " + criteria + " is " + numberOfUpdatedVertexesPerService);
159                 return numberOfUpdatedVertexesPerService;
160         }
161
162         public JanusGraph openGraph(String janusGraphFileLocation) {
163
164                 JanusGraph graph = JanusGraphFactory.open(janusGraphFileLocation);
165
166                 return graph;
167
168         }
169
170 }