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