[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-enrichment-lib / openecomp-sdc-enrichment-impl / src / main / java / org / openecomp / sdc / enrichment / impl / external / artifact / ProcessArtifactEnricher.java
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.vendorsoftwareproduct.dao.ProcessDao;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
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 VendorSoftwareProductDao vendorSoftwareProductDao;
32   //private ProcessArtifactDao processArtifactDao;
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) throws IOException {
39     Map<String, List<ErrorMessage>> errors = new HashMap<>();
40     String vspId = enrichmentInfo.getKey();
41     Version version = enrichmentInfo.getVersion();
42
43     Collection<ComponentEntity> components =
44         getVendorSoftwareProductDao().listComponents(vspId, version);
45     components.stream()
46         .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.stream()
77         .filter(entity -> entity.getType().equals(ProcessType.Lifecycle_Operations))
78         .forEach(entity -> {
79           ProcessArtifactEntity artifactEntity = new ProcessArtifactEntity(vspId, version,
80                   componentId, entity.getId());*/
81
82     processes.stream()
83         .filter(entity -> entity.getType().equals(ProcessType.Lifecycle_Operations))
84         .forEach(entity -> {
85           ProcessEntity artifactEntity = new ProcessEntity(vspId, version,
86               componentId, entity.getId());
87
88           ProcessEntity artifactProcessEntity = getProcessDao().get(artifactEntity);
89           //ProcessArtifactEntity artifact = getProcessArtifactDao().get(artifactEntity);
90           if (artifactProcessEntity != null) {
91             String componentName = componentEntity.getComponentCompositionData().getName();
92             String path = componentName + File.separator
93                 + ArtifactCategory.DEPLOYMENT.getDisplayName() + File.separator
94                 + "Lifecycle Operations" + File.separator + artifactProcessEntity.getArtifactName();
95
96             ComponentProcessInfo componentProcessInfo = new ComponentProcessInfo();
97             componentProcessInfo.setName(path);
98             componentProcessInfo.setContent(artifactProcessEntity.getArtifact().array());
99
100             ServiceArtifact processServiceArtifact = new ServiceArtifact();
101             processServiceArtifact.setVspId(vspId);
102             processServiceArtifact.setVersion(version);
103             enrichServiceArtifact(componentProcessInfo, processServiceArtifact, errors);
104           }
105         });
106
107     mdcDataDebugMessage.debugExitMessage(null, null);
108   }
109
110   void enrichServiceArtifact(ComponentProcessInfo componentProcessInfo,
111                              ServiceArtifact processServiceArtifact,
112                              Map<String, List<ErrorMessage>> errors) {
113
114
115     mdcDataDebugMessage.debugEntryMessage(null, null);
116
117     processServiceArtifact.setName(componentProcessInfo.getName());
118     processServiceArtifact.setContentData(FileUtils.toByteArray(componentProcessInfo.getContent()));
119     getEnrichedServiceModelDao().storeExternalArtifact(processServiceArtifact);
120     mdcDataDebugMessage.debugExitMessage(null, null);
121   }
122
123   private VendorSoftwareProductDao getVendorSoftwareProductDao() {
124     if (vendorSoftwareProductDao == null) {
125       vendorSoftwareProductDao = VendorSoftwareProductDaoFactory.getInstance().createInterface();
126     }
127     return vendorSoftwareProductDao;
128   }
129
130   private ProcessDao getProcessDao() {
131     if (processDao == null) {
132       processDao = ProcessDaoFactory.getInstance().createInterface();
133     }
134     return processDao;
135   }
136
137   private EnrichedServiceModelDao getEnrichedServiceModelDao() {
138
139     if (enrichedServiceModelDao == null) {
140       enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
141     }
142     return enrichedServiceModelDao;
143   }
144 }