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.logging.api.annotations.Metrics;
29 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
33 import org.openecomp.sdc.vendorsoftwareproduct.errors.OrchestrationTemplateNotFoundErrorBuilder;
34 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationTemplateFileHandler;
35 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUploadFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process.OrchestrationProcessFactory;
37 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
42 import org.openecomp.sdc.versioning.dao.types.Version;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.util.Collections;
47 import java.util.List;
49 import java.util.Objects;
50 import java.util.Optional;
52 public class OrchestrationTemplateCandidateManagerImpl
53 implements OrchestrationTemplateCandidateManager {
55 private final VendorSoftwareProductInfoDao vspInfoDao;
56 private final CandidateService candidateService;
58 public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
59 CandidateService candidateService
61 this.vspInfoDao = vspInfoDao;
62 this.candidateService = candidateService;
67 public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
68 String fileSuffix, String networkPackageName) {
69 OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
70 OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
72 VspDetails vspDetails = getVspDetails(vspId, version);
74 UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
75 .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
77 uploadResponse.setNetworkPackageName(networkPackageName);
78 return uploadResponse;
82 public OrchestrationTemplateActionResponse process(String vspId, Version version) {
83 OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
85 () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
87 return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
88 .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
89 .orElse(new OrchestrationTemplateActionResponse());
93 public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
94 return candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
98 public ValidationResponse updateFilesDataStructure(String vspId, Version version,
99 FilesDataStructure fileDataStructure) {
100 ValidationResponse response = new ValidationResponse();
101 Optional<List<ErrorMessage>> validateErrors =
102 candidateService.validateFileDataStructure(fileDataStructure);
103 if (validateErrors.isPresent()) {
104 List<ErrorMessage> errorMessages = validateErrors.get();
105 if (CollectionUtils.isNotEmpty(errorMessages)) {
106 Map<String, List<ErrorMessage>> errorsMap = Collections.singletonMap(SdcCommon.UPLOAD_FILE, errorMessages);
107 response.setUploadDataErrors(errorsMap);
112 .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
118 public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
119 VspDetails vspDetails = getVspDetails(vspId, version);
121 Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
122 fetchCandidateDataEntity(vspId, version);
124 if (!candidateDataEntity.isPresent()) {
125 return Optional.empty();
128 if(Objects.isNull(candidateDataEntity.get().getFileSuffix())) {
129 return Optional.empty();
132 OnboardingTypesEnum type =
133 OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
135 if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
136 FilesDataStructure structure = JsonUtil
137 .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
138 String manifest = candidateService.createManifest(vspDetails, structure);
140 new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
141 .replaceManifestInZip(candidateDataEntity.get().getContentData(),
142 manifest, vspId, type)));
146 new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
147 .getContentData().array()));
151 public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
152 return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
156 public void abort(String vspId, Version version) {
157 candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
160 private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
161 String vspId, Version version) {
163 .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
166 private VspDetails getVspDetails(String vspId, Version version) {
168 return vspInfoDao.get(new VspDetails(vspId, version));