aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/modelloader/notification/NotificationPublisher.java
blob: 7d5b1cc5c33ea02a9977ecb7c48dd5743d28005a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.modelloader.notification;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.cl.mdc.MdcContext;
import org.onap.aai.cl.mdc.MdcOverride;
import org.onap.aai.modelloader.config.ModelLoaderConfig;
import org.onap.aai.modelloader.service.ModelLoaderMsgs;
import org.onap.sdc.api.IDistributionClient;
import org.onap.sdc.api.notification.IArtifactInfo;
import org.onap.sdc.api.notification.INotificationData;
import org.onap.sdc.api.results.IDistributionClientResult;
import org.onap.sdc.utils.DistributionActionResultEnum;
import org.onap.sdc.utils.DistributionStatusEnum;
import org.springframework.stereotype.Component;

/**
 * This class is responsible for publishing the status of actions performed working with artifacts.
 */
@Component
public class NotificationPublisher {

    private static Logger logger = LoggerFactory.getInstance().getLogger(NotificationPublisher.class);
    private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(NotificationPublisher.class);

    private boolean publishingEnabled;

    public NotificationPublisher() {
        Properties configProperties = new Properties();
        try (InputStream configInputStream = Files.newInputStream(ModelLoaderConfig.propertiesFile())) {
            configProperties.load(configInputStream);
        } catch (IOException e) {
            String errorMsg = "Failed to load configuration: " + e.getMessage();
            logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, e, errorMsg);
        }

        ModelLoaderConfig config = new ModelLoaderConfig(configProperties);

        publishingEnabled = !config.getASDCConnectionDisabled();
    }

    /**
     * This method is responsible for publishing notification that the download of an artifact failed.
     *
     * @param client The distribution client this notification relates to
     * @param data data about the notification that resulted in this message being created
     * @param artifact the specific artifact to have its distribution status reported on
     * @param errorMessage the error message that is to be reported
     */
    void publishDownloadFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
            String errorMessage) {
        publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_ERROR, client, data, artifact, "failure");

        logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
                "Failed to download artifact " + artifact.getArtifactName() + ": " + errorMessage);
    }

    private void publishDownloadStatus(DistributionStatusEnum distributionStatusEnum, IDistributionClient client,
            INotificationData data, IArtifactInfo artifact, String result) {
        if (publishingEnabled) {
            MdcOverride override = initMDCStartTime();

            IDistributionClientResult sendDownloadStatus = client.sendDownloadStatus(
                    DistributionStatusMessageBuilder.build(client, data, artifact, distributionStatusEnum));
            metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "download " + result,
                    artifact.getArtifactName(), sendDownloadStatus.getDistributionActionResult().toString());

            if (sendDownloadStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
                logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Failed to publish download " + result
                        + " status: " + sendDownloadStatus.getDistributionMessageResult());
            }
        } else {
            logPublishingDisabled(distributionStatusEnum.toString(), result);
        }
    }

    private MdcOverride initMDCStartTime() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        MdcOverride override = new MdcOverride();

        override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));

        return override;
    }

    /**
     * This method is responsible for publishing notification that the download of an artifact was successful.
     *
     * @param client The distribution client this notification relates to
     * @param data data about the notification that resulted in this message being created
     * @param artifact the specific artifact to have its distribution status reported on
     */
    void publishDownloadSuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
        publishDownloadStatus(DistributionStatusEnum.DOWNLOAD_OK, client, data, artifact, "success");

        if (logger.isDebugEnabled()) {
            // @formatter:off
            String msg = "Downloaded artifact:\n" +
                "ArtInfo_Art_Name: " + artifact.getArtifactName() +
                "\nArtInfo_Art_description: " + artifact.getArtifactDescription() +
                "\nArtInfo_Art_CheckSum: " + artifact.getArtifactChecksum() +
                "\nArtInfo_Art_Url: " + artifact.getArtifactURL() +
                "\nArtInfo_Art_Type: " + artifact.getArtifactType() +
                "\nArtInfo_Serv_description: " + data.getServiceDescription() +
                "\nArtInfo_Serv_Name: " + data.getServiceName() +
                "\nGet_serviceVersion: " + data.getServiceVersion() +
                "\nGet_Service_UUID: " + data.getServiceUUID() +
                "\nArtInfo_DistributionId: " + data.getDistributionID();
            logger.debug(msg);
            // @formatter:on
        }
    }

    /**
     * This method is responsible for publishing notification that the deployment of an artifact failed.
     *
     * @param client The distribution client this notification relates to
     * @param data data about the notification that resulted in this message being created
     * @param artifact the specific artifact to have its deployment status reported on
     */
    public void publishDeployFailure(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
        publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_ERROR, "failure");
    }


    /**
     * This method is responsible for publishing notification that the deployment of an artifact was succesful.
     *
     * @param client The distribution client this notification relates to
     * @param data data about the notification that resulted in this message being created
     * @param artifact the specific artifact to have its deployment status reported on
     */
    public void publishDeploySuccess(IDistributionClient client, INotificationData data, IArtifactInfo artifact) {
        publishDeployStatus(client, data, artifact, DistributionStatusEnum.DEPLOY_OK, "success");
    }

    public void publishComponentSuccess(IDistributionClient client, INotificationData data) {
        if (publishingEnabled) {
            MdcOverride override = initMDCStartTime();

            IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
                    CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_OK));

            metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done ok", "all",
                    sendStatus.getDistributionActionResult().toString());

            if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
                logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
                        "Failed to publish component done ok: " + sendStatus.getDistributionMessageResult());
            }
        } else {
            logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_OK.toString(), null);
        }
    }

    public void publishComponentFailure(IDistributionClient client, INotificationData data, String errorReason) {
        if (publishingEnabled) {
            MdcOverride override = initMDCStartTime();

            IDistributionClientResult sendStatus = client.sendComponentDoneStatus(
                    CompDoneStatusMessageBuilder.build(client, data, DistributionStatusEnum.COMPONENT_DONE_ERROR),
                    errorReason);

            metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "component done error", "all",
                    sendStatus.getDistributionActionResult().toString());

            if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
                logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
                        "Failed to publish component done error: " + sendStatus.getDistributionMessageResult());
            }
        } else {
            logPublishingDisabled(DistributionStatusEnum.COMPONENT_DONE_ERROR.toString(), errorReason);
        }
    }

    private void publishDeployStatus(IDistributionClient client, INotificationData data, IArtifactInfo artifact,
            DistributionStatusEnum distributionStatusEnum, String result) {
        if (publishingEnabled) {
            MdcOverride override = initMDCStartTime();

            IDistributionClientResult sendStatus = client.sendDeploymentStatus(
                    DistributionStatusMessageBuilder.build(client, data, artifact, distributionStatusEnum));
            metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "deploy " + result,
                    artifact.getArtifactName(), sendStatus.getDistributionActionResult().toString());

            if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
                logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
                        "Failed to publish deploy " + result + " status: " + sendStatus.getDistributionMessageResult());
            }
        } else {
            logPublishingDisabled(distributionStatusEnum.toString(), result);
        }
    }

    private void logPublishingDisabled(String statusType, String message) {
        logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
                "Notification publishing is disabled, skipping publishing of the following status: " + statusType
                        + " with message: " + message);
    }
}