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;
41 import org.springframework.stereotype.Component;
44 * This class is responsible for publishing the status of actions performed working with artifacts.
47 public class NotificationPublisher {
49 private static Logger logger = LoggerFactory.getInstance().getLogger(NotificationPublisher.class);
50 private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(NotificationPublisher.class);
52 private boolean publishingEnabled;
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);
63 ModelLoaderConfig config = new ModelLoaderConfig(configProperties);
65 publishingEnabled = !config.getASDCConnectionDisabled();
69 * This method is responsible for publishing notification that the download of an artifact failed.
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
76 void publishDownloadFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
77 String errorMessage) {
78 publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_ERROR, client, data, artifact, "failure");
80 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
81 "Failed to download artifact " + artifact.getArtifactName() + ": " + errorMessage);
84 private void publishDownloadStatus(DistributionStatusEnum distributionStatusEnum, IDistributionClient client,
85 INotificationData data, IArtifactInfo artifact, String result) {
86 if (publishingEnabled) {
87 MdcOverride override = initMDCStartTime();
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());
94 if (sendDownloadStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
95 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Failed to publish download " + result
96 + " status: " + sendDownloadStatus.getDistributionMessageResult());
99 logPublishingDisabled(distributionStatusEnum.toString(), result);
103 private MdcOverride initMDCStartTime() {
104 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
105 MdcOverride override = new MdcOverride();
107 override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));
113 * This method is responsible for publishing notification that the download of an artifact was successful.
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
119 void publishDownloadSuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
120 publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_OK, client, data, artifact, "success");
122 if (logger.isDebugEnabled()) {
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();
141 * This method is responsible for publishing notification that the deployment of an artifact failed.
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
147 public void publishDeployFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
148 publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_ERROR, "failure");
153 * This method is responsible for publishing notification that the deployment of an artifact was succesful.
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
159 public void publishDeploySuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
160 publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_OK, "success");
163 public void publishComponentSuccess(IDistributionClient client, INotificationData data) {
164 if (publishingEnabled) {
165 MdcOverride override = initMDCStartTime();
167 IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
168 CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_OK));
170 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done ok", "all",
171 sendStatus.getDistributionActionResult().toString());
173 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
174 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
175 "Failed to publish component done ok: " + sendStatus.getDistributionMessageResult());
178 logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_OK.toString(), null);
182 public void publishComponentFailure(IDistributionClient client, INotificationData data, String errorReason) {
183 if (publishingEnabled) {
184 MdcOverride override = initMDCStartTime();
186 IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
187 CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_ERROR),
190 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done error", "all",
191 sendStatus.getDistributionActionResult().toString());
193 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
194 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
195 "Failed to publish component done error: " + sendStatus.getDistributionMessageResult());
198 logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_ERROR.toString(), errorReason);
202 private void publishDeployStatus(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
203 DistributionStatusEnum distributionStatusEnum, String result) {
204 if (publishingEnabled) {
205 MdcOverride override = initMDCStartTime();
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());
212 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
213 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
214 "Failed to publish deploy " + result + " status: " + sendStatus.getDistributionMessageResult());
217 logPublishingDisabled(distributionStatusEnum.toString(), result);
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);