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.nio.file.Files;
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;
42 * This class is responsible for publishing the status of actions performed working with artifacts.
44 public class NotificationPublisher {
46 private static Logger logger = LoggerFactory.getInstance().getLogger(NotificationPublisher.class);
47 private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(NotificationPublisher.class);
49 private boolean publishingEnabled;
51 public NotificationPublisher() {
52 Properties configProperties = new Properties();
54 configProperties.load(Files.newInputStream(ModelLoaderConfig.propertiesFile()));
55 } catch (IOException e) {
56 String errorMsg = "Failed to load configuration: " + e.getMessage();
57 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, e, errorMsg);
60 ModelLoaderConfig config = new ModelLoaderConfig(configProperties);
62 publishingEnabled = !config.getASDCConnectionDisabled();
66 * This method is responsible for publishing notification that the download of an artifact failed.
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
73 void publishDownloadFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
74 String errorMessage) {
75 publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_ERROR, client, data, artifact, "failure");
77 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
78 "Failed to download artifact " + artifact.getArtifactName() + ": " + errorMessage);
81 private void publishDownloadStatus(DistributionStatusEnum distributionStatusEnum, IDistributionClient client,
82 INotificationData data, IArtifactInfo artifact, String result) {
83 if (publishingEnabled) {
84 MdcOverride override = initMDCStartTime();
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());
91 if (sendDownloadStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
92 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Failed to publish download " + result
93 + " status: " + sendDownloadStatus.getDistributionMessageResult());
96 logPublishingDisabled(distributionStatusEnum.toString(), result);
100 private MdcOverride initMDCStartTime() {
101 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
102 MdcOverride override = new MdcOverride();
104 override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));
110 * This method is responsible for publishing notification that the download of an artifact was successful.
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
116 void publishDownloadSuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
117 publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_OK, client, data, artifact, "success");
119 if (logger.isDebugEnabled()) {
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();
138 * This method is responsible for publishing notification that the deployment of an artifact failed.
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
144 public void publishDeployFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
145 publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_ERROR, "failure");
150 * This method is responsible for publishing notification that the deployment of an artifact was succesful.
152 * @param client The distribution client this notification relates to
153 * @param data data about the notification that resulted in this message being created
154 * @param artifact the specific artifact to have its deployment status reported on
156 public void publishDeploySuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
157 publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_OK, "success");
160 public void publishComponentSuccess(IDistributionClient client, INotificationData data) {
161 if (publishingEnabled) {
162 MdcOverride override = initMDCStartTime();
164 IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
165 CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_OK));
167 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done ok", "all",
168 sendStatus.getDistributionActionResult().toString());
170 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
171 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
172 "Failed to publish component done ok: " + sendStatus.getDistributionMessageResult());
175 logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_OK.toString(), null);
179 public void publishComponentFailure(IDistributionClient client, INotificationData data, String errorReason) {
180 if (publishingEnabled) {
181 MdcOverride override = initMDCStartTime();
183 IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
184 CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_ERROR),
187 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done error", "all",
188 sendStatus.getDistributionActionResult().toString());
190 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
191 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
192 "Failed to publish component done error: " + sendStatus.getDistributionMessageResult());
195 logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_ERROR.toString(), errorReason);
199 private void publishDeployStatus(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
200 DistributionStatusEnum distributionStatusEnum, String result) {
201 if (publishingEnabled) {
202 MdcOverride override = initMDCStartTime();
204 IDistributionClientResult sendStatus = client.sendDeploymentStatus(
205 DistributionStatusMessageBuilder.build(client, data, artifact, distributionStatusEnum));
206 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "deploy " + result,
207 artifact.getArtifactName(), sendStatus.getDistributionActionResult().toString());
209 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
210 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
211 "Failed to publish deploy " + result + " status: " + sendStatus.getDistributionMessageResult());
214 logPublishingDisabled(distributionStatusEnum.toString(), result);
218 private void logPublishingDisabled(String statusType, String message) {
219 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
220 "Notification publishing is disabled, skipping publishing of the following status: " + statusType
221 + " with message: " + message);