9c588d959667b58cea17fdcf85e6e368e1975608
[sdc.git] /
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.be.model.jsonjanusgraph.operations;
22
23 import static fj.P.p;
24
25 import fj.P2;
26 import org.janusgraph.core.JanusGraphVertex;
27 import fj.data.Either;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.collections.MapUtils;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.commons.lang3.tuple.ImmutablePair;
32 import org.apache.tinkerpop.gremlin.structure.Direction;
33 import org.apache.tinkerpop.gremlin.structure.Edge;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.openecomp.sdc.be.config.ConfigurationManager;
36 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
37 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
38 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
39 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
40 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
41 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
42 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
43 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
44 import org.openecomp.sdc.be.datatypes.elements.*;
45 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
46 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
47 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
48 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
49 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
50 import org.openecomp.sdc.be.model.DistributionStatusEnum;
51 import org.openecomp.sdc.be.model.LifecycleStateEnum;
52 import org.openecomp.sdc.be.model.User;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
54 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
55 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
56 import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
58 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
59 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
60 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
61 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
62 import org.openecomp.sdc.common.log.wrappers.Logger;
63
64 import java.util.*;
65 import java.util.stream.Collectors;
66
67 @org.springframework.stereotype.Component("tosca-element-lifecycle-operation")
68
69 /**
70  * Allows to perform lifecycle operations: checkin, checkout, submit for testing, start certification and certification process for tosca element
71  */
72 public class ToscaElementLifecycleOperation extends BaseOperation {
73
74     private static final String FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS = "Failed to delete last state edge. Status is {}. ";
75         private static final String FAILED_TO_GET_VERTICES = "Failed to get vertices by id {}. Status is {}. ";
76     public static final String VERSION_DELIMITER = ".";
77     public static final String VERSION_DELIMITER_REGEXP = "\\.";
78
79     private static final Logger log = Logger.getLogger(ToscaElementLifecycleOperation.class);
80
81     /**
82      * Performs changing a lifecycle state of tosca element from "checked out" or "ready for certification" to "checked in"
83      * 
84      * @param currState
85      * @param toscaElementId
86      * @param modifierId
87      * @param ownerId
88      * @return
89      */
90     public Either<ToscaElement, StorageOperationStatus> checkinToscaELement(LifecycleStateEnum currState,
91         String toscaElementId, String modifierId, String ownerId) {
92         try {
93             return janusGraphDao
94                 .getVerticesByUniqueIdAndParseFlag(
95                     prepareParametersToGetVerticesForCheckin(toscaElementId, modifierId, ownerId))
96                 .right().map(status -> handleFailureToPrepareParameters(status, toscaElementId))
97                 .left().bind(
98                     verticesMap ->
99                         checkinToscaELement(
100                             currState,
101                             verticesMap.get(toscaElementId),
102                             verticesMap.get(ownerId),
103                             verticesMap.get(modifierId),
104                             LifecycleStateEnum.NOT_CERTIFIED_CHECKIN
105                         ).left().bind(checkinResult -> {
106                             //We retrieve the operation
107                             ToscaElementOperation operation =
108                                 getToscaElementOperation(verticesMap.get(toscaElementId).getLabel());
109
110                             //We retrieve the ToscaElement from the operation
111                             return getToscaElementFromOperation(operation, checkinResult.getUniqueId(), toscaElementId);
112                         })
113                 );
114         } catch (Exception e) {
115             CommonUtility.addRecordToLog(
116                 log, LogLevelEnum.DEBUG, "Exception occurred during checkin of tosca element {}. {} ", toscaElementId,
117                 e.getMessage());
118             return Either.right(StorageOperationStatus.GENERAL_ERROR);
119         }
120     }
121
122     static StorageOperationStatus handleFailureToPrepareParameters(final JanusGraphOperationStatus status, final String toscaElementId) {
123         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
124         return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
125     }
126
127     static Either<ToscaElement, StorageOperationStatus> getToscaElementFromOperation(final ToscaElementOperation operation,
128         final String uniqueId, final String toscaElementId) {
129         return operation.getToscaElement(uniqueId)
130             .right().map(status -> {
131                 //We log a potential error we got while retrieving the ToscaElement
132                 CommonUtility
133                     .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated tosca element {}. Status is {}",
134                         toscaElementId, status);
135                 return status;
136             });
137     }
138
139     /**
140      * Returns vertex presenting owner of tosca element specified by uniqueId
141      * 
142      * @param toscaElementId
143      * @return
144      */
145     public Either<User, StorageOperationStatus> getToscaElementOwner(String toscaElementId) {
146         Either<User, StorageOperationStatus> result = null;
147         GraphVertex toscaElement = null;
148         Either<GraphVertex, JanusGraphOperationStatus> getToscaElementRes = janusGraphDao
149             .getVertexById(toscaElementId, JsonParseFlagEnum.NoParse);
150         if (getToscaElementRes.isRight()) {
151             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementRes.right().value()));
152         }
153         if (result == null) {
154             toscaElement = getToscaElementRes.left().value();
155             Iterator<Vertex> vertices = toscaElement.getVertex().vertices(Direction.IN, EdgeLabelEnum.STATE.name());
156             if (vertices == null || !vertices.hasNext()) {
157                 result = Either.right(StorageOperationStatus.NOT_FOUND);
158             } else {
159                 result = Either.left(convertToUser(vertices.next()));
160             }
161         }
162         return result;
163     }
164
165     /**
166      * Returns vertex presenting owner of tosca element specified by uniqueId
167      * 
168      * @param toscaElement
169      * @return
170      */
171     public Either<User, StorageOperationStatus> getToscaElementOwner(GraphVertex toscaElement) {
172         Either<User, StorageOperationStatus> result = null;
173         Iterator<Vertex> vertices = toscaElement.getVertex().vertices(Direction.IN, EdgeLabelEnum.STATE.name());
174         if (vertices == null || !vertices.hasNext()) {
175             result = Either.right(StorageOperationStatus.NOT_FOUND);
176         } else {
177             result = Either.left(convertToUser(vertices.next()));
178         }
179         return result;
180     }
181
182     /**
183      * Performs checkout of a tosca element
184      * 
185      * @param toscaElementId
186      * @param modifierId
187      * @param ownerId
188      * @return
189      */
190     public Either<ToscaElement, StorageOperationStatus> checkoutToscaElement(String toscaElementId, String modifierId, String ownerId) {
191         Either<ToscaElement, StorageOperationStatus> result = null;
192         Map<String, GraphVertex> vertices = null;
193         try {
194             Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
195                 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForCheckout(toscaElementId, modifierId, ownerId));
196             if (getVerticesRes.isRight()) {
197                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
198                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
199             }
200             if (result == null) {
201                 vertices = getVerticesRes.left().value();
202                 // update previous component if not certified
203                 StorageOperationStatus status = updatePreviousVersion(vertices.get(toscaElementId), vertices.get(ownerId));
204                 if (status != StorageOperationStatus.OK) {
205                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update vertex with id {} . Status is {}. ", status);
206                     result = Either.right(status);
207                 }
208             }
209             if (result == null) {
210                 result = cloneToscaElementForCheckout(vertices.get(toscaElementId), vertices.get(modifierId));
211                 if (result.isRight()) {
212                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to checkout tosca element {}. Status is {} ", toscaElementId, result.right().value());
213                 }
214
215             }
216         } catch (Exception e) {
217             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during checkout tosca element {}. {}", toscaElementId, e.getMessage());
218         }
219         return result;
220     }
221
222     /**
223      * Performs undo checkout for tosca element
224      * 
225      * @param toscaElementId
226      * @return
227      */
228     public Either<ToscaElement, StorageOperationStatus> undoCheckout(String toscaElementId) {
229         try {
230             return janusGraphDao.getVertexById(toscaElementId, JsonParseFlagEnum.ParseMetadata)
231                 .right().map(errorStatus -> {
232                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
233                     return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(errorStatus);
234                 })
235                 .left().bind(this::retrieveAndUpdatePreviousVersion)
236                 .left().bind(this::updateEdgeToCatalogRootAndReturnPreVersionElement);
237         } catch (Exception e) {
238             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occurred during undo checkout tosca element {}. {}", toscaElementId, e.getMessage());
239             return null;
240         }
241     }
242
243     private Either<P2<GraphVertex, JanusGraphVertex>, StorageOperationStatus> retrieveAndUpdatePreviousVersion(
244         final GraphVertex currVersionV) {
245         if (!hasPreviousVersion(currVersionV)) {
246             return Either.left(p(currVersionV, null));
247         } else {
248             // find previous version
249             Iterator<Edge> nextVersionComponentIter = currVersionV.getVertex()
250                 .edges(Direction.IN, EdgeLabelEnum.VERSION.name());
251
252             if (nextVersionComponentIter == null || !nextVersionComponentIter.hasNext()) {
253                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
254                     "Failed to fetch previous version of tosca element with name {}. ",
255                     currVersionV.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME).toString());
256                 return Either.right(StorageOperationStatus.NOT_FOUND);
257             } else {
258                 Vertex preVersionVertex = nextVersionComponentIter.next().outVertex();
259                 StorageOperationStatus updateOldResourceResult = updateOldToscaElementBeforeUndoCheckout(
260                     preVersionVertex);
261                 if (updateOldResourceResult != StorageOperationStatus.OK) {
262                     return Either.right(updateOldResourceResult);
263                 } else {
264                     P2<GraphVertex, JanusGraphVertex> result = p(currVersionV, (JanusGraphVertex) preVersionVertex);
265
266                     return Either.left(result);
267                 }
268             }
269         }
270     }
271
272     private Either<ToscaElement, StorageOperationStatus> updateEdgeToCatalogRootAndReturnPreVersionElement(
273         final P2<GraphVertex, JanusGraphVertex> tuple) {
274
275         final GraphVertex currVersionV = tuple._1();
276         final JanusGraphVertex preVersionVertex = tuple._2();
277
278         StorageOperationStatus updateCatalogRes = updateEdgeToCatalogRootByUndoCheckout(preVersionVertex, currVersionV);
279         if (updateCatalogRes != StorageOperationStatus.OK) {
280             return Either.right(updateCatalogRes);
281         } else {
282             final ToscaElementOperation operation = getToscaElementOperation(currVersionV.getLabel());
283
284             return operation.deleteToscaElement(currVersionV)
285                 .left().bind(discarded -> getUpdatedPreVersionElement(operation, preVersionVertex));
286         }
287     }
288
289     private Either<ToscaElement, StorageOperationStatus> getUpdatedPreVersionElement(
290         final ToscaElementOperation operation,
291         final JanusGraphVertex preVersionVertex) {
292
293         if (preVersionVertex == null) {
294             return Either.left(null);
295         } else {
296             String uniqueIdPreVer = (String) janusGraphDao
297                 .getProperty(preVersionVertex, GraphPropertyEnum.UNIQUE_ID.getProperty());
298             return operation.getToscaElement(uniqueIdPreVer);
299         }
300     }
301
302     private boolean hasPreviousVersion(GraphVertex toscaElementVertex) {
303         boolean hasPreviousVersion = true;
304         String version = (String) toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION);
305         if (StringUtils.isEmpty(version) || "0.1".equals(version))
306             hasPreviousVersion = false;
307         return hasPreviousVersion;
308     }
309
310     public Either<ToscaElement, StorageOperationStatus> certifyToscaElement(
311         String toscaElementId,
312         String modifierId,
313         String ownerId) {
314         try {
315             return janusGraphDao
316                 .getVerticesByUniqueIdAndParseFlag(
317                     prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId))
318                 .right().map(status ->
319                     logDebugMessageAndReturnStorageOperationStatus(
320                         DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status),
321                         FAILED_TO_GET_VERTICES,
322                         toscaElementId))
323                 .left().bind(verticesRes -> {
324                     GraphVertex toscaElement = verticesRes.get(toscaElementId);
325                     GraphVertex modifier = verticesRes.get(modifierId);
326                     Integer majorVersion = getMajorVersion(
327                         (String) toscaElement.getMetadataProperty(GraphPropertyEnum.VERSION));
328
329                     return handleRelationsBeforeCertifyingAndProcessClone(toscaElement,
330                         modifier, majorVersion);
331                 });
332         } catch (Exception e) {
333             CommonUtility
334                 .addRecordToLog(log, LogLevelEnum.DEBUG,
335                     "Exception occurred during certification tosca element {}.",
336                     toscaElementId, e);
337             return Either.right(StorageOperationStatus.GENERAL_ERROR);
338         }
339     }
340
341     private Either<ToscaElement, StorageOperationStatus> handleRelationsBeforeCertifyingAndProcessClone(
342         GraphVertex toscaElement,
343         GraphVertex modifier,
344         Integer majorVersion) {
345         StorageOperationStatus status = handleRelationsOfPreviousToscaElementBeforeCertifying(toscaElement,
346             modifier, majorVersion);
347         if (status != StorageOperationStatus.OK) {
348             return Either.right(
349                 logDebugMessageAndReturnStorageOperationStatus(status,
350                     "Failed to handle relations of previous tosca element before certifying {}. Status is {}. ",
351                     toscaElement.getUniqueId(), status));
352         } else {
353             return cloneToscaElementAndHandleRelations(toscaElement, modifier, majorVersion);
354         }
355     }
356
357     private Either<ToscaElement, StorageOperationStatus> cloneToscaElementAndHandleRelations(
358         GraphVertex toscaElement,
359         GraphVertex modifier,
360         Integer majorVersion) {
361         return cloneToscaElementForCertify(toscaElement, modifier, majorVersion)
362             .right().map(status -> logDebugMessageAndReturnStorageOperationStatus(status,
363                 "Failed to clone tosca element during certification. "))
364             .left().bind(certifiedToscaElement ->
365                 handleRelationsOfNewestCertifiedToscaElementAndReturn(toscaElement, certifiedToscaElement));
366     }
367
368     private Either<ToscaElement, StorageOperationStatus> handleRelationsOfNewestCertifiedToscaElementAndReturn(
369         GraphVertex toscaElement,
370         GraphVertex certifiedToscaElement) {
371         StorageOperationStatus status = handleRelationsOfNewestCertifiedToscaElement(toscaElement,
372             certifiedToscaElement);
373         if (status != StorageOperationStatus.OK) {
374             return Either.right(
375                 logDebugMessageAndReturnStorageOperationStatus(status,
376                     "Failed to handle relations of newest certified tosca element {}. Status is {}. ",
377                     certifiedToscaElement.getUniqueId(), status));
378         } else {
379             return getToscaElementOperation(toscaElement.getLabel())
380                 .getToscaElement(certifiedToscaElement.getUniqueId());
381         }
382     }
383
384     private static StorageOperationStatus logDebugMessageAndReturnStorageOperationStatus(
385         final StorageOperationStatus status,
386         final String msg,
387         final Object... args) {
388         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, msg, args);
389         return status;
390     }
391
392     private StorageOperationStatus handleRelationsOfNewestCertifiedToscaElement(GraphVertex toscaElement, GraphVertex certifiedToscaElement) {
393
394         JanusGraphOperationStatus createVersionEdgeStatus = janusGraphDao.createEdge(toscaElement, certifiedToscaElement, EdgeLabelEnum.VERSION, new HashMap<>());
395         if (createVersionEdgeStatus != JanusGraphOperationStatus.OK) {
396             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create version edge from last element {} to new certified element {}. status=", toscaElement.getUniqueId(), certifiedToscaElement.getUniqueId(),
397                     createVersionEdgeStatus);
398             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createVersionEdgeStatus);
399         }
400         return StorageOperationStatus.OK;
401     }
402
403     public Either<GraphVertex, JanusGraphOperationStatus> findUser(String userId) {
404         return findUserVertex(userId);
405     }
406
407     private Either<Boolean, StorageOperationStatus> markToscaElementsAsDeleted(ToscaElementOperation operation, List<GraphVertex> toscaElements) {
408         Either<Boolean, StorageOperationStatus> result = Either.left(true);
409         for (GraphVertex resourceToDelete : toscaElements) {
410             if (!(resourceToDelete.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals(LifecycleStateEnum.CERTIFIED.name())) {
411                 Either<GraphVertex, StorageOperationStatus> deleteElementRes = operation.markComponentToDelete(resourceToDelete);
412                 if (deleteElementRes.isRight()) {
413                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete tosca element {}. Status is {}. ", resourceToDelete.getUniqueId(), deleteElementRes.right().value());
414                     result = Either.right(deleteElementRes.right().value());
415                     break;
416                 }
417             }
418         }
419         return result;
420     }
421
422     private StorageOperationStatus handleRelationsOfPreviousToscaElementBeforeCertifying(GraphVertex toscaElement, GraphVertex modifier, Integer majorVersion) {
423         StorageOperationStatus result = null;
424         if (majorVersion > 0) {
425             Either<Vertex, StorageOperationStatus> findRes = findLastCertifiedToscaElementVertex(toscaElement);
426             if (findRes.isRight()) {
427                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch last certified tosca element {} . Status is {}. ", toscaElement.getMetadataProperty(GraphPropertyEnum.NAME), findRes.right().value());
428                 result = findRes.right().value();
429             }
430             if (result == null) {
431                 Vertex lastCertifiedVertex = findRes.left().value();
432                 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
433                 properties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, false);
434                 JanusGraphOperationStatus status = janusGraphDao
435                     .updateVertexMetadataPropertiesWithJson(lastCertifiedVertex, properties);
436                 if (status != JanusGraphOperationStatus.OK) {
437                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to set highest version  of tosca element {} to [{}]. Status is {}", toscaElement.getUniqueId(), false, status);
438                     result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
439                 }
440                 // remove previous certified version from the catalog
441                 GraphVertex lastCertifiedV = new GraphVertex();
442                 lastCertifiedV.setVertex((JanusGraphVertex) lastCertifiedVertex);
443                 lastCertifiedV.setUniqueId((String) janusGraphDao
444                     .getProperty((JanusGraphVertex) lastCertifiedVertex, GraphPropertyEnum.UNIQUE_ID.getProperty()));
445                 StorageOperationStatus res = updateEdgeToCatalogRoot(null, lastCertifiedV);
446                 if (res != StorageOperationStatus.OK) {
447                     return res;
448                 }
449             }
450         }
451         if (result == null) {
452             result = StorageOperationStatus.OK;
453         }
454         return result;
455     }
456
457     private Either<Vertex, StorageOperationStatus> findLastCertifiedToscaElementVertex(GraphVertex toscaElement) {
458         return findLastCertifiedToscaElementVertexRecursively(toscaElement.getVertex());
459     }
460
461     private Either<Vertex, StorageOperationStatus> findLastCertifiedToscaElementVertexRecursively(Vertex vertex) {
462         if (isCertifiedVersion((String) vertex.property(GraphPropertyEnum.VERSION.getProperty()).value())) {
463             return Either.left(vertex);
464         }
465         Iterator<Edge> edgeIter = vertex.edges(Direction.IN, EdgeLabelEnum.VERSION.name());
466         if (!edgeIter.hasNext()) {
467             return Either.right(StorageOperationStatus.NOT_FOUND);
468         }
469         return findLastCertifiedToscaElementVertexRecursively(edgeIter.next().outVertex());
470     }
471
472     private boolean isCertifiedVersion(String version) {
473         String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
474         if (Integer.parseInt(versionParts[0]) > 0 && Integer.parseInt(versionParts[1]) == 0) {
475             return true;
476         }
477         return false;
478     }
479
480     private StorageOperationStatus updateOldToscaElementBeforeUndoCheckout(Vertex previousVersionToscaElement) {
481
482         StorageOperationStatus result = StorageOperationStatus.OK;
483         String previousVersion = (String) previousVersionToscaElement.property(GraphPropertyEnum.VERSION.getProperty()).value();
484         if (!previousVersion.endsWith(".0")) {
485             try {
486                 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update vertex of previous version of tosca element", previousVersionToscaElement.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()));
487
488                 Map<String, Object> propertiesToUpdate = new HashMap<>();
489                 propertiesToUpdate.put(GraphPropertyEnum.IS_HIGHEST_VERSION.getProperty(), true);
490                 Map<String, Object> jsonMetadataMap = JsonParserUtils.toMap((String) previousVersionToscaElement.property(GraphPropertyEnum.METADATA.getProperty()).value());
491                 jsonMetadataMap.put(GraphPropertyEnum.IS_HIGHEST_VERSION.getProperty(), true);
492                 propertiesToUpdate.put(GraphPropertyEnum.METADATA.getProperty(), JsonParserUtils.toJson(jsonMetadataMap));
493
494                 janusGraphDao.setVertexProperties(previousVersionToscaElement, propertiesToUpdate);
495
496                 Iterator<Edge> edgesIter = previousVersionToscaElement.edges(Direction.IN, EdgeLabelEnum.LAST_STATE.name());
497                 if (!edgesIter.hasNext()) {
498                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch last modifier vertex for tosca element {}. ", previousVersionToscaElement.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()));
499                     result = StorageOperationStatus.NOT_FOUND;
500                 } else {
501                     Edge lastStateEdge = edgesIter.next();
502                     Vertex lastModifier = lastStateEdge.outVertex();
503                     JanusGraphOperationStatus replaceRes = janusGraphDao
504                         .replaceEdgeLabel(lastModifier, previousVersionToscaElement, lastStateEdge, EdgeLabelEnum.LAST_STATE, EdgeLabelEnum.STATE);
505                     if (replaceRes != JanusGraphOperationStatus.OK) {
506                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to replace label from {} to {}. status = {}", EdgeLabelEnum.LAST_STATE, EdgeLabelEnum.STATE, replaceRes);
507                         result = StorageOperationStatus.INCONSISTENCY;
508                         if (replaceRes != JanusGraphOperationStatus.INVALID_ID) {
509                             result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(replaceRes);
510                         }
511                     }
512
513                 }
514             } catch (Exception e) {
515                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during update previous tosca element {} before undo checkout. {} ", e.getMessage());
516             }
517         }
518         return result;
519     }
520
521     private StorageOperationStatus updatePreviousVersion(GraphVertex toscaElementVertex, GraphVertex ownerVertex) {
522         StorageOperationStatus result = null;
523         String ownerId = (String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID);
524         String toscaElementId = toscaElementVertex.getUniqueId();
525         if (!toscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE).equals(LifecycleStateEnum.CERTIFIED.name())) {
526             toscaElementVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, false);
527             Either<GraphVertex, JanusGraphOperationStatus> updateVertexRes = janusGraphDao.updateVertex(toscaElementVertex);
528             if (updateVertexRes.isRight()) {
529                 JanusGraphOperationStatus titatStatus = updateVertexRes.right().value();
530                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update tosca element vertex {}. Status is  {}", toscaElementVertex.getUniqueId(), titatStatus);
531                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(titatStatus);
532             }
533             Either<Edge, JanusGraphOperationStatus> deleteEdgeRes = null;
534             if (result == null) {
535                 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to replace edge with label {} to label {} from {} to {}. ", EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE, ownerId, toscaElementId);
536
537                 deleteEdgeRes = janusGraphDao
538                     .deleteEdge(ownerVertex, toscaElementVertex, EdgeLabelEnum.STATE);
539                 if (deleteEdgeRes.isRight()) {
540                     JanusGraphOperationStatus janusGraphStatus = deleteEdgeRes.right().value();
541                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete edge with label {} from {} to {}. Status is {} ", EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE, ownerId, toscaElementId, janusGraphStatus);
542                     if (!janusGraphStatus.equals(JanusGraphOperationStatus.INVALID_ID)) {
543                         result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(janusGraphStatus);
544                     } else {
545                         result = StorageOperationStatus.INCONSISTENCY;
546                     }
547                 }
548             }
549             if (result == null) {
550                 JanusGraphOperationStatus
551                     createEdgeRes = janusGraphDao
552                     .createEdge(ownerVertex.getVertex(), toscaElementVertex.getVertex(), EdgeLabelEnum.LAST_STATE, deleteEdgeRes.left().value());
553                 if (createEdgeRes != JanusGraphOperationStatus.OK) {
554                     result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes);
555                 }
556             }
557         }
558         if (result == null) {
559             result = StorageOperationStatus.OK;
560         }
561         return result;
562     }
563
564     private Either<ToscaElement, StorageOperationStatus> cloneToscaElementForCheckout(GraphVertex toscaElementVertex, GraphVertex modifierVertex) {
565
566         Either<ToscaElement, StorageOperationStatus> result = null;
567         Either<GraphVertex, StorageOperationStatus> cloneResult = null;
568         ToscaElementOperation operation = getToscaElementOperation(toscaElementVertex.getLabel());
569         // check if component with the next version doesn't exist.
570         Iterator<Edge> nextVersionComponentIter = toscaElementVertex.getVertex().edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
571         if (nextVersionComponentIter != null && nextVersionComponentIter.hasNext()) {
572             Vertex nextVersionVertex = nextVersionComponentIter.next().inVertex();
573             String fetchedVersion = (String) nextVersionVertex.property(GraphPropertyEnum.VERSION.getProperty()).value();
574             String fetchedName = (String) nextVersionVertex.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
575             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to checkout component {} with version {}. The component with name {} and version {} was fetched from graph as existing following version. ",
576                     toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME).toString(), toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION).toString(), fetchedName, fetchedVersion);
577             result = Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS);
578         }
579         if (result == null) {
580             toscaElementVertex.getOrSetDefaultInstantiationTypeForToscaElementJson();
581             cloneResult = operation.cloneToscaElement(toscaElementVertex, cloneGraphVertexForCheckout(toscaElementVertex, modifierVertex), modifierVertex);
582             if (cloneResult.isRight()) {
583                 result = Either.right(cloneResult.right().value());
584             }
585         }
586         GraphVertex clonedVertex = null;
587         if (result == null) {
588             clonedVertex = cloneResult.left().value();
589             JanusGraphOperationStatus
590                 status = janusGraphDao
591                 .createEdge(toscaElementVertex.getVertex(), cloneResult.left().value().getVertex(), EdgeLabelEnum.VERSION, new HashMap<>());
592             if (status != JanusGraphOperationStatus.OK) {
593                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge with label {} from vertex {} to tosca element vertex {} on graph. Status is {}. ", EdgeLabelEnum.VERSION,
594                         toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), cloneResult.left().value().getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
595                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
596             }
597         }
598         if (result == null) {
599             Boolean isHighest = (Boolean) toscaElementVertex.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
600             GraphVertex prevVersionInCatalog = (isHighest != null && isHighest) ? null : toscaElementVertex;
601             StorageOperationStatus updateCatalogRes = updateEdgeToCatalogRoot(clonedVertex, prevVersionInCatalog);
602             if (updateCatalogRes != StorageOperationStatus.OK) {
603                 return Either.right(updateCatalogRes);
604             }
605             result = operation.getToscaElement(cloneResult.left().value().getUniqueId());
606             if (result.isRight()) {
607                 return result;
608             }
609             ToscaElement toscaElement = result.left().value();
610             if (toscaElement.getToscaType() == ToscaElementTypeEnum.TOPOLOGY_TEMPLATE) {
611                 result = handleFixTopologyTemplate(toscaElementVertex, result, operation, clonedVertex, toscaElement);
612             }
613         }
614
615         return result;
616     }
617
618     private Either<ToscaElement, StorageOperationStatus> handleFixTopologyTemplate(GraphVertex toscaElementVertex, Either<ToscaElement, StorageOperationStatus> result, ToscaElementOperation operation, GraphVertex clonedVertex,
619             ToscaElement toscaElement) {
620         TopologyTemplate topologyTemplate = (TopologyTemplate) toscaElement;
621         Map<String, MapPropertiesDataDefinition> instInputs = topologyTemplate.getInstInputs();
622         Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
623         Map<String, MapArtifactDataDefinition> instArtifactsMap = topologyTemplate.getInstanceArtifacts();
624         Map<String, ToscaElement> origCompMap = new HashMap<>();
625         if (instInputs == null) {
626             instInputs = new HashMap<>();
627         }
628         if (instGroups == null) {
629             instGroups = new HashMap<>();
630         }
631         if (instArtifactsMap == null) {
632             instArtifactsMap = new HashMap<>();
633         }
634         Map<String, ComponentInstanceDataDefinition> instancesMap = topologyTemplate.getComponentInstances();
635         boolean isAddInstGroup = instGroups == null || instGroups.isEmpty();
636         boolean needUpdateComposition = false;
637
638         if (instancesMap != null && !instancesMap.isEmpty()) {
639             for (ComponentInstanceDataDefinition vfInst : instancesMap.values()) {
640                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "vfInst name is {} . OriginType {}. ", vfInst.getName(), vfInst.getOriginType());
641                 if (vfInst.getOriginType().name().equals(OriginTypeEnum.VF.name())) {
642                     collectInstanceInputAndGroups(instInputs, instGroups, instArtifactsMap, origCompMap, isAddInstGroup, vfInst, clonedVertex);
643                 }
644                 needUpdateComposition = needUpdateComposition || fixToscaComponentName(vfInst, origCompMap);
645                 if (needUpdateComposition) {
646                     instancesMap.put(vfInst.getUniqueId(), vfInst);
647                 }
648             }
649             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before add to graph instInputs {}  instGroups {} needUpdateComposition {}", instInputs, instGroups, needUpdateComposition);
650             if (!instInputs.isEmpty()) {
651                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before add inst inputs {} ", instInputs == null ? 0 : instInputs.size());
652                 GraphVertex toscaDataVertex = null;
653                 Either<GraphVertex, JanusGraphOperationStatus> instInpVertexEither = janusGraphDao
654                     .getChildVertex(toscaElementVertex, EdgeLabelEnum.INST_INPUTS, JsonParseFlagEnum.ParseJson);
655                 if (instInpVertexEither.isLeft()) {
656                     toscaDataVertex = instInpVertexEither.left().value();
657                 }
658
659                 StorageOperationStatus status = handleToscaData(clonedVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, toscaDataVertex, instInputs);
660                 if (status != StorageOperationStatus.OK) {
661                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update instance inputs . Status is {}. ", status);
662                     result = Either.right(status);
663                     return result;
664                 }
665
666             }
667             if (!instGroups.isEmpty()) {
668                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before add inst groups {} ", instGroups == null ? 0 : instGroups.size());
669                 GraphVertex toscaDataVertex = null;
670                 Either<GraphVertex, JanusGraphOperationStatus> instGrVertexEither = janusGraphDao
671                     .getChildVertex(toscaElementVertex, EdgeLabelEnum.INST_GROUPS, JsonParseFlagEnum.ParseJson);
672                 if (instGrVertexEither.isLeft()) {
673                     toscaDataVertex = instGrVertexEither.left().value();
674                 }
675
676                 StorageOperationStatus status = handleToscaData(clonedVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, toscaDataVertex, instGroups);
677                 if (status != StorageOperationStatus.OK) {
678                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update instance group . Status is {}. ", status);
679                     result = Either.right(status);
680                     return result;
681                 }
682
683             }
684             if (needUpdateComposition) {
685                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before update Instances ");
686                 Map<String, CompositionDataDefinition> jsonComposition = (Map<String, CompositionDataDefinition>) clonedVertex.getJson();
687                 CompositionDataDefinition compositionDataDefinition = jsonComposition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
688                 compositionDataDefinition.setComponentInstances(instancesMap);
689                 Either<GraphVertex, JanusGraphOperationStatus> updateElement = janusGraphDao.updateVertex(clonedVertex);
690                 if (updateElement.isRight()) {
691                     JanusGraphOperationStatus status = updateElement.right().value();
692                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update instances on metadata vertex . Status is {}. ", status);
693                     result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
694                     return result;
695                 }
696             }
697
698             result = operation.getToscaElement(clonedVertex.getUniqueId());
699
700         } else {
701             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "RI map empty on component {}", toscaElement.getUniqueId());
702         }
703         return result;
704     }
705
706     // TODO remove after jsonModelMigration
707     public boolean resolveToscaComponentName(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap) {
708         return fixToscaComponentName(vfInst, origCompMap);
709     }
710
711     private boolean fixToscaComponentName(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap) {
712         if (vfInst.getToscaComponentName() == null || vfInst.getToscaComponentName().isEmpty()) {
713             String ciUid = vfInst.getUniqueId();
714             String origCompUid = vfInst.getComponentUid();
715             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "fixToscaComponentName:: Ri id {} . origin component id is {}. type is{} ", ciUid, origCompUid, vfInst.getOriginType());
716             ToscaElement origComp = null;
717             if (!origCompMap.containsKey(origCompUid)) {
718                 Either<ToscaElement, StorageOperationStatus> origCompEither;
719                 if (vfInst.getOriginType() == null || vfInst.getOriginType().name().equals(OriginTypeEnum.VF.name())) {
720                     origCompEither = topologyTemplateOperation.getToscaElement(origCompUid);
721                 } else {
722                     origCompEither = nodeTypeOperation.getToscaElement(origCompUid);
723                 }
724                 if (origCompEither.isRight()) {
725                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find orig component {} . Status is {}. ", origCompEither.right().value());
726                     return false;
727                 }
728                 origComp = origCompEither.left().value();
729                 origCompMap.put(origCompUid, origComp);
730             } else {
731                 origComp = origCompMap.get(origCompUid);
732             }
733             String toscaName = (String) origComp.getMetadataValue(JsonPresentationFields.TOSCA_RESOURCE_NAME);
734             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Origin component id is {}. toscaName {}", origCompUid, toscaName);
735             vfInst.setToscaComponentName(toscaName);
736             return true;
737         }
738         return false;
739     }
740
741     private void collectInstanceInputAndGroups(Map<String, MapPropertiesDataDefinition> instInputs, Map<String, MapGroupsDataDefinition> instGroups, Map<String, MapArtifactDataDefinition> instArtifactsMap, Map<String, ToscaElement> origCompMap,
742             boolean isAddInstGroup, ComponentInstanceDataDefinition vfInst, GraphVertex clonedVertex) {
743         String ciUid = vfInst.getUniqueId();
744         String origCompUid = vfInst.getComponentUid();
745         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "collectInstanceInputAndGroups:: Ri id {} . origin component id is {}. ", ciUid, origCompUid);
746         TopologyTemplate origComp = null;
747         if (!origCompMap.containsKey(origCompUid)) {
748             Either<ToscaElement, StorageOperationStatus> origCompEither = topologyTemplateOperation.getToscaElement(origCompUid);
749             if (origCompEither.isRight()) {
750                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find orig component {} . Status is {}. ", origCompEither.right().value());
751                 return;
752             }
753             origComp = (TopologyTemplate) origCompEither.left().value();
754             origCompMap.put(origCompUid, origComp);
755         } else {
756             origComp = (TopologyTemplate) origCompMap.get(origCompUid);
757         }
758         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Orig component {}. ", origComp.getUniqueId());
759
760         Map<String, PropertyDataDefinition> origInputs = origComp.getInputs();
761         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Orig component inputs size {}. ", origInputs == null ? 0 : origInputs.size());
762         if (origInputs != null) {
763             if (!instInputs.containsKey(ciUid)) {
764                 MapPropertiesDataDefinition instProperties = new MapPropertiesDataDefinition(origInputs);
765                 instInputs.put(ciUid, instProperties);
766             } else {
767
768                 MapPropertiesDataDefinition instInputMap = instInputs.get(ciUid);
769                 Map<String, PropertyDataDefinition> instProp = instInputMap.getMapToscaDataDefinition();
770                 origInputs.forEach((propName, propMap) -> {
771                     if (!instProp.containsKey(propName)) {
772                         instProp.put(propName, propMap);
773                     }
774                 });
775             }
776             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "ComponentInstanseInputs {}. ", instInputs.get(ciUid));
777         }
778
779         if (isAddInstGroup) {
780             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before create group instance. ");
781             List<GroupDataDefinition> filteredGroups = null;
782
783             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check vf groups before filter. Size is {} ", filteredGroups == null ? 0 : filteredGroups.size());
784             if (origComp.getGroups() != null && !origComp.getGroups().isEmpty()) {
785                 filteredGroups = origComp.getGroups().values().stream().filter(g -> g.getType().equals(VF_MODULE)).collect(Collectors.toList());
786                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check vf groups . Size is {} ", filteredGroups == null ? 0 : filteredGroups.size());
787             }
788             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check vf groups after filter. Size is {} ", filteredGroups == null ? 0 : filteredGroups.size());
789             if (CollectionUtils.isNotEmpty(filteredGroups)) {
790                 MapArtifactDataDefinition instArifacts = null;
791                 if (!instArtifactsMap.containsKey(ciUid)) {
792
793                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "istance artifacts not found ");
794
795                     Map<String, ArtifactDataDefinition> deploymentArtifacts = origComp.getDeploymentArtifacts();
796
797                     instArifacts = new MapArtifactDataDefinition(deploymentArtifacts);
798                     addToscaDataDeepElementsBlockToToscaElement(clonedVertex, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, instArifacts, ciUid);
799
800                     instArtifactsMap.put(ciUid, instArifacts);
801
802                 } else {
803                     instArifacts = instArtifactsMap.get(ciUid);
804                 }
805
806                 if (instArifacts != null) {
807                     Map<String, ArtifactDataDefinition> instDeplArtifMap = instArifacts.getMapToscaDataDefinition();
808
809                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check group dep artifacts. Size is {} ", instDeplArtifMap == null ? 0 : instDeplArtifMap.values().size());
810                     Map<String, GroupInstanceDataDefinition> groupInstanceToCreate = new HashMap<>();
811                     for (GroupDataDefinition group : filteredGroups) {
812                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "create new groupInstance  {} ", group.getName());
813                         GroupInstanceDataDefinition groupInstance = buildGroupInstanceDataDefinition(group, vfInst, instDeplArtifMap);
814                         List<String> artifactsUid = new ArrayList<>();
815                         List<String> artifactsId = new ArrayList<>();
816                         if (instDeplArtifMap!=null) {
817                                 for (ArtifactDataDefinition artifact : instDeplArtifMap.values()) {
818                                     Optional<String> op = group.getArtifacts().stream().filter(p -> p.equals(artifact.getGeneratedFromId())).findAny();
819                                     if (op.isPresent()) {
820                                         artifactsUid.add(artifact.getArtifactUUID());
821                                         artifactsId.add(artifact.getUniqueId());
822         
823                                     }
824                                 }
825                         }
826                         groupInstance.setGroupInstanceArtifacts(artifactsId);
827                         groupInstance.setGroupInstanceArtifactsUuid(artifactsUid);
828                         groupInstanceToCreate.put(groupInstance.getName(), groupInstance);
829                     }
830                     if (MapUtils.isNotEmpty(groupInstanceToCreate)) {
831                         instGroups.put(vfInst.getUniqueId(), new MapGroupsDataDefinition(groupInstanceToCreate));
832
833                     }
834                 }
835             }
836         }
837     }
838
839     private GraphVertex cloneGraphVertexForCheckout(GraphVertex toscaElementVertex, GraphVertex modifierVertex) {
840         GraphVertex nextVersionToscaElementVertex = new GraphVertex();
841         String uniqueId = UniqueIdBuilder.buildComponentUniqueId();
842         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>(toscaElementVertex.getMetadataProperties());
843         nextVersionToscaElementVertex.setMetadataProperties(metadataProperties);
844         nextVersionToscaElementVertex.setUniqueId(uniqueId);
845         nextVersionToscaElementVertex.setLabel(toscaElementVertex.getLabel());
846         nextVersionToscaElementVertex.setType(toscaElementVertex.getType());
847
848         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
849         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, nextVersionToscaElementVertex.getType().name());
850         String nextVersion = getNextVersion((String) toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION));
851         if (isFirstCheckoutAfterCertification(nextVersion)) {
852             nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.UUID, IdBuilderUtils.generateUUID());
853         }
854         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.VERSION, nextVersion);
855         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
856         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
857
858         if (toscaElementVertex.getType() == ComponentTypeEnum.SERVICE) {
859             nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
860         }
861         if (!MapUtils.isEmpty(toscaElementVertex.getMetadataJson())) {
862             nextVersionToscaElementVertex.setMetadataJson(new HashMap<>(toscaElementVertex.getMetadataJson()));
863             nextVersionToscaElementVertex.updateMetadataJsonWithCurrentMetadataProperties();
864         }
865         long currTime = System.currentTimeMillis();
866         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.CREATION_DATE, currTime);
867         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, currTime);
868         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_CREATOR, modifierVertex.getUniqueId());
869         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_LAST_UPDATER, modifierVertex.getUniqueId());
870         if (toscaElementVertex.getType() == ComponentTypeEnum.SERVICE) {
871             nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.CONFORMANCE_LEVEL, ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel());
872         }
873
874         if (!MapUtils.isEmpty(toscaElementVertex.getJson())) {
875             nextVersionToscaElementVertex.setJson(new HashMap<String, ToscaDataDefinition>(toscaElementVertex.getJson()));
876         }
877         return nextVersionToscaElementVertex;
878     }
879
880     private Either<GraphVertex, StorageOperationStatus> cloneToscaElementForCertify(GraphVertex toscaElementVertex, GraphVertex modifierVertex, Integer majorVersion) {
881         Either<GraphVertex, StorageOperationStatus> result;
882         Either<List<GraphVertex>, StorageOperationStatus> deleteResult = null;
883         GraphVertex clonedToscaElement = null;
884         result = getToscaElementOperation(toscaElementVertex.getLabel()).cloneToscaElement(toscaElementVertex, cloneGraphVertexForCertify(toscaElementVertex, modifierVertex, majorVersion), modifierVertex);
885         if (result.isRight()) {
886             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element {} for certification. Sattus is {}. ", toscaElementVertex.getUniqueId(), result.right().value());
887         } else {
888             clonedToscaElement = result.left().value();
889             StorageOperationStatus updateEdgeToCatalog = updateEdgeToCatalogRoot(clonedToscaElement, toscaElementVertex);
890             if (updateEdgeToCatalog != StorageOperationStatus.OK) {
891                 return Either.right(updateEdgeToCatalog);
892             }
893             deleteResult = deleteAllPreviousNotCertifiedVersions(toscaElementVertex);
894             if (deleteResult.isRight()) {
895                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete all previous npt certified versions of tosca element {}. Status is {}. ", toscaElementVertex.getUniqueId(), deleteResult.right().value());
896                 result = Either.right(deleteResult.right().value());
897             }
898         }
899         if (result.isLeft()) {
900             result = handlePreviousVersionRelation(clonedToscaElement, deleteResult.left().value(), majorVersion);
901         }
902         return result;
903     }
904
905     private Either<GraphVertex, StorageOperationStatus> handlePreviousVersionRelation(GraphVertex clonedToscaElement, List<GraphVertex> deletedVersions, Integer majorVersion) {
906         Either<GraphVertex, StorageOperationStatus> result = null;
907         Vertex previousCertifiedToscaElement = null;
908         if (majorVersion > 0) {
909             List<GraphVertex> firstMinorVersionVertex = deletedVersions.stream().filter(gv -> getMinorVersion((String) gv.getMetadataProperty(GraphPropertyEnum.VERSION)) == 1).collect(Collectors.toList());
910
911             if (CollectionUtils.isEmpty(firstMinorVersionVertex)) {
912                 result = Either.right(StorageOperationStatus.NOT_FOUND);
913             } else {
914                 previousCertifiedToscaElement = getPreviousCertifiedToscaElement(firstMinorVersionVertex.get(0));
915                 if (previousCertifiedToscaElement == null) {
916                     result = Either.right(StorageOperationStatus.NOT_FOUND);
917                 }
918             }
919             if (result == null) {
920                 JanusGraphOperationStatus
921                     status = janusGraphDao
922                     .createEdge(previousCertifiedToscaElement, clonedToscaElement.getVertex(), EdgeLabelEnum.VERSION, new HashMap<>());
923                 if (status != JanusGraphOperationStatus.OK) {
924                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge with label {} from vertex {} to tosca element vertex {} on graph. Status is {}. ", EdgeLabelEnum.VERSION,
925                             previousCertifiedToscaElement.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), clonedToscaElement.getUniqueId(), status);
926                     result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
927                 }
928             }
929
930         }
931         if (result == null) {
932             result = Either.left(clonedToscaElement);
933         }
934         return result;
935     }
936
937     private Vertex getPreviousCertifiedToscaElement(GraphVertex graphVertex) {
938
939         Iterator<Edge> edges = graphVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.VERSION.name());
940         if (edges.hasNext()) {
941             return edges.next().outVertex();
942         }
943         return null;
944     }
945
946     private Either<List<GraphVertex>, StorageOperationStatus> deleteAllPreviousNotCertifiedVersions(GraphVertex toscaElementVertex) {
947         Either<List<GraphVertex>, StorageOperationStatus> result = null;
948
949         ToscaElementOperation operation = getToscaElementOperation(toscaElementVertex.getLabel());
950         List<GraphVertex> previosVersions = null;
951         Object uuid = toscaElementVertex.getMetadataProperty(GraphPropertyEnum.UUID);
952         Object componentName = toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NAME);
953         try {
954             Map<GraphPropertyEnum, Object> properties = new HashMap<>();
955             properties.put(GraphPropertyEnum.UUID, uuid);
956             properties.put(GraphPropertyEnum.NAME, componentName);
957             Either<List<GraphVertex>, JanusGraphOperationStatus> getToscaElementsRes = janusGraphDao
958                 .getByCriteria(toscaElementVertex.getLabel(), properties, JsonParseFlagEnum.ParseMetadata);
959             if (getToscaElementsRes.isRight()) {
960                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementsRes.right().value()));
961             }
962             if (result == null) {
963                 previosVersions = getToscaElementsRes.left().value();
964                 Either<Boolean, StorageOperationStatus> deleteResult = markToscaElementsAsDeleted(operation, getToscaElementsRes.left().value());
965                 if (deleteResult.isRight()) {
966                     result = Either.right(deleteResult.right().value());
967                 }
968             }
969             if (result == null) {
970                 result = Either.left(previosVersions);
971             }
972         } catch (Exception e) {
973             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during deleteng all tosca elements by UUID {} and name {}. {} ", uuid, componentName, e.getMessage());
974         }
975         return result;
976     }
977
978     private GraphVertex cloneGraphVertexForCertify(GraphVertex toscaElementVertex, GraphVertex modifierVertex, Integer majorVersion) {
979
980         GraphVertex nextVersionToscaElementVertex = new GraphVertex();
981         String uniqueId = IdBuilderUtils.generateUniqueId();
982         Map<GraphPropertyEnum, Object> metadataProperties = new EnumMap<>(toscaElementVertex.getMetadataProperties());
983         nextVersionToscaElementVertex.setMetadataProperties(metadataProperties);
984         nextVersionToscaElementVertex.setUniqueId(uniqueId);
985         nextVersionToscaElementVertex.setLabel(toscaElementVertex.getLabel());
986         nextVersionToscaElementVertex.setType(toscaElementVertex.getType());
987
988         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
989         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, nextVersionToscaElementVertex.getType().name());
990         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.VERSION, (majorVersion + 1) + VERSION_DELIMITER + "0");
991         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
992         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
993         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.CREATION_DATE, System.currentTimeMillis());
994         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, null);
995         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_CREATOR, modifierVertex.getUniqueId());
996         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_LAST_UPDATER, modifierVertex.getUniqueId());
997
998         if (toscaElementVertex.getType() == ComponentTypeEnum.SERVICE) {
999             nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
1000         }
1001         if (!MapUtils.isEmpty(toscaElementVertex.getMetadataJson())) {
1002             nextVersionToscaElementVertex.setMetadataJson(new HashMap<>(toscaElementVertex.getMetadataJson()));
1003             nextVersionToscaElementVertex.updateMetadataJsonWithCurrentMetadataProperties();
1004         }
1005         if (!MapUtils.isEmpty(toscaElementVertex.getJson())) {
1006             nextVersionToscaElementVertex.setJson(new HashMap<String, ToscaDataDefinition>(toscaElementVertex.getJson()));
1007         }
1008         return nextVersionToscaElementVertex;
1009     }
1010
1011
1012     private Either<GraphVertex, StorageOperationStatus> checkinToscaELement(LifecycleStateEnum currState, GraphVertex toscaElementVertex, GraphVertex ownerVertex, GraphVertex modifierVertex, LifecycleStateEnum nextState) {
1013         Either<GraphVertex, StorageOperationStatus> updateRelationsRes;
1014         Either<GraphVertex, StorageOperationStatus> result = changeStateToCheckedIn(currState, toscaElementVertex, ownerVertex, modifierVertex);
1015         if (result.isLeft()) {
1016             toscaElementVertex.addMetadataProperty(GraphPropertyEnum.STATE, nextState.name());
1017             toscaElementVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
1018             result = updateToscaElementVertexMetadataPropertiesAndJson(toscaElementVertex);
1019         }
1020         if (result.isLeft()) {
1021             updateRelationsRes = updateLastModifierEdge(toscaElementVertex, ownerVertex, modifierVertex);
1022             if (updateRelationsRes.isRight()) {
1023                 result = Either.right(updateRelationsRes.right().value());
1024             }
1025         }
1026         return result;
1027     }
1028
1029     private Either<GraphVertex, StorageOperationStatus> updateToscaElementVertexMetadataPropertiesAndJson(GraphVertex toscaElementVertex) {
1030
1031         Either<GraphVertex, StorageOperationStatus> result;
1032
1033         Either<GraphVertex, JanusGraphOperationStatus> updateVertexRes = janusGraphDao.updateVertex(toscaElementVertex);
1034         if (updateVertexRes.isRight()) {
1035             JanusGraphOperationStatus titatStatus = updateVertexRes.right().value();
1036             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update state of tosca element vertex {} metadata. Status is  {}", toscaElementVertex.getUniqueId(), titatStatus);
1037             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(titatStatus));
1038         } else {
1039             result = Either.left(updateVertexRes.left().value());
1040         }
1041         return result;
1042     }
1043
1044     private Either<GraphVertex, StorageOperationStatus> changeStateToCheckedIn(LifecycleStateEnum currState, GraphVertex toscaElementVertex, GraphVertex ownerVertex, GraphVertex modifierVertex) {
1045         Either<GraphVertex, StorageOperationStatus> result = null;
1046         LifecycleStateEnum nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
1047         String faileToUpdateStateMsg = "Failed to update state of tosca element {}. Status is  {}";
1048
1049         // Remove CHECKOUT relation
1050             Either<Edge, JanusGraphOperationStatus> deleteEdgeResult = janusGraphDao
1051                 .deleteEdge(ownerVertex, toscaElementVertex, EdgeLabelEnum.STATE);
1052         if (deleteEdgeResult.isRight()) {
1053             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, faileToUpdateStateMsg, toscaElementVertex.getUniqueId());
1054                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteEdgeResult.right().value()));
1055         }
1056
1057         if (result == null) {
1058             // Create CHECKIN relation
1059             Map<EdgePropertyEnum, Object> edgeProperties = new EnumMap<>(EdgePropertyEnum.class);
1060             edgeProperties.put(EdgePropertyEnum.STATE, nextState);
1061             JanusGraphOperationStatus
1062                 createEdgeRes = janusGraphDao
1063                 .createEdge(modifierVertex.getVertex(), toscaElementVertex.getVertex(), EdgeLabelEnum.STATE, edgeProperties);
1064             if (createEdgeRes != JanusGraphOperationStatus.OK) {
1065                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, faileToUpdateStateMsg, toscaElementVertex.getUniqueId());
1066                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes));
1067             }
1068         }
1069         if (result == null) {
1070             result = Either.left(toscaElementVertex);
1071         }
1072         return result;
1073     }
1074
1075     private Either<GraphVertex, StorageOperationStatus> updateLastModifierEdge(GraphVertex toscaElementVertex, GraphVertex ownerVertex, GraphVertex modifierVertex) {
1076         Either<GraphVertex, StorageOperationStatus> result = null;
1077         if (!modifierVertex.getMetadataProperties().get(GraphPropertyEnum.USERID).equals(ownerVertex.getMetadataProperties().get(GraphPropertyEnum.USERID))) {
1078             Either<Edge, JanusGraphOperationStatus> deleteEdgeRes = janusGraphDao
1079                 .deleteEdge(ownerVertex, toscaElementVertex, EdgeLabelEnum.LAST_MODIFIER);
1080             if (deleteEdgeRes.isRight()) {
1081                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete last modifier {} to tosca element {}. Edge type is {}", ownerVertex.getUniqueId(), ownerVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
1082                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteEdgeRes.right().value()));
1083             }
1084             if (result == null) {
1085                 JanusGraphOperationStatus createEdgeRes = janusGraphDao
1086                     .createEdge(modifierVertex.getVertex(), toscaElementVertex.getVertex(), EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
1087
1088                 if (createEdgeRes != JanusGraphOperationStatus.OK) {
1089                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to associate user {} to component {}. Edge type is {}", modifierVertex.getUniqueId(), ownerVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
1090                     result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes));
1091                 } else {
1092                     result = Either.left(modifierVertex);
1093                 }
1094             }
1095         } else {
1096             result = Either.left(ownerVertex);
1097         }
1098         return result;
1099     }
1100
1101     private Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> prepareParametersToGetVerticesForCheckin(String toscaElementId, String modifierId, String ownerId) {
1102         Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGetParameters = new HashMap<>();
1103         verticesToGetParameters.put(toscaElementId, new ImmutablePair<>(GraphPropertyEnum.UNIQUE_ID, JsonParseFlagEnum.ParseMetadata));
1104         verticesToGetParameters.put(modifierId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1105         verticesToGetParameters.put(ownerId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1106         return verticesToGetParameters;
1107     }
1108
1109     private Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> prepareParametersToGetVerticesForRequestCertification(String toscaElementId, String modifierId, String ownerId) {
1110         Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGetParameters = new HashMap<>();
1111         verticesToGetParameters.put(toscaElementId, new ImmutablePair<>(GraphPropertyEnum.UNIQUE_ID, JsonParseFlagEnum.ParseAll));
1112         verticesToGetParameters.put(modifierId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1113         verticesToGetParameters.put(ownerId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1114         return verticesToGetParameters;
1115     }
1116
1117     private Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> prepareParametersToGetVerticesForCheckout(String toscaElementId, String modifierId, String ownerId) {
1118         //Implementation is currently identical
1119         return prepareParametersToGetVerticesForRequestCertification(toscaElementId,modifierId, ownerId);
1120     }
1121
1122     private String getNextCertifiedVersion(String version) {
1123         String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
1124         Integer nextMajorVersion = Integer.parseInt(versionParts[0]) + 1;
1125         return nextMajorVersion + VERSION_DELIMITER + "0";
1126     }
1127
1128     private String getNextVersion(String currVersion) {
1129         String[] versionParts = currVersion.split(VERSION_DELIMITER_REGEXP);
1130         Integer minorVersion = Integer.parseInt(versionParts[1]) + 1;
1131         return versionParts[0] + VERSION_DELIMITER + minorVersion;
1132     }
1133
1134     private Integer getMinorVersion(String version) {
1135         String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
1136         return Integer.parseInt(versionParts[1]);
1137     }
1138
1139     private Integer getMajorVersion(String version) {
1140         String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
1141         return Integer.parseInt(versionParts[0]);
1142     }
1143
1144     private boolean isFirstCheckoutAfterCertification(String version) {
1145         return (Integer.parseInt(version.split(VERSION_DELIMITER_REGEXP)[0]) != 0 && Integer.parseInt(version.split(VERSION_DELIMITER_REGEXP)[1]) == 1);
1146     }
1147
1148     public Either<ToscaElement, StorageOperationStatus> forceCerificationOfToscaElement(String toscaElementId, String modifierId, String ownerId, String currVersion) {
1149         Either<GraphVertex, StorageOperationStatus> resultUpdate = null;
1150         Either<ToscaElement, StorageOperationStatus> result = null;
1151         GraphVertex toscaElement = null;
1152         GraphVertex modifier = null;
1153         GraphVertex owner;
1154         try {
1155             Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
1156                 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
1157             if (getVerticesRes.isRight()) {
1158                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
1159                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
1160             }
1161             if (result == null) {
1162                 toscaElement = getVerticesRes.left().value().get(toscaElementId);
1163                 modifier = getVerticesRes.left().value().get(modifierId);
1164                 owner = getVerticesRes.left().value().get(ownerId);
1165
1166                 StorageOperationStatus status = handleRelationsUponForceCertification(toscaElement, modifier, owner);
1167                 if (status != StorageOperationStatus.OK) {
1168                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations on certification request for tosca element {}. Status is {}. ", toscaElement.getUniqueId(), status);
1169                 }
1170             }
1171             if (result == null) {
1172                 LifecycleStateEnum nextState = LifecycleStateEnum.CERTIFIED;
1173
1174                 toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1175                 toscaElement.addMetadataProperty(GraphPropertyEnum.VERSION, getNextCertifiedVersion(currVersion));
1176
1177                 resultUpdate = updateToscaElementVertexMetadataPropertiesAndJson(toscaElement);
1178                 if (resultUpdate.isRight()) {
1179                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to set lifecycle for tosca elememt {} to state {}, error: {}", toscaElement.getUniqueId(), nextState, resultUpdate.right().value());
1180                     result = Either.right(resultUpdate.right().value());
1181                 }
1182             }
1183             if (result == null) {
1184                 ToscaElementOperation operation = getToscaElementOperation(toscaElement.getLabel());
1185                 result = operation.getToscaElement(toscaElement.getUniqueId());
1186             }
1187             return result;
1188
1189         } catch (Exception e) {
1190             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during request certification tosca element {}. {}", toscaElementId, e.getMessage());
1191         }
1192         return result;
1193     }
1194
1195     private StorageOperationStatus handleRelationsUponForceCertification(GraphVertex toscaElement, GraphVertex modifier, GraphVertex owner) {
1196
1197         StorageOperationStatus result = null;
1198         JanusGraphOperationStatus status = janusGraphDao
1199             .replaceEdgeLabel(owner.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE);
1200         if (status != JanusGraphOperationStatus.OK) {
1201             result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
1202         }
1203         if (result == null) {
1204             Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
1205             properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.CERTIFIED);
1206             status = janusGraphDao
1207                 .createEdge(modifier, toscaElement, EdgeLabelEnum.STATE, properties);
1208             if (status != JanusGraphOperationStatus.OK) {
1209                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "failed to create edge. Status is {}", status);
1210                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
1211             }
1212         }
1213         if (result == null) {
1214             result = StorageOperationStatus.OK;
1215         }
1216         return result;
1217     }
1218
1219     private StorageOperationStatus updateEdgeToCatalogRootByUndoCheckout(JanusGraphVertex preV, GraphVertex curV) {
1220         if (preV == null) {
1221             return updateEdgeToCatalogRoot(null, curV);
1222         }
1223         String uniqueIdPreVer = (String) janusGraphDao
1224             .getProperty((JanusGraphVertex) preV, GraphPropertyEnum.UNIQUE_ID.getProperty());
1225         LifecycleStateEnum state = LifecycleStateEnum.findState((String) janusGraphDao
1226             .getProperty(preV, GraphPropertyEnum.STATE.getProperty()));
1227         if (state == LifecycleStateEnum.CERTIFIED) {
1228             return updateEdgeToCatalogRoot(null, curV);
1229         }
1230         return janusGraphDao.getVertexById(uniqueIdPreVer)
1231                 .either(l -> updateEdgeToCatalogRoot(l, curV),
1232                         DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
1233     }
1234
1235     private StorageOperationStatus updateEdgeToCatalogRoot(GraphVertex newVersionV, GraphVertex prevVersionV) {
1236         Either<GraphVertex, JanusGraphOperationStatus> catalog = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT);
1237         if (catalog.isRight()) {
1238             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch catalog vertex. error {}", catalog.right().value());
1239             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(catalog.right().value());
1240         }
1241         GraphVertex catalogV = catalog.left().value();
1242         if (newVersionV != null) {
1243             Boolean isAbstract = (Boolean) newVersionV.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
1244                         
1245                         if ( isAbstract == null || !isAbstract ) {
1246                 // no new vertex, only delete previous
1247                 JanusGraphOperationStatus
1248                     result = janusGraphDao
1249                     .createEdge(catalogV, newVersionV, EdgeLabelEnum.CATALOG_ELEMENT, null);
1250                 if (result != JanusGraphOperationStatus.OK) {
1251                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge from {} to catalog vertex. error {}", newVersionV.getUniqueId(), result);
1252                     return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
1253                 }
1254             }
1255         }
1256         if (prevVersionV != null) {
1257             Boolean isAbstract = (Boolean) prevVersionV.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
1258             if (isAbstract == null || !isAbstract) {
1259                 // if prev == null -> new resource was added
1260                 Either<Edge, JanusGraphOperationStatus> deleteResult = janusGraphDao
1261                     .deleteEdge(catalogV, prevVersionV, EdgeLabelEnum.CATALOG_ELEMENT);
1262                 if (deleteResult.isRight()) {
1263                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete edge from {} to catalog vertex. error {}", prevVersionV.getUniqueId(), deleteResult.right().value());
1264                     return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteResult.right().value());
1265                 }
1266             }
1267         }
1268         return StorageOperationStatus.OK;
1269     }
1270 }