2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
23 import org.janusgraph.core.JanusGraphVertex;
24 import fj.data.Either;
25 import org.apache.commons.collections.CollectionUtils;
26 import org.apache.commons.collections.MapUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.lang3.tuple.ImmutablePair;
29 import org.apache.tinkerpop.gremlin.structure.Direction;
30 import org.apache.tinkerpop.gremlin.structure.Edge;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.openecomp.sdc.be.config.ConfigurationManager;
33 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
34 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
35 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
36 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
37 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
38 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
39 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
40 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
41 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
42 import org.openecomp.sdc.be.datatypes.elements.*;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
45 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
46 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
47 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
48 import org.openecomp.sdc.be.model.DistributionStatusEnum;
49 import org.openecomp.sdc.be.model.LifecycleStateEnum;
50 import org.openecomp.sdc.be.model.User;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
52 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
54 import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum;
55 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
56 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
57 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
58 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
59 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
60 import org.openecomp.sdc.common.log.wrappers.Logger;
63 import java.util.stream.Collectors;
65 @org.springframework.stereotype.Component("tosca-element-lifecycle-operation")
68 * Allows to perform lifecycle operations: checkin, checkout, submit for testing, start certification and certification process for tosca element
70 public class ToscaElementLifecycleOperation extends BaseOperation {
72 private static final String FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS = "Failed to delete last state edge. Status is {}. ";
73 private static final String FAILED_TO_GET_VERTICES = "Failed to get vertices by id {}. Status is {}. ";
74 public static final String VERSION_DELIMITER = ".";
75 public static final String VERSION_DELIMITER_REGEXP = "\\.";
77 private static final Logger log = Logger.getLogger(ToscaElementLifecycleOperation.class);
80 * Performs changing a lifecycle state of tosca element from "checked out" or "ready for certification" to "checked in"
83 * @param toscaElementId
88 public Either<ToscaElement, StorageOperationStatus> checkinToscaELement(LifecycleStateEnum currState, String toscaElementId, String modifierId, String ownerId) {
89 Either<GraphVertex, StorageOperationStatus> updateResult = null;
90 Either<ToscaElement, StorageOperationStatus> result = null;
91 Map<String, GraphVertex> vertices = null;
92 ToscaElementOperation operation;
94 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
95 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForCheckin(toscaElementId, modifierId, ownerId));
96 if (getVerticesRes.isRight()) {
97 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
98 updateResult = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
100 vertices = getVerticesRes.left().value();
101 updateResult = checkinToscaELement(currState, vertices.get(toscaElementId), vertices.get(ownerId), vertices.get(modifierId), LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
103 if (updateResult.isLeft()) {
104 operation = getToscaElementOperation(vertices.get(toscaElementId).getLabel());
105 result = operation.getToscaElement(updateResult.left().value().getUniqueId());
106 if (result.isRight()) {
107 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated tosca element {}. Status is {}", toscaElementId, result.right().value());
110 result = Either.right(updateResult.right().value());
112 } catch (Exception e) {
113 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during checkin of tosca element {}. {} ", toscaElementId, e.getMessage());
119 * Returns vertex presenting owner of tosca element specified by uniqueId
121 * @param toscaElementId
124 public Either<User, StorageOperationStatus> getToscaElementOwner(String toscaElementId) {
125 Either<User, StorageOperationStatus> result = null;
126 GraphVertex toscaElement = null;
127 Either<GraphVertex, JanusGraphOperationStatus> getToscaElementRes = janusGraphDao
128 .getVertexById(toscaElementId, JsonParseFlagEnum.NoParse);
129 if (getToscaElementRes.isRight()) {
130 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementRes.right().value()));
132 if (result == null) {
133 toscaElement = getToscaElementRes.left().value();
134 Iterator<Vertex> vertices = toscaElement.getVertex().vertices(Direction.IN, EdgeLabelEnum.STATE.name());
135 if (vertices == null || !vertices.hasNext()) {
136 result = Either.right(StorageOperationStatus.NOT_FOUND);
138 result = Either.left(convertToUser(vertices.next()));
145 * Returns vertex presenting owner of tosca element specified by uniqueId
147 * @param toscaElement
150 public Either<User, StorageOperationStatus> getToscaElementOwner(GraphVertex toscaElement) {
151 Either<User, StorageOperationStatus> result = null;
152 Iterator<Vertex> vertices = toscaElement.getVertex().vertices(Direction.IN, EdgeLabelEnum.STATE.name());
153 if (vertices == null || !vertices.hasNext()) {
154 result = Either.right(StorageOperationStatus.NOT_FOUND);
156 result = Either.left(convertToUser(vertices.next()));
162 * Performs checkout of a tosca element
164 * @param toscaElementId
169 public Either<ToscaElement, StorageOperationStatus> checkoutToscaElement(String toscaElementId, String modifierId, String ownerId) {
170 Either<ToscaElement, StorageOperationStatus> result = null;
171 Map<String, GraphVertex> vertices = null;
173 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
174 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForCheckout(toscaElementId, modifierId, ownerId));
175 if (getVerticesRes.isRight()) {
176 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
177 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
179 if (result == null) {
180 vertices = getVerticesRes.left().value();
181 // update previous component if not certified
182 StorageOperationStatus status = updatePreviousVersion(vertices.get(toscaElementId), vertices.get(ownerId));
183 if (status != StorageOperationStatus.OK) {
184 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update vertex with id {} . Status is {}. ", status);
185 result = Either.right(status);
188 if (result == null) {
189 result = cloneToscaElementForCheckout(vertices.get(toscaElementId), vertices.get(modifierId));
190 if (result.isRight()) {
191 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to checkout tosca element {}. Status is {} ", toscaElementId, result.right().value());
195 } catch (Exception e) {
196 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during checkout tosca element {}. {}", toscaElementId, e.getMessage());
202 * Performs undo checkout for tosca element
204 * @param toscaElementId
207 public Either<ToscaElement, StorageOperationStatus> undoCheckout(String toscaElementId) {
208 Either<ToscaElement, StorageOperationStatus> result = null;
209 Either<GraphVertex, JanusGraphOperationStatus> getToscaElementRes = null;
210 Iterator<Edge> nextVersionComponentIter = null;
211 ToscaElementOperation operation;
212 Vertex preVersionVertex = null;
214 getToscaElementRes = janusGraphDao.getVertexById(toscaElementId, JsonParseFlagEnum.ParseMetadata);
215 if (getToscaElementRes.isRight()) {
216 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
217 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementRes.right().value()));
219 GraphVertex currVersionV = getToscaElementRes.left().value();
220 if (result == null && hasPreviousVersion(currVersionV)) {
221 // find previous version
222 nextVersionComponentIter = currVersionV.getVertex().edges(Direction.IN, EdgeLabelEnum.VERSION.name());
223 if (nextVersionComponentIter == null || !nextVersionComponentIter.hasNext()) {
224 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch previous version of tosca element with name {}. ", currVersionV.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME).toString());
225 result = Either.right(StorageOperationStatus.NOT_FOUND);
227 if (result == null) {
228 preVersionVertex = nextVersionComponentIter.next().outVertex();
229 StorageOperationStatus updateOldResourceResult = updateOldToscaElementBeforeUndoCheckout(preVersionVertex);
230 if (updateOldResourceResult != StorageOperationStatus.OK) {
231 result = Either.right(updateOldResourceResult);
235 if (result == null) {
236 StorageOperationStatus updateCatalogRes = updateEdgeToCatalogRootByUndoCheckout((JanusGraphVertex) preVersionVertex, currVersionV);
237 if (updateCatalogRes != StorageOperationStatus.OK) {
238 return Either.right(updateCatalogRes);
240 operation = getToscaElementOperation(currVersionV.getLabel());
241 result = operation.deleteToscaElement(currVersionV);
243 } catch (Exception e) {
244 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during undo checkout tosca element {}. {}", toscaElementId, e.getMessage());
249 private boolean hasPreviousVersion(GraphVertex toscaElementVertex) {
250 boolean hasPreviousVersion = true;
251 String version = (String) toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION);
252 if (StringUtils.isEmpty(version) || "0.1".equals(version))
253 hasPreviousVersion = false;
254 return hasPreviousVersion;
258 * Performs request certification for tosca element
260 * @param toscaElementId
265 public Either<ToscaElement, StorageOperationStatus> requestCertificationToscaElement(String toscaElementId, String modifierId, String ownerId) {
266 Either<GraphVertex, StorageOperationStatus> resultUpdate = null;
267 Either<ToscaElement, StorageOperationStatus> result = null;
268 GraphVertex toscaElement = null;
269 GraphVertex modifier = null;
272 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
273 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
274 if (getVerticesRes.isRight()) {
275 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
276 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
278 if (result == null) {
279 toscaElement = getVerticesRes.left().value().get(toscaElementId);
280 modifier = getVerticesRes.left().value().get(modifierId);
281 owner = getVerticesRes.left().value().get(ownerId);
283 StorageOperationStatus status = handleRelationsUponRequestForCertification(toscaElement, modifier, owner);
284 if (status != StorageOperationStatus.OK) {
285 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations on certification request for tosca element {}. Status is {}. ", toscaElement.getUniqueId(), status);
288 if (result == null) {
289 LifecycleStateEnum nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION;
291 toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, nextState.name());
292 toscaElement.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
294 resultUpdate = updateToscaElementVertexMetadataPropertiesAndJson(toscaElement);
295 if (resultUpdate.isRight()) {
296 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to set lifecycle for tosca elememt {} to state {}, error: {}", toscaElement.getUniqueId(), nextState, resultUpdate.right().value());
297 result = Either.right(resultUpdate.right().value());
300 if (result == null) {
301 ToscaElementOperation operation = getToscaElementOperation(toscaElement.getLabel());
302 result = operation.getToscaElement(toscaElement.getUniqueId());
306 } catch (Exception e) {
307 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during request certification tosca element {}. {}", toscaElementId, e.getMessage());
313 * Starts certification of tosca element
315 * @param toscaElementId
320 public Either<ToscaElement, StorageOperationStatus> startCertificationToscaElement(String toscaElementId, String modifierId, String ownerId) {
321 Either<ToscaElement, StorageOperationStatus> result = null;
322 Either<GraphVertex, StorageOperationStatus> resultUpdate = null;
323 GraphVertex toscaElement = null;
324 GraphVertex modifier = null;
327 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
328 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
329 if (getVerticesRes.isRight()) {
330 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
331 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
333 if (result == null) {
334 toscaElement = getVerticesRes.left().value().get(toscaElementId);
335 modifier = getVerticesRes.left().value().get(modifierId);
336 owner = getVerticesRes.left().value().get(ownerId);
338 StorageOperationStatus status = handleRelationsUponCertification(toscaElement, modifier, owner);
339 if (status != StorageOperationStatus.OK) {
340 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations during certification of tosca element {}. Status is {}. ", toscaElement.getUniqueId(), status);
343 if (result == null) {
344 LifecycleStateEnum nextState = LifecycleStateEnum.CERTIFICATION_IN_PROGRESS;
346 toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, nextState.name());
347 toscaElement.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
349 resultUpdate = updateToscaElementVertexMetadataPropertiesAndJson(toscaElement);
350 if (resultUpdate.isRight()) {
351 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Couldn't set lifecycle for component {} to state {}, error: {}", toscaElement.getUniqueId(), nextState, resultUpdate.right().value());
352 result = Either.right(resultUpdate.right().value());
355 if (result == null) {
356 ToscaElementOperation operation = getToscaElementOperation(toscaElement.getLabel());
357 result = operation.getToscaElement(toscaElement.getUniqueId());
359 } catch (Exception e) {
360 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during start certification tosca element {}. {}", toscaElementId, e.getMessage());
365 public Either<ToscaElement, StorageOperationStatus> certifyToscaElement(String toscaElementId, String modifierId, String ownerId) {
366 Either<ToscaElement, StorageOperationStatus> result = null;
367 Either<GraphVertex, StorageOperationStatus> cloneRes = null;
368 GraphVertex toscaElement = null;
369 GraphVertex modifier = null;
370 GraphVertex certifiedToscaElement = null;
371 Integer majorVersion = null;
373 StorageOperationStatus status;
375 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
376 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
377 if (getVerticesRes.isRight()) {
378 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
379 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
381 if (result == null) {
382 toscaElement = getVerticesRes.left().value().get(toscaElementId);
383 modifier = getVerticesRes.left().value().get(modifierId);
384 majorVersion = getMajorVersion((String) toscaElement.getMetadataProperty(GraphPropertyEnum.VERSION));
385 status = handleRelationsOfPreviousToscaElementBeforeCertifying(toscaElement, modifier, majorVersion);
386 if (status != StorageOperationStatus.OK) {
387 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations of previous tosca element before certifying {}. Status is {}. ", toscaElement.getUniqueId(), status);
390 if (result == null) {
391 cloneRes = cloneToscaElementForCertify(toscaElement, modifier, majorVersion);
392 if (cloneRes.isRight()) {
393 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element during certification. ");
394 result = Either.right(cloneRes.right().value());
397 if (result == null) {
398 certifiedToscaElement = cloneRes.left().value();
399 status = handleRelationsOfNewestCertifiedToscaElement(toscaElement, certifiedToscaElement);
400 if (status != StorageOperationStatus.OK) {
401 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations of newest certified tosca element {}. Status is {}. ", certifiedToscaElement.getUniqueId(), status);
404 if (result == null) {
405 return getToscaElementOperation(toscaElement.getLabel()).getToscaElement(certifiedToscaElement.getUniqueId());
407 } catch (Exception e) {
408 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during certification tosca element {}. {}", toscaElementId, e.getMessage());
414 * Deletes (marks as deleted) all tosca elements according received name and uuid
417 * @param componentType
418 * @param componentName
422 public Either<Boolean, StorageOperationStatus> deleteOldToscaElementVersions(VertexTypeEnum vertexType, ComponentTypeEnum componentType, String componentName, String uuid) {
424 Either<Boolean, StorageOperationStatus> result = null;
425 ToscaElementOperation operation = getToscaElementOperation(componentType);
428 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
429 properties.put(GraphPropertyEnum.UUID, uuid);
430 properties.put(GraphPropertyEnum.NAME, componentName);
431 Either<List<GraphVertex>, JanusGraphOperationStatus> getToscaElementsRes = janusGraphDao
432 .getByCriteria(vertexType, properties, JsonParseFlagEnum.ParseMetadata);
433 if (getToscaElementsRes.isRight()) {
434 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementsRes.right().value()));
436 if (result == null) {
437 result = markToscaElementsAsDeleted(operation, getToscaElementsRes.left().value());
439 if (result == null) {
440 result = Either.left(true);
442 } catch (Exception e) {
443 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during deleteng all tosca elements by UUID {} and name {}. {} ", uuid, componentName, e.getMessage());
449 * Performs cancelation or failure of certification for received tosca element
451 * @param toscaElementId
457 public Either<ToscaElement, StorageOperationStatus> cancelOrFailCertification(String toscaElementId, String modifierId, String ownerId, LifecycleStateEnum nextState) {
458 Either<ToscaElement, StorageOperationStatus> result = null;
459 StorageOperationStatus status;
460 ToscaElementOperation operation = null;
461 GraphVertex toscaElement = null;
462 GraphVertex modifier = null;
464 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
465 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
466 if (getVerticesRes.isRight()) {
467 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
468 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
470 if (result == null) {
471 toscaElement = getVerticesRes.left().value().get(toscaElementId);
472 modifier = getVerticesRes.left().value().get(modifierId);
473 operation = getToscaElementOperation(toscaElement.getLabel());
474 toscaElement.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
475 toscaElement.setJsonMetadataField(JsonPresentationFields.USER_ID_LAST_UPDATER, modifier.getUniqueId());
476 toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, nextState.name());
478 Either<GraphVertex, JanusGraphOperationStatus> updateVertexRes = janusGraphDao.updateVertex(toscaElement);
479 if (updateVertexRes.isRight()) {
480 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update vertex {} . Status is {}. ", toscaElementId, updateVertexRes.right().value());
481 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateVertexRes.right().value()));
484 if (result == null) {
485 // cancel certification process
486 status = handleRelationsUponCancelCertification(toscaElement, nextState);
487 if (status != StorageOperationStatus.OK) {
488 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations upon cancel certification {}. Status is {}. ", toscaElement.getUniqueId(), status);
491 if (result == null) {
492 // fail certification
493 status = handleRelationsUponFailCertification(toscaElement, nextState);
494 if (status != StorageOperationStatus.OK) {
495 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations upon fail certification {}. Status is {}. ", toscaElement.getUniqueId(), status);
498 if (result == null) {
499 result = operation.getToscaElement(toscaElementId);
501 } catch (Exception e) {
502 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during cancel or fail certification of tosca element {}. {}. ", toscaElementId, e.getMessage());
507 public Either<GraphVertex, JanusGraphOperationStatus> findUser(String userId) {
508 return findUserVertex(userId);
511 private Either<Boolean, StorageOperationStatus> markToscaElementsAsDeleted(ToscaElementOperation operation, List<GraphVertex> toscaElements) {
512 Either<Boolean, StorageOperationStatus> result = Either.left(true);
513 for (GraphVertex resourceToDelete : toscaElements) {
514 if (!((String) resourceToDelete.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals(LifecycleStateEnum.CERTIFIED.name())) {
515 Either<GraphVertex, StorageOperationStatus> deleteElementRes = operation.markComponentToDelete(resourceToDelete);
516 if (deleteElementRes.isRight()) {
517 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete tosca element {}. Status is {}. ", resourceToDelete.getUniqueId(), deleteElementRes.right().value());
518 result = Either.right(deleteElementRes.right().value());
526 private StorageOperationStatus handleRelationsOfNewestCertifiedToscaElement(GraphVertex toscaElement, GraphVertex certifiedToscaElement) {
527 StorageOperationStatus result = null;
528 Edge foundEdge = null;
529 Iterator<Edge> certReqUserEdgeIter = null;
530 // add rfc relation to preserve follower information
531 // get user of certification request
532 certReqUserEdgeIter = toscaElement.getVertex().edges(Direction.IN, GraphEdgeLabels.LAST_STATE.name());
533 if (certReqUserEdgeIter == null || !certReqUserEdgeIter.hasNext()) {
534 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find rfc relation during certification clone. ");
535 result = StorageOperationStatus.NOT_FOUND;
537 if (result == null) {
538 JanusGraphOperationStatus
539 createVersionEdgeStatus = janusGraphDao
540 .createEdge(toscaElement, certifiedToscaElement, EdgeLabelEnum.VERSION, new HashMap<>());
541 if (createVersionEdgeStatus != JanusGraphOperationStatus.OK) {
542 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create version edge from last element {} to new certified element {}. status=", toscaElement.getUniqueId(), certifiedToscaElement.getUniqueId(),
543 createVersionEdgeStatus);
544 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createVersionEdgeStatus);
547 if (result == null) {
548 if (certReqUserEdgeIter!=null) {
549 while (certReqUserEdgeIter.hasNext()) {
550 Edge edge = certReqUserEdgeIter.next();
551 if (((String) janusGraphDao.getProperty(edge, EdgePropertyEnum.STATE)).equals(LifecycleStateEnum.READY_FOR_CERTIFICATION.name())) {
558 if (foundEdge == null) {
559 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find rfc relation during certification clone. ");
560 result = StorageOperationStatus.NOT_FOUND;
563 if (result == null) {
564 JanusGraphOperationStatus
565 createEdgeRes = janusGraphDao
566 .createEdge(foundEdge.outVertex(), certifiedToscaElement.getVertex(), EdgeLabelEnum.LAST_STATE, foundEdge);
567 if (createEdgeRes != JanusGraphOperationStatus.OK) {
568 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create rfc relation for component {}. status=", certifiedToscaElement.getUniqueId(), createEdgeRes);
569 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes);
572 if (result == null) {
573 result = StorageOperationStatus.OK;
578 private StorageOperationStatus handleRelationsUponFailCertification(GraphVertex toscaElement, LifecycleStateEnum nextState) {
579 StorageOperationStatus result = null;
580 JanusGraphOperationStatus status = null;
583 if (nextState == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN) {
584 // fail certification
585 // delete relation CERTIFICATION_IN_PROGRESS
586 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
587 properties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
589 Either<Edge, JanusGraphOperationStatus> deleteResult = janusGraphDao
590 .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.STATE, properties);
591 if (deleteResult.isRight()) {
592 status = deleteResult.right().value();
593 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete state edge. Status is {}. ", status);
594 result = StorageOperationStatus.INCONSISTENCY;
596 if (result == null) {
597 // delete relation READY_FOR_CERTIFICATION
598 properties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.READY_FOR_CERTIFICATION);
599 deleteResult = janusGraphDao
600 .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.LAST_STATE, properties);
601 if (deleteResult.isRight()) {
602 status = deleteResult.right().value();
603 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS, status);
604 result = StorageOperationStatus.INCONSISTENCY;
607 if (result == null) {
608 // delete relation NOT_CERTIFIED_CHECKIN (in order to change to STATE)
609 properties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
610 deleteResult = janusGraphDao
611 .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.LAST_STATE, properties);
612 if (deleteResult.isRight()) {
613 status = deleteResult.right().value();
614 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS, status);
615 result = StorageOperationStatus.INCONSISTENCY;
618 if (result == null) {
619 // create new STATE relation NOT_CERTIFIED_CHECKIN
620 originEdge = deleteResult.left().value();
621 user = originEdge.outVertex();
622 status = janusGraphDao
623 .createEdge(user, toscaElement.getVertex(), EdgeLabelEnum.STATE, originEdge);
624 if (status != JanusGraphOperationStatus.OK) {
625 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create state edge. Status is {}. ", status);
626 result = StorageOperationStatus.INCONSISTENCY;
629 if (result == null) {
630 // delete relation LAST_MODIFIER (in order to change tester to designer)
631 deleteResult = janusGraphDao
632 .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
633 if (status != JanusGraphOperationStatus.OK) {
634 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create last modifier edge. Status is {}. ", status);
635 result = StorageOperationStatus.INCONSISTENCY;
638 if (result == null) {
639 // create new LAST_MODIFIER relation
640 originEdge = deleteResult.left().value();
641 status = janusGraphDao.createEdge(user, toscaElement.getVertex(), EdgeLabelEnum.LAST_MODIFIER, originEdge);
642 if (status != JanusGraphOperationStatus.OK) {
643 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create last modifier edge. Status is {}. ", status);
644 result = StorageOperationStatus.INCONSISTENCY;
648 if (result == null) {
649 result = StorageOperationStatus.OK;
654 private StorageOperationStatus handleRelationsUponCancelCertification(GraphVertex toscaElement, LifecycleStateEnum nextState) {
655 StorageOperationStatus result = null;
657 if (nextState == LifecycleStateEnum.READY_FOR_CERTIFICATION) {
658 // delete relation CERTIFICATION_IN_PROGRESS
659 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
660 properties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
661 Either<Edge, JanusGraphOperationStatus> deleteResult = janusGraphDao
662 .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.STATE, properties);
664 if (deleteResult.isRight()) {
665 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete state edge. Status is {}. ", deleteResult.right().value());
666 result = StorageOperationStatus.INCONSISTENCY;
668 if (result == null) {
669 // delete relation READY_FOR_CERTIFICATION (LAST_STATE)
670 properties.put(GraphPropertyEnum.STATE, nextState);
671 deleteResult = janusGraphDao
672 .deleteBelongingEdgeByCriteria(toscaElement, EdgeLabelEnum.LAST_STATE, properties);
674 if (deleteResult.isRight()) {
675 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS, deleteResult.right().value());
676 result = StorageOperationStatus.INCONSISTENCY;
679 if (result == null) {
680 // create relation READY_FOR_CERTIFICATION (STATE)
681 originEdge = deleteResult.left().value();
682 JanusGraphOperationStatus
683 status = janusGraphDao
684 .createEdge(originEdge.outVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, originEdge);
685 if (status != JanusGraphOperationStatus.OK) {
686 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create state edge. Status is {}. ", status);
687 result = StorageOperationStatus.INCONSISTENCY;
690 if (result == null) {
691 result = StorageOperationStatus.OK;
697 private StorageOperationStatus handleRelationsOfPreviousToscaElementBeforeCertifying(GraphVertex toscaElement, GraphVertex modifier, Integer majorVersion) {
698 StorageOperationStatus result = null;
699 if (majorVersion > 0) {
700 Either<Vertex, StorageOperationStatus> findRes = findLastCertifiedToscaElementVertex(toscaElement);
701 if (findRes.isRight()) {
702 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch last certified tosca element {} . Status is {}. ", toscaElement.getMetadataProperty(GraphPropertyEnum.NAME), findRes.right().value());
703 result = findRes.right().value();
705 if (result == null) {
706 Vertex lastCertifiedVertex = findRes.left().value();
707 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
708 properties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, false);
709 JanusGraphOperationStatus status = janusGraphDao
710 .updateVertexMetadataPropertiesWithJson(lastCertifiedVertex, properties);
711 if (status != JanusGraphOperationStatus.OK) {
712 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to set highest version of tosca element {} to [{}]. Status is {}", toscaElement.getUniqueId(), false, status);
713 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
715 // remove previous certified version from the catalog
716 GraphVertex lastCertifiedV = new GraphVertex();
717 lastCertifiedV.setVertex((JanusGraphVertex) lastCertifiedVertex);
718 lastCertifiedV.setUniqueId((String) janusGraphDao
719 .getProperty((JanusGraphVertex) lastCertifiedVertex, GraphPropertyEnum.UNIQUE_ID.getProperty()));
720 StorageOperationStatus res = updateEdgeToCatalogRoot(null, lastCertifiedV);
721 if (res != StorageOperationStatus.OK) {
726 if (result == null) {
727 result = StorageOperationStatus.OK;
732 private StorageOperationStatus handleRelationsUponRequestForCertification(GraphVertex toscaElement, GraphVertex modifier, GraphVertex owner) {
733 JanusGraphOperationStatus status;
734 StorageOperationStatus result = null;
736 if (((String) toscaElement.getMetadataProperty(GraphPropertyEnum.STATE)).equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
737 // remove CHECKOUT relation
738 Either<Edge, JanusGraphOperationStatus> deleteRes = janusGraphDao
739 .deleteEdge(owner, toscaElement, EdgeLabelEnum.STATE);
740 if (deleteRes.isRight()) {
741 status = deleteRes.right().value();
742 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete edge. Status is {}. ", status);
743 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
745 if (result == null) {
746 // create CHECKIN relation
747 Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
748 properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
749 status = janusGraphDao
750 .createEdge(modifier.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.LAST_STATE, properties);
751 if (status != JanusGraphOperationStatus.OK) {
752 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge. Status is {}", status);
753 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
757 status = janusGraphDao
758 .replaceEdgeLabel(owner.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE);
759 if (status != JanusGraphOperationStatus.OK) {
760 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
763 if (result == null) {
764 // create RFC relation
765 Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
766 properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.READY_FOR_CERTIFICATION);
767 status = janusGraphDao
768 .createEdge(modifier.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, properties);
769 if (status != JanusGraphOperationStatus.OK) {
770 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge. Status is {}", status);
771 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
774 if (result == null) {
775 result = StorageOperationStatus.OK;
780 private StorageOperationStatus handleRelationsUponCertification(GraphVertex toscaElement, GraphVertex modifier, GraphVertex owner) {
782 StorageOperationStatus result = null;
783 JanusGraphOperationStatus status = janusGraphDao
784 .replaceEdgeLabel(owner.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE);
785 if (status != JanusGraphOperationStatus.OK) {
786 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
788 if (result == null) {
789 Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
790 properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
791 status = janusGraphDao
792 .createEdge(modifier, toscaElement, EdgeLabelEnum.STATE, properties);
793 if (status != JanusGraphOperationStatus.OK) {
794 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "failed to create edge. Status is {}", status);
795 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
798 if (result == null) {
799 Either<GraphVertex, StorageOperationStatus> updateRelationsRes = updateLastModifierEdge(toscaElement, owner, modifier);
800 if (updateRelationsRes.isRight()) {
801 result = updateRelationsRes.right().value();
804 if (result == null) {
805 result = StorageOperationStatus.OK;
810 private Either<Vertex, StorageOperationStatus> findLastCertifiedToscaElementVertex(GraphVertex toscaElement) {
811 return findLastCertifiedToscaElementVertexRecursively(toscaElement.getVertex());
814 private Either<Vertex, StorageOperationStatus> findLastCertifiedToscaElementVertexRecursively(Vertex vertex) {
815 if (isCertifiedVersion((String) vertex.property(GraphPropertyEnum.VERSION.getProperty()).value())) {
816 return Either.left(vertex);
818 Iterator<Edge> edgeIter = vertex.edges(Direction.IN, EdgeLabelEnum.VERSION.name());
819 if (!edgeIter.hasNext()) {
820 return Either.right(StorageOperationStatus.NOT_FOUND);
822 return findLastCertifiedToscaElementVertexRecursively(edgeIter.next().outVertex());
825 private boolean isCertifiedVersion(String version) {
826 String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
827 if (Integer.parseInt(versionParts[0]) > 0 && Integer.parseInt(versionParts[1]) == 0) {
833 private StorageOperationStatus updateOldToscaElementBeforeUndoCheckout(Vertex previousVersionToscaElement) {
835 StorageOperationStatus result = StorageOperationStatus.OK;
836 String previousVersion = (String) previousVersionToscaElement.property(GraphPropertyEnum.VERSION.getProperty()).value();
837 if (!previousVersion.endsWith(".0")) {
839 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update vertex of previous version of tosca element", previousVersionToscaElement.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()));
841 Map<String, Object> propertiesToUpdate = new HashMap<>();
842 propertiesToUpdate.put(GraphPropertyEnum.IS_HIGHEST_VERSION.getProperty(), true);
843 Map<String, Object> jsonMetadataMap = JsonParserUtils.toMap((String) previousVersionToscaElement.property(GraphPropertyEnum.METADATA.getProperty()).value());
844 jsonMetadataMap.put(GraphPropertyEnum.IS_HIGHEST_VERSION.getProperty(), true);
845 propertiesToUpdate.put(GraphPropertyEnum.METADATA.getProperty(), JsonParserUtils.toJson(jsonMetadataMap));
847 janusGraphDao.setVertexProperties(previousVersionToscaElement, propertiesToUpdate);
849 Iterator<Edge> edgesIter = previousVersionToscaElement.edges(Direction.IN, EdgeLabelEnum.LAST_STATE.name());
850 if (!edgesIter.hasNext()) {
851 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch last modifier vertex for tosca element {}. ", previousVersionToscaElement.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()));
852 result = StorageOperationStatus.NOT_FOUND;
854 Edge lastStateEdge = edgesIter.next();
855 Vertex lastModifier = lastStateEdge.outVertex();
856 JanusGraphOperationStatus replaceRes = janusGraphDao
857 .replaceEdgeLabel(lastModifier, previousVersionToscaElement, lastStateEdge, EdgeLabelEnum.LAST_STATE, EdgeLabelEnum.STATE);
858 if (replaceRes != JanusGraphOperationStatus.OK) {
859 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to replace label from {} to {}. status = {}", EdgeLabelEnum.LAST_STATE, EdgeLabelEnum.STATE, replaceRes);
860 result = StorageOperationStatus.INCONSISTENCY;
861 if (replaceRes != JanusGraphOperationStatus.INVALID_ID) {
862 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(replaceRes);
867 } catch (Exception e) {
868 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during update previous tosca element {} before undo checkout. {} ", e.getMessage());
874 private StorageOperationStatus updatePreviousVersion(GraphVertex toscaElementVertex, GraphVertex ownerVertex) {
875 StorageOperationStatus result = null;
876 String ownerId = (String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID);
877 String toscaElementId = toscaElementVertex.getUniqueId();
878 if (!toscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE).equals(LifecycleStateEnum.CERTIFIED.name())) {
879 toscaElementVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, false);
880 Either<GraphVertex, JanusGraphOperationStatus> updateVertexRes = janusGraphDao.updateVertex(toscaElementVertex);
881 if (updateVertexRes.isRight()) {
882 JanusGraphOperationStatus titatStatus = updateVertexRes.right().value();
883 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update tosca element vertex {}. Status is {}", toscaElementVertex.getUniqueId(), titatStatus);
884 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(titatStatus);
886 Either<Edge, JanusGraphOperationStatus> deleteEdgeRes = null;
887 if (result == null) {
888 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to replace edge with label {} to label {} from {} to {}. ", EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE, ownerId, toscaElementId);
890 deleteEdgeRes = janusGraphDao
891 .deleteEdge(ownerVertex, toscaElementVertex, EdgeLabelEnum.STATE);
892 if (deleteEdgeRes.isRight()) {
893 JanusGraphOperationStatus janusGraphStatus = deleteEdgeRes.right().value();
894 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete edge with label {} from {} to {}. Status is {} ", EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE, ownerId, toscaElementId, janusGraphStatus);
895 if (!janusGraphStatus.equals(JanusGraphOperationStatus.INVALID_ID)) {
896 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(janusGraphStatus);
898 result = StorageOperationStatus.INCONSISTENCY;
902 if (result == null) {
903 JanusGraphOperationStatus
904 createEdgeRes = janusGraphDao
905 .createEdge(ownerVertex.getVertex(), toscaElementVertex.getVertex(), EdgeLabelEnum.LAST_STATE, deleteEdgeRes.left().value());
906 if (createEdgeRes != JanusGraphOperationStatus.OK) {
907 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes);
911 if (result == null) {
912 result = StorageOperationStatus.OK;
917 private Either<ToscaElement, StorageOperationStatus> cloneToscaElementForCheckout(GraphVertex toscaElementVertex, GraphVertex modifierVertex) {
919 Either<ToscaElement, StorageOperationStatus> result = null;
920 Either<GraphVertex, StorageOperationStatus> cloneResult = null;
921 ToscaElementOperation operation = getToscaElementOperation(toscaElementVertex.getLabel());
922 // check if component with the next version doesn't exist.
923 Iterator<Edge> nextVersionComponentIter = toscaElementVertex.getVertex().edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
924 if (nextVersionComponentIter != null && nextVersionComponentIter.hasNext()) {
925 Vertex nextVersionVertex = nextVersionComponentIter.next().inVertex();
926 String fetchedVersion = (String) nextVersionVertex.property(GraphPropertyEnum.VERSION.getProperty()).value();
927 String fetchedName = (String) nextVersionVertex.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
928 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to checkout component {} with version {}. The component with name {} and version {} was fetched from graph as existing following version. ",
929 toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME).toString(), toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION).toString(), fetchedName, fetchedVersion);
930 result = Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS);
932 if (result == null) {
933 toscaElementVertex.getOrSetDefaultInstantiationTypeForToscaElementJson();
934 cloneResult = operation.cloneToscaElement(toscaElementVertex, cloneGraphVertexForCheckout(toscaElementVertex, modifierVertex), modifierVertex);
935 if (cloneResult.isRight()) {
936 result = Either.right(cloneResult.right().value());
939 GraphVertex clonedVertex = null;
940 if (result == null) {
941 clonedVertex = cloneResult.left().value();
942 JanusGraphOperationStatus
943 status = janusGraphDao
944 .createEdge(toscaElementVertex.getVertex(), cloneResult.left().value().getVertex(), EdgeLabelEnum.VERSION, new HashMap<>());
945 if (status != JanusGraphOperationStatus.OK) {
946 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge with label {} from vertex {} to tosca element vertex {} on graph. Status is {}. ", EdgeLabelEnum.VERSION,
947 toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), cloneResult.left().value().getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
948 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
951 if (result == null) {
952 Boolean isHighest = (Boolean) toscaElementVertex.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
953 GraphVertex prevVersionInCatalog = (isHighest != null && isHighest) ? null : toscaElementVertex;
954 StorageOperationStatus updateCatalogRes = updateEdgeToCatalogRoot(clonedVertex, prevVersionInCatalog);
955 if (updateCatalogRes != StorageOperationStatus.OK) {
956 return Either.right(updateCatalogRes);
958 result = operation.getToscaElement(cloneResult.left().value().getUniqueId());
959 if (result.isRight()) {
962 ToscaElement toscaElement = result.left().value();
963 if (toscaElement.getToscaType() == ToscaElementTypeEnum.TOPOLOGY_TEMPLATE) {
964 result = handleFixTopologyTemplate(toscaElementVertex, result, operation, clonedVertex, toscaElement);
971 private Either<ToscaElement, StorageOperationStatus> handleFixTopologyTemplate(GraphVertex toscaElementVertex, Either<ToscaElement, StorageOperationStatus> result, ToscaElementOperation operation, GraphVertex clonedVertex,
972 ToscaElement toscaElement) {
973 TopologyTemplate topologyTemplate = (TopologyTemplate) toscaElement;
974 Map<String, MapPropertiesDataDefinition> instInputs = topologyTemplate.getInstInputs();
975 Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
976 Map<String, MapArtifactDataDefinition> instArtifactsMap = topologyTemplate.getInstanceArtifacts();
977 Map<String, ToscaElement> origCompMap = new HashMap<>();
978 if (instInputs == null) {
979 instInputs = new HashMap<>();
981 if (instGroups == null) {
982 instGroups = new HashMap<>();
984 if (instArtifactsMap == null) {
985 instArtifactsMap = new HashMap<>();
987 Map<String, ComponentInstanceDataDefinition> instancesMap = topologyTemplate.getComponentInstances();
988 boolean isAddInstGroup = instGroups == null || instGroups.isEmpty();
989 boolean needUpdateComposition = false;
991 if (instancesMap != null && !instancesMap.isEmpty()) {
992 for (ComponentInstanceDataDefinition vfInst : instancesMap.values()) {
993 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "vfInst name is {} . OriginType {}. ", vfInst.getName(), vfInst.getOriginType());
994 if (vfInst.getOriginType().name().equals(OriginTypeEnum.VF.name())) {
995 collectInstanceInputAndGroups(instInputs, instGroups, instArtifactsMap, origCompMap, isAddInstGroup, vfInst, clonedVertex);
997 needUpdateComposition = needUpdateComposition || fixToscaComponentName(vfInst, origCompMap);
998 if (needUpdateComposition) {
999 instancesMap.put(vfInst.getUniqueId(), vfInst);
1002 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before add to graph instInputs {} instGroups {} needUpdateComposition {}", instInputs, instGroups, needUpdateComposition);
1003 if (!instInputs.isEmpty()) {
1004 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before add inst inputs {} ", instInputs == null ? 0 : instInputs.size());
1005 GraphVertex toscaDataVertex = null;
1006 Either<GraphVertex, JanusGraphOperationStatus> instInpVertexEither = janusGraphDao
1007 .getChildVertex(toscaElementVertex, EdgeLabelEnum.INST_INPUTS, JsonParseFlagEnum.ParseJson);
1008 if (instInpVertexEither.isLeft()) {
1009 toscaDataVertex = instInpVertexEither.left().value();
1012 StorageOperationStatus status = handleToscaData(clonedVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, toscaDataVertex, instInputs);
1013 if (status != StorageOperationStatus.OK) {
1014 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update instance inputs . Status is {}. ", status);
1015 result = Either.right(status);
1020 if (!instGroups.isEmpty()) {
1021 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before add inst groups {} ", instGroups == null ? 0 : instGroups.size());
1022 GraphVertex toscaDataVertex = null;
1023 Either<GraphVertex, JanusGraphOperationStatus> instGrVertexEither = janusGraphDao
1024 .getChildVertex(toscaElementVertex, EdgeLabelEnum.INST_GROUPS, JsonParseFlagEnum.ParseJson);
1025 if (instGrVertexEither.isLeft()) {
1026 toscaDataVertex = instGrVertexEither.left().value();
1029 StorageOperationStatus status = handleToscaData(clonedVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, toscaDataVertex, instGroups);
1030 if (status != StorageOperationStatus.OK) {
1031 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update instance group . Status is {}. ", status);
1032 result = Either.right(status);
1037 if (needUpdateComposition) {
1038 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before update Instances ");
1039 Map<String, CompositionDataDefinition> jsonComposition = (Map<String, CompositionDataDefinition>) clonedVertex.getJson();
1040 CompositionDataDefinition compositionDataDefinition = jsonComposition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1041 compositionDataDefinition.setComponentInstances(instancesMap);
1042 Either<GraphVertex, JanusGraphOperationStatus> updateElement = janusGraphDao.updateVertex(clonedVertex);
1043 if (updateElement.isRight()) {
1044 JanusGraphOperationStatus status = updateElement.right().value();
1045 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update instances on metadata vertex . Status is {}. ", status);
1046 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1051 result = operation.getToscaElement(clonedVertex.getUniqueId());
1054 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "RI map empty on component {}", toscaElement.getUniqueId());
1059 // TODO remove after jsonModelMigration
1060 public boolean resolveToscaComponentName(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap) {
1061 return fixToscaComponentName(vfInst, origCompMap);
1064 private boolean fixToscaComponentName(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap) {
1065 if (vfInst.getToscaComponentName() == null || vfInst.getToscaComponentName().isEmpty()) {
1066 String ciUid = vfInst.getUniqueId();
1067 String origCompUid = vfInst.getComponentUid();
1068 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "fixToscaComponentName:: Ri id {} . origin component id is {}. type is{} ", ciUid, origCompUid, vfInst.getOriginType());
1069 ToscaElement origComp = null;
1070 if (!origCompMap.containsKey(origCompUid)) {
1071 Either<ToscaElement, StorageOperationStatus> origCompEither;
1072 if (vfInst.getOriginType() == null || vfInst.getOriginType().name().equals(OriginTypeEnum.VF.name())) {
1073 origCompEither = topologyTemplateOperation.getToscaElement(origCompUid);
1075 origCompEither = nodeTypeOperation.getToscaElement(origCompUid);
1077 if (origCompEither.isRight()) {
1078 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find orig component {} . Status is {}. ", origCompEither.right().value());
1081 origComp = origCompEither.left().value();
1082 origCompMap.put(origCompUid, origComp);
1084 origComp = origCompMap.get(origCompUid);
1086 String toscaName = (String) origComp.getMetadataValue(JsonPresentationFields.TOSCA_RESOURCE_NAME);
1087 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Origin component id is {}. toscaName {}", origCompUid, toscaName);
1088 vfInst.setToscaComponentName(toscaName);
1094 private void collectInstanceInputAndGroups(Map<String, MapPropertiesDataDefinition> instInputs, Map<String, MapGroupsDataDefinition> instGroups, Map<String, MapArtifactDataDefinition> instArtifactsMap, Map<String, ToscaElement> origCompMap,
1095 boolean isAddInstGroup, ComponentInstanceDataDefinition vfInst, GraphVertex clonedVertex) {
1096 String ciUid = vfInst.getUniqueId();
1097 String origCompUid = vfInst.getComponentUid();
1098 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "collectInstanceInputAndGroups:: Ri id {} . origin component id is {}. ", ciUid, origCompUid);
1099 TopologyTemplate origComp = null;
1100 if (!origCompMap.containsKey(origCompUid)) {
1101 Either<ToscaElement, StorageOperationStatus> origCompEither = topologyTemplateOperation.getToscaElement(origCompUid);
1102 if (origCompEither.isRight()) {
1103 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find orig component {} . Status is {}. ", origCompEither.right().value());
1106 origComp = (TopologyTemplate) origCompEither.left().value();
1107 origCompMap.put(origCompUid, origComp);
1109 origComp = (TopologyTemplate) origCompMap.get(origCompUid);
1111 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Orig component {}. ", origComp.getUniqueId());
1113 Map<String, PropertyDataDefinition> origInputs = origComp.getInputs();
1114 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Orig component inputs size {}. ", origInputs == null ? 0 : origInputs.size());
1115 if (origInputs != null) {
1116 if (!instInputs.containsKey(ciUid)) {
1117 MapPropertiesDataDefinition instProperties = new MapPropertiesDataDefinition(origInputs);
1118 instInputs.put(ciUid, instProperties);
1121 MapPropertiesDataDefinition instInputMap = instInputs.get(ciUid);
1122 Map<String, PropertyDataDefinition> instProp = instInputMap.getMapToscaDataDefinition();
1123 origInputs.forEach((propName, propMap) -> {
1124 if (!instProp.containsKey(propName)) {
1125 instProp.put(propName, propMap);
1129 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "ComponentInstanseInputs {}. ", instInputs.get(ciUid));
1132 if (isAddInstGroup) {
1133 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "before create group instance. ");
1134 List<GroupDataDefinition> filteredGroups = null;
1136 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check vf groups before filter. Size is {} ", filteredGroups == null ? 0 : filteredGroups.size());
1137 if (origComp.getGroups() != null && !origComp.getGroups().isEmpty()) {
1138 filteredGroups = origComp.getGroups().values().stream().filter(g -> g.getType().equals(VF_MODULE)).collect(Collectors.toList());
1139 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check vf groups . Size is {} ", filteredGroups == null ? 0 : filteredGroups.size());
1141 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check vf groups after filter. Size is {} ", filteredGroups == null ? 0 : filteredGroups.size());
1142 if (CollectionUtils.isNotEmpty(filteredGroups)) {
1143 MapArtifactDataDefinition instArifacts = null;
1144 if (!instArtifactsMap.containsKey(ciUid)) {
1146 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "istance artifacts not found ");
1148 Map<String, ArtifactDataDefinition> deploymentArtifacts = origComp.getDeploymentArtifacts();
1150 instArifacts = new MapArtifactDataDefinition(deploymentArtifacts);
1151 addToscaDataDeepElementsBlockToToscaElement(clonedVertex, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, instArifacts, ciUid);
1153 instArtifactsMap.put(ciUid, instArifacts);
1156 instArifacts = instArtifactsMap.get(ciUid);
1159 if (instArifacts != null) {
1160 Map<String, ArtifactDataDefinition> instDeplArtifMap = instArifacts.getMapToscaDataDefinition();
1162 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "check group dep artifacts. Size is {} ", instDeplArtifMap == null ? 0 : instDeplArtifMap.values().size());
1163 Map<String, GroupInstanceDataDefinition> groupInstanceToCreate = new HashMap<>();
1164 for (GroupDataDefinition group : filteredGroups) {
1165 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "create new groupInstance {} ", group.getName());
1166 GroupInstanceDataDefinition groupInstance = buildGroupInstanceDataDefinition(group, vfInst, instDeplArtifMap);
1167 List<String> artifactsUid = new ArrayList<>();
1168 List<String> artifactsId = new ArrayList<>();
1169 if (instDeplArtifMap!=null) {
1170 for (ArtifactDataDefinition artifact : instDeplArtifMap.values()) {
1171 Optional<String> op = group.getArtifacts().stream().filter(p -> p.equals(artifact.getGeneratedFromId())).findAny();
1172 if (op.isPresent()) {
1173 artifactsUid.add(artifact.getArtifactUUID());
1174 artifactsId.add(artifact.getUniqueId());
1179 groupInstance.setGroupInstanceArtifacts(artifactsId);
1180 groupInstance.setGroupInstanceArtifactsUuid(artifactsUid);
1181 groupInstanceToCreate.put(groupInstance.getName(), groupInstance);
1183 if (MapUtils.isNotEmpty(groupInstanceToCreate)) {
1184 instGroups.put(vfInst.getUniqueId(), new MapGroupsDataDefinition(groupInstanceToCreate));
1192 private GraphVertex cloneGraphVertexForCheckout(GraphVertex toscaElementVertex, GraphVertex modifierVertex) {
1193 GraphVertex nextVersionToscaElementVertex = new GraphVertex();
1194 String uniqueId = UniqueIdBuilder.buildComponentUniqueId();
1195 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>(toscaElementVertex.getMetadataProperties());
1196 nextVersionToscaElementVertex.setMetadataProperties(metadataProperties);
1197 nextVersionToscaElementVertex.setUniqueId(uniqueId);
1198 nextVersionToscaElementVertex.setLabel(toscaElementVertex.getLabel());
1199 nextVersionToscaElementVertex.setType(toscaElementVertex.getType());
1201 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
1202 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, nextVersionToscaElementVertex.getType().name());
1203 String nextVersion = getNextVersion((String) toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION));
1204 if (isFirstCheckoutAfterCertification(nextVersion)) {
1205 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.UUID, IdBuilderUtils.generateUUID());
1207 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.VERSION, nextVersion);
1208 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
1209 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1211 if (toscaElementVertex.getType() == ComponentTypeEnum.SERVICE && toscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE).equals(LifecycleStateEnum.CERTIFIED.name())) {
1212 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
1214 if (!MapUtils.isEmpty(toscaElementVertex.getMetadataJson())) {
1215 nextVersionToscaElementVertex.setMetadataJson(new HashMap<>(toscaElementVertex.getMetadataJson()));
1216 nextVersionToscaElementVertex.updateMetadataJsonWithCurrentMetadataProperties();
1218 long currTime = System.currentTimeMillis();
1219 nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.CREATION_DATE, currTime);
1220 nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, currTime);
1221 nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_CREATOR, modifierVertex.getUniqueId());
1222 nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_LAST_UPDATER, modifierVertex.getUniqueId());
1223 if (toscaElementVertex.getType() == ComponentTypeEnum.SERVICE) {
1224 nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.CONFORMANCE_LEVEL, ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel());
1227 if (!MapUtils.isEmpty(toscaElementVertex.getJson())) {
1228 nextVersionToscaElementVertex.setJson(new HashMap<String, ToscaDataDefinition>(toscaElementVertex.getJson()));
1230 return nextVersionToscaElementVertex;
1233 private Either<GraphVertex, StorageOperationStatus> cloneToscaElementForCertify(GraphVertex toscaElementVertex, GraphVertex modifierVertex, Integer majorVersion) {
1234 Either<GraphVertex, StorageOperationStatus> result;
1235 Either<List<GraphVertex>, StorageOperationStatus> deleteResult = null;
1236 GraphVertex clonedToscaElement = null;
1237 result = getToscaElementOperation(toscaElementVertex.getLabel()).cloneToscaElement(toscaElementVertex, cloneGraphVertexForCertify(toscaElementVertex, modifierVertex, majorVersion), modifierVertex);
1238 if (result.isRight()) {
1239 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element {} for certification. Sattus is {}. ", toscaElementVertex.getUniqueId(), result.right().value());
1241 clonedToscaElement = result.left().value();
1242 StorageOperationStatus updateEdgeToCatalog = updateEdgeToCatalogRoot(clonedToscaElement, toscaElementVertex);
1243 if (updateEdgeToCatalog != StorageOperationStatus.OK) {
1244 return Either.right(updateEdgeToCatalog);
1246 deleteResult = deleteAllPreviousNotCertifiedVersions(toscaElementVertex);
1247 if (deleteResult.isRight()) {
1248 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete all previous npt certified versions of tosca element {}. Status is {}. ", toscaElementVertex.getUniqueId(), deleteResult.right().value());
1249 result = Either.right(deleteResult.right().value());
1252 if (result.isLeft()) {
1253 result = handlePreviousVersionRelation(clonedToscaElement, deleteResult.left().value(), majorVersion);
1258 private Either<GraphVertex, StorageOperationStatus> handlePreviousVersionRelation(GraphVertex clonedToscaElement, List<GraphVertex> deletedVersions, Integer majorVersion) {
1259 Either<GraphVertex, StorageOperationStatus> result = null;
1260 Vertex previousCertifiedToscaElement = null;
1261 if (majorVersion > 0) {
1262 List<GraphVertex> firstMinorVersionVertex = deletedVersions.stream().filter(gv -> getMinorVersion((String) gv.getMetadataProperty(GraphPropertyEnum.VERSION)) == 1).collect(Collectors.toList());
1264 if (CollectionUtils.isEmpty(firstMinorVersionVertex)) {
1265 result = Either.right(StorageOperationStatus.NOT_FOUND);
1267 previousCertifiedToscaElement = getPreviousCertifiedToscaElement(firstMinorVersionVertex.get(0));
1268 if (previousCertifiedToscaElement == null) {
1269 result = Either.right(StorageOperationStatus.NOT_FOUND);
1272 if (result == null) {
1273 JanusGraphOperationStatus
1274 status = janusGraphDao
1275 .createEdge(previousCertifiedToscaElement, clonedToscaElement.getVertex(), EdgeLabelEnum.VERSION, new HashMap<>());
1276 if (status != JanusGraphOperationStatus.OK) {
1277 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge with label {} from vertex {} to tosca element vertex {} on graph. Status is {}. ", EdgeLabelEnum.VERSION,
1278 previousCertifiedToscaElement.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), clonedToscaElement.getUniqueId(), status);
1279 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1284 if (result == null) {
1285 result = Either.left(clonedToscaElement);
1290 private Vertex getPreviousCertifiedToscaElement(GraphVertex graphVertex) {
1292 Iterator<Edge> edges = graphVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.VERSION.name());
1293 if (edges.hasNext()) {
1294 return edges.next().outVertex();
1299 private Either<List<GraphVertex>, StorageOperationStatus> deleteAllPreviousNotCertifiedVersions(GraphVertex toscaElementVertex) {
1300 Either<List<GraphVertex>, StorageOperationStatus> result = null;
1302 ToscaElementOperation operation = getToscaElementOperation(toscaElementVertex.getLabel());
1303 List<GraphVertex> previosVersions = null;
1304 Object uuid = toscaElementVertex.getMetadataProperty(GraphPropertyEnum.UUID);
1305 Object componentName = toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NAME);
1307 Map<GraphPropertyEnum, Object> properties = new HashMap<>();
1308 properties.put(GraphPropertyEnum.UUID, uuid);
1309 properties.put(GraphPropertyEnum.NAME, componentName);
1310 Either<List<GraphVertex>, JanusGraphOperationStatus> getToscaElementsRes = janusGraphDao
1311 .getByCriteria(toscaElementVertex.getLabel(), properties, JsonParseFlagEnum.ParseMetadata);
1312 if (getToscaElementsRes.isRight()) {
1313 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getToscaElementsRes.right().value()));
1315 if (result == null) {
1316 previosVersions = getToscaElementsRes.left().value();
1317 Either<Boolean, StorageOperationStatus> deleteResult = markToscaElementsAsDeleted(operation, getToscaElementsRes.left().value());
1318 if (deleteResult.isRight()) {
1319 result = Either.right(deleteResult.right().value());
1322 if (result == null) {
1323 result = Either.left(previosVersions);
1325 } catch (Exception e) {
1326 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during deleteng all tosca elements by UUID {} and name {}. {} ", uuid, componentName, e.getMessage());
1331 private GraphVertex cloneGraphVertexForCertify(GraphVertex toscaElementVertex, GraphVertex modifierVertex, Integer majorVersion) {
1333 GraphVertex nextVersionToscaElementVertex = new GraphVertex();
1334 String uniqueId = IdBuilderUtils.generateUniqueId();
1335 Map<GraphPropertyEnum, Object> metadataProperties = new EnumMap<>(toscaElementVertex.getMetadataProperties());
1336 nextVersionToscaElementVertex.setMetadataProperties(metadataProperties);
1337 nextVersionToscaElementVertex.setUniqueId(uniqueId);
1338 nextVersionToscaElementVertex.setLabel(toscaElementVertex.getLabel());
1339 nextVersionToscaElementVertex.setType(toscaElementVertex.getType());
1341 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
1342 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, nextVersionToscaElementVertex.getType().name());
1343 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.VERSION, (majorVersion + 1) + VERSION_DELIMITER + "0");
1344 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1345 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1346 nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.CREATION_DATE, System.currentTimeMillis());
1347 nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, null);
1348 nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_CREATOR, modifierVertex.getUniqueId());
1349 nextVersionToscaElementVertex.setJsonMetadataField(JsonPresentationFields.USER_ID_LAST_UPDATER, modifierVertex.getUniqueId());
1351 if (toscaElementVertex.getType() == ComponentTypeEnum.SERVICE && toscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE).equals(LifecycleStateEnum.CERTIFIED)) {
1352 nextVersionToscaElementVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
1354 if (!MapUtils.isEmpty(toscaElementVertex.getMetadataJson())) {
1355 nextVersionToscaElementVertex.setMetadataJson(new HashMap<>(toscaElementVertex.getMetadataJson()));
1356 nextVersionToscaElementVertex.updateMetadataJsonWithCurrentMetadataProperties();
1358 if (!MapUtils.isEmpty(toscaElementVertex.getJson())) {
1359 nextVersionToscaElementVertex.setJson(new HashMap<String, ToscaDataDefinition>(toscaElementVertex.getJson()));
1361 return nextVersionToscaElementVertex;
1365 private Either<GraphVertex, StorageOperationStatus> checkinToscaELement(LifecycleStateEnum currState, GraphVertex toscaElementVertex, GraphVertex ownerVertex, GraphVertex modifierVertex, LifecycleStateEnum nextState) {
1366 Either<GraphVertex, StorageOperationStatus> updateRelationsRes;
1367 Either<GraphVertex, StorageOperationStatus> result = changeStateToCheckedIn(currState, toscaElementVertex, ownerVertex, modifierVertex);
1368 if (result.isLeft()) {
1369 toscaElementVertex.addMetadataProperty(GraphPropertyEnum.STATE, nextState.name());
1370 toscaElementVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
1371 result = updateToscaElementVertexMetadataPropertiesAndJson(toscaElementVertex);
1373 if (result.isLeft()) {
1374 updateRelationsRes = updateLastModifierEdge(toscaElementVertex, ownerVertex, modifierVertex);
1375 if (updateRelationsRes.isRight()) {
1376 result = Either.right(updateRelationsRes.right().value());
1382 private Either<GraphVertex, StorageOperationStatus> updateToscaElementVertexMetadataPropertiesAndJson(GraphVertex toscaElementVertex) {
1384 Either<GraphVertex, StorageOperationStatus> result;
1386 Either<GraphVertex, JanusGraphOperationStatus> updateVertexRes = janusGraphDao.updateVertex(toscaElementVertex);
1387 if (updateVertexRes.isRight()) {
1388 JanusGraphOperationStatus titatStatus = updateVertexRes.right().value();
1389 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update state of tosca element vertex {} metadata. Status is {}", toscaElementVertex.getUniqueId(), titatStatus);
1390 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(titatStatus));
1392 result = Either.left(updateVertexRes.left().value());
1397 private Either<GraphVertex, StorageOperationStatus> changeStateToCheckedIn(LifecycleStateEnum currState, GraphVertex toscaElementVertex, GraphVertex ownerVertex, GraphVertex modifierVertex) {
1398 Either<GraphVertex, StorageOperationStatus> result = null;
1399 LifecycleStateEnum nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
1400 String faileToUpdateStateMsg = "Failed to update state of tosca element {}. Status is {}";
1402 if (currState == LifecycleStateEnum.READY_FOR_CERTIFICATION) {
1403 // In case of cancel "ready for certification" remove last state edge with "STATE" property equals to "NOT_CERTIFIED_CHECKIN"
1404 Map<GraphPropertyEnum, Object> vertexProperties = new EnumMap<>(GraphPropertyEnum.class);
1405 vertexProperties.put(GraphPropertyEnum.STATE, nextState);
1406 Either<Edge, JanusGraphOperationStatus> deleteResult = janusGraphDao
1407 .deleteBelongingEdgeByCriteria(toscaElementVertex, EdgeLabelEnum.LAST_STATE, vertexProperties);
1408 if (deleteResult.isRight()) {
1409 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, faileToUpdateStateMsg, toscaElementVertex.getUniqueId(), deleteResult.right().value());
1410 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "failed to update last state relation");
1411 result = Either.right(StorageOperationStatus.INCONSISTENCY);
1414 if (result == null) {
1415 // Remove CHECKOUT relation
1416 Either<Edge, JanusGraphOperationStatus> deleteEdgeResult = janusGraphDao
1417 .deleteEdge(ownerVertex, toscaElementVertex, EdgeLabelEnum.STATE);
1418 if (deleteEdgeResult.isRight()) {
1419 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, faileToUpdateStateMsg, toscaElementVertex.getUniqueId());
1420 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteEdgeResult.right().value()));
1423 if (result == null) {
1424 // Create CHECKIN relation
1425 Map<EdgePropertyEnum, Object> edgeProperties = new EnumMap<>(EdgePropertyEnum.class);
1426 edgeProperties.put(EdgePropertyEnum.STATE, nextState);
1427 JanusGraphOperationStatus
1428 createEdgeRes = janusGraphDao
1429 .createEdge(modifierVertex.getVertex(), toscaElementVertex.getVertex(), EdgeLabelEnum.STATE, edgeProperties);
1430 if (createEdgeRes != JanusGraphOperationStatus.OK) {
1431 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, faileToUpdateStateMsg, toscaElementVertex.getUniqueId());
1432 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes));
1435 if (result == null) {
1436 result = Either.left(toscaElementVertex);
1441 private Either<GraphVertex, StorageOperationStatus> updateLastModifierEdge(GraphVertex toscaElementVertex, GraphVertex ownerVertex, GraphVertex modifierVertex) {
1442 Either<GraphVertex, StorageOperationStatus> result = null;
1443 if (!modifierVertex.getMetadataProperties().get(GraphPropertyEnum.USERID).equals(ownerVertex.getMetadataProperties().get(GraphPropertyEnum.USERID))) {
1444 Either<Edge, JanusGraphOperationStatus> deleteEdgeRes = janusGraphDao
1445 .deleteEdge(ownerVertex, toscaElementVertex, EdgeLabelEnum.LAST_MODIFIER);
1446 if (deleteEdgeRes.isRight()) {
1447 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete last modifier {} to tosca element {}. Edge type is {}", ownerVertex.getUniqueId(), ownerVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
1448 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteEdgeRes.right().value()));
1450 if (result == null) {
1451 JanusGraphOperationStatus createEdgeRes = janusGraphDao
1452 .createEdge(modifierVertex.getVertex(), toscaElementVertex.getVertex(), EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
1454 if (createEdgeRes != JanusGraphOperationStatus.OK) {
1455 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to associate user {} to component {}. Edge type is {}", modifierVertex.getUniqueId(), ownerVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
1456 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdgeRes));
1458 result = Either.left(modifierVertex);
1462 result = Either.left(ownerVertex);
1467 private Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> prepareParametersToGetVerticesForCheckin(String toscaElementId, String modifierId, String ownerId) {
1468 Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGetParameters = new HashMap<>();
1469 verticesToGetParameters.put(toscaElementId, new ImmutablePair<>(GraphPropertyEnum.UNIQUE_ID, JsonParseFlagEnum.ParseMetadata));
1470 verticesToGetParameters.put(modifierId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1471 verticesToGetParameters.put(ownerId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1472 return verticesToGetParameters;
1475 private Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> prepareParametersToGetVerticesForRequestCertification(String toscaElementId, String modifierId, String ownerId) {
1476 Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGetParameters = new HashMap<>();
1477 verticesToGetParameters.put(toscaElementId, new ImmutablePair<>(GraphPropertyEnum.UNIQUE_ID, JsonParseFlagEnum.ParseAll));
1478 verticesToGetParameters.put(modifierId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1479 verticesToGetParameters.put(ownerId, new ImmutablePair<>(GraphPropertyEnum.USERID, JsonParseFlagEnum.NoParse));
1480 return verticesToGetParameters;
1483 private Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> prepareParametersToGetVerticesForCheckout(String toscaElementId, String modifierId, String ownerId) {
1484 //Implementation is currently identical
1485 return prepareParametersToGetVerticesForRequestCertification(toscaElementId,modifierId, ownerId);
1488 private String getNextCertifiedVersion(String version) {
1489 String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
1490 Integer nextMajorVersion = Integer.parseInt(versionParts[0]) + 1;
1491 return nextMajorVersion + VERSION_DELIMITER + "0";
1494 private String getNextVersion(String currVersion) {
1495 String[] versionParts = currVersion.split(VERSION_DELIMITER_REGEXP);
1496 Integer minorVersion = Integer.parseInt(versionParts[1]) + 1;
1497 return versionParts[0] + VERSION_DELIMITER + minorVersion;
1500 private Integer getMinorVersion(String version) {
1501 String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
1502 return Integer.parseInt(versionParts[1]);
1505 private Integer getMajorVersion(String version) {
1506 String[] versionParts = version.split(VERSION_DELIMITER_REGEXP);
1507 return Integer.parseInt(versionParts[0]);
1510 private boolean isFirstCheckoutAfterCertification(String version) {
1511 return (Integer.parseInt(version.split(VERSION_DELIMITER_REGEXP)[0]) != 0 && Integer.parseInt(version.split(VERSION_DELIMITER_REGEXP)[1]) == 1);
1514 public Either<ToscaElement, StorageOperationStatus> forceCerificationOfToscaElement(String toscaElementId, String modifierId, String ownerId, String currVersion) {
1515 Either<GraphVertex, StorageOperationStatus> resultUpdate = null;
1516 Either<ToscaElement, StorageOperationStatus> result = null;
1517 GraphVertex toscaElement = null;
1518 GraphVertex modifier = null;
1521 Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesRes = janusGraphDao
1522 .getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
1523 if (getVerticesRes.isRight()) {
1524 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
1525 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVerticesRes.right().value()));
1527 if (result == null) {
1528 toscaElement = getVerticesRes.left().value().get(toscaElementId);
1529 modifier = getVerticesRes.left().value().get(modifierId);
1530 owner = getVerticesRes.left().value().get(ownerId);
1532 StorageOperationStatus status = handleRelationsUponForceCertification(toscaElement, modifier, owner);
1533 if (status != StorageOperationStatus.OK) {
1534 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to handle relations on certification request for tosca element {}. Status is {}. ", toscaElement.getUniqueId(), status);
1537 if (result == null) {
1538 LifecycleStateEnum nextState = LifecycleStateEnum.CERTIFIED;
1540 toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1541 toscaElement.addMetadataProperty(GraphPropertyEnum.VERSION, getNextCertifiedVersion(currVersion));
1543 resultUpdate = updateToscaElementVertexMetadataPropertiesAndJson(toscaElement);
1544 if (resultUpdate.isRight()) {
1545 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to set lifecycle for tosca elememt {} to state {}, error: {}", toscaElement.getUniqueId(), nextState, resultUpdate.right().value());
1546 result = Either.right(resultUpdate.right().value());
1549 if (result == null) {
1550 ToscaElementOperation operation = getToscaElementOperation(toscaElement.getLabel());
1551 result = operation.getToscaElement(toscaElement.getUniqueId());
1555 } catch (Exception e) {
1556 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occured during request certification tosca element {}. {}", toscaElementId, e.getMessage());
1561 private StorageOperationStatus handleRelationsUponForceCertification(GraphVertex toscaElement, GraphVertex modifier, GraphVertex owner) {
1563 StorageOperationStatus result = null;
1564 JanusGraphOperationStatus status = janusGraphDao
1565 .replaceEdgeLabel(owner.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE);
1566 if (status != JanusGraphOperationStatus.OK) {
1567 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
1569 if (result == null) {
1570 Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
1571 properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.CERTIFIED);
1572 status = janusGraphDao
1573 .createEdge(modifier, toscaElement, EdgeLabelEnum.STATE, properties);
1574 if (status != JanusGraphOperationStatus.OK) {
1575 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "failed to create edge. Status is {}", status);
1576 result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
1579 if (result == null) {
1580 result = StorageOperationStatus.OK;
1585 private StorageOperationStatus updateEdgeToCatalogRootByUndoCheckout(JanusGraphVertex preV, GraphVertex curV) {
1587 return updateEdgeToCatalogRoot(null, curV);
1589 String uniqueIdPreVer = (String) janusGraphDao
1590 .getProperty((JanusGraphVertex) preV, GraphPropertyEnum.UNIQUE_ID.getProperty());
1591 LifecycleStateEnum state = LifecycleStateEnum.findState((String) janusGraphDao
1592 .getProperty(preV, GraphPropertyEnum.STATE.getProperty()));
1593 if (state == LifecycleStateEnum.CERTIFIED) {
1594 return updateEdgeToCatalogRoot(null, curV);
1596 return janusGraphDao.getVertexById(uniqueIdPreVer)
1597 .either(l -> updateEdgeToCatalogRoot(l, curV),
1598 DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
1601 private StorageOperationStatus updateEdgeToCatalogRoot(GraphVertex newVersionV, GraphVertex prevVersionV) {
1602 Either<GraphVertex, JanusGraphOperationStatus> catalog = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT);
1603 if (catalog.isRight()) {
1604 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch catalog vertex. error {}", catalog.right().value());
1605 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(catalog.right().value());
1607 GraphVertex catalogV = catalog.left().value();
1608 if (newVersionV != null) {
1609 Boolean isAbstract = (Boolean) newVersionV.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
1611 if ( isAbstract == null || !isAbstract ) {
1612 // no new vertex, only delete previous
1613 JanusGraphOperationStatus
1614 result = janusGraphDao
1615 .createEdge(catalogV, newVersionV, EdgeLabelEnum.CATALOG_ELEMENT, null);
1616 if (result != JanusGraphOperationStatus.OK) {
1617 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge from {} to catalog vertex. error {}", newVersionV.getUniqueId(), result);
1618 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
1622 if (prevVersionV != null) {
1623 Boolean isAbstract = (Boolean) prevVersionV.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
1624 if (isAbstract == null || !isAbstract) {
1625 // if prev == null -> new resource was added
1626 Either<Edge, JanusGraphOperationStatus> deleteResult = janusGraphDao
1627 .deleteEdge(catalogV, prevVersionV, EdgeLabelEnum.CATALOG_ELEMENT);
1628 if (deleteResult.isRight()) {
1629 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete edge from {} to catalog vertex. error {}", prevVersionV.getUniqueId(), deleteResult.right().value());
1630 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteResult.right().value());
1634 return StorageOperationStatus.OK;