23cf41c5d54e5e750162d237ceaab432ef89e4ff
[sdc.git] /
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 java.io.ByteArrayInputStream;
24 import java.util.Optional;
25 import org.apache.commons.collections4.MapUtils;
26 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
27 import org.openecomp.sdc.common.utils.SdcCommon;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
32 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
36
37 public abstract class BaseOrchestrationTemplateHandler implements OrchestrationTemplateFileHandler {
38   protected static final Logger logger = LoggerFactory.getLogger(BaseOrchestrationTemplateHandler.class);
39
40   @Override
41   public UploadFileResponse upload(final VspDetails vspDetails,
42                                    final OnboardPackageInfo onboardPackageInfo,
43                                    final CandidateService candidateService) {
44     final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage();
45     final UploadFileResponse uploadFileResponse = new UploadFileResponse();
46     uploadFileResponse.setOnboardingType(getHandlerType());
47     if (isFileFileToUploadEmpty(onboardPackage, uploadFileResponse, candidateService)) {
48       return uploadFileResponse;
49     }
50
51     final byte[] fileContentByteArray = onboardPackage.getFileContent().array();
52     if (isInvalidRawZipData(onboardPackage.getFileExtension(),
53         uploadFileResponse, fileContentByteArray, candidateService)) {
54       return uploadFileResponse;
55     }
56
57     final UploadFileResponse validateResponse = validate(onboardPackageInfo);
58
59     if (!MapUtils.isEmpty(validateResponse.getErrors())) {
60       uploadFileResponse.addStructureErrors(validateResponse.getErrors());
61       return uploadFileResponse;
62     }
63
64     final UploadFileResponse responseFromUpdate = updateCandidateData(vspDetails, onboardPackageInfo,
65         candidateService);
66     if (!MapUtils.isEmpty(responseFromUpdate.getErrors())) {
67       uploadFileResponse.addStructureErrors(responseFromUpdate.getErrors());
68     }
69
70     return uploadFileResponse;
71   }
72
73   protected abstract UploadFileResponse updateCandidateData(final VspDetails vspDetails,
74                                                  final OnboardPackageInfo onboardPackageInfo,
75                                                  final CandidateService candidateService);
76
77   private boolean isFileFileToUploadEmpty(final OnboardPackage onboardPackage,
78                                           final UploadFileResponse uploadFileResponse,
79                                           final CandidateService candidateService) {
80     final ByteArrayInputStream fileToUpload = new ByteArrayInputStream(
81         onboardPackage.getFileContent().array());
82     Optional<ErrorMessage> errorMessage =
83         candidateService.validateNonEmptyFileToUpload(fileToUpload, onboardPackage.getFileExtension());
84     if (errorMessage.isPresent()) {
85       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get());
86       return true;
87     }
88     return false;
89   }
90
91   protected boolean isInvalidRawZipData(String fileSuffix,
92                                         UploadFileResponse uploadFileResponse,
93                                         byte[] uploadedFileData,
94                                         CandidateService candidateService) {
95     Optional<ErrorMessage> errorMessage;
96     errorMessage = candidateService.validateRawZipData(fileSuffix, uploadedFileData);
97     if (errorMessage.isPresent()) {
98       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get());
99       return true;
100     }
101     return false;
102   }
103
104   public abstract UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo);
105
106   protected abstract OnboardingTypesEnum getHandlerType();
107 }