77eeb394c72f62183498af43b0b3301627070e88
[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.types.LoggerTragetServiceName;
34 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
38 import org.openecomp.sdc.vendorsoftwareproduct.errors.OrchestrationTemplateNotFoundErrorBuilder;
39 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationTemplateFileHandler;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUploadFactory;
41 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process.OrchestrationProcessFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
47 import org.openecomp.sdc.versioning.dao.types.Version;
48
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.util.HashMap;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Optional;
55
56 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
57
58 public class OrchestrationTemplateCandidateManagerImpl
59     implements OrchestrationTemplateCandidateManager {
60   private static final Logger LOGGER =
61       LoggerFactory.getLogger(OrchestrationTemplateCandidateManagerImpl.class);
62   private final VendorSoftwareProductInfoDao vspInfoDao;
63   private final CandidateService candidateService;
64   private static final String VSP_ID = "VSP id";
65
66   public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
67                                                    CandidateService candidateService
68                                              ) {
69     this.vspInfoDao = vspInfoDao;
70     this.candidateService = candidateService;
71   }
72
73   @Override
74   @Metrics
75   public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
76                                    String fileSuffix, String networkPackageName) {
77     OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
78         OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
79
80     VspDetails vspDetails = getVspDetails(vspId, version);
81
82     UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
83         .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
84
85     uploadResponse.setNetworkPackageName(networkPackageName);
86     return uploadResponse;
87   }
88
89   @Override
90   public OrchestrationTemplateActionResponse process(String vspId, Version version) {
91     OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
92         .orElseThrow(
93             () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
94
95     return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
96         .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
97         .orElse(new OrchestrationTemplateActionResponse());
98   }
99
100   @Override
101   public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
102     Optional<FilesDataStructure> candidateFileDataStructure =
103         candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
104     if (candidateFileDataStructure.isPresent()) {
105       return candidateFileDataStructure;
106     } else {
107       return Optional.empty();
108     }
109   }
110
111   @Override
112   public ValidationResponse updateFilesDataStructure(String vspId, Version version,
113                                                      FilesDataStructure fileDataStructure) {
114     ValidationResponse response = new ValidationResponse();
115     Optional<List<ErrorMessage>> validateErrors =
116         candidateService.validateFileDataStructure(fileDataStructure);
117     if (validateErrors.isPresent()) {
118       List<ErrorMessage> errorMessages = validateErrors.get();
119       if (CollectionUtils.isNotEmpty(errorMessages)) {
120         Map<String, List<ErrorMessage>> errorsMap = new HashMap<>();
121         errorsMap.put(SdcCommon.UPLOAD_FILE, errorMessages);
122         response.setUploadDataErrors(errorsMap,
123                 LoggerTragetServiceName.VALIDATE_FILE_DATA_STRUCTURE);
124         return response;
125       }
126     }
127     candidateService
128         .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
129     return response;
130   }
131
132   @Override
133
134   public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
135     VspDetails vspDetails = getVspDetails(vspId, version);
136
137     Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
138         fetchCandidateDataEntity(vspId, version);
139
140     if (!candidateDataEntity.isPresent()) {
141       ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
142           getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage
143               (), ""));
144       LOGGER.error(errorMessage.getMessage());
145       return Optional.empty();
146     }
147     OnboardingTypesEnum type =
148         OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
149
150     if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
151       FilesDataStructure structure = JsonUtil
152           .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
153       String manifest = candidateService.createManifest(vspDetails, structure);
154       return Optional.of(
155           new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
156               .replaceManifestInZip(candidateDataEntity.get().getContentData(),
157                   manifest, vspId, type)));
158     }
159
160     return Optional.of(
161         new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
162             .getContentData().array()));
163   }
164
165   @Override
166   public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
167     return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
168   }
169
170   private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
171       String vspId, Version version) {
172     return Optional
173         .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
174   }
175
176   private VspDetails getVspDetails(String vspId, Version version) {
177
178     return vspInfoDao.get(new VspDetails(vspId, version));
179   }
180
181
182 }