2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.modelloader.notification;
23 import java.time.ZonedDateTime;
24 import java.time.format.DateTimeFormatter;
25 import java.util.ArrayList;
26 import java.util.Base64;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
31 import org.onap.aai.babel.service.data.BabelRequest;
32 import org.onap.aai.cl.api.Logger;
33 import org.onap.aai.cl.eelf.LoggerFactory;
34 import org.onap.aai.cl.mdc.MdcContext;
35 import org.onap.aai.cl.mdc.MdcOverride;
36 import org.onap.aai.modelloader.babel.BabelArtifactService;
37 import org.onap.aai.modelloader.entity.Artifact;
38 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
39 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
40 import org.onap.aai.modelloader.entity.model.IModelParser;
41 import org.onap.aai.modelloader.entity.model.NamedQueryArtifactParser;
42 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
43 import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
44 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
45 import org.onap.sdc.api.IDistributionClient;
46 import org.onap.sdc.api.notification.IArtifactInfo;
47 import org.onap.sdc.api.notification.INotificationData;
48 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
49 import org.onap.sdc.utils.ArtifactTypeEnum;
50 import org.onap.sdc.utils.DistributionActionResultEnum;
51 import org.springframework.stereotype.Component;
54 * This class is responsible for downloading the artifacts from the ASDC.
56 * The downloads can be TOSCA_CSAR files or VNF_CATALOG files.
58 * The status of the download is published. The status of the extraction of yml files from a TOSCA_CSAR file is also
59 * published as a deployment event.
61 * TOSCA_CSAR file artifacts will be converted into XML and returned as model artifacts.
64 public class ArtifactDownloadManager {
66 private static Logger logger = LoggerFactory.getInstance().getLogger(ArtifactDownloadManager.class);
68 private final IDistributionClient client;
69 private final NotificationPublisher notificationPublisher;
70 private final VnfCatalogExtractor vnfCatalogExtractor;
71 private final BabelArtifactService babelArtifactService;
73 public ArtifactDownloadManager(IDistributionClient client,
74 NotificationPublisher notificationPublisher, VnfCatalogExtractor vnfCatalogExtractor, BabelArtifactService babelArtifactService) {
76 this.notificationPublisher = notificationPublisher;
77 this.vnfCatalogExtractor = vnfCatalogExtractor;
78 this.babelArtifactService = babelArtifactService;
82 * This method downloads the artifacts from the ASDC.
84 * @param data data about the notification that is being processed
85 * @param artifacts the specific artifacts found in the data.
86 * @param modelArtifacts collection of artifacts for model query specs
87 * @param catalogArtifacts collection of artifacts that represent vnf catalog files
88 * @return boolean <code>true</code> if the download process was successful otherwise <code>false</code>
91 List<Artifact> downloadArtifacts(INotificationData data, List<IArtifactInfo> artifacts) throws Exception {
93 List<Artifact> allArtifacts = new ArrayList<>();
94 for (IArtifactInfo artifact : artifacts) {
96 IDistributionClientDownloadResult downloadResult = downloadIndividualArtifacts(data, artifact);
97 List<Artifact> processedArtifacts = processDownloadedArtifacts(artifact, downloadResult, data);
98 allArtifacts.addAll(processedArtifacts);
99 } catch (DownloadFailureException e) {
100 notificationPublisher.publishDownloadFailure(client, data, artifact, e.getMessage());
102 } catch (ProcessToscaArtifactsException | InvalidArchiveException | BabelArtifactParsingException e) {
103 notificationPublisher.publishDeployFailure(client, data, artifact);
110 private IDistributionClientDownloadResult downloadIndividualArtifacts(INotificationData data,
111 IArtifactInfo artifact) throws DownloadFailureException {
112 // Grab the current time so we can measure the download time for the metrics log
113 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
114 MdcOverride override = new MdcOverride();
115 override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));
117 IDistributionClientDownloadResult downloadResult = client.download(artifact);
119 logger.info(ModelLoaderMsgs.DOWNLOAD_COMPLETE, downloadResult.getDistributionActionResult().toString(),
120 downloadResult.getArtifactPayload() == null ? "null"
121 : Base64.getEncoder().encodeToString(downloadResult.getArtifactPayload()));
123 if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult())) {
124 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Downloaded artifact: " + artifact.getArtifactName());
125 notificationPublisher.publishDownloadSuccess(client, data, artifact);
127 throw new DownloadFailureException(downloadResult.getDistributionMessageResult());
130 return downloadResult;
133 private List<Artifact> processDownloadedArtifacts(
134 IArtifactInfo artifactInfo, IDistributionClientDownloadResult downloadResult, INotificationData data)
135 throws ProcessToscaArtifactsException, InvalidArchiveException, BabelArtifactParsingException {
136 List<Artifact> artifacts = new ArrayList<>();
137 List<Artifact> querySpecArtifacts = new ArrayList<>();
138 if ("TOSCA_CSAR".equalsIgnoreCase(artifactInfo.getArtifactType())) {
139 artifacts = processToscaArtifacts(downloadResult.getArtifactPayload(), artifactInfo,
140 data.getDistributionID(), data.getServiceVersion());
142 } else if (ArtifactTypeEnum.MODEL_QUERY_SPEC.toString().equalsIgnoreCase(artifactInfo.getArtifactType())) {
143 querySpecArtifacts = processModelQuerySpecArtifact(downloadResult);
145 logger.info(ModelLoaderMsgs.UNSUPPORTED_ARTIFACT_TYPE, artifactInfo.getArtifactName(),
146 artifactInfo.getArtifactType());
147 throw new InvalidArchiveException("Unsupported artifact type: " + artifactInfo.getArtifactType());
150 .concat(artifacts.stream(), querySpecArtifacts.stream())
151 .collect(Collectors.toList());
154 public List<Artifact> processToscaArtifacts(byte[] payload, IArtifactInfo artifactInfo, String distributionId, String serviceVersion)
155 throws ProcessToscaArtifactsException, InvalidArchiveException {
156 // Get translated artifacts from Babel Service
157 BabelRequest babelRequest = new BabelRequest();
158 babelRequest.setArtifactName(artifactInfo.getArtifactName());
159 babelRequest.setCsar(Base64.getEncoder().encodeToString(payload));
160 babelRequest.setArtifactVersion(serviceVersion);
161 List<Artifact> artifacts = babelArtifactService.invokeBabelService(babelRequest, distributionId);
163 // Get VNF Catalog artifacts directly from CSAR
164 List<Artifact> csarCatalogArtifacts = vnfCatalogExtractor.extract(payload, artifactInfo.getArtifactName());
166 // Throw an error if VNF Catalog data is present in the Babel payload and directly in the CSAR
167 if (isDuplicateVnfCatalogData(artifacts, csarCatalogArtifacts)) {
168 logger.error(ModelLoaderMsgs.DUPLICATE_VNFC_DATA_ERROR, artifactInfo.getArtifactName());
169 throw new InvalidArchiveException("CSAR: " + artifactInfo.getArtifactName()
170 + " contains VNF Catalog data in the format of both TOSCA and XML files. Only one format can be used for each CSAR file.");
173 .concat(artifacts.stream(), csarCatalogArtifacts.stream())
174 .collect(Collectors.toList());
178 private boolean isDuplicateVnfCatalogData(List<Artifact> babelArtifacts, List<Artifact> csarCatalogArtifacts) {
179 boolean babelIsEmpty = babelArtifacts.stream()
180 .filter(VnfCatalogArtifact.class::isInstance)
181 .findAny().isEmpty();
182 return !csarCatalogArtifacts.isEmpty() && !babelIsEmpty;
185 private List<Artifact> processModelQuerySpecArtifact(IDistributionClientDownloadResult downloadResult) throws BabelArtifactParsingException {
186 logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Processing named query artifact.");
188 IModelParser parser = new NamedQueryArtifactParser();
190 List<Artifact> parsedArtifacts =
191 parser.parse(new String(downloadResult.getArtifactPayload()), downloadResult.getArtifactFilename());
193 if (parsedArtifacts != null && !parsedArtifacts.isEmpty()) {
194 return parsedArtifacts;
196 throw new BabelArtifactParsingException(
197 "Could not parse generated XML: " + new String(downloadResult.getArtifactPayload()));