2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018-2019 Samsung Electronics Co., 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=========================================================
22 package org.onap.policy.common.endpoints.event.comm.bus.internal;
24 import java.io.IOException;
25 import java.net.MalformedURLException;
26 import java.util.UUID;
28 import org.onap.policy.common.endpoints.event.comm.FilterableTopicSource;
29 import org.onap.policy.common.endpoints.event.comm.TopicListener;
30 import org.onap.policy.common.endpoints.event.comm.bus.BusTopicSource;
31 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.FilterableBusConsumer;
32 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
33 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
34 import org.onap.policy.common.utils.network.NetworkUtil;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * This topic source implementation specializes in reading messages over a bus topic source and
40 * notifying its listeners.
42 public abstract class SingleThreadedBusTopicSource extends BusTopicBase
43 implements Runnable, BusTopicSource, FilterableTopicSource {
46 * Not to be converted to PolicyLogger. This will contain all instract /out traffic and only
47 * that in a single file in a concise format.
49 private static Logger logger = LoggerFactory.getLogger(InlineBusTopicSink.class);
54 protected final String consumerGroup;
57 * Bus consumer instance.
59 protected final String consumerInstance;
64 protected final int fetchTimeout;
69 protected final int fetchLimit;
72 * Message Bus Consumer.
74 protected BusConsumer consumer;
77 * Independent thread reading message over my topic.
79 protected Thread busPollerThread;
85 * @param busTopicParams topic parameters
87 * @throws IllegalArgumentException An invalid parameter passed in
89 public SingleThreadedBusTopicSource(BusTopicParams busTopicParams) {
91 super(busTopicParams);
93 if (busTopicParams.isConsumerGroupInvalid()) {
94 this.consumerGroup = UUID.randomUUID().toString();
96 this.consumerGroup = busTopicParams.getConsumerGroup();
99 if (busTopicParams.isConsumerInstanceInvalid()) {
100 this.consumerInstance = NetworkUtil.getHostname();
102 this.consumerInstance = busTopicParams.getConsumerInstance();
105 if (busTopicParams.getFetchTimeout() <= 0) {
106 this.fetchTimeout = NO_TIMEOUT_MS_FETCH;
108 this.fetchTimeout = busTopicParams.getFetchTimeout();
111 if (busTopicParams.getFetchLimit() <= 0) {
112 this.fetchLimit = NO_LIMIT_FETCH;
114 this.fetchLimit = busTopicParams.getFetchLimit();
120 * Initialize the Bus client.
122 public abstract void init() throws MalformedURLException;
125 public void register(TopicListener topicListener) {
127 super.register(topicListener);
130 if (!alive && !locked) {
133 logger.info("{}: register: start not attempted", this);
135 } catch (Exception e) {
136 logger.warn("{}: cannot start after registration of because of: {}", this, topicListener, e.getMessage(),
142 public void unregister(TopicListener topicListener) {
144 synchronized (this) {
145 super.unregister(topicListener);
146 stop = this.topicListeners.isEmpty();
155 public boolean start() {
156 logger.info("{}: starting", this);
158 synchronized (this) {
165 throw new IllegalStateException(this + " is locked.");
168 if (this.busPollerThread == null || !this.busPollerThread.isAlive() || this.consumer == null) {
173 this.busPollerThread = makePollerThread();
174 this.busPollerThread.setName(this.getTopicCommInfrastructure() + "-source-" + this.getTopic());
175 busPollerThread.start();
177 } catch (Exception e) {
178 logger.warn("{}: cannot start because of {}", this, e.getMessage(), e);
179 throw new IllegalStateException(e);
188 * Makes a new thread to be used for polling.
190 * @return a new Thread
192 protected Thread makePollerThread() {
193 return new Thread(this);
197 public boolean stop() {
198 logger.info("{}: stopping", this);
200 synchronized (this) {
201 BusConsumer consumerCopy = this.consumer;
204 this.consumer = null;
206 if (consumerCopy != null) {
208 consumerCopy.close();
209 } catch (Exception e) {
210 logger.warn("{}: stop failed because of {}", this, e.getMessage(), e);
221 * Run thread method for the Bus Reader.
228 } catch (Exception e) {
229 logger.error("{}: cannot fetch because of ", this, e.getMessage(), e);
233 logger.info("{}: exiting thread", this);
236 private void fetchAllMessages() throws InterruptedException, IOException {
237 for (String event : this.consumer.fetch()) {
238 synchronized (this) {
239 this.recentEvents.add(event);
242 NetLoggerUtil.log(EventType.IN, this.getTopicCommInfrastructure(), this.topic, event);
253 public boolean offer(String event) {
255 throw new IllegalStateException(this + " is not alive.");
258 synchronized (this) {
259 this.recentEvents.add(event);
262 NetLoggerUtil.log(EventType.IN, this.getTopicCommInfrastructure(), this.topic, event);
264 return broadcast(event);
269 public void setFilter(String filter) {
270 if (consumer instanceof FilterableBusConsumer) {
271 ((FilterableBusConsumer) consumer).setFilter(filter);
274 throw new UnsupportedOperationException("no server-side filtering for topic " + topic);
279 public String toString() {
280 return "SingleThreadedBusTopicSource [consumerGroup=" + consumerGroup + ", consumerInstance=" + consumerInstance
281 + ", fetchTimeout=" + fetchTimeout + ", fetchLimit=" + fetchLimit + ", consumer=" + this.consumer
282 + ", alive=" + alive + ", locked=" + locked + ", uebThread=" + busPollerThread + ", topicListeners="
283 + topicListeners.size() + ", toString()=" + super.toString() + "]";
287 public String getConsumerGroup() {
288 return consumerGroup;
292 public String getConsumerInstance() {
293 return consumerInstance;
297 public void shutdown() {
299 this.topicListeners.clear();
303 public int getFetchTimeout() {
308 public int getFetchLimit() {