2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.common.endpoints.event.comm.bus.internal;
23 import java.util.List;
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
56 * @param servers servers
58 * @param apiKey api secret
59 * @param apiSecret api secret
60 * @param partitionId partition id
61 * @param useHttps does connection use HTTPS?
62 * @param allowSelfSignedCerts are self-signed certificates allow
63 * @throws IllegalArgumentException in invalid parameters are passed in
65 public InlineBusTopicSink(List<String> servers, String topic, String apiKey, String apiSecret, String partitionId,
66 boolean useHttps, boolean allowSelfSignedCerts) {
68 super(servers, topic, apiKey, apiSecret, useHttps, allowSelfSignedCerts);
70 if (partitionId == null || partitionId.isEmpty()) {
71 this.partitionId = UUID.randomUUID().toString();
76 * Initialize the Bus publisher
78 public abstract void init();
84 public boolean start() {
85 logger.info("{}: starting", this);
94 throw new IllegalStateException(this + " is locked.");
108 public boolean stop() {
110 BusPublisher publisherCopy;
111 synchronized (this) {
113 publisherCopy = this.publisher;
114 this.publisher = null;
117 if (publisherCopy != null) {
119 publisherCopy.close();
120 } catch (Exception e) {
121 logger.warn("{}: cannot stop publisher because of {}", this, e.getMessage(), e);
124 logger.warn("{}: there is no publisher", this);
135 public boolean send(String message) {
137 if (message == null || message.isEmpty()) {
138 throw new IllegalArgumentException("Message to send is empty");
142 throw new IllegalStateException(this + " is stopped");
146 synchronized (this) {
147 this.recentEvents.add(message);
150 netLogger.info("[OUT|{}|{}]{}{}", this.getTopicCommInfrastructure(), this.topic, System.lineSeparator(),
153 publisher.send(this.partitionId, message);
155 } catch (Exception e) {
156 logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
168 public void setPartitionKey(String partitionKey) {
169 this.partitionId = partitionKey;
176 public String getPartitionKey() {
177 return this.partitionId;
184 public void shutdown() {
189 protected boolean anyNullOrEmpty(String... args) {
190 for (String arg : args) {
191 if (arg == null || arg.isEmpty()) {
200 protected boolean allNullOrEmpty(String... args) {
201 for (String arg : args) {
202 if (!(arg == null || arg.isEmpty())) {
212 public String toString() {
213 return "InlineBusTopicSink [partitionId=" + partitionId + ", alive=" + alive + ", publisher=" + publisher + "]";