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.
17 package org.onap.ccsdk.cds.sdclistener;
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;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.List;
42 import java.util.Objects;
44 import static org.onap.sdc.utils.DistributionActionResultEnum.SUCCESS;
46 @ConfigurationProperties("listenerservice")
48 @ComponentScan("org.onap.ccsdk.cds.cdssdclistener.dto")
49 public class SdcListenerNotificationCallback implements INotificationCallback {
52 private SdcListenerDto sdcListenerDto;
55 private ListenerService listenerService;
57 @Value("${listenerservice.config.archivePath}")
58 private String pathToStoreArchives;
61 private SdcListenerStatus listenerStatus;
63 private static final Logger LOGGER = LoggerFactory.getLogger(SdcListenerNotificationCallback.class);
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);
73 private void processNotification(INotificationData notificationData) {
74 final IDistributionClient distributionClient = sdcListenerDto.getDistributionClient();
75 notificationData.getServiceArtifacts()
76 .forEach(artifactInfo -> downloadCsarArtifacts(artifactInfo, distributionClient));
80 * Download the TOSCA CSAR artifact and process it.
82 * @param info - Artifact information
83 * @param distributionClient - SDC distribution client
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();
91 if (Objects.equals(info.getArtifactType(), SdcListenerConfiguration.TOSCA_CSAR)) {
92 LOGGER.info("Trying to download the artifact from : {} and UUID is {} ", url, id);
94 // Download the artifact
95 IDistributionClientDownloadResult result = distributionClient.download(info);
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);
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);
112 private void processCsarArtifact(IDistributionClientDownloadResult result) {
113 Path cbaArchivePath = Paths.get(pathToStoreArchives, "cba-archive");
114 Path csarArchivePath = Paths.get(pathToStoreArchives, "csar-archive");
116 // Extract and store the CSAR archive into local disk.
117 listenerService.extractCsarAndStore(result, csarArchivePath);
119 List<File> csarFiles = FileUtil.getFilesFromDisk(csarArchivePath);
121 if (!csarFiles.isEmpty()) {
122 final String archivePath = cbaArchivePath.toString();
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()));
128 LOGGER.error("Could not able to read CSAR files from this location {}", csarArchivePath);
131 listenerService.saveBluePrintToCdsDatabase(cbaArchivePath, sdcListenerDto.getManagedChannelForGrpc());