3a17e453af5649124fb6d579a3769fd803016e5c
[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.Collections;
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 final VendorSoftwareProductInfoDao vspInfoDao;
64   private final CandidateService candidateService;
65   private static final String VSP_ID = "VSP id";
66
67   public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
68                                                    CandidateService candidateService
69                                              ) {
70     this.vspInfoDao = vspInfoDao;
71     this.candidateService = candidateService;
72   }
73
74   @Override
75   @Metrics
76   public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
77                                    String fileSuffix, String networkPackageName) {
78     OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
79         OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
80
81     VspDetails vspDetails = getVspDetails(vspId, version);
82
83     UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
84         .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
85
86     uploadResponse.setNetworkPackageName(networkPackageName);
87     return uploadResponse;
88   }
89
90   @Override
91   public OrchestrationTemplateActionResponse process(String vspId, Version version) {
92     OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version)
93         .orElseThrow(
94             () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
95
96     return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
97         .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
98         .orElse(new OrchestrationTemplateActionResponse());
99   }
100
101   @Override
102   public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
103     Optional<FilesDataStructure> candidateFileDataStructure =
104         candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
105     if (candidateFileDataStructure.isPresent()) {
106       return candidateFileDataStructure;
107     } else {
108       return Optional.empty();
109     }
110   }
111
112   @Override
113   public ValidationResponse updateFilesDataStructure(String vspId, Version version,
114                                                      FilesDataStructure fileDataStructure) {
115     ValidationResponse response = new ValidationResponse();
116     Optional<List<ErrorMessage>> validateErrors =
117         candidateService.validateFileDataStructure(fileDataStructure);
118     if (validateErrors.isPresent()) {
119       List<ErrorMessage> errorMessages = validateErrors.get();
120       if (CollectionUtils.isNotEmpty(errorMessages)) {
121         Map<String, List<ErrorMessage>> errorsMap = Collections.singletonMap(SdcCommon.UPLOAD_FILE, errorMessages);
122         response.setUploadDataErrors(errorsMap);
123         return response;
124       }
125     }
126     candidateService
127         .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
128     return response;
129   }
130
131   @Override
132
133   public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
134     VspDetails vspDetails = getVspDetails(vspId, version);
135
136     Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
137         fetchCandidateDataEntity(vspId, version);
138
139     if (!candidateDataEntity.isPresent()) {
140       ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
141           getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage
142               (), ""));
143       LOGGER.error(errorMessage.getMessage());
144       return Optional.empty();
145     }
146     OnboardingTypesEnum type =
147         OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
148
149     if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
150       FilesDataStructure structure = JsonUtil
151           .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
152       String manifest = candidateService.createManifest(vspDetails, structure);
153       return Optional.of(
154           new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
155               .replaceManifestInZip(candidateDataEntity.get().getContentData(),
156                   manifest, vspId, type)));
157     }
158
159     return Optional.of(
160         new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
161             .getContentData().array()));
162   }
163
164   @Override
165   public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
166     return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
167   }
168
169   private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
170       String vspId, Version version) {
171     return Optional
172         .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
173   }
174
175   private VspDetails getVspDetails(String vspId, Version version) {
176
177     return vspInfoDao.get(new VspDetails(vspId, version));
178   }
179
180
181 }