7d009fe15831a6491d6427c7938433432d0740bd
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.impl.migration.v1707;
22
23 import java.util.EnumMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.commons.collections.CollectionUtils;
28 import org.openecomp.sdc.asdctool.impl.migration.Migration1707Task;
29 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
30 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
31 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
32 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
33 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
34 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
35 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
36 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
37 import org.openecomp.sdc.be.model.LifecycleStateEnum;
38 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
39 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
40 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
41 import org.openecomp.sdc.be.resources.data.ESArtifactData;
42 import org.openecomp.sdc.be.tosca.ToscaError;
43 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
44 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
45 import org.openecomp.sdc.common.util.GeneralUtility;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.stereotype.Component;
50
51 import fj.data.Either;
52
53 @Component("toscaTemplateRegeneration")
54 public class ToscaTemplateRegeneration implements Migration1707Task {
55
56         private static Logger LOGGER = LoggerFactory.getLogger(ToscaTemplateRegeneration.class);
57         
58         @Autowired
59         protected ArtifactCassandraDao artifactCassandraDao;
60         
61         @Autowired
62         private ToscaExportHandler toscaExportUtils;
63
64         @Autowired
65     private ToscaOperationFacade toscaOperationFacade;
66     
67         @Override
68         public boolean migrate() {
69                 boolean result = true;
70                 Either<List<GraphVertex>, StorageOperationStatus> getAllCertifiedComponentsRes;
71                 try{
72                         getAllCertifiedComponentsRes = getAllCertifiedComponents();
73                         if(getAllCertifiedComponentsRes.isRight()){
74                                 result = false;
75                         }
76                         if(result && CollectionUtils.isNotEmpty(getAllCertifiedComponentsRes.left().value())){
77                                 result = regenerateToscaTemplateArtifacts(getAllCertifiedComponentsRes.left().value());
78                         }
79                 } catch(Exception e){
80                         LOGGER.error("The exception {} has been occured upon tosca template regeneration migration. ", e);
81                         result = false;
82                 } finally {
83                         if(result){
84                                 toscaOperationFacade.commit();
85                         } else {
86                                 toscaOperationFacade.rollback();
87                         }
88                 }
89                 return result;
90         }
91
92         private boolean regenerateToscaTemplateArtifacts(List<GraphVertex> components) {
93                 boolean result = true;
94                 for(GraphVertex componentV : components){
95                         String componentId = componentV.getUniqueId();
96                         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentsRes = toscaOperationFacade.getToscaElement(componentId);
97                         if (getComponentsRes.isRight()) {
98                                 result = false;
99                                 break;
100                         }
101                         if(getComponentsRes.left().value().getToscaArtifacts()!=null && getComponentsRes.left().value().getToscaArtifacts().containsKey(ToscaExportHandler.ASSET_TOSCA_TEMPLATE)){
102                                 result = regenerateToscaTemplateArtifact(getComponentsRes.left().value(), componentV);
103                         }
104                         if(result){
105                                 toscaOperationFacade.commit();
106                         } else {
107                                 toscaOperationFacade.rollback();
108                                 break;
109                         }
110                 }
111                 return result;
112         }
113         
114         @SuppressWarnings("unchecked")
115         private boolean regenerateToscaTemplateArtifact(org.openecomp.sdc.be.model.Component parent, GraphVertex parentV) {
116                 boolean result = true;
117                 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = null;
118                 ArtifactDataDefinition data = null;
119                 LOGGER.debug("tosca artifact generation");
120                 Either<ToscaRepresentation, ToscaError> exportComponent = toscaExportUtils.exportComponent(parent);
121                 if (exportComponent.isRight()) {
122                         LOGGER.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), exportComponent.right().value());
123                         result = false;
124                 }
125                 if(result){
126                         LOGGER.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
127                         toscaDataVertexRes = toscaOperationFacade.getTitanDao().getChildVertex(parentV, EdgeLabelEnum.TOSCA_ARTIFACTS, JsonParseFlagEnum.ParseJson);
128                         if(toscaDataVertexRes.isRight()){
129                                 LOGGER.debug("Failed to fetch tosca data vertex {} for component {}. Status is {}", EdgeLabelEnum.TOSCA_ARTIFACTS, parent.getUniqueId(), exportComponent.right().value());
130                                 result = false;
131                         }
132                 }
133                 if(result){
134                         data = parent.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
135                         data.setArtifactChecksum(GeneralUtility.calculateMD5ByByteArray(exportComponent.left().value().getMainYaml().getBytes()));
136                         
137                         ((Map<String, ArtifactDataDefinition>) toscaDataVertexRes.left().value().getJson()).put(ToscaExportHandler.ASSET_TOSCA_TEMPLATE, data);
138                         
139                         Either<GraphVertex, TitanOperationStatus>  updateVertexRes = toscaOperationFacade.getTitanDao().updateVertex(toscaDataVertexRes.left().value());
140                         if(updateVertexRes.isRight()){
141                                 result = false;
142                         }
143                 }
144                 if(result){
145                         ESArtifactData artifactData = new ESArtifactData(data.getEsId(), exportComponent.left().value().getMainYaml().getBytes());
146                         CassandraOperationStatus status = artifactCassandraDao.saveArtifact(artifactData);
147                         if(status != CassandraOperationStatus.OK){
148                                 result = false;
149                         }
150                 }
151                 return result;
152         }
153
154         public Either<List<GraphVertex>, StorageOperationStatus> getAllCertifiedComponents() {
155
156                 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
157                 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
158                 List<GraphVertex> components = null;
159                 Either<List<GraphVertex>, TitanOperationStatus> getVerticiesRes = toscaOperationFacade.getTitanDao().getByCriteria(null, propertiesToMatch,JsonParseFlagEnum.NoParse);
160
161                 if (getVerticiesRes.isRight() && getVerticiesRes.right().value() != TitanOperationStatus.NOT_FOUND) {
162                         LOGGER.debug("Failed to fetch all certified components. Status is {}", getVerticiesRes.right().value());
163                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVerticiesRes.right().value()));
164                 }
165                 if(getVerticiesRes.isLeft()){
166                         components = getVerticiesRes.left().value();
167                 }
168                 return Either.left(components);
169         }
170         
171         @Override
172         public String description() {
173                 return "toscaTemplateRegeneration";
174         }
175 }