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