d4c7151c17cd52d75ef404d8b9a05513d12c2f25
[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 java.io.IOException;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Optional;
24 import org.apache.commons.collections4.CollectionUtils;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.openecomp.core.utilities.json.JsonUtil;
28 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
29 import org.openecomp.sdc.common.errors.CoreException;
30 import org.openecomp.sdc.common.utils.CommonUtil;
31 import org.openecomp.sdc.common.utils.SdcCommon;
32 import org.openecomp.sdc.datatypes.error.ErrorMessage;
33 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
37 import org.openecomp.sdc.vendorsoftwareproduct.errors.OrchestrationTemplateNotFoundErrorBuilder;
38 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationTemplateFileHandler;
39 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUploadFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process.OrchestrationProcessFactory;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
48 import org.openecomp.sdc.versioning.dao.types.Version;
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(final VspDetails vspDetails,
65                                    final OnboardPackageInfo onboardPackageInfo) {
66     final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage();
67     final OrchestrationTemplateFileHandler orchestrationTemplateFileHandler =
68         OrchestrationUploadFactory.createOrchestrationTemplateFileHandler(onboardPackageInfo.getPackageType());
69
70     final UploadFileResponse uploadFileResponse =
71         orchestrationTemplateFileHandler.upload(vspDetails, onboardPackageInfo, candidateService);
72     uploadFileResponse.setNetworkPackageName(onboardPackage.getFilename());
73     return uploadFileResponse;
74   }
75
76   @Override
77   public OrchestrationTemplateActionResponse process(String vspId, Version version) {
78     OrchestrationTemplateCandidateData candidate =
79         candidateService.getOrchestrationTemplateCandidate(vspId, version)
80             .orElseThrow(() -> new CoreException(
81                 new OrchestrationTemplateNotFoundErrorBuilder(vspId).build()));
82
83     return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix())
84         .map(processor -> processor.process(getVspDetails(vspId, version), candidate))
85         .orElse(new OrchestrationTemplateActionResponse());
86   }
87
88   @Override
89   public Optional<FilesDataStructure> getFilesDataStructure(String vspId, Version version) {
90     return candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
91   }
92
93   @Override
94   public ValidationResponse updateFilesDataStructure(String vspId, Version version,
95                                                      FilesDataStructure fileDataStructure) {
96     ValidationResponse response = new ValidationResponse();
97     Optional<List<ErrorMessage>> validateErrors =
98         candidateService.validateFileDataStructure(fileDataStructure);
99     if (validateErrors.isPresent()) {
100       List<ErrorMessage> errorMessages = validateErrors.get();
101       if (CollectionUtils.isNotEmpty(errorMessages)) {
102         Map<String, List<ErrorMessage>> errorsMap = Collections.singletonMap(SdcCommon.UPLOAD_FILE, errorMessages);
103         response.setUploadDataErrors(errorsMap);
104         return response;
105       }
106     }
107     candidateService
108         .updateOrchestrationTemplateCandidateFileDataStructure(vspId, version, fileDataStructure);
109     return response;
110   }
111
112   @Override
113
114   public Optional<Pair<String, byte[]>> get(String vspId, Version version) throws IOException {
115     VspDetails vspDetails = getVspDetails(vspId, version);
116
117     Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
118         candidateService.getOrchestrationTemplateCandidate(vspId, version);
119
120     if (!candidateDataEntity.isPresent()) {
121       return Optional.empty();
122     }
123
124     if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) {
125       FilesDataStructure structure = JsonUtil
126           .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
127       String manifest = candidateService.createManifest(vspDetails, structure);
128       OnboardingTypesEnum type =
129           OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix());
130       return Optional.of(
131           new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService
132               .replaceManifestInZip(candidateDataEntity.get().getContentData(),
133                   manifest, type)));
134     }
135
136     return Optional.of(
137         new ImmutablePair<>(candidateDataEntity.get().getFileSuffix(), candidateDataEntity.get()
138             .getContentData().array()));
139   }
140
141   @Override
142   public Optional<OrchestrationTemplateCandidateData> getInfo(String vspId, Version version) {
143     return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version);
144   }
145
146   @Override
147   public void abort(String vspId, Version version) {
148     candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
149   }
150
151   private VspDetails getVspDetails(String vspId, Version version) {
152     return vspInfoDao.get(new VspDetails(vspId, version));
153   }
154 }