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