summaryrefslogtreecommitdiffstats
path: root/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/dmaap/MRTopicMonitor.java
blob: aa1bc9649fe3a44c3e77017edef418e98e61229f (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 *  Copyright (C) 2022 Huawei Canada Limited.
 * ================================================================================
 * 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.slice.analysis.ms.dmaap;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import lombok.Getter;
import lombok.NonNull;
import org.onap.dmaap.mr.client.impl.MRConsumerImpl;
import org.onap.dmaap.mr.client.response.MRConsumerResponse;
import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
import org.onap.slice.analysis.ms.models.Configuration;
import org.onap.slice.analysis.ms.dmaap.MRTopicParams;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * This is a Dmaap message-router topic monitor.
 * It takes advantage of AT&T's dmaap-client's long-polling implementation, this monitor constantly fetch/refetch target msg topic.
 * So that new msg can be notified almost immediately. This is the major different from previous implementation.
 */
public class MRTopicMonitor implements Runnable {

    private final String name;
    private volatile boolean running = false;
    private Configuration configuration;
    private static Logger logger = LoggerFactory.getLogger(MRTopicMonitor.class);
    private static int DEFAULT_TIMEOUT_MS_FETCH = 15000;
    private MRConsumerWrapper consumerWrapper;
    private NotificationCallback callback;

    /**
     * Constructor
     * @param name name of topic subscriber in config
     * @param callback callbackfunction for received message
     */
    public MRTopicMonitor(String name, NotificationCallback callback){
        this.name = name;
        this.callback = callback;
    }

    /**
     * Start the monitoring thread
     */
    public void start(){
        logger.info("Starting Dmaap Bus Monitor");
        configuration = Configuration.getInstance();

        Map<String, Object> streamSubscribes = configuration.getStreamsSubscribes();
        Map<String, Object> topicParamsJson = (Map<String, Object>) streamSubscribes.get(name);
        JsonObject jsonObject = (new Gson()).toJsonTree(topicParamsJson).getAsJsonObject();
        consumerWrapper = buildConsumerWrapper(jsonObject);
        running = true;
        Executor executor = Executors.newSingleThreadExecutor();
        executor.execute(this);
    }

    /**
     * Main loop that keep fetching and processing
     */
    @Override
    public void run(){
        while (running){
            try {
                logger.debug("Topic: {} getting new msg...", name);
                Iterable<String> dmaapMsgs = consumerWrapper.fetch();
                for (String msg : dmaapMsgs){
                    logger.debug("Received message: {}" +
                            "\r\n and processing start", msg);
                    process(msg);
                }
            } catch (IOException | RuntimeException e){
                logger.error("fetchMessage encountered error: {}", e);
            }
        }
        logger.info("{}: exiting thread", this);
    }

    /**
     * Stop the monitor
     */
    public void stop(){
        logger.info("{}: exiting", this);
        running = false;
        this.consumerWrapper.close();
        this.consumerWrapper = null;
    }

    private void process(String msg) {
        try {
            callback.activateCallBack(msg);
        } catch (Exception e){
            logger.error("process message encountered error: {}", e);
        }
    }

    private Iterable<String> fetch() throws IOException {
        return this.consumerWrapper.fetch();
    }

    private MRConsumerWrapper buildConsumerWrapper(@NonNull JsonObject topicParamsJson )
            throws IllegalArgumentException {
        MRTopicParams topicParams = MRTopicParams.builder().buildFromConfigJson(topicParamsJson).build();
        return new MRConsumerWrapper(topicParams);
    }

    /**
     * Wrapper class of DmaapClient (package org.onap.dmaap.mr.client)
     * A polling fashion dmaap  consumer, whose #fetch() sleep a certain time when connection fails, otherwise keep retryiny.
     * It supports both https and http protocols.
     */
    private class MRConsumerWrapper {
        /**
         * Name of the "protocol" property.
         */
        protected static final String PROTOCOL_PROP = "Protocol";
        /**
         * Fetch timeout.
         */
        protected int fetchTimeout;

        /**
         * Time to sleep on a fetch failure.
         */
        @Getter
        private final int sleepTime;

        /**
         * Counted down when {@link #close()} is invoked.
         */
        private final CountDownLatch closeCondition = new CountDownLatch(1);

        /**
         * MR Consumer.
         */
        protected MRConsumerImpl consumer;

        /**
         * Constructs the object.
         *
         * @param MRTopicParams parameters for the bus topic
         */
        protected MRConsumerWrapper(MRTopicParams MRTopicParams) {
            this.fetchTimeout = MRTopicParams.getFetchTimeout();

            if (this.fetchTimeout <= 0) {
                this.sleepTime = DEFAULT_TIMEOUT_MS_FETCH;
            } else {
                // don't sleep too long, even if fetch timeout is large
                this.sleepTime = Math.min(this.fetchTimeout, DEFAULT_TIMEOUT_MS_FETCH);
            }

            if (MRTopicParams.isTopicInvalid()) {
                throw new IllegalArgumentException("No topic for DMaaP");
            }

            if (MRTopicParams.isServersInvalid()) {
                throw new IllegalArgumentException("Must provide at least one host for HTTP AAF");
            }

            try{
                this.consumer = new MRConsumerImpl.MRConsumerImplBuilder()
                        .setHostPart(MRTopicParams.getServers())
                        .setTopic(MRTopicParams.getTopic())
                        .setConsumerGroup(MRTopicParams.getConsumerGroup())
                        .setConsumerId(MRTopicParams.getConsumerInstance())
                        .setTimeoutMs(MRTopicParams.getFetchTimeout())
                        .setLimit(MRTopicParams.getFetchLimit())
                        .setApiKey(MRTopicParams.getApiKey())
                        .setApiSecret(MRTopicParams.getApiSecret())
                        .createMRConsumerImpl();
            } catch (MalformedURLException e) {
                throw new IllegalArgumentException("Illegal MrConsumer parameters");
            }


            this.consumer.setUsername(MRTopicParams.getUserName());
            this.consumer.setPassword(MRTopicParams.getPassword());

            if(MRTopicParams.isUserNameValid() && MRTopicParams.isPasswordValid()){
                this.consumer.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
            } else {
                this.consumer.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue());
            }

            Properties props = new Properties();

            if (MRTopicParams.isUseHttps()) {
                props.setProperty(PROTOCOL_PROP, "https");
                this.consumer.setHost(MRTopicParams.getServers().get(0) + ":3905");

            } else {
                props.setProperty(PROTOCOL_PROP, "http");
                this.consumer.setHost(MRTopicParams.getServers().get(0) + ":3904");
            }

            this.consumer.setProps(props);
        }

        /**
         * Try fetch new message. But backoff for some sleepTime when connection fails.
         * @return
         * @throws IOException
         */
        public Iterable<String> fetch() throws IOException {
            final MRConsumerResponse response = this.consumer.fetchWithReturnConsumerResponse();
            if (response == null) {
                logger.warn("{}: DMaaP NULL response received", this);

                sleepAfterFetchFailure();
                return new ArrayList<>();
            } else {
                logger.debug("DMaaP consumer received {} : {}", response.getResponseCode(),
                        response.getResponseMessage());

                if (!"200".equals(response.getResponseCode())) {

                    logger.error("DMaaP consumer received: {} : {}", response.getResponseCode(),
                            response.getResponseMessage());

                    sleepAfterFetchFailure();
                }
            }

            if (response.getActualMessages() == null) {
                return new ArrayList<>();
            } else {
                return response.getActualMessages();
            }
        }

        /**
         * Causes the thread to sleep; invoked after fetch() fails.  If the consumer is closed,
         * or the thread is interrupted, then this will return immediately.
         */
        protected void sleepAfterFetchFailure() {
            try {
                logger.info("{}: backoff for {}ms", this, sleepTime);
                if (this.closeCondition.await(this.sleepTime, TimeUnit.MILLISECONDS)) {
                    logger.info("{}: closed while handling fetch error", this);
                }

            } catch (InterruptedException e) {
                logger.warn("{}: interrupted while handling fetch error", this, e);
                Thread.currentThread().interrupt();
            }
        }

        /**
         * Close the dmaap client and this thread
         */
        public void close() {
            this.closeCondition.countDown();
            this.consumer.close();
        }
    }
}