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 org.janusgraph.core.JanusGraphFactory;
24 import org.janusgraph.core.JanusGraph;
25 import org.janusgraph.core.JanusGraphQuery;
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 janusGraphFile, Map<String, Object> keyValueToSet,
42 List<Map<String, Object>> orCriteria) {
44 JanusGraph graph = null;
46 Integer numberOfUpdatedVertexes = 0;
49 graph = openGraph(janusGraphFile);
51 if (orCriteria != null && false == orCriteria.isEmpty()) {
53 for (Map<String, Object> criteria : orCriteria) {
55 JanusGraphQuery<? extends JanusGraphQuery> 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);
99 return numberOfUpdatedVertexes;
101 } catch (Exception e) {
102 log.info("update Property On Service At Least Certified failed -{}" , e);
103 graph.tx().rollback();
115 private Integer updateVertexes(Map<String, Object> keyValueToSet, JanusGraph graph, Map<String, Object> criteria) {
116 Integer numberOfUpdatedVertexesPerService = 0;
118 JanusGraphQuery<? extends JanusGraphQuery> updateQuery = graph.query();
120 if (criteria != null && !criteria.isEmpty()) {
121 for (Map.Entry<String, Object> entry : criteria.entrySet()) {
122 updateQuery = updateQuery.has(entry.getKey(), entry.getValue());
125 Iterator updateIterator = updateQuery.vertices().iterator();
127 while (updateIterator.hasNext()) {
129 Vertex vertex = (Vertex) updateIterator.next();
131 Map<String, Object> leftProps = Utils.getProperties(vertex);
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);
139 if (keyValueToSet != null) {
140 for (Entry<String, Object> entry : keyValueToSet.entrySet()) {
141 String key = entry.getKey();
142 Object value = entry.getValue();
144 vertex.property(key, value);
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);
151 numberOfUpdatedVertexesPerService++;
158 "The number of updated services for criteria " + criteria + " is " + numberOfUpdatedVertexesPerService);
159 return numberOfUpdatedVertexesPerService;
162 public JanusGraph openGraph(String janusGraphFileLocation) {
164 JanusGraph graph = JanusGraphFactory.open(janusGraphFileLocation);