2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 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.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Transport Agnostic Bus Topic Sink to carry out the core functionality to interact with a sink
32 * regardless if it is UEB or DMaaP.
35 public abstract class InlineBusTopicSink extends BusTopicBase implements BusTopicSink {
40 private static Logger logger = LoggerFactory.getLogger(InlineBusTopicSink.class);
41 private static final Logger netLogger = LoggerFactory.getLogger(NETWORK_LOGGER);
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.isPartitionIdNullOrEmpty()) {
70 this.partitionId = UUID.randomUUID().toString();
72 this.partitionId = busTopicParams.getPartitionId();
77 * Initialize the Bus publisher.
79 public abstract void init();
85 public boolean start() {
86 logger.info("{}: starting", this);
95 throw new IllegalStateException(this + " is locked.");
109 public boolean stop() {
111 BusPublisher publisherCopy;
112 synchronized (this) {
114 publisherCopy = this.publisher;
115 this.publisher = null;
118 if (publisherCopy != null) {
120 publisherCopy.close();
121 } catch (Exception e) {
122 logger.warn("{}: cannot stop publisher because of {}", this, e.getMessage(), e);
125 logger.warn("{}: there is no publisher", this);
136 public boolean send(String message) {
138 if (message == null || message.isEmpty()) {
139 throw new IllegalArgumentException("Message to send is empty");
143 throw new IllegalStateException(this + " is stopped");
147 synchronized (this) {
148 this.recentEvents.add(message);
151 netLogger.info("[OUT|{}|{}]{}{}", this.getTopicCommInfrastructure(), this.topic, System.lineSeparator(),
154 publisher.send(this.partitionId, message);
156 } catch (Exception e) {
157 logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
169 public void setPartitionKey(String partitionKey) {
170 this.partitionId = partitionKey;
177 public String getPartitionKey() {
178 return this.partitionId;
185 public void shutdown() {
190 protected boolean anyNullOrEmpty(String... args) {
191 for (String arg : args) {
192 if (arg == null || arg.isEmpty()) {
201 protected boolean allNullOrEmpty(String... args) {
202 for (String arg : args) {
203 if (!(arg == null || arg.isEmpty())) {
213 public String toString() {
214 return "InlineBusTopicSink [partitionId=" + partitionId + ", alive=" + alive + ", publisher=" + publisher + "]";