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.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.logging.api.annotations.Metrics;
31 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
35 import org.openecomp.sdc.vendorsoftwareproduct.errors.OrchestrationTemplateNotFoundErrorBuilder;
36 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationTemplateFileHandler;
37 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUploadFactory;
38 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process.OrchestrationProcessFactory;
39 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
44 import org.openecomp.sdc.versioning.dao.types.Version;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.util.Collections;
49 import java.util.List;
51 import java.util.Optional;
53 public class OrchestrationTemplateCandidateManagerImpl
54 implements OrchestrationTemplateCandidateManager {
55 private static final Logger LOGGER =
56 LoggerFactory.getLogger(OrchestrationTemplateCandidateManagerImpl.class);
57 private final VendorSoftwareProductInfoDao vspInfoDao;
58 private final CandidateService candidateService;
60 public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
61 CandidateService candidateService
63 this.vspInfoDao = vspInfoDao;
64 this.candidateService = candidateService;
69 public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
70 String fileSuffix, String networkPackageName) {
71 OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
72 OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
74 VspDetails vspDetails = getVspDetails(vspId, version);
76 UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
77 .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
79 uploadResponse.setNetworkPackageName(networkPackageName);
80 return uploadResponse;
84 public OrchestrationTemplateActionResponse process(String vspId, Version version) {
85 OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
87 () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
89 return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
90 .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
91 .orElse(new OrchestrationTemplateActionResponse());
95 public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
96 return candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
100 public ValidationResponse updateFilesDataStructure(String vspId, Version version,
101 FilesDataStructure fileDataStructure) {
102 ValidationResponse response = new ValidationResponse();
103 Optional<List<ErrorMessage>> validateErrors =
104 candidateService.validateFileDataStructure(fileDataStructure);
105 if (validateErrors.isPresent()) {
106 List<ErrorMessage> errorMessages = validateErrors.get();
107 if (CollectionUtils.isNotEmpty(errorMessages)) {
108 Map<String, List<ErrorMessage>> errorsMap = Collections.singletonMap(SdcCommon.UPLOAD_FILE, errorMessages);
109 response.setUploadDataErrors(errorsMap);
114 .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
120 public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
121 VspDetails vspDetails = getVspDetails(vspId, version);
123 Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
124 fetchCandidateDataEntity(vspId, version);
126 if (!candidateDataEntity.isPresent()) {
127 return Optional.empty();
129 OnboardingTypesEnum type =
130 OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
132 if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
133 FilesDataStructure structure = JsonUtil
134 .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
135 String manifest = candidateService.createManifest(vspDetails, structure);
137 new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
138 .replaceManifestInZip(candidateDataEntity.get().getContentData(),
139 manifest, vspId, type)));
143 new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
144 .getContentData().array()));
148 public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
149 return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
153 public void abort(String vspId, Version version) {
154 candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
157 private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
158 String vspId, Version version) {
160 .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
163 private VspDetails getVspDetails(String vspId, Version version) {
165 return vspInfoDao.get(new VspDetails(vspId, version));