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;
27 import org.onap.dmaap.mr.client.impl.MRSimplerBatchPublisher;
28 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
29 import org.onap.policy.common.logging.eelf.PolicyLogger;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
33 public interface BusPublisher {
39 * @param message the message
40 * @return true if success, false otherwise
41 * @throws IllegalArgumentException if no message provided
43 public boolean send(String partitionId, String message);
46 * closes the publisher
51 * DmaapClient library wrapper
53 public static class DmaapPublisherWrapper implements BusPublisher {
54 private static Logger logger = FlexLogger.getLogger(DmaapPublisherWrapper.class);
58 protected MRSimplerBatchPublisher publisher;
60 public DmaapPublisherWrapper(List<String> servers, String topic,
64 ArrayList<String> dmaapServers = new ArrayList<>();
65 for (String server: servers) {
66 dmaapServers.add(server + ":3904");
70 new MRSimplerBatchPublisher.Builder().
71 againstUrls(dmaapServers).
75 this.publisher.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
77 this.publisher.setUsername(aafLogin);
78 this.publisher.setPassword(aafPassword);
80 Properties props = new Properties();
81 props.setProperty("Protocol", "http");
82 props.setProperty("contenttype", "application/json");
84 this.publisher.setProps(props);
86 this.publisher.setHost(servers.get(0));
88 if (PolicyLogger.isInfoEnabled())
89 PolicyLogger.info(DmaapPublisherWrapper.class.getName(),
98 if (logger.isInfoEnabled())
99 logger.info(DmaapPublisherWrapper.class.getName()+
100 "CREATION: " + this);
103 this.publisher.close(1, TimeUnit.SECONDS);
104 } catch (Exception e) {
105 logger.warn(DmaapPublisherWrapper.class.getName()+
106 "CLOSE: " + this + " because of " +
115 public boolean send(String partitionId, String message){
117 throw new IllegalArgumentException("No message provided");
119 this.publisher.send(partitionId, message);
125 public String toString() {
126 StringBuilder builder = new StringBuilder();
127 builder.append("DmaapPublisherWrapper [").
128 append("publisher.getAuthDate()=").append(publisher.getAuthDate()).
129 append(", publisher.getAuthKey()=").append(publisher.getAuthKey()).
130 append(", publisher.getHost()=").append(publisher.getHost()).
131 append(", publisher.getProtocolFlag()=").append(publisher.getProtocolFlag()).
132 append(", publisher.getUsername()=").append(publisher.getUsername()).
133 append(", publisher.getPendingMessageCount()=").append(publisher.getPendingMessageCount()).
135 return builder.toString();