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 org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.lang3.tuple.ImmutablePair;
21 import org.apache.commons.lang3.tuple.Pair;
22 import org.openecomp.core.utilities.json.JsonUtil;
23 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
24 import org.openecomp.sdc.common.errors.CoreException;
25 import org.openecomp.sdc.common.utils.CommonUtil;
26 import org.openecomp.sdc.common.utils.SdcCommon;
27 import org.openecomp.sdc.datatypes.error.ErrorMessage;
28 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
32 import org.openecomp.sdc.vendorsoftwareproduct.errors.OrchestrationTemplateNotFoundErrorBuilder;
33 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationTemplateFileHandler;
34 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUploadFactory;
35 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process.OrchestrationProcessFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
41 import org.openecomp.sdc.versioning.dao.types.Version;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.util.Collections;
46 import java.util.List;
48 import java.util.Objects;
49 import java.util.Optional;
51 public class OrchestrationTemplateCandidateManagerImpl
52 implements OrchestrationTemplateCandidateManager {
54 private final VendorSoftwareProductInfoDao vspInfoDao;
55 private final CandidateService candidateService;
57 public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
58 CandidateService candidateService
60 this.vspInfoDao = vspInfoDao;
61 this.candidateService = candidateService;
65 public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
66 String fileSuffix, String networkPackageName) {
67 OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
68 OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
70 VspDetails vspDetails = getVspDetails(vspId, version);
72 UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
73 .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
75 uploadResponse.setNetworkPackageName(networkPackageName);
76 return uploadResponse;
80 public OrchestrationTemplateActionResponse process(String vspId, Version version) {
81 OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
83 () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
85 return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
86 .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
87 .orElse(new OrchestrationTemplateActionResponse());
91 public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
92 return candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
96 public ValidationResponse updateFilesDataStructure(String vspId, Version version,
97 FilesDataStructure fileDataStructure) {
98 ValidationResponse response = new ValidationResponse();
99 Optional<List<ErrorMessage>> validateErrors =
100 candidateService.validateFileDataStructure(fileDataStructure);
101 if (validateErrors.isPresent()) {
102 List<ErrorMessage> errorMessages = validateErrors.get();
103 if (CollectionUtils.isNotEmpty(errorMessages)) {
104 Map<String, List<ErrorMessage>> errorsMap = Collections.singletonMap(SdcCommon.UPLOAD_FILE, errorMessages);
105 response.setUploadDataErrors(errorsMap);
110 .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
116 public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
117 VspDetails vspDetails = getVspDetails(vspId, version);
119 Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
120 fetchCandidateDataEntity(vspId, version);
122 if (!candidateDataEntity.isPresent()) {
123 return Optional.empty();
126 if(Objects.isNull(candidateDataEntity.get().getFileSuffix())) {
127 return Optional.empty();
130 OnboardingTypesEnum type =
131 OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
133 if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
134 FilesDataStructure structure = JsonUtil
135 .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
136 String manifest = candidateService.createManifest(vspDetails, structure);
138 new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
139 .replaceManifestInZip(candidateDataEntity.get().getContentData(),
140 manifest, vspId, type)));
144 new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
145 .getContentData().array()));
149 public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
150 return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
154 public void abort(String vspId, Version version) {
155 candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
158 private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
159 String vspId, Version version) {
161 .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
164 private VspDetails getVspDetails(String vspId, Version version) {
166 return vspInfoDao.get(new VspDetails(vspId, version));