[SDC-29] rebase continue work to align source
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / migration / v1707 / ToscaTemplateRegeneration.java
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                         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentsRes = toscaOperationFacade.getToscaElement(componentV);
76                         if (getComponentsRes.isRight()) {
77                                 result = false;
78                                 break;
79                         }
80                         if(getComponentsRes.left().value().getToscaArtifacts()!=null && getComponentsRes.left().value().getToscaArtifacts().containsKey(ToscaExportHandler.ASSET_TOSCA_TEMPLATE)){
81                                 result = regenerateToscaTemplateArtifact(getComponentsRes.left().value(), componentV);
82                         }
83                         if(result){
84                                 toscaOperationFacade.commit();
85                         } else {
86                                 toscaOperationFacade.rollback();
87                                 break;
88                         }
89                 }
90                 return result;
91         }
92         
93         @SuppressWarnings("unchecked")
94         private boolean regenerateToscaTemplateArtifact(org.openecomp.sdc.be.model.Component parent, GraphVertex parentV) {
95                 boolean result = true;
96                 Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = null;
97                 ArtifactDataDefinition data = null;
98                 LOGGER.debug("tosca artifact generation");
99                 Either<ToscaRepresentation, ToscaError> exportComponent = toscaExportUtils.exportComponent(parent);
100                 if (exportComponent.isRight()) {
101                         LOGGER.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), exportComponent.right().value());
102                         result = false;
103                 }
104                 if(result){
105                         LOGGER.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
106                         toscaDataVertexRes = toscaOperationFacade.getTitanDao().getChildVertex(parentV, EdgeLabelEnum.TOSCA_ARTIFACTS, JsonParseFlagEnum.ParseJson);
107                         if(toscaDataVertexRes.isRight()){
108                                 LOGGER.debug("Failed to fetch tosca data vertex {} for component {}. Status is {}", EdgeLabelEnum.TOSCA_ARTIFACTS, parent.getUniqueId(), exportComponent.right().value());
109                                 result = false;
110                         }
111                 }
112                 if(result){
113                         data = parent.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
114                         data.setArtifactChecksum(GeneralUtility.calculateMD5ByByteArray(exportComponent.left().value().getMainYaml().getBytes()));
115                         
116                         ((Map<String, ArtifactDataDefinition>) toscaDataVertexRes.left().value().getJson()).put(ToscaExportHandler.ASSET_TOSCA_TEMPLATE, data);
117                         
118                         Either<GraphVertex, TitanOperationStatus>  updateVertexRes = toscaOperationFacade.getTitanDao().updateVertex(toscaDataVertexRes.left().value());
119                         if(updateVertexRes.isRight()){
120                                 result = false;
121                         }
122                 }
123                 if(result){
124                         ESArtifactData artifactData = new ESArtifactData(data.getEsId(), exportComponent.left().value().getMainYaml().getBytes());
125                         CassandraOperationStatus status = artifactCassandraDao.saveArtifact(artifactData);
126                         if(status != CassandraOperationStatus.OK){
127                                 result = false;
128                         }
129                 }
130                 return result;
131         }
132
133         public Either<List<GraphVertex>, StorageOperationStatus> getAllCertifiedComponents() {
134
135                 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
136                 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
137                 List<GraphVertex> components = null;
138                 Either<List<GraphVertex>, TitanOperationStatus> getVerticiesRes = toscaOperationFacade.getTitanDao().getByCriteria(null, propertiesToMatch,JsonParseFlagEnum.ParseMetadata);
139
140                 if (getVerticiesRes.isRight() && getVerticiesRes.right().value() != TitanOperationStatus.NOT_FOUND) {
141                         LOGGER.debug("Failed to fetch all certified components. Status is {}", getVerticiesRes.right().value());
142                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVerticiesRes.right().value()));
143                 }
144                 if(getVerticiesRes.isLeft()){
145                         components = getVerticiesRes.left().value();
146                 }
147                 return Either.left(components);
148         }
149         
150         @Override
151         public String description() {
152                 return "toscaTemplateRegeneration";
153         }
154 }