Added oparent to sdc main
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / OrchestrationTemplateCSARHandler.java
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 org.apache.commons.lang3.tuple.Pair;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.common.errors.Messages;
28 import org.openecomp.sdc.common.utils.CommonUtil;
29 import org.openecomp.sdc.common.utils.SdcCommon;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.datatypes.error.ErrorMessage;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
34 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator;
35 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
38
39 import java.io.IOException;
40 import java.nio.ByteBuffer;
41 import java.util.List;
42 import java.util.Optional;
43
44 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
45
46 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler
47     implements OrchestrationTemplateFileHandler {
48
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 (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(VspDetails vspDetails, byte[] uploadedFileData,
78                                         FileContentHandler contentMap,
79                                         String fileSuffix, String networkPackageName,
80                                         CandidateService candidateService,
81                                         UploadFileResponse uploadFileResponse) {
82     try {
83       candidateService.updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(),
84           new OrchestrationTemplateCandidateData(ByteBuffer.wrap(uploadedFileData), "", fileSuffix,
85               networkPackageName));
86     } catch (Exception exception) {
87       logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
88           getHandlerType().toString()), exception);
89       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE,
90           new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
91       return true;
92     }
93     return false;
94   }
95
96   @Override
97   protected OnboardingTypesEnum getHandlerType() {
98     return OnboardingTypesEnum.CSAR;
99   }
100
101 }