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 / OrchestrationTemplateZipHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
22
23 import org.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
25 import org.openecomp.sdc.common.errors.Messages;
26 import org.openecomp.sdc.common.utils.SdcCommon;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
31 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
32 import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
34
35 import java.util.Optional;
36
37 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
38
39 public class OrchestrationTemplateZipHandler extends BaseOrchestrationTemplateHandler
40     implements OrchestrationTemplateFileHandler {
41
42   @Override
43   public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse,
44                                                         byte[] uploadedFileData) {
45     return OrchestrationUtil
46         .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse, uploadedFileData);
47   }
48
49   @Override
50   protected boolean updateCandidateData(VspDetails vspDetails, byte[] uploadedFileData,
51                                         FileContentHandler contentMap,
52                                         String fileSuffix, String networkPackageName,
53                                         CandidateService candidateService,
54                                         UploadFileResponse uploadFileResponse) {
55     try {
56       OrchestrationTemplateCandidateData candidateData =
57           new CandidateEntityBuilder(candidateService)
58               .buildCandidateEntityFromZip(vspDetails, uploadedFileData, contentMap,
59                   uploadFileResponse.getErrors());
60       candidateData.setFileSuffix(fileSuffix);
61       candidateData.setFileName(networkPackageName);
62
63       candidateService
64           .updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(), candidateData);
65     } catch (Exception exception) {
66       logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
67           getHandlerType().toString()), exception);
68       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE,
69           new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
70       return true;
71     }
72     return false;
73   }
74
75   @Override
76   protected OnboardingTypesEnum getHandlerType() {
77     return OnboardingTypesEnum.ZIP;
78   }
79 }