f3c736da310bd5bd06f1d7774a799468e66f2166
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.event.comm.bus.internal;
22
23 import java.util.List;
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      * 
56      * @param servers servers
57      * @param topic topic
58      * @param apiKey api secret
59      * @param apiSecret api secret
60      * @param partitionId partition id
61      * @param useHttps does connection use HTTPS?
62      * @param allowSelfSignedCerts are self-signed certificates allow
63      * @throws IllegalArgumentException in invalid parameters are passed in
64      */
65     public InlineBusTopicSink(List<String> servers, String topic, String apiKey, String apiSecret, String partitionId,
66             boolean useHttps, boolean allowSelfSignedCerts) {
67
68         super(servers, topic, apiKey, apiSecret, useHttps, allowSelfSignedCerts);
69
70         if (partitionId == null || partitionId.isEmpty()) {
71             this.partitionId = UUID.randomUUID().toString();
72         }
73     }
74
75     /**
76      * Initialize the Bus publisher
77      */
78     public abstract void init();
79
80     /**
81      * {@inheritDoc}
82      */
83     @Override
84     public boolean start() {
85         logger.info("{}: starting", this);
86
87         synchronized (this) {
88
89             if (this.alive) {
90                 return true;
91             }
92
93             if (locked) {
94                 throw new IllegalStateException(this + " is locked.");
95             }
96
97             this.alive = true;
98         }
99
100         this.init();
101         return true;
102     }
103
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public boolean stop() {
109
110         BusPublisher publisherCopy;
111         synchronized (this) {
112             this.alive = false;
113             publisherCopy = this.publisher;
114             this.publisher = null;
115         }
116
117         if (publisherCopy != null) {
118             try {
119                 publisherCopy.close();
120             } catch (Exception e) {
121                 logger.warn("{}: cannot stop publisher because of {}", this, e.getMessage(), e);
122             }
123         } else {
124             logger.warn("{}: there is no publisher", this);
125             return false;
126         }
127
128         return true;
129     }
130
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public boolean send(String message) {
136
137         if (message == null || message.isEmpty()) {
138             throw new IllegalArgumentException("Message to send is empty");
139         }
140
141         if (!this.alive) {
142             throw new IllegalStateException(this + " is stopped");
143         }
144
145         try {
146             synchronized (this) {
147                 this.recentEvents.add(message);
148             }
149
150             netLogger.info("[OUT|{}|{}]{}{}", this.getTopicCommInfrastructure(), this.topic, System.lineSeparator(),
151                     message);
152
153             publisher.send(this.partitionId, message);
154             broadcast(message);
155         } catch (Exception e) {
156             logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
157             return false;
158         }
159
160         return true;
161     }
162
163
164     /**
165      * {@inheritDoc}
166      */
167     @Override
168     public void setPartitionKey(String partitionKey) {
169         this.partitionId = partitionKey;
170     }
171
172     /**
173      * {@inheritDoc}
174      */
175     @Override
176     public String getPartitionKey() {
177         return this.partitionId;
178     }
179
180     /**
181      * {@inheritDoc}
182      */
183     @Override
184     public void shutdown() {
185         this.stop();
186     }
187
188     @Override
189     protected boolean anyNullOrEmpty(String... args) {
190         for (String arg : args) {
191             if (arg == null || arg.isEmpty()) {
192                 return true;
193             }
194         }
195
196         return false;
197     }
198
199     @Override
200     protected boolean allNullOrEmpty(String... args) {
201         for (String arg : args) {
202             if (!(arg == null || arg.isEmpty())) {
203                 return false;
204             }
205         }
206
207         return true;
208     }
209
210
211     @Override
212     public String toString() {
213         return "InlineBusTopicSink [partitionId=" + partitionId + ", alive=" + alive + ", publisher=" + publisher + "]";
214     }
215 }