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.asdctool.impl;
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;
32 import java.util.Iterator;
33 import java.util.List;
35 import java.util.Map.Entry;
37 public class UpdatePropertyOnVertex {
39 private static Logger log = Logger.getLogger(UpdatePropertyOnVertex.class.getName());
41 public Integer updatePropertyOnServiceAtLeastCertified(String titanFile, Map<String, Object> keyValueToSet,
42 List<Map<String, Object>> orCriteria) {
44 TitanGraph graph = null;
46 Integer numberOfUpdatedVertexes = 0;
49 graph = openGraph(titanFile);
51 if (orCriteria != null && false == orCriteria.isEmpty()) {
53 for (Map<String, Object> criteria : orCriteria) {
55 TitanGraphQuery<? extends TitanGraphQuery> query = graph.query();
57 if (criteria != null && !criteria.isEmpty()) {
58 for (Map.Entry<String, Object> entry : criteria.entrySet()) {
59 query = query.has(entry.getKey(), entry.getValue());
63 Iterator iterator = query
64 .has(GraphPropertiesDictionary.STATE.getProperty(), LifecycleStateEnum.CERTIFIED.name())
65 .vertices().iterator();
67 boolean isFoundAtLeastOneCertifiedService = false;
68 while (iterator.hasNext()) {
69 Vertex vertex = (Vertex) iterator.next();
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);
78 isFoundAtLeastOneCertifiedService = true;
82 if (true == isFoundAtLeastOneCertifiedService) {
84 Integer currentNumberOfUpdates = updateVertexes(keyValueToSet, graph, criteria);
86 if (currentNumberOfUpdates != null) {
87 numberOfUpdatedVertexes += currentNumberOfUpdates;
91 log.debug("No certified service was found for criteria {}",criteria);
100 return numberOfUpdatedVertexes;
102 } catch (Exception e) {
103 log.info("update Property On Service At Least Certified failed -{}" , e);
105 graph.tx().rollback();
118 private Integer updateVertexes(Map<String, Object> keyValueToSet, TitanGraph graph, Map<String, Object> criteria) {
119 Integer numberOfUpdatedVertexesPerService = 0;
121 TitanGraphQuery<? extends TitanGraphQuery> updateQuery = graph.query();
123 if (criteria != null && !criteria.isEmpty()) {
124 for (Map.Entry<String, Object> entry : criteria.entrySet()) {
125 updateQuery = updateQuery.has(entry.getKey(), entry.getValue());
128 Iterator updateIterator = updateQuery.vertices().iterator();
130 while (updateIterator.hasNext()) {
132 Vertex vertex = (Vertex) updateIterator.next();
134 Map<String, Object> leftProps = Utils.getProperties(vertex);
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);
142 if (keyValueToSet != null) {
143 for (Entry<String, Object> entry : keyValueToSet.entrySet()) {
144 String key = entry.getKey();
145 Object value = entry.getValue();
147 // vertex.setProperty(key, value);
148 vertex.property(key, value);
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);
155 numberOfUpdatedVertexesPerService++;
162 "The number of updated services for criteria " + criteria + " is " + numberOfUpdatedVertexesPerService);
163 return numberOfUpdatedVertexesPerService;
166 public TitanGraph openGraph(String titanFileLocation) {
168 TitanGraph graph = TitanFactory.open(titanFileLocation);