2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.policy.drools.event.comm.bus.internal;
23 import java.net.MalformedURLException;
24 import java.security.GeneralSecurityException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Properties;
28 import java.util.concurrent.TimeUnit;
30 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
31 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
32 import com.att.nsa.cambria.client.CambriaClientBuilders;
33 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
34 import com.att.nsa.mr.client.impl.MRSimplerBatchPublisher;
35 import com.att.nsa.mr.test.clients.ProtocolTypeConstants;
36 import com.fasterxml.jackson.annotation.JsonIgnore;
38 public interface BusPublisher {
44 * @param message the message
45 * @return true if success, false otherwise
46 * @throws IllegalArgumentException if no message provided
48 public boolean send(String partitionId, String message) throws IllegalArgumentException;
51 * closes the publisher
56 * Cambria based library publisher
58 public static class CambriaPublisherWrapper implements BusPublisher {
61 * The actual Cambria publisher
64 protected volatile CambriaBatchingPublisher publisher;
66 public CambriaPublisherWrapper(List<String> servers, String topic,
69 throws IllegalArgumentException {
70 PublisherBuilder builder = new CambriaClientBuilders.PublisherBuilder();
72 builder.usingHosts(servers)
75 // Only supported in 0.2.4 version
76 // .logSendFailuresAfter(DEFAULT_LOG_SEND_FAILURES_AFTER);
78 if (apiKey != null && !apiKey.isEmpty() &&
79 apiSecret != null && !apiSecret.isEmpty()) {
80 builder.authenticatedBy(apiKey, apiSecret);
84 this.publisher = builder.build();
85 } catch (MalformedURLException | GeneralSecurityException e) {
86 throw new IllegalArgumentException(e);
94 public boolean send(String partitionId, String message)
95 throws IllegalArgumentException {
97 throw new IllegalArgumentException("No message provided");
100 this.publisher.send(partitionId, message);
101 } catch (Exception e) {
102 PolicyLogger.warn(CambriaPublisherWrapper.class.getName(),
103 "SEND of " + message + " IN " +
104 this + " cannot be performed because of " +
115 public void close() {
116 if (PolicyLogger.isInfoEnabled())
117 PolicyLogger.info(CambriaPublisherWrapper.class.getName(),
118 "CREATION: " + this);
121 this.publisher.close();
122 } catch (Exception e) {
123 PolicyLogger.warn(CambriaPublisherWrapper.class.getName(),
124 "CLOSE on " + this + " FAILED because of " +
131 public String toString() {
132 StringBuilder builder = new StringBuilder();
133 builder.append("CambriaPublisherWrapper [").
134 append("publisher.getPendingMessageCount()=").
135 append(publisher.getPendingMessageCount()).
137 return builder.toString();
143 * DmaapClient library wrapper
145 public static class DmaapPublisherWrapper implements BusPublisher {
149 protected MRSimplerBatchPublisher publisher;
151 public DmaapPublisherWrapper(List<String> servers, String topic,
153 String aafPassword) {
155 ArrayList<String> dmaapServers = new ArrayList<String>();
156 for (String server: servers) {
157 dmaapServers.add(server + ":3904");
161 new MRSimplerBatchPublisher.Builder().
162 againstUrls(dmaapServers).
166 this.publisher.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
168 this.publisher.setUsername(aafLogin);
169 this.publisher.setPassword(aafPassword);
171 Properties props = new Properties();
172 props.setProperty("Protocol", "http");
173 props.setProperty("contenttype", "application/json");
175 this.publisher.setProps(props);
177 this.publisher.setHost(servers.get(0));
179 if (PolicyLogger.isInfoEnabled())
180 PolicyLogger.info(DmaapPublisherWrapper.class.getName(),
181 "CREATION: " + this);
188 public void close() {
189 if (PolicyLogger.isInfoEnabled())
190 PolicyLogger.info(DmaapPublisherWrapper.class.getName(),
191 "CREATION: " + this);
194 this.publisher.close(1, TimeUnit.SECONDS);
195 } catch (Exception e) {
196 PolicyLogger.warn(DmaapPublisherWrapper.class.getName(),
197 "CLOSE: " + this + " because of " +
206 public boolean send(String partitionId, String message)
207 throws IllegalArgumentException {
209 throw new IllegalArgumentException("No message provided");
211 this.publisher.send(partitionId, message);
217 public String toString() {
218 StringBuilder builder = new StringBuilder();
219 builder.append("DmaapPublisherWrapper [").
220 append("publisher.getAuthDate()=").append(publisher.getAuthDate()).
221 append(", publisher.getAuthKey()=").append(publisher.getAuthKey()).
222 append(", publisher.getHost()=").append(publisher.getHost()).
223 append(", publisher.getProtocolFlag()=").append(publisher.getProtocolFlag()).
224 append(", publisher.getUsername()=").append(publisher.getUsername()).
225 append(", publisher.getPendingMessageCount()=").append(publisher.getPendingMessageCount()).
227 return builder.toString();