2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications copyright (c) 2019 Nokia
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.utils;
24 import java.net.MalformedURLException;
25 import java.util.List;
26 import java.util.Properties;
28 import org.onap.dmaap.mr.client.MRClient.MRApiException;
29 import org.onap.dmaap.mr.client.impl.MRConsumerImpl;
30 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
32 public interface BusConsumer {
37 * @return list of messages
38 * @throws MRApiException when error encountered by underlying libraries
40 Iterable<String> fetch() throws MRApiException;
43 * close underlying library consumer.
50 class DmaapConsumerWrapper implements BusConsumer {
55 protected MRConsumerImpl consumer;
58 * MR Consumer Wrapper.
60 * @param servers messaging bus hosts
62 * @param apiKey API Key
63 * @param apiSecret API Secret
64 * @param aafLogin AAF Login
65 * @param aafPassword AAF Password
66 * @param consumerGroup Consumer Group
67 * @param consumerInstance Consumer Instance
68 * @param fetchTimeout Fetch Timeout
69 * @param fetchLimit Fetch Limit
71 public DmaapConsumerWrapper(List<String> servers, String topic, String aafLogin, String aafPassword,
72 String consumerGroup, String consumerInstance, int fetchTimeout, int fetchLimit)
73 throws MalformedURLException {
75 this(new MRConsumerImpl(servers, topic, consumerGroup, consumerInstance, fetchTimeout, fetchLimit, null,
76 aafLogin, aafPassword), aafLogin, aafPassword, servers.get(0));
80 DmaapConsumerWrapper(MRConsumerImpl consumer, String aafLogin, String aafPassword, String host) {
81 this.consumer = consumer;
82 this.consumer.setUsername(aafLogin);
83 this.consumer.setPassword(aafPassword);
84 this.consumer.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
85 this.consumer.setHost(host + ":3904");
87 Properties props = new Properties();
88 props.setProperty("Protocol", "http");
89 this.consumer.setProps(props);
96 public Iterable<String> fetch() throws MRApiException {
98 return consumer.fetch();
99 } catch (Exception e) {
100 throw new MRApiException("Error during MR consumer Fetch ", e);
108 public void close() {
109 this.consumer.close();
113 public String toString() {
114 StringBuilder builder = new StringBuilder();
115 builder.append("DmaapConsumerWrapper [").append("consumer.getAuthDate()=").append(consumer.getAuthDate())
116 .append(", consumer.getAuthKey()=").append(consumer.getAuthKey()).append(", consumer.getHost()=")
117 .append(consumer.getHost()).append(", consumer.getProtocolFlag()=")
118 .append(consumer.getProtocolFlag()).append(", consumer.getUsername()=")
119 .append(consumer.getUsername()).append("]");
120 return builder.toString();