Create new VSP, onboard from TOSCA file - UI
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / OrchestrationUtil.java
1 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
2
3 import org.apache.commons.collections4.CollectionUtils;
4 import org.openecomp.core.model.dao.ServiceModelDao;
5 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
6 import org.openecomp.core.util.UniqueValueUtil;
7 import org.openecomp.core.utilities.file.FileContentHandler;
8 import org.openecomp.core.utilities.file.FileUtils;
9 import org.openecomp.core.utilities.json.JsonUtil;
10 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
11 import org.openecomp.sdc.common.errors.CoreException;
12 import org.openecomp.sdc.common.errors.Messages;
13 import org.openecomp.sdc.common.utils.CommonUtil;
14 import org.openecomp.sdc.common.utils.SdcCommon;
15 import org.openecomp.sdc.datatypes.error.ErrorLevel;
16 import org.openecomp.sdc.datatypes.error.ErrorMessage;
17 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
18 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
19 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
20 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
21 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
22 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
23 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
24 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.MonitoringUploadDaoFactory;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadData;
39 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
44 import org.openecomp.sdc.versioning.dao.types.Version;
45
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.nio.ByteBuffer;
49 import java.util.Collection;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Objects;
54 import java.util.Optional;
55
56 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.GENERAL_COMPONENT_ID;
57 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME;
58
59 public class OrchestrationUtil {
60
61     public static final String ORCHESTRATION_CONFIG_NAMESPACE = "orchestration";
62     public static final String ORCHESTRATION_Impl_Key = "orchestration_impl";
63
64
65     private static VendorSoftwareProductDao vendorSoftwareProductDao =
66         VendorSoftwareProductDaoFactory.getInstance().createInterface();
67     private static NicDao nicDao = NicDaoFactory.getInstance().createInterface();
68     private static ComponentArtifactDao componentArtifactDao = MonitoringUploadDaoFactory.getInstance().createInterface();
69     private static ProcessDao processDao = ProcessDaoFactory.getInstance().createInterface();
70     private static OrchestrationTemplateDao orchestrationTemplateDataDao =
71         OrchestrationTemplateDaoFactory.getInstance().createInterface();
72     private static ComponentDao componentDao =
73         ComponentDaoFactory.getInstance().createInterface();
74     private static ServiceModelDao serviceModelDao = ServiceModelDaoFactory.getInstance()
75         .createInterface();
76     private static CompositionEntityDataManager compositionEntityDataManager =
77         CompositionEntityDataManagerFactory.getInstance().createInterface();
78     private static CompositionDataExtractor compositionDataExtractor =
79         CompositionDataExtractorFactory.getInstance().createInterface();
80
81     public static Optional<FileContentHandler> getFileContentMap(OnboardingTypesEnum type,
82                                                                  UploadFileResponse uploadFileResponse,
83                                                                  byte[] uploadedFileData) {
84         FileContentHandler contentMap = null;
85         try {
86             contentMap = CommonUtil.validateAndUploadFileContent(type, uploadedFileData);
87         } catch (IOException exception) {
88             uploadFileResponse.addStructureError(
89                     SdcCommon.UPLOAD_FILE,
90                     new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
91         } catch (CoreException coreException) {
92             uploadFileResponse.addStructureError(
93                     SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
94         }
95         return Optional.ofNullable(contentMap);
96     }
97
98     public static void backupComponentsQuestionnaireBeforeDelete(String vspId, Version activeVersion,
99                                                            Map<String, String> componentsQustanniare,
100                                                            Map<String, Map<String, String>>
101                                                                componentNicsQustanniare,
102                                                            Map<String, Collection<ComponentMonitoringUploadEntity>>
103                                                                componentMibList,
104                                                            Map<String, Collection<ProcessEntity>>
105                                                                componentProcesses,
106                                                            Map<String, ProcessEntity> processArtifact) {
107         //backup VSP processes
108         backupProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID,
109             componentProcesses, processArtifact);
110         Collection<ComponentEntity> componentsCompositionAndQuestionnaire = vendorSoftwareProductDao
111             .listComponentsCompositionAndQuestionnaire(vspId,
112                 activeVersion);
113         componentsCompositionAndQuestionnaire.forEach(componentEntity -> {
114             String componentName = componentEntity.getComponentCompositionData().getName();
115             componentsQustanniare.put(componentName, componentEntity
116                 .getQuestionnaireData());
117             Collection<NicEntity>
118                 nics = nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null));
119             //backup mib
120             Collection<ComponentMonitoringUploadEntity> componentMib =
121                 componentArtifactDao.listArtifacts(new
122                     ComponentMonitoringUploadEntity(vspId, activeVersion, componentEntity.getId(),
123                     null));
124             if (CollectionUtils.isNotEmpty(componentMib)) {
125                 componentMibList.put(componentName,componentMib);
126             }
127
128             //backup component processes
129             backupProcess(vspId, activeVersion, componentEntity.getId(), componentName,
130                 componentProcesses, processArtifact);
131             if (CollectionUtils.isNotEmpty(nics)) {
132                 Map<String, String> nicsQustanniare = new HashMap<>();
133                 nics.forEach(nicEntity -> {
134                     NicEntity nic = nicDao.get(new NicEntity(vspId, activeVersion, componentEntity.getId(),
135                         nicEntity.getId()));
136                     NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, activeVersion,
137                         componentEntity.getId(), nicEntity.getId());
138
139                     nicsQustanniare
140                         .put(nicEntity.getNicCompositionData().getName(),
141                             nicQuestionnaire.getQuestionnaireData());
142                 });
143                 componentNicsQustanniare.put(componentName, nicsQustanniare);
144             }
145         });
146     }
147
148     private static void backupProcess(String vspId, Version activeVersion, String componentId,
149                                String componentName, Map<String,
150         Collection<ProcessEntity>> processes,
151                                Map<String, ProcessEntity> processArtifact) {
152         Collection<ProcessEntity> processList = vendorSoftwareProductDao.listProcesses(vspId,
153             activeVersion, componentId);
154         if (!processList.isEmpty()) {
155             processes.put(componentName, processList);
156             processList.forEach(process -> {
157                 //ProcessArtifactEntity artifact = vendorSoftwareProductDao.getProcessArtifact(vspId,
158                 //    activeVersion, componentId, process.getId());
159                 ProcessEntity artifact =
160                     processDao.get(new ProcessEntity(vspId, activeVersion, componentId, process.getId()));
161                 if (artifact.getArtifact() != null) {
162                     processArtifact.put(process.getId(), artifact);
163                 }
164             });
165         }
166     }
167
168     public static void retainComponentQuestionnaireData(String vspId, Version activeVersion,
169                                                   Map<String, String> componentsQustanniare,
170                                                   Map<String, Map<String, String>>
171                                                       componentNicsQustanniare,
172                                                   Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList,
173                                                   Map<String, Collection<ProcessEntity>> processes,
174                                                   Map<String, ProcessEntity> processArtifact) {
175         //VSP processes
176         restoreProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID, processes,
177             processArtifact);
178         Collection<ComponentEntity>
179             components = vendorSoftwareProductDao.listComponents(vspId, activeVersion);
180         components.forEach(componentEntity -> {
181             String componentName = componentEntity.getComponentCompositionData().getName();
182             if (componentsQustanniare.containsKey(componentName)) {
183                 componentDao.updateQuestionnaireData(vspId, activeVersion,
184                     componentEntity.getId(),
185                     componentsQustanniare.get(componentEntity.getComponentCompositionData()
186                         .getName()));
187                 if (componentNicsQustanniare.containsKey(componentName)) {
188                     Map<String, String> nicsQustanniare = componentNicsQustanniare.get(componentName);
189                     Collection<NicEntity>
190                         nics =
191                         nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null));
192                     nics.forEach(nicEntity -> {
193                         if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) {
194                             nicDao.updateQuestionnaireData(vspId, activeVersion,
195                                 componentEntity.getId(), nicEntity.getId(),
196                                 nicsQustanniare.get(nicEntity.getNicCompositionData().getName()));
197                         }
198                     });
199                 }
200                 //MIB //todo add for VES_EVENTS
201                 if (componentMibList.containsKey(componentName)) {
202                     Collection<ComponentMonitoringUploadEntity> mibList =
203                         componentMibList.get(componentName);
204                     mibList.forEach(mib -> {
205                         mib.setComponentId(componentEntity.getId());
206                         componentArtifactDao.create(mib);
207                     });
208                 }
209                 //VFC processes
210                 restoreProcess(vspId, activeVersion, componentEntity.getId(), componentName, processes,
211                     processArtifact);
212             }
213         });
214     }
215
216     private static void restoreProcess(String vspId, Version activeVersion, String componentId,
217                                 String componentName,
218                                 Map<String, Collection<ProcessEntity>> processes,
219                                 Map<String, ProcessEntity> processArtifact) {
220         if (processes.containsKey(componentName)) {
221             Collection<ProcessEntity> processList = processes.get(componentName);
222             processList.forEach(process -> {
223                 //Reatin VFC process
224                 if (!GENERAL_COMPONENT_ID.equals(componentId) && processArtifact.containsKey(process.getId
225                     ())) {
226                     ProcessEntity artifact = processArtifact.get(process.getId());
227                     artifact.setComponentId(componentId);
228                     UniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, activeVersion.toString(),
229                         componentId, process.getName());
230                     vendorSoftwareProductDao.createProcess(artifact);
231                 }
232             });
233         }
234     }
235
236     public static void deleteUploadDataAndContent(String vspId, Version version) {
237         //fixme change this when more tables are zusammenized
238         vendorSoftwareProductDao.deleteUploadData(vspId, version);
239     }
240
241     public static void saveUploadData(String vspId, Version activeVersion, InputStream uploadedFileData,
242                                       FileContentHandler fileContentMap, HeatStructureTree tree) {
243         Map<String, Object> manifestAsMap =
244             fileContentMap.containsFile(SdcCommon.MANIFEST_NAME) ?
245             (Map<String, Object>) JsonUtil.json2Object(fileContentMap.getFileContent(
246                 SdcCommon.MANIFEST_NAME), Map.class)
247             : new HashMap<>();
248
249         UploadData uploadData = new UploadData();
250         uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData)));
251         uploadData.setValidationDataStructure(new ValidationStructureList(tree));
252         uploadData.setPackageName(Objects.isNull(manifestAsMap.get("name")) ? null :
253             (String) manifestAsMap.get("name"));
254         uploadData.setPackageVersion(Objects.isNull(manifestAsMap.get("version")) ? null :
255             (String) manifestAsMap.get("version"));
256         orchestrationTemplateDataDao.updateOrchestrationTemplateData(vspId, uploadData);
257     }
258
259     public static void saveServiceModel(String vspId,
260                                         Version version,
261                                         ToscaServiceModel serviceModelToExtract,
262                                         ToscaServiceModel serviceModelToStore){
263         if (serviceModelToExtract != null) {
264             serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore);
265             //Extracting the compostion data from the output service model of the first phase of
266             // translation
267             compositionEntityDataManager.saveCompositionData(vspId, version,
268                 compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract));
269 //            OrchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire,
270 //                componentNicsQuestionnaire, componentMibList, processes, processArtifact);
271         }
272     }
273
274     public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap,
275                                                    Map<String, List<ErrorMessage>> validationErrors){
276         HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
277         heatTreeManager.createTree();
278         heatTreeManager.addErrors(validationErrors);
279         return heatTreeManager.getTree();
280     }
281
282 }