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 * @throws IllegalArgumentException in invalid parameters are passed in
88 public InlineBusTopicSink(List<String> servers, String topic,
89 String apiKey, String apiSecret, String partitionId)
90 throws IllegalArgumentException {
92 super(servers, topic, apiKey, apiSecret);
94 if (partitionId == null || partitionId.isEmpty()) {
95 this.partitionId = UUID.randomUUID ().toString();
100 * Initialize the Bus publisher
102 public abstract void init();
108 public boolean start() throws IllegalStateException {
110 if (logger.isInfoEnabled())
111 logger.info("START: " + this);
119 throw new IllegalStateException(this + " is locked.");
132 public boolean stop() {
134 BusPublisher publisherCopy;
137 publisherCopy = this.publisher;
138 this.publisher = null;
141 if (publisherCopy != null) {
143 publisherCopy.close();
144 } catch (Exception e) {
145 logger.warn(MessageCodes.EXCEPTION_ERROR, e, "PUBLISHER.CLOSE", this.toString());
149 logger.warn("No publisher to close: " + this);
160 public boolean lock() {
162 if (logger.isInfoEnabled())
163 logger.info("LOCK: " + this);
165 synchronized (this) {
179 public boolean unlock() {
181 if (logger.isInfoEnabled())
182 logger.info("UNLOCK: " + this);
193 } catch (Exception e) {
194 logger.warn("can't start after unlocking " + this +
195 " because of " + e.getMessage());
205 public boolean isLocked() {
213 public boolean isAlive() {
221 public boolean send(String message) throws IllegalArgumentException, IllegalStateException {
223 if (message == null || message.isEmpty()) {
224 throw new IllegalArgumentException("Message to send is empty");
228 throw new IllegalStateException(this + " is stopped");
232 synchronized (this) {
233 this.recentEvents.add(message);
236 if (networkLogger.isInfoEnabled()) {
237 networkLogger.info("[OUT|" + this.getTopicCommInfrastructure() + "|" +
242 publisher.send(this.partitionId, message);
243 } catch (Exception e) {
244 logger.error("can't start after unlocking " + this +
245 " because of " + e.getMessage());
258 public void setPartitionKey(String partitionKey) {
259 this.partitionId = partitionKey;
266 public String getPartitionKey() {
267 return this.partitionId;
274 public void shutdown() throws IllegalStateException {
282 public abstract CommInfrastructure getTopicCommInfrastructure();