1 package org.openecomp.sdc.be.model.jsontitan.operations;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
9 import java.util.stream.Collectors;
11 import org.apache.commons.collections.CollectionUtils;
12 import org.apache.commons.collections.MapUtils;
13 import org.apache.commons.lang.StringUtils;
14 import org.apache.commons.lang3.tuple.ImmutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.apache.tinkerpop.gremlin.structure.Direction;
17 import org.apache.tinkerpop.gremlin.structure.Edge;
18 import org.apache.tinkerpop.gremlin.structure.Vertex;
19 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
20 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
21 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
22 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
23 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
24 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
25 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
26 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
27 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.GroupInstanceDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
32 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
33 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
34 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
35 import org.openecomp.sdc.be.model.ComponentInstance;
36 import org.openecomp.sdc.be.model.GroupDefinition;
37 import org.openecomp.sdc.be.model.User;
38 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
39 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
40 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
41 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
42 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
43 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
44 import org.openecomp.sdc.common.util.ValidationUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
49 import com.thinkaurelius.titan.core.TitanVertex;
51 import fj.data.Either;
54 * public abstract class BaseOperation provides base operation functionality and common fields
57 public abstract class BaseOperation {
59 private static Logger logger = LoggerFactory.getLogger(BaseOperation.class.getName());
61 public static final String VF_MODULE = "org.openecomp.groups.VfModule";
64 protected TitanDao titanDao;
67 protected NodeTypeOperation nodeTypeOperation;
70 protected TopologyTemplateOperation topologyTemplateOperation;
73 * Returns reference to appropriate toscaTemplateOperation
75 * @param componentType
78 public ToscaElementOperation getToscaElementOperation(ComponentTypeEnum componentType) {
79 ToscaElementOperation operation;
80 switch (componentType) {
83 operation = topologyTemplateOperation;
86 operation = nodeTypeOperation;
93 * Returns reference to appropriate toscaTemplateOperation
95 * @param toscaElementType
98 public ToscaElementOperation getToscaElementOperation(ToscaElementTypeEnum toscaElementType) {
99 ToscaElementOperation operation;
100 switch (toscaElementType) {
101 case TopologyTemplate:
102 operation = topologyTemplateOperation;
105 operation = nodeTypeOperation;
115 * Returns reference to appropriate toscaTemplateOperation
117 * @param toscaElementType
120 public ToscaElementOperation getToscaElementOperation(VertexTypeEnum toscaElementType) {
121 ToscaElementOperation operation;
122 switch (toscaElementType) {
123 case TOPOLOGY_TEMPLATE:
124 operation = topologyTemplateOperation;
127 operation = nodeTypeOperation;
136 * Converts received vertex to User object
141 public User convertToUser(Vertex ownerV) {
142 User owner = new User();
143 owner.setUserId((String) ownerV.property(GraphPropertyEnum.USERID.getProperty()).value());
144 VertexProperty<Object> property = ownerV.property(GraphPropertyEnum.ROLE.getProperty());
145 if(property != null && property.isPresent() ){
146 owner.setRole((String) property.value());
149 property = ownerV.property(GraphPropertyEnum.FIRST_NAME.getProperty());
150 if(property != null && property.isPresent() ){
151 owner.setFirstName((String) ownerV.property(GraphPropertyEnum.FIRST_NAME.getProperty()).value());
154 property = ownerV.property(GraphPropertyEnum.LAST_NAME.getProperty());
155 if( property != null && property.isPresent() ){
156 owner.setLastName((String) ownerV.property(GraphPropertyEnum.LAST_NAME.getProperty()).value());
159 property = ownerV.property(GraphPropertyEnum.EMAIL.getProperty());
160 if( property != null && property.isPresent() ){
161 owner.setEmail((String) ownerV.property(GraphPropertyEnum.EMAIL.getProperty()).value());
164 property = ownerV.property(GraphPropertyEnum.LAST_LOGIN_TIME.getProperty());
165 if( property != null && property.isPresent() ){
166 owner.setLastLoginTime((Long) ownerV.property(GraphPropertyEnum.LAST_LOGIN_TIME.getProperty()).value());
171 protected <T extends ToscaDataDefinition> Either<Map<String, T>, TitanOperationStatus> getDataFromGraph(GraphVertex componentV, EdgeLabelEnum edgelabel) {
172 Either<Pair<GraphVertex, Map<String, T>>, TitanOperationStatus> dataVertex = getDataAndVertexFromGraph(componentV, edgelabel);
173 if (dataVertex.isRight()) {
174 return Either.right(dataVertex.right().value());
176 Map<String, T> properties = dataVertex.left().value().getRight();
177 return Either.left(properties);
180 @SuppressWarnings("unchecked")
181 protected <T extends ToscaDataDefinition> Either<Pair<GraphVertex, Map<String, T>>, TitanOperationStatus> getDataAndVertexFromGraph(GraphVertex componentV, EdgeLabelEnum edgelabel) {
182 Either<GraphVertex, TitanOperationStatus> dataVertex = getDataVertex(componentV, edgelabel);
183 if (dataVertex.isRight()) {
184 return Either.right(dataVertex.right().value());
186 GraphVertex propV = dataVertex.left().value();
187 Map<String, T> properties = (Map<String, T>) propV.getJson();
188 Pair<GraphVertex, Map<String, T>> pair = new ImmutablePair<GraphVertex, Map<String, T>>(propV, properties);
189 return Either.left(pair);
192 protected <T extends ToscaDataDefinition> Either<GraphVertex, TitanOperationStatus> getDataVertex(GraphVertex componentV, EdgeLabelEnum edgelabel) {
193 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, edgelabel, JsonParseFlagEnum.ParseJson);
194 if (childVertex.isRight()) {
195 if (childVertex.right().value() != TitanOperationStatus.NOT_FOUND) {
196 logger.debug("failed to fetch {} for tosca element with id {}, error {}", edgelabel, componentV.getUniqueId(), childVertex.right().value());
198 return Either.right(childVertex.right().value());
200 GraphVertex propV = childVertex.left().value();
201 return Either.left(propV);
205 * Returns tosca data belonging to tosca element specified by uid according received label
207 * @param toscaElementUid
211 public <T extends ToscaDataDefinition> Either<Map<String, T>, TitanOperationStatus> getDataFromGraph(String toscaElementUid, EdgeLabelEnum edgelabel) {
213 Either<Map<String, T>, TitanOperationStatus> result = null;
214 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
216 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
217 if (getToscaElementRes.isRight()) {
218 TitanOperationStatus status = getToscaElementRes.right().value();
219 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon getting tosca data from graph. Status is {}. ", toscaElementUid, status);
220 result = Either.right(status);
222 if (result == null) {
223 result = getDataFromGraph(getToscaElementRes.left().value(), edgelabel);
228 public Either<GraphVertex, TitanOperationStatus> findUserVertex(String userId) {
229 return titanDao.getVertexByPropertyAndLabel(GraphPropertyEnum.USERID, userId, VertexTypeEnum.USER, JsonParseFlagEnum.NoParse);
238 public Either<Boolean, StorageOperationStatus> isCloneNeeded(String elemementId, EdgeLabelEnum label) {
239 Either<GraphVertex, TitanOperationStatus> vertexById = titanDao.getVertexById(elemementId);
240 if (vertexById.isRight()) {
241 logger.debug("Failed to fetch element by id {} error {}", elemementId, vertexById.right().value());
242 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexById.right().value()));
244 GraphVertex toscaElementVertex = vertexById.left().value();
245 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(toscaElementVertex, label, JsonParseFlagEnum.NoParse);
246 if (childVertex.isRight()) {
247 if (childVertex.right().value() != TitanOperationStatus.NOT_FOUND) {
248 logger.debug("failed to fetch {} for tosca element with id {}, error {}", label, toscaElementVertex.getUniqueId(), childVertex.right().value());
249 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value()));
251 return Either.left(Boolean.FALSE);
253 GraphVertex dataVertex = childVertex.left().value();
254 Iterator<Edge> edges = dataVertex.getVertex().edges(Direction.IN, label.name());
256 while (edges.hasNext()) {
261 return Either.left(Boolean.TRUE);
263 return Either.left(Boolean.FALSE);
267 protected Either<GraphVertex, TitanOperationStatus> updateOrCopyOnUpdate(GraphVertex dataVertex, GraphVertex toscaElementVertex, EdgeLabelEnum label) {
268 Iterator<Edge> edges = dataVertex.getVertex().edges(Direction.IN, label.name());
270 Edge edgeToRemove = null;
271 while (edges.hasNext()) {
272 Edge edge = edges.next();
274 Vertex outVertex = edge.outVertex();
275 String outId = (String) titanDao.getProperty((TitanVertex) outVertex, GraphPropertyEnum.UNIQUE_ID.getProperty());
276 if (toscaElementVertex.getUniqueId().equals(outId)) {
280 if (edgeToRemove == null) {
281 logger.debug("No edges {} from vertex {} to vertex {}", label, toscaElementVertex.getUniqueId(), dataVertex.getUniqueId());
282 return Either.right(TitanOperationStatus.GENERAL_ERROR);
287 logger.debug("No edges {} to vertex {}", label, dataVertex.getUniqueId());
288 return Either.right(TitanOperationStatus.GENERAL_ERROR);
291 logger.trace("Only one edge {} to vertex {}. Update vertex", label, dataVertex.getUniqueId());
292 return titanDao.updateVertex(dataVertex);
295 logger.trace("More than one edge {} to vertex {}. Need to clone vertex", label, dataVertex.getUniqueId());
296 return cloneDataVertex(dataVertex, toscaElementVertex, label, edgeToRemove);
300 private Either<GraphVertex, TitanOperationStatus> cloneDataVertex(GraphVertex dataVertex, GraphVertex toscaElementVertex, EdgeLabelEnum label, Edge edgeToRemove) {
301 GraphVertex newDataVertex = new GraphVertex(dataVertex.getLabel());
302 String id = IdBuilderUtils.generateChildId(toscaElementVertex.getUniqueId(), dataVertex.getLabel());
303 newDataVertex.setUniqueId(id);
304 newDataVertex.cloneData(dataVertex);
306 Either<GraphVertex, TitanOperationStatus> createVertex = titanDao.createVertex(newDataVertex);
307 if (createVertex.isRight()) {
308 logger.debug("Failed to clone data vertex for {} error {}", dataVertex.getUniqueId(), createVertex.right().value());
311 newDataVertex = createVertex.left().value();
312 TitanOperationStatus createEdge = titanDao.createEdge(toscaElementVertex, newDataVertex, label, titanDao.getEdgeProperties(edgeToRemove));
313 if (createEdge != TitanOperationStatus.OK) {
314 logger.debug("Failed to associate vertex {} to vertex {}, error {}", toscaElementVertex.getUniqueId(), newDataVertex.getUniqueId(), createEdge);
315 return Either.right(createEdge);
317 edgeToRemove.remove();
318 return Either.left(newDataVertex);
321 public Either<GraphVertex, StorageOperationStatus> assosiateElementToData(GraphVertex element, VertexTypeEnum vertexLabel, EdgeLabelEnum edgeLabel, Map<String, ? extends ToscaDataDefinition> data) {
322 GraphVertex dataV = new GraphVertex(vertexLabel);
323 String id = IdBuilderUtils.generateChildId(element.getUniqueId(), vertexLabel);
324 dataV.setUniqueId(id);
326 Either<GraphVertex, TitanOperationStatus> createVertex = titanDao.createVertex(dataV);
327 if (createVertex.isRight()) {
328 logger.trace("Failed to create {} vertex for type node {}", vertexLabel, element.getUniqueId());
329 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(createVertex.right().value()));
331 dataV = createVertex.left().value();
332 TitanOperationStatus createEdgeStatus = titanDao.createEdge(element.getVertex(), dataV.getVertex(), edgeLabel, new HashMap<>());
333 if (createEdgeStatus != TitanOperationStatus.OK) {
334 logger.trace("Failed to create {} vertex for type node {}", vertexLabel, element.getUniqueId());
335 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(createEdgeStatus));
337 return Either.left(dataV);
341 * Adds tosca data element to tosca element according received labels
343 * @param toscaElement
350 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataToToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, T toscaData, JsonPresentationFields mapKeyField) {
352 List<T> toscaDataList = new ArrayList<>();
353 toscaDataList.add(toscaData);
354 return addToscaDataToToscaElement(toscaElement, edgeLabel, vertexLabel, toscaDataList, mapKeyField);
358 * Adds tosca data deep element to tosca element according received labels
360 * @param toscaElement
368 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataDeepElementToToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, T toscaData, List<String> pathKeys,
369 JsonPresentationFields mapKeyField) {
371 List<T> toscaDataList = new ArrayList<>();
372 toscaDataList.add(toscaData);
373 return addToscaDataDeepElementsToToscaElement(toscaElement, edgeLabel, vertexLabel, toscaDataList, pathKeys, mapKeyField);
377 * Converts recieved map of tosca data deep elements to list and adds it to tosca element according received labels
379 * @param toscaElement
382 * @param toscaDataMap
387 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataDeepElementsToToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, Map<String, T> toscaDataMap, List<String> pathKeys,
388 JsonPresentationFields mapKeyField) {
390 if (toscaDataMap != null) {
391 return addToscaDataDeepElementsToToscaElement(toscaElement, edgeLabel, vertexLabel, toscaDataMap.values().stream().collect(Collectors.toList()), pathKeys, mapKeyField);
393 return StorageOperationStatus.OK;
397 * Adds list of tosca data deep elements to tosca element according received labels
399 * @param toscaElement
402 * @param toscaDataList
407 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataDeepElementsToToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<T> toscaDataList, List<String> pathKeys,
408 JsonPresentationFields mapKeyField) {
410 return updateOrAddToscaDataDeepElement(toscaElement, edgeLabel, vertexLabel, toscaDataList, pathKeys, mapKeyField, false);
414 * Updates list of tosca data elements of tosca element according received labels
416 * @param toscaElement
423 public <T extends ToscaDataDefinition> StorageOperationStatus updateToscaDataOfToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, T toscaData, JsonPresentationFields mapKeyField) {
424 List<T> toscaDataList = new ArrayList<>();
425 toscaDataList.add(toscaData);
426 return updateToscaDataOfToscaElement(toscaElement, edgeLabel, vertexLabel, toscaDataList, mapKeyField);
430 * Updates tosca data deep element of tosca element according received labels
432 * @param toscaElement
440 public <T extends ToscaDataDefinition> StorageOperationStatus updateToscaDataDeepElementOfToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, T toscaData, List<String> pathKeys,
441 JsonPresentationFields mapKeyField) {
442 List<T> toscaDataList = new ArrayList<>();
443 toscaDataList.add(toscaData);
444 return updateToscaDataDeepElementsOfToscaElement(toscaElement, edgeLabel, vertexLabel, toscaDataList, pathKeys, mapKeyField);
448 * Updates tosca data deep elements of tosca element according received labels
450 * @param toscaElement
453 * @param toscaDataList
458 public <T extends ToscaDataDefinition> StorageOperationStatus updateToscaDataDeepElementsOfToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<T> toscaDataList, List<String> pathKeys,
459 JsonPresentationFields mapKeyField) {
461 return updateOrAddToscaDataDeepElement(toscaElement, edgeLabel, vertexLabel, toscaDataList, pathKeys, mapKeyField, true);
465 * Adds tosca data element to tosca element with specified uid according received labels
467 * @param toscaElementUid
474 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataToToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, T toscaData, JsonPresentationFields mapKeyField) {
476 List<T> toscaDataList = new ArrayList<>();
477 toscaDataList.add(toscaData);
478 return addToscaDataToToscaElement(toscaElementUid, edgeLabel, vertexLabel, toscaDataList, mapKeyField);
482 * Adds tosca data deep element to tosca element with specified uid according received labels
484 * @param toscaElementUid
492 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataDeepElementToToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, T toscaData, List<String> pathKeys,
493 JsonPresentationFields mapKeyField) {
495 List<T> toscaDataList = new ArrayList<>();
496 toscaDataList.add(toscaData);
497 return addToscaDataDeepElementsToToscaElement(toscaElementUid, edgeLabel, vertexLabel, toscaDataList, pathKeys, mapKeyField);
500 public <T extends ToscaDataDefinition> StorageOperationStatus updateToscaDataDeepElementOfToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, T toscaData, List<String> pathKeys,
501 JsonPresentationFields mapKeyField) {
503 List<T> toscaDataList = new ArrayList<>();
504 toscaDataList.add(toscaData);
505 return updateToscaDataDeepElementsOfToscaElement(toscaElementUid, edgeLabel, vertexLabel, toscaDataList, pathKeys, mapKeyField);
508 public <T extends ToscaDataDefinition> StorageOperationStatus updateToscaDataDeepElementsOfToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<T> toscaDataList, List<String> pathKeys,
509 JsonPresentationFields mapKeyField) {
511 StorageOperationStatus statusRes = null;
512 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
514 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
515 if (getToscaElementRes.isRight()) {
516 TitanOperationStatus status = getToscaElementRes.right().value();
517 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
518 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
520 if (statusRes == null && CollectionUtils.isNotEmpty(toscaDataList)) {
521 statusRes = updateToscaDataDeepElementsOfToscaElement(getToscaElementRes.left().value(), edgeLabel, vertexLabel, toscaDataList, pathKeys, mapKeyField);
523 if (statusRes == null) {
524 statusRes = StorageOperationStatus.OK;
530 * Adds list of tosca data deep elements to tosca element with specified uid according received labels
532 * @param toscaElementUid
535 * @param toscaDataList
540 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataDeepElementsToToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<T> toscaDataList, List<String> pathKeys,
541 JsonPresentationFields mapKeyField) {
543 StorageOperationStatus statusRes = null;
544 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
546 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
547 if (getToscaElementRes.isRight()) {
548 TitanOperationStatus status = getToscaElementRes.right().value();
549 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
550 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
552 if (statusRes == null && CollectionUtils.isNotEmpty(toscaDataList)) {
553 statusRes = addToscaDataDeepElementsToToscaElement(getToscaElementRes.left().value(), edgeLabel, vertexLabel, toscaDataList, pathKeys, mapKeyField);
555 if (statusRes == null) {
556 statusRes = StorageOperationStatus.OK;
561 public <T extends ToscaDataDefinition> StorageOperationStatus deleteToscaDataDeepElementsBlockToToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, String key) {
563 StorageOperationStatus statusRes = null;
564 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
566 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
567 if (getToscaElementRes.isRight()) {
568 TitanOperationStatus status = getToscaElementRes.right().value();
569 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
570 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
572 if (statusRes == null) {
573 statusRes = deleteToscaDataDeepElementsBlockToToscaElement(getToscaElementRes.left().value(), edgeLabel, vertexLabel, key);
575 if (statusRes == null) {
576 statusRes = StorageOperationStatus.OK;
581 public <T extends ToscaDataDefinition> StorageOperationStatus deleteToscaDataDeepElementsBlockToToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, String key) {
583 StorageOperationStatus result = null;
584 GraphVertex toscaDataVertex = null;
585 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = titanDao.getChildVertex(toscaElement, edgeLabel, JsonParseFlagEnum.ParseJson);
586 if (toscaDataVertexRes.isRight()) {
587 TitanOperationStatus status = toscaDataVertexRes.right().value();
588 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get child vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, status);
589 result = DaoStatusConverter.convertTitanStatusToStorageStatus(toscaDataVertexRes.right().value());
591 if (result == null) {
592 toscaDataVertex = toscaDataVertexRes.left().value();
593 result = deleteDeepElementsBlock(toscaDataVertex, key);
595 if (result == null) {
596 Either<GraphVertex, TitanOperationStatus> updateOrCopyRes = updateOrCopyOnUpdate(toscaDataVertex, toscaElement, edgeLabel);
597 if (updateOrCopyRes.isRight()) {
598 TitanOperationStatus status = updateOrCopyRes.right().value();
599 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to delete tosca data block {} from the tosca element {}. Status is {}. ", edgeLabel, toscaElement.getUniqueId(), status);
600 result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
603 if (result == null) {
604 result = StorageOperationStatus.OK;
609 @SuppressWarnings("rawtypes")
610 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataDeepElementsBlockToToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, MapDataDefinition toscaDataMap, String key) {
612 StorageOperationStatus statusRes = null;
613 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
615 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
616 if (getToscaElementRes.isRight()) {
617 TitanOperationStatus status = getToscaElementRes.right().value();
618 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
619 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
621 if (statusRes == null && toscaDataMap != null) {
622 statusRes = addToscaDataDeepElementsBlockToToscaElement(getToscaElementRes.left().value(), edgeLabel, vertexLabel, toscaDataMap, key);
624 if (statusRes == null) {
625 statusRes = StorageOperationStatus.OK;
630 @SuppressWarnings("rawtypes")
631 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataDeepElementsBlockToToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, MapDataDefinition toscaDataMap, String key) {
633 StorageOperationStatus result = null;
634 GraphVertex toscaDataVertex = null;
635 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = titanDao.getChildVertex(toscaElement, edgeLabel, JsonParseFlagEnum.ParseJson);
636 if (toscaDataVertexRes.isRight() && toscaDataVertexRes.right().value() != TitanOperationStatus.NOT_FOUND) {
637 TitanOperationStatus status = toscaDataVertexRes.right().value();
638 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get child vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, status);
639 result = DaoStatusConverter.convertTitanStatusToStorageStatus(toscaDataVertexRes.right().value());
641 if (result == null) {
642 if (toscaDataVertexRes.isLeft()) {
643 toscaDataVertex = toscaDataVertexRes.left().value();
644 result = addDeepElementsBlock(toscaDataVertex, toscaDataMap, key);
647 if (result == null) {
648 if (toscaDataVertex != null) {
649 Either<GraphVertex, TitanOperationStatus> updateOrCopyRes = updateOrCopyOnUpdate(toscaDataVertex, toscaElement, edgeLabel);
650 if (updateOrCopyRes.isRight()) {
651 TitanOperationStatus status = updateOrCopyRes.right().value();
652 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to add tosca data {} to the tosca element {}. Status is {}. ", edgeLabel, toscaElement.getUniqueId(), status);
653 result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
656 Map<String, MapDataDefinition> data = new HashMap<>();
657 data.put(key, toscaDataMap);
658 Either<GraphVertex, StorageOperationStatus> createRes = assosiateElementToData(toscaElement, vertexLabel, edgeLabel, data);
659 if (createRes.isRight()) {
660 StorageOperationStatus status = createRes.right().value();
661 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to assosiate tosca data {} of the tosca element {}. Status is {}. ", edgeLabel, toscaElement.getUniqueId(), status);
666 if (result == null) {
667 result = StorageOperationStatus.OK;
673 * Updates tosca data element of tosca element by specified uid according received labels
675 * @param toscaElementUid
682 public <T extends ToscaDataDefinition> StorageOperationStatus updateToscaDataOfToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, T toscaData, JsonPresentationFields mapKeyField) {
684 List<T> toscaDataList = new ArrayList<>();
685 toscaDataList.add(toscaData);
686 return updateToscaDataOfToscaElement(toscaElementUid, edgeLabel, vertexLabel, toscaDataList, mapKeyField);
690 * Updates list of tosca data elements belonging to tosca element with specified uid according received labels
692 * @param toscaElementUid
695 * @param toscaDataList
699 public <T extends ToscaDataDefinition> StorageOperationStatus updateToscaDataOfToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<T> toscaDataList, JsonPresentationFields mapKeyField) {
701 StorageOperationStatus statusRes = null;
702 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
704 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
705 if (getToscaElementRes.isRight()) {
706 TitanOperationStatus status = getToscaElementRes.right().value();
707 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
708 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
710 if (statusRes == null && CollectionUtils.isNotEmpty(toscaDataList)) {
711 statusRes = updateToscaDataOfToscaElement(getToscaElementRes.left().value(), edgeLabel, vertexLabel, toscaDataList, mapKeyField);
713 if (statusRes == null) {
714 statusRes = StorageOperationStatus.OK;
720 * Adds list of tosca data elements to tosca element with specified uid according received labels
722 * @param toscaElementUid
725 * @param toscaDataList
729 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataToToscaElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<T> toscaDataList, JsonPresentationFields mapKeyField) {
731 StorageOperationStatus statusRes = null;
732 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
734 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
735 if (getToscaElementRes.isRight()) {
736 TitanOperationStatus status = getToscaElementRes.right().value();
737 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
738 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
740 if (statusRes == null && CollectionUtils.isNotEmpty(toscaDataList)) {
741 statusRes = addToscaDataToToscaElement(getToscaElementRes.left().value(), edgeLabel, vertexLabel, toscaDataList, mapKeyField);
743 if (statusRes == null) {
744 statusRes = StorageOperationStatus.OK;
750 * Converts recieved map of tosca data elements to list and adds it to tosca element according received labels
752 * @param toscaElement
755 * @param toscaDataMap
759 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataToToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, Map<String, T> toscaDataMap, JsonPresentationFields mapKeyField) {
761 return addToscaDataToToscaElement(toscaElement, edgeLabel, vertexLabel, toscaDataMap.values().stream().collect(Collectors.toList()), mapKeyField);
765 * Adds list of tosca data elements to tosca element according received labels
767 * @param toscaElement
770 * @param toscaDataList
774 public <T extends ToscaDataDefinition> StorageOperationStatus addToscaDataToToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<T> toscaDataList, JsonPresentationFields mapKeyField) {
776 return updateOrAddToscaData(toscaElement, edgeLabel, vertexLabel, toscaDataList, mapKeyField, false);
780 * Updates list of tosca data elements belonging to tosca element according received labels
782 * @param toscaElement
785 * @param toscaDataList
789 public <T extends ToscaDataDefinition> StorageOperationStatus updateToscaDataOfToscaElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<T> toscaDataList, JsonPresentationFields mapKeyField) {
791 return updateOrAddToscaData(toscaElement, edgeLabel, vertexLabel, toscaDataList, mapKeyField, true);
794 @SuppressWarnings("unchecked")
795 private <T extends ToscaDataDefinition> StorageOperationStatus updateOrAddToscaData(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<T> toscaDataList, JsonPresentationFields mapKeyField, boolean isUpdate) {
796 StorageOperationStatus result = null;
797 GraphVertex toscaDataVertex = null;
798 Map<String, T> existingToscaDataMap = null;
799 Either<Map<String, T>, StorageOperationStatus> validateRes = null;
800 Map<String, T> mergedToscaDataMap;
801 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = titanDao.getChildVertex(toscaElement, edgeLabel, JsonParseFlagEnum.ParseJson);
802 if (toscaDataVertexRes.isRight() && toscaDataVertexRes.right().value() != TitanOperationStatus.NOT_FOUND) {
803 TitanOperationStatus status = toscaDataVertexRes.right().value();
804 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get child vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, status);
805 result = DaoStatusConverter.convertTitanStatusToStorageStatus(toscaDataVertexRes.right().value());
807 if (result == null) {
808 if (toscaDataVertexRes.isLeft()) {
809 toscaDataVertex = toscaDataVertexRes.left().value();
810 existingToscaDataMap = (Map<String, T>) toscaDataVertex.getJson();
813 validateRes = validateMergeToscaData(toscaElement, toscaDataList, mapKeyField, existingToscaDataMap, isUpdate);
814 if (validateRes.isRight()) {
815 result = validateRes.right().value();
816 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed validate tosca data upon adding to tosca element {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, result);
819 if (result == null) {
820 mergedToscaDataMap = validateRes.left().value();
821 result = handleToscaData(toscaElement, vertexLabel, edgeLabel, toscaDataVertex, mergedToscaDataMap);
823 if (result == null) {
824 result = StorageOperationStatus.OK;
830 @SuppressWarnings("unchecked")
831 public <T extends ToscaDataDefinition> StorageOperationStatus updateFullToscaData(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, Map<String, T> toscaData) {
832 StorageOperationStatus result = null;
833 GraphVertex toscaDataVertex = null;
834 Map<String, T> existingToscaDataMap = null;
836 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = titanDao.getChildVertex(toscaElement, edgeLabel, JsonParseFlagEnum.ParseJson);
837 if (toscaDataVertexRes.isRight() && toscaDataVertexRes.right().value() != TitanOperationStatus.NOT_FOUND) {
838 TitanOperationStatus status = toscaDataVertexRes.right().value();
839 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get child vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, status);
840 result = DaoStatusConverter.convertTitanStatusToStorageStatus(toscaDataVertexRes.right().value());
842 if (result == null) {
843 if (toscaDataVertexRes.isLeft()) {
844 toscaDataVertex = toscaDataVertexRes.left().value();
845 existingToscaDataMap = (Map<String, T>) toscaDataVertex.getJson();
850 if (result == null) {
852 result = handleToscaData(toscaElement, vertexLabel, edgeLabel, toscaDataVertex, toscaData);
854 if (result == null) {
855 result = StorageOperationStatus.OK;
861 @SuppressWarnings({ "rawtypes", "unchecked" })
862 private <T, K extends ToscaDataDefinition> StorageOperationStatus updateOrAddToscaDataDeepElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<K> toscaDataList, List<String> pathKeys,
863 JsonPresentationFields mapKeyField, boolean isUpdate) {
865 StorageOperationStatus result = null;
866 GraphVertex toscaDataVertex = null;
867 Map<String, K> existingDeepElementsMap = null;
868 Either<Map<String, K>, StorageOperationStatus> validateRes = null;
869 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = titanDao.getChildVertex(toscaElement, edgeLabel, JsonParseFlagEnum.ParseJson);
870 if (toscaDataVertexRes.isRight() && toscaDataVertexRes.right().value() != TitanOperationStatus.NOT_FOUND) {
871 TitanOperationStatus status = toscaDataVertexRes.right().value();
872 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get child vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, status);
873 result = DaoStatusConverter.convertTitanStatusToStorageStatus(toscaDataVertexRes.right().value());
875 if (result == null) {
876 if (toscaDataVertexRes.isLeft()) {
877 toscaDataVertex = toscaDataVertexRes.left().value();
878 existingDeepElementsMap = getDeepElements(toscaDataVertex, pathKeys);
880 validateRes = validateMergeToscaData(toscaElement, toscaDataList, mapKeyField, existingDeepElementsMap, isUpdate);
881 if (validateRes.isRight()) {
882 result = validateRes.right().value();
883 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed validate tosca data upon adding to tosca element {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, result);
886 if (result == null) {
887 updateDeepElements(toscaDataVertex, validateRes.left().value(), pathKeys);
888 Map<String, K> toscaDataToHandle;
889 if(toscaDataVertex == null){
890 toscaDataToHandle = new HashMap<>();
891 Map<String, K> currMap = toscaDataToHandle;
892 for (int i = 1; i < pathKeys.size()-1; ++i) {
893 currMap.put(pathKeys.get(i), (K) new MapDataDefinition());
894 currMap = (Map<String, K>) ((MapDataDefinition) currMap).getMapToscaDataDefinition().get(pathKeys.get(i));
896 toscaDataToHandle.put(pathKeys.get(pathKeys.size()-1), (K) new MapDataDefinition(validateRes.left().value()));
899 toscaDataToHandle = (Map<String, K>) toscaDataVertex.getJson();
901 result = handleToscaData(toscaElement, vertexLabel, edgeLabel, toscaDataVertex, toscaDataToHandle);
903 if (result == null) {
904 result = StorageOperationStatus.OK;
909 @SuppressWarnings({ "rawtypes", "unchecked" })
910 private <T, K extends ToscaDataDefinition> void updateDeepElements(GraphVertex toscaDataVertex, Map<String, K> mergedDeepElementMap, List<String> pathKeys) {
912 if (toscaDataVertex != null && MapUtils.isNotEmpty(mergedDeepElementMap)) {
913 Map<String, MapDataDefinition> currMap = (Map<String, MapDataDefinition>) toscaDataVertex.getJson();
914 if(!currMap.containsKey(pathKeys.get(0))){
915 currMap.put(pathKeys.get(0), new MapDataDefinition<>());
917 MapDataDefinition currDeepElement = currMap.get(pathKeys.get(0));
919 for (int i = 1; i < pathKeys.size(); ++i) {
920 if(currDeepElement.findByKey(pathKeys.get(i)) == null){
921 currDeepElement.put(pathKeys.get(i), new MapDataDefinition<>());
923 currDeepElement = (MapDataDefinition) currDeepElement.findByKey(pathKeys.get(i));
925 if(currDeepElement != null){
926 for (Map.Entry<String, K> elementEntry : mergedDeepElementMap.entrySet()) {
927 currDeepElement.put(elementEntry.getKey(), elementEntry.getValue());
933 @SuppressWarnings({ "unchecked", "rawtypes" })
934 private <T, K extends ToscaDataDefinition> Map<String, K> getDeepElements(GraphVertex toscaDataVertex, List<String> pathKeys) {
935 Map<String, K> result = null;
936 Map<String, T> currMap = (Map<String, T>) toscaDataVertex.getJson();
937 MapDataDefinition currDeepElement = (MapDataDefinition) currMap.get(pathKeys.get(0));
938 for (int i = 1; i < pathKeys.size(); ++i) {
939 currDeepElement = (MapDataDefinition) currDeepElement.findByKey(pathKeys.get(i));
941 if(currDeepElement != null){
942 result = (Map<String, K>) currDeepElement.getMapToscaDataDefinition();
947 @SuppressWarnings("unchecked")
948 private <T extends ToscaDataDefinition> StorageOperationStatus addDeepElementsBlock(GraphVertex toscaDataVertex, T toscaDataBlock, String key) {
950 StorageOperationStatus result = null;
951 Map<String, T> currMap = (Map<String, T>) toscaDataVertex.getJson();
952 if (currMap.containsKey(key)) {
953 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to add block of deep tosca data elements by label {}." + " The block element with the same key {} already exists. ", toscaDataVertex.getLabel(), key);
954 result = StorageOperationStatus.ENTITY_ALREADY_EXISTS;
956 if (result == null) {
957 currMap.put(key, toscaDataBlock);
962 @SuppressWarnings("unchecked")
963 private <T extends ToscaDataDefinition> StorageOperationStatus deleteDeepElementsBlock(GraphVertex toscaDataVertex, String key) {
965 StorageOperationStatus result = null;
966 Map<String, T> currMap = (Map<String, T>) toscaDataVertex.getJson();
967 if (!currMap.containsKey(key)) {
968 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to delete block of deep tosca data elements by label {}." + " The block element with the same key {} doesn't exist. ", toscaDataVertex.getLabel(), key);
969 result = StorageOperationStatus.NOT_FOUND;
971 if (result == null) {
978 * Removes tosca data vertex belonging to tosca element specified by uid according label
980 * @param toscaElementUid
985 public StorageOperationStatus removeToscaData(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
987 StorageOperationStatus statusRes = StorageOperationStatus.OK;
988 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
990 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
991 if (getToscaElementRes.isRight()) {
992 TitanOperationStatus status = getToscaElementRes.right().value();
993 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
994 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
996 if (statusRes == StorageOperationStatus.OK) {
997 statusRes = removeToscaDataVertex(getToscaElementRes.left().value(), edgeLabel, vertexLabel);
1003 * Removes tosca data vertex belonging to tosca element according label
1005 * @param toscaElement
1007 * @param vertexLabel
1010 public StorageOperationStatus removeToscaDataVertex(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel) {
1011 StorageOperationStatus result = null;
1012 GraphVertex toscaDataVertex = null;
1013 Iterator<Edge> edges = null;
1014 int edgeCounter = 0;
1016 Edge edgeToDelete = null;
1017 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = titanDao.getChildVertex(toscaElement, edgeLabel, JsonParseFlagEnum.ParseJson);
1018 if (toscaDataVertexRes.isRight()) {
1019 TitanOperationStatus status = toscaDataVertexRes.right().value();
1020 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed remove tosca data vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, status);
1021 result = DaoStatusConverter.convertTitanStatusToStorageStatus(toscaDataVertexRes.right().value());
1023 if (result == null) {
1024 toscaDataVertex = toscaDataVertexRes.left().value();
1025 edges = toscaDataVertex.getVertex().edges(Direction.IN);
1026 if (edges == null || !edges.hasNext()) {
1027 result = StorageOperationStatus.NOT_FOUND;
1028 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed remove tosca data vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, result);
1031 if (result == null) {
1032 while (edges.hasNext()) {
1034 edge = edges.next();
1035 if (edge.outVertex().id().equals(toscaElement.getVertex().id())) {
1036 edgeToDelete = edge;
1040 if (edgeToDelete == null) {
1041 result = StorageOperationStatus.NOT_FOUND;
1042 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed remove tosca data vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, result);
1045 if (result == null) {
1046 if (edgeCounter > 1) {
1047 edgeToDelete.remove();
1049 toscaDataVertex.getVertex().remove();
1052 if (result == null) {
1053 result = StorageOperationStatus.OK;
1059 * Deletes tosca data elements belonging to tosca element specified by uid according label
1061 * @param toscaElementUid
1063 * @param vertexLabel
1065 * @param mapKeyField
1068 public StorageOperationStatus deleteToscaDataElements(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<String> uniqueKeys, JsonPresentationFields mapKeyField) {
1070 StorageOperationStatus statusRes = StorageOperationStatus.OK;
1071 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
1073 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
1074 if (getToscaElementRes.isRight()) {
1075 TitanOperationStatus status = getToscaElementRes.right().value();
1076 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
1077 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
1079 if (statusRes == StorageOperationStatus.OK) {
1080 statusRes = deleteToscaDataElements(getToscaElementRes.left().value(), edgeLabel, vertexLabel, uniqueKeys, mapKeyField);
1086 * Deletes tosca data element belonging to tosca element specified by uid according label
1088 * @param toscaElementUid
1090 * @param vertexLabel
1092 * @param mapKeyField
1095 public StorageOperationStatus deleteToscaDataElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, String uniqueKey, JsonPresentationFields mapKeyField) {
1097 StorageOperationStatus statusRes = StorageOperationStatus.OK;
1098 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
1100 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
1101 if (getToscaElementRes.isRight()) {
1102 TitanOperationStatus status = getToscaElementRes.right().value();
1103 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
1104 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
1106 if (statusRes == StorageOperationStatus.OK) {
1107 statusRes = deleteToscaDataElement(getToscaElementRes.left().value(), edgeLabel, vertexLabel, uniqueKey, mapKeyField);
1114 * Deletes tosca data deep element belonging to tosca element specified by uid according label
1116 * @param toscaElementUid
1118 * @param vertexLabel
1121 * @param mapKeyField
1124 public StorageOperationStatus deleteToscaDataDeepElement(String toscaElementUid, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, String uniqueKey, List<String> pathKeys, JsonPresentationFields mapKeyField) {
1126 StorageOperationStatus statusRes = StorageOperationStatus.OK;
1127 Either<GraphVertex, TitanOperationStatus> getToscaElementRes;
1129 getToscaElementRes = titanDao.getVertexById(toscaElementUid, JsonParseFlagEnum.NoParse);
1130 if (getToscaElementRes.isRight()) {
1131 TitanOperationStatus status = getToscaElementRes.right().value();
1132 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get tosca element {} upon adding the properties. Status is {}. ", toscaElementUid, status);
1133 statusRes = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
1135 if (statusRes == StorageOperationStatus.OK) {
1136 statusRes = deleteToscaDataDeepElement(getToscaElementRes.left().value(), edgeLabel, vertexLabel, uniqueKey, pathKeys, mapKeyField);
1143 * Deletes tosca data deep element belonging to tosca element according label
1145 * @param toscaElement
1147 * @param vertexLabel
1150 * @param mapKeyField
1153 public StorageOperationStatus deleteToscaDataDeepElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, String uniqueKey, List<String> pathKeys, JsonPresentationFields mapKeyField) {
1155 List<String> uniqueKeys = new ArrayList<>();
1156 uniqueKeys.add(uniqueKey);
1157 return deleteToscaDataDeepElements(toscaElement, edgeLabel, vertexLabel, uniqueKeys, pathKeys, mapKeyField);
1160 public StorageOperationStatus deleteToscaDataDeepElements(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<String> uniqueKeys, List<String> pathKeys, JsonPresentationFields mapKeyField) {
1162 StorageOperationStatus result = null;
1163 GraphVertex toscaDataVertex;
1164 Map<String, ToscaDataDefinition> existingToscaDataMap = null;
1165 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = titanDao.getChildVertex(toscaElement, edgeLabel, JsonParseFlagEnum.ParseJson);
1166 if (toscaDataVertexRes.isRight()) {
1167 TitanOperationStatus status = toscaDataVertexRes.right().value();
1168 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get child vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, status);
1169 result = DaoStatusConverter.convertTitanStatusToStorageStatus(toscaDataVertexRes.right().value());
1171 if (result == null) {
1172 toscaDataVertex = toscaDataVertexRes.left().value();
1173 existingToscaDataMap = (Map<String, ToscaDataDefinition>) getDeepElements(toscaDataVertexRes.left().value(), pathKeys);
1174 for (String uniqueKey : uniqueKeys) {
1175 result = removeToscaDataElement(toscaElement, edgeLabel, uniqueKey, toscaDataVertex, existingToscaDataMap);
1176 if (result != StorageOperationStatus.OK) {
1181 if (result == null) {
1182 result = StorageOperationStatus.OK;
1188 * Deletes tosca data element belonging to tosca element according label
1190 * @param toscaElement
1192 * @param vertexLabel
1194 * @param mapKeyField
1197 public StorageOperationStatus deleteToscaDataElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, String uniqueKey, JsonPresentationFields mapKeyField) {
1199 List<String> uniqueKeys = new ArrayList<>();
1200 uniqueKeys.add(uniqueKey);
1201 return deleteToscaDataElements(toscaElement, edgeLabel, vertexLabel, uniqueKeys, mapKeyField);
1204 @SuppressWarnings("unchecked")
1206 * Deletes tosca data elements belonging to tosca element according label
1208 * @param toscaElement
1210 * @param vertexLabel
1212 * @param mapKeyField
1215 public StorageOperationStatus deleteToscaDataElements(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexLabel, List<String> uniqueKeys, JsonPresentationFields mapKeyField) {
1217 StorageOperationStatus result = null;
1218 GraphVertex toscaDataVertex;
1219 Map<String, ToscaDataDefinition> existingToscaDataMap = null;
1220 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = titanDao.getChildVertex(toscaElement, edgeLabel, JsonParseFlagEnum.ParseJson);
1221 if (toscaDataVertexRes.isRight()) {
1222 TitanOperationStatus status = toscaDataVertexRes.right().value();
1223 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get child vertex of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, status);
1224 result = DaoStatusConverter.convertTitanStatusToStorageStatus(toscaDataVertexRes.right().value());
1226 if (result == null) {
1227 toscaDataVertex = toscaDataVertexRes.left().value();
1228 existingToscaDataMap = (Map<String, ToscaDataDefinition>) toscaDataVertex.getJson();
1229 for (String uniqueKey : uniqueKeys) {
1230 result = removeToscaDataElement(toscaElement, edgeLabel, uniqueKey, toscaDataVertex, existingToscaDataMap);
1231 if (result != StorageOperationStatus.OK) {
1236 if (result == null) {
1237 result = StorageOperationStatus.OK;
1242 private <T extends ToscaDataDefinition> StorageOperationStatus removeToscaDataElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, String uniqueKey, GraphVertex toscaDataVertex, Map<String, T> existingToscaDataMap) {
1244 StorageOperationStatus result = StorageOperationStatus.OK;
1245 if (!existingToscaDataMap.containsKey(uniqueKey)) {
1246 result = StorageOperationStatus.NOT_FOUND;
1247 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to delete tosca data element of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, result);
1249 existingToscaDataMap.remove(uniqueKey);
1250 Either<GraphVertex, TitanOperationStatus> updateOrCopyRes = updateOrCopyOnUpdate(toscaDataVertex, toscaElement, edgeLabel);
1251 if (updateOrCopyRes.isRight()) {
1252 result = DaoStatusConverter.convertTitanStatusToStorageStatus(updateOrCopyRes.right().value());
1253 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to update tosca data {} of the tosca element {}. Status is {}. ", edgeLabel, toscaElement.getUniqueId(), result);
1259 protected <K extends ToscaDataDefinition> StorageOperationStatus handleToscaData(GraphVertex toscaElement, VertexTypeEnum vertexLabel, EdgeLabelEnum edgeLabel, GraphVertex toscaDataVertex, Map<String, K> mergedToscaDataMap) {
1261 StorageOperationStatus result = StorageOperationStatus.OK;
1262 if (toscaDataVertex == null) {
1264 Either<GraphVertex, StorageOperationStatus> createRes = assosiateElementToData(toscaElement, vertexLabel, edgeLabel, mergedToscaDataMap);
1265 if (createRes.isRight()) {
1266 StorageOperationStatus status = createRes.right().value();
1267 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to assosiate tosca data {} of the tosca element {}. Status is {}. ", edgeLabel, toscaElement.getUniqueId(), status);
1271 toscaDataVertex.setJson(mergedToscaDataMap);
1272 Either<GraphVertex, TitanOperationStatus> updateOrCopyRes = updateOrCopyOnUpdate(toscaDataVertex, toscaElement, edgeLabel);
1273 if (updateOrCopyRes.isRight()) {
1274 TitanOperationStatus status = updateOrCopyRes.right().value();
1275 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to add tosca data {} to the tosca element {}. Status is {}. ", edgeLabel, toscaElement.getUniqueId(), status);
1276 result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
1282 private <T extends ToscaDataDefinition> Either<Map<String, T>, StorageOperationStatus> validateMergeToscaData(GraphVertex toscaElement, List<T> toscaDataList, JsonPresentationFields mapKeyField, Map<String, T> existingToscaDataMap,
1285 Map<String, T> mergedToscaDataMap = new HashMap<>();
1286 StorageOperationStatus status;
1287 Either<Map<String, T>, StorageOperationStatus> result = Either.left(mergedToscaDataMap);
1288 if (MapUtils.isNotEmpty(existingToscaDataMap)) {
1289 mergedToscaDataMap.putAll(existingToscaDataMap);
1291 for (T toscaDataElement : toscaDataList) {
1292 status = handleToscaDataElement(toscaElement, mapKeyField, mergedToscaDataMap, toscaDataElement, isUpdate);
1293 if (status != StorageOperationStatus.OK) {
1294 result = Either.right(status);
1301 private <T extends ToscaDataDefinition> StorageOperationStatus handleToscaDataElement(GraphVertex toscaElement, JsonPresentationFields mapKeyField, Map<String, T> mergedToscaDataMap, T toscaDataElement, boolean isUpdate) {
1303 StorageOperationStatus status = StorageOperationStatus.OK;
1304 String currKey = (String) toscaDataElement.getToscaPresentationValue(mapKeyField);
1305 if (StringUtils.isEmpty(currKey)) {
1306 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to add tosca data to tosca element {}. The key is empty. ");
1307 status = StorageOperationStatus.BAD_REQUEST;
1308 } else if (!isUpdate && mergedToscaDataMap.containsKey(currKey)) {
1309 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to add tosca data to tosca element {}. The element with the same key {} already exists. ", toscaElement.getUniqueId(), currKey);
1310 status = StorageOperationStatus.BAD_REQUEST;
1312 mergedToscaDataMap.put(currKey, toscaDataElement);
1316 // public StorageOperationStatus updateDataOnGraph(GraphVertex dataVertex) {
1317 // Either<GraphVertex, TitanOperationStatus> updateVertex = titanDao.updateVertex(dataVertex);
1318 // if (updateVertex.isRight()) {
1319 // return DaoStatusConverter.convertTitanStatusToStorageStatus(updateVertex.right().value());
1321 // return StorageOperationStatus.OK;
1324 protected GroupInstanceDataDefinition buildGroupInstanceDataDefinition(GroupDataDefinition group, ComponentInstanceDataDefinition componentInstance) {
1326 String componentInstanceName = componentInstance.getName();
1327 Long creationDate = System.currentTimeMillis();
1328 GroupInstanceDataDefinition groupInstance = new GroupInstanceDataDefinition();
1329 String groupUid = group.getUniqueId();
1331 groupInstance.setGroupUid(groupUid);
1332 groupInstance.setType(group.getType());
1333 groupInstance.setCustomizationUUID(generateCustomizationUUID());
1334 groupInstance.setCreationTime(creationDate);
1335 groupInstance.setModificationTime(creationDate);
1336 groupInstance.setName(buildGroupInstanceName(componentInstanceName, group.getName()));
1337 groupInstance.setGroupName(group.getName());
1338 groupInstance.setNormalizedName(ValidationUtils.normalizeComponentInstanceName(groupInstance.getName()));
1339 groupInstance.setUniqueId(UniqueIdBuilder.buildResourceInstanceUniuqeId(componentInstance.getUniqueId(), groupUid, groupInstance.getNormalizedName()));
1340 groupInstance.setArtifacts(group.getArtifacts());
1341 groupInstance.setArtifactsUuid(group.getArtifactsUuid());
1342 groupInstance.setProperties(group.getProperties());
1343 groupInstance.setInvariantUUID(group.getInvariantUUID());
1344 groupInstance.setGroupUUID(group.getGroupUUID());
1345 groupInstance.setVersion(group.getVersion());
1347 return groupInstance;
1350 protected String buildGroupInstanceName(String instanceName, String groupName) {
1351 return ValidationUtils.normaliseComponentName(instanceName) + ".." + groupName;
1354 protected String generateCustomizationUUID() {
1355 return UUID.randomUUID().toString();