e2aae9654edd4ab88240ef7c9c4cb404179c6412
[ccsdk/cds.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada. All rights reserved.
3  *
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.
7  */
8
9 package org.onap.ccsdk.cds.cdssdclistener;
10
11 import static org.onap.sdc.utils.DistributionActionResultEnum.SUCCESS;
12 import java.io.File;
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;
32
33 @ConfigurationProperties("listenerservice")
34 @Component
35 @ComponentScan("org.onap.ccsdk.cds.cdssdclistener.dto")
36 public class CdsSdcListenerNotificationCallback implements INotificationCallback {
37
38     @Autowired
39     private CdsSdcListenerDto cdsSdcListenerDto;
40
41     @Autowired
42     private ListenerServiceImpl listenerService;
43
44     @Value("${listenerservice.config.archivePath}")
45     private String pathToStoreArchives;
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(CdsSdcListenerNotificationCallback.class);
48
49     @Override
50     public void activateCallback(INotificationData notificationData) {
51         LOGGER.info(notificationData.getDistributionID(), "The UUID generated by SDC {}");
52         processNotification(notificationData);
53     }
54
55     private void processNotification(INotificationData notificationData) {
56         final IDistributionClient distributionClient = cdsSdcListenerDto.getDistributionClient();
57         notificationData.getServiceArtifacts()
58             .forEach(artifactInfo -> downloadCsarArtifacts(artifactInfo, distributionClient));
59     }
60
61     /**
62      * Download the TOSCA CSAR artifact and process it.
63      *
64      * @param info - Artifact information
65      * @param distributionClient - SDC distribution client
66      */
67     private void downloadCsarArtifacts(IArtifactInfo info, IDistributionClient distributionClient) {
68         final String url = info.getArtifactURL();
69         final String id = info.getArtifactUUID();
70
71         if (Objects.equals(info.getArtifactType(), CdsSdcListenerConfiguration.TOSCA_CSAR)) {
72             LOGGER.info("Trying to download the artifact from : {} and UUID is {} ", url, id);
73
74             // Download the artifact
75             IDistributionClientDownloadResult result = distributionClient.download(info);
76
77             if (!Objects.equals(result.getDistributionActionResult(), SUCCESS)) {
78                 LOGGER.error("Failed to download the artifact from : {} due to {} ", url,
79                     result.getDistributionActionResult());
80             } else {
81                 LOGGER.info("Trying to write CSAR artifact to file  with URL {} and UUID {}", url, id);
82                 processCsarArtifact(result);
83             }
84         }
85     }
86
87     public void processCsarArtifact(IDistributionClientDownloadResult result) {
88         Path cbaArchivePath = Paths.get(pathToStoreArchives, "cba-archive");
89         Path csarArchivePath = Paths.get(pathToStoreArchives, "csar-archive");
90
91         // extract and store the CSAR archive into local disk.
92         listenerService.extractCsarAndStore(result, csarArchivePath.toString());
93
94         Optional<List<File>> csarFiles = listenerService.getFilesFromDisk(csarArchivePath);
95
96         if (csarFiles.isPresent()) {
97
98             //Extract CBA archive from CSAR package and and store it into CDS Database.
99             csarFiles.get()
100                 .forEach(file -> listenerService.extractBluePrint(file.getAbsolutePath(), cbaArchivePath.toString()));
101
102             listenerService.saveBluePrintToCdsDatabase(cbaArchivePath);
103         } else {
104             LOGGER.error("The CSAR file is not present at this location {}", csarArchivePath);
105         }
106     }
107 }