2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.policy.drools.event.comm.bus.internal;
23 import java.util.List;
24 import java.util.UUID;
26 import org.openecomp.policy.drools.event.comm.bus.BusTopicSink;
27 import org.slf4j.LoggerFactory;
28 import org.slf4j.Logger;
31 * Transport Agnostic Bus Topic Sink to carry out the core functionality
32 * to interact with a sink 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;
50 * reflects invocation of start()/stop()
51 * !locked & start() => alive
54 protected volatile boolean alive = false;
58 * reflects invocation of lock()/unlock() operations
59 * locked => !alive (but not in the other direction necessarily)
60 * locked => !offer, !run, !start, !stop (but this last one is obvious
61 * since locked => !alive)
63 protected volatile boolean locked = false;
66 * message bus publisher
68 protected BusPublisher publisher;
71 * constructor for abstract sink
73 * @param servers servers
75 * @param apiKey api secret
76 * @param apiSecret api secret
77 * @param partitionId partition id
78 * @param useHttps does connection use HTTPS?
79 * @param allowSelfSignedCerts are self-signed certificates allow
80 * @throws IllegalArgumentException in invalid parameters are passed in
82 public InlineBusTopicSink(List<String> servers, String topic,
83 String apiKey, String apiSecret, String partitionId, boolean useHttps, boolean allowSelfSignedCerts)
84 throws IllegalArgumentException {
86 super(servers, topic, apiKey, apiSecret, useHttps, allowSelfSignedCerts);
88 if (partitionId == null || partitionId.isEmpty()) {
89 this.partitionId = UUID.randomUUID ().toString();
94 * Initialize the Bus publisher
96 public abstract void init();
102 public boolean start() throws IllegalStateException {
104 logger.info("{}: starting", this);
112 throw new IllegalStateException(this + " is locked.");
125 public boolean stop() {
127 BusPublisher publisherCopy;
130 publisherCopy = this.publisher;
131 this.publisher = null;
134 if (publisherCopy != null) {
136 publisherCopy.close();
137 } catch (Exception e) {
138 logger.warn("{}: cannot stop publisher because of {}",
139 this, e.getMessage(), e);
142 logger.warn("{}: there is no publisher", this);
153 public boolean lock() {
155 logger.info("{}: locking", this);
157 synchronized (this) {
171 public boolean unlock() {
173 logger.info("{}: unlocking", this);
184 } catch (Exception e) {
185 logger.warn("{}: cannot start after unlocking because of {}",
186 this, e.getMessage(), e);
195 public boolean isLocked() {
203 public boolean isAlive() {
211 public boolean send(String message) throws IllegalArgumentException, IllegalStateException {
213 if (message == null || message.isEmpty()) {
214 throw new IllegalArgumentException("Message to send is empty");
218 throw new IllegalStateException(this + " is stopped");
222 synchronized (this) {
223 this.recentEvents.add(message);
226 netLogger.info("[OUT|{}|{}]{}{}", this.getTopicCommInfrastructure(),
227 this.topic, System.lineSeparator(), message);
229 publisher.send(this.partitionId, message);
230 } catch (Exception e) {
231 logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
243 public void setPartitionKey(String partitionKey) {
244 this.partitionId = partitionKey;
251 public String getPartitionKey() {
252 return this.partitionId;
259 public void shutdown() throws IllegalStateException {
267 public abstract CommInfrastructure getTopicCommInfrastructure();