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.util.UUID;
26 import org.onap.policy.common.endpoints.event.comm.bus.BusTopicSink;
27 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
28 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Transport Agnostic Bus Topic Sink to carry out the core functionality to interact with a sink
34 * regardless if it is UEB or DMaaP.
37 public abstract class InlineBusTopicSink extends BusTopicBase implements BusTopicSink {
42 private static Logger logger = LoggerFactory.getLogger(InlineBusTopicSink.class);
45 * The partition key to publish to.
47 protected String partitionId;
50 * Message bus publisher.
52 protected BusPublisher publisher;
55 * Constructor for abstract sink.
56 * @param busTopicParams contains below listed attributes
60 * apiSecret api secret
61 * partitionId partition id
62 * useHttps does connection use HTTPS?
63 * allowSelfSignedCerts are self-signed certificates allow *
64 * @throws IllegalArgumentException in invalid parameters are passed in
66 public InlineBusTopicSink(BusTopicParams busTopicParams) {
68 super(busTopicParams);
70 if (busTopicParams.isPartitionIdInvalid()) {
71 this.partitionId = UUID.randomUUID().toString();
73 this.partitionId = busTopicParams.getPartitionId();
78 * Initialize the Bus publisher.
80 public abstract void init();
83 public boolean start() {
84 logger.info("{}: starting", this);
93 throw new IllegalStateException(this + " is locked.");
104 public boolean stop() {
106 BusPublisher publisherCopy;
107 synchronized (this) {
109 publisherCopy = this.publisher;
110 this.publisher = null;
113 if (publisherCopy != null) {
115 publisherCopy.close();
116 } catch (Exception e) {
117 logger.warn("{}: cannot stop publisher because of {}", this, e.getMessage(), e);
120 logger.warn("{}: there is no publisher", this);
128 public boolean send(String message) {
130 if (message == null || message.isEmpty()) {
131 throw new IllegalArgumentException("Message to send is empty");
135 throw new IllegalStateException(this + " is stopped");
139 synchronized (this) {
140 this.recentEvents.add(message);
143 NetLoggerUtil.log(EventType.OUT, this.getTopicCommInfrastructure(), this.topic, message);
145 publisher.send(this.partitionId, message);
147 } catch (Exception e) {
148 logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
156 public void setPartitionKey(String partitionKey) {
157 this.partitionId = partitionKey;
161 public String getPartitionKey() {
162 return this.partitionId;
166 public void shutdown() {
171 protected boolean anyNullOrEmpty(String... args) {
172 for (String arg : args) {
173 if (arg == null || arg.isEmpty()) {
182 protected boolean allNullOrEmpty(String... args) {
183 for (String arg : args) {
184 if (!(arg == null || arg.isEmpty())) {
193 public String toString() {
194 return "InlineBusTopicSink [partitionId=" + partitionId + ", alive=" + alive + ", publisher=" + publisher + "]";