2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018-2019 Samsung Electronics Co., Ltd.
7 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
8 * Modifications Copyright (C) 2023 Nordix Foundation.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.common.endpoints.event.comm.bus.internal;
26 import java.util.UUID;
29 import org.onap.policy.common.endpoints.event.comm.bus.BusTopicSink;
30 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
31 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Transport Agnostic Bus Topic Sink to carry out the core functionality to interact with a sink
37 * regardless if it is UEB or DMaaP.
40 public abstract class InlineBusTopicSink extends BusTopicBase implements BusTopicSink {
45 private static Logger logger = LoggerFactory.getLogger(InlineBusTopicSink.class);
48 * The partition key to publish to.
52 protected String partitionKey;
55 * Message bus publisher.
57 protected BusPublisher publisher;
60 * Constructor for abstract sink.
61 * @param busTopicParams contains below listed attributes
65 * apiSecret api secret
66 * partitionId partition id
67 * useHttps does connection use HTTPS?
68 * allowTracing is tracing allowed?
69 * allowSelfSignedCerts are self-signed certificates allow *
70 * @throws IllegalArgumentException if invalid parameters are passed in
72 protected InlineBusTopicSink(BusTopicParams busTopicParams) {
74 super(busTopicParams);
76 if (busTopicParams.isPartitionIdInvalid()) {
77 this.partitionKey = UUID.randomUUID().toString();
79 this.partitionKey = busTopicParams.getPartitionId();
84 * Initialize the Bus publisher.
86 public abstract void init();
89 public boolean start() {
90 logger.info("{}: starting", this);
95 throw new IllegalStateException(this + " is locked.");
107 public boolean stop() {
109 BusPublisher publisherCopy;
110 synchronized (this) {
112 publisherCopy = this.publisher;
113 this.publisher = null;
116 if (publisherCopy != null) {
118 publisherCopy.close();
119 } catch (Exception e) {
120 logger.warn("{}: cannot stop publisher because of {}", this, e.getMessage(), e);
123 logger.warn("{}: there is no publisher", this);
131 public boolean send(String message) {
133 if (message == null || message.isEmpty()) {
134 throw new IllegalArgumentException("Message to send is empty");
138 throw new IllegalStateException(this + " is stopped");
142 synchronized (this) {
143 this.recentEvents.add(message);
146 NetLoggerUtil.log(EventType.OUT, this.getTopicCommInfrastructure(), this.topic, message);
148 publisher.send(this.partitionKey, message);
150 } catch (Exception e) {
151 logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
159 public void shutdown() {
164 public String toString() {
165 return "InlineBusTopicSink [partitionId=" + partitionKey + ", alive=" + alive + ", publisher=" + publisher