2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
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.Base64;
26 import java.util.List;
28 import java.util.stream.Collectors;
30 import org.onap.aai.babel.service.data.BabelArtifact;
31 import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
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.config.ModelLoaderConfig;
37 import org.onap.aai.modelloader.entity.Artifact;
38 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
39 import org.onap.aai.modelloader.entity.model.IModelParser;
40 import org.onap.aai.modelloader.entity.model.NamedQueryArtifactParser;
41 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
42 import org.onap.aai.modelloader.restclient.BabelServiceClient;
43 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
44 import org.openecomp.sdc.api.IDistributionClient;
45 import org.openecomp.sdc.api.notification.IArtifactInfo;
46 import org.openecomp.sdc.api.notification.INotificationData;
47 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
48 import org.openecomp.sdc.utils.ArtifactTypeEnum;
49 import org.openecomp.sdc.utils.DistributionActionResultEnum;
52 * This class is responsible for downloading the artifacts from the ASDC.
54 * The downloads can be TOSCA_CSAR files or VNF_CATALOG files.
56 * The status of the download is published. The status of the extraction of yml files from a TOSCA_CSAR file is also
57 * published as a deployment event.
59 * TOSCA_CSAR file artifacts will be converted into XML and returned as model artifacts.
61 public class ArtifactDownloadManager {
63 private static Logger logger = LoggerFactory.getInstance().getLogger(ArtifactDownloadManager.class);
65 private IDistributionClient client;
66 private NotificationPublisher notificationPublisher;
67 private BabelArtifactConverter babelArtifactConverter;
68 private ModelLoaderConfig config;
70 public ArtifactDownloadManager(IDistributionClient client, ModelLoaderConfig config) {
76 * This method downloads the artifacts from the ASDC.
78 * @param data data about the notification that is being processed
79 * @param artifacts the specific artifacts found in the data.
80 * @param modelArtifacts collection of artifacts for model query specs
81 * @param catalogArtifacts collection of artifacts that represent vnf catalog files
82 * @return boolean <code>true</code> if the download process was successful otherwise <code>false</code>
84 boolean downloadArtifacts(INotificationData data, List<IArtifactInfo> artifacts, List<Artifact> modelArtifacts,
85 List<Artifact> catalogArtifacts) {
86 boolean success = true;
88 for (IArtifactInfo artifact : artifacts) {
90 IDistributionClientDownloadResult downloadResult = downloadIndividualArtifacts(data, artifact);
91 processDownloadedArtifacts(modelArtifacts, catalogArtifacts, artifact, downloadResult, data);
92 } catch (DownloadFailureException e) {
93 getNotificationPublisher().publishDownloadFailure(client, data, artifact, e.getMessage());
95 } catch (Exception e) {
96 getNotificationPublisher().publishDeployFailure(client, data, artifact);
108 private IDistributionClientDownloadResult downloadIndividualArtifacts(INotificationData data,
109 IArtifactInfo artifact) throws DownloadFailureException {
110 // Grab the current time so we can measure the download time for the metrics log
111 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
112 MdcOverride override = new MdcOverride();
113 override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));
115 IDistributionClientDownloadResult downloadResult = client.download(artifact);
117 logger.info(ModelLoaderMsgs.DOWNLOAD_COMPLETE, downloadResult.getDistributionActionResult().toString(),
118 downloadResult.getArtifactPayload() == null ? "null"
119 : Base64.getEncoder().encodeToString(downloadResult.getArtifactPayload()));
121 if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult())) {
122 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Downloaded artifact: " + artifact.getArtifactName());
123 getNotificationPublisher().publishDownloadSuccess(client, data, artifact);
125 throw new DownloadFailureException(downloadResult.getDistributionMessageResult());
128 return downloadResult;
131 private void processDownloadedArtifacts(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts,
132 IArtifactInfo artifactInfo, IDistributionClientDownloadResult downloadResult, INotificationData data)
133 throws ProcessToscaArtifactsException, InvalidArchiveException, BabelArtifactParsingException {
134 if ("TOSCA_CSAR".equalsIgnoreCase(artifactInfo.getArtifactType())) {
135 processToscaArtifacts(modelArtifacts, catalogArtifacts, downloadResult.getArtifactPayload(), artifactInfo,
136 data.getDistributionID(), data.getServiceVersion());
137 } else if (ArtifactTypeEnum.MODEL_QUERY_SPEC.toString().equalsIgnoreCase(artifactInfo.getArtifactType())) {
138 processModelQuerySpecArtifact(modelArtifacts, downloadResult);
140 logger.info(ModelLoaderMsgs.UNSUPPORTED_ARTIFACT_TYPE, artifactInfo.getArtifactName(),
141 artifactInfo.getArtifactType());
142 throw new InvalidArchiveException("Unsupported artifact type: " + artifactInfo.getArtifactType());
146 public void processToscaArtifacts(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts, byte[] payload,
147 IArtifactInfo artifactInfo, String distributionId, String serviceVersion)
148 throws ProcessToscaArtifactsException {
150 BabelServiceClient babelClient = createBabelServiceClient(artifactInfo, serviceVersion);
152 logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT,
153 "Posting artifact: " + artifactInfo.getArtifactName() + ", version: " + serviceVersion);
155 List<BabelArtifact> babelArtifacts =
156 babelClient.postArtifact(payload, artifactInfo.getArtifactName(), serviceVersion, distributionId);
158 // Sort Babel artifacts based on type
159 Map<ArtifactType, List<BabelArtifact>> artifactMap =
160 babelArtifacts.stream().collect(Collectors.groupingBy(BabelArtifact::getType));
162 if (artifactMap.containsKey(BabelArtifact.ArtifactType.MODEL)) {
163 modelArtifacts.addAll(
164 getBabelArtifactConverter().convertToModel(artifactMap.get(BabelArtifact.ArtifactType.MODEL)));
165 artifactMap.remove(BabelArtifact.ArtifactType.MODEL);
168 if (artifactMap.containsKey(BabelArtifact.ArtifactType.VNFCATALOG)) {
169 catalogArtifacts.addAll(getBabelArtifactConverter()
170 .convertToCatalog(artifactMap.get(BabelArtifact.ArtifactType.VNFCATALOG)));
171 artifactMap.remove(BabelArtifact.ArtifactType.VNFCATALOG);
174 // Log unexpected artifact types
175 if (!artifactMap.isEmpty()) {
176 logger.warn(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
177 artifactInfo.getArtifactName() + " " + serviceVersion
178 + ". Unexpected artifact types returned by the babel service: "
179 + artifactMap.keySet().toString());
182 } catch (BabelArtifactParsingException e) {
183 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
184 "Error for artifact " + artifactInfo.getArtifactName() + " " + serviceVersion + e);
185 throw new ProcessToscaArtifactsException(
186 "An error occurred while trying to parse the Babel artifacts: " + e.getLocalizedMessage());
187 } catch (Exception e) {
188 logger.error(ModelLoaderMsgs.BABEL_REST_REQUEST_ERROR, e, "POST", config.getBabelBaseUrl(),
189 "Error posting artifact " + artifactInfo.getArtifactName() + " " + serviceVersion + " to Babel: "
190 + e.getLocalizedMessage());
191 throw new ProcessToscaArtifactsException(
192 "An error occurred while calling the Babel service: " + e.getLocalizedMessage());
196 private BabelServiceClient createBabelServiceClient(IArtifactInfo artifact, String serviceVersion)
197 throws ProcessToscaArtifactsException {
198 BabelServiceClient babelClient;
200 logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Creating Babel client");
201 babelClient = new BabelServiceClient(config);
202 } catch (Exception e) {
203 logger.error(ModelLoaderMsgs.BABEL_REST_REQUEST_ERROR, e, "POST", config.getBabelBaseUrl(),
204 "Error posting artifact " + artifact.getArtifactName() + " " + serviceVersion + " to Babel: "
205 + e.getLocalizedMessage());
206 throw new ProcessToscaArtifactsException(
207 "An error occurred tyring to convert the tosca artifacts to xml artifacts: "
208 + e.getLocalizedMessage());
214 private void processModelQuerySpecArtifact(List<Artifact> modelArtifacts,
215 IDistributionClientDownloadResult downloadResult) throws BabelArtifactParsingException {
216 logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Processing named query artifact.");
218 IModelParser parser = new NamedQueryArtifactParser();
220 List<Artifact> parsedArtifacts =
221 parser.parse(new String(downloadResult.getArtifactPayload()), downloadResult.getArtifactFilename());
223 if (parsedArtifactsExist(parsedArtifacts)) {
224 modelArtifacts.addAll(parsedArtifacts);
226 throw new BabelArtifactParsingException(
227 "Could not parse generated XML: " + new String(downloadResult.getArtifactPayload()));
231 private boolean parsedArtifactsExist(List<Artifact> parsedArtifacts) {
232 return parsedArtifacts != null && !parsedArtifacts.isEmpty();
235 private NotificationPublisher getNotificationPublisher() {
236 if (notificationPublisher == null) {
237 notificationPublisher = new NotificationPublisher();
240 return notificationPublisher;
243 private BabelArtifactConverter getBabelArtifactConverter() {
244 if (babelArtifactConverter == null) {
245 babelArtifactConverter = new BabelArtifactConverter();
248 return babelArtifactConverter;