2 * ============LICENSE_START==========================================
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
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.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;
42 * This class is responsible for publishing the status of actions performed working with artifacts.
44 class NotificationPublisher {
46 private static Logger logger = LoggerFactory.getInstance().getLogger(NotificationPublisher.class);
47 private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(NotificationPublisher.class);
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";
55 private boolean publishingEnabled;
57 public NotificationPublisher() {
58 Properties configProperties = new Properties();
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);
66 ModelLoaderConfig config = new ModelLoaderConfig(configProperties, CONFIG_AUTH_LOCATION);
68 publishingEnabled = !config.getASDCConnectionDisabled();
72 * This method is responsible for publishing notification that the download of an artifact failed.
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
79 void publishDownloadFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
80 String errorMessage) {
81 publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_ERROR, client, data, artifact, "failure");
83 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
84 "Failed to download artifact " + artifact.getArtifactName() + ": " + errorMessage);
87 private void publishDownloadStatus(DistributionStatusEnum distributionStatusEnum, IDistributionClient client,
88 INotificationData data, IArtifactInfo artifact, String result) {
89 if (publishingEnabled) {
90 MdcOverride override = initMDCStartTime();
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());
97 if (sendDownloadStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
98 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Failed to publish download " + result
99 + " status: " + sendDownloadStatus.getDistributionMessageResult());
102 logPublishingDisabled(distributionStatusEnum.toString(), result);
106 private MdcOverride initMDCStartTime() {
107 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
108 MdcOverride override = new MdcOverride();
110 override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));
116 * This method is responsible for publishing notification that the download of an artifact was successful.
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
122 void publishDownloadSuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
123 publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_OK, client, data, artifact, "success");
125 if (logger.isDebugEnabled()) {
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();
144 * This method is responsible for publishing notification that the deployment of an artifact failed.
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
150 void publishDeployFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
151 publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_ERROR, "failure");
154 private void publishDeployStatus(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
155 DistributionStatusEnum distributionStatusEnum, String result) {
156 if (publishingEnabled) {
157 MdcOverride override = initMDCStartTime();
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());
164 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
165 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
166 "Failed to publish deploy " + result + " status: " + sendStatus.getDistributionMessageResult());
169 logPublishingDisabled(distributionStatusEnum.toString(), result);
174 * This method is responsible for publishing notification that the deployment of an artifact was succesful.
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
180 void publishDeploySuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
181 publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_OK, "success");
184 void publishComponentSuccess(IDistributionClient client, INotificationData data) {
185 if (publishingEnabled) {
186 MdcOverride override = initMDCStartTime();
188 IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
189 CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_OK));
191 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done ok", "all",
192 sendStatus.getDistributionActionResult().toString());
194 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
195 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
196 "Failed to publish component done ok: " + sendStatus.getDistributionMessageResult());
199 logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_OK.toString(), null);
203 void publishComponentFailure(IDistributionClient client, INotificationData data, String errorReason) {
204 if (publishingEnabled) {
205 MdcOverride override = initMDCStartTime();
207 IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
208 CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_ERROR),
211 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done error", "all",
212 sendStatus.getDistributionActionResult().toString());
214 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
215 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
216 "Failed to publish component done error: " + sendStatus.getDistributionMessageResult());
219 logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_ERROR.toString(), errorReason);
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);