Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / sdclistener / application / src / main / java / org / onap / ccsdk / cds / sdclistener / SdcListenerNotificationCallback.java
1 /*
2  * Copyright © 2019 Bell Canada
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.sdclistener;
18
19 import org.onap.ccsdk.cds.sdclistener.dto.SdcListenerDto;
20 import org.onap.ccsdk.cds.sdclistener.service.ListenerService;
21 import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus;
22 import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus.NotificationType;
23 import org.onap.ccsdk.cds.sdclistener.util.FileUtil;
24 import org.onap.sdc.api.IDistributionClient;
25 import org.onap.sdc.api.consumer.INotificationCallback;
26 import org.onap.sdc.api.notification.IArtifactInfo;
27 import org.onap.sdc.api.notification.INotificationData;
28 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
29 import org.onap.sdc.utils.DistributionStatusEnum;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.boot.context.properties.ConfigurationProperties;
35 import org.springframework.context.annotation.ComponentScan;
36 import org.springframework.stereotype.Component;
37
38 import java.io.File;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.List;
42 import java.util.Objects;
43
44 import static org.onap.sdc.utils.DistributionActionResultEnum.SUCCESS;
45
46 @ConfigurationProperties("listenerservice")
47 @Component
48 @ComponentScan("org.onap.ccsdk.cds.cdssdclistener.dto")
49 public class SdcListenerNotificationCallback implements INotificationCallback {
50
51     @Autowired
52     private SdcListenerDto sdcListenerDto;
53
54     @Autowired
55     private ListenerService listenerService;
56
57     @Value("${listenerservice.config.archivePath}")
58     private String pathToStoreArchives;
59
60     @Autowired
61     private SdcListenerStatus listenerStatus;
62
63     private static final Logger LOGGER = LoggerFactory.getLogger(SdcListenerNotificationCallback.class);
64
65     @Override
66     public void activateCallback(INotificationData notificationData) {
67         final String distributionId = notificationData.getDistributionID();
68         sdcListenerDto.setDistributionId(distributionId);
69         LOGGER.info("Received service distribution from SDC with the id {}", distributionId);
70         processNotification(notificationData);
71     }
72
73     private void processNotification(INotificationData notificationData) {
74         final IDistributionClient distributionClient = sdcListenerDto.getDistributionClient();
75         notificationData.getServiceArtifacts()
76                 .forEach(artifactInfo -> downloadCsarArtifacts(artifactInfo, distributionClient));
77     }
78
79     /**
80      * Download the TOSCA CSAR artifact and process it.
81      *
82      * @param info - Artifact information
83      * @param distributionClient - SDC distribution client
84      */
85     private void downloadCsarArtifacts(IArtifactInfo info, IDistributionClient distributionClient) {
86         final String url = info.getArtifactURL();
87         sdcListenerDto.setArtifactUrl(url);
88         final String id = info.getArtifactUUID();
89         final String distributionId = sdcListenerDto.getDistributionId();
90
91         if (Objects.equals(info.getArtifactType(), SdcListenerConfiguration.TOSCA_CSAR)) {
92             LOGGER.info("Trying to download the artifact from : {} and UUID is {} ", url, id);
93
94             // Download the artifact
95             IDistributionClientDownloadResult result = distributionClient.download(info);
96
97             if (!Objects.equals(result.getDistributionActionResult(), SUCCESS)) {
98                 final String errorMessage = String.format("Failed to download the artifact from : %s due to %s ", url,
99                         result.getDistributionActionResult());
100                 listenerStatus.sendResponseBackToSdc(distributionId, DistributionStatusEnum.DOWNLOAD_ERROR,
101                         errorMessage, url, NotificationType.DOWNLOAD);
102                 LOGGER.error(errorMessage);
103             } else {
104                 listenerStatus.sendResponseBackToSdc(distributionId, DistributionStatusEnum.DOWNLOAD_OK, null, url,
105                         NotificationType.DOWNLOAD);
106                 LOGGER.info("Trying to write CSAR artifact to file  with URL {} and UUID {}", url, id);
107                 processCsarArtifact(result);
108             }
109         }
110     }
111
112     private void processCsarArtifact(IDistributionClientDownloadResult result) {
113         Path cbaArchivePath = Paths.get(pathToStoreArchives, "cba-archive");
114         Path csarArchivePath = Paths.get(pathToStoreArchives, "csar-archive");
115
116         // Extract and store the CSAR archive into local disk.
117         listenerService.extractCsarAndStore(result, csarArchivePath);
118
119         List<File> csarFiles = FileUtil.getFilesFromDisk(csarArchivePath);
120
121         if (!csarFiles.isEmpty()) {
122             final String archivePath = cbaArchivePath.toString();
123
124             // Extract CBA archive from CSAR package and store it into local disk
125             csarFiles.forEach(file -> listenerService.extractBluePrint(file.getAbsolutePath(), archivePath));
126             csarFiles.forEach(file -> FileUtil.deleteFile(file, csarArchivePath.toString()));
127         } else {
128             LOGGER.error("Could not able to read CSAR files from this location {}", csarArchivePath);
129         }
130
131         listenerService.saveBluePrintToCdsDatabase(cbaArchivePath, sdcListenerDto.getManagedChannelForGrpc());
132     }
133
134 }