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