30a8965809c371c0072b340eff856b20d1762eb1
[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.tosca.datatypes.ToscaServiceModel;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
19 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessType;
20 import org.openecomp.sdc.versioning.dao.types.Version;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 public class ProcessArtifactEnricher implements ExternalArtifactEnricherInterface {
30
31   private ComponentDao componentDao;
32   private ProcessDao processDao;
33   private EnrichedServiceModelDao enrichedServiceModelDao;
34
35   @Override
36   public Map<String, List<ErrorMessage>> enrich(EnrichmentInfo enrichmentInfo,
37                                                 ToscaServiceModel serviceModel) throws IOException {
38     Map<String, List<ErrorMessage>> errors = new HashMap<>();
39     String vspId = enrichmentInfo.getKey();
40     Version version = enrichmentInfo.getVersion();
41
42     Collection<ComponentEntity> components =
43         getComponentDao().list(new ComponentEntity(vspId, version, null));
44     components.forEach(componentEntry -> errors.putAll(enrichComponent(componentEntry,
45         vspId, version)));
46
47     return errors;
48   }
49
50   Map<String, List<ErrorMessage>> enrichComponent(ComponentEntity componentEntry, String vspId,
51                                                   Version version) {
52     Map<String, List<ErrorMessage>> errors = new HashMap<>();
53     enrichComponentProcessArtifact(componentEntry, vspId, version, errors);
54     return errors;
55   }
56
57   void enrichComponentProcessArtifact(ComponentEntity componentEntity,
58                                       String vspId, Version version,
59                                       Map<String, List<ErrorMessage>> errors) {
60     String componentId = componentEntity.getId();
61     ProcessEntity processEntity = new ProcessEntity(vspId, version, componentId, null);
62     final Collection<ProcessEntity> processes = getProcessDao().list(processEntity);
63
64     processes.forEach(entity -> {
65       ProcessEntity artifactEntity = new ProcessEntity(vspId, version,
66           componentId, entity.getId());
67
68           ProcessEntity artifactProcessEntity = getProcessDao().getArtifact(artifactEntity);
69           if (artifactProcessEntity != null && ProcessType.Lifecycle_Operations.equals(
70               artifactProcessEntity.getType())
71               && artifactProcessEntity.getArtifactName() != null ) {
72             String componentName = componentEntity.getComponentCompositionData().getName();
73             String path = componentName + File.separator
74                 + ArtifactCategory.DEPLOYMENT.getDisplayName() + File.separator
75                 + "Lifecycle Operations" + File.separator + artifactProcessEntity.getArtifactName();
76
77             ComponentProcessInfo componentProcessInfo = new ComponentProcessInfo();
78             componentProcessInfo.setName(path);
79             componentProcessInfo.setContent(artifactProcessEntity.getArtifact().array());
80
81             ServiceArtifact processServiceArtifact = new ServiceArtifact();
82             processServiceArtifact.setVspId(vspId);
83             processServiceArtifact.setVersion(version);
84             enrichServiceArtifact(componentProcessInfo, processServiceArtifact);
85           }
86         });
87   }
88
89   void enrichServiceArtifact(ComponentProcessInfo componentProcessInfo,
90                              ServiceArtifact processServiceArtifact) {
91     processServiceArtifact.setName(componentProcessInfo.getName());
92     processServiceArtifact.setContentData(FileUtils.toByteArray(componentProcessInfo.getContent()));
93     getEnrichedServiceModelDao().storeExternalArtifact(processServiceArtifact);
94   }
95
96   private ComponentDao getComponentDao() {
97     if (componentDao == null) {
98       componentDao = ComponentDaoFactory.getInstance().createInterface();
99     }
100     return componentDao;
101   }
102
103   private ProcessDao getProcessDao() {
104     if (processDao == null) {
105       processDao = ProcessDaoFactory.getInstance().createInterface();
106     }
107     return processDao;
108   }
109
110   private EnrichedServiceModelDao getEnrichedServiceModelDao() {
111
112     if (enrichedServiceModelDao == null) {
113       enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
114     }
115     return enrichedServiceModelDao;
116   }
117 }