2 * Copyright © 2016-2017 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.errors.Messages;
26 import org.openecomp.sdc.common.utils.CommonUtil;
27 import org.openecomp.sdc.common.utils.SdcCommon;
28 import org.openecomp.sdc.datatypes.error.ErrorLevel;
29 import org.openecomp.sdc.datatypes.error.ErrorMessage;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.logging.api.annotations.Metrics;
33 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
34 import org.openecomp.sdc.logging.types.LoggerServiceName;
35 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
36 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
40 import org.openecomp.sdc.vendorsoftwareproduct.errors.OrchestrationTemplateNotFoundErrorBuilder;
41 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationTemplateFileHandler;
42 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUploadFactory;
43 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process.OrchestrationProcessFactory;
44 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
49 import org.openecomp.sdc.versioning.dao.types.Version;
51 import java.io.IOException;
52 import java.io.InputStream;
53 import java.util.HashMap;
54 import java.util.List;
56 import java.util.Optional;
58 public class OrchestrationTemplateCandidateManagerImpl
59 implements OrchestrationTemplateCandidateManager {
60 private static final Logger LOGGER =
61 LoggerFactory.getLogger(OrchestrationTemplateCandidateManagerImpl.class);
62 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
64 private final VendorSoftwareProductInfoDao vspInfoDao;
65 private final CandidateService candidateService;
66 private static final String VSP_ID = "VSP id";
68 public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
69 CandidateService candidateService
71 this.vspInfoDao = vspInfoDao;
72 this.candidateService = candidateService;
77 public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
78 String fileSuffix, String networkPackageName) {
79 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
81 OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
82 OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
84 VspDetails vspDetails = getVspDetails(vspId, version);
86 UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
87 .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
89 uploadResponse.setNetworkPackageName(networkPackageName);
90 return uploadResponse;
94 public OrchestrationTemplateActionResponse process(String vspId, Version version) {
95 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
97 OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
99 () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
101 return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
102 .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
103 .orElse(new OrchestrationTemplateActionResponse());
107 public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
108 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
110 Optional<FilesDataStructure> candidateFileDataStructure =
111 candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
112 if (candidateFileDataStructure.isPresent()) {
113 return candidateFileDataStructure;
115 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
116 return Optional.empty();
121 public ValidationResponse updateFilesDataStructure(String vspId, Version version,
122 FilesDataStructure fileDataStructure) {
123 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
125 ValidationResponse response = new ValidationResponse();
126 Optional<List<ErrorMessage>> validateErrors =
127 candidateService.validateFileDataStructure(fileDataStructure);
128 if (validateErrors.isPresent()) {
129 List<ErrorMessage> errorMessages = validateErrors.get();
130 if (CollectionUtils.isNotEmpty(errorMessages)) {
131 Map<String, List<ErrorMessage>> errorsMap = new HashMap<>();
132 errorsMap.put(SdcCommon.UPLOAD_FILE, errorMessages);
133 response.setUploadDataErrors(errorsMap, LoggerServiceName.Update_Manifest,
134 LoggerTragetServiceName.VALIDATE_FILE_DATA_STRUCTURE);
136 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
141 .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
143 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
149 public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
150 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
152 VspDetails vspDetails = getVspDetails(vspId, version);
154 Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
155 fetchCandidateDataEntity(vspId, version);
157 if (!candidateDataEntity.isPresent()) {
158 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
159 Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage());
160 LOGGER.error(errorMessage.getMessage());
162 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
163 return Optional.empty();
165 OnboardingTypesEnum type =
166 OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
168 if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
169 FilesDataStructure structure = JsonUtil
170 .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
171 String manifest = candidateService.createManifest(vspDetails, structure);
173 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
175 new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
176 .replaceManifestInZip(candidateDataEntity.get().getContentData(),
177 manifest, vspId, type)));
181 new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
182 .getContentData().array()));
186 public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
187 return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
190 private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
191 String vspId, Version version) {
193 .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
196 private VspDetails getVspDetails(String vspId, Version version) {
198 return vspInfoDao.get(new VspDetails(vspId, version));