Moving all files to root directory
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / openecomp / appc / adapter / dmaap / DmaapProducer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.adapter.dmaap;
23
24 import java.net.URI;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33
34 import org.apache.http.client.methods.CloseableHttpResponse;
35 import org.apache.http.client.methods.HttpPost;
36 import org.apache.http.entity.StringEntity;
37
38 public class DmaapProducer extends CommonHttpClient implements Producer {
39
40     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(DmaapProducer.class);
41
42     private static final String CONTENT_TYPE = "application/cambria";
43     private static final String URL_TEMPLATE = "%s/events/%s";
44
45     private List<String> hosts;
46     private Set<String> topics;
47
48     private boolean useHttps = false;
49
50     public DmaapProducer(Collection<String> urls, String topicName) {
51         hosts = new ArrayList<String>();
52         topics = new HashSet<String>();
53         topics.add(topicName);
54
55         for (String host : urls) {
56             hosts.add(formatHostString(host));
57         }
58     }
59
60     public DmaapProducer(Collection<String> urls, Set<String> topicNames) {
61         hosts = new ArrayList<String>();
62         topics = topicNames;
63
64         for (String host : urls) {
65             hosts.add(formatHostString(host));
66         }
67     }
68
69     @Override
70     public void updateCredentials(String user, String pass) {
71         LOG.debug(String.format("Setting auth to %s for %s", user, this.toString()));
72         this.setBasicAuth(user, pass);
73     }
74
75     @Override
76     public boolean post(String partition, String data) {
77         int sent = 0;
78         try {
79             HttpPost request = postReq(null);
80             request.setHeader("Content-Type", CONTENT_TYPE);
81             request.setEntity(new StringEntity(bodyLine(partition, data)));
82
83             for (String topic : topics) {
84                 String uriStr = String.format(URL_TEMPLATE, hosts.get(0), topic);
85                 try {
86                     request.setURI(new URI(uriStr));
87                     CloseableHttpResponse response = getClient().execute(request);
88                     if (response.getStatusLine().getStatusCode() == 200) {
89                         sent++;
90                     }
91                     response.close();
92                 } catch (Exception sendEx) {
93                     LOG.error(String.format("Failed to send message to %s. Reason: %s", uriStr, sendEx.getMessage()),
94                         sendEx);
95                     if (hosts.size() > 1) {
96                         String failedUrl = hosts.remove(0);
97                         hosts.add(failedUrl);
98                         LOG.debug(String.format("Moving host %s to the end of the pool. New primary host is %s",
99                             failedUrl, hosts.get(0)));
100                     }
101                 }
102             }
103         } catch (Exception buildEx) {
104             LOG.error(
105                 String.format("Failed to build request with string [%s]. Message not sent to any topic. Reason: %s",
106                     data, buildEx.getMessage()),
107                 buildEx);
108         }
109         return sent == topics.size();
110     }
111
112     @Override
113     public void useHttps(boolean yes) {
114         useHttps = yes;
115     }
116
117     /**
118      * Format the body for the application/cambria content type with no partitioning.
119      *
120      * @param msg
121      *            The message body to format
122      * @return A string in the application/cambria content type
123      */
124     private String bodyLine(String partition, String msg) {
125         String p = (partition == null) ? "" : partition;
126         String m = (msg == null) ? "" : msg;
127         return String.format("%d.%d.%s%s", p.length(), m.length(), p, m);
128     }
129     
130     public void close(){
131         //not used yet
132     }
133 }