2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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.ccsdk.sli.plugins.restapicall;
 
  24 import com.sun.jersey.api.client.Client;
 
  25 import com.sun.jersey.api.client.ClientHandlerException;
 
  26 import com.sun.jersey.api.client.ClientResponse;
 
  27 import com.sun.jersey.api.client.UniformInterfaceException;
 
  28 import com.sun.jersey.api.client.WebResource;
 
  29 import com.sun.jersey.api.client.config.ClientConfig;
 
  30 import com.sun.jersey.api.client.config.DefaultClientConfig;
 
  31 import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
 
  32 import com.sun.jersey.api.client.filter.HTTPDigestAuthFilter;
 
  33 import com.sun.jersey.client.urlconnection.HTTPSProperties;
 
  34 import com.sun.jersey.oauth.client.OAuthClientFilter;
 
  35 import com.sun.jersey.oauth.signature.OAuthParameters;
 
  36 import com.sun.jersey.oauth.signature.OAuthSecrets;
 
  37 import org.apache.commons.lang3.StringUtils;
 
  38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  40 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
 
  41 import org.slf4j.Logger;
 
  42 import org.slf4j.LoggerFactory;
 
  44 import javax.net.ssl.HostnameVerifier;
 
  45 import javax.net.ssl.HttpsURLConnection;
 
  46 import javax.net.ssl.KeyManagerFactory;
 
  47 import javax.net.ssl.SSLContext;
 
  48 import javax.ws.rs.core.EntityTag;
 
  49 import javax.ws.rs.core.MultivaluedMap;
 
  50 import javax.ws.rs.core.UriBuilder;
 
  51 import java.io.FileInputStream;
 
  52 import java.io.IOException;
 
  53 import java.net.SocketException;
 
  55 import java.nio.file.Files;
 
  56 import java.nio.file.Paths;
 
  57 import java.security.KeyStore;
 
  58 import java.util.ArrayList;
 
  59 import java.util.Collections;
 
  60 import java.util.HashMap;
 
  61 import java.util.HashSet;
 
  62 import java.util.List;
 
  64 import java.util.Map.Entry;
 
  65 import java.util.Properties;
 
  68 import static java.lang.Boolean.valueOf;
 
  69 import static org.onap.ccsdk.sli.plugins.restapicall.AuthType.fromString;
 
  71 public class RestapiCallNode implements SvcLogicJavaPlugin {
 
  73     private static final Logger log = LoggerFactory.getLogger(RestapiCallNode.class);
 
  75     private String uebServers;
 
  76     private String defaultUebTemplateFileName = "/opt/bvc/restapi/templates/default-ueb-message.json";
 
  77     protected RetryPolicyStore retryPolicyStore;
 
  78     protected static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";
 
  79     protected static final String UEB_PROPERTIES_FILE_NAME = "ueb.properties";
 
  80     protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
 
  81     protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
 
  83     public RetryPolicyStore getRetryPolicyStore() {
 
  84         return retryPolicyStore;
 
  87     public void setRetryPolicyStore(RetryPolicyStore retryPolicyStore) {
 
  88         this.retryPolicyStore = retryPolicyStore;
 
  91     public RestapiCallNode() {
 
  92         String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
 
  94         try (FileInputStream in = new FileInputStream(configDir + "/" + DME2_PROPERTIES_FILE_NAME)) {
 
  95             Properties props = new Properties();
 
  97             this.retryPolicyStore = new RetryPolicyStore();
 
  98             this.retryPolicyStore.setProxyServers(props.getProperty("proxyUrl"));
 
  99             log.info("DME2 support enabled");
 
 100         } catch (Exception e) {
 
 101             log.warn("DME2 properties could not be read, DME2 support will not be enabled.", e);
 
 104         try (FileInputStream in = new FileInputStream(configDir + "/" + UEB_PROPERTIES_FILE_NAME)) {
 
 105             Properties props = new Properties();
 
 107             this.uebServers = props.getProperty("servers");
 
 108             log.info("UEB support enabled");
 
 109         } catch (Exception e) {
 
 110             log.warn("UEB properties could not be read, UEB support will not be enabled.", e);
 
 115      * Allows Directed Graphs  the ability to interact with REST APIs.
 
 116      * @param paramMap HashMap<String,String> of parameters passed by the DG to this function
 
 118      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th><th>example values</th></thead>
 
 120      *      <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>
 
 121      *      <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>
 
 122      *      <tr><td>restapiUser</td><td>Optional</td><td>user name to use for http basic authentication</td><td>sdnc_ws</td></tr>
 
 123      *      <tr><td>restapiPassword</td><td>Optional</td><td>unencrypted password to use for http basic authentication</td><td>plain_password</td></tr>
 
 124      *      <tr><td>oAuthConsumerKey</td><td>Optional</td><td>Consumer key to use for http oAuth authentication</td><td>plain_key</td></tr>
 
 125      *      <tr><td>oAuthConsumerSecret</td><td>Optional</td><td>Consumer secret to use for http oAuth authentication</td><td>plain_secret</td></tr>
 
 126      *      <tr><td>oAuthSignatureMethod</td><td>Optional</td><td>Consumer method to use for http oAuth authentication</td><td>method</td></tr>
 
 127      *      <tr><td>oAuthVersion</td><td>Optional</td><td>Version http oAuth authentication</td><td>version</td></tr>
 
 128      *      <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>
 
 129      *      <tr><td>format</td><td>Optional</td><td>should match request body format</td><td>json or xml</td></tr>
 
 130      *      <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>
 
 131      *      <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>
 
 132      *      <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>
 
 133      *      <tr><td>skipSending</td><td>Optional</td><td></td><td>true or false</td></tr>
 
 134      *      <tr><td>convertResponse </td><td>Optional</td><td>whether the response should be converted</td><td>true or false</td></tr>
 
 135      *      <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>
 
 136      *      <tr><td>dumpHeaders</td><td>Optional</td><td>when true writes http header content to context memory</td><td>true or false</td></tr>
 
 137      *      <tr><td>partner</td><td>Optional</td><td>needed for DME2 calls</td><td>dme2proxy</td></tr>
 
 138      *      <tr><td>returnRequestPayload</td><td>Optional</td><td>used to return payload built in the request</td><td>true or false</td></tr>
 
 141      * @param ctx Reference to context memory
 
 142      * @throws SvcLogicException
 
 144      * @see String#split(String, int)
 
 146     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
 
 147         sendRequest(paramMap, ctx, null);
 
 150     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx, Integer retryCount)
 
 151             throws SvcLogicException {
 
 153         RetryPolicy retryPolicy = null;
 
 154         HttpResponse r = new HttpResponse();
 
 156             Parameters p = getParameters(paramMap, new Parameters());
 
 157             if (p.partner != null) {
 
 158                 retryPolicy = retryPolicyStore.getRetryPolicy(p.partner);
 
 160             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
 
 163             if (p.templateFileName != null) {
 
 164                 String reqTemplate = readFile(p.templateFileName);
 
 165                 req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
 
 166             } else if (p.requestBody != null) {
 
 169             r = sendHttpRequest(req, p);
 
 170             setResponseStatus(ctx, p.responsePrefix, r);
 
 172             if (p.dumpHeaders && r.headers != null) {
 
 173                 for (Entry<String, List<String>> a : r.headers.entrySet()) {
 
 174                     ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
 
 178             if (p.returnRequestPayload && req != null) {
 
 179                 ctx.setAttribute(pp + "httpRequest", req);
 
 182             if (r.body != null && r.body.trim().length() > 0) {
 
 183                 ctx.setAttribute(pp + "httpResponse", r.body);
 
 185                 if (p.convertResponse) {
 
 186                     Map<String, String> mm = null;
 
 187                     if (p.format == Format.XML)
 
 188                         mm = XmlParser.convertToProperties(r.body, p.listNameList);
 
 189                     else if (p.format == Format.JSON)
 
 190                         mm = JsonParser.convertToProperties(r.body);
 
 193                         for (Map.Entry<String,String> entry : mm.entrySet())
 
 194                             ctx.setAttribute(pp + entry.getKey(), entry.getValue());
 
 197         } catch (SvcLogicException e) {
 
 198             boolean shouldRetry = false;
 
 199             if (e.getCause().getCause() instanceof SocketException) {
 
 203             log.error("Error sending the request: " + e.getMessage(), e);
 
 204             String prefix = parseParam(paramMap, "responsePrefix", false, null);
 
 205             if (retryPolicy == null || shouldRetry == false) {
 
 206                 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
 
 208                 if (retryCount == null) {
 
 211                 String retryMessage = retryCount + " attempts were made out of " + retryPolicy.getMaximumRetries() +
 
 213                 log.debug(retryMessage);
 
 215                     retryCount = retryCount + 1;
 
 216                     if (retryCount < retryPolicy.getMaximumRetries() + 1) {
 
 217                         URI uri = new URI(paramMap.get("restapiUrl"));
 
 218                         String hostname = uri.getHost();
 
 219                         String retryString = retryPolicy.getNextHostName(uri.toString());
 
 220                         URI uriTwo = new URI(retryString);
 
 221                         URI retryUri = UriBuilder.fromUri(uri).host(uriTwo.getHost()).port(uriTwo.getPort()).scheme(
 
 222                                 uriTwo.getScheme()).build();
 
 223                         paramMap.put("restapiUrl", retryUri.toString());
 
 224                         log.debug("URL was set to {}", retryUri.toString());
 
 225                         log.debug("Failed to communicate with host {}. Request will be re-attempted using the host {}.",
 
 226                             hostname, retryString);
 
 227                         log.debug("This is retry attempt {} out of {}", retryCount, retryPolicy.getMaximumRetries());
 
 228                         sendRequest(paramMap, ctx, retryCount);
 
 230                         log.debug("Maximum retries reached, calling setFailureResponseStatus.");
 
 231                         setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
 
 233                 } catch (Exception ex) {
 
 234                     log.error("Could not attempt retry.", ex);
 
 235                     String retryErrorMessage =
 
 236                             "Retry attempt has failed. No further retry shall be attempted, calling " +
 
 237                                 "setFailureResponseStatus.";
 
 238                     setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
 
 243         if (r != null && r.code >= 300)
 
 244             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
 
 248      * Returns parameters from the parameter map.
 
 250      * @param paramMap parameter map
 
 251      * @param p        parameters instance
 
 252      * @return parameters filed instance
 
 253      * @throws SvcLogicException when svc logic exception occurs
 
 255     public static Parameters getParameters(Map<String, String> paramMap,
 
 257             throws SvcLogicException {
 
 258         p.templateFileName = parseParam(paramMap, "templateFileName",
 
 260         p.requestBody = parseParam(paramMap, "requestBody", false, null);
 
 261         p.restapiUrl = parseParam(paramMap, "restapiUrl", true, null);
 
 262         validateUrl(p.restapiUrl);
 
 263         p.restapiUser = parseParam(paramMap, "restapiUser", false, null);
 
 264         p.restapiPassword = parseParam(paramMap, "restapiPassword", false,
 
 266         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey",
 
 268         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret",
 
 270         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod",
 
 272         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
 
 273         p.contentType = parseParam(paramMap, "contentType", false, null);
 
 274         p.format = Format.fromString(parseParam(paramMap, "format", false,
 
 276         p.authtype = fromString(parseParam(paramMap, "authType", false,
 
 278         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod",
 
 280         p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
 
 281         p.listNameList = getListNameList(paramMap);
 
 282         String skipSendingStr = paramMap.get("skipSending");
 
 283         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
 
 284         p.convertResponse = valueOf(parseParam(paramMap, "convertResponse",
 
 286         p.trustStoreFileName = parseParam(paramMap, "trustStoreFileName",
 
 288         p.trustStorePassword = parseParam(paramMap, "trustStorePassword",
 
 290         p.keyStoreFileName = parseParam(paramMap, "keyStoreFileName",
 
 292         p.keyStorePassword = parseParam(paramMap, "keyStorePassword",
 
 294         p.ssl = p.trustStoreFileName != null && p.trustStorePassword != null
 
 295                 && p.keyStoreFileName != null && p.keyStorePassword != null;
 
 296         p.customHttpHeaders = parseParam(paramMap, "customHttpHeaders",
 
 298         p.partner = parseParam(paramMap, "partner", false, null);
 
 299         p.dumpHeaders = valueOf(parseParam(paramMap, "dumpHeaders",
 
 301         p.returnRequestPayload = valueOf(parseParam(
 
 302                 paramMap, "returnRequestPayload", false, null));
 
 307      * Validates the given URL in the parameters.
 
 309      * @param restapiUrl rest api URL
 
 310      * @throws SvcLogicException when URL validation fails
 
 312     private static void validateUrl(String restapiUrl)
 
 313             throws SvcLogicException {
 
 315             URI.create(restapiUrl);
 
 316         } catch (IllegalArgumentException e) {
 
 317             throw new SvcLogicException("Invalid input of url "
 
 318                                                 + e.getLocalizedMessage(), e);
 
 323      * Returns the list of list name.
 
 325      * @param paramMap parameters map
 
 326      * @return list of list name
 
 328     private static Set<String> getListNameList(Map<String, String> paramMap) {
 
 329         Set<String> ll = new HashSet<>();
 
 330         for (Map.Entry<String,String> entry : paramMap.entrySet())
 
 331             if (entry.getKey().startsWith("listName"))
 
 332                 ll.add(entry.getValue());
 
 337      * Parses the parameter string map of property, validates if required,
 
 338      * assigns default value if present and returns the value.
 
 340      * @param paramMap string param map
 
 341      * @param name     name of the property
 
 342      * @param required if value required
 
 343      * @param def      default value
 
 344      * @return value of the property
 
 345      * @throws SvcLogicException if required parameter value is empty
 
 347     public static String parseParam(Map<String, String> paramMap, String name,
 
 348                                     boolean required, String def)
 
 349             throws SvcLogicException {
 
 350         String s = paramMap.get(name);
 
 352         if (s == null || s.trim().length() == 0) {
 
 355             throw new SvcLogicException("Parameter " + name + " is required in RestapiCallNode");
 
 359         StringBuilder value = new StringBuilder();
 
 361         int i1 = s.indexOf('%');
 
 363             int i2 = s.indexOf('%', i1 + 1);
 
 367             String varName = s.substring(i1 + 1, i2);
 
 368             String varValue = System.getenv(varName);
 
 369             if (varValue == null)
 
 370                 varValue = "%" + varName + "%";
 
 372             value.append(s.substring(i, i1));
 
 373             value.append(varValue);
 
 376             i1 = s.indexOf('%', i);
 
 378         value.append(s.substring(i));
 
 380         log.info("Parameter {}: [{}]", name, value);
 
 381         return value.toString();
 
 384     protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format)
 
 385         throws SvcLogicException {
 
 386         log.info("Building {} started", format);
 
 387         long t1 = System.currentTimeMillis();
 
 389         template = expandRepeats(ctx, template, 1);
 
 391         Map<String, String> mm = new HashMap<>();
 
 392         for (String s : ctx.getAttributeKeySet())
 
 393             mm.put(s, ctx.getAttribute(s));
 
 395         StringBuilder ss = new StringBuilder();
 
 397         while (i < template.length()) {
 
 398             int i1 = template.indexOf("${", i);
 
 400                 ss.append(template.substring(i));
 
 404             int i2 = template.indexOf('}', i1 + 2);
 
 406                 throw new SvcLogicException("Template error: Matching } not found");
 
 408             String var1 = template.substring(i1 + 2, i2);
 
 409             String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
 
 410             // log.info(" " + var1 + ": " + value1);
 
 411             if (value1 == null || value1.trim().length() == 0) {
 
 412                 // delete the whole element (line)
 
 413                 int i3 = template.lastIndexOf('\n', i1);
 
 416                 int i4 = template.indexOf('\n', i1);
 
 418                     i4 = template.length();
 
 421                     ss.append(template.substring(i, i3));
 
 424                 ss.append(template.substring(i, i1)).append(value1);
 
 429         String req = format == Format.XML
 
 430                 ? XmlJsonUtil.removeEmptyStructXml(ss.toString()) : XmlJsonUtil.removeEmptyStructJson(ss.toString());
 
 432         if (format == Format.JSON)
 
 433             req = XmlJsonUtil.removeLastCommaJson(req);
 
 435         long t2 = System.currentTimeMillis();
 
 436         log.info("Building {} completed. Time: {}", format, (t2 - t1));
 
 441     protected String expandRepeats(SvcLogicContext ctx, String template, int level) throws SvcLogicException {
 
 442         StringBuilder newTemplate = new StringBuilder();
 
 444         while (k < template.length()) {
 
 445             int i1 = template.indexOf("${repeat:", k);
 
 447                 newTemplate.append(template.substring(k));
 
 451             int i2 = template.indexOf(':', i1 + 9);
 
 453                 throw new SvcLogicException(
 
 454                         "Template error: Context variable name followed by : is required after repeat");
 
 456             // Find the closing }, store in i3
 
 460             while (nn > 0 && i < template.length()) {
 
 461                 i3 = template.indexOf('}', i);
 
 463                     throw new SvcLogicException("Template error: Matching } not found");
 
 464                 int i32 = template.indexOf('{', i);
 
 465                 if (i32 >= 0 && i32 < i3) {
 
 474             String var1 = template.substring(i1 + 9, i2);
 
 475             String value1 = ctx.getAttribute(var1);
 
 476             log.info("     {}:{}", var1, value1);
 
 479                 n = Integer.parseInt(value1);
 
 480             } catch (NumberFormatException e) {
 
 481                 log.info("value1 not set or not a number, n will remain set at zero");
 
 484             newTemplate.append(template.substring(k, i1));
 
 486             String rpt = template.substring(i2 + 1, i3);
 
 488             for (int ii = 0; ii < n; ii++) {
 
 489                 String ss = rpt.replaceAll("\\[\\$\\{" + level + "\\}\\]", "[" + ii + "]");
 
 490                 if (ii == n - 1 && ss.trim().endsWith(",")) {
 
 491                     int i4 = ss.lastIndexOf(',');
 
 493                         ss = ss.substring(0, i4) + ss.substring(i4 + 1);
 
 495                 newTemplate.append(ss);
 
 502             return newTemplate.toString();
 
 504         return expandRepeats(ctx, newTemplate.toString(), level + 1);
 
 507     protected String readFile(String fileName) throws SvcLogicException {
 
 509             byte[] encoded = Files.readAllBytes(Paths.get(fileName));
 
 510             return new String(encoded, "UTF-8");
 
 511         } catch (IOException | SecurityException e) {
 
 512             throw new SvcLogicException("Unable to read file " + fileName + e.getLocalizedMessage(), e);
 
 516     protected Client addAuthType(Client c, FileParam fp) throws SvcLogicException {
 
 517         Parameters p = new Parameters();
 
 518         p.restapiUser = fp.user;
 
 519         p.restapiPassword = fp.password;
 
 520         p.oAuthConsumerKey = fp.oAuthConsumerKey;
 
 521         p.oAuthVersion = fp.oAuthVersion;
 
 522         p.oAuthConsumerSecret = fp.oAuthConsumerSecret;
 
 523         p.oAuthSignatureMethod = fp.oAuthSignatureMethod;
 
 524         p.authtype = fp.authtype;
 
 525         return addAuthType(c,p);
 
 528     protected Client addAuthType(Client client, Parameters p) throws SvcLogicException {
 
 529         if (p.authtype == AuthType.Unspecified){
 
 530             if (p.restapiUser != null && p.restapiPassword != null)
 
 531                 client.addFilter(new HTTPBasicAuthFilter(p.restapiUser, p.restapiPassword));
 
 532             else if(p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null
 
 533                     && p.oAuthSignatureMethod != null) {
 
 534                 OAuthParameters params = new OAuthParameters()
 
 535                         .signatureMethod(p.oAuthSignatureMethod)
 
 536                         .consumerKey(p.oAuthConsumerKey)
 
 537                         .version(p.oAuthVersion);
 
 539                 OAuthSecrets secrets = new OAuthSecrets()
 
 540                         .consumerSecret(p.oAuthConsumerSecret);
 
 541                 client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets));
 
 544             if (p.authtype == AuthType.DIGEST) {
 
 545                 if (p.restapiUser != null && p.restapiPassword != null) {
 
 546                     client.addFilter(new HTTPDigestAuthFilter(p.restapiUser, p.restapiPassword));
 
 548                     throw new SvcLogicException("oAUTH authentication type selected but all restapiUser and restapiPassword " +
 
 549                                                         "parameters doesn't exist", new Throwable());
 
 551             } else if (p.authtype == AuthType.BASIC){
 
 552                 if (p.restapiUser != null && p.restapiPassword != null) {
 
 553                     client.addFilter(new HTTPBasicAuthFilter(p.restapiUser, p.restapiPassword));
 
 555                     throw new SvcLogicException("oAUTH authentication type selected but all restapiUser and restapiPassword " +
 
 556                                                         "parameters doesn't exist", new Throwable());
 
 558             } else if(p.authtype == AuthType.OAUTH ) {
 
 559                 if(p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
 
 560                     OAuthParameters params = new OAuthParameters()
 
 561                             .signatureMethod(p.oAuthSignatureMethod)
 
 562                             .consumerKey(p.oAuthConsumerKey)
 
 563                             .version(p.oAuthVersion);
 
 565                     OAuthSecrets secrets = new OAuthSecrets()
 
 566                             .consumerSecret(p.oAuthConsumerSecret);
 
 567                     client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets));
 
 569                     throw new SvcLogicException("oAUTH authentication type selected but all oAuthConsumerKey, oAuthConsumerSecret " +
 
 570                                                         "and oAuthSignatureMethod parameters doesn't exist", new Throwable());
 
 578      * Receives the http response for the http request sent.
 
 580      * @param request request msg
 
 581      * @param p       parameters
 
 582      * @return HTTP response
 
 583      * @throws SvcLogicException when sending http request fails
 
 585     public HttpResponse sendHttpRequest(String request, Parameters p)
 
 586             throws SvcLogicException {
 
 588         ClientConfig config = new DefaultClientConfig();
 
 589         SSLContext ssl = null;
 
 590         if (p.ssl && p.restapiUrl.startsWith("https"))
 
 591             ssl = createSSLContext(p);
 
 593             HostnameVerifier hostnameVerifier = (hostname, session) -> true;
 
 595             config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
 
 596                     new HTTPSProperties(hostnameVerifier, ssl));
 
 599         logProperties(config.getProperties());
 
 601         Client client = Client.create(config);
 
 602         client.setConnectTimeout(5000);
 
 603         WebResource webResource = addAuthType(client,p).resource(p.restapiUrl);
 
 605         log.info("Sending request:");
 
 607         long t1 = System.currentTimeMillis();
 
 609         HttpResponse r = new HttpResponse();
 
 612         if (!p.skipSending) {
 
 613             String tt = p.format == Format.XML ? "application/xml" : "application/json";
 
 614             String tt1 = tt + ";charset=UTF-8";
 
 615             if (p.contentType != null) {
 
 620             WebResource.Builder webResourceBuilder = webResource.accept(tt).type(tt1);
 
 621             if(p.format == Format.NONE){
 
 622                 webResourceBuilder = webResource.header("","");
 
 625             if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
 
 626                 String[] keyValuePairs = p.customHttpHeaders.split(",");
 
 627                 for (String singlePair : keyValuePairs) {
 
 628                     int equalPosition = singlePair.indexOf('=');
 
 629                     webResourceBuilder.header(singlePair.substring(0, equalPosition),
 
 630                             singlePair.substring(equalPosition + 1, singlePair.length()));
 
 634             webResourceBuilder.header("X-ECOMP-RequestID",org.slf4j.MDC.get("X-ECOMP-RequestID"));
 
 636             ClientResponse response;
 
 639                 response = webResourceBuilder.method(p.httpMethod.toString(), ClientResponse.class, request);
 
 640             } catch (UniformInterfaceException | ClientHandlerException e) {
 
 641                 throw new SvcLogicException("Exception while sending http request to client "
 
 642                     + e.getLocalizedMessage(), e);
 
 645             r.code = response.getStatus();
 
 646             r.headers = response.getHeaders();
 
 647             EntityTag etag = response.getEntityTag();
 
 649                 r.message = etag.getValue();
 
 650             if (response.hasEntity() && r.code != 204)
 
 651                 r.body = response.getEntity(String.class);
 
 654         long t2 = System.currentTimeMillis();
 
 655         log.info("Response received. Time: {}", (t2 - t1));
 
 656         log.info("HTTP response code: {}", r.code);
 
 657         log.info("HTTP response message: {}", r.message);
 
 658         logHeaders(r.headers);
 
 659         log.info("HTTP response: {}", r.body);
 
 664     protected SSLContext createSSLContext(Parameters p) {
 
 665         try (FileInputStream in = new FileInputStream(p.keyStoreFileName)) {
 
 666             System.setProperty("jsse.enableSNIExtension", "false");
 
 667             System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
 
 668             System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);
 
 670             HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);
 
 672             KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
 
 673             KeyStore ks = KeyStore.getInstance("PKCS12");
 
 674             char[] pwd = p.keyStorePassword.toCharArray();
 
 678             SSLContext ctx = SSLContext.getInstance("TLS");
 
 679             ctx.init(kmf.getKeyManagers(), null, null);
 
 681         } catch (Exception e) {
 
 682             log.error("Error creating SSLContext: {}", e.getMessage(), e);
 
 687     protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage,
 
 690         resp.message = errorMessage;
 
 691         String pp = prefix != null ? prefix + '.' : "";
 
 692         ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
 
 693         ctx.setAttribute(pp + "response-message", resp.message);
 
 696     protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
 
 697         String pp = prefix != null ? prefix + '.' : "";
 
 698         ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
 
 699         ctx.setAttribute(pp + "response-message", r.message);
 
 702     public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
 
 703         HttpResponse r = null;
 
 705             FileParam p = getFileParameters(paramMap);
 
 706             byte[] data = Files.readAllBytes(Paths.get(p.fileName));
 
 708             r = sendHttpData(data, p);
 
 709             setResponseStatus(ctx, p.responsePrefix, r);
 
 711         } catch (SvcLogicException | IOException e) {
 
 712             log.error("Error sending the request: {}", e.getMessage(), e);
 
 714             r = new HttpResponse();
 
 716             r.message = e.getMessage();
 
 717             String prefix = parseParam(paramMap, "responsePrefix", false, null);
 
 718             setResponseStatus(ctx, prefix, r);
 
 721         if (r != null && r.code >= 300)
 
 722             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
 
 725     private static class FileParam {
 
 727         public String fileName;
 
 730         public String password;
 
 731         public HttpMethod httpMethod;
 
 732         public String responsePrefix;
 
 733         public boolean skipSending;
 
 734         public String oAuthConsumerKey;
 
 735         public String oAuthConsumerSecret;
 
 736         public String oAuthSignatureMethod;
 
 737         public String oAuthVersion;
 
 738         public AuthType authtype;
 
 741     private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
 
 742         FileParam p = new FileParam();
 
 743         p.fileName = parseParam(paramMap, "fileName", true, null);
 
 744         p.url = parseParam(paramMap, "url", true, null);
 
 745         p.user = parseParam(paramMap, "user", false, null);
 
 746         p.password = parseParam(paramMap, "password", false, null);
 
 747         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
 
 748         p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
 
 749         String skipSendingStr = paramMap.get("skipSending");
 
 750         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
 
 751         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
 
 752         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
 
 753         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
 
 754         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
 
 755         p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
 
 759     protected HttpResponse sendHttpData(byte[] data, FileParam p) throws SvcLogicException {
 
 760         Client client = Client.create();
 
 761         client.setConnectTimeout(5000);
 
 762         client.setFollowRedirects(true);
 
 763         WebResource webResource = addAuthType(client,p).resource(p.url);
 
 765         log.info("Sending file");
 
 766         long t1 = System.currentTimeMillis();
 
 768         HttpResponse r = new HttpResponse();
 
 771         if (!p.skipSending) {
 
 772             String tt = "application/octet-stream";
 
 774             ClientResponse response;
 
 776                 if (p.httpMethod == HttpMethod.POST)
 
 777                     response = webResource.accept(tt).type(tt).post(ClientResponse.class, data);
 
 778                 else if (p.httpMethod == HttpMethod.PUT)
 
 779                     response = webResource.accept(tt).type(tt).put(ClientResponse.class, data);
 
 781                     throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
 
 782             } catch (UniformInterfaceException | ClientHandlerException e) {
 
 783                 throw new SvcLogicException("Exception while sending http request to client " +
 
 784                     e.getLocalizedMessage(), e);
 
 787             r.code = response.getStatus();
 
 788             r.headers = response.getHeaders();
 
 789             EntityTag etag = response.getEntityTag();
 
 791                 r.message = etag.getValue();
 
 792             if (response.hasEntity() && r.code != 204)
 
 793                 r.body = response.getEntity(String.class);
 
 796                 String newUrl = response.getHeaders().getFirst("Location");
 
 798                 log.info("Got response code 301. Sending same request to URL: {}", newUrl);
 
 800                 webResource = client.resource(newUrl);
 
 803                     if (p.httpMethod == HttpMethod.POST)
 
 804                         response = webResource.accept(tt).type(tt).post(ClientResponse.class, data);
 
 805                     else if (p.httpMethod == HttpMethod.PUT)
 
 806                         response = webResource.accept(tt).type(tt).put(ClientResponse.class, data);
 
 808                         throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
 
 809                 } catch (UniformInterfaceException | ClientHandlerException e) {
 
 810                     throw new SvcLogicException("Exception while sending http request to client " +
 
 811                         e.getLocalizedMessage(), e);
 
 814                 r.code = response.getStatus();
 
 815                 etag = response.getEntityTag();
 
 817                     r.message = etag.getValue();
 
 818                 if (response.hasEntity() && r.code != 204)
 
 819                     r.body = response.getEntity(String.class);
 
 823         long t2 = System.currentTimeMillis();
 
 824         log.info("Response received. Time: {}", (t2 - t1));
 
 825         log.info("HTTP response code: {}", r.code);
 
 826         log.info("HTTP response message: {}", r.message);
 
 827         logHeaders(r.headers);
 
 828         log.info("HTTP response: {}", r.body);
 
 833     public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
 
 836             UebParam p = getUebParameters(paramMap);
 
 838             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
 
 842             if (p.templateFileName == null) {
 
 843                 log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName);
 
 844                 p.templateFileName = defaultUebTemplateFileName;
 
 847             String reqTemplate = readFile(p.templateFileName);
 
 848             reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
 
 849             req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
 
 851             r = postOnUeb(req, p);
 
 852             setResponseStatus(ctx, p.responsePrefix, r);
 
 854                 ctx.setAttribute(pp + "httpResponse", r.body);
 
 856         } catch (SvcLogicException e) {
 
 857             log.error("Error sending the request: {}", e.getMessage(), e);
 
 859             r = new HttpResponse();
 
 861             r.message = e.getMessage();
 
 862             String prefix = parseParam(paramMap, "responsePrefix", false, null);
 
 863             setResponseStatus(ctx, prefix, r);
 
 867             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
 
 870     private static class UebParam {
 
 873         public String templateFileName;
 
 874         public String rootVarName;
 
 875         public String responsePrefix;
 
 876         public boolean skipSending;
 
 879     private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
 
 880         UebParam p = new UebParam();
 
 881         p.topic = parseParam(paramMap, "topic", true, null);
 
 882         p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
 
 883         p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
 
 884         p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
 
 885         String skipSendingStr = paramMap.get("skipSending");
 
 886         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
 
 890     protected HttpResponse postOnUeb(String request, UebParam p) throws SvcLogicException {
 
 891         String[] urls = uebServers.split(" ");
 
 892         for (int i = 0; i < urls.length; i++) {
 
 893             if (!urls[i].endsWith("/"))
 
 895             urls[i] += "events/" + p.topic;
 
 898         Client client = Client.create();
 
 899         client.setConnectTimeout(5000);
 
 900         WebResource webResource = client.resource(urls[0]);
 
 902         log.info("UEB URL: {}", urls[0]);
 
 903         log.info("Sending request:");
 
 905         long t1 = System.currentTimeMillis();
 
 907         HttpResponse r = new HttpResponse();
 
 910         if (!p.skipSending) {
 
 911             String tt = "application/json";
 
 912             String tt1 = tt + ";charset=UTF-8";
 
 914             ClientResponse response;
 
 917                 response = webResource.accept(tt).type(tt1).post(ClientResponse.class, request);
 
 918             } catch (UniformInterfaceException | ClientHandlerException e) {
 
 919                 throw new SvcLogicException("Exception while posting http request to client " +
 
 920                     e.getLocalizedMessage(), e);
 
 923             r.code = response.getStatus();
 
 924             r.headers = response.getHeaders();
 
 925             if (response.hasEntity())
 
 926                 r.body = response.getEntity(String.class);
 
 929         long t2 = System.currentTimeMillis();
 
 930         log.info("Response received. Time: {}", (t2 - t1));
 
 931         log.info("HTTP response code: {}", r.code);
 
 932         logHeaders(r.headers);
 
 933         log.info("HTTP response:\n {}", r.body);
 
 938     protected void logProperties(Map<String, Object> mm) {
 
 939         List<String> ll = new ArrayList<>();
 
 940         for (Object o : mm.keySet())
 
 942         Collections.sort(ll);
 
 944         log.info("Properties:");
 
 945         for (String name : ll)
 
 946             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
 
 949     protected void logHeaders(MultivaluedMap<String, String> mm) {
 
 950         log.info("HTTP response headers:");
 
 955         List<String> ll = new ArrayList<>();
 
 956         for (Object o : mm.keySet())
 
 958         Collections.sort(ll);
 
 960         for (String name : ll)
 
 961             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
 
 964     public void setUebServers(String uebServers) {
 
 965         this.uebServers = uebServers;
 
 968     public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
 
 969         this.defaultUebTemplateFileName = defaultUebTemplateFileName;