c9bb956a18555093554a47474d7ab1bff3d13848
[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.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;
50
51 import java.io.IOException;
52 import java.io.InputStream;
53 import java.util.HashMap;
54 import java.util.List;
55 import java.util.Map;
56 import java.util.Optional;
57
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();
63
64   private final VendorSoftwareProductInfoDao vspInfoDao;
65   private final CandidateService candidateService;
66   private static final String VSP_ID = "VSP id";
67
68   public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
69                                                    CandidateService candidateService
70                                              ) {
71     this.vspInfoDao = vspInfoDao;
72     this.candidateService = candidateService;
73   }
74
75   @Override
76   @Metrics
77   public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
78                                    String fileSuffix, String networkPackageName) {
79     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
80
81     OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
82         OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
83
84     VspDetails vspDetails = getVspDetails(vspId, version);
85
86     UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
87         .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
88
89     uploadResponse.setNetworkPackageName(networkPackageName);
90     return uploadResponse;
91   }
92
93   @Override
94   public OrchestrationTemplateActionResponse process(String vspId, Version version) {
95     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
96
97     OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
98         .orElseThrow(
99             () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
100
101     return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
102         .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
103         .orElse(new OrchestrationTemplateActionResponse());
104   }
105
106   @Override
107   public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
108     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
109
110     Optional<FilesDataStructure> candidateFileDataStructure =
111         candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
112     if (candidateFileDataStructure.isPresent()) {
113       return candidateFileDataStructure;
114     } else {
115       MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
116       return Optional.empty();
117     }
118   }
119
120   @Override
121   public ValidationResponse updateFilesDataStructure(String vspId, Version version,
122                                                      FilesDataStructure fileDataStructure) {
123     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
124
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);
135
136         MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
137         return response;
138       }
139     }
140     candidateService
141         .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
142
143     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
144     return response;
145   }
146
147   @Override
148
149   public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
150     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
151
152     VspDetails vspDetails = getVspDetails(vspId, version);
153
154     Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
155         fetchCandidateDataEntity(vspId, version);
156
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());
161
162       MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
163       return Optional.empty();
164     }
165     OnboardingTypesEnum type =
166         OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
167
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);
172
173       MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
174       return Optional.of(
175           new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
176               .replaceManifestInZip(candidateDataEntity.get().getContentData(),
177                   manifest, vspId, type)));
178     }
179
180     return Optional.of(
181         new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
182             .getContentData().array()));
183   }
184
185   @Override
186   public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
187     return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
188   }
189
190   private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
191       String vspId, Version version) {
192     return Optional
193         .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
194   }
195
196   private VspDetails getVspDetails(String vspId, Version version) {
197
198     return vspInfoDao.get(new VspDetails(vspId, version));
199   }
200
201
202 }