Fixes for sonar critical issues
[policy/engine.git] / PolicyEngineUtils / src / main / java / org / onap / policy / utils / BusConsumer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineUtils
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.utils;
22
23 import java.net.MalformedURLException;
24 import java.util.List;
25 import java.util.Properties;
26
27 import com.att.nsa.mr.client.MRClient.MRApiException;
28 import com.att.nsa.mr.client.impl.MRConsumerImpl;
29 import com.att.nsa.mr.test.clients.ProtocolTypeConstants;
30
31 public interface BusConsumer {
32         
33         /**
34          * fetch messages
35          * 
36          * @return list of messages
37          * @throws MRApiException when error encountered by underlying libraries
38          */
39         public Iterable<String> fetch() throws MRApiException;
40         
41         /**
42          * close underlying library consumer
43          */
44         public void close();
45         
46         /**
47          * MR based consumer
48          */
49         public static class DmaapConsumerWrapper implements BusConsumer {
50                 
51                 /**
52                  * MR Consumer
53                  */
54                 protected MRConsumerImpl consumer;
55                 
56                 /**
57                  * MR Consumer Wrapper
58                  * 
59                  * @param servers messaging bus hosts
60                  * @param topic topic
61                  * @param apiKey API Key
62                  * @param apiSecret API Secret
63                  * @param aafLogin AAF Login
64                  * @param aafPassword AAF Password
65                  * @param consumerGroup Consumer Group
66                  * @param consumerInstance Consumer Instance
67                  * @param fetchTimeout Fetch Timeout
68                  * @param fetchLimit Fetch Limit
69                  */
70                 public DmaapConsumerWrapper(List<String> servers, String topic, 
71                                                                 String aafLogin, String aafPassword,
72                                                                 String consumerGroup, String consumerInstance,
73                                                                 int fetchTimeout, int fetchLimit)  throws MalformedURLException{
74                                         
75                         this.consumer = new MRConsumerImpl(servers, topic, 
76                                                                                            consumerGroup, consumerInstance, 
77                                                                                            fetchTimeout, fetchLimit, 
78                                                                                    null, aafLogin, aafPassword);
79                         
80                         this.consumer.setUsername(aafLogin);
81                         this.consumer.setPassword(aafPassword);
82                         
83                         this.consumer.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
84                         
85                         Properties props = new Properties();
86                         props.setProperty("Protocol", "http");
87                         this.consumer.setProps(props);
88                         this.consumer.setHost(servers.get(0) + ":3904");
89                 }
90                 
91                 /**
92                  * {@inheritDoc}
93                  */
94                 public Iterable<String> fetch() throws MRApiException {
95                         try {
96                 return this.consumer.fetch();
97             } catch (Exception e) {
98                 throw new MRApiException("Error during MR consumer Fetch ",e);
99             }
100                 }
101                 
102                 /**
103                  * {@inheritDoc}
104                  */
105                 public void close() {
106                         this.consumer.close();
107                 }
108                 
109                 @Override
110                 public String toString() {
111                         StringBuilder builder = new StringBuilder();
112                         builder.
113                         append("DmaapConsumerWrapper [").
114                         append("consumer.getAuthDate()=").append(consumer.getAuthDate()).
115                         append(", consumer.getAuthKey()=").append(consumer.getAuthKey()).
116                         append(", consumer.getHost()=").append(consumer.getHost()).
117                         append(", consumer.getProtocolFlag()=").append(consumer.getProtocolFlag()).
118                         append(", consumer.getUsername()=").append(consumer.getUsername()).
119                         append("]");
120                         return builder.toString();
121                 }
122         }
123
124 }