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.apache.log4j.Logger;
28 import org.openecomp.policy.drools.event.comm.bus.BusTopicSink;
29 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
30 import org.openecomp.policy.common.logging.eelf.MessageCodes;
33 * Transport Agnostic Bus Topic Sink to carry out the core functionality
34 * to interact with a sink regardless if it is UEB or DMaaP.
37 public abstract class InlineBusTopicSink extends BusTopicBase implements BusTopicSink {
42 private static org.openecomp.policy.common.logging.flexlogger.Logger logger =
43 FlexLogger.getLogger(InlineBusTopicSink.class);
46 * Not to be converted to PolicyLogger.
47 * This will contain all in/out traffic and only that in a single file in a concise format.
49 protected static final Logger networkLogger = Logger.getLogger(NETWORK_LOGGER);
52 * The partition key to publish to
54 protected String partitionId;
58 * reflects invocation of start()/stop()
59 * !locked & start() => alive
62 protected volatile boolean alive = false;
66 * reflects invocation of lock()/unlock() operations
67 * locked => !alive (but not in the other direction necessarily)
68 * locked => !offer, !run, !start, !stop (but this last one is obvious
69 * since locked => !alive)
71 protected volatile boolean locked = false;
74 * message bus publisher
76 protected BusPublisher publisher;
79 * constructor for abstract sink
81 * @param servers servers
83 * @param apiKey api secret
84 * @param apiSecret api secret
85 * @param partitionId partition id
86 * @param useHttps does connection use HTTPS?
87 * @param allowSelfSignedCerts are self-signed certificates allow
88 * @throws IllegalArgumentException in invalid parameters are passed in
90 public InlineBusTopicSink(List<String> servers, String topic,
91 String apiKey, String apiSecret, String partitionId, boolean useHttps, boolean allowSelfSignedCerts)
92 throws IllegalArgumentException {
94 super(servers, topic, apiKey, apiSecret, useHttps, allowSelfSignedCerts);
96 if (partitionId == null || partitionId.isEmpty()) {
97 this.partitionId = UUID.randomUUID ().toString();
102 * Initialize the Bus publisher
104 public abstract void init();
110 public boolean start() throws IllegalStateException {
112 if (logger.isInfoEnabled())
113 logger.info("START: " + this);
121 throw new IllegalStateException(this + " is locked.");
134 public boolean stop() {
136 BusPublisher publisherCopy;
139 publisherCopy = this.publisher;
140 this.publisher = null;
143 if (publisherCopy != null) {
145 publisherCopy.close();
146 } catch (Exception e) {
147 logger.warn(MessageCodes.EXCEPTION_ERROR, e, "PUBLISHER.CLOSE", this.toString());
151 logger.warn("No publisher to close: " + this);
162 public boolean lock() {
164 if (logger.isInfoEnabled())
165 logger.info("LOCK: " + this);
167 synchronized (this) {
181 public boolean unlock() {
183 if (logger.isInfoEnabled())
184 logger.info("UNLOCK: " + this);
195 } catch (Exception e) {
196 logger.warn("can't start after unlocking " + this +
197 " because of " + e.getMessage());
207 public boolean isLocked() {
215 public boolean isAlive() {
223 public boolean send(String message) throws IllegalArgumentException, IllegalStateException {
225 if (message == null || message.isEmpty()) {
226 throw new IllegalArgumentException("Message to send is empty");
230 throw new IllegalStateException(this + " is stopped");
234 synchronized (this) {
235 this.recentEvents.add(message);
238 if (networkLogger.isInfoEnabled()) {
239 networkLogger.info("[OUT|" + this.getTopicCommInfrastructure() + "|" +
244 publisher.send(this.partitionId, message);
245 } catch (Exception e) {
246 logger.error("can't start after unlocking " + this +
247 " because of " + e.getMessage());
260 public void setPartitionKey(String partitionKey) {
261 this.partitionId = partitionKey;
268 public String getPartitionKey() {
269 return this.partitionId;
276 public void shutdown() throws IllegalStateException {
284 public abstract CommInfrastructure getTopicCommInfrastructure();