Add lombok support to simple classes
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaElementLifecycleOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
22
23 import org.janusgraph.core.JanusGraphVertex;
24 import fj.data.Either;
25 import org.apache.commons.collections.CollectionUtils;
26 import org.apache.commons.collections.MapUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.lang3.tuple.ImmutablePair;
29 import org.apache.tinkerpop.gremlin.structure.Direction;
30 import org.apache.tinkerpop.gremlin.structure.Edge;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.openecomp.sdc.be.config.ConfigurationManager;
33 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
34 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
35 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
36 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
37 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
38 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
39 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
40 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
41 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
42 import org.openecomp.sdc.be.datatypes.elements.*;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
45 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
46 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
47 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
48 import org.openecomp.sdc.be.model.DistributionStatusEnum;
49 import org.openecomp.sdc.be.model.LifecycleStateEnum;
50 import org.openecomp.sdc.be.model.User;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
52 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
54 import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum;
55 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
56 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
57 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
58 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
59 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
60 import org.openecomp.sdc.common.log.wrappers.Logger;
61
62 import java.util.*;
63 import java.util.stream.Collectors;
64
65 @org.springframework.stereotype.Component("tosca-element-lifecycle-operation")
66
67 /**
68  * Allows to perform lifecycle operations: checkin, checkout, submit for testing, start certification and certification process for tosca element
69  */
70 public class ToscaElementLifecycleOperation extends BaseOperation {
71
72     private static final String FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS = "Failed to delete last state edge. Status is {}. ";
73         private static final String FAILED_TO_GET_VERTICES = "Failed to get vertices by id {}. Status is {}. ";
74     public static final String VERSION_DELIMITER = ".";
75     public static final String VERSION_DELIMITER_REGEXP = "\\.";
76
77     private static final Logger log = Logger.getLogger(ToscaElementLifecycleOperation.class);
78
79     /**
80      * Performs changing a lifecycle state of tosca element from "checked out" or "ready for certification" to "checked in"
81      * 
82      * @param currState
83      * @param toscaElementId
84      * @param modifierId
85      * @param ownerId
86      * @return
87      */
88     public Either<ToscaElement, StorageOperationStatus> checkinToscaELement(LifecycleStateEnum currState, String toscaElementId, String modifierId, String ownerId) {
89         Either<GraphVertex, StorageOperationStatus> updateResult = null;
90         Either<ToscaElement, StorageOperationStatus> result = null;
91         Map<String, GraphVertex> vertices = null;
92         ToscaElementOperation operation;
93         try {
94             Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
95                 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForCheckin(toscaElementId, modifierId, ownerId));
96             if (getVerticesRes.isRight()) {
97                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
98                 updateResult = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
99             } else {
100                 vertices = getVerticesRes.left().value();
101                 updateResult = checkinToscaELement(currState, vertices.get(toscaElementId), vertices.get(ownerId), vertices.get(modifierId), LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
102             }
103             if (updateResult.isLeft()) {
104                 operation = getToscaElementOperation(vertices.get(toscaElementId).getLabel());
105                 result = operation.getToscaElement(updateResult.left().value().getUniqueId());
106                 if (result.isRight()) {
107                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated tosca element {}. Status is {}", toscaElementId, result.right().value());
108                 }
109             } else {
110                 result = Either.right(updateResult.right().value());
111             }
112         } catch (Exception e) {
113             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during checkin of tosca element {}. {} ", toscaElementId, e.getMessage());
114         }
115         return result;
116     }
117
118     /**
119      * Returns vertex presenting owner of tosca element specified by uniqueId
120      * 
121      * @param toscaElementId
122      * @return
123      */
124     public Either<User, StorageOperationStatus> getToscaElementOwner(String toscaElementId) {
125         Either<User, StorageOperationStatus> result = null;
126         GraphVertex toscaElement = null;
127         Either<GraphVertex, JanusGraphOperationStatus> getToscaElementRes = janusGraphDao
128             .getVertexById(toscaElementId, JsonParseFlagEnum.NoParse);
129         if (getToscaElementRes.isRight()) {
130             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementRes.right().value()));
131         }
132         if (result == null) {
133             toscaElement = getToscaElementRes.left().value();
134             Iterator<Vertex> vertices = toscaElement.getVertex().vertices(Direction.IN, EdgeLabelEnum.STATE.name());
135             if (vertices == null || !vertices.hasNext()) {
136                 result = Either.right(StorageOperationStatus.NOT_FOUND);
137             } else {
138                 result = Either.left(convertToUser(vertices.next()));
139             }
140         }
141         return result;
142     }
143
144     /**
145      * Returns vertex presenting owner of tosca element specified by uniqueId
146      * 
147      * @param toscaElement
148      * @return
149      */
150     public Either<User, StorageOperationStatus> getToscaElementOwner(GraphVertex toscaElement) {
151         Either<User, StorageOperationStatus> result = null;
152         Iterator<Vertex> vertices = toscaElement.getVertex().vertices(Direction.IN, EdgeLabelEnum.STATE.name());
153         if (vertices == null || !vertices.hasNext()) {
154             result = Either.right(StorageOperationStatus.NOT_FOUND);
155         } else {
156             result = Either.left(convertToUser(vertices.next()));
157         }
158         return result;
159     }
160
161     /**
162      * Performs checkout of a tosca element
163      * 
164      * @param toscaElementId
165      * @param modifierId
166      * @param ownerId
167      * @return
168      */
169     public Either<ToscaElement, StorageOperationStatus> checkoutToscaElement(String toscaElementId, String modifierId, String ownerId) {
170         Either<ToscaElement, StorageOperationStatus> result = null;
171         Map<String, GraphVertex> vertices = null;
172         try {
173             Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
174                 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForCheckout(toscaElementId, modifierId, ownerId));
175             if (getVerticesRes.isRight()) {
176                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
177                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
178             }
179             if (result == null) {
180                 vertices = getVerticesRes.left().value();
181                 // update previous component if not certified
182                 StorageOperationStatus status = updatePreviousVersion(vertices.get(toscaElementId), vertices.get(ownerId));
183                 if (status != StorageOperationStatus.OK) {
184                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update vertex with id {} . Status is {}. ", status);
185                     result = Either.right(status);
186                 }
187             }
188             if (result == null) {
189                 result = cloneToscaElementForCheckout(vertices.get(toscaElementId), vertices.get(modifierId));
190                 if (result.isRight()) {
191                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to checkout tosca element {}. Status is {} ", toscaElementId, result.right().value());
192                 }
193
194             }
195         } catch (Exception e) {
196             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during checkout tosca element {}. {}", toscaElementId, e.getMessage());
197         }
198         return result;
199     }
200
201     /**
202      * Performs undo checkout for tosca element
203      * 
204      * @param toscaElementId
205      * @return
206      */
207     public Either<ToscaElement, StorageOperationStatus> undoCheckout(String toscaElementId) {
208         Either<ToscaElement, StorageOperationStatus> result = null;
209         Either<GraphVertex, JanusGraphOperationStatus> getToscaElementRes = null;
210         Iterator<Edge> nextVersionComponentIter = null;
211         ToscaElementOperation operation;
212         Vertex preVersionVertex = null;
213         try {
214             getToscaElementRes = janusGraphDao.getVertexById(toscaElementId, JsonParseFlagEnum.ParseMetadata);
215             if (getToscaElementRes.isRight()) {
216                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
217                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementRes.right().value()));
218             }
219             GraphVertex currVersionV = getToscaElementRes.left().value();
220             if (result == null && hasPreviousVersion(currVersionV)) {
221                 // find previous version
222                 nextVersionComponentIter = currVersionV.getVertex().edges(Direction.IN, EdgeLabelEnum.VERSION.name());
223                 if (nextVersionComponentIter == null || !nextVersionComponentIter.hasNext()) {
224                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch previous version of tosca element with name {}. ", currVersionV.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME).toString());
225                     result = Either.right(StorageOperationStatus.NOT_FOUND);
226                 }
227                 if (result == null) {
228                     preVersionVertex = nextVersionComponentIter.next().outVertex();
229                     StorageOperationStatus updateOldResourceResult = updateOldToscaElementBeforeUndoCheckout(preVersionVertex);
230                     if (updateOldResourceResult != StorageOperationStatus.OK) {
231                         result = Either.right(updateOldResourceResult);
232                     }
233                 }
234             }
235             if (result == null) {
236                 StorageOperationStatus updateCatalogRes = updateEdgeToCatalogRootByUndoCheckout((JanusGraphVertex) preVersionVertex, currVersionV);
237                 if (updateCatalogRes != StorageOperationStatus.OK) {
238                     return Either.right(updateCatalogRes);
239                 }
240                 operation = getToscaElementOperation(currVersionV.getLabel());
241                 result = operation.deleteToscaElement(currVersionV);
242             }
243         } catch (Exception e) {
244             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during undo checkout tosca element {}. {}", toscaElementId, e.getMessage());
245         }
246         return result;
247     }
248
249     private boolean hasPreviousVersion(GraphVertex toscaElementVertex) {
250         boolean hasPreviousVersion = true;
251         String version = (String) toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION);
252         if (StringUtils.isEmpty(version) || "0.1".equals(version))
253             hasPreviousVersion = false;
254         return hasPreviousVersion;
255     }
256
257     /**
258      * Performs request certification for tosca element
259      * 
260      * @param toscaElementId
261      * @param modifierId
262      * @param ownerId
263      * @return
264      */
265     public Either<ToscaElement, StorageOperationStatus> requestCertificationToscaElement(String toscaElementId, String modifierId, String ownerId) {
266         Either<GraphVertex, StorageOperationStatus> resultUpdate = null;
267         Either<ToscaElement, StorageOperationStatus> result = null;
268         GraphVertex toscaElement = null;
269         GraphVertex modifier = null;
270         GraphVertex owner;
271         try {
272             Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
273                 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
274             if (getVerticesRes.isRight()) {
275                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
276                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
277             }
278             if (result == null) {
279                 toscaElement = getVerticesRes.left().value().get(toscaElementId);
280                 modifier = getVerticesRes.left().value().get(modifierId);
281                 owner = getVerticesRes.left().value().get(ownerId);
282
283                 StorageOperationStatus status = handleRelationsUponRequestForCertification(toscaElement, modifier, owner);
284                 if (status != StorageOperationStatus.OK) {
285                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations on certification request for tosca element {}. Status is {}. ", toscaElement.getUniqueId(), status);
286                 }
287             }
288             if (result == null) {
289                 LifecycleStateEnum nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION;
290
291                 toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, nextState.name());
292                 toscaElement.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
293
294                 resultUpdate = updateToscaElementVertexMetadataPropertiesAndJson(toscaElement);
295                 if (resultUpdate.isRight()) {
296                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to set lifecycle for tosca elememt {} to state {}, error: {}", toscaElement.getUniqueId(), nextState, resultUpdate.right().value());
297                     result = Either.right(resultUpdate.right().value());
298                 }
299             }
300             if (result == null) {
301                 ToscaElementOperation operation = getToscaElementOperation(toscaElement.getLabel());
302                 result = operation.getToscaElement(toscaElement.getUniqueId());
303             }
304             return result;
305
306         } catch (Exception e) {
307             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during request certification tosca element {}. {}", toscaElementId, e.getMessage());
308         }
309         return result;
310     }
311
312     /**
313      * Starts certification of tosca element
314      * 
315      * @param toscaElementId
316      * @param modifierId
317      * @param ownerId
318      * @return
319      */
320     public Either<ToscaElement, StorageOperationStatus> startCertificationToscaElement(String toscaElementId, String modifierId, String ownerId) {
321         Either<ToscaElement, StorageOperationStatus> result = null;
322         Either<GraphVertex, StorageOperationStatus> resultUpdate = null;
323         GraphVertex toscaElement = null;
324         GraphVertex modifier = null;
325         GraphVertex owner;
326         try {
327             Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
328                 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
329             if (getVerticesRes.isRight()) {
330                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
331                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
332             }
333             if (result == null) {
334                 toscaElement = getVerticesRes.left().value().get(toscaElementId);
335                 modifier = getVerticesRes.left().value().get(modifierId);
336                 owner = getVerticesRes.left().value().get(ownerId);
337
338                 StorageOperationStatus status = handleRelationsUponCertification(toscaElement, modifier, owner);
339                 if (status != StorageOperationStatus.OK) {
340                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations during certification of tosca element {}. Status is {}. ", toscaElement.getUniqueId(), status);
341                 }
342             }
343             if (result == null) {
344                 LifecycleStateEnum nextState = LifecycleStateEnum.CERTIFICATION_IN_PROGRESS;
345
346                 toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, nextState.name());
347                 toscaElement.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
348
349                 resultUpdate = updateToscaElementVertexMetadataPropertiesAndJson(toscaElement);
350                 if (resultUpdate.isRight()) {
351                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Couldn't set lifecycle for component {} to state {}, error: {}", toscaElement.getUniqueId(), nextState, resultUpdate.right().value());
352                     result = Either.right(resultUpdate.right().value());
353                 }
354             }
355             if (result == null) {
356                 ToscaElementOperation operation = getToscaElementOperation(toscaElement.getLabel());
357                 result = operation.getToscaElement(toscaElement.getUniqueId());
358             }
359         } catch (Exception e) {
360             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during start certification tosca element {}. {}", toscaElementId, e.getMessage());
361         }
362         return result;
363     }
364
365     public Either<ToscaElement, StorageOperationStatus> certifyToscaElement(String toscaElementId, String modifierId, String ownerId) {
366         Either<ToscaElement, StorageOperationStatus> result = null;
367         Either<GraphVertex, StorageOperationStatus> cloneRes = null;
368         GraphVertex toscaElement = null;
369         GraphVertex modifier = null;
370         GraphVertex certifiedToscaElement = null;
371         Integer majorVersion = null;
372
373         StorageOperationStatus status;
374         try {
375             Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
376                 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
377             if (getVerticesRes.isRight()) {
378                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
379                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
380             }
381             if (result == null) {
382                 toscaElement = getVerticesRes.left().value().get(toscaElementId);
383                 modifier = getVerticesRes.left().value().get(modifierId);
384                 majorVersion = getMajorVersion((String) toscaElement.getMetadataProperty(GraphPropertyEnum.VERSION));
385                 status = handleRelationsOfPreviousToscaElementBeforeCertifying(toscaElement, modifier, majorVersion);
386                 if (status != StorageOperationStatus.OK) {
387                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations of previous tosca element before certifying {}. Status is {}. ", toscaElement.getUniqueId(), status);
388                 }
389             }
390             if (result == null) {
391                 cloneRes = cloneToscaElementForCertify(toscaElement, modifier, majorVersion);
392                 if (cloneRes.isRight()) {
393                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element during certification. ");
394                     result = Either.right(cloneRes.right().value());
395                 }
396             }
397             if (result == null) {
398                 certifiedToscaElement = cloneRes.left().value();
399                 status = handleRelationsOfNewestCertifiedToscaElement(toscaElement, certifiedToscaElement);
400                 if (status != StorageOperationStatus.OK) {
401                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations of newest certified tosca element {}. Status is {}. ", certifiedToscaElement.getUniqueId(), status);
402                 }
403             }
404             if (result == null) {
405                 return getToscaElementOperation(toscaElement.getLabel()).getToscaElement(certifiedToscaElement.getUniqueId());
406             }
407         } catch (Exception e) {
408             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during certification tosca element {}. {}", toscaElementId, e.getMessage());
409         }
410         return result;
411     }
412
413     /**
414      * Deletes (marks as deleted) all tosca elements according received name and uuid
415      * 
416      * @param vertexType
417      * @param componentType
418      * @param componentName
419      * @param uuid
420      * @return
421      */
422     public Either<Boolean, StorageOperationStatus> deleteOldToscaElementVersions(VertexTypeEnum vertexType, ComponentTypeEnum componentType, String componentName, String uuid) {
423
424         Either<Boolean, StorageOperationStatus> result = null;
425         ToscaElementOperation operation = getToscaElementOperation(componentType);
426
427         try {
428             Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
429             properties.put(GraphPropertyEnum.UUID, uuid);
430             properties.put(GraphPropertyEnum.NAME, componentName);
431             Either<List<GraphVertex>, JanusGraphOperationStatus> getToscaElementsRes = janusGraphDao
432                 .getByCriteria(vertexType, properties, JsonParseFlagEnum.ParseMetadata);
433             if (getToscaElementsRes.isRight()) {
434                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementsRes.right().value()));
435             }
436             if (result == null) {
437                 result = markToscaElementsAsDeleted(operation, getToscaElementsRes.left().value());
438             }
439             if (result == null) {
440                 result = Either.left(true);
441             }
442         } catch (Exception e) {
443             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during deleteng all tosca elements by UUID {} and name {}. {} ", uuid, componentName, e.getMessage());
444         }
445         return result;
446     }
447
448     /**
449      * Performs cancelation or failure of certification for received tosca element
450      * 
451      * @param toscaElementId
452      * @param modifierId
453      * @param ownerId
454      * @param nextState
455      * @return
456      */
457     public Either<ToscaElement, StorageOperationStatus> cancelOrFailCertification(String toscaElementId, String modifierId, String ownerId, LifecycleStateEnum nextState) {
458         Either<ToscaElement, StorageOperationStatus> result = null;
459         StorageOperationStatus status;
460         ToscaElementOperation operation = null;
461         GraphVertex toscaElement = null;
462         GraphVertex modifier = null;
463         try {
464             Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
465                 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
466             if (getVerticesRes.isRight()) {
467                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
468                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
469             }
470             if (result == null) {
471                 toscaElement = getVerticesRes.left().value().get(toscaElementId);
472                 modifier = getVerticesRes.left().value().get(modifierId);
473                 operation = getToscaElementOperation(toscaElement.getLabel());
474                 toscaElement.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
475                 toscaElement.setJsonMetadataField(JsonPresentationFields.USER_ID_LAST_UPDATER, modifier.getUniqueId());
476                 toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, nextState.name());
477
478                 Either<GraphVertex, JanusGraphOperationStatus> updateVertexRes = janusGraphDao.updateVertex(toscaElement);
479                 if (updateVertexRes.isRight()) {
480                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update vertex {} . Status is {}. ", toscaElementId, updateVertexRes.right().value());
481                     result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateVertexRes.right().value()));
482                 }
483             }
484             if (result == null) {
485                 // cancel certification process
486                 status = handleRelationsUponCancelCertification(toscaElement, nextState);
487                 if (status != StorageOperationStatus.OK) {
488                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations upon cancel certification {}. Status is {}. ", toscaElement.getUniqueId(), status);
489                 }
490             }
491             if (result == null) {
492                 // fail certification
493                 status = handleRelationsUponFailCertification(toscaElement, nextState);
494                 if (status != StorageOperationStatus.OK) {
495                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations upon fail certification {}. Status is {}. ", toscaElement.getUniqueId(), status);
496                 }
497             }
498             if (result == null) {
499                 result = operation.getToscaElement(toscaElementId);
500             }
501         } catch (Exception e) {
502             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during cancel or fail certification of tosca element {}. {}. ", toscaElementId, e.getMessage());
503         }
504         return result;
505     }
506
507     public Either<GraphVertex, JanusGraphOperationStatus> findUser(String userId) {
508         return findUserVertex(userId);
509     }
510
511     private Either<Boolean, StorageOperationStatus> markToscaElementsAsDeleted(ToscaElementOperation operation, List<GraphVertex> toscaElements) {
512         Either<Boolean, StorageOperationStatus> result = Either.left(true);
513         for (GraphVertex resourceToDelete : toscaElements) {
514             if (!((String) resourceToDelete.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals(LifecycleStateEnum.CERTIFIED.name())) {
515                 Either<GraphVertex, StorageOperationStatus> deleteElementRes = operation.markComponentToDelete(resourceToDelete);
516                 if (deleteElementRes.isRight()) {
517                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete tosca element {}. Status is {}. ", resourceToDelete.getUniqueId(), deleteElementRes.right().value());
518                     result = Either.right(deleteElementRes.right().value());
519                     break;
520                 }
521             }
522         }
523         return result;
524     }
525
526     private StorageOperationStatus handleRelationsOfNewestCertifiedToscaElement(GraphVertex toscaElement, GraphVertex certifiedToscaElement) {
527         StorageOperationStatus result = null;
528         Edge foundEdge = null;
529         Iterator<Edge> certReqUserEdgeIter = null;
530         // add rfc relation to preserve follower information
531         // get user of certification request
532         certReqUserEdgeIter = toscaElement.getVertex().edges(Direction.IN, GraphEdgeLabels.LAST_STATE.name());
533         if (certReqUserEdgeIter == null || !certReqUserEdgeIter.hasNext()) {
534             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find rfc relation during certification clone. ");
535             result = StorageOperationStatus.NOT_FOUND;
536         }
537         if (result == null) {
538             JanusGraphOperationStatus
539                 createVersionEdgeStatus = janusGraphDao
540                 .createEdge(toscaElement, certifiedToscaElement, EdgeLabelEnum.VERSION, new HashMap<>());
541             if (createVersionEdgeStatus != JanusGraphOperationStatus.OK) {
542                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create version edge from last element {} to new certified element {}. status=", toscaElement.getUniqueId(), certifiedToscaElement.getUniqueId(),
543                         createVersionEdgeStatus);
544                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createVersionEdgeStatus);
545             }
546         }
547         if (result == null) {
548                 if (certReqUserEdgeIter!=null) {
549                     while (certReqUserEdgeIter.hasNext()) {
550                         Edge edge = certReqUserEdgeIter.next();
551                         if (((String) janusGraphDao.getProperty(edge, EdgePropertyEnum.STATE)).equals(LifecycleStateEnum.READY_FOR_CERTIFICATION.name())) {
552                             foundEdge = edge;
553                             break;
554                         }
555         
556                     }
557                 }
558             if (foundEdge == null) {
559                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find rfc relation during certification clone. ");
560                 result = StorageOperationStatus.NOT_FOUND;
561             }
562         }
563         if (result == null) {
564             JanusGraphOperationStatus
565                 createEdgeRes = janusGraphDao
566                 .createEdge(foundEdge.outVertex(), certifiedToscaElement.getVertex(), EdgeLabelEnum.LAST_STATE, foundEdge);
567             if (createEdgeRes != JanusGraphOperationStatus.OK) {
568                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create rfc relation for component {}. status=", certifiedToscaElement.getUniqueId(), createEdgeRes);
569                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes);
570             }
571         }
572         if (result == null) {
573             result = StorageOperationStatus.OK;
574         }
575         return result;
576     }
577
578     private StorageOperationStatus handleRelationsUponFailCertification(GraphVertex toscaElement, LifecycleStateEnum nextState) {
579         StorageOperationStatus result = null;
580         JanusGraphOperationStatus status = null;
581         Edge originEdge;
582         Vertex user = null;
583         if (nextState == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN) {
584             // fail certification
585             // delete relation CERTIFICATION_IN_PROGRESS
586             Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
587             properties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
588
589             Either<Edge, JanusGraphOperationStatus> deleteResult = janusGraphDao
590                 .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.STATE, properties);
591             if (deleteResult.isRight()) {
592                 status = deleteResult.right().value();
593                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete state edge. Status is {}. ", status);
594                 result = StorageOperationStatus.INCONSISTENCY;
595             }
596             if (result == null) {
597                 // delete relation READY_FOR_CERTIFICATION
598                 properties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.READY_FOR_CERTIFICATION);
599                 deleteResult = janusGraphDao
600                     .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.LAST_STATE, properties);
601                 if (deleteResult.isRight()) {
602                     status = deleteResult.right().value();
603                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS, status);
604                     result = StorageOperationStatus.INCONSISTENCY;
605                 }
606             }
607             if (result == null) {
608                 // delete relation NOT_CERTIFIED_CHECKIN (in order to change to STATE)
609                 properties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
610                 deleteResult = janusGraphDao
611                     .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.LAST_STATE, properties);
612                 if (deleteResult.isRight()) {
613                     status = deleteResult.right().value();
614                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS, status);
615                     result = StorageOperationStatus.INCONSISTENCY;
616                 }
617             }
618             if (result == null) {
619                 // create new STATE relation NOT_CERTIFIED_CHECKIN
620                 originEdge = deleteResult.left().value();
621                 user = originEdge.outVertex();
622                 status = janusGraphDao
623                     .createEdge(user, toscaElement.getVertex(), EdgeLabelEnum.STATE, originEdge);
624                 if (status != JanusGraphOperationStatus.OK) {
625                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create state edge. Status is {}. ", status);
626                     result = StorageOperationStatus.INCONSISTENCY;
627                 }
628             }
629             if (result == null) {
630                 // delete relation LAST_MODIFIER (in order to change tester to designer)
631                 deleteResult = janusGraphDao
632                     .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
633                 if (status != JanusGraphOperationStatus.OK) {
634                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create last modifier edge. Status is {}. ", status);
635                     result = StorageOperationStatus.INCONSISTENCY;
636                 }
637             }
638             if (result == null) {
639                 // create new LAST_MODIFIER relation
640                 originEdge = deleteResult.left().value();
641                 status = janusGraphDao.createEdge(user, toscaElement.getVertex(), EdgeLabelEnum.LAST_MODIFIER, originEdge);
642                 if (status != JanusGraphOperationStatus.OK) {
643                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create last modifier edge. Status is {}. ", status);
644                     result = StorageOperationStatus.INCONSISTENCY;
645                 }
646             }
647         }
648         if (result == null) {
649             result = StorageOperationStatus.OK;
650         }
651         return result;
652     }
653
654     private StorageOperationStatus handleRelationsUponCancelCertification(GraphVertex toscaElement, LifecycleStateEnum nextState) {
655         StorageOperationStatus result = null;
656         Edge originEdge;
657         if (nextState == LifecycleStateEnum.READY_FOR_CERTIFICATION) {
658             // delete relation CERTIFICATION_IN_PROGRESS
659             Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
660             properties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
661             Either<Edge, JanusGraphOperationStatus> deleteResult = janusGraphDao
662                 .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.STATE, properties);
663
664             if (deleteResult.isRight()) {
665                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete state edge. Status is  {}. ", deleteResult.right().value());
666                 result = StorageOperationStatus.INCONSISTENCY;
667             }
668             if (result == null) {
669                 // delete relation READY_FOR_CERTIFICATION (LAST_STATE)
670                 properties.put(GraphPropertyEnum.STATE, nextState);
671                 deleteResult = janusGraphDao
672                     .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.LAST_STATE, properties);
673
674                 if (deleteResult.isRight()) {
675                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS, deleteResult.right().value());
676                     result = StorageOperationStatus.INCONSISTENCY;
677                 }
678             }
679             if (result == null) {
680                 // create relation READY_FOR_CERTIFICATION (STATE)
681                 originEdge = deleteResult.left().value();
682                 JanusGraphOperationStatus
683                     status = janusGraphDao
684                     .createEdge(originEdge.outVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, originEdge);
685                 if (status != JanusGraphOperationStatus.OK) {
686                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create state edge. Status is {}. ", status);
687                     result = StorageOperationStatus.INCONSISTENCY;
688                 }
689             }
690             if (result == null) {
691                 result = StorageOperationStatus.OK;
692             }
693         }
694         return result;
695     }
696
697     private StorageOperationStatus handleRelationsOfPreviousToscaElementBeforeCertifying(GraphVertex toscaElement, GraphVertex modifier, Integer majorVersion) {
698         StorageOperationStatus result = null;
699         if (majorVersion > 0) {
700             Either<Vertex, StorageOperationStatus> findRes = findLastCertifiedToscaElementVertex(toscaElement);
701             if (findRes.isRight()) {
702                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch last certified tosca element {} . Status is {}. ", toscaElement.getMetadataProperty(GraphPropertyEnum.NAME), findRes.right().value());
703                 result = findRes.right().value();
704             }
705             if (result == null) {
706                 Vertex lastCertifiedVertex = findRes.left().value();
707                 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
708                 properties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, false);
709                 JanusGraphOperationStatus status = janusGraphDao
710                     .updateVertexMetadataPropertiesWithJson(lastCertifiedVertex, properties);
711                 if (status != JanusGraphOperationStatus.OK) {
712                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to set highest version  of tosca element {} to [{}]. Status is {}", toscaElement.getUniqueId(), false, status);
713                     result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
714                 }
715                 // remove previous certified version from the catalog
716                 GraphVertex lastCertifiedV = new GraphVertex();
717                 lastCertifiedV.setVertex((JanusGraphVertex) lastCertifiedVertex);
718                 lastCertifiedV.setUniqueId((String) janusGraphDao
719                     .getProperty((JanusGraphVertex) lastCertifiedVertex, GraphPropertyEnum.UNIQUE_ID.getProperty()));
720                 StorageOperationStatus res = updateEdgeToCatalogRoot(null, lastCertifiedV);
721                 if (res != StorageOperationStatus.OK) {
722                     return res;
723                 }
724             }
725         }
726         if (result == null) {
727             result = StorageOperationStatus.OK;
728         }
729         return result;
730     }
731
732     private StorageOperationStatus handleRelationsUponRequestForCertification(GraphVertex toscaElement, GraphVertex modifier, GraphVertex owner) {
733         JanusGraphOperationStatus status;
734         StorageOperationStatus result = null;
735
736         if (((String) toscaElement.getMetadataProperty(GraphPropertyEnum.STATE)).equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
737             // remove CHECKOUT relation
738             Either<Edge, JanusGraphOperationStatus> deleteRes = janusGraphDao
739                 .deleteEdge(owner, toscaElement, EdgeLabelEnum.STATE);
740             if (deleteRes.isRight()) {
741                 status = deleteRes.right().value();
742                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete edge. Status is {}. ", status);
743                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
744             }
745             if (result == null) {
746                 // create CHECKIN relation
747                 Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
748                 properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
749                 status = janusGraphDao
750                     .createEdge(modifier.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.LAST_STATE, properties);
751                 if (status != JanusGraphOperationStatus.OK) {
752                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge. Status is {}", status);
753                     result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
754                 }
755             }
756         } else {
757             status = janusGraphDao
758                 .replaceEdgeLabel(owner.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE);
759             if (status != JanusGraphOperationStatus.OK) {
760                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
761             }
762         }
763         if (result == null) {
764             // create RFC relation
765             Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
766             properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.READY_FOR_CERTIFICATION);
767             status = janusGraphDao
768                 .createEdge(modifier.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, properties);
769             if (status != JanusGraphOperationStatus.OK) {
770                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge. Status is {}", status);
771                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
772             }
773         }
774         if (result == null) {
775             result = StorageOperationStatus.OK;
776         }
777         return result;
778     }
779
780     private StorageOperationStatus handleRelationsUponCertification(GraphVertex toscaElement, GraphVertex modifier, GraphVertex owner) {
781
782         StorageOperationStatus result = null;
783         JanusGraphOperationStatus status = janusGraphDao
784             .replaceEdgeLabel(owner.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE);
785         if (status != JanusGraphOperationStatus.OK) {
786             result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
787         }
788         if (result == null) {
789             Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
790             properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
791             status = janusGraphDao
792                 .createEdge(modifier, toscaElement, EdgeLabelEnum.STATE, properties);
793             if (status != JanusGraphOperationStatus.OK) {
794                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "failed to create edge. Status is {}", status);
795                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
796             }
797         }
798         if (result == null) {
799             Either<GraphVertex, StorageOperationStatus> updateRelationsRes = updateLastModifierEdge(toscaElement, owner, modifier);
800             if (updateRelationsRes.isRight()) {
801                 result = updateRelationsRes.right().value();
802             }
803         }
804         if (result == null) {
805             result = StorageOperationStatus.OK;
806         }
807         return result;
808     }
809
810     private Either<Vertex, StorageOperationStatus> findLastCertifiedToscaElementVertex(GraphVertex toscaElement) {
811         return findLastCertifiedToscaElementVertexRecursively(toscaElement.getVertex());
812     }
813
814     private Either<Vertex, StorageOperationStatus> findLastCertifiedToscaElementVertexRecursively(Vertex vertex) {
815         if (isCertifiedVersion((String) vertex.property(GraphPropertyEnum.VERSION.getProperty()).value())) {
816             return Either.left(vertex);
817         }
818         Iterator<Edge> edgeIter = vertex.edges(Direction.IN, EdgeLabelEnum.VERSION.name());
819         if (!edgeIter.hasNext()) {
820             return Either.right(StorageOperationStatus.NOT_FOUND);
821         }
822         return findLastCertifiedToscaElementVertexRecursively(edgeIter.next().outVertex());
823     }
824
825     private boolean isCertifiedVersion(String version) {
826         String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
827         if (Integer.parseInt(versionParts[0]) > 0 && Integer.parseInt(versionParts[1]) == 0) {
828             return true;
829         }
830         return false;
831     }
832
833     private StorageOperationStatus updateOldToscaElementBeforeUndoCheckout(Vertex previousVersionToscaElement) {
834
835         StorageOperationStatus result = StorageOperationStatus.OK;
836         String previousVersion = (String) previousVersionToscaElement.property(GraphPropertyEnum.VERSION.getProperty()).value();
837         if (!previousVersion.endsWith(".0")) {
838             try {
839                 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update vertex of previous version of tosca element", previousVersionToscaElement.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()));
840
841                 Map<String, Object> propertiesToUpdate = new HashMap<>();
842                 propertiesToUpdate.put(GraphPropertyEnum.IS_HIGHEST_VERSION.getProperty(), true);
843                 Map<String, Object> jsonMetadataMap = JsonParserUtils.toMap((String) previousVersionToscaElement.property(GraphPropertyEnum.METADATA.getProperty()).value());
844                 jsonMetadataMap.put(GraphPropertyEnum.IS_HIGHEST_VERSION.getProperty(), true);
845                 propertiesToUpdate.put(GraphPropertyEnum.METADATA.getProperty(), JsonParserUtils.toJson(jsonMetadataMap));
846
847                 janusGraphDao.setVertexProperties(previousVersionToscaElement, propertiesToUpdate);
848
849                 Iterator<Edge> edgesIter = previousVersionToscaElement.edges(Direction.IN, EdgeLabelEnum.LAST_STATE.name());
850                 if (!edgesIter.hasNext()) {
851                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch last modifier vertex for tosca element {}. ", previousVersionToscaElement.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()));
852                     result = StorageOperationStatus.NOT_FOUND;
853                 } else {
854                     Edge lastStateEdge = edgesIter.next();
855                     Vertex lastModifier = lastStateEdge.outVertex();
856                     JanusGraphOperationStatus replaceRes = janusGraphDao
857                         .replaceEdgeLabel(lastModifier, previousVersionToscaElement, lastStateEdge, EdgeLabelEnum.LAST_STATE, EdgeLabelEnum.STATE);
858                     if (replaceRes != JanusGraphOperationStatus.OK) {
859                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to replace label from {} to {}. status = {}", EdgeLabelEnum.LAST_STATE, EdgeLabelEnum.STATE, replaceRes);
860                         result = StorageOperationStatus.INCONSISTENCY;
861                         if (replaceRes != JanusGraphOperationStatus.INVALID_ID) {
862                             result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(replaceRes);
863                         }
864                     }
865
866                 }
867             } catch (Exception e) {
868                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during update previous tosca element {} before undo checkout. {} ", e.getMessage());
869             }
870         }
871         return result;
872     }
873
874     private StorageOperationStatus updatePreviousVersion(GraphVertex toscaElementVertex, GraphVertex ownerVertex) {
875         StorageOperationStatus result = null;
876         String ownerId = (String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID);
877         String toscaElementId = toscaElementVertex.getUniqueId();
878         if (!toscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE).equals(LifecycleStateEnum.CERTIFIED.name())) {
879             toscaElementVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, false);
880             Either<GraphVertex, JanusGraphOperationStatus> updateVertexRes = janusGraphDao.updateVertex(toscaElementVertex);
881             if (updateVertexRes.isRight()) {
882                 JanusGraphOperationStatus titatStatus = updateVertexRes.right().value();
883                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update tosca element vertex {}. Status is  {}", toscaElementVertex.getUniqueId(), titatStatus);
884                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(titatStatus);
885             }
886             Either<Edge, JanusGraphOperationStatus> deleteEdgeRes = null;
887             if (result == null) {
888                 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to replace edge with label {} to label {} from {} to {}. ", EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE, ownerId, toscaElementId);
889
890                 deleteEdgeRes = janusGraphDao
891                     .deleteEdge(ownerVertex, toscaElementVertex, EdgeLabelEnum.STATE);
892                 if (deleteEdgeRes.isRight()) {
893                     JanusGraphOperationStatus janusGraphStatus = deleteEdgeRes.right().value();
894                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete edge with label {} from {} to {}. Status is {} ", EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE, ownerId, toscaElementId, janusGraphStatus);
895                     if (!janusGraphStatus.equals(JanusGraphOperationStatus.INVALID_ID)) {
896                         result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(janusGraphStatus);
897                     } else {
898                         result = StorageOperationStatus.INCONSISTENCY;
899                     }
900                 }
901             }
902             if (result == null) {
903                 JanusGraphOperationStatus
904                     createEdgeRes = janusGraphDao
905                     .createEdge(ownerVertex.getVertex(), toscaElementVertex.getVertex(), EdgeLabelEnum.LAST_STATE, deleteEdgeRes.left().value());
906                 if (createEdgeRes != JanusGraphOperationStatus.OK) {
907                     result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes);
908                 }
909             }
910         }
911         if (result == null) {
912             result = StorageOperationStatus.OK;
913         }
914         return result;
915     }
916
917     private Either<ToscaElement, StorageOperationStatus> cloneToscaElementForCheckout(GraphVertex toscaElementVertex, GraphVertex modifierVertex) {
918
919         Either<ToscaElement, StorageOperationStatus> result = null;
920         Either<GraphVertex, StorageOperationStatus> cloneResult = null;
921         ToscaElementOperation operation = getToscaElementOperation(toscaElementVertex.getLabel());
922         // check if component with the next version doesn't exist.
923         Iterator<Edge> nextVersionComponentIter = toscaElementVertex.getVertex().edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
924         if (nextVersionComponentIter != null && nextVersionComponentIter.hasNext()) {
925             Vertex nextVersionVertex = nextVersionComponentIter.next().inVertex();
926             String fetchedVersion = (String) nextVersionVertex.property(GraphPropertyEnum.VERSION.getProperty()).value();
927             String fetchedName = (String) nextVersionVertex.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
928             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. ",
929                     toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME).toString(), toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION).toString(), fetchedName, fetchedVersion);
930             result = Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS);
931         }
932         if (result == null) {
933             toscaElementVertex.getOrSetDefaultInstantiationTypeForToscaElementJson();
934             cloneResult = operation.cloneToscaElement(toscaElementVertex, cloneGraphVertexForCheckout(toscaElementVertex, modifierVertex), modifierVertex);
935             if (cloneResult.isRight()) {
936                 result = Either.right(cloneResult.right().value());
937             }
938         }
939         GraphVertex clonedVertex = null;
940         if (result == null) {
941             clonedVertex = cloneResult.left().value();
942             JanusGraphOperationStatus
943                 status = janusGraphDao
944                 .createEdge(toscaElementVertex.getVertex(), cloneResult.left().value().getVertex(), EdgeLabelEnum.VERSION, new HashMap<>());
945             if (status != JanusGraphOperationStatus.OK) {
946                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge with label {} from vertex {} to tosca element vertex {} on graph. Status is {}. ", EdgeLabelEnum.VERSION,
947                         toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), cloneResult.left().value().getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
948                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
949             }
950         }
951         if (result == null) {
952             Boolean isHighest = (Boolean) toscaElementVertex.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
953             GraphVertex prevVersionInCatalog = (isHighest != null && isHighest) ? null : toscaElementVertex;
954             StorageOperationStatus updateCatalogRes = updateEdgeToCatalogRoot(clonedVertex, prevVersionInCatalog);
955             if (updateCatalogRes != StorageOperationStatus.OK) {
956                 return Either.right(updateCatalogRes);
957             }
958             result = operation.getToscaElement(cloneResult.left().value().getUniqueId());
959             if (result.isRight()) {
960                 return result;
961             }
962             ToscaElement toscaElement = result.left().value();
963             if (toscaElement.getToscaType() == ToscaElementTypeEnum.TOPOLOGY_TEMPLATE) {
964                 result = handleFixTopologyTemplate(toscaElementVertex, result, operation, clonedVertex, toscaElement);
965             }
966         }
967
968         return result;
969     }
970
971     private Either<ToscaElement, StorageOperationStatus> handleFixTopologyTemplate(GraphVertex toscaElementVertex, Either<ToscaElement, StorageOperationStatus> result, ToscaElementOperation operation, GraphVertex clonedVertex,
972             ToscaElement toscaElement) {
973         TopologyTemplate topologyTemplate = (TopologyTemplate) toscaElement;
974         Map<String, MapPropertiesDataDefinition> instInputs = topologyTemplate.getInstInputs();
975         Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
976         Map<String, MapArtifactDataDefinition> instArtifactsMap = topologyTemplate.getInstanceArtifacts();
977         Map<String, ToscaElement> origCompMap = new HashMap<>();
978         if (instInputs == null) {
979             instInputs = new HashMap<>();
980         }
981         if (instGroups == null) {
982             instGroups = new HashMap<>();
983         }
984         if (instArtifactsMap == null) {
985             instArtifactsMap = new HashMap<>();
986         }
987         Map<String, ComponentInstanceDataDefinition> instancesMap = topologyTemplate.getComponentInstances();
988         boolean isAddInstGroup = instGroups == null || instGroups.isEmpty();
989         boolean needUpdateComposition = false;
990
991         if (instancesMap != null && !instancesMap.isEmpty()) {
992             for (ComponentInstanceDataDefinition vfInst : instancesMap.values()) {
993                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "vfInst name is {} . OriginType {}. ", vfInst.getName(), vfInst.getOriginType());
994                 if (vfInst.getOriginType().name().equals(OriginTypeEnum.VF.name())) {
995                     collectInstanceInputAndGroups(instInputs, instGroups, instArtifactsMap, origCompMap, isAddInstGroup, vfInst, clonedVertex);
996                 }
997                 needUpdateComposition = needUpdateComposition || fixToscaComponentName(vfInst, origCompMap);
998                 if (needUpdateComposition) {
999                     instancesMap.put(vfInst.getUniqueId(), vfInst);
1000                 }
1001             }
1002             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before add to graph instInputs {}  instGroups {} needUpdateComposition {}", instInputs, instGroups, needUpdateComposition);
1003             if (!instInputs.isEmpty()) {
1004                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before add inst inputs {} ", instInputs == null ? 0 : instInputs.size());
1005                 GraphVertex toscaDataVertex = null;
1006                 Either<GraphVertex, JanusGraphOperationStatus> instInpVertexEither = janusGraphDao
1007                     .getChildVertex(toscaElementVertex, EdgeLabelEnum.INST_INPUTS, JsonParseFlagEnum.ParseJson);
1008                 if (instInpVertexEither.isLeft()) {
1009                     toscaDataVertex = instInpVertexEither.left().value();
1010                 }
1011
1012                 StorageOperationStatus status = handleToscaData(clonedVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, toscaDataVertex, instInputs);
1013                 if (status != StorageOperationStatus.OK) {
1014                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update instance inputs . Status is {}. ", status);
1015                     result = Either.right(status);
1016                     return result;
1017                 }
1018
1019             }
1020             if (!instGroups.isEmpty()) {
1021                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before add inst groups {} ", instGroups == null ? 0 : instGroups.size());
1022                 GraphVertex toscaDataVertex = null;
1023                 Either<GraphVertex, JanusGraphOperationStatus> instGrVertexEither = janusGraphDao
1024                     .getChildVertex(toscaElementVertex, EdgeLabelEnum.INST_GROUPS, JsonParseFlagEnum.ParseJson);
1025                 if (instGrVertexEither.isLeft()) {
1026                     toscaDataVertex = instGrVertexEither.left().value();
1027                 }
1028
1029                 StorageOperationStatus status = handleToscaData(clonedVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, toscaDataVertex, instGroups);
1030                 if (status != StorageOperationStatus.OK) {
1031                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update instance group . Status is {}. ", status);
1032                     result = Either.right(status);
1033                     return result;
1034                 }
1035
1036             }
1037             if (needUpdateComposition) {
1038                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before update Instances ");
1039                 Map<String, CompositionDataDefinition> jsonComposition = (Map<String, CompositionDataDefinition>) clonedVertex.getJson();
1040                 CompositionDataDefinition compositionDataDefinition = jsonComposition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1041                 compositionDataDefinition.setComponentInstances(instancesMap);
1042                 Either<GraphVertex, JanusGraphOperationStatus> updateElement = janusGraphDao.updateVertex(clonedVertex);
1043                 if (updateElement.isRight()) {
1044                     JanusGraphOperationStatus status = updateElement.right().value();
1045                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update instances on metadata vertex . Status is {}. ", status);
1046                     result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1047                     return result;
1048                 }
1049             }
1050
1051             result = operation.getToscaElement(clonedVertex.getUniqueId());
1052
1053         } else {
1054             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "RI map empty on component {}", toscaElement.getUniqueId());
1055         }
1056         return result;
1057     }
1058
1059     // TODO remove after jsonModelMigration
1060     public boolean resolveToscaComponentName(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap) {
1061         return fixToscaComponentName(vfInst, origCompMap);
1062     }
1063
1064     private boolean fixToscaComponentName(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap) {
1065         if (vfInst.getToscaComponentName() == null || vfInst.getToscaComponentName().isEmpty()) {
1066             String ciUid = vfInst.getUniqueId();
1067             String origCompUid = vfInst.getComponentUid();
1068             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "fixToscaComponentName:: Ri id {} . origin component id is {}. type is{} ", ciUid, origCompUid, vfInst.getOriginType());
1069             ToscaElement origComp = null;
1070             if (!origCompMap.containsKey(origCompUid)) {
1071                 Either<ToscaElement, StorageOperationStatus> origCompEither;
1072                 if (vfInst.getOriginType() == null || vfInst.getOriginType().name().equals(OriginTypeEnum.VF.name())) {
1073                     origCompEither = topologyTemplateOperation.getToscaElement(origCompUid);
1074                 } else {
1075                     origCompEither = nodeTypeOperation.getToscaElement(origCompUid);
1076                 }
1077                 if (origCompEither.isRight()) {
1078                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find orig component {} . Status is {}. ", origCompEither.right().value());
1079                     return false;
1080                 }
1081                 origComp = origCompEither.left().value();
1082                 origCompMap.put(origCompUid, origComp);
1083             } else {
1084                 origComp = origCompMap.get(origCompUid);
1085             }
1086             String toscaName = (String) origComp.getMetadataValue(JsonPresentationFields.TOSCA_RESOURCE_NAME);
1087             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Origin component id is {}. toscaName {}", origCompUid, toscaName);
1088             vfInst.setToscaComponentName(toscaName);
1089             return true;
1090         }
1091         return false;
1092     }
1093
1094     private void collectInstanceInputAndGroups(Map<String, MapPropertiesDataDefinition> instInputs, Map<String, MapGroupsDataDefinition> instGroups, Map<String, MapArtifactDataDefinition> instArtifactsMap, Map<String, ToscaElement> origCompMap,
1095             boolean isAddInstGroup, ComponentInstanceDataDefinition vfInst, GraphVertex clonedVertex) {
1096         String ciUid = vfInst.getUniqueId();
1097         String origCompUid = vfInst.getComponentUid();
1098         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "collectInstanceInputAndGroups:: Ri id {} . origin component id is {}. ", ciUid, origCompUid);
1099         TopologyTemplate origComp = null;
1100         if (!origCompMap.containsKey(origCompUid)) {
1101             Either<ToscaElement, StorageOperationStatus> origCompEither = topologyTemplateOperation.getToscaElement(origCompUid);
1102             if (origCompEither.isRight()) {
1103                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find orig component {} . Status is {}. ", origCompEither.right().value());
1104                 return;
1105             }
1106             origComp = (TopologyTemplate) origCompEither.left().value();
1107             origCompMap.put(origCompUid, origComp);
1108         } else {
1109             origComp = (TopologyTemplate) origCompMap.get(origCompUid);
1110         }
1111         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Orig component {}. ", origComp.getUniqueId());
1112
1113         Map<String, PropertyDataDefinition> origInputs = origComp.getInputs();
1114         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Orig component inputs size {}. ", origInputs == null ? 0 : origInputs.size());
1115         if (origInputs != null) {
1116             if (!instInputs.containsKey(ciUid)) {
1117                 MapPropertiesDataDefinition instProperties = new MapPropertiesDataDefinition(origInputs);
1118                 instInputs.put(ciUid, instProperties);
1119             } else {
1120
1121                 MapPropertiesDataDefinition instInputMap = instInputs.get(ciUid);
1122                 Map<String, PropertyDataDefinition> instProp = instInputMap.getMapToscaDataDefinition();
1123                 origInputs.forEach((propName, propMap) -> {
1124                     if (!instProp.containsKey(propName)) {
1125                         instProp.put(propName, propMap);
1126                     }
1127                 });
1128             }
1129             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "ComponentInstanseInputs {}. ", instInputs.get(ciUid));
1130         }
1131
1132         if (isAddInstGroup) {
1133             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before create group instance. ");
1134             List<GroupDataDefinition> filteredGroups = null;
1135
1136             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check vf groups before filter. Size is {} ", filteredGroups == null ? 0 : filteredGroups.size());
1137             if (origComp.getGroups() != null && !origComp.getGroups().isEmpty()) {
1138                 filteredGroups = origComp.getGroups().values().stream().filter(g -> g.getType().equals(VF_MODULE)).collect(Collectors.toList());
1139                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check vf groups . Size is {} ", filteredGroups == null ? 0 : filteredGroups.size());
1140             }
1141             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check vf groups after filter. Size is {} ", filteredGroups == null ? 0 : filteredGroups.size());
1142             if (CollectionUtils.isNotEmpty(filteredGroups)) {
1143                 MapArtifactDataDefinition instArifacts = null;
1144                 if (!instArtifactsMap.containsKey(ciUid)) {
1145
1146                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "istance artifacts not found ");
1147
1148                     Map<String, ArtifactDataDefinition> deploymentArtifacts = origComp.getDeploymentArtifacts();
1149
1150                     instArifacts = new MapArtifactDataDefinition(deploymentArtifacts);
1151                     addToscaDataDeepElementsBlockToToscaElement(clonedVertex, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, instArifacts, ciUid);
1152
1153                     instArtifactsMap.put(ciUid, instArifacts);
1154
1155                 } else {
1156                     instArifacts = instArtifactsMap.get(ciUid);
1157                 }
1158
1159                 if (instArifacts != null) {
1160                     Map<String, ArtifactDataDefinition> instDeplArtifMap = instArifacts.getMapToscaDataDefinition();
1161
1162                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check group dep artifacts. Size is {} ", instDeplArtifMap == null ? 0 : instDeplArtifMap.values().size());
1163                     Map<String, GroupInstanceDataDefinition> groupInstanceToCreate = new HashMap<>();
1164                     for (GroupDataDefinition group : filteredGroups) {
1165                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "create new groupInstance  {} ", group.getName());
1166                         GroupInstanceDataDefinition groupInstance = buildGroupInstanceDataDefinition(group, vfInst, instDeplArtifMap);
1167                         List<String> artifactsUid = new ArrayList<>();
1168                         List<String> artifactsId = new ArrayList<>();
1169                         if (instDeplArtifMap!=null) {
1170                                 for (ArtifactDataDefinition artifact : instDeplArtifMap.values()) {
1171                                     Optional<String> op = group.getArtifacts().stream().filter(p -> p.equals(artifact.getGeneratedFromId())).findAny();
1172                                     if (op.isPresent()) {
1173                                         artifactsUid.add(artifact.getArtifactUUID());
1174                                         artifactsId.add(artifact.getUniqueId());
1175         
1176                                     }
1177                                 }
1178                         }
1179                         groupInstance.setGroupInstanceArtifacts(artifactsId);
1180                         groupInstance.setGroupInstanceArtifactsUuid(artifactsUid);
1181                         groupInstanceToCreate.put(groupInstance.getName(), groupInstance);
1182                     }
1183                     if (MapUtils.isNotEmpty(groupInstanceToCreate)) {
1184                         instGroups.put(vfInst.getUniqueId(), new MapGroupsDataDefinition(groupInstanceToCreate));
1185
1186                     }
1187                 }
1188             }
1189         }
1190     }
1191
1192     private GraphVertex cloneGraphVertexForCheckout(GraphVertex toscaElementVertex, GraphVertex modifierVertex) {
1193         GraphVertex nextVersionToscaElementVertex = new GraphVertex();
1194         String uniqueId = UniqueIdBuilder.buildComponentUniqueId();
1195         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>(toscaElementVertex.getMetadataProperties());
1196         nextVersionToscaElementVertex.setMetadataProperties(metadataProperties);
1197         nextVersionToscaElementVertex.setUniqueId(uniqueId);
1198         nextVersionToscaElementVertex.setLabel(toscaElementVertex.getLabel());
1199         nextVersionToscaElementVertex.setType(toscaElementVertex.getType());
1200
1201         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
1202         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, nextVersionToscaElementVertex.getType().name());
1203         String nextVersion = getNextVersion((String) toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION));
1204         if (isFirstCheckoutAfterCertification(nextVersion)) {
1205             nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.UUID, IdBuilderUtils.generateUUID());
1206         }
1207         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.VERSION, nextVersion);
1208         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
1209         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1210
1211         if (toscaElementVertex.getType() == ComponentTypeEnum.SERVICE && toscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE).equals(LifecycleStateEnum.CERTIFIED.name())) {
1212             nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
1213         }
1214         if (!MapUtils.isEmpty(toscaElementVertex.getMetadataJson())) {
1215             nextVersionToscaElementVertex.setMetadataJson(new HashMap<>(toscaElementVertex.getMetadataJson()));
1216             nextVersionToscaElementVertex.updateMetadataJsonWithCurrentMetadataProperties();
1217         }
1218         long currTime = System.currentTimeMillis();
1219         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.CREATION_DATE, currTime);
1220         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, currTime);
1221         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_CREATOR, modifierVertex.getUniqueId());
1222         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_LAST_UPDATER, modifierVertex.getUniqueId());
1223         if (toscaElementVertex.getType() == ComponentTypeEnum.SERVICE) {
1224             nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.CONFORMANCE_LEVEL, ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel());
1225         }
1226
1227         if (!MapUtils.isEmpty(toscaElementVertex.getJson())) {
1228             nextVersionToscaElementVertex.setJson(new HashMap<String, ToscaDataDefinition>(toscaElementVertex.getJson()));
1229         }
1230         return nextVersionToscaElementVertex;
1231     }
1232
1233     private Either<GraphVertex, StorageOperationStatus> cloneToscaElementForCertify(GraphVertex toscaElementVertex, GraphVertex modifierVertex, Integer majorVersion) {
1234         Either<GraphVertex, StorageOperationStatus> result;
1235         Either<List<GraphVertex>, StorageOperationStatus> deleteResult = null;
1236         GraphVertex clonedToscaElement = null;
1237         result = getToscaElementOperation(toscaElementVertex.getLabel()).cloneToscaElement(toscaElementVertex, cloneGraphVertexForCertify(toscaElementVertex, modifierVertex, majorVersion), modifierVertex);
1238         if (result.isRight()) {
1239             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element {} for certification. Sattus is {}. ", toscaElementVertex.getUniqueId(), result.right().value());
1240         } else {
1241             clonedToscaElement = result.left().value();
1242             StorageOperationStatus updateEdgeToCatalog = updateEdgeToCatalogRoot(clonedToscaElement, toscaElementVertex);
1243             if (updateEdgeToCatalog != StorageOperationStatus.OK) {
1244                 return Either.right(updateEdgeToCatalog);
1245             }
1246             deleteResult = deleteAllPreviousNotCertifiedVersions(toscaElementVertex);
1247             if (deleteResult.isRight()) {
1248                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete all previous npt certified versions of tosca element {}. Status is {}. ", toscaElementVertex.getUniqueId(), deleteResult.right().value());
1249                 result = Either.right(deleteResult.right().value());
1250             }
1251         }
1252         if (result.isLeft()) {
1253             result = handlePreviousVersionRelation(clonedToscaElement, deleteResult.left().value(), majorVersion);
1254         }
1255         return result;
1256     }
1257
1258     private Either<GraphVertex, StorageOperationStatus> handlePreviousVersionRelation(GraphVertex clonedToscaElement, List<GraphVertex> deletedVersions, Integer majorVersion) {
1259         Either<GraphVertex, StorageOperationStatus> result = null;
1260         Vertex previousCertifiedToscaElement = null;
1261         if (majorVersion > 0) {
1262             List<GraphVertex> firstMinorVersionVertex = deletedVersions.stream().filter(gv -> getMinorVersion((String) gv.getMetadataProperty(GraphPropertyEnum.VERSION)) == 1).collect(Collectors.toList());
1263
1264             if (CollectionUtils.isEmpty(firstMinorVersionVertex)) {
1265                 result = Either.right(StorageOperationStatus.NOT_FOUND);
1266             } else {
1267                 previousCertifiedToscaElement = getPreviousCertifiedToscaElement(firstMinorVersionVertex.get(0));
1268                 if (previousCertifiedToscaElement == null) {
1269                     result = Either.right(StorageOperationStatus.NOT_FOUND);
1270                 }
1271             }
1272             if (result == null) {
1273                 JanusGraphOperationStatus
1274                     status = janusGraphDao
1275                     .createEdge(previousCertifiedToscaElement, clonedToscaElement.getVertex(), EdgeLabelEnum.VERSION, new HashMap<>());
1276                 if (status != JanusGraphOperationStatus.OK) {
1277                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge with label {} from vertex {} to tosca element vertex {} on graph. Status is {}. ", EdgeLabelEnum.VERSION,
1278                             previousCertifiedToscaElement.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), clonedToscaElement.getUniqueId(), status);
1279                     result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1280                 }
1281             }
1282
1283         }
1284         if (result == null) {
1285             result = Either.left(clonedToscaElement);
1286         }
1287         return result;
1288     }
1289
1290     private Vertex getPreviousCertifiedToscaElement(GraphVertex graphVertex) {
1291
1292         Iterator<Edge> edges = graphVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.VERSION.name());
1293         if (edges.hasNext()) {
1294             return edges.next().outVertex();
1295         }
1296         return null;
1297     }
1298
1299     private Either<List<GraphVertex>, StorageOperationStatus> deleteAllPreviousNotCertifiedVersions(GraphVertex toscaElementVertex) {
1300         Either<List<GraphVertex>, StorageOperationStatus> result = null;
1301
1302         ToscaElementOperation operation = getToscaElementOperation(toscaElementVertex.getLabel());
1303         List<GraphVertex> previosVersions = null;
1304         Object uuid = toscaElementVertex.getMetadataProperty(GraphPropertyEnum.UUID);
1305         Object componentName = toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NAME);
1306         try {
1307             Map<GraphPropertyEnum, Object> properties = new HashMap<>();
1308             properties.put(GraphPropertyEnum.UUID, uuid);
1309             properties.put(GraphPropertyEnum.NAME, componentName);
1310             Either<List<GraphVertex>, JanusGraphOperationStatus> getToscaElementsRes = janusGraphDao
1311                 .getByCriteria(toscaElementVertex.getLabel(), properties, JsonParseFlagEnum.ParseMetadata);
1312             if (getToscaElementsRes.isRight()) {
1313                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementsRes.right().value()));
1314             }
1315             if (result == null) {
1316                 previosVersions = getToscaElementsRes.left().value();
1317                 Either<Boolean, StorageOperationStatus> deleteResult = markToscaElementsAsDeleted(operation, getToscaElementsRes.left().value());
1318                 if (deleteResult.isRight()) {
1319                     result = Either.right(deleteResult.right().value());
1320                 }
1321             }
1322             if (result == null) {
1323                 result = Either.left(previosVersions);
1324             }
1325         } catch (Exception e) {
1326             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during deleteng all tosca elements by UUID {} and name {}. {} ", uuid, componentName, e.getMessage());
1327         }
1328         return result;
1329     }
1330
1331     private GraphVertex cloneGraphVertexForCertify(GraphVertex toscaElementVertex, GraphVertex modifierVertex, Integer majorVersion) {
1332
1333         GraphVertex nextVersionToscaElementVertex = new GraphVertex();
1334         String uniqueId = IdBuilderUtils.generateUniqueId();
1335         Map<GraphPropertyEnum, Object> metadataProperties = new EnumMap<>(toscaElementVertex.getMetadataProperties());
1336         nextVersionToscaElementVertex.setMetadataProperties(metadataProperties);
1337         nextVersionToscaElementVertex.setUniqueId(uniqueId);
1338         nextVersionToscaElementVertex.setLabel(toscaElementVertex.getLabel());
1339         nextVersionToscaElementVertex.setType(toscaElementVertex.getType());
1340
1341         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
1342         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, nextVersionToscaElementVertex.getType().name());
1343         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.VERSION, (majorVersion + 1) + VERSION_DELIMITER + "0");
1344         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1345         nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1346         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.CREATION_DATE, System.currentTimeMillis());
1347         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, null);
1348         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_CREATOR, modifierVertex.getUniqueId());
1349         nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_LAST_UPDATER, modifierVertex.getUniqueId());
1350
1351         if (toscaElementVertex.getType() == ComponentTypeEnum.SERVICE && toscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE).equals(LifecycleStateEnum.CERTIFIED)) {
1352             nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
1353         }
1354         if (!MapUtils.isEmpty(toscaElementVertex.getMetadataJson())) {
1355             nextVersionToscaElementVertex.setMetadataJson(new HashMap<>(toscaElementVertex.getMetadataJson()));
1356             nextVersionToscaElementVertex.updateMetadataJsonWithCurrentMetadataProperties();
1357         }
1358         if (!MapUtils.isEmpty(toscaElementVertex.getJson())) {
1359             nextVersionToscaElementVertex.setJson(new HashMap<String, ToscaDataDefinition>(toscaElementVertex.getJson()));
1360         }
1361         return nextVersionToscaElementVertex;
1362     }
1363
1364
1365     private Either<GraphVertex, StorageOperationStatus> checkinToscaELement(LifecycleStateEnum currState, GraphVertex toscaElementVertex, GraphVertex ownerVertex, GraphVertex modifierVertex, LifecycleStateEnum nextState) {
1366         Either<GraphVertex, StorageOperationStatus> updateRelationsRes;
1367         Either<GraphVertex, StorageOperationStatus> result = changeStateToCheckedIn(currState, toscaElementVertex, ownerVertex, modifierVertex);
1368         if (result.isLeft()) {
1369             toscaElementVertex.addMetadataProperty(GraphPropertyEnum.STATE, nextState.name());
1370             toscaElementVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
1371             result = updateToscaElementVertexMetadataPropertiesAndJson(toscaElementVertex);
1372         }
1373         if (result.isLeft()) {
1374             updateRelationsRes = updateLastModifierEdge(toscaElementVertex, ownerVertex, modifierVertex);
1375             if (updateRelationsRes.isRight()) {
1376                 result = Either.right(updateRelationsRes.right().value());
1377             }
1378         }
1379         return result;
1380     }
1381
1382     private Either<GraphVertex, StorageOperationStatus> updateToscaElementVertexMetadataPropertiesAndJson(GraphVertex toscaElementVertex) {
1383
1384         Either<GraphVertex, StorageOperationStatus> result;
1385
1386         Either<GraphVertex, JanusGraphOperationStatus> updateVertexRes = janusGraphDao.updateVertex(toscaElementVertex);
1387         if (updateVertexRes.isRight()) {
1388             JanusGraphOperationStatus titatStatus = updateVertexRes.right().value();
1389             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update state of tosca element vertex {} metadata. Status is  {}", toscaElementVertex.getUniqueId(), titatStatus);
1390             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(titatStatus));
1391         } else {
1392             result = Either.left(updateVertexRes.left().value());
1393         }
1394         return result;
1395     }
1396
1397     private Either<GraphVertex, StorageOperationStatus> changeStateToCheckedIn(LifecycleStateEnum currState, GraphVertex toscaElementVertex, GraphVertex ownerVertex, GraphVertex modifierVertex) {
1398         Either<GraphVertex, StorageOperationStatus> result = null;
1399         LifecycleStateEnum nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
1400         String faileToUpdateStateMsg = "Failed to update state of tosca element {}. Status is  {}";
1401
1402         if (currState == LifecycleStateEnum.READY_FOR_CERTIFICATION) {
1403             // In case of cancel "ready for certification" remove last state edge with "STATE" property equals to "NOT_CERTIFIED_CHECKIN"
1404             Map<GraphPropertyEnum, Object> vertexProperties = new EnumMap<>(GraphPropertyEnum.class);
1405             vertexProperties.put(GraphPropertyEnum.STATE, nextState);
1406             Either<Edge, JanusGraphOperationStatus> deleteResult = janusGraphDao
1407                 .deleteBelongingEdgeByCriteria(toscaElementVertex, EdgeLabelEnum.LAST_STATE, vertexProperties);
1408             if (deleteResult.isRight()) {
1409                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, faileToUpdateStateMsg, toscaElementVertex.getUniqueId(), deleteResult.right().value());
1410                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "failed to update last state relation");
1411                 result = Either.right(StorageOperationStatus.INCONSISTENCY);
1412             }
1413         }
1414         if (result == null) {
1415             // Remove CHECKOUT relation
1416             Either<Edge, JanusGraphOperationStatus> deleteEdgeResult = janusGraphDao
1417                 .deleteEdge(ownerVertex, toscaElementVertex, EdgeLabelEnum.STATE);
1418             if (deleteEdgeResult.isRight()) {
1419                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, faileToUpdateStateMsg, toscaElementVertex.getUniqueId());
1420                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteEdgeResult.right().value()));
1421             }
1422         }
1423         if (result == null) {
1424             // Create CHECKIN relation
1425             Map<EdgePropertyEnum, Object> edgeProperties = new EnumMap<>(EdgePropertyEnum.class);
1426             edgeProperties.put(EdgePropertyEnum.STATE, nextState);
1427             JanusGraphOperationStatus
1428                 createEdgeRes = janusGraphDao
1429                 .createEdge(modifierVertex.getVertex(), toscaElementVertex.getVertex(), EdgeLabelEnum.STATE, edgeProperties);
1430             if (createEdgeRes != JanusGraphOperationStatus.OK) {
1431                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, faileToUpdateStateMsg, toscaElementVertex.getUniqueId());
1432                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes));
1433             }
1434         }
1435         if (result == null) {
1436             result = Either.left(toscaElementVertex);
1437         }
1438         return result;
1439     }
1440
1441     private Either<GraphVertex, StorageOperationStatus> updateLastModifierEdge(GraphVertex toscaElementVertex, GraphVertex ownerVertex, GraphVertex modifierVertex) {
1442         Either<GraphVertex, StorageOperationStatus> result = null;
1443         if (!modifierVertex.getMetadataProperties().get(GraphPropertyEnum.USERID).equals(ownerVertex.getMetadataProperties().get(GraphPropertyEnum.USERID))) {
1444             Either<Edge, JanusGraphOperationStatus> deleteEdgeRes = janusGraphDao
1445                 .deleteEdge(ownerVertex, toscaElementVertex, EdgeLabelEnum.LAST_MODIFIER);
1446             if (deleteEdgeRes.isRight()) {
1447                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete last modifier {} to tosca element {}. Edge type is {}", ownerVertex.getUniqueId(), ownerVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
1448                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteEdgeRes.right().value()));
1449             }
1450             if (result == null) {
1451                 JanusGraphOperationStatus createEdgeRes = janusGraphDao
1452                     .createEdge(modifierVertex.getVertex(), toscaElementVertex.getVertex(), EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
1453
1454                 if (createEdgeRes != JanusGraphOperationStatus.OK) {
1455                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to associate user {} to component {}. Edge type is {}", modifierVertex.getUniqueId(), ownerVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
1456                     result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes));
1457                 } else {
1458                     result = Either.left(modifierVertex);
1459                 }
1460             }
1461         } else {
1462             result = Either.left(ownerVertex);
1463         }
1464         return result;
1465     }
1466
1467     private Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> prepareParametersToGetVerticesForCheckin(String toscaElementId, String modifierId, String ownerId) {
1468         Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGetParameters = new HashMap<>();
1469         verticesToGetParameters.put(toscaElementId, new ImmutablePair<>(GraphPropertyEnum.UNIQUE_ID, JsonParseFlagEnum.ParseMetadata));
1470         verticesToGetParameters.put(modifierId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1471         verticesToGetParameters.put(ownerId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1472         return verticesToGetParameters;
1473     }
1474
1475     private Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> prepareParametersToGetVerticesForRequestCertification(String toscaElementId, String modifierId, String ownerId) {
1476         Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGetParameters = new HashMap<>();
1477         verticesToGetParameters.put(toscaElementId, new ImmutablePair<>(GraphPropertyEnum.UNIQUE_ID, JsonParseFlagEnum.ParseAll));
1478         verticesToGetParameters.put(modifierId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1479         verticesToGetParameters.put(ownerId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1480         return verticesToGetParameters;
1481     }
1482
1483     private Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> prepareParametersToGetVerticesForCheckout(String toscaElementId, String modifierId, String ownerId) {
1484         //Implementation is currently identical
1485         return prepareParametersToGetVerticesForRequestCertification(toscaElementId,modifierId, ownerId);
1486     }
1487
1488     private String getNextCertifiedVersion(String version) {
1489         String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
1490         Integer nextMajorVersion = Integer.parseInt(versionParts[0]) + 1;
1491         return nextMajorVersion + VERSION_DELIMITER + "0";
1492     }
1493
1494     private String getNextVersion(String currVersion) {
1495         String[] versionParts = currVersion.split(VERSION_DELIMITER_REGEXP);
1496         Integer minorVersion = Integer.parseInt(versionParts[1]) + 1;
1497         return versionParts[0] + VERSION_DELIMITER + minorVersion;
1498     }
1499
1500     private Integer getMinorVersion(String version) {
1501         String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
1502         return Integer.parseInt(versionParts[1]);
1503     }
1504
1505     private Integer getMajorVersion(String version) {
1506         String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
1507         return Integer.parseInt(versionParts[0]);
1508     }
1509
1510     private boolean isFirstCheckoutAfterCertification(String version) {
1511         return (Integer.parseInt(version.split(VERSION_DELIMITER_REGEXP)[0]) != 0 && Integer.parseInt(version.split(VERSION_DELIMITER_REGEXP)[1]) == 1);
1512     }
1513
1514     public Either<ToscaElement, StorageOperationStatus> forceCerificationOfToscaElement(String toscaElementId, String modifierId, String ownerId, String currVersion) {
1515         Either<GraphVertex, StorageOperationStatus> resultUpdate = null;
1516         Either<ToscaElement, StorageOperationStatus> result = null;
1517         GraphVertex toscaElement = null;
1518         GraphVertex modifier = null;
1519         GraphVertex owner;
1520         try {
1521             Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
1522                 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
1523             if (getVerticesRes.isRight()) {
1524                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
1525                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
1526             }
1527             if (result == null) {
1528                 toscaElement = getVerticesRes.left().value().get(toscaElementId);
1529                 modifier = getVerticesRes.left().value().get(modifierId);
1530                 owner = getVerticesRes.left().value().get(ownerId);
1531
1532                 StorageOperationStatus status = handleRelationsUponForceCertification(toscaElement, modifier, owner);
1533                 if (status != StorageOperationStatus.OK) {
1534                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations on certification request for tosca element {}. Status is {}. ", toscaElement.getUniqueId(), status);
1535                 }
1536             }
1537             if (result == null) {
1538                 LifecycleStateEnum nextState = LifecycleStateEnum.CERTIFIED;
1539
1540                 toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1541                 toscaElement.addMetadataProperty(GraphPropertyEnum.VERSION, getNextCertifiedVersion(currVersion));
1542
1543                 resultUpdate = updateToscaElementVertexMetadataPropertiesAndJson(toscaElement);
1544                 if (resultUpdate.isRight()) {
1545                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to set lifecycle for tosca elememt {} to state {}, error: {}", toscaElement.getUniqueId(), nextState, resultUpdate.right().value());
1546                     result = Either.right(resultUpdate.right().value());
1547                 }
1548             }
1549             if (result == null) {
1550                 ToscaElementOperation operation = getToscaElementOperation(toscaElement.getLabel());
1551                 result = operation.getToscaElement(toscaElement.getUniqueId());
1552             }
1553             return result;
1554
1555         } catch (Exception e) {
1556             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during request certification tosca element {}. {}", toscaElementId, e.getMessage());
1557         }
1558         return result;
1559     }
1560
1561     private StorageOperationStatus handleRelationsUponForceCertification(GraphVertex toscaElement, GraphVertex modifier, GraphVertex owner) {
1562
1563         StorageOperationStatus result = null;
1564         JanusGraphOperationStatus status = janusGraphDao
1565             .replaceEdgeLabel(owner.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE);
1566         if (status != JanusGraphOperationStatus.OK) {
1567             result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
1568         }
1569         if (result == null) {
1570             Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
1571             properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.CERTIFIED);
1572             status = janusGraphDao
1573                 .createEdge(modifier, toscaElement, EdgeLabelEnum.STATE, properties);
1574             if (status != JanusGraphOperationStatus.OK) {
1575                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "failed to create edge. Status is {}", status);
1576                 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
1577             }
1578         }
1579         if (result == null) {
1580             result = StorageOperationStatus.OK;
1581         }
1582         return result;
1583     }
1584
1585     private StorageOperationStatus updateEdgeToCatalogRootByUndoCheckout(JanusGraphVertex preV, GraphVertex curV) {
1586         if (preV == null) {
1587             return updateEdgeToCatalogRoot(null, curV);
1588         }
1589         String uniqueIdPreVer = (String) janusGraphDao
1590             .getProperty((JanusGraphVertex) preV, GraphPropertyEnum.UNIQUE_ID.getProperty());
1591         LifecycleStateEnum state = LifecycleStateEnum.findState((String) janusGraphDao
1592             .getProperty(preV, GraphPropertyEnum.STATE.getProperty()));
1593         if (state == LifecycleStateEnum.CERTIFIED) {
1594             return updateEdgeToCatalogRoot(null, curV);
1595         }
1596         return janusGraphDao.getVertexById(uniqueIdPreVer)
1597                 .either(l -> updateEdgeToCatalogRoot(l, curV),
1598                         DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
1599     }
1600
1601     private StorageOperationStatus updateEdgeToCatalogRoot(GraphVertex newVersionV, GraphVertex prevVersionV) {
1602         Either<GraphVertex, JanusGraphOperationStatus> catalog = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT);
1603         if (catalog.isRight()) {
1604             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch catalog vertex. error {}", catalog.right().value());
1605             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(catalog.right().value());
1606         }
1607         GraphVertex catalogV = catalog.left().value();
1608         if (newVersionV != null) {
1609             Boolean isAbstract = (Boolean) newVersionV.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
1610                         
1611                         if ( isAbstract == null || !isAbstract ) {
1612                 // no new vertex, only delete previous
1613                 JanusGraphOperationStatus
1614                     result = janusGraphDao
1615                     .createEdge(catalogV, newVersionV, EdgeLabelEnum.CATALOG_ELEMENT, null);
1616                 if (result != JanusGraphOperationStatus.OK) {
1617                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge from {} to catalog vertex. error {}", newVersionV.getUniqueId(), result);
1618                     return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
1619                 }
1620             }
1621         }
1622         if (prevVersionV != null) {
1623             Boolean isAbstract = (Boolean) prevVersionV.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
1624             if (isAbstract == null || !isAbstract) {
1625                 // if prev == null -> new resource was added
1626                 Either<Edge, JanusGraphOperationStatus> deleteResult = janusGraphDao
1627                     .deleteEdge(catalogV, prevVersionV, EdgeLabelEnum.CATALOG_ELEMENT);
1628                 if (deleteResult.isRight()) {
1629                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete edge from {} to catalog vertex. error {}", prevVersionV.getUniqueId(), deleteResult.right().value());
1630                     return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteResult.right().value());
1631                 }
1632             }
1633         }
1634         return StorageOperationStatus.OK;
1635     }
1636 }