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