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