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