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