2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.modelloader.notification;
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;
43 * This class is responsible for publishing the status of actions performed working with artifacts.
45 public class NotificationPublisher {
47 private static Logger logger = LoggerFactory.getInstance().getLogger(NotificationPublisher.class);
48 private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(NotificationPublisher.class);
50 private boolean publishingEnabled;
52 public NotificationPublisher() {
53 Properties configProperties = new Properties();
54 try (InputStream configInputStream = Files.newInputStream(ModelLoaderConfig.propertiesFile())) {
55 configProperties.load(configInputStream);
56 } catch (IOException e) {
57 String errorMsg = "Failed to load configuration: " + e.getMessage();
58 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, e, errorMsg);
61 ModelLoaderConfig config = new ModelLoaderConfig(configProperties);
63 publishingEnabled = !config.getASDCConnectionDisabled();
67 * This method is responsible for publishing notification that the download of an artifact failed.
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
74 void publishDownloadFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
75 String errorMessage) {
76 publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_ERROR, client, data, artifact, "failure");
78 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
79 "Failed to download artifact " + artifact.getArtifactName() + ": " + errorMessage);
82 private void publishDownloadStatus(DistributionStatusEnum distributionStatusEnum, IDistributionClient client,
83 INotificationData data, IArtifactInfo artifact, String result) {
84 if (publishingEnabled) {
85 MdcOverride override = initMDCStartTime();
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());
92 if (sendDownloadStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
93 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Failed to publish download " + result
94 + " status: " + sendDownloadStatus.getDistributionMessageResult());
97 logPublishingDisabled(distributionStatusEnum.toString(), result);
101 private MdcOverride initMDCStartTime() {
102 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
103 MdcOverride override = new MdcOverride();
105 override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));
111 * This method is responsible for publishing notification that the download of an artifact was successful.
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
117 void publishDownloadSuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
118 publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_OK, client, data, artifact, "success");
120 if (logger.isDebugEnabled()) {
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();
139 * This method is responsible for publishing notification that the deployment of an artifact failed.
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
145 public void publishDeployFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
146 publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_ERROR, "failure");
151 * This method is responsible for publishing notification that the deployment of an artifact was succesful.
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
157 public void publishDeploySuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
158 publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_OK, "success");
161 public void publishComponentSuccess(IDistributionClient client, INotificationData data) {
162 if (publishingEnabled) {
163 MdcOverride override = initMDCStartTime();
165 IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
166 CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_OK));
168 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done ok", "all",
169 sendStatus.getDistributionActionResult().toString());
171 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
172 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
173 "Failed to publish component done ok: " + sendStatus.getDistributionMessageResult());
176 logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_OK.toString(), null);
180 public void publishComponentFailure(IDistributionClient client, INotificationData data, String errorReason) {
181 if (publishingEnabled) {
182 MdcOverride override = initMDCStartTime();
184 IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
185 CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_ERROR),
188 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done error", "all",
189 sendStatus.getDistributionActionResult().toString());
191 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
192 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
193 "Failed to publish component done error: " + sendStatus.getDistributionMessageResult());
196 logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_ERROR.toString(), errorReason);
200 private void publishDeployStatus(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
201 DistributionStatusEnum distributionStatusEnum, String result) {
202 if (publishingEnabled) {
203 MdcOverride override = initMDCStartTime();
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());
210 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
211 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
212 "Failed to publish deploy " + result + " status: " + sendStatus.getDistributionMessageResult());
215 logPublishingDisabled(distributionStatusEnum.toString(), result);
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);