Catalog alignment
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / mig2002 / SdcCollapsingRolesCERTIFIEDstateMigration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.migration.tasks.mig2002;
22
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;
44
45 import java.math.BigInteger;
46 import java.util.EnumMap;
47 import java.util.List;
48 import java.util.Map;
49
50 @Component
51 public class SdcCollapsingRolesCERTIFIEDstateMigration extends InstanceMigrationBase implements Migration {
52
53     private static final Logger log = LoggerFactory.getLogger(SdcCollapsingRolesCERTIFIEDstateMigration.class);
54
55     public SdcCollapsingRolesCERTIFIEDstateMigration(JanusGraphDao janusGraphDao) {
56         super(janusGraphDao);
57     }
58
59     @Override
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";
62     }
63
64     @Override
65     public DBVersion getVersion() {
66         return DBVersion.from(BigInteger.valueOf(2002), BigInteger.valueOf(0));
67     }
68
69     @Override
70     public MigrationResult migrate() {
71         StorageOperationStatus status = updateServiceLifeCycleState();
72         return status == StorageOperationStatus.OK ?
73                 MigrationResult.success() : MigrationResult.error("failed to service state. Error : " + status);
74     }
75
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);
84     }
85
86     @Override
87     protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) {
88         StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
89         GraphVertex containerV = getVertexById(containerVorig.getUniqueId());
90         try {
91
92             //update edges to meet above change
93             // update LS eges from RFC to  NOT_CERTIFIED_CHECKIN
94
95             updateEdgeProperty(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name(), getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE));
96
97             if (containerV.getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS).equals(DistributionStatusEnum.DISTRIBUTION_APPROVED.name()) || containerV.getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS).equals(DistributionStatusEnum.DISTRIBUTION_REJECTED.name())) {
98
99                 // update vertex state property from DISTRIBUTION_APPROVED/REJECTED to DISTRIBUTION_NOT_APPROVED state
100
101                 Map<GraphPropertyEnum, Object> metadataProperties = containerV.getMetadataProperties();
102                 metadataProperties.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
103                 containerV.setMetadataProperties(metadataProperties);
104
105                 //update edges to meet above change
106                 //delete LAST_DISTRIBUTION_STATE_MODIFIER edge
107
108                 removeEdges(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER));
109
110             }
111
112             status = updateVertexAndCommit(containerV);
113
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());
123             }
124
125         } finally {
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());
130             } else {
131                 log.info("vertex ID {} successfully updated", containerV.getUniqueId());
132             }
133
134         }
135         return status;
136     }
137
138
139 }