re base code
[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.structure.HeatStructureTree;
31 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
32 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
33 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
34 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.*;
36 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
37 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
38 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
42 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
43 import org.openecomp.sdc.versioning.dao.types.Version;
44
45 import java.io.ByteArrayInputStream;
46 import java.util.*;
47
48 public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler {
49
50   private final CandidateService candidateService =
51       CandidateServiceFactory.getInstance().createInterface();
52
53   @Override
54   public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
55                                                      OrchestrationTemplateCandidateData candidateData) {
56     String vspId = vspDetails.getId();
57     Version version = vspDetails.getVersion();
58     OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
59     UploadFileResponse uploadFileResponse = new UploadFileResponse();
60     Optional<FileContentHandler> fileContent = OrchestrationUtil
61         .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse,
62             candidateData.getContentData().array());
63     if (!fileContent.isPresent()) {
64       response.addStructureErrors(uploadFileResponse.getErrors());
65       return response;
66     }
67
68     Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
69     FileContentHandler fileContentMap = fileContent.get();
70     FilesDataStructure structure =
71         JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class);
72
73     if (CollectionUtils.isNotEmpty(structure.getUnassigned())) {
74       response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE,
75           Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR);
76       return response;
77     }
78
79
80     String manifest = candidateService.createManifest(vspDetails, structure);
81     fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes());
82
83     Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
84         .fetchZipFileByteArrayInputStream(
85             vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors);
86     if (!zipByteArrayInputStream.isPresent()) {
87       return response;
88     }
89
90     HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap);
91     Map<String, List<ErrorMessage>> errors = getErrors(response);
92     if (MapUtils.isNotEmpty(errors)) {
93       response.addStructureErrors(errors);
94       candidateService.updateValidationData(vspId, version, new ValidationStructureList(tree));
95       return response;
96     }
97     Map<String, String> componentsQuestionnaire = new HashMap<>();
98     Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
99     Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
100     Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
101     Map<String, ProcessEntity> processArtifact = new HashMap<>();
102
103     OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
104     Map<String, String> vspComponentIdNameInfoBeforeProcess =
105         orchestrationUtil.getVspComponentIdNameInfo(vspId, version);
106     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
107         orchestrationUtil.getComponentDependenciesBeforeDelete(vspId, version);
108     orchestrationUtil
109         .backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire,
110             componentNicsQuestionnaire, componentMibList, processes, processArtifact);
111
112     orchestrationUtil.deleteUploadDataAndContent(vspId, version);
113     orchestrationUtil
114         .saveUploadData(vspDetails, candidateData, zipByteArrayInputStream.get(), fileContentMap,
115             tree);
116
117     TranslatorOutput translatorOutput =
118         HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap);
119
120     ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
121     orchestrationUtil
122         .saveServiceModel(vspId, version, translatorOutput.getNonUnifiedToscaServiceModel(),
123             toscaServiceModel);
124     orchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire,
125         componentNicsQuestionnaire, componentMibList, processes, processArtifact);
126     orchestrationUtil.updateVspComponentDependencies(vspId, version,
127         vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete);
128
129     uploadFileResponse.addStructureErrors(uploadErrors);
130     candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
131     return response;
132   }
133
134   private Map<String, List<ErrorMessage>> getErrors(OrchestrationTemplateActionResponse
135                                                         orchestrationTemplateActionResponse) {
136     Map<String, List<ErrorMessage>> errors =
137         MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR,
138             orchestrationTemplateActionResponse.getErrors());
139     return MapUtils.isEmpty(errors) ? null : orchestrationTemplateActionResponse.getErrors();
140   }
141
142   private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response,
143                                                       FileContentHandler fileContentMap) {
144     VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response);
145     Map<String, List<ErrorMessage>> validationErrors =
146         ValidationManagerUtil.initValidationManager(fileContentMap).validate();
147     response.getErrors().putAll(validationErrors);
148
149     return OrchestrationUtil.createHeatTree(fileContentMap, validationErrors);
150   }
151 }