57f6b672bed490753edf21e0d60113cbde33c1ca
[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.Optional;
49
50 public class OrchestrationTemplateCandidateManagerImpl
51     implements OrchestrationTemplateCandidateManager {
52
53   private final VendorSoftwareProductInfoDao vspInfoDao;
54   private final CandidateService candidateService;
55
56   public OrchestrationTemplateCandidateManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
57                                                    CandidateService candidateService
58   ) {
59     this.vspInfoDao = vspInfoDao;
60     this.candidateService = candidateService;
61   }
62
63   @Override
64   public UploadFileResponse upload(String vspId, Version version, InputStream fileToUpload,
65                                    String fileSuffix, String networkPackageName) {
66     OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
67         OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(fileSuffix);
68
69     VspDetails vspDetails = getVspDetails(vspId, version);
70
71     UploadFileResponse uploadResponse = orchestrationTemplateFileHandler
72         .upload(vspDetails, fileToUpload, fileSuffix, networkPackageName, candidateService);
73
74     uploadResponse.setNetworkPackageName(networkPackageName);
75     return uploadResponse;
76   }
77
78   @Override
79   public OrchestrationTemplateActionResponse process(String vspId, Version version) {
80     OrchestrationTemplateCandidateData candidate =
81         candidateService.getOrchestrationTemplateCandidate(vspId, version)
82             .orElseThrow(() -> new CoreException(
83                 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         candidateService.getOrchestrationTemplateCandidate(vspId, version);
121
122     if (!candidateDataEntity.isPresent()) {
123       return Optional.empty();
124     }
125
126     if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
127       FilesDataStructure structure = JsonUtil
128           .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
129       String manifest = candidateService.createManifest(vspDetails, structure);
130       OnboardingTypesEnum type =
131           OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
132       return Optional.of(
133           new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
134               .replaceManifestInZip(candidateDataEntity.get().getContentData(),
135                   manifest, type)));
136     }
137
138     return Optional.of(
139         new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
140             .getContentData().array()));
141   }
142
143   @Override
144   public Optional<OrchestrationTemplateCandidateData> getInfo(String vspId, Version version) {
145     return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
146   }
147
148   @Override
149   public void abort(String vspId, Version version) {
150     candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
151   }
152
153   private VspDetails getVspDetails(String vspId, Version version) {
154     return vspInfoDao.get(new VspDetails(vspId, version));
155   }
156 }