2d18a62122eb40d41a5947be6dc80a4073d3cde7
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  * Modifications copyright (c) 2021 Nokia
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process;
19
20 import org.apache.commons.collections4.CollectionUtils;
21 import org.apache.commons.collections4.MapUtils;
22 import org.openecomp.core.translator.datatypes.TranslatorOutput;
23 import org.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
26 import org.openecomp.core.validation.util.MessageContainerUtil;
27 import org.openecomp.sdc.common.errors.Messages;
28 import org.openecomp.sdc.common.utils.SdcCommon;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.datatypes.error.ErrorMessage;
31 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
32 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
33 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
34 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
35 import org.openecomp.sdc.logging.api.Logger;
36 import org.openecomp.sdc.logging.api.LoggerFactory;
37 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
38 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
39 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.*;
41 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
43 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
47 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
48 import org.openecomp.sdc.versioning.dao.types.Version;
49
50 import java.io.ByteArrayInputStream;
51 import java.io.File;
52 import java.io.FileInputStream;
53 import java.io.InputStream;
54 import java.util.*;
55
56 public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler {
57   private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class);
58
59   private final CandidateService candidateService =
60       CandidateServiceFactory.getInstance().createInterface();
61
62   @Override
63   public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
64                                                      OrchestrationTemplateCandidateData candidateData) {
65     String vspId = vspDetails.getId();
66     Version version = vspDetails.getVersion();
67     OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
68     UploadFileResponse uploadFileResponse = new UploadFileResponse();
69     Optional<FileContentHandler> fileContent = OrchestrationUtil
70         .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse,
71             candidateData.getContentData().array());
72     if (!fileContent.isPresent()) {
73       response.addStructureErrors(uploadFileResponse.getErrors());
74       return response;
75     }
76
77     Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
78     FileContentHandler fileContentMap = fileContent.get();
79     try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
80       addDummyHeatBase(zipFileManifest ,fileContentMap);
81     } catch (Exception e) {
82       LOGGER.error("Invalid package content", e);
83     }
84     FilesDataStructure structure =
85         JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class);
86
87     if (CollectionUtils.isNotEmpty(structure.getUnassigned())) {
88       response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE,
89           Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR);
90       return response;
91     }
92
93
94     ManifestContent zipManifestFile = readManifestFromZip(fileContentMap);
95     String manifest = null;
96     if (zipManifestFile == null) {
97       manifest = candidateService.createManifest(vspDetails, structure);
98     } else {
99       manifest = candidateService.createManifestFromExisting(vspDetails, structure, zipManifestFile);
100     }
101     fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes());
102
103     Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
104         .fetchZipFileByteArrayInputStream(
105             vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors);
106     if (!zipByteArrayInputStream.isPresent()) {
107       return response;
108     }
109
110     HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap);
111     Map<String, List<ErrorMessage>> errors = getErrors(response);
112     if (MapUtils.isNotEmpty(errors)) {
113       response.addStructureErrors(errors);
114       candidateService.updateValidationData(vspId, version, new ValidationStructureList(tree));
115       return response;
116     }
117     Map<String, String> componentsQuestionnaire = new HashMap<>();
118     Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
119     Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
120     Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
121     Map<String, ProcessEntity> processArtifact = new HashMap<>();
122
123     OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
124     Map<String, String> vspComponentIdNameInfoBeforeProcess =
125         orchestrationUtil.getVspComponentIdNameInfo(vspId, version);
126     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
127         orchestrationUtil.getComponentDependenciesBeforeDelete(vspId, version);
128     orchestrationUtil
129         .backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire,
130             componentNicsQuestionnaire, componentMibList, processes, processArtifact);
131
132     orchestrationUtil.deleteUploadDataAndContent(vspId, version);
133     orchestrationUtil
134         .saveUploadData(vspDetails, candidateData, zipByteArrayInputStream.get(), fileContentMap,
135             tree);
136
137     TranslatorOutput translatorOutput =
138         HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap);
139
140     ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
141     orchestrationUtil
142         .saveServiceModel(vspId, version, translatorOutput.getNonUnifiedToscaServiceModel(),
143             toscaServiceModel);
144     orchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire,
145         componentNicsQuestionnaire, componentMibList, processes, processArtifact);
146     orchestrationUtil.updateVspComponentDependencies(vspId, version,
147         vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete);
148
149     uploadFileResponse.addStructureErrors(uploadErrors);
150     candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
151     return response;
152   }
153
154   private ManifestContent readManifestFromZip(FileContentHandler fileContentMap) {
155     ManifestContent zipManifestFile = null;
156     try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
157       zipManifestFile = JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
158     } catch (Exception e) {
159       LOGGER.error("Invalid package content", e);
160     }
161     return zipManifestFile;
162   }
163
164   private FileContentHandler addDummyHeatBase(InputStream zipFileManifest, FileContentHandler fileContentMap) {
165     ManifestContent manifestContent =
166             JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
167     for (FileData fileData : manifestContent.getData()) {
168       if (Objects.nonNull(fileData.getType()) &&
169               fileData.getType().equals(FileData.Type.HELM) && fileData.getBase()) {
170         String filePath = new File("").getAbsolutePath() + "/resources";
171         File envFilePath = new File(filePath + "/base_template.env");
172         File baseFilePath = new File(filePath + "/base_template.yaml");
173         try (
174                 InputStream envStream = new FileInputStream(envFilePath);
175                 InputStream baseStream = new FileInputStream(baseFilePath);) {
176           fileContentMap.addFile("base_template_dummy_ignore.env", envStream);
177           fileContentMap.addFile("base_template_dummy_ignore.yaml", baseStream);
178         } catch (Exception e) {
179           LOGGER.error("File not found error {}", e);
180         }
181       }
182     }
183     return fileContentMap;
184   }
185
186   private Map<String, List<ErrorMessage>> getErrors(OrchestrationTemplateActionResponse
187                                                         orchestrationTemplateActionResponse) {
188     Map<String, List<ErrorMessage>> errors =
189         MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR,
190             orchestrationTemplateActionResponse.getErrors());
191     return MapUtils.isEmpty(errors) ? null : orchestrationTemplateActionResponse.getErrors();
192   }
193
194   private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response,
195                                                       FileContentHandler fileContentMap) {
196     VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response);
197     Map<String, List<ErrorMessage>> validationErrors =
198         ValidationManagerUtil.initValidationManager(fileContentMap).validate();
199     response.getErrors().putAll(validationErrors);
200
201     return OrchestrationUtil.createHeatTree(fileContentMap, validationErrors);
202   }
203 }