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.Optional;
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(String vspId, Version version, InputStream fileToUpload,
65 String fileSuffix, String networkPackageName) {
66 OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
67 OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
69 VspDetails vspDetails = getVspDetails(vspId, version);
71 UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
72 .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
74 uploadResponse.setNetworkPackageName(networkPackageName);
75 return uploadResponse;
79 public OrchestrationTemplateActionResponse process(String vspId, Version version) {
80 OrchestrationTemplateCandidateData candidate =
81 candidateService.getOrchestrationTemplateCandidate(vspId, version)
82 .orElseThrow(() -> new CoreException(
83 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 candidateService.getOrchestrationTemplateCandidate(vspId, version);
122 if (!candidateDataEntity.isPresent()) {
123 return Optional.empty();
126 if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
127 FilesDataStructure structure = JsonUtil
128 .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
129 String manifest = candidateService.createManifest(vspDetails, structure);
130 OnboardingTypesEnum type =
131 OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
133 new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
134 .replaceManifestInZip(candidateDataEntity.get().getContentData(),
139 new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
140 .getContentData().array()));
144 public Optional<OrchestrationTemplateCandidateData> getInfo(String vspId, Version version) {
145 return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
149 public void abort(String vspId, Version version) {
150 candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
153 private VspDetails getVspDetails(String vspId, Version version) {
154 return vspInfoDao.get(new VspDetails(vspId, version));