aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/modelloader/notification/ArtifactDownloadManager.java
blob: dcec799b990db3161deb8cf19ba302075f056e16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.modelloader.notification;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.onap.aai.babel.service.data.BabelArtifact;
import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.cl.mdc.MdcContext;
import org.onap.aai.cl.mdc.MdcOverride;
import org.onap.aai.modelloader.config.ModelLoaderConfig;
import org.onap.aai.modelloader.entity.Artifact;
import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
import org.onap.aai.modelloader.entity.model.IModelParser;
import org.onap.aai.modelloader.entity.model.NamedQueryArtifactParser;
import org.onap.aai.modelloader.extraction.InvalidArchiveException;
import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
import org.onap.aai.modelloader.restclient.BabelServiceClient;
import org.onap.aai.modelloader.restclient.BabelServiceClientException;
import org.onap.aai.modelloader.service.BabelServiceClientFactory;
import org.onap.aai.modelloader.service.ModelLoaderMsgs;
import org.onap.sdc.api.IDistributionClient;
import org.onap.sdc.api.notification.IArtifactInfo;
import org.onap.sdc.api.notification.INotificationData;
import org.onap.sdc.api.results.IDistributionClientDownloadResult;
import org.onap.sdc.utils.ArtifactTypeEnum;
import org.onap.sdc.utils.DistributionActionResultEnum;
import org.springframework.stereotype.Component;

/**
 * This class is responsible for downloading the artifacts from the ASDC.
 *
 * The downloads can be TOSCA_CSAR files or VNF_CATALOG files.
 *
 * The status of the download is published. The status of the extraction of yml files from a TOSCA_CSAR file is also
 * published as a deployment event.
 *
 * TOSCA_CSAR file artifacts will be converted into XML and returned as model artifacts.
 */
@Component
public class ArtifactDownloadManager {

    private static Logger logger = LoggerFactory.getInstance().getLogger(ArtifactDownloadManager.class);

    private final IDistributionClient client;
    private final NotificationPublisher notificationPublisher;
    private final BabelArtifactConverter babelArtifactConverter;
    private final ModelLoaderConfig config;
    private final BabelServiceClientFactory clientFactory;
    private final VnfCatalogExtractor vnfCatalogExtractor;

    public ArtifactDownloadManager(IDistributionClient client, ModelLoaderConfig config,
            BabelServiceClientFactory clientFactory, BabelArtifactConverter babelArtifactConverter, NotificationPublisher notificationPublisher, VnfCatalogExtractor vnfCatalogExtractor) {
        this.client = client;
        this.notificationPublisher = notificationPublisher;
        this.babelArtifactConverter = babelArtifactConverter;
        this.config = config;
        this.clientFactory = clientFactory;
        this.vnfCatalogExtractor = vnfCatalogExtractor;
    }

    /**
     * This method downloads the artifacts from the ASDC.
     *
     * @param data data about the notification that is being processed
     * @param artifacts the specific artifacts found in the data.
     * @param modelArtifacts collection of artifacts for model query specs
     * @param catalogArtifacts collection of artifacts that represent vnf catalog files
     * @return boolean <code>true</code> if the download process was successful otherwise <code>false</code>
     */
    boolean downloadArtifacts(INotificationData data, List<IArtifactInfo> artifacts, List<Artifact> modelArtifacts,
            List<Artifact> catalogArtifacts) {
        boolean success = true;

        for (IArtifactInfo artifact : artifacts) {
            try {
                IDistributionClientDownloadResult downloadResult = downloadIndividualArtifacts(data, artifact);
                processDownloadedArtifacts(modelArtifacts, catalogArtifacts, artifact, downloadResult, data);
            } catch (DownloadFailureException e) {
                notificationPublisher.publishDownloadFailure(client, data, artifact, e.getMessage());
                success = false;
            } catch (Exception e) {
                notificationPublisher.publishDeployFailure(client, data, artifact);
                success = false;
            }

            if (!success) {
                break;
            }
        }

        return success;
    }

