2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 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;
25 import org.onap.policy.common.endpoints.event.comm.bus.BusTopicSink;
26 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
27 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Transport Agnostic Bus Topic Sink to carry out the core functionality to interact with a sink
33 * regardless if it is UEB or DMaaP.
36 public abstract class InlineBusTopicSink extends BusTopicBase implements BusTopicSink {
41 private static Logger logger = LoggerFactory.getLogger(InlineBusTopicSink.class);
44 * The partition key to publish to.
46 protected String partitionId;
49 * Message bus publisher.
51 protected BusPublisher publisher;
54 * Constructor for abstract sink.
55 * @param busTopicParams contains below listed attributes
59 * apiSecret api secret
60 * partitionId partition id
61 * useHttps does connection use HTTPS?
62 * allowSelfSignedCerts are self-signed certificates allow *
63 * @throws IllegalArgumentException in invalid parameters are passed in
65 public InlineBusTopicSink(BusTopicParams busTopicParams) {
67 super(busTopicParams);
69 if (busTopicParams.isPartitionIdInvalid()) {
70 this.partitionId = UUID.randomUUID().toString();
72 this.partitionId = busTopicParams.getPartitionId();
77 * Initialize the Bus publisher.
79 public abstract void init();
82 public boolean start() {
83 logger.info("{}: starting", this);
88 throw new IllegalStateException(this + " is locked.");
100 public boolean stop() {
102 BusPublisher publisherCopy;
103 synchronized (this) {
105 publisherCopy = this.publisher;
106 this.publisher = null;
109 if (publisherCopy != null) {
111 publisherCopy.close();
112 } catch (Exception e) {
113 logger.warn("{}: cannot stop publisher because of {}", this, e.getMessage(), e);
116 logger.warn("{}: there is no publisher", this);
124 public boolean send(String message) {
126 if (message == null || message.isEmpty()) {
127 throw new IllegalArgumentException("Message to send is empty");
131 throw new IllegalStateException(this + " is stopped");
135 synchronized (this) {
136 this.recentEvents.add(message);
139 NetLoggerUtil.log(EventType.OUT, this.getTopicCommInfrastructure(), this.topic, message);
141 publisher.send(this.partitionId, message);
143 } catch (Exception e) {
144 logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
152 public void setPartitionKey(String partitionKey) {
153 this.partitionId = partitionKey;
157 public String getPartitionKey() {
158 return this.partitionId;
162 public void shutdown() {
167 protected boolean anyNullOrEmpty(String... args) {
168 for (String arg : args) {
169 if (arg == null || arg.isEmpty()) {
178 protected boolean allNullOrEmpty(String... args) {
179 for (String arg : args) {
180 if (!(arg == null || arg.isEmpty())) {
189 public String toString() {
190 return "InlineBusTopicSink [partitionId=" + partitionId + ", alive=" + alive + ", publisher=" + publisher + "]";