Convert project from AJSC to Spring Boot
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / notification / NotificationPublisher.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.io.FileInputStream;
24 import java.io.IOException;
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
27 import java.util.Properties;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.cl.mdc.MdcContext;
31 import org.onap.aai.cl.mdc.MdcOverride;
32 import org.onap.aai.modelloader.config.ModelLoaderConfig;
33 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
34 import org.onap.sdc.api.IDistributionClient;
35 import org.onap.sdc.api.notification.IArtifactInfo;
36 import org.onap.sdc.api.notification.INotificationData;
37 import org.onap.sdc.api.results.IDistributionClientResult;
38 import org.onap.sdc.utils.DistributionActionResultEnum;
39 import org.onap.sdc.utils.DistributionStatusEnum;
40
41 /**
42  * This class is responsible for publishing the status of actions performed working with artifacts.
43  */
44 class NotificationPublisher {
45
46     private static Logger logger = LoggerFactory.getInstance().getLogger(NotificationPublisher.class);
47     private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(NotificationPublisher.class);
48
49     private boolean publishingEnabled;
50
51     public NotificationPublisher() {
52         Properties configProperties = new Properties();
53         try {
54             configProperties.load(new FileInputStream(ModelLoaderConfig.propertiesFile()));
55         } catch (IOException e) {
56             String errorMsg = "Failed to load configuration: " + e.getMessage();
57             logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, e, errorMsg);
58         }
59
60         ModelLoaderConfig config = new ModelLoaderConfig(configProperties);
61
62         publishingEnabled = !config.getASDCConnectionDisabled();
63     }
64
65     /**
66      * This method is responsible for publishing notification that the download of an artifact failed.
67      *
68      * @param client The distribution client this notification relates to
69      * @param data data about the notification that resulted in this message being created
70      * @param artifact the specific artifact to have its distribution status reported on
71      * @param errorMessage the error message that is to be reported
72      */
73     void publishDownloadFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
74             String errorMessage) {
75         publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_ERROR, client, data, artifact, "failure");
76
77         logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
78                 "Failed to download artifact " + artifact.getArtifactName() + ": " + errorMessage);
79     }
80
81     private void publishDownloadStatus(DistributionStatusEnum distributionStatusEnum, IDistributionClient client,
82             INotificationData data, IArtifactInfo artifact, String result) {
83         if (publishingEnabled) {
84             MdcOverride override = initMDCStartTime();
85
86             IDistributionClientResult sendDownloadStatus = client.sendDownloadStatus(
87                     DistributionStatusMessageBuilder.build(client, data, artifact, distributionStatusEnum));
88             metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "download " + result,
89                     artifact.getArtifactName(), sendDownloadStatus.getDistributionActionResult().toString());
90
91             if (sendDownloadStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
92                 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Failed to publish download " + result
93                         + " status: " + sendDownloadStatus.getDistributionMessageResult());
94             }
95         } else {
96             logPublishingDisabled(distributionStatusEnum.toString(), result);
97         }
98     }
99
100     private MdcOverride initMDCStartTime() {
101         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
102         MdcOverride override = new MdcOverride();
103
104         override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));
105
106         return override;
107     }
108
109     /**
110      * This method is responsible for publishing notification that the download of an artifact was successful.
111      *
112      * @param client The distribution client this notification relates to
113      * @param data data about the notification that resulted in this message being created
114      * @param artifact the specific artifact to have its distribution status reported on
115      */
116     void publishDownloadSuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
117         publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_OK, client, data, artifact, "success");
118
119         if (logger.isDebugEnabled()) {
120             // @formatter:off
121             String msg = "Downloaded artifact:\n" +
122                 "ArtInfo_Art_Name: " + artifact.getArtifactName() +
123                 "\nArtInfo_Art_description: " + artifact.getArtifactDescription() +
124                 "\nArtInfo_Art_CheckSum: " + artifact.getArtifactChecksum() +
125                 "\nArtInfo_Art_Url: " + artifact.getArtifactURL() +
126                 "\nArtInfo_Art_Type: " + artifact.getArtifactType() +
127                 "\nArtInfo_Serv_description: " + data.getServiceDescription() +
128                 "\nArtInfo_Serv_Name: " + data.getServiceName() +
129                 "\nGet_serviceVersion: " + data.getServiceVersion() +
130                 "\nGet_Service_UUID: " + data.getServiceUUID() +
131                 "\nArtInfo_DistributionId: " + data.getDistributionID();
132             logger.debug(msg);
133             // @formatter:on
134         }
135     }
136
137     /**
138      * This method is responsible for publishing notification that the deployment of an artifact failed.
139      *
140      * @param client The distribution client this notification relates to
141      * @param data data about the notification that resulted in this message being created
142      * @param artifact the specific artifact to have its deployment status reported on
143      */
144     void publishDeployFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
145         publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_ERROR, "failure");
146     }
147
148     private void publishDeployStatus(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
149             DistributionStatusEnum distributionStatusEnum, String result) {
150         if (publishingEnabled) {
151             MdcOverride override = initMDCStartTime();
152
153             IDistributionClientResult sendStatus = client.sendDeploymentStatus(
154                     DistributionStatusMessageBuilder.build(client, data, artifact, distributionStatusEnum));
155             metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "deploy " + result,
156                     artifact.getArtifactName(), sendStatus.getDistributionActionResult().toString());
157
158             if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
159                 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
160                         "Failed to publish deploy " + result + " status: " + sendStatus.getDistributionMessageResult());
161             }
162         } else {
163             logPublishingDisabled(distributionStatusEnum.toString(), result);
164         }
165     }
166
167     /**
168      * This method is responsible for publishing notification that the deployment of an artifact was succesful.
169      *
170      * @param client The distribution client this notification relates to
171      * @param data data about the notification that resulted in this message being created
172      * @param artifact the specific artifact to have its deployment status reported on
173      */
174     void publishDeploySuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
175         publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_OK, "success");
176     }
177
178     void publishComponentSuccess(IDistributionClient client, INotificationData data) {
179         if (publishingEnabled) {
180             MdcOverride override = initMDCStartTime();
181
182             IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
183                     CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_OK));
184
185             metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done ok", "all",
186                     sendStatus.getDistributionActionResult().toString());
187
188             if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
189                 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
190                         "Failed to publish component done ok: " + sendStatus.getDistributionMessageResult());
191             }
192         } else {
193             logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_OK.toString(), null);
194         }
195     }
196
197     void publishComponentFailure(IDistributionClient client, INotificationData data, String errorReason) {
198         if (publishingEnabled) {
199             MdcOverride override = initMDCStartTime();
200
201             IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
202                     CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_ERROR),
203                     errorReason);
204
205             metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done error", "all",
206                     sendStatus.getDistributionActionResult().toString());
207
208             if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
209                 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
210                         "Failed to publish component done error: " + sendStatus.getDistributionMessageResult());
211             }
212         } else {
213             logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_ERROR.toString(), errorReason);
214         }
215     }
216
217     private void logPublishingDisabled(String statusType, String message) {
218         logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
219                 "Notification publishing is disabled, skipping publishing of the following status: " + statusType
220                         + " with message: " + message);
221     }
222 }