Revisions made to the Model Loader to use Babel
[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 Amdocs
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 import org.onap.aai.cl.api.Logger;
26 import org.onap.aai.cl.eelf.LoggerFactory;
27 import org.onap.aai.cl.mdc.MdcContext;
28 import org.onap.aai.modelloader.config.ModelLoaderConfig;
29 import org.onap.aai.modelloader.entity.Artifact;
30 import org.onap.aai.modelloader.extraction.ArtifactInfoExtractor;
31 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
32 import org.openecomp.sdc.api.IDistributionClient;
33 import org.openecomp.sdc.api.consumer.INotificationCallback;
34 import org.openecomp.sdc.api.notification.IArtifactInfo;
35 import org.openecomp.sdc.api.notification.INotificationData;
36 import org.slf4j.MDC;
37
38 public class EventCallback implements INotificationCallback {
39     private static Logger logger = LoggerFactory.getInstance().getLogger(EventCallback.class.getName());
40     private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(EventCallback.class.getName());
41
42     private ArtifactDeploymentManager artifactDeploymentManager;
43     private ArtifactDownloadManager artifactDownloadManager;
44     private IDistributionClient client;
45     private ModelLoaderConfig config;
46
47     public EventCallback(IDistributionClient client, ModelLoaderConfig config) {
48         this.client = client;
49         this.config = config;
50     }
51
52     @Override
53     public void activateCallback(INotificationData data) {
54         MdcContext.initialize(data.getDistributionID(), "ModelLoader", "", "Event-Bus", "");
55         logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received distribution " + data.getDistributionID());
56
57         List<IArtifactInfo> artifacts = new ArtifactInfoExtractor().extract(data);
58         List<Artifact> catalogArtifacts = new ArrayList<>();
59         List<Artifact> modelArtifacts = new ArrayList<>();
60
61         boolean success = getArtifactDownloadManager()
62                 .downloadArtifacts(data, artifacts, modelArtifacts, catalogArtifacts);
63
64         if (success) {
65             success = getArtifactDeploymentManager().deploy(data, artifacts, modelArtifacts, catalogArtifacts);
66         }
67
68         String statusString = success ? "SUCCESS" : "FAILURE";
69         auditLogger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
70                 "Processed distribution " + data.getDistributionID() + "  (" + statusString + ")");
71         MDC.clear();
72     }
73
74     private ArtifactDeploymentManager getArtifactDeploymentManager() {
75         if (artifactDeploymentManager == null) {
76             artifactDeploymentManager = new ArtifactDeploymentManager(client, config);
77         }
78
79         return artifactDeploymentManager;
80     }
81
82     private ArtifactDownloadManager getArtifactDownloadManager() {
83         if (artifactDownloadManager == null) {
84             artifactDownloadManager = new ArtifactDownloadManager(client, config);
85         }
86
87         return artifactDownloadManager;
88     }
89 }