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 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
60 public class OrchestrationTemplateCandidateManagerImpl
61 implements OrchestrationTemplateCandidateManager {
62 private static final Logger LOGGER =
63 LoggerFactory.getLogger(OrchestrationTemplateCandidateManagerImpl.class);
64 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
66 private final VendorSoftwareProductInfoDao vspInfoDao;
67 private final CandidateService candidateService;
68 private static final String VSP_ID = "VSP id";
70 public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
71 CandidateService candidateService
73 this.vspInfoDao = vspInfoDao;
74 this.candidateService = candidateService;
79 public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
80 String fileSuffix, String networkPackageName) {
81 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
83 OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
84 OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
86 VspDetails vspDetails = getVspDetails(vspId, version);
88 UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
89 .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
91 uploadResponse.setNetworkPackageName(networkPackageName);
92 return uploadResponse;
96 public OrchestrationTemplateActionResponse process(String vspId, Version version) {
97 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
99 OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
101 () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
103 return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
104 .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
105 .orElse(new OrchestrationTemplateActionResponse());
109 public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
110 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
112 Optional<FilesDataStructure> candidateFileDataStructure =
113 candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
114 if (candidateFileDataStructure.isPresent()) {
115 return candidateFileDataStructure;
117 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
118 return Optional.empty();
123 public ValidationResponse updateFilesDataStructure(String vspId, Version version,
124 FilesDataStructure fileDataStructure) {
125 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
127 ValidationResponse response = new ValidationResponse();
128 Optional<List<ErrorMessage>> validateErrors =
129 candidateService.validateFileDataStructure(fileDataStructure);
130 if (validateErrors.isPresent()) {
131 List<ErrorMessage> errorMessages = validateErrors.get();
132 if (CollectionUtils.isNotEmpty(errorMessages)) {
133 Map<String, List<ErrorMessage>> errorsMap = new HashMap<>();
134 errorsMap.put(SdcCommon.UPLOAD_FILE, errorMessages);
135 response.setUploadDataErrors(errorsMap, LoggerServiceName.Update_Manifest,
136 LoggerTragetServiceName.VALIDATE_FILE_DATA_STRUCTURE);
138 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
143 .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
145 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
151 public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
152 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
154 VspDetails vspDetails = getVspDetails(vspId, version);
156 Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
157 fetchCandidateDataEntity(vspId, version);
159 if (!candidateDataEntity.isPresent()) {
160 ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
161 getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage
163 LOGGER.error(errorMessage.getMessage());
165 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
166 return Optional.empty();
168 OnboardingTypesEnum type =
169 OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
171 if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
172 FilesDataStructure structure = JsonUtil
173 .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
174 String manifest = candidateService.createManifest(vspDetails, structure);
176 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
178 new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
179 .replaceManifestInZip(candidateDataEntity.get().getContentData(),
180 manifest, vspId, type)));
184 new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
185 .getContentData().array()));
189 public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
190 return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
193 private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
194 String vspId, Version version) {
196 .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
199 private VspDetails getVspDetails(String vspId, Version version) {
201 return vspInfoDao.get(new VspDetails(vspId, version));