summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/validation/publisher/ValidationEventPublisher.java
blob: 4b0b58370c71b9b5159bc5de95d2d5ca0a308803 (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2018-2019 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.validation.publisher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject;
import org.onap.aai.event.client.DMaaPEventPublisher;
import org.onap.aai.validation.config.TopicAdminConfig;
import org.onap.aai.validation.config.TopicConfig;
import org.onap.aai.validation.config.TopicConfig.Topic;
import org.onap.aai.validation.exception.ValidationServiceError;
import org.onap.aai.validation.exception.ValidationServiceException;
import org.onap.aai.validation.factory.DMaaPEventPublisherFactory;
import org.onap.aai.validation.logging.ApplicationMsgs;
import org.onap.aai.validation.logging.LogHelper;

/**
 * Event Publisher
 *
 */
public class ValidationEventPublisher implements MessagePublisher {

    private static LogHelper applicationLogger = LogHelper.INSTANCE;

    private List<Topic> publisherTopics;

    private boolean enablePublishing;

    private long retries;

    private long retriesRemaining;

    private DMaaPEventPublisherFactory dMaapFactory;


    /**
     * Instantiates an Event Publisher instance using properties from config file.
     *
     * @param topicConfig
     * @param topicAdminConfig
     */
    @Inject
    public ValidationEventPublisher(TopicConfig topicConfig, TopicAdminConfig topicAdminConfig) {
        enablePublishing = topicAdminConfig.isPublishEnable();
        if (enablePublishing) {
            publisherTopics = topicConfig.getPublisherTopics();
            retries = topicAdminConfig.getPublishRetries();
        }
        dMaapFactory = new DMaaPEventPublisherFactory();
    }

    /**
     * Connect to the event publisher, add the message, and then publish it by closing the publisher.
     */
    @Override
    public void publishMessage(String message) throws ValidationServiceException {
        Collection<String> messages = new ArrayList<>();
        messages.add(message);
        publishMessages(messages);
    }

    /**
     * Connect to the event publisher, adds the messages, and then publish them by closing the publisher.
     */
    @Override
    public void publishMessages(Collection<String> messages) throws ValidationServiceException {
        if (enablePublishing) {
            applicationLogger.debug("Publishing messages: " + messages);
            for (Topic topic : publisherTopics) {
                retriesRemaining = retries;
                publishMessages(messages, topic);
            }
        }
    }

    private void publishMessages(Collection<String> messages, Topic topic) throws ValidationServiceException {

        DMaaPEventPublisher dMaapEventPublisher = dMaapFactory.createEventPublisher(topic.getHost(), topic.getName(),
                topic.getUsername(), topic.getPassword(), topic.getTransportType(), topic.getProtocol());

        try {
            // Add our message to the publisher's queue/bus
            int result = dMaapEventPublisher.sendSync(topic.getPartition(), messages);
            if (result != messages.size()) {
                applicationLogger.warn(ApplicationMsgs.UNSENT_MESSAGE_WARN);
                closeEventPublisher(dMaapEventPublisher);
                retryOrThrow(messages, topic, new ValidationServiceException(
                        ValidationServiceError.EVENT_CLIENT_INCORRECT_NUMBER_OF_MESSAGES_SENT, result));
            }
        } catch (Exception e) {
            applicationLogger.error(ApplicationMsgs.UNSENT_MESSAGE_ERROR);
            closeEventPublisher(dMaapEventPublisher);
            retryOrThrow(messages, topic,
                    new ValidationServiceException(ValidationServiceError.EVENT_CLIENT_SEND_ERROR, e));
        }

        completeMessageSending(dMaapEventPublisher, topic);
    }

    /**
     * Publish the queued messages by closing the publisher.
     *
     * @param eventPublisher
     *            the publisher to close
     * @throws AuditException
     */
    private void completeMessageSending(DMaaPEventPublisher eventPublisher, Topic topic)
            throws ValidationServiceException {
        List<String> unsentMsgs = closeEventPublisher(eventPublisher);

        if (unsentMsgs != null && !unsentMsgs.isEmpty()) {
            // Log the error, as the exception will not be propagated due to the fact that the Cambria Client throws
            // an exception first in a separate thread.
            applicationLogger.error(ApplicationMsgs.EVENT_CLIENT_CLOSE_UNSENT_MESSAGE,
                    ValidationServiceError.EVENT_CLIENT_CLOSE_UNSENT_MESSAGE.getMessage(unsentMsgs));

            retryOrThrow(unsentMsgs, topic, new ValidationServiceException(
                    ValidationServiceError.EVENT_CLIENT_CLOSE_UNSENT_MESSAGE, unsentMsgs));
        }
    }

    private void retryOrThrow(Collection<String> messages, Topic topic, ValidationServiceException exceptionToThrow)
            throws ValidationServiceException {
        if (retriesRemaining <= 0) {
            applicationLogger.warn(ApplicationMsgs.SEND_MESSAGE_ABORT_WARN);
            throw exceptionToThrow;
        } else {
            applicationLogger.warn(ApplicationMsgs.SEND_MESSAGE_RETRY_WARN);
            retriesRemaining--;
            publishMessages(messages, topic);
        }
    }

    private List<String> closeEventPublisher(DMaaPEventPublisher eventPublisher) throws ValidationServiceException {
        try {
            return eventPublisher.closeWithUnsent();
        } catch (Exception e) {
            throw new ValidationServiceException(ValidationServiceError.EVENT_CLIENT_CLOSE_ERROR, e);
        }
    }

    public void setEventPublisherFactory(DMaaPEventPublisherFactory dMaapFactory) {
        this.dMaapFactory = dMaapFactory;
    }

}