089cfe7a82312cdfb6b2f8978eafe1e9cdf237d8
[sdc.git] /
1 package org.openecomp.sdc.enrichment.impl.external.artifact;
2
3 import org.openecomp.core.enrichment.types.ArtifactCategory;
4 import org.openecomp.core.enrichment.types.ComponentProcessInfo;
5 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
6 import org.openecomp.core.model.dao.EnrichedServiceModelDaoFactory;
7 import org.openecomp.core.model.types.ServiceArtifact;
8 import org.openecomp.core.utilities.file.FileUtils;
9 import org.openecomp.sdc.datatypes.error.ErrorMessage;
10 import org.openecomp.sdc.enrichment.EnrichmentInfo;
11 import org.openecomp.sdc.enrichment.inter.ExternalArtifactEnricherInterface;
12 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
13 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
19 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
20 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessType;
21 import org.openecomp.sdc.versioning.dao.types.Version;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 public class ProcessArtifactEnricher implements ExternalArtifactEnricherInterface {
31
32   private ComponentDao componentDao;
33   private ProcessDao processDao;
34   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
35   private EnrichedServiceModelDao enrichedServiceModelDao;
36
37   @Override
38   public Map<String, List<ErrorMessage>> enrich(EnrichmentInfo enrichmentInfo,
39                                                 ToscaServiceModel serviceModel) throws IOException {
40     Map<String, List<ErrorMessage>> errors = new HashMap<>();
41     String vspId = enrichmentInfo.getKey();
42     Version version = enrichmentInfo.getVersion();
43
44     Collection<ComponentEntity> components =
45         getComponentDao().list(new ComponentEntity(vspId, version, null));
46     components.forEach(componentEntry -> errors.putAll(enrichComponent(componentEntry,
47         vspId, version)));
48
49     return errors;
50   }
51
52   Map<String, List<ErrorMessage>> enrichComponent(ComponentEntity componentEntry, String vspId,
53                                                   Version version) {
54     mdcDataDebugMessage.debugEntryMessage("LifeCycleOperationArtifactEnricher vspId ",
55         vspId);
56
57     Map<String, List<ErrorMessage>> errors = new HashMap<>();
58     enrichComponentProcessArtifact(componentEntry, vspId, version, errors);
59
60     mdcDataDebugMessage.debugExitMessage("LifeCycleOperationArtifactEnricher vspId ",
61         vspId);
62     return errors;
63   }
64
65   void enrichComponentProcessArtifact(ComponentEntity componentEntity,
66                                       String vspId, Version version,
67                                       Map<String, List<ErrorMessage>> errors) {
68
69
70     mdcDataDebugMessage.debugEntryMessage(null, null);
71
72     String componentId = componentEntity.getId();
73     ProcessEntity processEntity = new ProcessEntity(vspId, version, componentId, null);
74     final Collection<ProcessEntity> processes = getProcessDao().list(processEntity);
75
76     processes.forEach(entity -> {
77       ProcessEntity artifactEntity = new ProcessEntity(vspId, version,
78           componentId, entity.getId());
79
80           ProcessEntity artifactProcessEntity = getProcessDao().get(artifactEntity);
81           if (artifactProcessEntity != null && ProcessType.Lifecycle_Operations.equals(
82               artifactProcessEntity.getType())
83               && artifactProcessEntity.getArtifactName() != null ) {
84             String componentName = componentEntity.getComponentCompositionData().getName();
85             String path = componentName + File.separator
86                 + ArtifactCategory.DEPLOYMENT.getDisplayName() + File.separator
87                 + "Lifecycle Operations" + File.separator + artifactProcessEntity.getArtifactName();
88
89             ComponentProcessInfo componentProcessInfo = new ComponentProcessInfo();
90             componentProcessInfo.setName(path);
91             componentProcessInfo.setContent(artifactProcessEntity.getArtifact().array());
92
93             ServiceArtifact processServiceArtifact = new ServiceArtifact();
94             processServiceArtifact.setVspId(vspId);
95             processServiceArtifact.setVersion(version);
96             enrichServiceArtifact(componentProcessInfo, processServiceArtifact);
97           }
98         });
99
100     mdcDataDebugMessage.debugExitMessage(null);
101   }
102
103   void enrichServiceArtifact(ComponentProcessInfo componentProcessInfo,
104                              ServiceArtifact processServiceArtifact) {
105
106
107     mdcDataDebugMessage.debugEntryMessage(null);
108
109     processServiceArtifact.setName(componentProcessInfo.getName());
110     processServiceArtifact.setContentData(FileUtils.toByteArray(componentProcessInfo.getContent()));
111     getEnrichedServiceModelDao().storeExternalArtifact(processServiceArtifact);
112     mdcDataDebugMessage.debugExitMessage(null);
113   }
114
115   private ComponentDao getComponentDao() {
116     if (componentDao == null) {
117       componentDao = ComponentDaoFactory.getInstance().createInterface();
118     }
119     return componentDao;
120   }
121
122   private ProcessDao getProcessDao() {
123     if (processDao == null) {
124       processDao = ProcessDaoFactory.getInstance().createInterface();
125     }
126     return processDao;
127   }
128
129   private EnrichedServiceModelDao getEnrichedServiceModelDao() {
130
131     if (enrichedServiceModelDao == null) {
132       enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
133     }
134     return enrichedServiceModelDao;
135   }
136 }