9cf56cf325e530af3a70bfe0c313b26722c598cc
[sdc.git] /
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
83             .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch,
84                 JsonParseFlagEnum.ParseAll);
85         return byCriteria.either(this::proceed, this::handleError);
86     }
87
88     @Override
89     protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) {
90         StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
91         GraphVertex containerV = getVertexById(containerVorig.getUniqueId());
92
93         if (containerV == null) {
94             log.error("Unexpected null value for `containerV`");
95         } else {
96             try {
97
98                 //update edges to meet above change
99                 // update LS eges from RFC to  NOT_CERTIFIED_CHECKIN
100
101                 updateEdgeProperty(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name(),
102                     getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE));
103
104                 if (containerV.getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS)
105                     .equals(DistributionStatusEnum.DISTRIBUTION_APPROVED.name()) || containerV
106                     .getMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS)
107                     .equals(DistributionStatusEnum.DISTRIBUTION_REJECTED.name())) {
108
109                     // update vertex state property from DISTRIBUTION_APPROVED/REJECTED to DISTRIBUTION_NOT_APPROVED state
110
111                     Map<GraphPropertyEnum, Object> metadataProperties = containerV.getMetadataProperties();
112                     metadataProperties.put(GraphPropertyEnum.DISTRIBUTION_STATUS,
113                         DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
114                     containerV.setMetadataProperties(metadataProperties);
115
116                     //update edges to meet above change
117                     //delete LAST_DISTRIBUTION_STATE_MODIFIER edge
118
119                     removeEdges(
120                         getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER));
121
122                 }
123
124                 status = updateVertexAndCommit(containerV);
125
126             } catch (NullPointerException e) {
127                 log.error(
128                     "Null Pointer Exception occurred - this mean we have zombie vertex, migration task will continue anyway",
129                     e);
130                 status = StorageOperationStatus.EXEUCTION_FAILED;
131             } catch (Exception e) {
132                 //it is happy flow as well
133                 log.error("Exception occurred:", e);
134                 log.error(
135                     "Migration task will continue anyway, please find below vertex details related to this exception",
136                     e);
137                 log.error("containerV.getUniqueId() ---> {}  ", containerV.getUniqueId());
138
139             } finally {
140                 if (status != StorageOperationStatus.OK) {
141                     janusGraphDao.rollback();
142                     log.info("failed to update vertex ID {} ", containerV.getUniqueId());
143                     log.info("Storage Operation Status {}", status.toString());
144                 } else {
145                     log.info("vertex ID {} successfully updated", containerV.getUniqueId());
146                 }
147             }
148         }
149
150         return status;
151     }
152
153
154 }