Add collaboration feature
[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.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 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         getComponentDao().list(new ComponentEntity(vspId, version, null));
45     components.forEach(componentEntry -> errors.putAll(enrichComponent(componentEntry,
46         vspId, version)));
47
48     return errors;
49   }
50
51   Map<String, List<ErrorMessage>> enrichComponent(ComponentEntity componentEntry, String vspId,
52                                                   Version version) {
53     mdcDataDebugMessage.debugEntryMessage("LifeCycleOperationArtifactEnricher vspId ",
54         vspId);
55
56     Map<String, List<ErrorMessage>> errors = new HashMap<>();
57     enrichComponentProcessArtifact(componentEntry, vspId, version, errors);
58
59     mdcDataDebugMessage.debugExitMessage("LifeCycleOperationArtifactEnricher vspId ",
60         vspId);
61     return errors;
62   }
63
64   void enrichComponentProcessArtifact(ComponentEntity componentEntity,
65                                       String vspId, Version version,
66                                       Map<String, List<ErrorMessage>> errors) {
67
68
69     mdcDataDebugMessage.debugEntryMessage(null, null);
70
71     String componentId = componentEntity.getId();
72     ProcessEntity processEntity = new ProcessEntity(vspId, version, componentId, null);
73     final Collection<ProcessEntity> processes = getProcessDao().list(processEntity);
74
75     /*processes.stream()
76         .filter(entity -> entity.getType().equals(ProcessType.Lifecycle_Operations))
77         .forEach(entity -> {
78           ProcessArtifactEntity artifactEntity = new ProcessArtifactEntity(vspId, version,
79                   componentId, entity.getId());*/
80
81     processes.forEach(entity -> {
82       ProcessEntity artifactEntity = new ProcessEntity(vspId, version,
83           componentId, entity.getId());
84
85           ProcessEntity artifactProcessEntity = getProcessDao().get(artifactEntity);
86           //ProcessArtifactEntity artifact = getProcessArtifactDao().get(artifactEntity);
87           if (artifactProcessEntity != null && ProcessType.Lifecycle_Operations.equals(
88               artifactProcessEntity.getType())
89               && artifactProcessEntity.getArtifactName() != null ) {
90             String componentName = componentEntity.getComponentCompositionData().getName();
91             String path = componentName + File.separator
92                 + ArtifactCategory.DEPLOYMENT.getDisplayName() + File.separator
93                 + "Lifecycle Operations" + File.separator + artifactProcessEntity.getArtifactName();
94
95             ComponentProcessInfo componentProcessInfo = new ComponentProcessInfo();
96             componentProcessInfo.setName(path);
97             componentProcessInfo.setContent(artifactProcessEntity.getArtifact().array());
98
99             ServiceArtifact processServiceArtifact = new ServiceArtifact();
100             processServiceArtifact.setVspId(vspId);
101             processServiceArtifact.setVersion(version);
102             enrichServiceArtifact(componentProcessInfo, processServiceArtifact, errors);
103           }
104         });
105
106     mdcDataDebugMessage.debugExitMessage(null);
107   }
108
109   void enrichServiceArtifact(ComponentProcessInfo componentProcessInfo,
110                              ServiceArtifact processServiceArtifact,
111                              Map<String, List<ErrorMessage>> errors) {
112
113
114     mdcDataDebugMessage.debugEntryMessage(null);
115
116     processServiceArtifact.setName(componentProcessInfo.getName());
117     processServiceArtifact.setContentData(FileUtils.toByteArray(componentProcessInfo.getContent()));
118     getEnrichedServiceModelDao().storeExternalArtifact(processServiceArtifact);
119     mdcDataDebugMessage.debugExitMessage(null);
120   }
121
122   private ComponentDao getComponentDao() {
123     if (componentDao == null) {
124       componentDao = ComponentDaoFactory.getInstance().createInterface();
125     }
126     return componentDao;
127   }
128
129   private ProcessDao getProcessDao() {
130     if (processDao == null) {
131       processDao = ProcessDaoFactory.getInstance().createInterface();
132     }
133     return processDao;
134   }
135
136   private EnrichedServiceModelDao getEnrichedServiceModelDao() {
137
138     if (enrichedServiceModelDao == null) {
139       enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
140     }
141     return enrichedServiceModelDao;
142   }
143 }