2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
22 import java.io.ByteArrayInputStream;
23 import java.util.Optional;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
26 import org.openecomp.sdc.common.utils.SdcCommon;
27 import org.openecomp.sdc.datatypes.error.ErrorMessage;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
31 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
36 public abstract class BaseOrchestrationTemplateHandler implements OrchestrationTemplateFileHandler {
38 protected static final Logger logger = LoggerFactory.getLogger(BaseOrchestrationTemplateHandler.class);
41 public UploadFileResponse upload(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo,
42 final CandidateService candidateService) {
43 final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage();
44 final UploadFileResponse uploadFileResponse = new UploadFileResponse();
45 uploadFileResponse.setOnboardingType(getHandlerType());
46 if (isFileToUploadEmpty(onboardPackage, uploadFileResponse, candidateService)) {
47 return uploadFileResponse;
49 final byte[] fileContentByteArray = onboardPackage.getFileContent().array();
50 if (isInvalidRawZipData(onboardPackage.getFileExtension(), uploadFileResponse, fileContentByteArray, candidateService)) {
51 return uploadFileResponse;
53 final UploadFileResponse validateResponse = validate(onboardPackageInfo);
54 if (!MapUtils.isEmpty(validateResponse.getErrors())) {
55 uploadFileResponse.addStructureErrors(validateResponse.getErrors());
56 return uploadFileResponse;
58 final UploadFileResponse responseFromUpdate = updateCandidateData(vspDetails, onboardPackageInfo, candidateService);
59 if (!MapUtils.isEmpty(responseFromUpdate.getErrors())) {
60 uploadFileResponse.addStructureErrors(responseFromUpdate.getErrors());
62 return uploadFileResponse;
65 protected abstract UploadFileResponse updateCandidateData(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo,
66 final CandidateService candidateService);
68 private boolean isFileToUploadEmpty(final OnboardPackage onboardPackage, final UploadFileResponse uploadFileResponse,
69 final CandidateService candidateService) {
70 final ByteArrayInputStream fileToUpload = new ByteArrayInputStream(onboardPackage.getFileContent().array());
71 Optional<ErrorMessage> errorMessage = candidateService.validateNonEmptyFileToUpload(fileToUpload, onboardPackage.getFileExtension());
72 if (errorMessage.isPresent()) {
73 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get());
79 protected boolean isInvalidRawZipData(String fileSuffix, UploadFileResponse uploadFileResponse, byte[] uploadedFileData,
80 CandidateService candidateService) {
81 Optional<ErrorMessage> errorMessage;
82 errorMessage = candidateService.validateRawZipData(fileSuffix, uploadedFileData);
83 if (errorMessage.isPresent()) {
84 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get());
90 public abstract UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo);
92 protected abstract OnboardingTypesEnum getHandlerType();