8f0029aa83045719891880477dbda614c143f60e
[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.common.zip.exception.ZipException;
36 import org.openecomp.sdc.datatypes.error.ErrorLevel;
37 import org.openecomp.sdc.datatypes.error.ErrorMessage;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator;
41 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
46
47 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler
48     implements OrchestrationTemplateFileHandler {
49
50   @Override
51   public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse,
52                                                         byte[] uploadedFileData) {
53     FileContentHandler contentMap = null;
54     List<String> folderList;
55     try {
56       Pair<FileContentHandler, List<String>> fileContentMapFromOrchestrationCandidateZip =
57           CommonUtil.getFileContentMapFromOrchestrationCandidateZip(uploadedFileData);
58       contentMap = fileContentMapFromOrchestrationCandidateZip.getKey();
59       folderList = fileContentMapFromOrchestrationCandidateZip.getRight();
60       Validator validator = ValidatorFactory.getValidator(contentMap);
61       uploadFileResponse.addStructureErrors(validator.validateContent(contentMap, folderList));
62     } catch (final ZipException | IOException exception) {
63       logger.error(exception.getMessage(), exception);
64       uploadFileResponse.addStructureError(
65           SdcCommon.UPLOAD_FILE,
66           new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
67     } catch (CoreException coreException) {
68       logger.error(coreException.getMessage(), coreException);
69       uploadFileResponse.addStructureError(
70           SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
71     }
72
73     return Optional.ofNullable(contentMap);
74   }
75
76   @Override
77   protected boolean updateCandidateData(final VspDetails vspDetails,
78                                         final OnboardPackageInfo onboardPackageInfo,
79                                         final CandidateService candidateService,
80                                         final UploadFileResponse uploadFileResponse,
81                                         final FileContentHandler contentMap) {
82     try {
83       final OnboardPackage csarPackage = onboardPackageInfo.getOnboardPackage();
84       final OnboardPackage originalOnboardPackage = onboardPackageInfo.getOriginalOnboardPackage();
85       candidateService.updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(),
86           new OrchestrationTemplateCandidateData(csarPackage.getFileContent(),
87               "", csarPackage.getFileExtension(),
88               csarPackage.getFilename(), originalOnboardPackage.getFilename(), originalOnboardPackage.getFileExtension(),
89               originalOnboardPackage.getFileContent()));
90     } catch (final Exception exception) {
91       logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
92           getHandlerType().toString()), exception);
93       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE,
94           new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
95       return true;
96     }
97     return false;
98   }
99
100   @Override
101   protected OnboardingTypesEnum getHandlerType() {
102     return OnboardingTypesEnum.CSAR;
103   }
104
105 }