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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
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;
37 import java.io.InputStream;
38 import java.util.Optional;
40 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
42 public abstract class BaseOrchestrationTemplateHandler implements OrchestrationTemplateFileHandler {
43 protected static final Logger logger =
44 LoggerFactory.getLogger(BaseOrchestrationTemplateHandler.class);
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;
55 byte[] uploadedFileData = FileUtils.toByteArray(fileToUpload);
56 if (isInvalidRawZipData(fileSuffix, uploadFileResponse, uploadedFileData, candidateService)) {
57 return uploadFileResponse;
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;
71 if (!MapUtils.isEmpty(uploadFileResponse.getErrors())) {
72 return uploadFileResponse;
74 if (updateCandidateData(vspDetails, uploadedFileData, optionalContentMap.get(), fileSuffix,
75 networkPackageName, candidateService, uploadFileResponse)) {
76 return uploadFileResponse;
78 return uploadFileResponse;
82 protected abstract boolean updateCandidateData(VspDetails vspDetails,
83 byte[] uploadedFileData,
84 FileContentHandler contentMap,
86 String networkPackageName,
87 CandidateService candidateService,
88 UploadFileResponse uploadFileResponse);
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());
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());
115 public abstract Optional<FileContentHandler> getFileContentMap(
116 UploadFileResponse uploadFileResponse,
117 byte[] uploadedFileData);
119 protected abstract OnboardingTypesEnum getHandlerType();