61d1799aa0bd340ddc92c8c452b099a1e67845b3
[sdc.git] /
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property.
3
4   * Modifications Copyright (c) 2018 Verizon Property.
5   * Modifications Copyright (c) 2019 Nordix Foundation.
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *     http://www.apache.org/licenses/LICENSE-2.0
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
15  * See the License for the specific language governing permissions and
16
17  * limitations under the License.
18
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
22
23 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
24
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.Optional;
28 import org.apache.commons.lang3.tuple.Pair;
29 import org.openecomp.core.utilities.file.FileContentHandler;
30 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
31 import org.openecomp.sdc.common.errors.CoreException;
32 import org.openecomp.sdc.common.errors.Messages;
33 import org.openecomp.sdc.common.utils.CommonUtil;
34 import org.openecomp.sdc.common.utils.SdcCommon;
35 import org.openecomp.sdc.datatypes.error.ErrorLevel;
36 import org.openecomp.sdc.datatypes.error.ErrorMessage;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
39 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
45
46 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler
47     implements OrchestrationTemplateFileHandler {
48
49   @Override
50   public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse,
51                                                         byte[] uploadedFileData) {
52     FileContentHandler contentMap = null;
53     List<String> folderList;
54     try {
55       Pair<FileContentHandler, List<String>> fileContentMapFromOrchestrationCandidateZip =
56           CommonUtil.getFileContentMapFromOrchestrationCandidateZip(uploadedFileData);
57       contentMap = fileContentMapFromOrchestrationCandidateZip.getKey();
58       folderList = fileContentMapFromOrchestrationCandidateZip.getRight();
59       Validator validator = ValidatorFactory.getValidator(contentMap);
60       uploadFileResponse.addStructureErrors(validator.validateContent(contentMap, folderList));
61     } catch (IOException exception) {
62       logger.error(exception.getMessage(), exception);
63       uploadFileResponse.addStructureError(
64           SdcCommon.UPLOAD_FILE,
65           new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
66     } catch (CoreException coreException) {
67       logger.error(coreException.getMessage(), coreException);
68       uploadFileResponse.addStructureError(
69           SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
70     }
71
72     return Optional.ofNullable(contentMap);
73   }
74
75   @Override
76   protected boolean updateCandidateData(final VspDetails vspDetails,
77                                         final OnboardPackageInfo onboardPackageInfo,
78                                         final CandidateService candidateService,
79                                         final UploadFileResponse uploadFileResponse,
80                                         final FileContentHandler contentMap) {
81     try {
82       final OnboardPackage csarPackage = onboardPackageInfo.getOnboardPackage();
83       final OnboardPackage originalOnboardPackage = onboardPackageInfo.getOriginalOnboardPackage();
84       candidateService.updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(),
85           new OrchestrationTemplateCandidateData(csarPackage.getFileContent(),
86               "", csarPackage.getFileExtension(),
87               csarPackage.getFilename(), originalOnboardPackage.getFilename(), originalOnboardPackage.getFileExtension(),
88               originalOnboardPackage.getFileContent()));
89     } catch (final Exception exception) {
90       logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
91           getHandlerType().toString()), exception);
92       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE,
93           new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
94       return true;
95     }
96     return false;
97   }
98
99   @Override
100   protected OnboardingTypesEnum getHandlerType() {
101     return OnboardingTypesEnum.CSAR;
102   }
103
104 }