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;
 
  28 import java.io.BufferedReader;
 
  30 import java.io.FileInputStream;
 
  31 import java.io.IOException;
 
  32 import java.io.InputStreamReader;
 
  33 import java.io.OutputStream;
 
  34 import java.net.HttpURLConnection;
 
  35 import java.net.ProtocolException;
 
  36 import java.net.SocketException;
 
  39 import java.nio.file.Files;
 
  40 import java.nio.file.Paths;
 
  41 import java.util.ArrayList;
 
  42 import java.util.Base64;
 
  43 import java.util.Collections;
 
  44 import java.util.HashMap;
 
  45 import java.util.HashSet;
 
  46 import java.util.Iterator;
 
  47 import java.util.List;
 
  49 import java.util.Map.Entry;
 
  50 import java.util.Properties;
 
  52 import java.util.regex.Matcher;
 
  53 import java.util.regex.Pattern;
 
  54 import javax.net.ssl.HttpsURLConnection;
 
  55 import javax.ws.rs.ProcessingException;
 
  56 import javax.ws.rs.client.Client;
 
  57 import javax.ws.rs.client.ClientBuilder;
 
  58 import javax.ws.rs.client.Entity;
 
  59 import javax.ws.rs.client.Invocation;
 
  60 import javax.ws.rs.client.WebTarget;
 
  61 import javax.ws.rs.core.EntityTag;
 
  62 import javax.ws.rs.core.Feature;
 
  63 import javax.ws.rs.core.MediaType;
 
  64 import javax.ws.rs.core.MultivaluedMap;
 
  65 import javax.ws.rs.core.Response;
 
  66 import javax.ws.rs.core.UriBuilder;
 
  67 import org.apache.commons.lang3.StringUtils;
 
  68 import org.codehaus.jettison.json.JSONException;
 
  69 import org.codehaus.jettison.json.JSONObject;
 
  70 import org.glassfish.jersey.client.ClientProperties;
 
  71 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
 
  72 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
 
  73 import org.glassfish.jersey.client.oauth1.ConsumerCredentials;
 
  74 import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
 
  75 import org.glassfish.jersey.media.multipart.MultiPart;
 
  76 import org.glassfish.jersey.media.multipart.MultiPartFeature;
 
  77 import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
 
  78 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  79 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  80 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
 
  81 import org.onap.logging.filter.base.HttpURLConnectionMetricUtil;
 
  82 import org.onap.logging.filter.base.MetricLogClientFilter;
 
  83 import org.onap.logging.filter.base.ONAPComponents;
 
  84 import org.onap.logging.ref.slf4j.ONAPLogConstants;
 
  85 import org.slf4j.Logger;
 
  86 import org.slf4j.LoggerFactory;
 
  89 public class RestapiCallNode implements SvcLogicJavaPlugin {
 
  91     protected static final String PARTNERS_FILE_NAME = "partners.json";
 
  92     protected static final String UEB_PROPERTIES_FILE_NAME = "ueb.properties";
 
  93     protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
 
  94     protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
 
  95     protected static final int DEFAULT_HTTP_CONNECT_TIMEOUT_MS = 30000; // 30 seconds
 
  96     protected static final int DEFAULT_HTTP_READ_TIMEOUT_MS = 600000; // 10 minutes
 
  98     private static final Logger log = LoggerFactory.getLogger(RestapiCallNode.class);
 
  99     private String uebServers;
 
 100     private String defaultUebTemplateFileName = "/opt/bvc/restapi/templates/default-ueb-message.json";
 
 102     private String responseReceivedMessage = "Response received. Time: {}";
 
 103     private String responseHttpCodeMessage = "HTTP response code: {}";
 
 104     private String requestPostingException = "Exception while posting http request to client ";
 
 105     protected static final String skipSendingMessage = "skipSending";
 
 106     protected static final String responsePrefix = "responsePrefix";
 
 107     protected static final String restapiUrlString = "restapiUrl";
 
 108     protected static final String restapiUserKey = "restapiUser";
 
 109     protected static final String restapiPasswordKey = "restapiPassword";
 
 110     protected Integer httpConnectTimeout;
 
 111     protected Integer httpReadTimeout;
 
 113     protected HashMap<String, PartnerDetails> partnerStore;
 
 114     private static final Pattern retryPattern = Pattern.compile(".*,(http|https):.*");
 
 116     public RestapiCallNode() {
 
 117         String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
 
 119             String jsonString = readFile(configDir + "/" + PARTNERS_FILE_NAME);
 
 120             JSONObject partners = new JSONObject(jsonString);
 
 121             partnerStore = new HashMap<>();
 
 122             loadPartners(partners);
 
 123             log.info("Partners support enabled");
 
 124         } catch (Exception e) {
 
 125             log.warn("Partners file could not be read, Partner support will not be enabled. " + e.getMessage());
 
 128         try (FileInputStream in = new FileInputStream(configDir + "/" + UEB_PROPERTIES_FILE_NAME)) {
 
 129             Properties props = new Properties();
 
 131             uebServers = props.getProperty("servers");
 
 132             log.info("UEB support enabled");
 
 133         } catch (Exception e) {
 
 134             log.warn("UEB properties could not be read, UEB support will not be enabled. " + e.getMessage());
 
 136         httpConnectTimeout = readOptionalInteger("HTTP_CONNECT_TIMEOUT_MS",DEFAULT_HTTP_CONNECT_TIMEOUT_MS);
 
 137         httpReadTimeout = readOptionalInteger("HTTP_READ_TIMEOUT_MS",DEFAULT_HTTP_READ_TIMEOUT_MS);
 
 140     @SuppressWarnings("unchecked")
 
 141     protected void loadPartners(JSONObject partners) {
 
 142         Iterator<String> keys = partners.keys();
 
 143         String partnerUserKey = "user";
 
 144         String partnerPasswordKey = "password";
 
 145         String partnerUrlKey = "url";
 
 147         while (keys.hasNext()) {
 
 148             String partnerKey = keys.next();
 
 150                 JSONObject partnerObject = (JSONObject) partners.get(partnerKey);
 
 151                 if (partnerObject.has(partnerUserKey) && partnerObject.has(partnerPasswordKey)) {
 
 153                     if (partnerObject.has(partnerUrlKey)) {
 
 154                         url = partnerObject.getString(partnerUrlKey);
 
 156                     String userName = partnerObject.getString(partnerUserKey);
 
 157                     String password = partnerObject.getString(partnerPasswordKey);
 
 158                     PartnerDetails details = new PartnerDetails(userName, getObfuscatedVal(password), url);
 
 159                     partnerStore.put(partnerKey, details);
 
 160                     log.info("mapped partner using partner key " + partnerKey);
 
 162                     log.info("Partner " + partnerKey + " is missing required keys, it won't be mapped");
 
 164             } catch (JSONException e) {
 
 165                 log.info("Couldn't map the partner using partner key " + partnerKey, e);
 
 170     /* Unobfuscate param value */
 
 171     private static String getObfuscatedVal(String paramValue) {
 
 172         String resValue = paramValue;
 
 173         if (paramValue != null && paramValue.startsWith("${") && paramValue.endsWith("}"))
 
 175             String paramStr = paramValue.substring(2, paramValue.length()-1);
 
 176             if (paramStr  != null && paramStr.length() > 0)
 
 178                 String val = System.getenv(paramStr);
 
 179                 if (val != null && val.length() > 0)
 
 182                     log.info("Obfuscated value RESET for param value:" + paramValue);
 
 190      * Returns parameters from the parameter map.
 
 192      * @param paramMap parameter map
 
 193      * @param p parameters instance
 
 194      * @return parameters filed instance
 
 195      * @throws SvcLogicException when svc logic exception occurs
 
 197     public static Parameters getParameters(Map<String, String> paramMap, Parameters p) throws SvcLogicException {
 
 199         p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
 
 200         p.requestBody = parseParam(paramMap, "requestBody", false, null);
 
 201         p.restapiUrl = parseParam(paramMap, restapiUrlString, true, null);
 
 202         p.restapiUrlSuffix = parseParam(paramMap, "restapiUrlSuffix", false, null);
 
 203         if (p.restapiUrlSuffix != null) {
 
 204             p.restapiUrl = p.restapiUrl + p.restapiUrlSuffix;
 
 207         p.restapiUrl = UriBuilder.fromUri(p.restapiUrl).toTemplate();
 
 208         validateUrl(p.restapiUrl);
 
 210         p.restapiUser = parseParam(paramMap, restapiUserKey, false, null);
 
 211         p.restapiPassword = parseParam(paramMap, restapiPasswordKey, false, null);
 
 212         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
 
 213         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
 
 214         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
 
 215         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
 
 216         p.contentType = parseParam(paramMap, "contentType", false, null);
 
 217         p.format = Format.fromString(parseParam(paramMap, "format", false, "json"));
 
 218         p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
 
 219         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
 
 220         p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
 
 221         p.listNameList = getListNameList(paramMap);
 
 222         String skipSendingStr = paramMap.get(skipSendingMessage);
 
 223         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
 
 224         p.convertResponse = valueOf(parseParam(paramMap, "convertResponse", false, "true"));
 
 225         p.customHttpHeaders = parseParam(paramMap, "customHttpHeaders", false, null);
 
 226         p.partner = parseParam(paramMap, "partner", false, null);
 
 227         p.dumpHeaders = valueOf(parseParam(paramMap, "dumpHeaders", false, null));
 
 228         p.returnRequestPayload = valueOf(parseParam(paramMap, "returnRequestPayload", false, null));
 
 229         p.accept = parseParam(paramMap, "accept", false, null);
 
 230         p.multipartFormData = valueOf(parseParam(paramMap, "multipartFormData", false, "false"));
 
 231         p.multipartFile = parseParam(paramMap, "multipartFile", false, null);
 
 232         p.targetEntity = parseParam(paramMap, "targetEntity", false, null);
 
 237      * Validates the given URL in the parameters.
 
 239      * @param restapiUrl rest api URL
 
 240      * @throws SvcLogicException when URL validation fails
 
 242     private static void validateUrl(String restapiUrl) throws SvcLogicException {
 
 243         if (containsMultipleUrls(restapiUrl)) {
 
 244             String[] urls = getMultipleUrls(restapiUrl);
 
 245             for (String url : urls) {
 
 250                 URI.create(restapiUrl);
 
 251             } catch (IllegalArgumentException e) {
 
 252                 throw new SvcLogicException("Invalid input of url " + e.getLocalizedMessage(), e);
 
 258      * Returns the list of list name.
 
 260      * @param paramMap parameters map
 
 261      * @return list of list name
 
 263     private static Set<String> getListNameList(Map<String, String> paramMap) {
 
 264         Set<String> ll = new HashSet<>();
 
 265         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
 
 266             if (entry.getKey().startsWith("listName")) {
 
 267                 ll.add(entry.getValue());
 
 274      * Parses the parameter string map of property, validates if required, assigns default value if
 
 275      * present and returns the value.
 
 277      * @param paramMap string param map
 
 278      * @param name name of the property
 
 279      * @param required if value required
 
 280      * @param def default value
 
 281      * @return value of the property
 
 282      * @throws SvcLogicException if required parameter value is empty
 
 284     public static String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
 
 285         throws SvcLogicException {
 
 286         String s = paramMap.get(name);
 
 288         if (s == null || s.trim().length() == 0) {
 
 292             throw new SvcLogicException("Parameter " + name + " is required in RestapiCallNode");
 
 296         StringBuilder value = new StringBuilder();
 
 298         int i1 = s.indexOf('%');
 
 300             int i2 = s.indexOf('%', i1 + 1);
 
 305             String varName = s.substring(i1 + 1, i2);
 
 306             String varValue = System.getenv(varName);
 
 307             if (varValue == null) {
 
 308                 varValue = "%" + varName + "%";
 
 311             value.append(s.substring(i, i1));
 
 312             value.append(varValue);
 
 315             i1 = s.indexOf('%', i);
 
 317         value.append(s.substring(i));
 
 319         log.info("Parameter {}: [{}]", name, maskPassword(name, value));
 
 321         return value.toString();
 
 324     private static Object maskPassword(String name, Object value) {
 
 325         String[] pwdNames = {"pwd", "passwd", "password", "Pwd", "Passwd", "Password"};
 
 326         for (String pwdName : pwdNames) {
 
 327             if (name.contains(pwdName)) {
 
 335      * Allows Directed Graphs the ability to interact with REST APIs.
 
 337      * @param paramMap HashMap<String,String> of parameters passed by the DG to this function
 
 341      *        <th>Mandatory/Optional</th>
 
 342      *        <th>description</th>
 
 343      *        <th>example values</th></thead> <tbody>
 
 345      *        <td>templateFileName</td>
 
 347      *        <td>full path to template file that can be used to build a request</td>
 
 348      *        <td>/sdncopt/bvc/restapi/templates/vnf_service-configuration-operation_minimal.json</td>
 
 351      *        <td>restapiUrl</td>
 
 353      *        <td>url to send the request to</td>
 
 354      *        <td>https://sdncodl:8543/restconf/operations/L3VNF-API:create-update-vnf-request</td>
 
 357      *        <td>restapiUser</td>
 
 359      *        <td>user name to use for http basic authentication</td>
 
 363      *        <td>restapiPassword</td>
 
 365      *        <td>unencrypted password to use for http basic authentication</td>
 
 366      *        <td>plain_password</td>
 
 369      *        <td>oAuthConsumerKey</td>
 
 371      *        <td>Consumer key to use for http oAuth authentication</td>
 
 375      *        <td>oAuthConsumerSecret</td>
 
 377      *        <td>Consumer secret to use for http oAuth authentication</td>
 
 378      *        <td>plain_secret</td>
 
 381      *        <td>oAuthSignatureMethod</td>
 
 383      *        <td>Consumer method to use for http oAuth authentication</td>
 
 387      *        <td>oAuthVersion</td>
 
 389      *        <td>Version http oAuth authentication</td>
 
 393      *        <td>contentType</td>
 
 395      *        <td>http content type to set in the http header</td>
 
 396      *        <td>usually application/json or application/xml</td>
 
 401      *        <td>should match request body format</td>
 
 402      *        <td>json or xml</td>
 
 405      *        <td>httpMethod</td>
 
 407      *        <td>http method to use when sending the request</td>
 
 408      *        <td>get post put delete patch</td>
 
 411      *        <td>responsePrefix</td>
 
 413      *        <td>location the response will be written to in context memory</td>
 
 414      *        <td>tmp.restapi.result</td>
 
 417      *        <td>listName[i]</td>
 
 419      *        <td>Used for processing XML responses with repeating
 
 420      *        elements.</td>vpn-information.vrf-details
 
 424      *        <td>skipSending</td>
 
 427      *        <td>true or false</td>
 
 430      *        <td>convertResponse</td>
 
 432      *        <td>whether the response should be converted</td>
 
 433      *        <td>true or false</td>
 
 436      *        <td>customHttpHeaders</td>
 
 438      *        <td>a list additional http headers to be passed in, follow the format in the example</td>
 
 439      *        <td>X-CSI-MessageId=messageId,headerFieldName=headerFieldValue</td>
 
 442      *        <td>dumpHeaders</td>
 
 444      *        <td>when true writes http header content to context memory</td>
 
 445      *        <td>true or false</td>
 
 450      *        <td>used to retrieve username, password and url if partner store exists</td>
 
 454      *        <td>returnRequestPayload</td>
 
 456      *        <td>used to return payload built in the request</td>
 
 457      *        <td>true or false</td>
 
 461      * @param ctx Reference to context memory
 
 462      * @throws SvcLogicException
 
 464      * @see String#split(String, int)
 
 466     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
 
 467         sendRequest(paramMap, ctx, null);
 
 470     protected void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx, RetryPolicy retryPolicy)
 
 471         throws SvcLogicException {
 
 473         HttpResponse r = new HttpResponse();
 
 475             handlePartner(paramMap);
 
 476             Parameters p = getParameters(paramMap, new Parameters());
 
 477             if(p.targetEntity != null && !p.targetEntity.isEmpty()) {
 
 478                 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, p.targetEntity);
 
 480             if (containsMultipleUrls(p.restapiUrl) && retryPolicy == null) {
 
 481                 String[] urls = getMultipleUrls(p.restapiUrl);
 
 482                 retryPolicy = new RetryPolicy(urls, urls.length * 2);
 
 483                 p.restapiUrl = urls[0];
 
 485             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
 
 488             if (p.templateFileName != null) {
 
 489                 String reqTemplate = readFile(p.templateFileName);
 
 490                 req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
 
 491             } else if (p.requestBody != null) {
 
 494             r = sendHttpRequest(req, p);
 
 495             setResponseStatus(ctx, p.responsePrefix, r);
 
 497             if (p.dumpHeaders && r.headers != null) {
 
 498                 for (Entry<String, List<String>> a : r.headers.entrySet()) {
 
 499                     ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
 
 503             if (p.returnRequestPayload && req != null) {
 
 504                 ctx.setAttribute(pp + "httpRequest", req);
 
 507             if (r.body != null && r.body.trim().length() > 0) {
 
 508                 ctx.setAttribute(pp + "httpResponse", r.body);
 
 510                 if (p.convertResponse) {
 
 511                     Map<String, String> mm = null;
 
 512                     if (p.format == Format.XML) {
 
 513                         mm = XmlParser.convertToProperties(r.body, p.listNameList);
 
 514                     } else if (p.format == Format.JSON) {
 
 515                         mm = JsonParser.convertToProperties(r.body);
 
 519                         for (Map.Entry<String, String> entry : mm.entrySet()) {
 
 520                             ctx.setAttribute(pp + entry.getKey(), entry.getValue());
 
 525         } catch (SvcLogicException e) {
 
 526             boolean shouldRetry = false;
 
 527             if (e.getCause().getCause() instanceof SocketException) {
 
 531             log.error("Error sending the request: " + e.getMessage(), e);
 
 532             String prefix = parseParam(paramMap, responsePrefix, false, null);
 
 533             if (retryPolicy == null || !shouldRetry) {
 
 534                 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
 
 536                 log.debug(retryPolicy.getRetryMessage());
 
 538                     // calling getNextHostName increments the retry count so it should be called before shouldRetry
 
 539                     String retryString = retryPolicy.getNextHostName();
 
 540                     if (retryPolicy.shouldRetry()) {
 
 541                         paramMap.put(restapiUrlString, retryString);
 
 542                         log.debug("retry attempt {} will use the retry url {}", retryPolicy.getRetryCount(),
 
 544                         sendRequest(paramMap, ctx, retryPolicy);
 
 546                         log.debug("Maximum retries reached, won't attempt to retry. Calling setFailureResponseStatus.");
 
 547                         setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
 
 549                 } catch (Exception ex) {
 
 550                     String retryErrorMessage = "Retry attempt " + retryPolicy.getRetryCount()
 
 551                         + "has failed with error message " + ex.getMessage();
 
 552                     setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
 
 557         if (r != null && r.code >= 300) {
 
 558             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
 
 562     protected void handlePartner(Map<String, String> paramMap) {
 
 563         String partner = paramMap.get("partner");
 
 564         if (partner != null && partner.length() > 0) {
 
 565             PartnerDetails details = partnerStore.get(partner);
 
 566             paramMap.put(restapiUserKey, details.username);
 
 567             paramMap.put(restapiPasswordKey, details.password);
 
 568             if (paramMap.get(restapiUrlString) == null) {
 
 569                 paramMap.put(restapiUrlString, details.url);
 
 574     protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format) throws SvcLogicException {
 
 575         log.info("Building {} started", format);
 
 576         long t1 = System.currentTimeMillis();
 
 577         String originalTemplate = template;
 
 579         template = expandRepeats(ctx, template, 1);
 
 581         Map<String, String> mm = new HashMap<>();
 
 582         for (String s : ctx.getAttributeKeySet()) {
 
 583             mm.put(s, ctx.getAttribute(s));
 
 586         StringBuilder ss = new StringBuilder();
 
 588         while (i < template.length()) {
 
 589             int i1 = template.indexOf("${", i);
 
 591                 ss.append(template.substring(i));
 
 595             int i2 = template.indexOf('}', i1 + 2);
 
 597                 throw new SvcLogicException("Template error: Matching } not found");
 
 600             String var1 = template.substring(i1 + 2, i2);
 
 601             String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
 
 602             if (value1 == null || value1.trim().length() == 0) {
 
 603                 // delete the whole element (line)
 
 604                 int i3 = template.lastIndexOf('\n', i1);
 
 608                 int i4 = template.indexOf('\n', i1);
 
 610                     i4 = template.length();
 
 614                     ss.append(template.substring(i, i3));
 
 618                 ss.append(template.substring(i, i1)).append(value1);
 
 623         String req = format == Format.XML ? XmlJsonUtil.removeEmptyStructXml(ss.toString())
 
 624             : XmlJsonUtil.removeEmptyStructJson(originalTemplate, ss.toString());
 
 626         if (format == Format.JSON) {
 
 627             req = XmlJsonUtil.removeLastCommaJson(req);
 
 630         long t2 = System.currentTimeMillis();
 
 631         log.info("Building {} completed. Time: {}", format, t2 - t1);
 
 636     protected String expandRepeats(SvcLogicContext ctx, String template, int level) throws SvcLogicException {
 
 637         StringBuilder newTemplate = new StringBuilder();
 
 639         while (k < template.length()) {
 
 640             int i1 = template.indexOf("${repeat:", k);
 
 642                 newTemplate.append(template.substring(k));
 
 646             int i2 = template.indexOf(':', i1 + 9);
 
 648                 throw new SvcLogicException(
 
 649                     "Template error: Context variable name followed by : is required after repeat");
 
 652             // Find the closing }, store in i3
 
 656             while (nn > 0 && i < template.length()) {
 
 657                 i3 = template.indexOf('}', i);
 
 659                     throw new SvcLogicException("Template error: Matching } not found");
 
 661                 int i32 = template.indexOf('{', i);
 
 662                 if (i32 >= 0 && i32 < i3) {
 
 671             String var1 = template.substring(i1 + 9, i2);
 
 672             String value1 = ctx.getAttribute(var1);
 
 673             log.info("     {}:{}", var1, value1);
 
 676                 n = Integer.parseInt(value1);
 
 677             } catch (NumberFormatException e) {
 
 678                 log.info("value1 not set or not a number, n will remain set at zero");
 
 681             newTemplate.append(template.substring(k, i1));
 
 683             String rpt = template.substring(i2 + 1, i3);
 
 685             for (int ii = 0; ii < n; ii++) {
 
 686                 String ss = rpt.replaceAll("\\[\\$\\{" + level + "\\}\\]", "[" + ii + "]");
 
 687                 if (ii == n - 1 && ss.trim().endsWith(",")) {
 
 688                     int i4 = ss.lastIndexOf(',');
 
 690                         ss = ss.substring(0, i4) + ss.substring(i4 + 1);
 
 693                 newTemplate.append(ss);
 
 700             return newTemplate.toString();
 
 703         return expandRepeats(ctx, newTemplate.toString(), level + 1);
 
 706     protected String readFile(String fileName) throws SvcLogicException {
 
 708             byte[] encoded = Files.readAllBytes(Paths.get(fileName));
 
 709             return new String(encoded, "UTF-8");
 
 710         } catch (IOException | SecurityException e) {
 
 711             throw new SvcLogicException("Unable to read file " + fileName + e.getLocalizedMessage(), e);
 
 715     protected Client addAuthType(Client c, FileParam fp) throws SvcLogicException {
 
 716         Parameters p = new Parameters();
 
 717         p.restapiUser = fp.user;
 
 718         p.restapiPassword = fp.password;
 
 719         p.oAuthConsumerKey = fp.oAuthConsumerKey;
 
 720         p.oAuthVersion = fp.oAuthVersion;
 
 721         p.oAuthConsumerSecret = fp.oAuthConsumerSecret;
 
 722         p.oAuthSignatureMethod = fp.oAuthSignatureMethod;
 
 723         p.authtype = fp.authtype;
 
 724         return addAuthType(c, p);
 
 727     public Client addAuthType(Client client, Parameters p) throws SvcLogicException {
 
 728         if (p.authtype == AuthType.Unspecified) {
 
 729             if (p.restapiUser != null && p.restapiPassword != null) {
 
 730                 client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
 
 731             } else if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
 
 732                 Feature oAuth1Feature =
 
 733                     OAuth1ClientSupport.builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
 
 734                         .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
 
 735                 client.register(oAuth1Feature);
 
 739             if (p.authtype == AuthType.DIGEST) {
 
 740                 if (p.restapiUser != null && p.restapiPassword != null) {
 
 741                     client.register(HttpAuthenticationFeature.digest(p.restapiUser, p.restapiPassword));
 
 743                     throw new SvcLogicException(
 
 744                         "oAUTH authentication type selected but all restapiUser and restapiPassword "
 
 745                             + "parameters doesn't exist",
 
 748             } else if (p.authtype == AuthType.BASIC) {
 
 749                 if (p.restapiUser != null && p.restapiPassword != null) {
 
 750                     client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
 
 752                     throw new SvcLogicException(
 
 753                         "oAUTH authentication type selected but all restapiUser and restapiPassword "
 
 754                             + "parameters doesn't exist",
 
 757             } else if (p.authtype == AuthType.OAUTH) {
 
 758                 if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
 
 759                     Feature oAuth1Feature = OAuth1ClientSupport
 
 760                         .builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
 
 761                         .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
 
 762                     client.register(oAuth1Feature);
 
 764                     throw new SvcLogicException(
 
 765                         "oAUTH authentication type selected but all oAuthConsumerKey, oAuthConsumerSecret "
 
 766                             + "and oAuthSignatureMethod parameters doesn't exist",
 
 775      * Receives the http response for the http request sent.
 
 777      * @param request request msg
 
 778      * @param p parameters
 
 779      * @return HTTP response
 
 780      * @throws SvcLogicException when sending http request fails
 
 782     public HttpResponse sendHttpRequest(String request, Parameters p) throws SvcLogicException {
 
 784         HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);
 
 786         Client client = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true).build();
 
 787         setClientTimeouts(client);
 
 788         // Needed to support additional HTTP methods such as PATCH
 
 789         client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
 
 790         client.register(new MetricLogClientFilter());
 
 791         WebTarget webTarget = addAuthType(client, p).target(p.restapiUrl);
 
 793         long t1 = System.currentTimeMillis();
 
 795         HttpResponse r = new HttpResponse();
 
 797         String accept = p.accept;
 
 798         if (accept == null) {
 
 799             accept = p.format == Format.XML ? "application/xml" : "application/json";
 
 802         String contentType = p.contentType;
 
 803         if (contentType == null) {
 
 804             contentType = accept + ";charset=UTF-8";
 
 807         if (!p.skipSending && !p.multipartFormData) {
 
 809             Invocation.Builder invocationBuilder = webTarget.request(contentType).accept(accept);
 
 811             if (p.format == Format.NONE) {
 
 812                 invocationBuilder.header("", "");
 
 815             if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
 
 816                 String[] keyValuePairs = p.customHttpHeaders.split(",");
 
 817                 for (String singlePair : keyValuePairs) {
 
 818                     int equalPosition = singlePair.indexOf('=');
 
 819                     invocationBuilder.header(singlePair.substring(0, equalPosition),
 
 820                         singlePair.substring(equalPosition + 1, singlePair.length()));
 
 824             invocationBuilder.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
 
 829                 // When the HTTP operation has no body do not set the content-type
 
 830                 //setting content-type has caused errors with some servers when no body is present
 
 831                 if (request == null) {
 
 832                     response = invocationBuilder.method(p.httpMethod.toString());
 
 834                     log.info("Sending request below to url " + p.restapiUrl);
 
 836                     response = invocationBuilder.method(p.httpMethod.toString(), entity(request, contentType));
 
 838             } catch (ProcessingException | IllegalStateException e) {
 
 839                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
 
 842             r.code = response.getStatus();
 
 843             r.headers = response.getStringHeaders();
 
 844             EntityTag etag = response.getEntityTag();
 
 846                 r.message = etag.getValue();
 
 848             if (response.hasEntity() && r.code != 204) {
 
 849                 r.body = response.readEntity(String.class);
 
 851         } else if (!p.skipSending && p.multipartFormData) {
 
 853             WebTarget wt = client.register(MultiPartFeature.class).target(p.restapiUrl);
 
 855             MultiPart multiPart = new MultiPart();
 
 856             multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
 
 858             FileDataBodyPart fileDataBodyPart =
 
 859                 new FileDataBodyPart("file", new File(p.multipartFile), MediaType.APPLICATION_OCTET_STREAM_TYPE);
 
 860             multiPart.bodyPart(fileDataBodyPart);
 
 863             Invocation.Builder invocationBuilder = wt.request(contentType).accept(accept);
 
 865             if (p.format == Format.NONE) {
 
 866                 invocationBuilder.header("", "");
 
 869             if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
 
 870                 String[] keyValuePairs = p.customHttpHeaders.split(",");
 
 871                 for (String singlePair : keyValuePairs) {
 
 872                     int equalPosition = singlePair.indexOf('=');
 
 873                     invocationBuilder.header(singlePair.substring(0, equalPosition),
 
 874                         singlePair.substring(equalPosition + 1, singlePair.length()));
 
 882                     invocationBuilder.method(p.httpMethod.toString(), entity(multiPart, multiPart.getMediaType()));
 
 883             } catch (ProcessingException | IllegalStateException e) {
 
 884                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
 
 887             r.code = response.getStatus();
 
 888             r.headers = response.getStringHeaders();
 
 889             EntityTag etag = response.getEntityTag();
 
 891                 r.message = etag.getValue();
 
 893             if (response.hasEntity() && r.code != 204) {
 
 894                 r.body = response.readEntity(String.class);
 
 899         long t2 = System.currentTimeMillis();
 
 900         log.info(responseReceivedMessage, t2 - t1);
 
 901         log.info(responseHttpCodeMessage, r.code);
 
 902         log.info("HTTP response message: {}", r.message);
 
 903         logHeaders(r.headers);
 
 904         log.info("HTTP response: {}", r.body);
 
 909     protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage,
 
 912         resp.message = errorMessage;
 
 913         String pp = prefix != null ? prefix + '.' : "";
 
 914         ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
 
 915         ctx.setAttribute(pp + "response-message", resp.message);
 
 918     protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
 
 919         String pp = prefix != null ? prefix + '.' : "";
 
 920         ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
 
 921         ctx.setAttribute(pp + "response-message", r.message);
 
 924     public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
 
 925         HttpResponse r = null;
 
 927             FileParam p = getFileParameters(paramMap);
 
 928             byte[] data = Files.readAllBytes(Paths.get(p.fileName));
 
 930             r = sendHttpData(data, p);
 
 932             for (int i = 0; i < 10 && r.code == 301; i++) {
 
 933                 String newUrl = r.headers2.get("Location").get(0);
 
 935                 log.info("Got response code 301. Sending same request to URL: " + newUrl);
 
 938                 r = sendHttpData(data, p);
 
 941             setResponseStatus(ctx, p.responsePrefix, r);
 
 943         } catch (SvcLogicException | IOException e) {
 
 944             log.error("Error sending the request: {}", e.getMessage(), e);
 
 946             r = new HttpResponse();
 
 948             r.message = e.getMessage();
 
 949             String prefix = parseParam(paramMap, responsePrefix, false, null);
 
 950             setResponseStatus(ctx, prefix, r);
 
 953         if (r != null && r.code >= 300) {
 
 954             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
 
 958     private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
 
 959         FileParam p = new FileParam();
 
 960         p.fileName = parseParam(paramMap, "fileName", true, null);
 
 961         p.url = parseParam(paramMap, "url", true, null);
 
 962         p.user = parseParam(paramMap, "user", false, null);
 
 963         p.password = parseParam(paramMap, "password", false, null);
 
 964         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
 
 965         p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
 
 966         String skipSendingStr = paramMap.get(skipSendingMessage);
 
 967         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
 
 968         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
 
 969         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
 
 970         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
 
 971         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
 
 972         p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
 
 976     public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
 
 979             UebParam p = getUebParameters(paramMap);
 
 981             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
 
 985             if (p.templateFileName == null) {
 
 986                 log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName);
 
 987                 p.templateFileName = defaultUebTemplateFileName;
 
 990             String reqTemplate = readFile(p.templateFileName);
 
 991             reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
 
 992             req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
 
 994             r = postOnUeb(req, p);
 
 995             setResponseStatus(ctx, p.responsePrefix, r);
 
 996             if (r.body != null) {
 
 997                 ctx.setAttribute(pp + "httpResponse", r.body);
 
1000         } catch (SvcLogicException e) {
 
1001             log.error("Error sending the request: {}", e.getMessage(), e);
 
1003             r = new HttpResponse();
 
1005             r.message = e.getMessage();
 
1006             String prefix = parseParam(paramMap, responsePrefix, false, null);
 
1007             setResponseStatus(ctx, prefix, r);
 
1010         if (r.code >= 300) {
 
1011             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
 
1015     protected HttpResponse sendHttpData(byte[] data, FileParam p) throws IOException {
 
1016         URL url = new URL(p.url);
 
1017         HttpURLConnection con = (HttpURLConnection) url.openConnection();
 
1019         log.info("Connection: " + con.getClass().getName());
 
1021         con.setRequestMethod(p.httpMethod.toString());
 
1022         con.setRequestProperty("Content-Type", "application/octet-stream");
 
1023         con.setRequestProperty("Accept", "*/*");
 
1024         con.setRequestProperty("Expect", "100-continue");
 
1025         con.setFixedLengthStreamingMode(data.length);
 
1026         con.setInstanceFollowRedirects(false);
 
1028         if (p.user != null && p.password != null) {
 
1029             String authString = p.user + ":" + p.password;
 
1030             String authStringEnc = Base64.getEncoder().encodeToString(authString.getBytes());
 
1031             con.setRequestProperty("Authorization", "Basic " + authStringEnc);
 
1034         con.setDoInput(true);
 
1035         con.setDoOutput(true);
 
1037         log.info("Sending file");
 
1038         long t1 = System.currentTimeMillis();
 
1040         HttpResponse r = new HttpResponse();
 
1043         if (!p.skipSending) {
 
1044             HttpURLConnectionMetricUtil util = new HttpURLConnectionMetricUtil();
 
1045             util.logBefore(con, ONAPComponents.DMAAP);
 
1049             boolean continue100failed = false;
 
1051                 OutputStream os = con.getOutputStream();
 
1055             } catch (ProtocolException e) {
 
1056                 continue100failed = true;
 
1059             r.code = con.getResponseCode();
 
1060             r.headers2 = con.getHeaderFields();
 
1062             if (r.code != 204 && !continue100failed) {
 
1063                 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
 
1065                 StringBuffer response = new StringBuffer();
 
1066                 while ((inputLine = in.readLine()) != null) {
 
1067                     response.append(inputLine);
 
1071                 r.body = response.toString();
 
1079         long t2 = System.currentTimeMillis();
 
1080         log.info("Response received. Time: {}", t2 - t1);
 
1081         log.info("HTTP response code: {}", r.code);
 
1082         log.info("HTTP response message: {}", r.message);
 
1083         logHeaders(r.headers2);
 
1084         log.info("HTTP response: {}", r.body);
 
1089     private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
 
1090         UebParam p = new UebParam();
 
1091         p.topic = parseParam(paramMap, "topic", true, null);
 
1092         p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
 
1093         p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
 
1094         p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
 
1095         String skipSendingStr = paramMap.get(skipSendingMessage);
 
1096         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
 
1100     protected void logProperties(Map<String, Object> mm) {
 
1101         List<String> ll = new ArrayList<>();
 
1102         for (Object o : mm.keySet()) {
 
1105         Collections.sort(ll);
 
1107         log.info("Properties:");
 
1108         for (String name : ll) {
 
1109             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
 
1113     protected void logHeaders(MultivaluedMap<String, String> mm) {
 
1114         log.info("HTTP response headers:");
 
1120         List<String> ll = new ArrayList<>();
 
1121         for (Object o : mm.keySet()) {
 
1124         Collections.sort(ll);
 
1126         for (String name : ll) {
 
1127             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
 
1131     private void logHeaders(Map<String, List<String>> mm) {
 
1132         if (mm == null || mm.isEmpty()) {
 
1136         List<String> ll = new ArrayList<>();
 
1137         for (String s : mm.keySet()) {
 
1142         Collections.sort(ll);
 
1144         for (String name : ll) {
 
1145             List<String> v = mm.get(name);
 
1146             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
 
1147             log.info("--- " + name + ": " + (v.size() == 1 ? v.get(0) : v));
 
1151     protected HttpResponse postOnUeb(String request, UebParam p) throws SvcLogicException {
 
1152         String[] urls = uebServers.split(" ");
 
1153         for (int i = 0; i < urls.length; i++) {
 
1154             if (!urls[i].endsWith("/")) {
 
1157             urls[i] += "events/" + p.topic;
 
1160         Client client = ClientBuilder.newBuilder().build();
 
1161         setClientTimeouts(client);
 
1162         WebTarget webTarget = client.target(urls[0]);
 
1164         log.info("UEB URL: {}", urls[0]);
 
1165         log.info("Sending request:");
 
1167         long t1 = System.currentTimeMillis();
 
1169         HttpResponse r = new HttpResponse();
 
1172         if (!p.skipSending) {
 
1173             String tt = "application/json";
 
1174             String tt1 = tt + ";charset=UTF-8";
 
1177             Invocation.Builder invocationBuilder = webTarget.request(tt1).accept(tt);
 
1180                 response = invocationBuilder.post(Entity.entity(request, tt1));
 
1181             } catch (ProcessingException e) {
 
1182                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
 
1184             r.code = response.getStatus();
 
1185             r.headers = response.getStringHeaders();
 
1186             if (response.hasEntity()) {
 
1187                 r.body = response.readEntity(String.class);
 
1191         long t2 = System.currentTimeMillis();
 
1192         log.info(responseReceivedMessage, t2 - t1);
 
1193         log.info(responseHttpCodeMessage, r.code);
 
1194         logHeaders(r.headers);
 
1195         log.info("HTTP response:\n {}", r.body);
 
1200     public void setUebServers(String uebServers) {
 
1201         this.uebServers = uebServers;
 
1204     public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
 
1205         this.defaultUebTemplateFileName = defaultUebTemplateFileName;
 
1208     protected void setClientTimeouts(Client client) {
 
1209         client.property(ClientProperties.CONNECT_TIMEOUT, httpConnectTimeout);
 
1210         client.property(ClientProperties.READ_TIMEOUT, httpReadTimeout);
 
1213     protected Integer readOptionalInteger(String propertyName, Integer defaultValue) {
 
1214         String stringValue = System.getProperty(propertyName);
 
1215         if (stringValue != null && stringValue.length() > 0) {
 
1217                 return Integer.valueOf(stringValue);
 
1218             } catch (NumberFormatException e) {
 
1219                 log.warn("property " + propertyName + " had the value " + stringValue + " that could not be converted to an Integer, default " + defaultValue + " will be used instead", e);
 
1222         return defaultValue;
 
1225     protected static String[] getMultipleUrls(String restapiUrl) {
 
1226         List<String> urls = new ArrayList<>();
 
1228         for (int i = 0; i < restapiUrl.length(); i++) {
 
1229             if (restapiUrl.charAt(i) == ',') {
 
1230                 if (i + 9 < restapiUrl.length()) {
 
1231                     String part = restapiUrl.substring(i + 1, i + 9);
 
1232                     if (part.equals("https://") || part.startsWith("http://")) {
 
1233                         urls.add(restapiUrl.substring(start, i));
 
1237             } else if (i == restapiUrl.length() - 1) {
 
1238                 urls.add(restapiUrl.substring(start, i + 1));
 
1241         String[] arr = new String[urls.size()];
 
1242         return urls.toArray(arr);
 
1245     protected static boolean containsMultipleUrls(String restapiUrl) {
 
1246         Matcher m = retryPattern.matcher(restapiUrl);
 
1250     private static class FileParam {
 
1252         public String fileName;
 
1255         public String password;
 
1256         public HttpMethod httpMethod;
 
1257         public String responsePrefix;
 
1258         public boolean skipSending;
 
1259         public String oAuthConsumerKey;
 
1260         public String oAuthConsumerSecret;
 
1261         public String oAuthSignatureMethod;
 
1262         public String oAuthVersion;
 
1263         public AuthType authtype;
 
1266     private static class UebParam {
 
1268         public String topic;
 
1269         public String templateFileName;
 
1270         public String rootVarName;
 
1271         public String responsePrefix;
 
1272         public boolean skipSending;