bc4fb668b06ee4b4ebe2bf25ee4e737f8fc8fb53
[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 static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
24
25 import java.util.Optional;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
28 import org.openecomp.sdc.common.errors.Messages;
29 import org.openecomp.sdc.common.utils.SdcCommon;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.datatypes.error.ErrorMessage;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
34 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
35 import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
39
40 public class OrchestrationTemplateZipHandler extends BaseOrchestrationTemplateHandler
41     implements OrchestrationTemplateFileHandler {
42
43   @Override
44   public Optional<FileContentHandler> getFileContentMap(UploadFileResponse uploadFileResponse,
45                                                         byte[] uploadedFileData) {
46     return OrchestrationUtil
47         .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse, uploadedFileData);
48   }
49
50   @Override
51   protected boolean updateCandidateData(final VspDetails vspDetails,
52                                         final OnboardPackageInfo onboardPackageInfo,
53                                         final CandidateService candidateService,
54                                         final UploadFileResponse uploadFileResponse,
55                                         final FileContentHandler contentMap) {
56     try {
57       final OnboardPackage zipPackage = onboardPackageInfo.getOnboardPackage();
58       final OrchestrationTemplateCandidateData candidateData =
59           new CandidateEntityBuilder(candidateService)
60               .buildCandidateEntityFromZip(vspDetails, zipPackage.getFileContent().array(), contentMap,
61                   uploadFileResponse.getErrors());
62       candidateData.setFileName(zipPackage.getFilename());
63       candidateData.setFileSuffix(zipPackage.getFileExtension());
64       candidateService
65           .updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(), candidateData);
66     } catch (final Exception exception) {
67       logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
68           getHandlerType().toString()), exception);
69       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE,
70           new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
71       return true;
72     }
73     return false;
74   }
75
76   @Override
77   protected OnboardingTypesEnum getHandlerType() {
78     return OnboardingTypesEnum.ZIP;
79   }
80 }