d9af918a281f5a7d8c98c24995605fdd7bf88b58
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
18
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;
49
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.Optional;
56
57 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
58
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();
64
65   private final VendorSoftwareProductInfoDao vspInfoDao;
66   private final CandidateService candidateService;
67   private static final String VSP_ID = "VSP id";
68
69   public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
70                                                    CandidateService candidateService
71                                              ) {
72     this.vspInfoDao = vspInfoDao;
73     this.candidateService = candidateService;
74   }
75
76   @Override
77   @Metrics
78   public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
79                                    String fileSuffix, String networkPackageName) {
80     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
81
82     OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
83         OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
84
85     VspDetails vspDetails = getVspDetails(vspId, version);
86
87     UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
88         .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
89
90     uploadResponse.setNetworkPackageName(networkPackageName);
91     return uploadResponse;
92   }
93
94   @Override
95   public OrchestrationTemplateActionResponse process(String vspId, Version version) {
96     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
97
98     OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
99         .orElseThrow(
100             () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
101
102     return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
103         .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
104         .orElse(new OrchestrationTemplateActionResponse());
105   }
106
107   @Override
108   public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
109     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
110
111     Optional<FilesDataStructure> candidateFileDataStructure =
112         candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
113     if (candidateFileDataStructure.isPresent()) {
114       return candidateFileDataStructure;
115     } else {
116       MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
117       return Optional.empty();
118     }
119   }
120
121   @Override
122   public ValidationResponse updateFilesDataStructure(String vspId, Version version,
123                                                      FilesDataStructure fileDataStructure) {
124     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
125
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);
136
137         MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
138         return response;
139       }
140     }
141     candidateService
142         .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
143
144     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
145     return response;
146   }
147
148   @Override
149
150   public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
151     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
152
153     VspDetails vspDetails = getVspDetails(vspId, version);
154
155     Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
156         fetchCandidateDataEntity(vspId, version);
157
158     if (!candidateDataEntity.isPresent()) {
159       ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
160           getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage
161               (), ""));
162       LOGGER.error(errorMessage.getMessage());
163
164       MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
165       return Optional.empty();
166     }
167     OnboardingTypesEnum type =
168         OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
169
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);
174
175       MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
176       return Optional.of(
177           new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
178               .replaceManifestInZip(candidateDataEntity.get().getContentData(),
179                   manifest, vspId, type)));
180     }
181
182     return Optional.of(
183         new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
184             .getContentData().array()));
185   }
186
187   @Override
188   public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
189     return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
190   }
191
192   private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
193       String vspId, Version version) {
194     return Optional
195         .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
196   }
197
198   private VspDetails getVspDetails(String vspId, Version version) {
199
200     return vspInfoDao.get(new VspDetails(vspId, version));
201   }
202
203
204 }