    private IDistributionClientDownloadResult downloadIndividualArtifacts(INotificationData data,
            IArtifactInfo artifact) throws DownloadFailureException {
        // Grab the current time so we can measure the download time for the metrics log
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        MdcOverride override = new MdcOverride();
        override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));

        IDistributionClientDownloadResult downloadResult = client.download(artifact);

        logger.info(ModelLoaderMsgs.DOWNLOAD_COMPLETE, downloadResult.getDistributionActionResult().toString(),
                downloadResult.getArtifactPayload() == null ? "null"
                        : Base64.getEncoder().encodeToString(downloadResult.getArtifactPayload()));

        if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult())) {
            logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Downloaded artifact: " + artifact.getArtifactName());
            notificationPublisher.publishDownloadSuccess(client, data, artifact);
        } else {
            throw new DownloadFailureException(downloadResult.getDistributionMessageResult());
        }

        return downloadResult;
    }

    private void processDownloadedArtifacts(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts,
            IArtifactInfo artifactInfo, IDistributionClientDownloadResult downloadResult, INotificationData data)
            throws ProcessToscaArtifactsException, InvalidArchiveException, BabelArtifactParsingException {
        if ("TOSCA_CSAR".equalsIgnoreCase(artifactInfo.getArtifactType())) {
            processToscaArtifacts(modelArtifacts, catalogArtifacts, downloadResult.getArtifactPayload(), artifactInfo,
                    data.getDistributionID(), data.getServiceVersion());
        } else if (ArtifactTypeEnum.MODEL_QUERY_SPEC.toString().equalsIgnoreCase(artifactInfo.getArtifactType())) {
            processModelQuerySpecArtifact(modelArtifacts, downloadResult);
        } else {
            logger.info(ModelLoaderMsgs.UNSUPPORTED_ARTIFACT_TYPE, artifactInfo.getArtifactName(),
                    artifactInfo.getArtifactType());
            throw new InvalidArchiveException("Unsupported artifact type: " + artifactInfo.getArtifactType());
        }
    }

    public void processToscaArtifacts(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts, byte[] payload,
            IArtifactInfo artifactInfo, String distributionId, String serviceVersion)
            throws ProcessToscaArtifactsException, InvalidArchiveException {
        // Get translated artifacts from Babel Service
        invokeBabelService(modelArtifacts, catalogArtifacts, payload, artifactInfo, distributionId, serviceVersion);

        // Get VNF Catalog artifacts directly from CSAR
        List<Artifact> csarCatalogArtifacts = vnfCatalogExtractor.extract(payload, artifactInfo.getArtifactName());

        // Throw an error if VNF Catalog data is present in the Babel payload and directly in the CSAR
        if (!catalogArtifacts.isEmpty() && !csarCatalogArtifacts.isEmpty()) {
            logger.error(ModelLoaderMsgs.DUPLICATE_VNFC_DATA_ERROR, artifactInfo.getArtifactName());
            throw new InvalidArchiveException("CSAR: " + artifactInfo.getArtifactName()
                    + " contains VNF Catalog data in the format of both TOSCA and XML files. Only one format can be used for each CSAR file.");
        } else if (!csarCatalogArtifacts.isEmpty()) {
            catalogArtifacts.addAll(csarCatalogArtifacts);
        }
    }

    public void invokeBabelService(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts, byte[] payload,
            IArtifactInfo artifactInfo, String distributionId, String serviceVersion)
            throws ProcessToscaArtifactsException {
        try {
            BabelServiceClient babelClient = createBabelServiceClient(artifactInfo, serviceVersion);

            logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
                    "Posting artifact: " + artifactInfo.getArtifactName() + ", service version: " + serviceVersion
                            + ", artifact version: " + artifactInfo.getArtifactVersion());

            List<BabelArtifact> babelArtifacts =
                    babelClient.postArtifact(payload, artifactInfo.getArtifactName(), serviceVersion, distributionId);

            // Sort Babel artifacts based on type
            Map<ArtifactType, List<BabelArtifact>> artifactMap =
                    babelArtifacts.stream().collect(Collectors.groupingBy(BabelArtifact::getType));

            if (artifactMap.containsKey(BabelArtifact.ArtifactType.MODEL)) {
                modelArtifacts.addAll(
                        babelArtifactConverter.convertToModel(artifactMap.get(BabelArtifact.ArtifactType.MODEL)));
                artifactMap.remove(BabelArtifact.ArtifactType.MODEL);
            }

            if (artifactMap.containsKey(BabelArtifact.ArtifactType.VNFCATALOG)) {
                catalogArtifacts.addAll(babelArtifactConverter
                        .convertToCatalog(artifactMap.get(BabelArtifact.ArtifactType.VNFCATALOG)));
                artifactMap.remove(BabelArtifact.ArtifactType.VNFCATALOG);
            }

            // Log unexpected artifact types
            if (!artifactMap.isEmpty()) {
                logger.warn(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
                        artifactInfo.getArtifactName() + " " + serviceVersion
                                + ". Unexpected artifact types returned by the babel service: "
                                + artifactMap.keySet().toString());
            }

        } catch (BabelArtifactParsingException e) {
            logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR,
                    "Error for artifact " + artifactInfo.getArtifactName() + " " + serviceVersion + e);
            throw new ProcessToscaArtifactsException(
                    "An error occurred while trying to parse the Babel artifacts: " + e.getLocalizedMessage());
        } catch (Exception e) {
            logger.error(ModelLoaderMsgs.BABEL_REST_REQUEST_ERROR, e, "POST", config.getBabelBaseUrl(),
                    "Error posting artifact " + artifactInfo.getArtifactName() + " " + serviceVersion + " to Babel: "
                            + e.getLocalizedMessage());
            throw new ProcessToscaArtifactsException(
                    "An error occurred while calling the Babel service: " + e.getLocalizedMessage());
        }
    }

    BabelServiceClient createBabelServiceClient(IArtifactInfo artifact, String serviceVersion)
            throws ProcessToscaArtifactsException {
        BabelServiceClient babelClient;
        try {
            logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Creating Babel client");
            babelClient = clientFactory.create(config);
        } catch (BabelServiceClientException e) {
            logger.error(ModelLoaderMsgs.BABEL_REST_REQUEST_ERROR, e, "POST", config.getBabelBaseUrl(),
                    "Error posting artifact " + artifact.getArtifactName() + " " + serviceVersion + " to Babel: "
                            + e.getLocalizedMessage());
            throw new ProcessToscaArtifactsException(
                    "An error occurred tyring to convert the tosca artifacts to xml artifacts: "
                            + e.getLocalizedMessage());
        }

        return babelClient;
    }

    private void processModelQuerySpecArtifact(List<Artifact> modelArtifacts,
            IDistributionClientDownloadResult downloadResult) throws BabelArtifactParsingException {
        logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Processing named query artifact.");

        IModelParser parser = new NamedQueryArtifactParser();

        List<Artifact> parsedArtifacts =
                parser.parse(new String(downloadResult.getArtifactPayload()), downloadResult.getArtifactFilename());

        if (parsedArtifactsExist(parsedArtifacts)) {
            modelArtifacts.addAll(parsedArtifacts);
        } else {
            throw new BabelArtifactParsingException(
                    "Could not parse generated XML: " + new String(downloadResult.getArtifactPayload()));
        }
    }

    private boolean parsedArtifactsExist(List<Artifact> parsedArtifacts) {
        return parsedArtifacts != null && !parsedArtifacts.isEmpty();
    }
}