2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.asdctool.migration.tasks.mig2002;
23 import fj.data.Either;
24 import org.apache.tinkerpop.gremlin.structure.Direction;
25 import org.janusgraph.core.JanusGraphVertex;
26 import org.openecomp.sdc.asdctool.enums.LifecycleStateEnum;
27 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
28 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
29 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
30 import org.openecomp.sdc.asdctool.migration.tasks.InstanceMigrationBase;
31 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
32 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
33 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
34 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
36 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
37 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
39 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Component;
45 import java.math.BigInteger;
46 import java.util.EnumMap;
47 import java.util.HashMap;
48 import java.util.List;
52 public class SdcCollapsingRolesRFCstateMigration extends InstanceMigrationBase implements Migration {
54 private static final Logger log = LoggerFactory.getLogger(SdcCollapsingRolesRFCstateMigration.class);
56 public SdcCollapsingRolesRFCstateMigration(JanusGraphDao janusGraphDao) {
61 public String description() {
62 return "update Service state from READY_FOR_CERTIFICATION to NOT_CERTIFIED_CHECKOUT state ";
66 public DBVersion getVersion() {
67 return DBVersion.from(BigInteger.valueOf(2002), BigInteger.valueOf(0));
71 public MigrationResult migrate() {
72 StorageOperationStatus status = updateServiceLifeCycleState();
73 return status == StorageOperationStatus.OK ?
74 MigrationResult.success() : MigrationResult.error("failed to service state. Error : " + status);
77 protected StorageOperationStatus updateServiceLifeCycleState() {
78 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
79 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
80 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.READY_FOR_CERTIFICATION.name());
81 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
82 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
83 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
84 Either<List<GraphVertex>, JanusGraphOperationStatus> byCriteria = janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
85 return byCriteria.either(this::proceed, this::handleError);
89 protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) {
90 StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
91 GraphVertex containerV = getVertexById(containerVorig.getUniqueId());
94 // update vertex state property from READY_FOR_CERTIFICATION to NOT_CERTIFIED_CHECKIN state
96 Map<GraphPropertyEnum, Object> metadataProperties = containerV.getMetadataProperties();
97 metadataProperties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
98 containerV.setMetadataProperties(metadataProperties);
100 //update edges to meet above change
102 List<JanusGraphVertex> stateEdgesOutVertexList = getVertexByEdgeSide(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.STATE), EdgeSide.OUT);
103 List<JanusGraphVertex> lastStateEdgesOutVertexList = getVertexByEdgeSide(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE), EdgeSide.OUT);
105 if (sameUser(stateEdgesOutVertexList, lastStateEdgesOutVertexList)) {
106 updateEdgeProperty(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name(), getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.STATE));
107 removeEdges(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE));
109 removeEdges(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.STATE));
110 removeEdges(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE));
111 Map<EdgePropertyEnum, Object> edgeProperties = new HashMap<>();
112 edgeProperties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
113 janusGraphDao.createEdge(lastStateEdgesOutVertexList.get(0), containerV.getVertex(), EdgeLabelEnum.STATE, edgeProperties);
117 status = updateVertexAndCommit(containerV);
119 } catch (NullPointerException e) {
120 log.error("Null Pointer Exception occurred - this mean we have zombie vertex, migration task will continue anyway", e);
121 status = StorageOperationStatus.EXEUCTION_FAILED;
122 } catch (Exception e) {
123 //it is happy flow as well
124 log.error("Exception occurred:", e);
125 log.error("Migration task will ?" +
128 ", please find below vertex details related to this exception", e);
129 if (containerV != null) {
130 log.error("containerV.getUniqueId() ---> {} ", containerV.getUniqueId());
134 if (status != StorageOperationStatus.OK) {
135 janusGraphDao.rollback();
136 log.info("failed to update vertex ID {} ", containerV.getUniqueId());
137 log.info("Storage Operation Status {}", status.toString());
139 log.info("vertex ID {} successfully updated", containerV.getUniqueId());