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