2 * Copyright © 2019 Bell Canada
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.ccsdk.cds.sdclistener;
18 import static org.onap.sdc.utils.DistributionActionResultEnum.SUCCESS;
20 import java.nio.file.Path;
21 import java.nio.file.Paths;
22 import java.util.List;
23 import java.util.Objects;
24 import org.onap.ccsdk.cds.sdclistener.dto.SdcListenerDto;
25 import org.onap.ccsdk.cds.sdclistener.service.ListenerService;
26 import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus;
27 import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus.NotificationType;
28 import org.onap.ccsdk.cds.sdclistener.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;
43 @ConfigurationProperties("listenerservice")
45 @ComponentScan("org.onap.ccsdk.cds.cdssdclistener.dto")
46 public class SdcListenerNotificationCallback implements INotificationCallback {
49 private SdcListenerDto sdcListenerDto;
52 private ListenerService listenerService;
54 @Value("${listenerservice.config.archivePath}")
55 private String pathToStoreArchives;
58 private SdcListenerStatus listenerStatus;
60 private static final Logger LOGGER = LoggerFactory.getLogger(SdcListenerNotificationCallback.class);
63 public void activateCallback(INotificationData notificationData) {
64 final String distributionId = notificationData.getDistributionID();
65 sdcListenerDto.setDistributionId(distributionId);
66 LOGGER.info("Received service distribution from SDC with the id {}", distributionId);
67 processNotification(notificationData);
70 private void processNotification(INotificationData notificationData) {
71 final IDistributionClient distributionClient = sdcListenerDto.getDistributionClient();
72 notificationData.getServiceArtifacts()
73 .forEach(artifactInfo -> downloadCsarArtifacts(artifactInfo, distributionClient));
77 * Download the TOSCA CSAR artifact and process it.
79 * @param info - Artifact information
80 * @param distributionClient - SDC distribution client
82 private void downloadCsarArtifacts(IArtifactInfo info, IDistributionClient distributionClient) {
83 final String url = info.getArtifactURL();
84 sdcListenerDto.setArtifactUrl(url);
85 final String id = info.getArtifactUUID();
86 final String distributionId = sdcListenerDto.getDistributionId();
88 if (Objects.equals(info.getArtifactType(), SdcListenerConfiguration.TOSCA_CSAR)) {
89 LOGGER.info("Trying to download the artifact from : {} and UUID is {} ", url, id);
91 // Download the artifact
92 IDistributionClientDownloadResult result = distributionClient.download(info);
94 if (!Objects.equals(result.getDistributionActionResult(), SUCCESS)) {
95 final String errorMessage = String.format("Failed to download the artifact from : %s due to %s ", url,
96 result.getDistributionActionResult());
98 .sendResponseBackToSdc(distributionId, DistributionStatusEnum.DOWNLOAD_ERROR, errorMessage,
99 url, NotificationType.DOWNLOAD);
100 LOGGER.error(errorMessage);
102 listenerStatus.sendResponseBackToSdc(distributionId, DistributionStatusEnum.DOWNLOAD_OK, null, url,
103 NotificationType.DOWNLOAD);
104 LOGGER.info("Trying to write CSAR artifact to file with URL {} and UUID {}", url, id);
105 processCsarArtifact(result);
110 private void processCsarArtifact(IDistributionClientDownloadResult result) {
111 Path cbaArchivePath = Paths.get(pathToStoreArchives, "cba-archive");
112 Path csarArchivePath = Paths.get(pathToStoreArchives, "csar-archive");
114 // Extract and store the CSAR archive into local disk.
115 listenerService.extractCsarAndStore(result, csarArchivePath);
117 List<File> csarFiles = FileUtil.getFilesFromDisk(csarArchivePath);
119 if (!csarFiles.isEmpty()) {
120 final String archivePath = cbaArchivePath.toString();
122 //Extract CBA archive from CSAR package and store it into local disk
123 csarFiles.forEach(file -> listenerService.extractBluePrint(file.getAbsolutePath(), archivePath));
124 csarFiles.forEach(file -> FileUtil.deleteFile(file, csarArchivePath.toString()));
126 LOGGER.error("Could not able to read CSAR files from this location {}", csarArchivePath);
129 listenerService.saveBluePrintToCdsDatabase(cbaArchivePath, sdcListenerDto.getManagedChannelForGrpc());