58c667c268027c00396f56ccd44e4bd433d3b5f3
[ccsdk/cds.git] / ms / cds-sdc-listener / application / src / main / java / org / onap / ccsdk / cds / cdssdclistener / CdsSdcListenerNotificationCallback.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 package org.onap.ccsdk.cds.cdssdclistener;
17
18 import static org.onap.sdc.utils.DistributionActionResultEnum.SUCCESS;
19 import java.io.File;
20 import java.nio.file.Path;
21 import java.nio.file.Paths;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
25 import org.onap.ccsdk.cds.cdssdclistener.dto.CdsSdcListenerDto;
26 import org.onap.ccsdk.cds.cdssdclistener.service.ListenerService;
27 import org.onap.ccsdk.cds.cdssdclistener.status.CdsSdcListenerStatus;
28 import org.onap.ccsdk.cds.cdssdclistener.util.FileUtil;
29 import org.onap.sdc.api.IDistributionClient;
30 import org.onap.sdc.api.consumer.INotificationCallback;
31 import org.onap.sdc.api.notification.IArtifactInfo;
32 import org.onap.sdc.api.notification.INotificationData;
33 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
34 import org.onap.sdc.utils.DistributionStatusEnum;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.boot.context.properties.ConfigurationProperties;
40 import org.springframework.context.annotation.ComponentScan;
41 import org.springframework.stereotype.Component;
42
43 @ConfigurationProperties("listenerservice")
44 @Component
45 @ComponentScan("org.onap.ccsdk.cds.cdssdclistener.dto")
46 public class CdsSdcListenerNotificationCallback implements INotificationCallback {
47
48     @Autowired
49     private CdsSdcListenerDto cdsSdcListenerDto;
50
51     @Autowired
52     private ListenerService listenerService;
53
54     @Value("${listenerservice.config.archivePath}")
55     private String pathToStoreArchives;
56
57     @Autowired
58     private CdsSdcListenerStatus listenerStatus;
59
60     private static final Logger LOGGER = LoggerFactory.getLogger(CdsSdcListenerNotificationCallback.class);
61
62     @Override
63     public void activateCallback(INotificationData notificationData) {
64         final String distributionId = notificationData.getDistributionID();
65         cdsSdcListenerDto.setDistributionId(distributionId);
66         LOGGER.info("Received service distribution from SDC with the id {}", distributionId);
67         processNotification(notificationData);
68     }
69
70     private void processNotification(INotificationData notificationData) {
71         final IDistributionClient distributionClient = cdsSdcListenerDto.getDistributionClient();
72         notificationData.getServiceArtifacts()
73             .forEach(artifactInfo -> downloadCsarArtifacts(artifactInfo, distributionClient));
74     }
75
76     /**
77      * Download the TOSCA CSAR artifact and process it.
78      *
79      * @param info - Artifact information
80      * @param distributionClient - SDC distribution client
81      */
82     private void downloadCsarArtifacts(IArtifactInfo info, IDistributionClient distributionClient) {
83         final String url = info.getArtifactURL();
84         final String id = info.getArtifactUUID();
85
86         if (Objects.equals(info.getArtifactType(), CdsSdcListenerConfiguration.TOSCA_CSAR)) {
87             LOGGER.info("Trying to download the artifact from : {} and UUID is {} ", url, id);
88
89             // Download the artifact
90             IDistributionClientDownloadResult result = distributionClient.download(info);
91
92             if (!Objects.equals(result.getDistributionActionResult(), SUCCESS)) {
93                 String errorMessage = String.format("Failed to download the artifact from : %s due to %s ", url,
94                     result.getDistributionActionResult());
95                 listenerStatus.sendResponseStatusBackToSDC(cdsSdcListenerDto.getDistributionId(),
96                     DistributionStatusEnum.COMPONENT_DONE_ERROR, errorMessage);
97                 LOGGER.error(errorMessage);
98             } else {
99                 LOGGER.info("Trying to write CSAR artifact to file  with URL {} and UUID {}", url, id);
100                 processCsarArtifact(result);
101             }
102         }
103     }
104
105     public void processCsarArtifact(IDistributionClientDownloadResult result) {
106         Path cbaArchivePath = Paths.get(pathToStoreArchives, "cba-archive");
107         Path csarArchivePath = Paths.get(pathToStoreArchives, "csar-archive");
108
109         // Extract and store the CSAR archive into local disk.
110         listenerService.extractCsarAndStore(result, csarArchivePath.toString());
111
112         Optional<List<File>> csarFiles = FileUtil.getFilesFromDisk(csarArchivePath);
113
114         if (csarFiles.isPresent()) {
115             //Extract CBA archive from CSAR package and store it into local disk.
116             List<File> files = csarFiles.get();
117
118             if (!files.isEmpty()) {
119                 final String archivePath = cbaArchivePath.toString();
120                 files.forEach(file -> listenerService.extractBluePrint(file.getAbsolutePath(), archivePath));
121                 files.forEach(file -> FileUtil.deleteFile(file, archivePath));
122             } else {
123                 LOGGER.error("The CSAR file is not present at this location {}", csarArchivePath);
124             }
125         }
126
127         listenerService.saveBluePrintToCdsDatabase(cbaArchivePath, cdsSdcListenerDto.getManagedChannelForGrpc());
128     }
129 }