2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
19 import java.io.IOException;
20 import java.util.Collections;
21 import java.util.List;
23 import java.util.Optional;
24 import org.apache.commons.collections4.CollectionUtils;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.openecomp.core.utilities.json.JsonUtil;
28 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
29 import org.openecomp.sdc.common.errors.CoreException;
30 import org.openecomp.sdc.common.utils.CommonUtil;
31 import org.openecomp.sdc.common.utils.SdcCommon;
32 import org.openecomp.sdc.datatypes.error.ErrorMessage;
33 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
37 import org.openecomp.sdc.vendorsoftwareproduct.errors.OrchestrationTemplateNotFoundErrorBuilder;
38 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationTemplateFileHandler;
39 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUploadFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process.OrchestrationProcessFactory;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
48 import org.openecomp.sdc.versioning.dao.types.Version;
50 public class OrchestrationTemplateCandidateManagerImpl
51 implements OrchestrationTemplateCandidateManager {
53 private final VendorSoftwareProductInfoDao vspInfoDao;
54 private final CandidateService candidateService;
56 public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
57 CandidateService candidateService
59 this.vspInfoDao = vspInfoDao;
60 this.candidateService = candidateService;
64 public UploadFileResponse upload(final VspDetails vspDetails,
65 final OnboardPackageInfo onboardPackageInfo) {
66 final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage();
67 final OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
68 OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(onboardPackageInfo.getPackageType());
70 final UploadFileResponse uploadFileResponse =
71 orchestrationTemplateFileHandler.upload(vspDetails, onboardPackageInfo, candidateService);
72 uploadFileResponse.setNetworkPackageName(onboardPackage.getFilename());
73 return uploadFileResponse;
77 public OrchestrationTemplateActionResponse process(String vspId, Version version) {
78 OrchestrationTemplateCandidateData candidate =
79 candidateService.getOrchestrationTemplateCandidate(vspId, version)
80 .orElseThrow(() -> new CoreException(
81 new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
83 return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
84 .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
85 .orElse(new OrchestrationTemplateActionResponse());
89 public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
90 return candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
94 public ValidationResponse updateFilesDataStructure(String vspId, Version version,
95 FilesDataStructure fileDataStructure) {
96 ValidationResponse response = new ValidationResponse();
97 Optional<List<ErrorMessage>> validateErrors =
98 candidateService.validateFileDataStructure(fileDataStructure);
99 if (validateErrors.isPresent()) {
100 List<ErrorMessage> errorMessages = validateErrors.get();
101 if (CollectionUtils.isNotEmpty(errorMessages)) {
102 Map<String, List<ErrorMessage>> errorsMap = Collections.singletonMap(SdcCommon.UPLOAD_FILE, errorMessages);
103 response.setUploadDataErrors(errorsMap);
108 .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
114 public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
115 VspDetails vspDetails = getVspDetails(vspId, version);
117 Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
118 candidateService.getOrchestrationTemplateCandidate(vspId, version);
120 if (!candidateDataEntity.isPresent()) {
121 return Optional.empty();
124 if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
125 FilesDataStructure structure = JsonUtil
126 .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
127 String manifest = candidateService.createManifest(vspDetails, structure);
128 OnboardingTypesEnum type =
129 OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
131 new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
132 .replaceManifestInZip(candidateDataEntity.get().getContentData(),
137 new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
138 .getContentData().array()));
142 public Optional<OrchestrationTemplateCandidateData> getInfo(String vspId, Version version) {
143 return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
147 public void abort(String vspId, Version version) {
148 candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
151 private VspDetails getVspDetails(String vspId, Version version) {
152 return vspInfoDao.get(new VspDetails(vspId, version));