2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * Modifications Copyright © 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.sli.plugins.restapicall;
25 import static java.lang.Boolean.valueOf;
26 import static javax.ws.rs.client.Entity.entity;
27 import static org.onap.ccsdk.sli.plugins.restapicall.AuthType.fromString;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.net.SocketException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.security.KeyStore;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.Iterator;
41 import java.util.List;
43 import java.util.Map.Entry;
44 import java.util.Properties;
46 import javax.net.ssl.HttpsURLConnection;
47 import javax.net.ssl.KeyManagerFactory;
48 import javax.net.ssl.SSLContext;
49 import javax.ws.rs.ProcessingException;
50 import javax.ws.rs.client.Client;
51 import javax.ws.rs.client.ClientBuilder;
52 import javax.ws.rs.client.Entity;
53 import javax.ws.rs.client.Invocation;
54 import javax.ws.rs.client.WebTarget;
55 import javax.ws.rs.core.EntityTag;
56 import javax.ws.rs.core.Feature;
57 import javax.ws.rs.core.MediaType;
58 import javax.ws.rs.core.MultivaluedMap;
59 import javax.ws.rs.core.Response;
60 import javax.ws.rs.core.UriBuilder;
61 import javax.ws.rs.core.Response;
62 import org.apache.commons.lang3.StringUtils;
63 import org.codehaus.jettison.json.JSONException;
64 import org.codehaus.jettison.json.JSONObject;
65 import org.glassfish.jersey.client.ClientProperties;
66 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
67 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
68 import org.glassfish.jersey.client.oauth1.ConsumerCredentials;
69 import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
70 import org.glassfish.jersey.media.multipart.MultiPart;
71 import org.glassfish.jersey.media.multipart.MultiPartFeature;
72 import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
73 import org.glassfish.jersey.client.oauth1.ConsumerCredentials;
74 import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
75 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
76 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
77 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
81 public class RestapiCallNode implements SvcLogicJavaPlugin {
83 protected static final String PARTNERS_FILE_NAME = "partners.json";
84 protected static final String UEB_PROPERTIES_FILE_NAME = "ueb.properties";
85 protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
86 protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
88 private static final Logger log = LoggerFactory.getLogger(RestapiCallNode.class);
89 private String uebServers;
90 private String defaultUebTemplateFileName = "/opt/bvc/restapi/templates/default-ueb-message.json";
92 private String responseReceivedMessage = "Response received. Time: {}";
93 private String responseHttpCodeMessage = "HTTP response code: {}";
94 private String requestPostingException = "Exception while posting http request to client ";
95 protected static final String skipSendingMessage = "skipSending";
96 protected static final String responsePrefix = "responsePrefix";
97 protected static final String restapiUrlString = "restapiUrl";
98 protected static final String restapiUserKey = "restapiUser";
99 protected static final String restapiPasswordKey = "restapiPassword";
101 protected HashMap<String,PartnerDetails> partnerStore;
103 public RestapiCallNode() {
104 String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
106 String jsonString = readFile(configDir + "/" + PARTNERS_FILE_NAME);
107 JSONObject partners = new JSONObject(jsonString);
108 partnerStore = new HashMap<String,PartnerDetails>();
109 loadPartners(partners);
110 log.info("Partners support enabled");
111 } catch (Exception e) {
112 log.warn("Partners file could not be read, Partner support will not be enabled.", e);
115 try (FileInputStream in = new FileInputStream(configDir + "/" + UEB_PROPERTIES_FILE_NAME)) {
116 Properties props = new Properties();
118 uebServers = props.getProperty("servers");
119 log.info("UEB support enabled");
120 } catch (Exception e) {
121 log.warn("UEB properties could not be read, UEB support will not be enabled.", e);
125 protected void loadPartners(JSONObject partners) {
126 Iterator<String> keys = partners.keys();
127 String partnerUserKey = "user";
128 String partnerPasswordKey = "password";
129 String partnerUrlKey = "url";
131 while (keys.hasNext()) {
132 String partnerKey = keys.next();
134 JSONObject partnerObject = (JSONObject) partners.get(partnerKey);
135 if (partnerObject.has(partnerUserKey) && partnerObject.has(partnerPasswordKey)) {
137 if (partnerObject.has(partnerUrlKey)) {
138 url = partnerObject.getString(partnerUrlKey);
140 String userName = partnerObject.getString(partnerUserKey);
141 String password = partnerObject.getString(partnerPasswordKey);
142 PartnerDetails details = new PartnerDetails(userName, password, url);
143 partnerStore.put(partnerKey, details);
144 log.info("mapped partner using partner key " + partnerKey);
146 log.info("Partner " + partnerKey + " is missing required keys, it won't be mapped");
148 } catch (JSONException e) {
149 log.info("Couldn't map the partner using partner key " + partnerKey, e);
155 * Returns parameters from the parameter map.
157 * @param paramMap parameter map
158 * @param p parameters instance
159 * @return parameters filed instance
160 * @throws SvcLogicException when svc logic exception occurs
162 public static Parameters getParameters(Map<String, String> paramMap,
164 throws SvcLogicException {
166 p.templateFileName = parseParam(paramMap, "templateFileName",
168 p.requestBody = parseParam(paramMap, "requestBody", false, null);
169 p.restapiUrl = parseParam(paramMap, restapiUrlString, true, null);
170 validateUrl(p.restapiUrl);
171 p.restapiUrlSuffix = parseParam(paramMap, "restapiUrlSuffix",
173 p.restapiUser = parseParam(paramMap, restapiUserKey, false, null);
174 p.restapiPassword = parseParam(paramMap, restapiPasswordKey, false,
176 if(p.restapiUrlSuffix != null) {
177 p.restapiUrl = p.restapiUrl + p.restapiUrlSuffix;
178 validateUrl(p.restapiUrl);
180 p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey",
182 p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret",
184 p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod",
186 p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
187 p.contentType = parseParam(paramMap, "contentType", false, null);
188 p.format = Format.fromString(parseParam(paramMap, "format", false,
190 p.authtype = fromString(parseParam(paramMap, "authType", false,
192 p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod",
194 p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
195 p.listNameList = getListNameList(paramMap);
196 String skipSendingStr = paramMap.get(skipSendingMessage);
197 p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
198 p.convertResponse = valueOf(parseParam(paramMap, "convertResponse",
200 p.trustStoreFileName = parseParam(paramMap, "trustStoreFileName",
202 p.trustStorePassword = parseParam(paramMap, "trustStorePassword",
204 p.keyStoreFileName = parseParam(paramMap, "keyStoreFileName",
206 p.keyStorePassword = parseParam(paramMap, "keyStorePassword",
208 p.ssl = p.trustStoreFileName != null && p.trustStorePassword != null
209 && p.keyStoreFileName != null && p.keyStorePassword != null;
210 p.customHttpHeaders = parseParam(paramMap, "customHttpHeaders",
212 p.partner = parseParam(paramMap, "partner", false, null);
213 p.dumpHeaders = valueOf(parseParam(paramMap, "dumpHeaders",
215 p.returnRequestPayload = valueOf(parseParam(
216 paramMap, "returnRequestPayload", false, null));
217 p.accept = parseParam(paramMap, "accept",
219 p.multipartFormData = valueOf(parseParam(paramMap, "multipartFormData",
221 p.multipartFile = parseParam(paramMap, "multipartFile",
227 * Validates the given URL in the parameters.
229 * @param restapiUrl rest api URL
230 * @throws SvcLogicException when URL validation fails
232 private static void validateUrl(String restapiUrl) throws SvcLogicException {
233 if (restapiUrl.contains(",")) {
234 String[] urls = restapiUrl.split(",");
235 for(String url : urls) {
240 URI.create(restapiUrl);
241 } catch (IllegalArgumentException e) {
242 throw new SvcLogicException("Invalid input of url " + e.getLocalizedMessage(), e);
248 * Returns the list of list name.
250 * @param paramMap parameters map
251 * @return list of list name
253 private static Set<String> getListNameList(Map<String, String> paramMap) {
254 Set<String> ll = new HashSet<>();
255 for (Map.Entry<String, String> entry : paramMap.entrySet()) {
256 if (entry.getKey().startsWith("listName")) {
257 ll.add(entry.getValue());
264 * Parses the parameter string map of property, validates if required,
265 * assigns default value if present and returns the value.
267 * @param paramMap string param map
268 * @param name name of the property
269 * @param required if value required
270 * @param def default value
271 * @return value of the property
272 * @throws SvcLogicException if required parameter value is empty
274 public static String parseParam(Map<String, String> paramMap, String name,
275 boolean required, String def)
276 throws SvcLogicException {
277 String s = paramMap.get(name);
279 if (s == null || s.trim().length() == 0) {
283 throw new SvcLogicException("Parameter " + name + " is required in RestapiCallNode");
287 StringBuilder value = new StringBuilder();
289 int i1 = s.indexOf('%');
291 int i2 = s.indexOf('%', i1 + 1);
296 String varName = s.substring(i1 + 1, i2);
297 String varValue = System.getenv(varName);
298 if (varValue == null) {
299 varValue = "%" + varName + "%";
302 value.append(s.substring(i, i1));
303 value.append(varValue);
306 i1 = s.indexOf('%', i);
308 value.append(s.substring(i));
310 log.info("Parameter {}: [{}]", name, value);
311 return value.toString();
315 * Allows Directed Graphs the ability to interact with REST APIs.
316 * @param paramMap HashMap<String,String> of parameters passed by the DG to this function
318 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th><th>example values</th></thead>
320 * <tr><td>templateFileName</td><td>Optional</td><td>full path to template file that can be used to build a request</td><td>/sdncopt/bvc/restapi/templates/vnf_service-configuration-operation_minimal.json</td></tr>
321 * <tr><td>restapiUrl</td><td>Mandatory</td><td>url to send the request to</td><td>https://sdncodl:8543/restconf/operations/L3VNF-API:create-update-vnf-request</td></tr>
322 * <tr><td>restapiUser</td><td>Optional</td><td>user name to use for http basic authentication</td><td>sdnc_ws</td></tr>
323 * <tr><td>restapiPassword</td><td>Optional</td><td>unencrypted password to use for http basic authentication</td><td>plain_password</td></tr>
324 * <tr><td>oAuthConsumerKey</td><td>Optional</td><td>Consumer key to use for http oAuth authentication</td><td>plain_key</td></tr>
325 * <tr><td>oAuthConsumerSecret</td><td>Optional</td><td>Consumer secret to use for http oAuth authentication</td><td>plain_secret</td></tr>
326 * <tr><td>oAuthSignatureMethod</td><td>Optional</td><td>Consumer method to use for http oAuth authentication</td><td>method</td></tr>
327 * <tr><td>oAuthVersion</td><td>Optional</td><td>Version http oAuth authentication</td><td>version</td></tr>
328 * <tr><td>contentType</td><td>Optional</td><td>http content type to set in the http header</td><td>usually application/json or application/xml</td></tr>
329 * <tr><td>format</td><td>Optional</td><td>should match request body format</td><td>json or xml</td></tr>
330 * <tr><td>httpMethod</td><td>Optional</td><td>http method to use when sending the request</td><td>get post put delete patch</td></tr>
331 * <tr><td>responsePrefix</td><td>Optional</td><td>location the response will be written to in context memory</td><td>tmp.restapi.result</td></tr>
332 * <tr><td>listName[i]</td><td>Optional</td><td>Used for processing XML responses with repeating elements.</td>vpn-information.vrf-details<td></td></tr>
333 * <tr><td>skipSending</td><td>Optional</td><td></td><td>true or false</td></tr>
334 * <tr><td>convertResponse </td><td>Optional</td><td>whether the response should be converted</td><td>true or false</td></tr>
335 * <tr><td>customHttpHeaders</td><td>Optional</td><td>a list additional http headers to be passed in, follow the format in the example</td><td>X-CSI-MessageId=messageId,headerFieldName=headerFieldValue</td></tr>
336 * <tr><td>dumpHeaders</td><td>Optional</td><td>when true writes http header content to context memory</td><td>true or false</td></tr>
337 * <tr><td>partner</td><td>Optional</td><td>used to retrieve username, password and url if partner store exists</td><td>aaf</td></tr>
338 * <tr><td>returnRequestPayload</td><td>Optional</td><td>used to return payload built in the request</td><td>true or false</td></tr>
341 * @param ctx Reference to context memory
342 * @throws SvcLogicException
344 * @see String#split(String, int)
346 public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
347 sendRequest(paramMap, ctx, null);
350 protected void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx, RetryPolicy retryPolicy)
351 throws SvcLogicException {
353 HttpResponse r = new HttpResponse();
355 handlePartner(paramMap);
356 Parameters p = getParameters(paramMap, new Parameters());
357 if(p.restapiUrl.contains(",") && retryPolicy == null) {
358 String[] urls = p.restapiUrl.split(",");
359 retryPolicy = new RetryPolicy(urls,urls.length * 2);
360 p.restapiUrl = urls[0];
362 String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
365 if (p.templateFileName != null) {
366 String reqTemplate = readFile(p.templateFileName);
367 req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
368 } else if (p.requestBody != null) {
371 r = sendHttpRequest(req, p);
372 setResponseStatus(ctx, p.responsePrefix, r);
374 if (p.dumpHeaders && r.headers != null) {
375 for (Entry<String, List<String>> a : r.headers.entrySet()) {
376 ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
380 if (p.returnRequestPayload && req != null) {
381 ctx.setAttribute(pp + "httpRequest", req);
384 if (r.body != null && r.body.trim().length() > 0) {
385 ctx.setAttribute(pp + "httpResponse", r.body);
387 if (p.convertResponse) {
388 Map<String, String> mm = null;
389 if (p.format == Format.XML) {
390 mm = XmlParser.convertToProperties(r.body, p.listNameList);
391 } else if (p.format == Format.JSON) {
392 mm = JsonParser.convertToProperties(r.body);
396 for (Map.Entry<String, String> entry : mm.entrySet()) {
397 ctx.setAttribute(pp + entry.getKey(), entry.getValue());
402 } catch (SvcLogicException e) {
403 boolean shouldRetry = false;
404 if (e.getCause().getCause() instanceof SocketException) {
408 log.error("Error sending the request: " + e.getMessage(), e);
409 String prefix = parseParam(paramMap, responsePrefix, false, null);
410 if (retryPolicy == null || !shouldRetry) {
411 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
413 log.debug(retryPolicy.getRetryMessage());
415 //calling getNextHostName increments the retry count so it should be called before shouldRetry
416 String retryString = retryPolicy.getNextHostName();
417 if (retryPolicy.shouldRetry()) {
418 paramMap.put(restapiUrlString, retryString);
419 log.debug("retry attempt {} will use the retry url {}", retryPolicy.getRetryCount(), retryString);
420 sendRequest(paramMap, ctx, retryPolicy);
422 log.debug("Maximum retries reached, won't attempt to retry. Calling setFailureResponseStatus.");
423 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
425 } catch (Exception ex) {
426 String retryErrorMessage = "Retry attempt " + retryPolicy.getRetryCount() + "has failed with error message " + ex.getMessage();
427 setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
432 if (r != null && r.code >= 300) {
433 throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
437 protected void handlePartner(Map<String, String> paramMap) {
438 String partner = paramMap.get("partner");
439 if (partner != null && partner.length() > 0) {
440 PartnerDetails details = partnerStore.get(partner);
441 paramMap.put(restapiUserKey, details.username);
442 paramMap.put(restapiPasswordKey, details.password);
443 if(paramMap.get(restapiUrlString) == null) {
444 paramMap.put(restapiUrlString, details.url);
449 protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format)
450 throws SvcLogicException {
451 log.info("Building {} started", format);
452 long t1 = System.currentTimeMillis();
453 String originalTemplate = template;
455 template = expandRepeats(ctx, template, 1);
457 Map<String, String> mm = new HashMap<>();
458 for (String s : ctx.getAttributeKeySet()) {
459 mm.put(s, ctx.getAttribute(s));
462 StringBuilder ss = new StringBuilder();
464 while (i < template.length()) {
465 int i1 = template.indexOf("${", i);
467 ss.append(template.substring(i));
471 int i2 = template.indexOf('}', i1 + 2);
473 throw new SvcLogicException("Template error: Matching } not found");
476 String var1 = template.substring(i1 + 2, i2);
477 String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
478 if (value1 == null || value1.trim().length() == 0) {
479 // delete the whole element (line)
480 int i3 = template.lastIndexOf('\n', i1);
484 int i4 = template.indexOf('\n', i1);
486 i4 = template.length();
490 ss.append(template.substring(i, i3));
494 ss.append(template.substring(i, i1)).append(value1);
499 String req = format == Format.XML
500 ? XmlJsonUtil.removeEmptyStructXml(ss.toString()) : XmlJsonUtil.removeEmptyStructJson(originalTemplate, ss.toString());
502 if (format == Format.JSON) {
503 req = XmlJsonUtil.removeLastCommaJson(req);
506 long t2 = System.currentTimeMillis();
507 log.info("Building {} completed. Time: {}", format, t2 - t1);
512 protected String expandRepeats(SvcLogicContext ctx, String template, int level) throws SvcLogicException {
513 StringBuilder newTemplate = new StringBuilder();
515 while (k < template.length()) {
516 int i1 = template.indexOf("${repeat:", k);
518 newTemplate.append(template.substring(k));
522 int i2 = template.indexOf(':', i1 + 9);
524 throw new SvcLogicException(
525 "Template error: Context variable name followed by : is required after repeat");
528 // Find the closing }, store in i3
532 while (nn > 0 && i < template.length()) {
533 i3 = template.indexOf('}', i);
535 throw new SvcLogicException("Template error: Matching } not found");
537 int i32 = template.indexOf('{', i);
538 if (i32 >= 0 && i32 < i3) {
547 String var1 = template.substring(i1 + 9, i2);
548 String value1 = ctx.getAttribute(var1);
549 log.info(" {}:{}", var1, value1);
552 n = Integer.parseInt(value1);
553 } catch (NumberFormatException e) {
554 log.info("value1 not set or not a number, n will remain set at zero");
557 newTemplate.append(template.substring(k, i1));
559 String rpt = template.substring(i2 + 1, i3);
561 for (int ii = 0; ii < n; ii++) {
562 String ss = rpt.replaceAll("\\[\\$\\{" + level + "\\}\\]", "[" + ii + "]");
563 if (ii == n - 1 && ss.trim().endsWith(",")) {
564 int i4 = ss.lastIndexOf(',');
566 ss = ss.substring(0, i4) + ss.substring(i4 + 1);
569 newTemplate.append(ss);
576 return newTemplate.toString();
579 return expandRepeats(ctx, newTemplate.toString(), level + 1);
582 protected String readFile(String fileName) throws SvcLogicException {
584 byte[] encoded = Files.readAllBytes(Paths.get(fileName));
585 return new String(encoded, "UTF-8");
586 } catch (IOException | SecurityException e) {
587 throw new SvcLogicException("Unable to read file " + fileName + e.getLocalizedMessage(), e);
591 protected Client addAuthType(Client c, FileParam fp) throws SvcLogicException {
592 Parameters p = new Parameters();
593 p.restapiUser = fp.user;
594 p.restapiPassword = fp.password;
595 p.oAuthConsumerKey = fp.oAuthConsumerKey;
596 p.oAuthVersion = fp.oAuthVersion;
597 p.oAuthConsumerSecret = fp.oAuthConsumerSecret;
598 p.oAuthSignatureMethod = fp.oAuthSignatureMethod;
599 p.authtype = fp.authtype;
600 return addAuthType(c, p);
603 public Client addAuthType(Client client, Parameters p) throws SvcLogicException {
604 if (p.authtype == AuthType.Unspecified) {
605 if (p.restapiUser != null && p.restapiPassword != null) {
606 client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
607 } else if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null
608 && p.oAuthSignatureMethod != null) {
609 Feature oAuth1Feature = OAuth1ClientSupport
610 .builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
611 .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
612 client.register(oAuth1Feature);
616 if (p.authtype == AuthType.DIGEST) {
617 if (p.restapiUser != null && p.restapiPassword != null) {
618 client.register(HttpAuthenticationFeature.digest(p.restapiUser, p.restapiPassword));
620 throw new SvcLogicException(
621 "oAUTH authentication type selected but all restapiUser and restapiPassword " +
622 "parameters doesn't exist", new Throwable());
624 } else if (p.authtype == AuthType.BASIC) {
625 if (p.restapiUser != null && p.restapiPassword != null) {
626 client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
628 throw new SvcLogicException(
629 "oAUTH authentication type selected but all restapiUser and restapiPassword " +
630 "parameters doesn't exist", new Throwable());
632 } else if (p.authtype == AuthType.OAUTH) {
633 if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
634 Feature oAuth1Feature = OAuth1ClientSupport
635 .builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
636 .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
637 client.register(oAuth1Feature);
639 throw new SvcLogicException(
640 "oAUTH authentication type selected but all oAuthConsumerKey, oAuthConsumerSecret " +
641 "and oAuthSignatureMethod parameters doesn't exist", new Throwable());
649 * Receives the http response for the http request sent.
651 * @param request request msg
652 * @param p parameters
653 * @return HTTP response
654 * @throws SvcLogicException when sending http request fails
656 public HttpResponse sendHttpRequest(String request, Parameters p)
657 throws SvcLogicException {
659 SSLContext ssl = null;
660 if (p.ssl && p.restapiUrl.startsWith("https")) {
661 ssl = createSSLContext(p);
666 HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());
667 client = ClientBuilder.newBuilder().sslContext(ssl).hostnameVerifier((s, sslSession) -> true)
670 client = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true)
673 client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
674 // Needed to support additional HTTP methods such as PATCH
675 client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
677 WebTarget webTarget = addAuthType(client, p).target(p.restapiUrl);
679 log.info("Sending request below to url " + p.restapiUrl);
681 long t1 = System.currentTimeMillis();
683 HttpResponse r = new HttpResponse();
685 String accept = p.accept;
687 accept = p.format == Format.XML ? "application/xml" : "application/json";
690 String contentType = p.contentType;
691 if(contentType == null) {
692 contentType = accept + ";charset=UTF-8";
695 if (!p.skipSending && !p.multipartFormData) {
697 Invocation.Builder invocationBuilder = webTarget.request(contentType).accept(accept);
699 if (p.format == Format.NONE) {
700 invocationBuilder.header("", "");
703 if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
704 String[] keyValuePairs = p.customHttpHeaders.split(",");
705 for (String singlePair : keyValuePairs) {
706 int equalPosition = singlePair.indexOf('=');
707 invocationBuilder.header(singlePair.substring(0, equalPosition),
708 singlePair.substring(equalPosition + 1, singlePair.length()));
712 invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
714 invocationBuilder.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
719 response = invocationBuilder.method(p.httpMethod.toString(), entity(request, contentType));
720 } catch (ProcessingException | IllegalStateException e) {
721 throw new SvcLogicException(requestPostingException +
722 e.getLocalizedMessage(), e);
725 r.code = response.getStatus();
726 r.headers = response.getStringHeaders();
727 EntityTag etag = response.getEntityTag();
729 r.message = etag.getValue();
731 if (response.hasEntity() && r.code != 204) {
732 r.body = response.readEntity(String.class);
734 } else if (!p.skipSending && p.multipartFormData) {
736 WebTarget wt = client.register(MultiPartFeature.class).target(p.restapiUrl);
738 MultiPart multiPart = new MultiPart();
739 multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
741 FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
742 new File(p.multipartFile),
743 MediaType.APPLICATION_OCTET_STREAM_TYPE);
744 multiPart.bodyPart(fileDataBodyPart);
747 Invocation.Builder invocationBuilder = wt.request(contentType).accept(accept);
749 if (p.format == Format.NONE) {
750 invocationBuilder.header("", "");
753 if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
754 String[] keyValuePairs = p.customHttpHeaders.split(",");
755 for (String singlePair : keyValuePairs) {
756 int equalPosition = singlePair.indexOf('=');
757 invocationBuilder.header(singlePair.substring(0, equalPosition),
758 singlePair.substring(equalPosition + 1, singlePair.length()));
762 invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
767 response = invocationBuilder.method(p.httpMethod.toString(), entity(multiPart, multiPart.getMediaType()));
768 } catch (ProcessingException | IllegalStateException e) {
769 throw new SvcLogicException(requestPostingException +
770 e.getLocalizedMessage(), e);
773 r.code = response.getStatus();
774 r.headers = response.getStringHeaders();
775 EntityTag etag = response.getEntityTag();
777 r.message = etag.getValue();
779 if (response.hasEntity() && r.code != 204) {
780 r.body = response.readEntity(String.class);
785 long t2 = System.currentTimeMillis();
786 log.info(responseReceivedMessage, t2 - t1);
787 log.info(responseHttpCodeMessage, r.code);
788 log.info("HTTP response message: {}", r.message);
789 logHeaders(r.headers);
790 log.info("HTTP response: {}", r.body);
795 protected SSLContext createSSLContext(Parameters p) {
796 try (FileInputStream in = new FileInputStream(p.keyStoreFileName)) {
797 System.setProperty("jsse.enableSNIExtension", "false");
798 System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
799 System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);
801 HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);
803 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
804 KeyStore ks = KeyStore.getInstance("PKCS12");
805 char[] pwd = p.keyStorePassword.toCharArray();
809 SSLContext ctx = SSLContext.getInstance("TLS");
810 ctx.init(kmf.getKeyManagers(), null, null);
812 } catch (Exception e) {
813 log.error("Error creating SSLContext: {}", e.getMessage(), e);
818 protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage,
821 resp.message = errorMessage;
822 String pp = prefix != null ? prefix + '.' : "";
823 ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
824 ctx.setAttribute(pp + "response-message", resp.message);
827 protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
828 String pp = prefix != null ? prefix + '.' : "";
829 ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
830 ctx.setAttribute(pp + "response-message", r.message);
833 public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
834 HttpResponse r = null;
836 FileParam p = getFileParameters(paramMap);
837 byte[] data = Files.readAllBytes(Paths.get(p.fileName));
839 r = sendHttpData(data, p);
840 setResponseStatus(ctx, p.responsePrefix, r);
842 } catch (SvcLogicException | IOException e) {
843 log.error("Error sending the request: {}", e.getMessage(), e);
845 r = new HttpResponse();
847 r.message = e.getMessage();
848 String prefix = parseParam(paramMap, responsePrefix, false, null);
849 setResponseStatus(ctx, prefix, r);
852 if (r != null && r.code >= 300) {
853 throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
857 private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
858 FileParam p = new FileParam();
859 p.fileName = parseParam(paramMap, "fileName", true, null);
860 p.url = parseParam(paramMap, "url", true, null);
861 p.user = parseParam(paramMap, "user", false, null);
862 p.password = parseParam(paramMap, "password", false, null);
863 p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
864 p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
865 String skipSendingStr = paramMap.get(skipSendingMessage);
866 p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
867 p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
868 p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
869 p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
870 p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
871 p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
875 public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
878 UebParam p = getUebParameters(paramMap);
880 String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
884 if (p.templateFileName == null) {
885 log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName);
886 p.templateFileName = defaultUebTemplateFileName;
889 String reqTemplate = readFile(p.templateFileName);
890 reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
891 req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
893 r = postOnUeb(req, p);
894 setResponseStatus(ctx, p.responsePrefix, r);
895 if (r.body != null) {
896 ctx.setAttribute(pp + "httpResponse", r.body);
899 } catch (SvcLogicException e) {
900 log.error("Error sending the request: {}", e.getMessage(), e);
902 r = new HttpResponse();
904 r.message = e.getMessage();
905 String prefix = parseParam(paramMap, responsePrefix, false, null);
906 setResponseStatus(ctx, prefix, r);
910 throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
914 protected HttpResponse sendHttpData(byte[] data, FileParam p) throws SvcLogicException {
916 Client client = ClientBuilder.newBuilder().build();
917 client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
918 client.property(ClientProperties.FOLLOW_REDIRECTS, true);
919 WebTarget webTarget = addAuthType(client, p).target(p.url);
921 log.info("Sending file");
922 long t1 = System.currentTimeMillis();
924 HttpResponse r = new HttpResponse();
927 if (!p.skipSending) {
928 String tt = "application/octet-stream";
929 Invocation.Builder invocationBuilder = webTarget.request(tt).accept(tt);
934 if (p.httpMethod == HttpMethod.POST) {
935 response = invocationBuilder.post(Entity.entity(data, tt));
936 } else if (p.httpMethod == HttpMethod.PUT) {
937 response = invocationBuilder.put(Entity.entity(data, tt));
939 throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
941 } catch (ProcessingException e) {
942 throw new SvcLogicException(requestPostingException +
943 e.getLocalizedMessage(), e);
946 r.code = response.getStatus();
947 r.headers = response.getStringHeaders();
948 EntityTag etag = response.getEntityTag();
950 r.message = etag.getValue();
952 if (response.hasEntity() && r.code != 204) {
953 r.body = response.readEntity(String.class);
957 String newUrl = response.getStringHeaders().getFirst("Location");
959 log.info("Got response code 301. Sending same request to URL: {}", newUrl);
961 webTarget = client.target(newUrl);
962 invocationBuilder = webTarget.request(tt).accept(tt);
965 if (p.httpMethod == HttpMethod.POST) {
966 response = invocationBuilder.post(Entity.entity(data, tt));
967 } else if (p.httpMethod == HttpMethod.PUT) {
968 response = invocationBuilder.put(Entity.entity(data, tt));
970 throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
972 } catch (ProcessingException e) {
973 throw new SvcLogicException(requestPostingException +
974 e.getLocalizedMessage(), e);
977 r.code = response.getStatus();
978 etag = response.getEntityTag();
980 r.message = etag.getValue();
982 if (response.hasEntity() && r.code != 204) {
983 r.body = response.readEntity(String.class);
988 long t2 = System.currentTimeMillis();
989 log.info(responseReceivedMessage, t2 - t1);
990 log.info(responseHttpCodeMessage, r.code);
991 log.info("HTTP response message: {}", r.message);
992 logHeaders(r.headers);
993 log.info("HTTP response: {}", r.body);
998 private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
999 UebParam p = new UebParam();
1000 p.topic = parseParam(paramMap, "topic", true, null);
1001 p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
1002 p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
1003 p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
1004 String skipSendingStr = paramMap.get(skipSendingMessage);
1005 p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
1009 protected void logProperties(Map<String, Object> mm) {
1010 List<String> ll = new ArrayList<>();
1011 for (Object o : mm.keySet()) {
1014 Collections.sort(ll);
1016 log.info("Properties:");
1017 for (String name : ll) {
1018 log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
1022 protected void logHeaders(MultivaluedMap<String, String> mm) {
1023 log.info("HTTP response headers:");
1029 List<String> ll = new ArrayList<>();
1030 for (Object o : mm.keySet()) {
1033 Collections.sort(ll);
1035 for (String name : ll) {
1036 log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
1040 protected HttpResponse postOnUeb(String request, UebParam p) throws SvcLogicException {
1041 String[] urls = uebServers.split(" ");
1042 for (int i = 0; i < urls.length; i++) {
1043 if (!urls[i].endsWith("/")) {
1046 urls[i] += "events/" + p.topic;
1049 Client client = ClientBuilder.newBuilder().build();
1050 client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
1051 WebTarget webTarget = client.target(urls[0]);
1053 log.info("UEB URL: {}", urls[0]);
1054 log.info("Sending request:");
1056 long t1 = System.currentTimeMillis();
1058 HttpResponse r = new HttpResponse();
1061 if (!p.skipSending) {
1062 String tt = "application/json";
1063 String tt1 = tt + ";charset=UTF-8";
1066 Invocation.Builder invocationBuilder = webTarget.request(tt1).accept(tt);
1069 response = invocationBuilder.post(Entity.entity(request, tt1));
1070 } catch (ProcessingException e) {
1071 throw new SvcLogicException(requestPostingException +
1072 e.getLocalizedMessage(), e);
1074 r.code = response.getStatus();
1075 r.headers = response.getStringHeaders();
1076 if (response.hasEntity()) {
1077 r.body = response.readEntity(String.class);
1081 long t2 = System.currentTimeMillis();
1082 log.info(responseReceivedMessage, t2 - t1);
1083 log.info(responseHttpCodeMessage, r.code);
1084 logHeaders(r.headers);
1085 log.info("HTTP response:\n {}", r.body);
1090 public void setUebServers(String uebServers) {
1091 this.uebServers = uebServers;
1094 public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
1095 this.defaultUebTemplateFileName = defaultUebTemplateFileName;
1098 private static class FileParam {
1100 public String fileName;
1103 public String password;
1104 public HttpMethod httpMethod;
1105 public String responsePrefix;
1106 public boolean skipSending;
1107 public String oAuthConsumerKey;
1108 public String oAuthConsumerSecret;
1109 public String oAuthSignatureMethod;
1110 public String oAuthVersion;
1111 public AuthType authtype;
1114 private static class UebParam {
1116 public String topic;
1117 public String templateFileName;
1118 public String rootVarName;
1119 public String responsePrefix;
1120 public boolean skipSending;