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-2024 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.
39 public abstract class InlineBusTopicSink extends BusTopicBase implements BusTopicSink {
44 private static Logger logger = LoggerFactory.getLogger(InlineBusTopicSink.class);
47 * The partition key to publish to.
51 protected String partitionKey;
54 * Message bus publisher.
56 protected BusPublisher publisher;
59 * Constructor for abstract sink.
60 * @param busTopicParams contains below listed attributes
64 * apiSecret api secret
65 * partitionId partition id
66 * useHttps does connection use HTTPS?
67 * allowTracing is tracing allowed?
68 * allowSelfSignedCerts are self-signed certificates allow *
69 * @throws IllegalArgumentException if invalid parameters are passed in
71 protected InlineBusTopicSink(BusTopicParams busTopicParams) {
73 super(busTopicParams);
75 if (busTopicParams.isPartitionIdInvalid()) {
76 this.partitionKey = UUID.randomUUID().toString();
78 this.partitionKey = busTopicParams.getPartitionId();
83 * Initialize the Bus publisher.
85 public abstract void init();
88 public boolean start() {
89 logger.info("{}: starting", this);
94 throw new IllegalStateException(this + " is locked.");
106 public boolean stop() {
108 BusPublisher publisherCopy;
109 synchronized (this) {
111 publisherCopy = this.publisher;
112 this.publisher = null;
115 if (publisherCopy != null) {
117 publisherCopy.close();
118 } catch (Exception e) {
119 logger.warn("{}: cannot stop publisher because of {}", this, e.getMessage(), e);
122 logger.warn("{}: there is no publisher", this);
130 public boolean send(String message) {
132 if (message == null || message.isEmpty()) {
133 throw new IllegalArgumentException("Message to send is empty");
137 throw new IllegalStateException(this + " is stopped");
141 synchronized (this) {
142 this.recentEvents.add(message);
145 NetLoggerUtil.log(EventType.OUT, this.getTopicCommInfrastructure(), this.topic, message);
147 publisher.send(this.partitionKey, message);
149 } catch (Exception e) {
150 logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
158 public void shutdown() {
163 public String toString() {
164 return "InlineBusTopicSink [partitionId=" + partitionKey + ", alive=" + alive + ", publisher=" + publisher