af067403e77d2410925abc1b46a1c9124cf9ba35
[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.mig1911;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
25 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
26 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
27 import org.openecomp.sdc.asdctool.migration.tasks.InstanceMigrationBase;
28 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
29 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
30 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
31 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
32 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
33 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
34 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
43
44 import java.math.BigInteger;
45 import java.util.Collection;
46 import java.util.EnumMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.function.Predicate;
50 import java.util.stream.Collectors;
51
52 @Component
53 public class SdcDeploymentArtTimeOutMigration extends InstanceMigrationBase implements Migration {
54
55     private static final Logger log = LoggerFactory.getLogger(SdcDeploymentArtTimeOutMigration.class);
56     private static Integer defaultTimeOut = 120;
57
58     public SdcDeploymentArtTimeOutMigration(JanusGraphDao janusGraphDao) {
59         super(janusGraphDao);
60     }
61
62     @Override
63     public String description() {
64         return "update instance deployment artifact timeOut to default value 120 minutes";
65     }
66
67     @Override
68     public DBVersion getVersion() {
69         return DBVersion.from(BigInteger.valueOf(1911), BigInteger.valueOf(0));
70     }
71
72     @Override
73     public MigrationResult migrate() {
74         StorageOperationStatus status = updateDeploymentArtifactTimeOut();
75         return status == StorageOperationStatus.OK ?
76             MigrationResult.success()
77             : MigrationResult.error("failed to update instance deployment artifact timeOut. Error : " + status);
78     }
79
80     protected StorageOperationStatus updateDeploymentArtifactTimeOut() {
81         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
82         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
83         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
84         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
85         Either<List<GraphVertex>, JanusGraphOperationStatus> byCriteria = janusGraphDao
86             .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch,
87                 JsonParseFlagEnum.ParseAll);
88         return byCriteria.either(this::proceed, this::handleError);
89     }
90
91     @Override
92     protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) {
93         StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
94         GraphVertex containerV = getVertexById(containerVorig.getUniqueId());
95
96         if (containerV == null) {
97             log.error("Unexpected null value for `containerV`");
98         } else {
99             try {
100                 Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao
101                     .getChildVertex(containerV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, JsonParseFlagEnum.ParseAll);
102                 GraphVertex instDeployArt = childVertex.left().value();
103                 Collection<MapArtifactDataDefinition> values = (Collection<MapArtifactDataDefinition>) instDeployArt
104                     .getJson().values();
105                 List<ArtifactDataDefinition> artifactDataDefinitionsList = values.stream()
106                     .map(f -> f.getMapToscaDataDefinition().values())
107                     .flatMap(f -> f.stream().filter(isRelevantArtifact()))
108                     .collect(Collectors.toList());
109                 artifactDataDefinitionsList.forEach(t -> t.setTimeout(defaultTimeOut));
110                 status = updateVertexAndCommit(instDeployArt);
111
112             } catch (NullPointerException e) {
113                 log.error(
114                     "Null Pointer Exception occurred - this mean we have zombie vertex, migration task will continue anyway",
115                     e);
116                 status = StorageOperationStatus.OK;
117             } catch (Exception e) {
118                 //it is happy flow as well
119                 log.error("Exception occurred:", e);
120                 log.error(
121                     "Migration task will continue anyway, please find below vertex details related to this exception",
122                     e);
123                 log.error("containerV.getUniqueId() {} ---> ", containerV.getUniqueId());
124
125                 status = StorageOperationStatus.OK;
126             } finally {
127                 if (status != StorageOperationStatus.OK) {
128                     janusGraphDao.rollback();
129                     log.info("failed to update vertex ID {} ", containerV.getUniqueId());
130                     if (status == StorageOperationStatus.NOT_FOUND) {
131                         //it is happy flow as well
132                         status = StorageOperationStatus.OK;
133                     }
134                 } else {
135                     log.info("vertex ID {} successfully updated", containerV.getUniqueId());
136                 }
137             }
138         }
139
140         return status;
141     }
142
143     private static Predicate<ArtifactDataDefinition> isRelevantArtifact() {
144
145         return p -> ((p.getArtifactType().equals(ArtifactTypeEnum.HEAT.getType()) || p.getArtifactType()
146             .equals(ArtifactTypeEnum.HEAT_VOL.getType()) || p.getArtifactType()
147             .equals(ArtifactTypeEnum.HEAT_NET.getType()))
148             && p.getTimeout() != defaultTimeOut);
149
150     }
151
152 }