Catalog alignment
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / mig2002 / SdcCollapsingRolesCIPstateMigration.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.apache.tinkerpop.gremlin.structure.Edge;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.openecomp.sdc.asdctool.enums.LifecycleStateEnum;
28 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
29 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
30 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
31 import org.openecomp.sdc.asdctool.migration.tasks.InstanceMigrationBase;
32 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
33 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
34 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
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.datatypes.enums.ComponentTypeEnum;
40 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.stereotype.Component;
45
46 import java.math.BigInteger;
47 import java.util.EnumMap;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.List;
51 import java.util.Map;
52
53 @Component
54 public class SdcCollapsingRolesCIPstateMigration extends InstanceMigrationBase implements Migration {
55
56     private static final Logger log = LoggerFactory.getLogger(SdcCollapsingRolesCIPstateMigration.class);
57
58     public SdcCollapsingRolesCIPstateMigration(JanusGraphDao janusGraphDao) {
59         super(janusGraphDao);
60     }
61
62     @Override
63     public String description() {
64         return "update Service state from CERTIFICATION_IN_PROGRES to NOT_CERTIFIED_CHECKOUT state ";
65     }
66
67     @Override
68     public DBVersion getVersion() {
69         return DBVersion.from(BigInteger.valueOf(2002), BigInteger.valueOf(0));
70     }
71
72     @Override
73     public MigrationResult migrate() {
74         StorageOperationStatus status = updateServiceLifeCycleState();
75         return status == StorageOperationStatus.OK ?
76                 MigrationResult.success() : MigrationResult.error("failed to service state. Error : " + status);
77     }
78
79     protected StorageOperationStatus updateServiceLifeCycleState() {
80         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
81         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
82         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS.name());
83         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
84         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
85         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
86         Either<List<GraphVertex>, JanusGraphOperationStatus> byCriteria = janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
87         return byCriteria.either(this::proceed, this::handleError);
88     }
89
90     @Override
91     protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) {
92         StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
93         GraphVertex containerV = getVertexById(containerVorig.getUniqueId());
94         try {
95
96             // update vertex state property from READY_FOR_CERTIFICATION to NOT_CERTIFIED_CHECKIN state
97
98             Map<GraphPropertyEnum, Object> metadataProperties = containerV.getMetadataProperties();
99             metadataProperties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
100             containerV.setMetadataProperties(metadataProperties);
101
102              //update edges to meet above change
103             // remove STATE and LAST_MODIFIER edges
104             removeEdges(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.STATE));
105             removeEdges(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_MODIFIER));
106
107             //find designer with LS = NOT_CERTIFIED_CHECKIN
108             Vertex relevantDesigner = findRelevantDesigner(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE));
109             removeEdges(getVertexEdge(containerV, Direction.IN, EdgeLabelEnum.LAST_STATE));
110             Map<EdgePropertyEnum, Object> edgeProperties = new HashMap<>();
111             edgeProperties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
112             JanusGraphOperationStatus createSTedgeStatus = janusGraphDao.createEdge(relevantDesigner, containerV.getVertex(), EdgeLabelEnum.STATE, edgeProperties);
113             JanusGraphOperationStatus createLMedgeStatus = janusGraphDao.createEdge(relevantDesigner, containerV.getVertex(), EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
114
115             status = updateVertexAndCommit(containerV);
116
117         } catch (NullPointerException e) {
118             log.error("Null Pointer Exception occurred - this mean we have zombie vertex, migration task will continue anyway", e);
119             status = StorageOperationStatus.EXEUCTION_FAILED;
120         } catch (Exception e) {
121             //it is happy flow as well
122             log.error("Exception occurred:", e);
123             log.error("Migration task will continue anyway, please find below vertex details related to this exception", e);
124             if (containerV != null) {
125                 log.error("containerV.getUniqueId() ---> {}  ", containerV.getUniqueId());
126             }
127
128         } finally {
129             if (status != StorageOperationStatus.OK) {
130                 janusGraphDao.rollback();
131                 log.info("failed to update vertex ID {} ", containerV.getUniqueId());
132                 log.info("Storage Operation Status {}", status.toString());
133             } else {
134                 log.info("vertex ID {} successfully updated", containerV.getUniqueId());
135             }
136
137         }
138         return status;
139     }
140
141     private Vertex findRelevantDesigner(Iterator<Edge> edges) {
142         Vertex vertex = null;
143         while (edges.hasNext()) {
144             Edge edge = edges.next();
145             String state = (String) janusGraphDao.getProperty(edge, EdgePropertyEnum.STATE);
146             if (state.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name())) {
147                 vertex = edge.outVertex();
148             }
149         }
150         return vertex;
151     }
152
153 }