2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
8 * Modifications Copyright (C) 2022-2023 Nordix Foundation.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.common.endpoints.event.comm.bus.internal;
26 import com.att.nsa.apiClient.http.HttpClient.ConnectionType;
27 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
28 import com.att.nsa.cambria.client.CambriaClientBuilders;
29 import java.net.MalformedURLException;
30 import java.security.GeneralSecurityException;
31 import java.util.ArrayList;
32 import java.util.List;
34 import java.util.Properties;
35 import java.util.UUID;
36 import java.util.concurrent.TimeUnit;
37 import org.apache.commons.lang3.StringUtils;
38 import org.apache.kafka.clients.producer.KafkaProducer;
39 import org.apache.kafka.clients.producer.Producer;
40 import org.apache.kafka.clients.producer.ProducerConfig;
41 import org.apache.kafka.clients.producer.ProducerRecord;
42 import org.onap.dmaap.mr.client.impl.MRSimplerBatchPublisher;
43 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
44 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
45 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
46 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public interface BusPublisher {
55 * @param partitionId id
56 * @param message the message
57 * @return true if success, false otherwise
58 * @throws IllegalArgumentException if no message provided
60 public boolean send(String partitionId, String message);
63 * closes the publisher.
68 * Cambria based library publisher.
70 public static class CambriaPublisherWrapper implements BusPublisher {
72 private static Logger logger = LoggerFactory.getLogger(CambriaPublisherWrapper.class);
75 * The actual Cambria publisher.
78 protected CambriaBatchingPublisher publisher;
83 * @param busTopicParams topic parameters
85 public CambriaPublisherWrapper(BusTopicParams busTopicParams) {
87 var builder = new CambriaClientBuilders.PublisherBuilder();
89 builder.usingHosts(busTopicParams.getServers()).onTopic(busTopicParams.getTopic());
91 // Set read timeout to 30 seconds (TBD: this should be configurable)
92 builder.withSocketTimeout(30000);
94 if (busTopicParams.isUseHttps()) {
95 if (busTopicParams.isAllowSelfSignedCerts()) {
96 builder.withConnectionType(ConnectionType.HTTPS_NO_VALIDATION);
98 builder.withConnectionType(ConnectionType.HTTPS);
103 if (busTopicParams.isApiKeyValid() && busTopicParams.isApiSecretValid()) {
104 builder.authenticatedBy(busTopicParams.getApiKey(), busTopicParams.getApiSecret());
107 if (busTopicParams.isUserNameValid() && busTopicParams.isPasswordValid()) {
108 builder.authenticatedByHttp(busTopicParams.getUserName(), busTopicParams.getPassword());
112 this.publisher = builder.build();
113 } catch (MalformedURLException | GeneralSecurityException e) {
114 throw new IllegalArgumentException(e);
119 public boolean send(String partitionId, String message) {
120 if (message == null) {
121 throw new IllegalArgumentException("No message provided");
125 this.publisher.send(partitionId, message);
126 } catch (Exception e) {
127 logger.warn("{}: SEND of {} cannot be performed because of {}", this, message, e.getMessage(), e);
134 public void close() {
135 logger.info("{}: CLOSE", this);
138 this.publisher.close();
139 } catch (Exception e) {
140 logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e);
146 public String toString() {
147 return "CambriaPublisherWrapper []";
153 * Kafka based library publisher.
155 public static class KafkaPublisherWrapper implements BusPublisher {
157 private static Logger logger = LoggerFactory.getLogger(KafkaPublisherWrapper.class);
158 private static final String KEY_SERIALIZER = "org.apache.kafka.common.serialization.StringSerializer";
160 private String topic;
165 private Producer<String, String> producer;
166 protected Properties kafkaProps;
169 * Kafka Publisher Wrapper.
171 * @param busTopicParams topic parameters
173 protected KafkaPublisherWrapper(BusTopicParams busTopicParams) {
175 if (busTopicParams.isTopicInvalid()) {
176 throw new IllegalArgumentException("No topic for Kafka");
179 this.topic = busTopicParams.getTopic();
181 //Setup Properties for consumer
182 kafkaProps = new Properties();
183 kafkaProps.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, busTopicParams.getServers().get(0));
184 if (busTopicParams.isAdditionalPropsValid()) {
185 for (Map.Entry<String, String> entry : busTopicParams.getAdditionalProps().entrySet()) {
186 kafkaProps.put(entry.getKey(), entry.getValue());
189 if (kafkaProps.get(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG) == null) {
190 kafkaProps.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, KEY_SERIALIZER);
192 if (kafkaProps.get(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG) == null) {
193 kafkaProps.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KEY_SERIALIZER);
196 producer = new KafkaProducer<>(kafkaProps);
200 public boolean send(String partitionId, String message) {
201 if (message == null) {
202 throw new IllegalArgumentException("No message provided");
207 ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic,
208 UUID.randomUUID().toString(), message);
210 this.producer.send(record);
212 } catch (Exception e) {
213 logger.warn("{}: SEND of {} cannot be performed because of {}", this, message, e.getMessage(), e);
220 public void close() {
221 logger.info("{}: CLOSE", this);
224 this.producer.close();
225 } catch (Exception e) {
226 logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e);
232 public String toString() {
233 return "KafkaPublisherWrapper []";
239 * DmaapClient library wrapper.
241 public abstract class DmaapPublisherWrapper implements BusPublisher {
243 private static Logger logger = LoggerFactory.getLogger(DmaapPublisherWrapper.class);
246 * MR based Publisher.
248 protected MRSimplerBatchPublisher publisher;
249 protected Properties props;
252 * MR Publisher Wrapper.
254 * @param servers messaging bus hosts
256 * @param username AAF or DME2 Login
257 * @param password AAF or DME2 Password
259 protected DmaapPublisherWrapper(ProtocolTypeConstants protocol, List<String> servers, String topic,
260 String username, String password, boolean useHttps) {
263 if (StringUtils.isBlank(topic)) {
264 throw new IllegalArgumentException("No topic for DMaaP");
268 configureProtocol(topic, protocol, servers, useHttps);
270 this.publisher.logTo(LoggerFactory.getLogger(MRSimplerBatchPublisher.class.getName()));
272 this.publisher.setUsername(username);
273 this.publisher.setPassword(password);
275 props = new Properties();
277 props.setProperty("Protocol", (useHttps ? "https" : "http"));
278 props.setProperty("contenttype", "application/json");
279 props.setProperty("username", username);
280 props.setProperty("password", password);
282 props.setProperty("topic", topic);
284 this.publisher.setProps(props);
286 if (protocol == ProtocolTypeConstants.AAF_AUTH) {
287 this.publisher.setHost(servers.get(0));
290 logger.info("{}: CREATION: using protocol {}", this, protocol.getValue());
293 private void configureProtocol(String topic, ProtocolTypeConstants protocol, List<String> servers,
296 if (protocol == ProtocolTypeConstants.AAF_AUTH) {
297 if (servers == null || servers.isEmpty()) {
298 throw new IllegalArgumentException("No DMaaP servers or DME2 partner provided");
301 ArrayList<String> dmaapServers = new ArrayList<>();
302 String port = useHttps ? ":3905" : ":3904";
303 for (String server : servers) {
304 dmaapServers.add(server + port);
308 this.publisher = new MRSimplerBatchPublisher.Builder().againstUrls(dmaapServers).onTopic(topic).build();
310 this.publisher.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
312 } else if (protocol == ProtocolTypeConstants.DME2) {
313 ArrayList<String> dmaapServers = new ArrayList<>();
314 dmaapServers.add("0.0.0.0:3904");
316 this.publisher = new MRSimplerBatchPublisher.Builder().againstUrls(dmaapServers).onTopic(topic).build();
318 this.publisher.setProtocolFlag(ProtocolTypeConstants.DME2.getValue());
321 throw new IllegalArgumentException("Invalid DMaaP protocol " + protocol);
326 public void close() {
327 logger.info("{}: CLOSE", this);
330 this.publisher.close(1, TimeUnit.SECONDS);
332 } catch (InterruptedException e) {
333 logger.warn("{}: CLOSE FAILED", this, e);
334 Thread.currentThread().interrupt();
336 } catch (Exception e) {
337 logger.warn("{}: CLOSE FAILED", this, e);
342 public boolean send(String partitionId, String message) {
343 if (message == null) {
344 throw new IllegalArgumentException("No message provided");
347 this.publisher.setPubResponse(new MRPublisherResponse());
348 this.publisher.send(partitionId, message);
349 MRPublisherResponse response = this.publisher.sendBatchWithResponse();
350 if (response != null) {
351 logger.debug("DMaaP publisher received {} : {}", response.getResponseCode(),
352 response.getResponseMessage());
359 public String toString() {
360 return "DmaapPublisherWrapper [" + "publisher.getAuthDate()=" + publisher.getAuthDate()
361 + ", publisher.getAuthKey()=" + publisher.getAuthKey() + ", publisher.getHost()="
362 + publisher.getHost() + ", publisher.getProtocolFlag()=" + publisher.getProtocolFlag()
363 + ", publisher.getUsername()=" + publisher.getUsername() + "]";
368 * DmaapClient library wrapper.
370 public static class DmaapAafPublisherWrapper extends DmaapPublisherWrapper {
372 * MR based Publisher.
374 public DmaapAafPublisherWrapper(List<String> servers, String topic, String aafLogin, String aafPassword,
377 super(ProtocolTypeConstants.AAF_AUTH, servers, topic, aafLogin, aafPassword, useHttps);
381 public static class DmaapDmePublisherWrapper extends DmaapPublisherWrapper {
386 * @param busTopicParams topic parameters
388 public DmaapDmePublisherWrapper(BusTopicParams busTopicParams) {
390 super(ProtocolTypeConstants.DME2, busTopicParams.getServers(), busTopicParams.getTopic(),
391 busTopicParams.getUserName(), busTopicParams.getPassword(), busTopicParams.isUseHttps());
393 String dme2RouteOffer = busTopicParams.isAdditionalPropsValid()
394 ? busTopicParams.getAdditionalProps().get(
395 PolicyEndPointProperties.DME2_ROUTE_OFFER_PROPERTY)
398 validateParams(busTopicParams, dme2RouteOffer);
400 String serviceName = busTopicParams.getServers().get(0);
402 /* These are required, no defaults */
403 props.setProperty("Environment", busTopicParams.getEnvironment());
404 props.setProperty("AFT_ENVIRONMENT", busTopicParams.getAftEnvironment());
406 props.setProperty(PolicyEndPointProperties.DME2_SERVICE_NAME_PROPERTY, serviceName);
408 if (busTopicParams.getPartner() != null) {
409 props.setProperty("Partner", busTopicParams.getPartner());
411 if (dme2RouteOffer != null) {
412 props.setProperty(PolicyEndPointProperties.DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer);
415 props.setProperty("Latitude", busTopicParams.getLatitude());
416 props.setProperty("Longitude", busTopicParams.getLongitude());
418 // ServiceName also a default, found in additionalProps
420 /* These are optional, will default to these values if not set in optionalProps */
421 props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", "50000");
422 props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", "240000");
423 props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", "15000");
424 props.setProperty("Version", "1.0");
425 props.setProperty("SubContextPath", "/");
426 props.setProperty("sessionstickinessrequired", "no");
428 /* These should not change */
429 props.setProperty("TransportType", "DME2");
430 props.setProperty("MethodType", "POST");
432 if (busTopicParams.isAdditionalPropsValid()) {
433 addAdditionalProps(busTopicParams);
436 this.publisher.setProps(props);
439 private void validateParams(BusTopicParams busTopicParams, String dme2RouteOffer) {
440 if (busTopicParams.isEnvironmentInvalid()) {
441 throw parmException(busTopicParams.getTopic(),
442 PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX);
444 if (busTopicParams.isAftEnvironmentInvalid()) {
445 throw parmException(busTopicParams.getTopic(),
446 PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX);
448 if (busTopicParams.isLatitudeInvalid()) {
449 throw parmException(busTopicParams.getTopic(),
450 PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX);
452 if (busTopicParams.isLongitudeInvalid()) {
453 throw parmException(busTopicParams.getTopic(),
454 PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX);
457 if ((busTopicParams.isPartnerInvalid())
458 && StringUtils.isBlank(dme2RouteOffer)) {
459 throw new IllegalArgumentException(
460 "Must provide at least " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
461 + busTopicParams.getTopic()
462 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX + " or "
463 + PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + busTopicParams.getTopic()
464 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX + " for DME2");
468 private void addAdditionalProps(BusTopicParams busTopicParams) {
469 for (Map.Entry<String, String> entry : busTopicParams.getAdditionalProps().entrySet()) {
470 String key = entry.getKey();
471 String value = entry.getValue();
474 props.setProperty(key, value);
479 private IllegalArgumentException parmException(String topic, String propnm) {
480 return new IllegalArgumentException("Missing " + PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
481 + topic + propnm + " property for DME2 in DMaaP");