aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-network-adapter/src/main/java/org/openecomp/mso/adapters/network/BpelRestClient.java
blob: dcd12d24e7f8e886014b5f14be95e45d4ca20591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.adapters.network;


import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.bind.DatatypeConverter;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.properties.MsoJavaProperties;
import org.openecomp.mso.properties.MsoPropertiesException;
import org.openecomp.mso.properties.MsoPropertiesFactory;

/**
 * This is the class that is used to POST replies from the MSO adapters to the BPEL engine.
 * It can be configured via property file, or modified using the member methods.
 * The properties to use are:
 * org.openecomp.mso.adapters.vnf.bpelauth  encrypted authorization string to send to BEPL engine
 * org.openecomp.mso.adapters.vnf.sockettimeout socket timeout value
 * org.openecomp.mso.adapters.vnf.connecttimeout connect timeout value
 * org.openecomp.mso.adapters.vnf.retrycount number of times to retry failed connections
 * org.openecomp.mso.adapters.vnf.retryinterval interval (in seconds) between retries
 * org.openecomp.mso.adapters.vnf.retrylist list of response codes that will trigger a retry (the special code
 * 			900 means "connection was not established")
 */
public class BpelRestClient {
	public  static final String MSO_PROP_NETWORK_ADAPTER = "MSO_PROP_NETWORK_ADAPTER";
	private static final String PROPERTY_DOMAIN          = "org.openecomp.mso.adapters.network";
	private static final String BPEL_AUTH_PROPERTY       = PROPERTY_DOMAIN+".bpelauth";
	private static final String SOCKET_TIMEOUT_PROPERTY  = PROPERTY_DOMAIN+".sockettimeout";
	private static final String CONN_TIMEOUT_PROPERTY    = PROPERTY_DOMAIN+".connecttimeout";
	private static final String RETRY_COUNT_PROPERTY     = PROPERTY_DOMAIN+".retrycount";
	private static final String RETRY_INTERVAL_PROPERTY  = PROPERTY_DOMAIN+".retryinterval";
	private static final String RETRY_LIST_PROPERTY      = PROPERTY_DOMAIN+".retrylist";
	private static final String ENCRYPTION_KEY           = "aa3871669d893c7fb8abbcda31b88b4f";
	private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);

	/** Default socket timeout (in seconds) */
	public static final int DEFAULT_SOCKET_TIMEOUT = 5;
	/** Default connect timeout (in seconds) */
	public static final int DEFAULT_CONNECT_TIMEOUT = 5;
	/** By default, retry up to five times */
	public static final int DEFAULT_RETRY_COUNT = 5;
	/** Default interval to wait between retries (in seconds), negative means use backoff algorithm */
	public static final int DEFAULT_RETRY_INTERVAL = -15;
	/** Default list of response codes to trigger a retry */
	public static final String DEFAULT_RETRY_LIST = "408,429,500,502,503,504,900";	// 900 is "connection failed"
	/** Default credentials */
	public static final String DEFAULT_CREDENTIALS = "";

	// Properties of the BPEL client -- all are configurable
	private int socketTimeout;
	private int connectTimeout;
	private int retryCount;
	private int retryInterval;
	private Set<Integer> retryList;
	private String credentials;

	// last response from BPEL engine
	private int lastResponseCode;
	private String lastResponse;

	/**
	 * Create a client to send results to the BPEL engine, using configuration from the
	 * MSO_PROP_NETWORK_ADAPTER properties.
	 */
	public BpelRestClient() {
		socketTimeout  = DEFAULT_SOCKET_TIMEOUT;
		connectTimeout = DEFAULT_CONNECT_TIMEOUT;
		retryCount     = DEFAULT_RETRY_COUNT;
		retryInterval  = DEFAULT_RETRY_INTERVAL;
		setRetryList(DEFAULT_RETRY_LIST);
		credentials    = DEFAULT_CREDENTIALS;
		lastResponseCode = 0;
		lastResponse = "";

		try {
			MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
			MsoJavaProperties jp = msoPropertiesFactory.getMsoJavaProperties (MSO_PROP_NETWORK_ADAPTER);
			socketTimeout  = jp.getIntProperty(SOCKET_TIMEOUT_PROPERTY, DEFAULT_SOCKET_TIMEOUT);
			connectTimeout = jp.getIntProperty(CONN_TIMEOUT_PROPERTY,   DEFAULT_CONNECT_TIMEOUT);
			retryCount     = jp.getIntProperty(RETRY_COUNT_PROPERTY,    DEFAULT_RETRY_COUNT);
			retryInterval  = jp.getIntProperty(RETRY_INTERVAL_PROPERTY, DEFAULT_RETRY_INTERVAL);
			setRetryList(jp.getProperty(RETRY_LIST_PROPERTY, DEFAULT_RETRY_LIST));
			credentials    = jp.getEncryptedProperty(BPEL_AUTH_PROPERTY, DEFAULT_CREDENTIALS, ENCRYPTION_KEY);
		} catch (MsoPropertiesException e) {
			String error = "Unable to get properties:" + MSO_PROP_NETWORK_ADAPTER;
			LOGGER.error (MessageEnum.RA_CONFIG_EXC, error, "", "", MsoLogger.ErrorCode.DataError, "Unable to get properties", e);
		}
	}

	public int getSocketTimeout() {
		return socketTimeout;
	}

	public void setSocketTimeout(int socketTimeout) {
		this.socketTimeout = socketTimeout;
	}

	public int getConnectTimeout() {
		return connectTimeout;
	}

	public void setConnectTimeout(int connectTimeout) {
		this.connectTimeout = connectTimeout;
	}

	public int getRetryCount() {
		return retryCount;
	}

	public void setRetryCount(int retryCount) {
		if (retryCount < 0)
			retryCount = DEFAULT_RETRY_COUNT;
		this.retryCount = retryCount;
	}

	public int getRetryInterval() {
		return retryInterval;
	}

	public void setRetryInterval(int retryInterval) {
		this.retryInterval = retryInterval;
	}

	public String getCredentials() {
		return credentials;
	}

	public void setCredentials(String credentials) {
		this.credentials = credentials;
	}

	public String getRetryList() {
		if (retryList.size() == 0)
			return "";
		String t = retryList.toString();
		return t.substring(1, t.length()-1);
	}

	public void setRetryList(String retryList) {
		Set<Integer> s = new TreeSet<Integer>();
		for (String t : retryList.split("[, ]")) {
			try {
				s.add(Integer.parseInt(t));
			} catch (NumberFormatException x) {
				// ignore
			}
		}
		this.retryList = s;
	}

	public int getLastResponseCode() {
		return lastResponseCode;
	}

	public String getLastResponse() {
		return lastResponse;
	}

	/**
	 * Post a response to the URL of the BPEL engine.  As long as the response code is one of those in
	 * the retryList, the post will be retried up to "retrycount" times with an interval (in seconds)
	 * of "retryInterval".  If retryInterval is negative, then each successive retry interval will be
	 * double the previous one.
	 * @param toBpelStr the content (XML or JSON) to post
	 * @param bpelUrl the URL to post to
	 * @param isxml true if the content is XML, otherwise assumed to be JSON
	 * @return true if the post succeeded, false if all retries failed
	 */
	public boolean bpelPost(final String toBpelStr, final String bpelUrl, final boolean isxml)  {
		debug("Sending response to BPEL: " + toBpelStr);
		int totalretries = 0;
		int retryint = retryInterval;
		while (true) {
			sendOne(toBpelStr, bpelUrl, isxml);
			// Note: really should handle response code 415 by switching between content types if needed
			if (!retryList.contains(lastResponseCode)) {
				debug("Got response code: " + lastResponseCode + ": returning.");
				return true;
			}
			if (totalretries >= retryCount) {
				debug("Retried " + totalretries + " times, giving up.");
				LOGGER.error(MessageEnum.RA_SEND_VNF_NOTIF_ERR, "Could not deliver response to BPEL after "+totalretries+" tries: "+toBpelStr, "Camunda", "", MsoLogger.ErrorCode.DataError, "Could not deliver response to BPEL");
				return false;
			}
			totalretries++;
			int sleepinterval = retryint;
			if (retryint < 0) {
				// if retry interval is negative double the retry on each pass
				sleepinterval = -retryint;
				retryint *= 2;
			}
			debug("Sleeping for " + sleepinterval + " seconds.");
			try {
				Thread.sleep(sleepinterval * 1000L);
			} catch (InterruptedException e) {
				// ignore
			}
		}
	}
	private void debug(String m) {
		LOGGER.debug(m);
//		System.err.println(m);
	}
	private void sendOne(final String toBpelStr, final String bpelUrl, final boolean isxml) {
		LOGGER.debug("Sending to BPEL server: "+bpelUrl);
		LOGGER.debug("Content is: "+toBpelStr);

        //POST
		HttpPost post = new HttpPost(bpelUrl);
		if (credentials != null && !credentials.isEmpty())
			post.addHeader("Authorization", "Basic " + DatatypeConverter.printBase64Binary(credentials.getBytes()));

        //ContentType
        ContentType ctype = isxml ? ContentType.APPLICATION_XML : ContentType.APPLICATION_JSON;
        post.setEntity(new StringEntity(toBpelStr, ctype));

        //Timeouts
		RequestConfig requestConfig = RequestConfig
			.custom()
			.setSocketTimeout(socketTimeout * 1000)
			.setConnectTimeout(connectTimeout * 1000)
			.build();
		post.setConfig(requestConfig);

        //Client 4.3+
		//Execute & GetResponse
		try (CloseableHttpClient client = HttpClients.createDefault();
             CloseableHttpResponse response = client.execute(post)) {
			if (response != null) {
				lastResponseCode = response.getStatusLine().getStatusCode();
				HttpEntity entity = response.getEntity();
				lastResponse = (entity != null) ? EntityUtils.toString(entity) : "";
			} else {
				lastResponseCode = 900;
				lastResponse = "";
			}
		} catch (Exception e) {
			String error = "Error sending Bpel notification:" + toBpelStr;
			LOGGER.error (MessageEnum.RA_SEND_VNF_NOTIF_ERR, error, "Camunda", "", MsoLogger.ErrorCode.AvailabilityError, "Exception sending Bpel notification", e);
			lastResponseCode = 900;
			lastResponse = "";
		}
		LOGGER.debug("Response code from BPEL server: "+lastResponseCode);
		LOGGER.debug("Response body is: "+lastResponse);
	}

	public static void main(String[] a) throws MsoPropertiesException {
		final String bpelengine = "http://mtmac1.research.att.com:8080/catch.jsp";
		final String propfile = "/tmp/mso.vnf.properties";
		// "/Users/eby/src/mso.rest/mso/packages/mso-config-centralized/mso-po-adapter-config/mso.vnf.properties"
		final String xml =
			"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
			"<updateVolumeGroupResponse><volumeGroupId>1464013300723</volumeGroupId><volumeGroupOutputs>" +
	        "<entry><key>clyde</key><value>10</value></entry>" +
	        "<entry><key>wayne</key><value>99</value></entry>" +
	        "<entry><key>mickey</key><value>7</value></entry>" +
	        "</volumeGroupOutputs></updateVolumeGroupResponse>";

		MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
		msoPropertiesFactory.initializeMsoProperties (MSO_PROP_NETWORK_ADAPTER, propfile);

		BpelRestClient bc = new BpelRestClient();
		System.out.println(bc.getRetryList());
		System.out.println(bc.getCredentials()); // poAvos:Domain2.0!

		bc.bpelPost(xml, bpelengine, true);
		System.out.println("respcode = "+bc.getLastResponseCode());
		System.out.println("resp = "+bc.getLastResponse());
	}
}