Catalog alignment
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / mig2002 / SdcCollapsingRolesRFCstateMigration.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.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;
44
45 import java.math.BigInteger;
46 import java.util.EnumMap;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50
51 @Component
52 public class SdcCollapsingRolesRFCstateMigration extends InstanceMigrationBase implements Migration {
53
54     private static final Logger log = LoggerFactory.getLogger(SdcCollapsingRolesRFCstateMigration.class);
55
56     public SdcCollapsingRolesRFCstateMigration(JanusGraphDao janusGraphDao) {
57         super(janusGraphDao);
58     }
59
60     @Override
61     public String description() {
62         return "update Service state from READY_FOR_CERTIFICATION to NOT_CERTIFIED_CHECKOUT state ";
63     }
64
65     @Override
66     public DBVersion getVersion() {
67         return DBVersion.from(BigInteger.valueOf(2002), BigInteger.valueOf(0));
68     }
69
70     @Override
71     public MigrationResult migrate() {
72         StorageOperationStatus status = updateServiceLifeCycleState();
73         return status == StorageOperationStatus.OK ?
74                 MigrationResult.success() : MigrationResult.error("failed to service state. Error : " + status);
75     }
76
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);
86     }
87
88     @Override
89     protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) {
90         StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
91         GraphVertex containerV = getVertexById(containerVorig.getUniqueId());
92         try {
93
94             // update vertex state property from READY_FOR_CERTIFICATION to NOT_CERTIFIED_CHECKIN state
95
96             Map<GraphPropertyEnum, Object> metadataProperties = containerV.getMetadataProperties();
97             metadataProperties.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
98             containerV.setMetadataProperties(metadataProperties);
99
100             //update edges to meet above change
101
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);
104
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));
108             } else {
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);
114
115             }
116
117             status = updateVertexAndCommit(containerV);
118
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 ?" +
126                     "" +
127                     "" +
128                     ", please find below vertex details related to this exception", e);
129             if (containerV != null) {
130                 log.error("containerV.getUniqueId() ---> {}  ", containerV.getUniqueId());
131             }
132
133         } finally {
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());
138             } else {
139                 log.info("vertex ID {} successfully updated", containerV.getUniqueId());
140             }
141
142         }
143         return status;
144     }
145
146
147 }