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.net.MalformedURLException;
25 import java.util.UUID;
27 import org.onap.policy.common.endpoints.event.comm.FilterableTopicSource;
28 import org.onap.policy.common.endpoints.event.comm.TopicListener;
29 import org.onap.policy.common.endpoints.event.comm.bus.BusTopicSource;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.FilterableBusConsumer;
31 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
32 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
33 import org.onap.policy.common.utils.network.NetworkUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * This topic source implementation specializes in reading messages over a bus topic source and
39 * notifying its listeners.
41 public abstract class SingleThreadedBusTopicSource extends BusTopicBase
42 implements Runnable, BusTopicSource, FilterableTopicSource {
45 * Not to be converted to PolicyLogger. This will contain all instract /out traffic and only
46 * that in a single file in a concise format.
48 private static Logger logger = LoggerFactory.getLogger(InlineBusTopicSink.class);
53 protected final String consumerGroup;
56 * Bus consumer instance.
58 protected final String consumerInstance;
63 protected final int fetchTimeout;
68 protected final int fetchLimit;
71 * Message Bus Consumer.
73 protected BusConsumer consumer;
76 * Independent thread reading message over my topic.
78 protected Thread busPollerThread;
84 * @param busTopicParams topic parameters
86 * @throws IllegalArgumentException An invalid parameter passed in
88 public SingleThreadedBusTopicSource(BusTopicParams busTopicParams) {
90 super(busTopicParams);
92 if (busTopicParams.isConsumerGroupInvalid()) {
93 this.consumerGroup = UUID.randomUUID().toString();
95 this.consumerGroup = busTopicParams.getConsumerGroup();
98 if (busTopicParams.isConsumerInstanceInvalid()) {
99 this.consumerInstance = NetworkUtil.getHostname();
101 this.consumerInstance = busTopicParams.getConsumerInstance();
104 if (busTopicParams.getFetchTimeout() <= 0) {
105 this.fetchTimeout = NO_TIMEOUT_MS_FETCH;
107 this.fetchTimeout = busTopicParams.getFetchTimeout();
110 if (busTopicParams.getFetchLimit() <= 0) {
111 this.fetchLimit = NO_LIMIT_FETCH;
113 this.fetchLimit = busTopicParams.getFetchLimit();
119 * Initialize the Bus client.
121 public abstract void init() throws MalformedURLException;
124 public void register(TopicListener topicListener) {
126 super.register(topicListener);
129 if (!alive && !locked) {
132 logger.info("{}: register: start not attempted", this);
134 } catch (Exception e) {
135 logger.warn("{}: cannot start after registration of because of: {}", this, topicListener, e.getMessage(),
141 public void unregister(TopicListener topicListener) {
143 synchronized (this) {
144 super.unregister(topicListener);
145 stop = this.topicListeners.isEmpty();
154 public boolean start() {
155 logger.info("{}: starting", this);
157 synchronized (this) {
164 throw new IllegalStateException(this + " is locked.");
167 if (this.busPollerThread == null || !this.busPollerThread.isAlive() || this.consumer == null) {
172 this.busPollerThread = makePollerThread();
173 this.busPollerThread.setName(this.getTopicCommInfrastructure() + "-source-" + this.getTopic());
174 busPollerThread.start();
176 } catch (Exception e) {
177 logger.warn("{}: cannot start because of {}", this, e.getMessage(), e);
178 throw new IllegalStateException(e);
187 * Makes a new thread to be used for polling.
189 * @return a new Thread
191 protected Thread makePollerThread() {
192 return new Thread(this);
196 public boolean stop() {
197 logger.info("{}: stopping", this);
199 synchronized (this) {
200 BusConsumer consumerCopy = this.consumer;
203 this.consumer = null;
205 if (consumerCopy != null) {
207 consumerCopy.close();
208 } catch (Exception e) {
209 logger.warn("{}: stop failed because of {}", this, e.getMessage(), e);
220 * Run thread method for the Bus Reader.
226 for (String event : this.consumer.fetch()) {
227 synchronized (this) {
228 this.recentEvents.add(event);
231 NetLoggerUtil.log(EventType.IN, this.getTopicCommInfrastructure(), this.topic, event);
239 } catch (Exception e) {
240 logger.error("{}: cannot fetch because of ", this, e.getMessage(), e);
244 logger.info("{}: exiting thread", this);
248 public boolean offer(String event) {
250 throw new IllegalStateException(this + " is not alive.");
253 synchronized (this) {
254 this.recentEvents.add(event);
257 NetLoggerUtil.log(EventType.IN, this.getTopicCommInfrastructure(), this.topic, event);
259 return broadcast(event);
264 public void setFilter(String filter) {
265 if (consumer instanceof FilterableBusConsumer) {
266 ((FilterableBusConsumer) consumer).setFilter(filter);
269 throw new UnsupportedOperationException("no server-side filtering for topic " + topic);
274 public String toString() {
275 return "SingleThreadedBusTopicSource [consumerGroup=" + consumerGroup + ", consumerInstance=" + consumerInstance
276 + ", fetchTimeout=" + fetchTimeout + ", fetchLimit=" + fetchLimit + ", consumer=" + this.consumer
277 + ", alive=" + alive + ", locked=" + locked + ", uebThread=" + busPollerThread + ", topicListeners="
278 + topicListeners.size() + ", toString()=" + super.toString() + "]";
282 public String getConsumerGroup() {
283 return consumerGroup;
287 public String getConsumerInstance() {
288 return consumerInstance;
292 public void shutdown() {
294 this.topicListeners.clear();
298 public int getFetchTimeout() {
303 public int getFetchLimit() {