d7bccbac63720abe479284809b866e8a22a728fd
[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.config.ModelLoaderConfig;
30 import org.onap.aai.modelloader.entity.Artifact;
31 import org.onap.aai.modelloader.extraction.ArtifactInfoExtractor;
32 import org.onap.aai.modelloader.service.ArtifactDeploymentManager;
33 import org.onap.aai.modelloader.service.BabelServiceClientFactory;
34 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
35 import org.onap.sdc.api.IDistributionClient;
36 import org.onap.sdc.api.consumer.INotificationCallback;
37 import org.onap.sdc.api.notification.IArtifactInfo;
38 import org.onap.sdc.api.notification.INotificationData;
39 import org.slf4j.MDC;
40
41 public class EventCallback implements INotificationCallback {
42     private static Logger logger = LoggerFactory.getInstance().getLogger(EventCallback.class.getName());
43     private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(EventCallback.class.getName());
44
45     private ArtifactDeploymentManager artifactDeploymentManager;
46     private ArtifactDownloadManager artifactDownloadManager;
47     private NotificationPublisher notificationPublisher;
48     private IDistributionClient client;
49     private ModelLoaderConfig config;
50     private BabelServiceClientFactory babelServiceClientFactory;
51
52     public EventCallback(IDistributionClient client, ModelLoaderConfig config, BabelServiceClientFactory babelServiceClientFactory) {
53         this.client = client;
54         this.config = config;
55         this.babelServiceClientFactory = babelServiceClientFactory;
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         List<Artifact> catalogArtifacts = new ArrayList<>();
65         List<Artifact> modelArtifacts = new ArrayList<>();
66
67         boolean success =
68                 getArtifactDownloadManager().downloadArtifacts(data, artifacts, modelArtifacts, catalogArtifacts);
69
70         if (success) {
71             success = getArtifactDeploymentManager().deploy(data, modelArtifacts, catalogArtifacts);
72         }
73
74         String statusString = success ? "SUCCESS" : "FAILURE";
75         auditLogger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
76                 "Processed distribution " + data.getDistributionID() + "  (" + statusString + ")");
77
78         publishNotifications(data, "TOSCA_CSAR", artifacts, success);
79
80         MDC.clear();
81     }
82
83
84     private void publishNotifications(INotificationData data, String filterType, List<IArtifactInfo> artifacts,
85             boolean deploymentSuccess) {
86         if (deploymentSuccess) {
87             artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType()))
88                     .forEach(a -> getNotificationPublisher().publishDeploySuccess(client, data, a));
89             getNotificationPublisher().publishComponentSuccess(client, data);
90         } else {
91             artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType()))
92                     .forEach(a -> getNotificationPublisher().publishDeployFailure(client, data, a));
93             getNotificationPublisher().publishComponentFailure(client, data, "deploy failure");
94         }
95     }
96
97     private ArtifactDeploymentManager getArtifactDeploymentManager() {
98         if (artifactDeploymentManager == null) {
99             artifactDeploymentManager = new ArtifactDeploymentManager(config);
100         }
101
102         return artifactDeploymentManager;
103     }
104
105     private ArtifactDownloadManager getArtifactDownloadManager() {
106         if (artifactDownloadManager == null) {
107             artifactDownloadManager = new ArtifactDownloadManager(client, config, babelServiceClientFactory);
108         }
109
110         return artifactDownloadManager;
111     }
112
113
114     private NotificationPublisher getNotificationPublisher() {
115         if (notificationPublisher == null) {
116             notificationPublisher = new NotificationPublisher();
117         }
118
119         return notificationPublisher;
120     }
121 }