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.LoggerTragetServiceName;
35 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
39 import org.openecomp.sdc.vendorsoftwareproduct.errors.OrchestrationTemplateNotFoundErrorBuilder;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationTemplateFileHandler;
41 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUploadFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process.OrchestrationProcessFactory;
43 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
48 import org.openecomp.sdc.versioning.dao.types.Version;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.util.HashMap;
53 import java.util.List;
55 import java.util.Optional;
57 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
59 public class OrchestrationTemplateCandidateManagerImpl
60 implements OrchestrationTemplateCandidateManager {
61 private static final Logger LOGGER =
62 LoggerFactory.getLogger(OrchestrationTemplateCandidateManagerImpl.class);
63 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
65 private final VendorSoftwareProductInfoDao vspInfoDao;
66 private final CandidateService candidateService;
67 private static final String VSP_ID = "VSP id";
69 public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
70 CandidateService candidateService
72 this.vspInfoDao = vspInfoDao;
73 this.candidateService = candidateService;
78 public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
79 String fileSuffix, String networkPackageName) {
80 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
82 OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
83 OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
85 VspDetails vspDetails = getVspDetails(vspId, version);
87 UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
88 .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
90 uploadResponse.setNetworkPackageName(networkPackageName);
91 return uploadResponse;
95 public OrchestrationTemplateActionResponse process(String vspId, Version version) {
96 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
98 OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
100 () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
102 return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
103 .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
104 .orElse(new OrchestrationTemplateActionResponse());
108 public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
109 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
111 Optional<FilesDataStructure> candidateFileDataStructure =
112 candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
113 if (candidateFileDataStructure.isPresent()) {
114 return candidateFileDataStructure;
116 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
117 return Optional.empty();
122 public ValidationResponse updateFilesDataStructure(String vspId, Version version,
123 FilesDataStructure fileDataStructure) {
124 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
126 ValidationResponse response = new ValidationResponse();
127 Optional<List<ErrorMessage>> validateErrors =
128 candidateService.validateFileDataStructure(fileDataStructure);
129 if (validateErrors.isPresent()) {
130 List<ErrorMessage> errorMessages = validateErrors.get();
131 if (CollectionUtils.isNotEmpty(errorMessages)) {
132 Map<String, List<ErrorMessage>> errorsMap = new HashMap<>();
133 errorsMap.put(SdcCommon.UPLOAD_FILE, errorMessages);
134 response.setUploadDataErrors(errorsMap,
135 LoggerTragetServiceName.VALIDATE_FILE_DATA_STRUCTURE);
137 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
142 .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
144 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
150 public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
151 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
153 VspDetails vspDetails = getVspDetails(vspId, version);
155 Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
156 fetchCandidateDataEntity(vspId, version);
158 if (!candidateDataEntity.isPresent()) {
159 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
160 getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage
162 LOGGER.error(errorMessage.getMessage());
164 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
165 return Optional.empty();
167 OnboardingTypesEnum type =
168 OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
170 if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
171 FilesDataStructure structure = JsonUtil
172 .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
173 String manifest = candidateService.createManifest(vspDetails, structure);
175 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
177 new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
178 .replaceManifestInZip(candidateDataEntity.get().getContentData(),
179 manifest, vspId, type)));
183 new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
184 .getContentData().array()));
188 public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
189 return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
192 private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
193 String vspId, Version version) {
195 .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
198 private VspDetails getVspDetails(String vspId, Version version) {
200 return vspInfoDao.get(new VspDetails(vspId, version));