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