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.openecomp.sdc.asdctool.enums.DistributionStatusEnum;
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.List;
51 public class SdcCollapsingRolesCERTIFIEDstateMigration extends InstanceMigrationBase implements Migration {
53 private static final Logger log = LoggerFactory.getLogger(SdcCollapsingRolesCERTIFIEDstateMigration.class);
55 public SdcCollapsingRolesCERTIFIEDstateMigration(JanusGraphDao janusGraphDao) {
60 public String description() {
61 return "remove LS=READY_FOR_CERTIFICATION edge from service node + migrate DISTRIBUTION approved/rejected states to <waiting for distribution> state";
65 public DBVersion getVersion() {
66 return DBVersion.from(BigInteger.valueOf(2002), BigInteger.valueOf(0));
70 public MigrationResult migrate() {
71 StorageOperationStatus status = updateServiceLifeCycleState();
72 return status == StorageOperationStatus.OK ?
73 MigrationResult.success() : MigrationResult.error("failed to service state. Error : " + status);
76 protected StorageOperationStatus updateServiceLifeCycleState() {
77 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
78 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
79 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
80 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
81 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
82 Either<List<GraphVertex>, JanusGraphOperationStatus> byCriteria = janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
83 return byCriteria.either(this::proceed, this::handleError);
87 protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) {
88 StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
89 GraphVertex containerV = getVertexById(containerVorig.getUniqueId());
92 //update edges to meet above change
93 // update LS eges from RFC to NOT_CERTIFIED_CHECKIN
95 updateEdgeProperty(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name(), getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE));
97 if (containerV.getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS).equals(DistributionStatusEnum.DISTRIBUTION_APPROVED.name()) || containerV.getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS).equals(DistributionStatusEnum.DISTRIBUTION_REJECTED.name())) {
99 // update vertex state property from DISTRIBUTION_APPROVED/REJECTED to DISTRIBUTION_NOT_APPROVED state
101 Map<GraphPropertyEnum, Object> metadataProperties = containerV.getMetadataProperties();
102 metadataProperties.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
103 containerV.setMetadataProperties(metadataProperties);
105 //update edges to meet above change
106 //delete LAST_DISTRIBUTION_STATE_MODIFIER edge
108 removeEdges(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER));
112 status = updateVertexAndCommit(containerV);
114 } catch (NullPointerException e) {
115 log.error("Null Pointer Exception occurred - this mean we have zombie vertex, migration task will continue anyway", e);
116 status = StorageOperationStatus.EXEUCTION_FAILED;
117 } catch (Exception e) {
118 //it is happy flow as well
119 log.error("Exception occurred:", e);
120 log.error("Migration task will continue anyway, please find below vertex details related to this exception", e);
121 if (containerV != null) {
122 log.error("containerV.getUniqueId() ---> {} ", containerV.getUniqueId());
126 if (status != StorageOperationStatus.OK) {
127 janusGraphDao.rollback();
128 log.info("failed to update vertex ID {} ", containerV.getUniqueId());
129 log.info("Storage Operation Status {}", status.toString());
131 log.info("vertex ID {} successfully updated", containerV.getUniqueId());