Added oparent to sdc main
[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 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.enrichment.impl.external.artifact;
22
23 import org.openecomp.core.enrichment.types.ArtifactCategory;
24 import org.openecomp.core.enrichment.types.ComponentProcessInfo;
25 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
26 import org.openecomp.core.model.dao.EnrichedServiceModelDaoFactory;
27 import org.openecomp.core.model.types.ServiceArtifact;
28 import org.openecomp.core.utilities.file.FileUtils;
29 import org.openecomp.sdc.datatypes.error.ErrorMessage;
30 import org.openecomp.sdc.enrichment.EnrichmentInfo;
31 import org.openecomp.sdc.enrichment.inter.ExternalArtifactEnricherInterface;
32 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessType;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.util.Collection;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 public class ProcessArtifactEnricher implements ExternalArtifactEnricherInterface {
50
51   private ComponentDao componentDao;
52   private ProcessDao processDao;
53   private EnrichedServiceModelDao enrichedServiceModelDao;
54
55   @Override
56   public Map<String, List<ErrorMessage>> enrich(EnrichmentInfo enrichmentInfo,
57                                                 ToscaServiceModel serviceModel) throws IOException {
58     Map<String, List<ErrorMessage>> errors = new HashMap<>();
59     String vspId = enrichmentInfo.getKey();
60     Version version = enrichmentInfo.getVersion();
61
62     Collection<ComponentEntity> components =
63         getComponentDao().list(new ComponentEntity(vspId, version, null));
64     components.forEach(componentEntry -> errors.putAll(enrichComponent(componentEntry,
65         vspId, version)));
66
67     return errors;
68   }
69
70   Map<String, List<ErrorMessage>> enrichComponent(ComponentEntity componentEntry, String vspId,
71                                                   Version version) {
72     Map<String, List<ErrorMessage>> errors = new HashMap<>();
73     enrichComponentProcessArtifact(componentEntry, vspId, version, errors);
74     return errors;
75   }
76
77   void enrichComponentProcessArtifact(ComponentEntity componentEntity,
78                                       String vspId, Version version,
79                                       Map<String, List<ErrorMessage>> errors) {
80     String componentId = componentEntity.getId();
81     ProcessEntity processEntity = new ProcessEntity(vspId, version, componentId, null);
82     final Collection<ProcessEntity> processes = getProcessDao().list(processEntity);
83
84     processes.forEach(entity -> {
85       ProcessEntity artifactEntity = new ProcessEntity(vspId, version,
86           componentId, entity.getId());
87
88           ProcessEntity artifactProcessEntity = getProcessDao().getArtifact(artifactEntity);
89           if (artifactProcessEntity != null && ProcessType.Lifecycle_Operations.equals(
90               artifactProcessEntity.getType())
91               && artifactProcessEntity.getArtifactName() != null ) {
92             String componentName = componentEntity.getComponentCompositionData().getName();
93             String path = componentName + File.separator
94                 + ArtifactCategory.DEPLOYMENT.getDisplayName() + File.separator
95                 + "Lifecycle Operations" + File.separator + artifactProcessEntity.getArtifactName();
96
97             ComponentProcessInfo componentProcessInfo = new ComponentProcessInfo();
98             componentProcessInfo.setName(path);
99             componentProcessInfo.setContent(artifactProcessEntity.getArtifact().array());
100
101             ServiceArtifact processServiceArtifact = new ServiceArtifact();
102             processServiceArtifact.setVspId(vspId);
103             processServiceArtifact.setVersion(version);
104             enrichServiceArtifact(componentProcessInfo, processServiceArtifact);
105           }
106         });
107   }
108
109   void enrichServiceArtifact(ComponentProcessInfo componentProcessInfo,
110                              ServiceArtifact processServiceArtifact) {
111     processServiceArtifact.setName(componentProcessInfo.getName());
112     processServiceArtifact.setContentData(FileUtils.toByteArray(componentProcessInfo.getContent()));
113     getEnrichedServiceModelDao().storeExternalArtifact(processServiceArtifact);
114   }
115
116   private ComponentDao getComponentDao() {
117     if (componentDao == null) {
118       componentDao = ComponentDaoFactory.getInstance().createInterface();
119     }
120     return componentDao;
121   }
122
123   private ProcessDao getProcessDao() {
124     if (processDao == null) {
125       processDao = ProcessDaoFactory.getInstance().createInterface();
126     }
127     return processDao;
128   }
129
130   private EnrichedServiceModelDao getEnrichedServiceModelDao() {
131
132     if (enrichedServiceModelDao == null) {
133       enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
134     }
135     return enrichedServiceModelDao;
136   }
137 }