c8d6bd5ad031786a7d9119a9d64a02494f0a4855
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm.bus.internal;
23
24 import java.util.UUID;
25
26 import org.onap.policy.common.endpoints.event.comm.bus.BusTopicSink;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Transport Agnostic Bus Topic Sink to carry out the core functionality to interact with a sink
32  * regardless if it is UEB or DMaaP.
33  *
34  */
35 public abstract class InlineBusTopicSink extends BusTopicBase implements BusTopicSink {
36
37     /**
38      * Loggers.
39      */
40     private static Logger logger = LoggerFactory.getLogger(InlineBusTopicSink.class);
41     private static final Logger netLogger = LoggerFactory.getLogger(NETWORK_LOGGER);
42
43     /**
44      * The partition key to publish to.
45      */
46     protected String partitionId;
47
48     /**
49      * Message bus publisher.
50      */
51     protected BusPublisher publisher;
52
53     /**
54      * Constructor for abstract sink.
55      * @param busTopicParams contains below listed attributes
56      *     servers servers
57      *     topic topic
58      *     apiKey api secret
59      *     apiSecret api secret
60      *     partitionId partition id
61      *     useHttps does connection use HTTPS?
62      *     allowSelfSignedCerts are self-signed certificates allow     *
63      * @throws IllegalArgumentException in invalid parameters are passed in
64      */
65     public InlineBusTopicSink(BusTopicParams busTopicParams) {
66
67         super(busTopicParams);
68
69         if (busTopicParams.isPartitionIdInvalid()) {
70             this.partitionId = UUID.randomUUID().toString();
71         } else {
72             this.partitionId = busTopicParams.getPartitionId();
73         }
74     }
75
76     /**
77      * Initialize the Bus publisher.
78      */
79     public abstract void init();
80
81     @Override
82     public boolean start() {
83         logger.info("{}: starting", this);
84
85         synchronized (this) {
86
87             if (this.alive) {
88                 return true;
89             }
90
91             if (locked) {
92                 throw new IllegalStateException(this + " is locked.");
93             }
94
95             this.alive = true;
96         }
97
98         this.init();
99         return true;
100     }
101
102     @Override
103     public boolean stop() {
104
105         BusPublisher publisherCopy;
106         synchronized (this) {
107             this.alive = false;
108             publisherCopy = this.publisher;
109             this.publisher = null;
110         }
111
112         if (publisherCopy != null) {
113             try {
114                 publisherCopy.close();
115             } catch (Exception e) {
116                 logger.warn("{}: cannot stop publisher because of {}", this, e.getMessage(), e);
117             }
118         } else {
119             logger.warn("{}: there is no publisher", this);
120             return false;
121         }
122
123         return true;
124     }
125
126     @Override
127     public boolean send(String message) {
128
129         if (message == null || message.isEmpty()) {
130             throw new IllegalArgumentException("Message to send is empty");
131         }
132
133         if (!this.alive) {
134             throw new IllegalStateException(this + " is stopped");
135         }
136
137         try {
138             synchronized (this) {
139                 this.recentEvents.add(message);
140             }
141
142             netLogger.info("[OUT|{}|{}]{}{}", this.getTopicCommInfrastructure(), this.topic, System.lineSeparator(),
143                     message);
144
145             publisher.send(this.partitionId, message);
146             broadcast(message);
147         } catch (Exception e) {
148             logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
149             return false;
150         }
151
152         return true;
153     }
154
155     @Override
156     public void setPartitionKey(String partitionKey) {
157         this.partitionId = partitionKey;
158     }
159
160     @Override
161     public String getPartitionKey() {
162         return this.partitionId;
163     }
164
165     @Override
166     public void shutdown() {
167         this.stop();
168     }
169
170     @Override
171     protected boolean anyNullOrEmpty(String... args) {
172         for (String arg : args) {
173             if (arg == null || arg.isEmpty()) {
174                 return true;
175             }
176         }
177
178         return false;
179     }
180
181     @Override
182     protected boolean allNullOrEmpty(String... args) {
183         for (String arg : args) {
184             if (!(arg == null || arg.isEmpty())) {
185                 return false;
186             }
187         }
188
189         return true;
190     }
191
192     @Override
193     public String toString() {
194         return "InlineBusTopicSink [partitionId=" + partitionId + ", alive=" + alive + ", publisher=" + publisher + "]";
195     }
196 }