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