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.cambria.client.CambriaBatchingPublisher;
24 import com.att.nsa.cambria.client.CambriaClientBuilders;
25 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
26 import com.fasterxml.jackson.annotation.JsonIgnore;
28 import java.net.MalformedURLException;
29 import java.security.GeneralSecurityException;
30 import java.util.List;
32 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Cambria based library publisher
39 public class CambriaPublisherWrapper implements BusPublisher {
41 private static Logger logger = LoggerFactory.getLogger(CambriaPublisherWrapper.class);
44 * The actual Cambria publisher
47 protected volatile CambriaBatchingPublisher publisher;
49 public CambriaPublisherWrapper(List<String> servers, String topic, String apiKey, String apiSecret,
51 this(servers, topic, apiKey, apiSecret, null, null, useHttps);
54 public CambriaPublisherWrapper(List<String> servers, String topic, String apiKey, String apiSecret, String username,
55 String password, boolean useHttps) {
57 PublisherBuilder builder = new CambriaClientBuilders.PublisherBuilder();
59 builder.usingHosts(servers).onTopic(topic);
61 // Set read timeout to 30 seconds (TBD: this should be configurable)
62 builder.withSocketTimeout(30000);
69 if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) {
70 builder.authenticatedBy(apiKey, apiSecret);
73 if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
74 builder.authenticatedByHttp(username, password);
78 this.publisher = builder.build();
79 } catch (MalformedURLException | GeneralSecurityException e) {
80 throw new IllegalArgumentException(e);
88 public boolean send(String partitionId, String message) {
89 if (message == null) {
90 throw new IllegalArgumentException("No message provided");
94 this.publisher.send(partitionId, message);
95 } catch (Exception e) {
96 logger.warn("{}: SEND of {} cannot be performed because of {}", this, message, e.getMessage(), e);
106 public void close() {
107 logger.info("{}: CLOSE", this);
110 this.publisher.close();
111 } catch (Exception e) {
112 logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e);
118 public String toString() {
119 return "CambriaPublisherWrapper []";