Return List<Artifact> in ArtifactDownloadManager
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / notification / EventCallback.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.modelloader.notification;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.cl.mdc.MdcContext;
29 import org.onap.aai.modelloader.entity.Artifact;
30 import org.onap.aai.modelloader.entity.ArtifactType;
31 import org.onap.aai.modelloader.extraction.ArtifactInfoExtractor;
32 import org.onap.aai.modelloader.service.ArtifactDeploymentManager;
33 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
34 import org.onap.sdc.api.IDistributionClient;
35 import org.onap.sdc.api.consumer.INotificationCallback;
36 import org.onap.sdc.api.notification.IArtifactInfo;
37 import org.onap.sdc.api.notification.INotificationData;
38 import org.slf4j.MDC;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class EventCallback implements INotificationCallback {
43     private static Logger logger = LoggerFactory.getInstance().getLogger(EventCallback.class.getName());
44     private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(EventCallback.class.getName());
45
46     private final ArtifactDeploymentManager artifactDeploymentManager;
47     private final ArtifactDownloadManager artifactDownloadManager;
48     private final NotificationPublisher notificationPublisher;
49     private final IDistributionClient client;
50
51     public EventCallback(IDistributionClient client, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager, NotificationPublisher notificationPublisher) {
52         this.artifactDeploymentManager = artifactDeploymentManager;
53         this.artifactDownloadManager = artifactDownloadManager;
54         this.notificationPublisher = notificationPublisher;
55         this.client = client;
56     }
57
58     @Override
59     public void activateCallback(INotificationData data) {
60         MdcContext.initialize(data.getDistributionID(), "ModelLoader", "", "Event-Bus", "");
61         logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received distribution " + data.getDistributionID());
62
63         List<IArtifactInfo> artifacts = new ArtifactInfoExtractor().extract(data);
64         boolean success = true;
65         List<Artifact> downloadedArtifacts = new ArrayList<>();
66         try {
67             downloadedArtifacts =
68                     artifactDownloadManager.downloadArtifacts(data, artifacts);
69         } catch (Exception e) {
70             success = false;
71         }
72
73         List<Artifact> catalogArtifacts = new ArrayList<>();
74         List<Artifact> modelArtifacts = new ArrayList<>();
75         if(downloadedArtifacts != null) {
76             for(Artifact artifact : downloadedArtifacts) {
77                 if(artifact.getType() == ArtifactType.VNF_CATALOG || artifact.getType() == ArtifactType.VNF_CATALOG_XML) {
78                     catalogArtifacts.add(artifact);
79                 } else {
80                     modelArtifacts.add(artifact);
81                 }
82             }
83         }
84
85         if (success) {
86             success = artifactDeploymentManager.deploy(data.getDistributionID(), modelArtifacts, catalogArtifacts);
87         }
88
89         String statusString = success ? "SUCCESS" : "FAILURE";
90         auditLogger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
91                 "Processed distribution " + data.getDistributionID() + "  (" + statusString + ")");
92
93         publishNotifications(data, "TOSCA_CSAR", artifacts, success);
94
95         MDC.clear();
96     }
97
98
99     private void publishNotifications(INotificationData data, String filterType, List<IArtifactInfo> artifacts,
100             boolean deploymentSuccess) {
101         if (deploymentSuccess) {
102             artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType()))
103                     .forEach(a -> notificationPublisher.publishDeploySuccess(client, data, a));
104             notificationPublisher.publishComponentSuccess(client, data);
105         } else {
106             artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType()))
107                     .forEach(a -> notificationPublisher.publishDeployFailure(client, data, a));
108             notificationPublisher.publishComponentFailure(client, data, "deploy failure");
109         }
110     }
111 }