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 / BaseOrchestrationTemplateHandler.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.apache.commons.collections4.MapUtils;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
27 import org.openecomp.sdc.common.errors.Messages;
28 import org.openecomp.sdc.common.utils.SdcCommon;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.datatypes.error.ErrorMessage;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
34 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
36
37 import java.io.InputStream;
38 import java.util.Optional;
39
40 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
41
42 public abstract class BaseOrchestrationTemplateHandler implements OrchestrationTemplateFileHandler {
43   protected static final Logger logger =
44       LoggerFactory.getLogger(BaseOrchestrationTemplateHandler.class);
45   @Override
46   public UploadFileResponse upload(VspDetails vspDetails, InputStream fileToUpload,
47                                    String fileSuffix, String networkPackageName,
48                                    CandidateService candidateService) {
49     UploadFileResponse uploadFileResponse = new UploadFileResponse();
50     uploadFileResponse.setOnboardingType(getHandlerType());
51     if (isNotEmptyFileToUpload(fileSuffix, fileToUpload, uploadFileResponse, candidateService)) {
52       return uploadFileResponse;
53     }
54
55     byte[] uploadedFileData = FileUtils.toByteArray(fileToUpload);
56     if (isInvalidRawZipData(fileSuffix, uploadFileResponse, uploadedFileData, candidateService)) {
57       return uploadFileResponse;
58     }
59
60     Optional<FileContentHandler> optionalContentMap =
61         getFileContentMap(uploadFileResponse, uploadedFileData);
62     if (!optionalContentMap.isPresent()) {
63       logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
64           getHandlerType().toString()));
65       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
66           getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
67               getHandlerType().toString())));
68       return uploadFileResponse;
69     }
70
71     if (!MapUtils.isEmpty(uploadFileResponse.getErrors())) {
72       return uploadFileResponse;
73     }
74     if (updateCandidateData(vspDetails, uploadedFileData, optionalContentMap.get(), fileSuffix,
75         networkPackageName, candidateService, uploadFileResponse)) {
76       return uploadFileResponse;
77     }
78     return uploadFileResponse;
79
80   }
81
82   protected abstract boolean updateCandidateData(VspDetails vspDetails,
83                                                  byte[] uploadedFileData,
84                                                  FileContentHandler contentMap,
85                                                  String fileSuffix,
86                                                  String networkPackageName,
87                                                  CandidateService candidateService,
88                                                  UploadFileResponse uploadFileResponse);
89
90   private boolean isNotEmptyFileToUpload(String fileSuffix, InputStream fileToUpload,
91                                          UploadFileResponse uploadFileResponse,
92                                          CandidateService candidateService) {
93     Optional<ErrorMessage> errorMessage =
94         candidateService.validateNonEmptyFileToUpload(fileToUpload, fileSuffix);
95     if (errorMessage.isPresent()) {
96       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get());
97       return true;
98     }
99     return false;
100   }
101
102   protected boolean isInvalidRawZipData(String fileSuffix,
103                                         UploadFileResponse uploadFileResponse,
104                                         byte[] uploadedFileData,
105                                         CandidateService candidateService) {
106     Optional<ErrorMessage> errorMessage;
107     errorMessage = candidateService.validateRawZipData(fileSuffix, uploadedFileData);
108     if (errorMessage.isPresent()) {
109       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get());
110       return true;
111     }
112     return false;
113   }
114
115   public abstract Optional<FileContentHandler> getFileContentMap(
116       UploadFileResponse uploadFileResponse,
117       byte[] uploadedFileData);
118
119   protected abstract OnboardingTypesEnum getHandlerType();
120 }