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 org.apache.commons.lang3.StringUtils;
 
  61 import org.codehaus.jettison.json.JSONException;
 
  62 import org.codehaus.jettison.json.JSONObject;
 
  63 import org.glassfish.jersey.client.ClientProperties;
 
  64 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
 
  65 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
 
  66 import org.glassfish.jersey.client.oauth1.ConsumerCredentials;
 
  67 import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
 
  68 import org.glassfish.jersey.media.multipart.MultiPart;
 
  69 import org.glassfish.jersey.media.multipart.MultiPartFeature;
 
  70 import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
 
  71 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  72 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  73 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
 
  74 import org.slf4j.Logger;
 
  75 import org.slf4j.LoggerFactory;
 
  77 public class RestapiCallNode implements SvcLogicJavaPlugin {
 
  79     protected static final String PARTNERS_FILE_NAME = "partners.json";
 
  80     protected static final String UEB_PROPERTIES_FILE_NAME = "ueb.properties";
 
  81     protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
 
  82     protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
 
  84     private static final Logger log = LoggerFactory.getLogger(RestapiCallNode.class);
 
  85     private String uebServers;
 
  86     private String defaultUebTemplateFileName = "/opt/bvc/restapi/templates/default-ueb-message.json";
 
  88     private String responseReceivedMessage = "Response received. Time: {}";
 
  89     private String responseHttpCodeMessage = "HTTP response code: {}";
 
  90     private String requestPostingException = "Exception while posting http request to client ";
 
  91     protected static final String skipSendingMessage = "skipSending";
 
  92     protected static final String responsePrefix = "responsePrefix";
 
  93     protected static final String restapiUrlString = "restapiUrl";
 
  94     protected static final String restapiUserKey = "restapiUser";
 
  95     protected static final String restapiPasswordKey = "restapiPassword";
 
  97     protected HashMap<String, PartnerDetails> partnerStore;
 
  99     public RestapiCallNode() {
 
 100         String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
 
 102             String jsonString = readFile(configDir + "/" + PARTNERS_FILE_NAME);
 
 103             JSONObject partners = new JSONObject(jsonString);
 
 104             partnerStore = new HashMap<>();
 
 105             loadPartners(partners);
 
 106             log.info("Partners support enabled");
 
 107         } catch (Exception e) {
 
 108             log.warn("Partners file could not be read, Partner support will not be enabled.", e);
 
 111         try (FileInputStream in = new FileInputStream(configDir + "/" + UEB_PROPERTIES_FILE_NAME)) {
 
 112             Properties props = new Properties();
 
 114             uebServers = props.getProperty("servers");
 
 115             log.info("UEB support enabled");
 
 116         } catch (Exception e) {
 
 117             log.warn("UEB properties could not be read, UEB support will not be enabled.", e);
 
 121     protected void loadPartners(JSONObject partners) {
 
 122         Iterator<String> keys = partners.keys();
 
 123         String partnerUserKey = "user";
 
 124         String partnerPasswordKey = "password";
 
 125         String partnerUrlKey = "url";
 
 127         while (keys.hasNext()) {
 
 128             String partnerKey = keys.next();
 
 130                 JSONObject partnerObject = (JSONObject) partners.get(partnerKey);
 
 131                 if (partnerObject.has(partnerUserKey) && partnerObject.has(partnerPasswordKey)) {
 
 133                     if (partnerObject.has(partnerUrlKey)) {
 
 134                         url = partnerObject.getString(partnerUrlKey);
 
 136                     String userName = partnerObject.getString(partnerUserKey);
 
 137                     String password = partnerObject.getString(partnerPasswordKey);
 
 138                     PartnerDetails details = new PartnerDetails(userName, password, url);
 
 139                     partnerStore.put(partnerKey, details);
 
 140                     log.info("mapped partner using partner key " + partnerKey);
 
 142                     log.info("Partner " + partnerKey + " is missing required keys, it won't be mapped");
 
 144             } catch (JSONException e) {
 
 145                 log.info("Couldn't map the partner using partner key " + partnerKey, e);
 
 151      * Returns parameters from the parameter map.
 
 153      * @param paramMap parameter map
 
 154      * @param p parameters instance
 
 155      * @return parameters filed instance
 
 156      * @throws SvcLogicException when svc logic exception occurs
 
 158     public static Parameters getParameters(Map<String, String> paramMap, Parameters p) throws SvcLogicException {
 
 160         p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
 
 161         p.requestBody = parseParam(paramMap, "requestBody", false, null);
 
 162         p.restapiUrl = parseParam(paramMap, restapiUrlString, true, null);
 
 163         validateUrl(p.restapiUrl);
 
 164         p.restapiUrlSuffix = parseParam(paramMap, "restapiUrlSuffix", false, null);
 
 165         p.restapiUser = parseParam(paramMap, restapiUserKey, false, null);
 
 166         p.restapiPassword = parseParam(paramMap, restapiPasswordKey, false, null);
 
 167         if (p.restapiUrlSuffix != null) {
 
 168             p.restapiUrl = p.restapiUrl + p.restapiUrlSuffix;
 
 169             validateUrl(p.restapiUrl);
 
 171         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
 
 172         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
 
 173         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
 
 174         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
 
 175         p.contentType = parseParam(paramMap, "contentType", false, null);
 
 176         p.format = Format.fromString(parseParam(paramMap, "format", false, "json"));
 
 177         p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
 
 178         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
 
 179         p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
 
 180         p.listNameList = getListNameList(paramMap);
 
 181         String skipSendingStr = paramMap.get(skipSendingMessage);
 
 182         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
 
 183         p.convertResponse = valueOf(parseParam(paramMap, "convertResponse", false, "true"));
 
 184         p.trustStoreFileName = parseParam(paramMap, "trustStoreFileName", false, null);
 
 185         p.trustStorePassword = parseParam(paramMap, "trustStorePassword", false, null);
 
 186         p.keyStoreFileName = parseParam(paramMap, "keyStoreFileName", false, null);
 
 187         p.keyStorePassword = parseParam(paramMap, "keyStorePassword", false, null);
 
 188         p.ssl = p.trustStoreFileName != null && p.trustStorePassword != null && p.keyStoreFileName != null
 
 189                 && p.keyStorePassword != null;
 
 190         p.customHttpHeaders = parseParam(paramMap, "customHttpHeaders", false, null);
 
 191         p.partner = parseParam(paramMap, "partner", false, null);
 
 192         p.dumpHeaders = valueOf(parseParam(paramMap, "dumpHeaders", false, null));
 
 193         p.returnRequestPayload = valueOf(parseParam(paramMap, "returnRequestPayload", false, null));
 
 194         p.accept = parseParam(paramMap, "accept", false, null);
 
 195         p.multipartFormData = valueOf(parseParam(paramMap, "multipartFormData", false, "false"));
 
 196         p.multipartFile = parseParam(paramMap, "multipartFile", false, null);
 
 201      * Validates the given URL in the parameters.
 
 203      * @param restapiUrl rest api URL
 
 204      * @throws SvcLogicException when URL validation fails
 
 206     private static void validateUrl(String restapiUrl) throws SvcLogicException {
 
 207         if (restapiUrl.contains(",")) {
 
 208             String[] urls = restapiUrl.split(",");
 
 209             for (String url : urls) {
 
 214                 URI.create(restapiUrl);
 
 215             } catch (IllegalArgumentException e) {
 
 216                 throw new SvcLogicException("Invalid input of url " + e.getLocalizedMessage(), e);
 
 222      * Returns the list of list name.
 
 224      * @param paramMap parameters map
 
 225      * @return list of list name
 
 227     private static Set<String> getListNameList(Map<String, String> paramMap) {
 
 228         Set<String> ll = new HashSet<>();
 
 229         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
 
 230             if (entry.getKey().startsWith("listName")) {
 
 231                 ll.add(entry.getValue());
 
 238      * Parses the parameter string map of property, validates if required, assigns default value if
 
 239      * present and returns the value.
 
 241      * @param paramMap string param map
 
 242      * @param name name of the property
 
 243      * @param required if value required
 
 244      * @param def default value
 
 245      * @return value of the property
 
 246      * @throws SvcLogicException if required parameter value is empty
 
 248     public static String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
 
 249             throws SvcLogicException {
 
 250         String s = paramMap.get(name);
 
 252         if (s == null || s.trim().length() == 0) {
 
 256             throw new SvcLogicException("Parameter " + name + " is required in RestapiCallNode");
 
 260         StringBuilder value = new StringBuilder();
 
 262         int i1 = s.indexOf('%');
 
 264             int i2 = s.indexOf('%', i1 + 1);
 
 269             String varName = s.substring(i1 + 1, i2);
 
 270             String varValue = System.getenv(varName);
 
 271             if (varValue == null) {
 
 272                 varValue = "%" + varName + "%";
 
 275             value.append(s.substring(i, i1));
 
 276             value.append(varValue);
 
 279             i1 = s.indexOf('%', i);
 
 281         value.append(s.substring(i));
 
 283         log.info("Parameter {}: [{}]", name, maskPassword(name, value));
 
 285         return value.toString();
 
 288     private static Object maskPassword(String name, Object value) {
 
 289         String[] pwdNames = {"pwd", "passwd", "password", "Pwd", "Passwd", "Password"};
 
 290         for (String pwdName : pwdNames) {
 
 291             if (name.contains(pwdName)) {
 
 299      * Allows Directed Graphs the ability to interact with REST APIs.
 
 301      * @param paramMap HashMap<String,String> of parameters passed by the DG to this function
 
 305      *        <th>Mandatory/Optional</th>
 
 306      *        <th>description</th>
 
 307      *        <th>example values</th></thead> <tbody>
 
 309      *        <td>templateFileName</td>
 
 311      *        <td>full path to template file that can be used to build a request</td>
 
 312      *        <td>/sdncopt/bvc/restapi/templates/vnf_service-configuration-operation_minimal.json</td>
 
 315      *        <td>restapiUrl</td>
 
 317      *        <td>url to send the request to</td>
 
 318      *        <td>https://sdncodl:8543/restconf/operations/L3VNF-API:create-update-vnf-request</td>
 
 321      *        <td>restapiUser</td>
 
 323      *        <td>user name to use for http basic authentication</td>
 
 327      *        <td>restapiPassword</td>
 
 329      *        <td>unencrypted password to use for http basic authentication</td>
 
 330      *        <td>plain_password</td>
 
 333      *        <td>oAuthConsumerKey</td>
 
 335      *        <td>Consumer key to use for http oAuth authentication</td>
 
 339      *        <td>oAuthConsumerSecret</td>
 
 341      *        <td>Consumer secret to use for http oAuth authentication</td>
 
 342      *        <td>plain_secret</td>
 
 345      *        <td>oAuthSignatureMethod</td>
 
 347      *        <td>Consumer method to use for http oAuth authentication</td>
 
 351      *        <td>oAuthVersion</td>
 
 353      *        <td>Version http oAuth authentication</td>
 
 357      *        <td>contentType</td>
 
 359      *        <td>http content type to set in the http header</td>
 
 360      *        <td>usually application/json or application/xml</td>
 
 365      *        <td>should match request body format</td>
 
 366      *        <td>json or xml</td>
 
 369      *        <td>httpMethod</td>
 
 371      *        <td>http method to use when sending the request</td>
 
 372      *        <td>get post put delete patch</td>
 
 375      *        <td>responsePrefix</td>
 
 377      *        <td>location the response will be written to in context memory</td>
 
 378      *        <td>tmp.restapi.result</td>
 
 381      *        <td>listName[i]</td>
 
 383      *        <td>Used for processing XML responses with repeating
 
 384      *        elements.</td>vpn-information.vrf-details
 
 388      *        <td>skipSending</td>
 
 391      *        <td>true or false</td>
 
 394      *        <td>convertResponse</td>
 
 396      *        <td>whether the response should be converted</td>
 
 397      *        <td>true or false</td>
 
 400      *        <td>customHttpHeaders</td>
 
 402      *        <td>a list additional http headers to be passed in, follow the format in the example</td>
 
 403      *        <td>X-CSI-MessageId=messageId,headerFieldName=headerFieldValue</td>
 
 406      *        <td>dumpHeaders</td>
 
 408      *        <td>when true writes http header content to context memory</td>
 
 409      *        <td>true or false</td>
 
 414      *        <td>used to retrieve username, password and url if partner store exists</td>
 
 418      *        <td>returnRequestPayload</td>
 
 420      *        <td>used to return payload built in the request</td>
 
 421      *        <td>true or false</td>
 
 425      * @param ctx Reference to context memory
 
 426      * @throws SvcLogicException
 
 428      * @see String#split(String, int)
 
 430     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
 
 431         sendRequest(paramMap, ctx, null);
 
 434     protected void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx, RetryPolicy retryPolicy)
 
 435             throws SvcLogicException {
 
 437         HttpResponse r = new HttpResponse();
 
 439             handlePartner(paramMap);
 
 440             Parameters p = getParameters(paramMap, new Parameters());
 
 441             if (p.restapiUrl.contains(",") && retryPolicy == null) {
 
 442                 String[] urls = p.restapiUrl.split(",");
 
 443                 retryPolicy = new RetryPolicy(urls, urls.length * 2);
 
 444                 p.restapiUrl = urls[0];
 
 446             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
 
 449             if (p.templateFileName != null) {
 
 450                 String reqTemplate = readFile(p.templateFileName);
 
 451                 req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
 
 452             } else if (p.requestBody != null) {
 
 455             r = sendHttpRequest(req, p);
 
 456             setResponseStatus(ctx, p.responsePrefix, r);
 
 458             if (p.dumpHeaders && r.headers != null) {
 
 459                 for (Entry<String, List<String>> a : r.headers.entrySet()) {
 
 460                     ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
 
 464             if (p.returnRequestPayload && req != null) {
 
 465                 ctx.setAttribute(pp + "httpRequest", req);
 
 468             if (r.body != null && r.body.trim().length() > 0) {
 
 469                 ctx.setAttribute(pp + "httpResponse", r.body);
 
 471                 if (p.convertResponse) {
 
 472                     Map<String, String> mm = null;
 
 473                     if (p.format == Format.XML) {
 
 474                         mm = XmlParser.convertToProperties(r.body, p.listNameList);
 
 475                     } else if (p.format == Format.JSON) {
 
 476                         mm = JsonParser.convertToProperties(r.body);
 
 480                         for (Map.Entry<String, String> entry : mm.entrySet()) {
 
 481                             ctx.setAttribute(pp + entry.getKey(), entry.getValue());
 
 486         } catch (SvcLogicException e) {
 
 487             boolean shouldRetry = false;
 
 488             if (e.getCause().getCause() instanceof SocketException) {
 
 492             log.error("Error sending the request: " + e.getMessage(), e);
 
 493             String prefix = parseParam(paramMap, responsePrefix, false, null);
 
 494             if (retryPolicy == null || !shouldRetry) {
 
 495                 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
 
 497                 log.debug(retryPolicy.getRetryMessage());
 
 499                     // calling getNextHostName increments the retry count so it should be called before shouldRetry
 
 500                     String retryString = retryPolicy.getNextHostName();
 
 501                     if (retryPolicy.shouldRetry()) {
 
 502                         paramMap.put(restapiUrlString, retryString);
 
 503                         log.debug("retry attempt {} will use the retry url {}", retryPolicy.getRetryCount(),
 
 505                         sendRequest(paramMap, ctx, retryPolicy);
 
 507                         log.debug("Maximum retries reached, won't attempt to retry. Calling setFailureResponseStatus.");
 
 508                         setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
 
 510                 } catch (Exception ex) {
 
 511                     String retryErrorMessage = "Retry attempt " + retryPolicy.getRetryCount()
 
 512                             + "has failed with error message " + ex.getMessage();
 
 513                     setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
 
 518         if (r != null && r.code >= 300) {
 
 519             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
 
 523     protected void handlePartner(Map<String, String> paramMap) {
 
 524         String partner = paramMap.get("partner");
 
 525         if (partner != null && partner.length() > 0) {
 
 526             PartnerDetails details = partnerStore.get(partner);
 
 527             paramMap.put(restapiUserKey, details.username);
 
 528             paramMap.put(restapiPasswordKey, details.password);
 
 529             if (paramMap.get(restapiUrlString) == null) {
 
 530                 paramMap.put(restapiUrlString, details.url);
 
 535     protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format) throws SvcLogicException {
 
 536         log.info("Building {} started", format);
 
 537         long t1 = System.currentTimeMillis();
 
 538         String originalTemplate = template;
 
 540         template = expandRepeats(ctx, template, 1);
 
 542         Map<String, String> mm = new HashMap<>();
 
 543         for (String s : ctx.getAttributeKeySet()) {
 
 544             mm.put(s, ctx.getAttribute(s));
 
 547         StringBuilder ss = new StringBuilder();
 
 549         while (i < template.length()) {
 
 550             int i1 = template.indexOf("${", i);
 
 552                 ss.append(template.substring(i));
 
 556             int i2 = template.indexOf('}', i1 + 2);
 
 558                 throw new SvcLogicException("Template error: Matching } not found");
 
 561             String var1 = template.substring(i1 + 2, i2);
 
 562             String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
 
 563             if (value1 == null || value1.trim().length() == 0) {
 
 564                 // delete the whole element (line)
 
 565                 int i3 = template.lastIndexOf('\n', i1);
 
 569                 int i4 = template.indexOf('\n', i1);
 
 571                     i4 = template.length();
 
 575                     ss.append(template.substring(i, i3));
 
 579                 ss.append(template.substring(i, i1)).append(value1);
 
 584         String req = format == Format.XML ? XmlJsonUtil.removeEmptyStructXml(ss.toString())
 
 585                 : XmlJsonUtil.removeEmptyStructJson(originalTemplate, ss.toString());
 
 587         if (format == Format.JSON) {
 
 588             req = XmlJsonUtil.removeLastCommaJson(req);
 
 591         long t2 = System.currentTimeMillis();
 
 592         log.info("Building {} completed. Time: {}", format, t2 - t1);
 
 597     protected String expandRepeats(SvcLogicContext ctx, String template, int level) throws SvcLogicException {
 
 598         StringBuilder newTemplate = new StringBuilder();
 
 600         while (k < template.length()) {
 
 601             int i1 = template.indexOf("${repeat:", k);
 
 603                 newTemplate.append(template.substring(k));
 
 607             int i2 = template.indexOf(':', i1 + 9);
 
 609                 throw new SvcLogicException(
 
 610                         "Template error: Context variable name followed by : is required after repeat");
 
 613             // Find the closing }, store in i3
 
 617             while (nn > 0 && i < template.length()) {
 
 618                 i3 = template.indexOf('}', i);
 
 620                     throw new SvcLogicException("Template error: Matching } not found");
 
 622                 int i32 = template.indexOf('{', i);
 
 623                 if (i32 >= 0 && i32 < i3) {
 
 632             String var1 = template.substring(i1 + 9, i2);
 
 633             String value1 = ctx.getAttribute(var1);
 
 634             log.info("     {}:{}", var1, value1);
 
 637                 n = Integer.parseInt(value1);
 
 638             } catch (NumberFormatException e) {
 
 639                 log.info("value1 not set or not a number, n will remain set at zero");
 
 642             newTemplate.append(template.substring(k, i1));
 
 644             String rpt = template.substring(i2 + 1, i3);
 
 646             for (int ii = 0; ii < n; ii++) {
 
 647                 String ss = rpt.replaceAll("\\[\\$\\{" + level + "\\}\\]", "[" + ii + "]");
 
 648                 if (ii == n - 1 && ss.trim().endsWith(",")) {
 
 649                     int i4 = ss.lastIndexOf(',');
 
 651                         ss = ss.substring(0, i4) + ss.substring(i4 + 1);
 
 654                 newTemplate.append(ss);
 
 661             return newTemplate.toString();
 
 664         return expandRepeats(ctx, newTemplate.toString(), level + 1);
 
 667     protected String readFile(String fileName) throws SvcLogicException {
 
 669             byte[] encoded = Files.readAllBytes(Paths.get(fileName));
 
 670             return new String(encoded, "UTF-8");
 
 671         } catch (IOException | SecurityException e) {
 
 672             throw new SvcLogicException("Unable to read file " + fileName + e.getLocalizedMessage(), e);
 
 676     protected Client addAuthType(Client c, FileParam fp) throws SvcLogicException {
 
 677         Parameters p = new Parameters();
 
 678         p.restapiUser = fp.user;
 
 679         p.restapiPassword = fp.password;
 
 680         p.oAuthConsumerKey = fp.oAuthConsumerKey;
 
 681         p.oAuthVersion = fp.oAuthVersion;
 
 682         p.oAuthConsumerSecret = fp.oAuthConsumerSecret;
 
 683         p.oAuthSignatureMethod = fp.oAuthSignatureMethod;
 
 684         p.authtype = fp.authtype;
 
 685         return addAuthType(c, p);
 
 688     public Client addAuthType(Client client, Parameters p) throws SvcLogicException {
 
 689         if (p.authtype == AuthType.Unspecified) {
 
 690             if (p.restapiUser != null && p.restapiPassword != null) {
 
 691                 client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
 
 692             } else if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
 
 693                 Feature oAuth1Feature =
 
 694                         OAuth1ClientSupport.builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
 
 695                                 .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
 
 696                 client.register(oAuth1Feature);
 
 700             if (p.authtype == AuthType.DIGEST) {
 
 701                 if (p.restapiUser != null && p.restapiPassword != null) {
 
 702                     client.register(HttpAuthenticationFeature.digest(p.restapiUser, p.restapiPassword));
 
 704                     throw new SvcLogicException(
 
 705                             "oAUTH authentication type selected but all restapiUser and restapiPassword "
 
 706                                     + "parameters doesn't exist",
 
 709             } else if (p.authtype == AuthType.BASIC) {
 
 710                 if (p.restapiUser != null && p.restapiPassword != null) {
 
 711                     client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
 
 713                     throw new SvcLogicException(
 
 714                             "oAUTH authentication type selected but all restapiUser and restapiPassword "
 
 715                                     + "parameters doesn't exist",
 
 718             } else if (p.authtype == AuthType.OAUTH) {
 
 719                 if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
 
 720                     Feature oAuth1Feature = OAuth1ClientSupport
 
 721                             .builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
 
 722                             .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
 
 723                     client.register(oAuth1Feature);
 
 725                     throw new SvcLogicException(
 
 726                             "oAUTH authentication type selected but all oAuthConsumerKey, oAuthConsumerSecret "
 
 727                                     + "and oAuthSignatureMethod parameters doesn't exist",
 
 736      * Receives the http response for the http request sent.
 
 738      * @param request request msg
 
 739      * @param p parameters
 
 740      * @return HTTP response
 
 741      * @throws SvcLogicException when sending http request fails
 
 743     public HttpResponse sendHttpRequest(String request, Parameters p) throws SvcLogicException {
 
 745         SSLContext ssl = null;
 
 746         if (p.ssl && p.restapiUrl.startsWith("https")) {
 
 747             ssl = createSSLContext(p);
 
 752             HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());
 
 753             client = ClientBuilder.newBuilder().sslContext(ssl).hostnameVerifier((s, sslSession) -> true).build();
 
 755             client = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true).build();
 
 757         client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
 
 758         // Needed to support additional HTTP methods such as PATCH
 
 759         client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
 
 761         WebTarget webTarget = addAuthType(client, p).target(p.restapiUrl);
 
 763         log.info("Sending request below to url " + p.restapiUrl);
 
 765         long t1 = System.currentTimeMillis();
 
 767         HttpResponse r = new HttpResponse();
 
 769         String accept = p.accept;
 
 770         if (accept == null) {
 
 771             accept = p.format == Format.XML ? "application/xml" : "application/json";
 
 774         String contentType = p.contentType;
 
 775         if (contentType == null) {
 
 776             contentType = accept + ";charset=UTF-8";
 
 779         if (!p.skipSending && !p.multipartFormData) {
 
 781             Invocation.Builder invocationBuilder = webTarget.request(contentType).accept(accept);
 
 783             if (p.format == Format.NONE) {
 
 784                 invocationBuilder.header("", "");
 
 787             if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
 
 788                 String[] keyValuePairs = p.customHttpHeaders.split(",");
 
 789                 for (String singlePair : keyValuePairs) {
 
 790                     int equalPosition = singlePair.indexOf('=');
 
 791                     invocationBuilder.header(singlePair.substring(0, equalPosition),
 
 792                             singlePair.substring(equalPosition + 1, singlePair.length()));
 
 796             invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
 
 798             invocationBuilder.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
 
 803                 response = invocationBuilder.method(p.httpMethod.toString(), entity(request, contentType));
 
 804             } catch (ProcessingException | IllegalStateException e) {
 
 805                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
 
 808             r.code = response.getStatus();
 
 809             r.headers = response.getStringHeaders();
 
 810             EntityTag etag = response.getEntityTag();
 
 812                 r.message = etag.getValue();
 
 814             if (response.hasEntity() && r.code != 204) {
 
 815                 r.body = response.readEntity(String.class);
 
 817         } else if (!p.skipSending && p.multipartFormData) {
 
 819             WebTarget wt = client.register(MultiPartFeature.class).target(p.restapiUrl);
 
 821             MultiPart multiPart = new MultiPart();
 
 822             multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
 
 824             FileDataBodyPart fileDataBodyPart =
 
 825                     new FileDataBodyPart("file", new File(p.multipartFile), MediaType.APPLICATION_OCTET_STREAM_TYPE);
 
 826             multiPart.bodyPart(fileDataBodyPart);
 
 829             Invocation.Builder invocationBuilder = wt.request(contentType).accept(accept);
 
 831             if (p.format == Format.NONE) {
 
 832                 invocationBuilder.header("", "");
 
 835             if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
 
 836                 String[] keyValuePairs = p.customHttpHeaders.split(",");
 
 837                 for (String singlePair : keyValuePairs) {
 
 838                     int equalPosition = singlePair.indexOf('=');
 
 839                     invocationBuilder.header(singlePair.substring(0, equalPosition),
 
 840                             singlePair.substring(equalPosition + 1, singlePair.length()));
 
 844             invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
 
 850                         invocationBuilder.method(p.httpMethod.toString(), entity(multiPart, multiPart.getMediaType()));
 
 851             } catch (ProcessingException | IllegalStateException e) {
 
 852                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
 
 855             r.code = response.getStatus();
 
 856             r.headers = response.getStringHeaders();
 
 857             EntityTag etag = response.getEntityTag();
 
 859                 r.message = etag.getValue();
 
 861             if (response.hasEntity() && r.code != 204) {
 
 862                 r.body = response.readEntity(String.class);
 
 867         long t2 = System.currentTimeMillis();
 
 868         log.info(responseReceivedMessage, t2 - t1);
 
 869         log.info(responseHttpCodeMessage, r.code);
 
 870         log.info("HTTP response message: {}", r.message);
 
 871         logHeaders(r.headers);
 
 872         log.info("HTTP response: {}", r.body);
 
 877     protected SSLContext createSSLContext(Parameters p) {
 
 878         try (FileInputStream in = new FileInputStream(p.keyStoreFileName)) {
 
 879             System.setProperty("jsse.enableSNIExtension", "false");
 
 880             System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
 
 881             System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);
 
 883             HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);
 
 885             KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
 
 886             KeyStore ks = KeyStore.getInstance("PKCS12");
 
 887             char[] pwd = p.keyStorePassword.toCharArray();
 
 891             SSLContext ctx = SSLContext.getInstance("TLS");
 
 892             ctx.init(kmf.getKeyManagers(), null, null);
 
 894         } catch (Exception e) {
 
 895             log.error("Error creating SSLContext: {}", e.getMessage(), e);
 
 900     protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage,
 
 903         resp.message = errorMessage;
 
 904         String pp = prefix != null ? prefix + '.' : "";
 
 905         ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
 
 906         ctx.setAttribute(pp + "response-message", resp.message);
 
 909     protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
 
 910         String pp = prefix != null ? prefix + '.' : "";
 
 911         ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
 
 912         ctx.setAttribute(pp + "response-message", r.message);
 
 915     public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
 
 916         HttpResponse r = null;
 
 918             FileParam p = getFileParameters(paramMap);
 
 919             byte[] data = Files.readAllBytes(Paths.get(p.fileName));
 
 921             r = sendHttpData(data, p);
 
 922             setResponseStatus(ctx, p.responsePrefix, r);
 
 924         } catch (SvcLogicException | IOException e) {
 
 925             log.error("Error sending the request: {}", e.getMessage(), e);
 
 927             r = new HttpResponse();
 
 929             r.message = e.getMessage();
 
 930             String prefix = parseParam(paramMap, responsePrefix, false, null);
 
 931             setResponseStatus(ctx, prefix, r);
 
 934         if (r != null && r.code >= 300) {
 
 935             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
 
 939     private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
 
 940         FileParam p = new FileParam();
 
 941         p.fileName = parseParam(paramMap, "fileName", true, null);
 
 942         p.url = parseParam(paramMap, "url", true, null);
 
 943         p.user = parseParam(paramMap, "user", false, null);
 
 944         p.password = parseParam(paramMap, "password", false, null);
 
 945         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
 
 946         p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
 
 947         String skipSendingStr = paramMap.get(skipSendingMessage);
 
 948         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
 
 949         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
 
 950         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
 
 951         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
 
 952         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
 
 953         p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
 
 957     public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
 
 960             UebParam p = getUebParameters(paramMap);
 
 962             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
 
 966             if (p.templateFileName == null) {
 
 967                 log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName);
 
 968                 p.templateFileName = defaultUebTemplateFileName;
 
 971             String reqTemplate = readFile(p.templateFileName);
 
 972             reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
 
 973             req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
 
 975             r = postOnUeb(req, p);
 
 976             setResponseStatus(ctx, p.responsePrefix, r);
 
 977             if (r.body != null) {
 
 978                 ctx.setAttribute(pp + "httpResponse", r.body);
 
 981         } catch (SvcLogicException e) {
 
 982             log.error("Error sending the request: {}", e.getMessage(), e);
 
 984             r = new HttpResponse();
 
 986             r.message = e.getMessage();
 
 987             String prefix = parseParam(paramMap, responsePrefix, false, null);
 
 988             setResponseStatus(ctx, prefix, r);
 
 992             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
 
 996     protected HttpResponse sendHttpData(byte[] data, FileParam p) throws SvcLogicException {
 
 998         Client client = ClientBuilder.newBuilder().build();
 
 999         client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
 
1000         client.property(ClientProperties.FOLLOW_REDIRECTS, true);
 
1001         WebTarget webTarget = addAuthType(client, p).target(p.url);
 
1003         log.info("Sending file");
 
1004         long t1 = System.currentTimeMillis();
 
1006         HttpResponse r = new HttpResponse();
 
1009         if (!p.skipSending) {
 
1010             String tt = "application/octet-stream";
 
1011             Invocation.Builder invocationBuilder = webTarget.request(tt).accept(tt);
 
1016                 if (p.httpMethod == HttpMethod.POST) {
 
1017                     response = invocationBuilder.post(Entity.entity(data, tt));
 
1018                 } else if (p.httpMethod == HttpMethod.PUT) {
 
1019                     response = invocationBuilder.put(Entity.entity(data, tt));
 
1021                     throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
 
1023             } catch (ProcessingException e) {
 
1024                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
 
1027             r.code = response.getStatus();
 
1028             r.headers = response.getStringHeaders();
 
1029             EntityTag etag = response.getEntityTag();
 
1031                 r.message = etag.getValue();
 
1033             if (response.hasEntity() && r.code != 204) {
 
1034                 r.body = response.readEntity(String.class);
 
1037             if (r.code == 301) {
 
1038                 String newUrl = response.getStringHeaders().getFirst("Location");
 
1040                 log.info("Got response code 301. Sending same request to URL: {}", newUrl);
 
1042                 webTarget = client.target(newUrl);
 
1043                 invocationBuilder = webTarget.request(tt).accept(tt);
 
1046                     if (p.httpMethod == HttpMethod.POST) {
 
1047                         response = invocationBuilder.post(Entity.entity(data, tt));
 
1048                     } else if (p.httpMethod == HttpMethod.PUT) {
 
1049                         response = invocationBuilder.put(Entity.entity(data, tt));
 
1051                         throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
 
1053                 } catch (ProcessingException e) {
 
1054                     throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
 
1057                 r.code = response.getStatus();
 
1058                 etag = response.getEntityTag();
 
1060                     r.message = etag.getValue();
 
1062                 if (response.hasEntity() && r.code != 204) {
 
1063                     r.body = response.readEntity(String.class);
 
1068         long t2 = System.currentTimeMillis();
 
1069         log.info(responseReceivedMessage, t2 - t1);
 
1070         log.info(responseHttpCodeMessage, r.code);
 
1071         log.info("HTTP response message: {}", r.message);
 
1072         logHeaders(r.headers);
 
1073         log.info("HTTP response: {}", r.body);
 
1078     private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
 
1079         UebParam p = new UebParam();
 
1080         p.topic = parseParam(paramMap, "topic", true, null);
 
1081         p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
 
1082         p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
 
1083         p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
 
1084         String skipSendingStr = paramMap.get(skipSendingMessage);
 
1085         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
 
1089     protected void logProperties(Map<String, Object> mm) {
 
1090         List<String> ll = new ArrayList<>();
 
1091         for (Object o : mm.keySet()) {
 
1094         Collections.sort(ll);
 
1096         log.info("Properties:");
 
1097         for (String name : ll) {
 
1098             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
 
1102     protected void logHeaders(MultivaluedMap<String, String> mm) {
 
1103         log.info("HTTP response headers:");
 
1109         List<String> ll = new ArrayList<>();
 
1110         for (Object o : mm.keySet()) {
 
1113         Collections.sort(ll);
 
1115         for (String name : ll) {
 
1116             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
 
1120     protected HttpResponse postOnUeb(String request, UebParam p) throws SvcLogicException {
 
1121         String[] urls = uebServers.split(" ");
 
1122         for (int i = 0; i < urls.length; i++) {
 
1123             if (!urls[i].endsWith("/")) {
 
1126             urls[i] += "events/" + p.topic;
 
1129         Client client = ClientBuilder.newBuilder().build();
 
1130         client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
 
1131         WebTarget webTarget = client.target(urls[0]);
 
1133         log.info("UEB URL: {}", urls[0]);
 
1134         log.info("Sending request:");
 
1136         long t1 = System.currentTimeMillis();
 
1138         HttpResponse r = new HttpResponse();
 
1141         if (!p.skipSending) {
 
1142             String tt = "application/json";
 
1143             String tt1 = tt + ";charset=UTF-8";
 
1146             Invocation.Builder invocationBuilder = webTarget.request(tt1).accept(tt);
 
1149                 response = invocationBuilder.post(Entity.entity(request, tt1));
 
1150             } catch (ProcessingException e) {
 
1151                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
 
1153             r.code = response.getStatus();
 
1154             r.headers = response.getStringHeaders();
 
1155             if (response.hasEntity()) {
 
1156                 r.body = response.readEntity(String.class);
 
1160         long t2 = System.currentTimeMillis();
 
1161         log.info(responseReceivedMessage, t2 - t1);
 
1162         log.info(responseHttpCodeMessage, r.code);
 
1163         logHeaders(r.headers);
 
1164         log.info("HTTP response:\n {}", r.body);
 
1169     public void setUebServers(String uebServers) {
 
1170         this.uebServers = uebServers;
 
1173     public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
 
1174         this.defaultUebTemplateFileName = defaultUebTemplateFileName;
 
1177     private static class FileParam {
 
1179         public String fileName;
 
1182         public String password;
 
1183         public HttpMethod httpMethod;
 
1184         public String responsePrefix;
 
1185         public boolean skipSending;
 
1186         public String oAuthConsumerKey;
 
1187         public String oAuthConsumerSecret;
 
1188         public String oAuthSignatureMethod;
 
1189         public String oAuthVersion;
 
1190         public AuthType authtype;
 
1193     private static class UebParam {
 
1195         public String topic;
 
1196         public String templateFileName;
 
1197         public String rootVarName;
 
1198         public String responsePrefix;
 
1199         public boolean skipSending;