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