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