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