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