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.onap.policy.utils;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
26 import java.util.concurrent.TimeUnit;
28 import org.onap.policy.common.logging.eelf.PolicyLogger;
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
32 import com.att.nsa.mr.client.impl.MRSimplerBatchPublisher;
33 import com.att.nsa.mr.test.clients.ProtocolTypeConstants;
35 public interface BusPublisher {
41 * @param message the message
42 * @return true if success, false otherwise
43 * @throws IllegalArgumentException if no message provided
45 public boolean send(String partitionId, String message);
48 * closes the publisher
53 * DmaapClient library wrapper
55 public static class DmaapPublisherWrapper implements BusPublisher {
56 private static Logger logger = FlexLogger.getLogger(DmaapPublisherWrapper.class);
60 protected MRSimplerBatchPublisher publisher;
62 public DmaapPublisherWrapper(List<String> servers, String topic,
66 ArrayList<String> dmaapServers = new ArrayList<>();
67 for (String server: servers) {
68 dmaapServers.add(server + ":3904");
72 new MRSimplerBatchPublisher.Builder().
73 againstUrls(dmaapServers).
77 this.publisher.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
79 this.publisher.setUsername(aafLogin);
80 this.publisher.setPassword(aafPassword);
82 Properties props = new Properties();
83 props.setProperty("Protocol", "http");
84 props.setProperty("contenttype", "application/json");
86 this.publisher.setProps(props);
88 this.publisher.setHost(servers.get(0));
90 if (PolicyLogger.isInfoEnabled())
91 PolicyLogger.info(DmaapPublisherWrapper.class.getName(),
100 if (logger.isInfoEnabled())
101 logger.info(DmaapPublisherWrapper.class.getName()+
102 "CREATION: " + this);
105 this.publisher.close(1, TimeUnit.SECONDS);
106 } catch (Exception e) {
107 logger.warn(DmaapPublisherWrapper.class.getName()+
108 "CLOSE: " + this + " because of " +
117 public boolean send(String partitionId, String message){
119 throw new IllegalArgumentException("No message provided");
121 this.publisher.send(partitionId, message);
127 public String toString() {
128 StringBuilder builder = new StringBuilder();
129 builder.append("DmaapPublisherWrapper [").
130 append("publisher.getAuthDate()=").append(publisher.getAuthDate()).
131 append(", publisher.getAuthKey()=").append(publisher.getAuthKey()).
132 append(", publisher.getHost()=").append(publisher.getHost()).
133 append(", publisher.getProtocolFlag()=").append(publisher.getProtocolFlag()).
134 append(", publisher.getUsername()=").append(publisher.getUsername()).
135 append(", publisher.getPendingMessageCount()=").append(publisher.getPendingMessageCount()).
137 return builder.toString();