2 * Copyright (C) 2019 Bell Canada. All rights reserved.
4 * NOTICE: All the intellectual and technical concepts contained herein are
5 * proprietary to Bell Canada and are protected by trade secret or copyright law.
6 * Unauthorized copying of this file, via any medium is strictly prohibited.
9 package org.onap.ccsdk.cds.cdssdclistener;
11 import static org.onap.sdc.utils.DistributionActionResultEnum.SUCCESS;
13 import java.nio.file.Path;
14 import java.nio.file.Paths;
15 import java.util.List;
16 import java.util.Objects;
17 import java.util.Optional;
18 import org.onap.ccsdk.cds.cdssdclistener.dto.CdsSdcListenerDto;
19 import org.onap.ccsdk.cds.cdssdclistener.service.ListenerServiceImpl;
20 import org.onap.sdc.api.IDistributionClient;
21 import org.onap.sdc.api.consumer.INotificationCallback;
22 import org.onap.sdc.api.notification.IArtifactInfo;
23 import org.onap.sdc.api.notification.INotificationData;
24 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.boot.context.properties.ConfigurationProperties;
30 import org.springframework.context.annotation.ComponentScan;
31 import org.springframework.stereotype.Component;
33 @ConfigurationProperties("listenerservice")
35 @ComponentScan("org.onap.ccsdk.cds.cdssdclistener.dto")
36 public class CdsSdcListenerNotificationCallback implements INotificationCallback {
39 private CdsSdcListenerDto cdsSdcListenerDto;
42 private ListenerServiceImpl listenerService;
44 @Value("${listenerservice.config.archivePath}")
45 private String pathToStoreArchives;
47 private static final Logger LOGGER = LoggerFactory.getLogger(CdsSdcListenerNotificationCallback.class);
50 public void activateCallback(INotificationData notificationData) {
51 LOGGER.info(notificationData.getDistributionID(), "The UUID generated by SDC {}");
52 processNotification(notificationData);
55 private void processNotification(INotificationData notificationData) {
56 final IDistributionClient distributionClient = cdsSdcListenerDto.getDistributionClient();
57 notificationData.getServiceArtifacts()
58 .forEach(artifactInfo -> downloadCsarArtifacts(artifactInfo, distributionClient));
62 * Download the TOSCA CSAR artifact and process it.
64 * @param info - Artifact information
65 * @param distributionClient - SDC distribution client
67 private void downloadCsarArtifacts(IArtifactInfo info, IDistributionClient distributionClient) {
68 final String url = info.getArtifactURL();
69 final String id = info.getArtifactUUID();
71 if (Objects.equals(info.getArtifactType(), CdsSdcListenerConfiguration.TOSCA_CSAR)) {
72 LOGGER.info("Trying to download the artifact from : {} and UUID is {} ", url, id);
74 // Download the artifact
75 IDistributionClientDownloadResult result = distributionClient.download(info);
77 if (!Objects.equals(result.getDistributionActionResult(), SUCCESS)) {
78 LOGGER.error("Failed to download the artifact from : {} due to {} ", url,
79 result.getDistributionActionResult());
81 LOGGER.info("Trying to write CSAR artifact to file with URL {} and UUID {}", url, id);
82 processCsarArtifact(result);
87 public void processCsarArtifact(IDistributionClientDownloadResult result) {
88 Path cbaArchivePath = Paths.get(pathToStoreArchives, "cba-archive");
89 Path csarArchivePath = Paths.get(pathToStoreArchives, "csar-archive");
91 // extract and store the CSAR archive into local disk.
92 listenerService.extractCsarAndStore(result, csarArchivePath.toString());
94 Optional<List<File>> csarFiles = listenerService.getFilesFromDisk(csarArchivePath);
96 if (csarFiles.isPresent()) {
98 //Extract CBA archive from CSAR package and and store it into CDS Database.
100 .forEach(file -> listenerService.extractBluePrint(file.getAbsolutePath(), cbaArchivePath.toString()));
102 listenerService.saveBluePrintToCdsDatabase(cbaArchivePath);
104 LOGGER.error("The CSAR file is not present at this location {}", csarArchivePath);