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