2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.common.endpoints.event.comm.bus.internal.impl;
23 import com.att.nsa.apiClient.http.HttpClient.ConnectionType;
24 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
25 import com.att.nsa.cambria.client.CambriaClientBuilders;
26 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
27 import com.fasterxml.jackson.annotation.JsonIgnore;
29 import java.net.MalformedURLException;
30 import java.security.GeneralSecurityException;
31 import java.util.List;
33 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Cambria based library publisher
40 public class CambriaPublisherWrapper implements BusPublisher {
42 private static Logger logger = LoggerFactory.getLogger(CambriaPublisherWrapper.class);
45 * The actual Cambria publisher
48 protected volatile CambriaBatchingPublisher publisher;
50 public CambriaPublisherWrapper(List<String> servers, String topic, String apiKey, String apiSecret,
52 this(servers, topic, apiKey, apiSecret, null, null, useHttps, false);
55 public CambriaPublisherWrapper(List<String> servers, String topic, String apiKey, String apiSecret, String username,
56 String password, boolean useHttps, boolean selfSignedCerts) {
58 PublisherBuilder builder = new CambriaClientBuilders.PublisherBuilder();
60 builder.usingHosts(servers).onTopic(topic);
62 // Set read timeout to 30 seconds (TBD: this should be configurable)
63 builder.withSocketTimeout(30000);
66 if (selfSignedCerts) {
67 builder.withConnectionType(ConnectionType.HTTPS_NO_VALIDATION);
69 builder.withConnectionType(ConnectionType.HTTPS);
74 if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) {
75 builder.authenticatedBy(apiKey, apiSecret);
78 if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
79 builder.authenticatedByHttp(username, password);
83 this.publisher = builder.build();
84 } catch (MalformedURLException | GeneralSecurityException e) {
85 throw new IllegalArgumentException(e);
93 public boolean send(String partitionId, String message) {
94 if (message == null) {
95 throw new IllegalArgumentException("No message provided");
99 this.publisher.send(partitionId, message);
100 } catch (Exception e) {
101 logger.warn("{}: SEND of {} cannot be performed because of {}", this, message, e.getMessage(), e);
111 public void close() {
112 logger.info("{}: CLOSE", this);
115 this.publisher.close();
116 } catch (Exception e) {
117 logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e);
123 public String toString() {
124 return "CambriaPublisherWrapper []";