166699049ec98be499d11c9c0831451e1b1503b6
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018-2019 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.onap.policy.common.endpoints.utils.NetLoggerUtil;
28 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Transport Agnostic Bus Topic Sink to carry out the core functionality to interact with a sink
34  * regardless if it is UEB or DMaaP.
35  *
36  */
37 public abstract class InlineBusTopicSink extends BusTopicBase implements BusTopicSink {
38
39     /**
40      * Loggers.
41      */
42     private static Logger logger = LoggerFactory.getLogger(InlineBusTopicSink.class);
43
44     /**
45      * The partition key to publish to.
46      */
47     protected String partitionId;
48
49     /**
50      * Message bus publisher.
51      */
52     protected BusPublisher publisher;
53
54     /**
55      * Constructor for abstract sink.
56      * @param busTopicParams contains below listed attributes
57      *     servers servers
58      *     topic topic
59      *     apiKey api secret
60      *     apiSecret api secret
61      *     partitionId partition id
62      *     useHttps does connection use HTTPS?
63      *     allowSelfSignedCerts are self-signed certificates allow     *
64      * @throws IllegalArgumentException in invalid parameters are passed in
65      */
66     public InlineBusTopicSink(BusTopicParams busTopicParams) {
67
68         super(busTopicParams);
69
70         if (busTopicParams.isPartitionIdInvalid()) {
71             this.partitionId = UUID.randomUUID().toString();
72         } else {
73             this.partitionId = busTopicParams.getPartitionId();
74         }
75     }
76
77     /**
78      * Initialize the Bus publisher.
79      */
80     public abstract void init();
81
82     @Override
83     public boolean start() {
84         logger.info("{}: starting", this);
85
86         synchronized (this) {
87             if (!this.alive) {
88                 if (locked) {
89                     throw new IllegalStateException(this + " is locked.");
90                 }
91
92                 this.init();
93                 this.alive = true;
94             }
95         }
96
97         return true;
98     }
99
100     @Override
101     public boolean stop() {
102
103         BusPublisher publisherCopy;
104         synchronized (this) {
105             this.alive = false;
106             publisherCopy = this.publisher;
107             this.publisher = null;
108         }
109
110         if (publisherCopy != null) {
111             try {
112                 publisherCopy.close();
113             } catch (Exception e) {
114                 logger.warn("{}: cannot stop publisher because of {}", this, e.getMessage(), e);
115             }
116         } else {
117             logger.warn("{}: there is no publisher", this);
118             return false;
119         }
120
121         return true;
122     }
123
124     @Override
125     public boolean send(String message) {
126
127         if (message == null || message.isEmpty()) {
128             throw new IllegalArgumentException("Message to send is empty");
129         }
130
131         if (!this.alive) {
132             throw new IllegalStateException(this + " is stopped");
133         }
134
135         try {
136             synchronized (this) {
137                 this.recentEvents.add(message);
138             }
139
140             NetLoggerUtil.log(EventType.OUT, this.getTopicCommInfrastructure(), this.topic, message);
141
142             publisher.send(this.partitionId, message);
143             broadcast(message);
144         } catch (Exception e) {
145             logger.warn("{}: cannot send because of {}", this, e.getMessage(), e);
146             return false;
147         }
148
149         return true;
150     }
151
152     @Override
153     public void setPartitionKey(String partitionKey) {
154         this.partitionId = partitionKey;
155     }
156
157     @Override
158     public String getPartitionKey() {
159         return this.partitionId;
160     }
161
162     @Override
163     public void shutdown() {
164         this.stop();
165     }
166
167     @Override
168     protected boolean anyNullOrEmpty(String... args) {
169         for (String arg : args) {
170             if (arg == null || arg.isEmpty()) {
171                 return true;
172             }
173         }
174
175         return false;
176     }
177
178     @Override
179     protected boolean allNullOrEmpty(String... args) {
180         for (String arg : args) {
181             if (!(arg == null || arg.isEmpty())) {
182                 return false;
183             }
184         }
185
186         return true;
187     }
188
189     @Override
190     public String toString() {
191         return "InlineBusTopicSink [partitionId=" + partitionId + ", alive=" + alive + ", publisher=" + publisher + "]";
192     }
193 }