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.List;
42 import java.util.Map.Entry;
43 import java.util.Properties;
45 import javax.net.ssl.HttpsURLConnection;
46 import javax.net.ssl.KeyManagerFactory;
47 import javax.net.ssl.SSLContext;
48 import javax.ws.rs.ProcessingException;
49 import javax.ws.rs.client.Client;
50 import javax.ws.rs.client.ClientBuilder;
51 import javax.ws.rs.client.Entity;
52 import javax.ws.rs.client.Invocation;
53 import javax.ws.rs.client.WebTarget;
54 import javax.ws.rs.core.EntityTag;
55 import javax.ws.rs.core.Feature;
56 import javax.ws.rs.core.MultivaluedMap;
57 import javax.ws.rs.core.Response;
58 import javax.ws.rs.core.UriBuilder;
59 import org.apache.commons.lang3.StringUtils;
60 import org.glassfish.jersey.client.ClientProperties;
61 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
62 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
63 import org.glassfish.jersey.client.oauth1.ConsumerCredentials;
64 import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
65 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
66 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
67 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
71 public class RestapiCallNode implements SvcLogicJavaPlugin {
73 protected static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";
74 protected static final String UEB_PROPERTIES_FILE_NAME = "ueb.properties";
75 protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
76 protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
78 private static final Logger log = LoggerFactory.getLogger(RestapiCallNode.class);
79 protected RetryPolicyStore retryPolicyStore;
80 private String uebServers;
81 private String defaultUebTemplateFileName = "/opt/bvc/restapi/templates/default-ueb-message.json";
83 private String responseReceivedMessage = "Response received. Time: {}";
84 private String responseHttpCodeMessage = "HTTP response code: {}";
85 private String requestPostingException = "Exception while posting http request to client ";
86 private static String skipSendingMessage = "skipSending";
87 private static String responsePrefix = "responsePrefix";
88 private static String restapiUrlString = "restapiUrl";
90 public RestapiCallNode() {
91 String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
93 try (FileInputStream in = new FileInputStream(configDir + "/" + DME2_PROPERTIES_FILE_NAME)) {
94 Properties props = new Properties();
96 this.retryPolicyStore = new RetryPolicyStore();
97 this.retryPolicyStore.setProxyServers(props.getProperty("proxyUrl"));
98 log.info("DME2 support enabled");
99 } catch (Exception e) {
100 log.warn("DME2 properties could not be read, DME2 support will not be enabled.", e);
103 try (FileInputStream in = new FileInputStream(configDir + "/" + UEB_PROPERTIES_FILE_NAME)) {
104 Properties props = new Properties();
106 this.uebServers = props.getProperty("servers");
107 log.info("UEB support enabled");
108 } catch (Exception e) {
109 log.warn("UEB properties could not be read, UEB support will not be enabled.", e);
114 * Returns parameters from the parameter map.
116 * @param paramMap parameter map
117 * @param p parameters instance
118 * @return parameters filed instance
119 * @throws SvcLogicException when svc logic exception occurs
121 public static Parameters getParameters(Map<String, String> paramMap,
123 throws SvcLogicException {
124 p.templateFileName = parseParam(paramMap, "templateFileName",
126 p.requestBody = parseParam(paramMap, "requestBody", false, null);
127 p.restapiUrl = parseParam(paramMap, restapiUrlString, true, null);
128 validateUrl(p.restapiUrl);
129 p.restapiUser = parseParam(paramMap, "restapiUser", false, null);
130 p.restapiPassword = parseParam(paramMap, "restapiPassword", false,
132 p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey",
134 p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret",
136 p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod",
138 p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
139 p.contentType = parseParam(paramMap, "contentType", false, null);
140 p.format = Format.fromString(parseParam(paramMap, "format", false,
142 p.authtype = fromString(parseParam(paramMap, "authType", false,
144 p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod",
146 p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
147 p.listNameList = getListNameList(paramMap);
148 String skipSendingStr = paramMap.get(skipSendingMessage);
149 p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
150 p.convertResponse = valueOf(parseParam(paramMap, "convertResponse",
152 p.trustStoreFileName = parseParam(paramMap, "trustStoreFileName",
154 p.trustStorePassword = parseParam(paramMap, "trustStorePassword",
156 p.keyStoreFileName = parseParam(paramMap, "keyStoreFileName",
158 p.keyStorePassword = parseParam(paramMap, "keyStorePassword",
160 p.ssl = p.trustStoreFileName != null && p.trustStorePassword != null
161 && p.keyStoreFileName != null && p.keyStorePassword != null;
162 p.customHttpHeaders = parseParam(paramMap, "customHttpHeaders",
164 p.partner = parseParam(paramMap, "partner", false, null);
165 p.dumpHeaders = valueOf(parseParam(paramMap, "dumpHeaders",
167 p.returnRequestPayload = valueOf(parseParam(
168 paramMap, "returnRequestPayload", false, null));
169 p.accept = parseParam(paramMap, "accept",
175 * Validates the given URL in the parameters.
177 * @param restapiUrl rest api URL
178 * @throws SvcLogicException when URL validation fails
180 private static void validateUrl(String restapiUrl)
181 throws SvcLogicException {
183 URI.create(restapiUrl);
184 } catch (IllegalArgumentException e) {
185 throw new SvcLogicException("Invalid input of url "
186 + e.getLocalizedMessage(), e);
191 * Returns the list of list name.
193 * @param paramMap parameters map
194 * @return list of list name
196 private static Set<String> getListNameList(Map<String, String> paramMap) {
197 Set<String> ll = new HashSet<>();
198 for (Map.Entry<String, String> entry : paramMap.entrySet()) {
199 if (entry.getKey().startsWith("listName")) {
200 ll.add(entry.getValue());
207 * Parses the parameter string map of property, validates if required,
208 * assigns default value if present and returns the value.
210 * @param paramMap string param map
211 * @param name name of the property
212 * @param required if value required
213 * @param def default value
214 * @return value of the property
215 * @throws SvcLogicException if required parameter value is empty
217 public static String parseParam(Map<String, String> paramMap, String name,
218 boolean required, String def)
219 throws SvcLogicException {
220 String s = paramMap.get(name);
222 if (s == null || s.trim().length() == 0) {
226 throw new SvcLogicException("Parameter " + name + " is required in RestapiCallNode");
230 StringBuilder value = new StringBuilder();
232 int i1 = s.indexOf('%');
234 int i2 = s.indexOf('%', i1 + 1);
239 String varName = s.substring(i1 + 1, i2);
240 String varValue = System.getenv(varName);
241 if (varValue == null) {
242 varValue = "%" + varName + "%";
245 value.append(s.substring(i, i1));
246 value.append(varValue);
249 i1 = s.indexOf('%', i);
251 value.append(s.substring(i));
253 log.info("Parameter {}: [{}]", name, value);
254 return value.toString();
257 public RetryPolicyStore getRetryPolicyStore() {
258 return retryPolicyStore;
261 public void setRetryPolicyStore(RetryPolicyStore retryPolicyStore) {
262 this.retryPolicyStore = retryPolicyStore;
266 * Allows Directed Graphs the ability to interact with REST APIs.
267 * @param paramMap HashMap<String,String> of parameters passed by the DG to this function
269 * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th><th>example values</th></thead>
271 * <tr><td>templateFileName</td><td>Optional</td><td>full path to template file that can be used to build a request</td><td>/sdncopt/bvc/restapi/templates/vnf_service-configuration-operation_minimal.json</td></tr>
272 * <tr><td>restapiUrl</td><td>Mandatory</td><td>url to send the request to</td><td>https://sdncodl:8543/restconf/operations/L3VNF-API:create-update-vnf-request</td></tr>
273 * <tr><td>restapiUser</td><td>Optional</td><td>user name to use for http basic authentication</td><td>sdnc_ws</td></tr>
274 * <tr><td>restapiPassword</td><td>Optional</td><td>unencrypted password to use for http basic authentication</td><td>plain_password</td></tr>
275 * <tr><td>oAuthConsumerKey</td><td>Optional</td><td>Consumer key to use for http oAuth authentication</td><td>plain_key</td></tr>
276 * <tr><td>oAuthConsumerSecret</td><td>Optional</td><td>Consumer secret to use for http oAuth authentication</td><td>plain_secret</td></tr>
277 * <tr><td>oAuthSignatureMethod</td><td>Optional</td><td>Consumer method to use for http oAuth authentication</td><td>method</td></tr>
278 * <tr><td>oAuthVersion</td><td>Optional</td><td>Version http oAuth authentication</td><td>version</td></tr>
279 * <tr><td>contentType</td><td>Optional</td><td>http content type to set in the http header</td><td>usually application/json or application/xml</td></tr>
280 * <tr><td>format</td><td>Optional</td><td>should match request body format</td><td>json or xml</td></tr>
281 * <tr><td>httpMethod</td><td>Optional</td><td>http method to use when sending the request</td><td>get post put delete patch</td></tr>
282 * <tr><td>responsePrefix</td><td>Optional</td><td>location the response will be written to in context memory</td><td>tmp.restapi.result</td></tr>
283 * <tr><td>listName[i]</td><td>Optional</td><td>Used for processing XML responses with repeating elements.</td>vpn-information.vrf-details<td></td></tr>
284 * <tr><td>skipSending</td><td>Optional</td><td></td><td>true or false</td></tr>
285 * <tr><td>convertResponse </td><td>Optional</td><td>whether the response should be converted</td><td>true or false</td></tr>
286 * <tr><td>customHttpHeaders</td><td>Optional</td><td>a list additional http headers to be passed in, follow the format in the example</td><td>X-CSI-MessageId=messageId,headerFieldName=headerFieldValue</td></tr>
287 * <tr><td>dumpHeaders</td><td>Optional</td><td>when true writes http header content to context memory</td><td>true or false</td></tr>
288 * <tr><td>partner</td><td>Optional</td><td>needed for DME2 calls</td><td>dme2proxy</td></tr>
289 * <tr><td>returnRequestPayload</td><td>Optional</td><td>used to return payload built in the request</td><td>true or false</td></tr>
292 * @param ctx Reference to context memory
293 * @throws SvcLogicException
295 * @see String#split(String, int)
297 public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
298 sendRequest(paramMap, ctx, null);
301 public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx, Integer retryCount)
302 throws SvcLogicException {
304 RetryPolicy retryPolicy = null;
305 HttpResponse r = new HttpResponse();
307 Parameters p = getParameters(paramMap, new Parameters());
308 if (p.partner != null) {
309 retryPolicy = retryPolicyStore.getRetryPolicy(p.partner);
311 String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
314 if (p.templateFileName != null) {
315 String reqTemplate = readFile(p.templateFileName);
316 req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
317 } else if (p.requestBody != null) {
320 r = sendHttpRequest(req, p);
321 setResponseStatus(ctx, p.responsePrefix, r);
323 if (p.dumpHeaders && r.headers != null) {
324 for (Entry<String, List<String>> a : r.headers.entrySet()) {
325 ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
329 if (p.returnRequestPayload && req != null) {
330 ctx.setAttribute(pp + "httpRequest", req);
333 if (r.body != null && r.body.trim().length() > 0) {
334 ctx.setAttribute(pp + "httpResponse", r.body);
336 if (p.convertResponse) {
337 Map<String, String> mm = null;
338 if (p.format == Format.XML) {
339 mm = XmlParser.convertToProperties(r.body, p.listNameList);
340 } else if (p.format == Format.JSON) {
341 mm = JsonParser.convertToProperties(r.body);
345 for (Map.Entry<String, String> entry : mm.entrySet()) {
346 ctx.setAttribute(pp + entry.getKey(), entry.getValue());
351 } catch (SvcLogicException e) {
352 boolean shouldRetry = false;
353 if (e.getCause().getCause() instanceof SocketException) {
357 log.error("Error sending the request: " + e.getMessage(), e);
358 String prefix = parseParam(paramMap, responsePrefix, false, null);
359 if (retryPolicy == null || !shouldRetry) {
360 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
362 if (retryCount == null) {
365 String retryMessage = retryCount + " attempts were made out of " + retryPolicy.getMaximumRetries() +
367 log.debug(retryMessage);
369 retryCount = retryCount + 1;
370 if (retryCount < retryPolicy.getMaximumRetries() + 1) {
371 URI uri = new URI(paramMap.get(restapiUrlString));
372 String hostname = uri.getHost();
373 String retryString = retryPolicy.getNextHostName(uri.toString());
374 URI uriTwo = new URI(retryString);
375 URI retryUri = UriBuilder.fromUri(uri).host(uriTwo.getHost()).port(uriTwo.getPort()).scheme(
376 uriTwo.getScheme()).build();
377 paramMap.put(restapiUrlString, retryUri.toString());
378 log.debug("URL was set to {}", retryUri.toString());
379 log.debug("Failed to communicate with host {}. Request will be re-attempted using the host {}.",
380 hostname, retryString);
381 log.debug("This is retry attempt {} out of {}", retryCount, retryPolicy.getMaximumRetries());
382 sendRequest(paramMap, ctx, retryCount);
384 log.debug("Maximum retries reached, calling setFailureResponseStatus.");
385 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
387 } catch (Exception ex) {
388 log.error("Could not attempt retry.", ex);
389 String retryErrorMessage =
390 "Retry attempt has failed. No further retry shall be attempted, calling " +
391 "setFailureResponseStatus.";
392 setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
397 if (r != null && r.code >= 300) {
398 throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
402 protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format)
403 throws SvcLogicException {
404 log.info("Building {} started", format);
405 long t1 = System.currentTimeMillis();
406 String originalTemplate = template;
408 template = expandRepeats(ctx, template, 1);
410 Map<String, String> mm = new HashMap<>();
411 for (String s : ctx.getAttributeKeySet()) {
412 mm.put(s, ctx.getAttribute(s));
415 StringBuilder ss = new StringBuilder();
417 while (i < template.length()) {
418 int i1 = template.indexOf("${", i);
420 ss.append(template.substring(i));
424 int i2 = template.indexOf('}', i1 + 2);
426 throw new SvcLogicException("Template error: Matching } not found");
429 String var1 = template.substring(i1 + 2, i2);
430 String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
431 if (value1 == null || value1.trim().length() == 0) {
432 // delete the whole element (line)
433 int i3 = template.lastIndexOf('\n', i1);
437 int i4 = template.indexOf('\n', i1);
439 i4 = template.length();
443 ss.append(template.substring(i, i3));
447 ss.append(template.substring(i, i1)).append(value1);
452 String req = format == Format.XML
453 ? XmlJsonUtil.removeEmptyStructXml(ss.toString()) : XmlJsonUtil.removeEmptyStructJson(originalTemplate, ss.toString());
455 if (format == Format.JSON) {
456 req = XmlJsonUtil.removeLastCommaJson(req);
459 long t2 = System.currentTimeMillis();
460 log.info("Building {} completed. Time: {}", format, (t2 - t1));
465 protected String expandRepeats(SvcLogicContext ctx, String template, int level) throws SvcLogicException {
466 StringBuilder newTemplate = new StringBuilder();
468 while (k < template.length()) {
469 int i1 = template.indexOf("${repeat:", k);
471 newTemplate.append(template.substring(k));
475 int i2 = template.indexOf(':', i1 + 9);
477 throw new SvcLogicException(
478 "Template error: Context variable name followed by : is required after repeat");
481 // Find the closing }, store in i3
485 while (nn > 0 && i < template.length()) {
486 i3 = template.indexOf('}', i);
488 throw new SvcLogicException("Template error: Matching } not found");
490 int i32 = template.indexOf('{', i);
491 if (i32 >= 0 && i32 < i3) {
500 String var1 = template.substring(i1 + 9, i2);
501 String value1 = ctx.getAttribute(var1);
502 log.info(" {}:{}", var1, value1);
505 n = Integer.parseInt(value1);
506 } catch (NumberFormatException e) {
507 log.info("value1 not set or not a number, n will remain set at zero");
510 newTemplate.append(template.substring(k, i1));
512 String rpt = template.substring(i2 + 1, i3);
514 for (int ii = 0; ii < n; ii++) {
515 String ss = rpt.replaceAll("\\[\\$\\{" + level + "\\}\\]", "[" + ii + "]");
516 if (ii == n - 1 && ss.trim().endsWith(",")) {
517 int i4 = ss.lastIndexOf(',');
519 ss = ss.substring(0, i4) + ss.substring(i4 + 1);
522 newTemplate.append(ss);
529 return newTemplate.toString();
532 return expandRepeats(ctx, newTemplate.toString(), level + 1);
535 protected String readFile(String fileName) throws SvcLogicException {
537 byte[] encoded = Files.readAllBytes(Paths.get(fileName));
538 return new String(encoded, "UTF-8");
539 } catch (IOException | SecurityException e) {
540 throw new SvcLogicException("Unable to read file " + fileName + e.getLocalizedMessage(), e);
544 protected Client addAuthType(Client c, FileParam fp) throws SvcLogicException {
545 Parameters p = new Parameters();
546 p.restapiUser = fp.user;
547 p.restapiPassword = fp.password;
548 p.oAuthConsumerKey = fp.oAuthConsumerKey;
549 p.oAuthVersion = fp.oAuthVersion;
550 p.oAuthConsumerSecret = fp.oAuthConsumerSecret;
551 p.oAuthSignatureMethod = fp.oAuthSignatureMethod;
552 p.authtype = fp.authtype;
553 return addAuthType(c, p);
556 public Client addAuthType(Client client, Parameters p) throws SvcLogicException {
557 if (p.authtype == AuthType.Unspecified) {
558 if (p.restapiUser != null && p.restapiPassword != null) {
559 client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
560 } else if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null
561 && p.oAuthSignatureMethod != null) {
562 Feature oAuth1Feature = OAuth1ClientSupport
563 .builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
564 .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
565 client.register(oAuth1Feature);
568 if (p.authtype == AuthType.DIGEST) {
569 if (p.restapiUser != null && p.restapiPassword != null) {
570 client.register(HttpAuthenticationFeature.digest(p.restapiUser, p.restapiPassword));
572 throw new SvcLogicException(
573 "oAUTH authentication type selected but all restapiUser and restapiPassword " +
574 "parameters doesn't exist", new Throwable());
576 } else if (p.authtype == AuthType.BASIC) {
577 if (p.restapiUser != null && p.restapiPassword != null) {
578 client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
580 throw new SvcLogicException(
581 "oAUTH authentication type selected but all restapiUser and restapiPassword " +
582 "parameters doesn't exist", new Throwable());
584 } else if (p.authtype == AuthType.OAUTH) {
585 if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
586 Feature oAuth1Feature = OAuth1ClientSupport
587 .builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
588 .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
589 client.register(oAuth1Feature);
591 throw new SvcLogicException(
592 "oAUTH authentication type selected but all oAuthConsumerKey, oAuthConsumerSecret " +
593 "and oAuthSignatureMethod parameters doesn't exist", new Throwable());
601 * Receives the http response for the http request sent.
603 * @param request request msg
604 * @param p parameters
605 * @return HTTP response
606 * @throws SvcLogicException when sending http request fails
608 public HttpResponse sendHttpRequest(String request, Parameters p)
609 throws SvcLogicException {
611 SSLContext ssl = null;
612 if (p.ssl && p.restapiUrl.startsWith("https")) {
613 ssl = createSSLContext(p);
618 HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());
619 client = ClientBuilder.newBuilder().sslContext(ssl).hostnameVerifier((s, sslSession) -> true)
622 client = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true)
625 client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
626 // Needed to support additional HTTP methods such as PATCH
627 client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
629 WebTarget webTarget = addAuthType(client, p).target(p.restapiUrl);
631 log.info("Sending request:");
633 long t1 = System.currentTimeMillis();
635 HttpResponse r = new HttpResponse();
638 if (!p.skipSending) {
639 String accept = p.accept;
641 accept = p.format == Format.XML ? "application/xml" : "application/json";
643 String contentType = p.contentType;
644 if(contentType == null) {
645 contentType = accept + ";charset=UTF-8";
648 Invocation.Builder invocationBuilder = webTarget.request(contentType).accept(accept);
650 if (p.format == Format.NONE) {
651 invocationBuilder.header("", "");
654 if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
655 String[] keyValuePairs = p.customHttpHeaders.split(",");
656 for (String singlePair : keyValuePairs) {
657 int equalPosition = singlePair.indexOf('=');
658 invocationBuilder.header(singlePair.substring(0, equalPosition),
659 singlePair.substring(equalPosition + 1, singlePair.length()));
663 invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
668 response = invocationBuilder.method(p.httpMethod.toString(), entity(request, contentType));
669 } catch (ProcessingException | IllegalStateException e) {
670 throw new SvcLogicException(requestPostingException +
671 e.getLocalizedMessage(), e);
674 r.code = response.getStatus();
675 r.headers = response.getStringHeaders();
676 EntityTag etag = response.getEntityTag();
678 r.message = etag.getValue();
680 if (response.hasEntity() && r.code != 204) {
681 r.body = response.readEntity(String.class);
685 long t2 = System.currentTimeMillis();
686 log.info(responseReceivedMessage, (t2 - t1));
687 log.info(responseHttpCodeMessage, r.code);
688 log.info("HTTP response message: {}", r.message);
689 logHeaders(r.headers);
690 log.info("HTTP response: {}", r.body);
695 protected SSLContext createSSLContext(Parameters p) {
696 try (FileInputStream in = new FileInputStream(p.keyStoreFileName)) {
697 System.setProperty("jsse.enableSNIExtension", "false");
698 System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
699 System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);
701 HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);
703 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
704 KeyStore ks = KeyStore.getInstance("PKCS12");
705 char[] pwd = p.keyStorePassword.toCharArray();
709 SSLContext ctx = SSLContext.getInstance("TLS");
710 ctx.init(kmf.getKeyManagers(), null, null);
712 } catch (Exception e) {
713 log.error("Error creating SSLContext: {}", e.getMessage(), e);
718 protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage,
721 resp.message = errorMessage;
722 String pp = prefix != null ? prefix + '.' : "";
723 ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
724 ctx.setAttribute(pp + "response-message", resp.message);
727 protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
728 String pp = prefix != null ? prefix + '.' : "";
729 ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
730 ctx.setAttribute(pp + "response-message", r.message);
733 public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
734 HttpResponse r = null;
736 FileParam p = getFileParameters(paramMap);
737 byte[] data = Files.readAllBytes(Paths.get(p.fileName));
739 r = sendHttpData(data, p);
740 setResponseStatus(ctx, p.responsePrefix, r);
742 } catch (SvcLogicException | IOException e) {
743 log.error("Error sending the request: {}", e.getMessage(), e);
745 r = new HttpResponse();
747 r.message = e.getMessage();
748 String prefix = parseParam(paramMap, responsePrefix, false, null);
749 setResponseStatus(ctx, prefix, r);
752 if (r != null && r.code >= 300) {
753 throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
757 private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
758 FileParam p = new FileParam();
759 p.fileName = parseParam(paramMap, "fileName", true, null);
760 p.url = parseParam(paramMap, "url", true, null);
761 p.user = parseParam(paramMap, "user", false, null);
762 p.password = parseParam(paramMap, "password", false, null);
763 p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
764 p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
765 String skipSendingStr = paramMap.get(skipSendingMessage);
766 p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
767 p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
768 p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
769 p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
770 p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
771 p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
775 public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
778 UebParam p = getUebParameters(paramMap);
780 String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
784 if (p.templateFileName == null) {
785 log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName);
786 p.templateFileName = defaultUebTemplateFileName;
789 String reqTemplate = readFile(p.templateFileName);
790 reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
791 req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
793 r = postOnUeb(req, p);
794 setResponseStatus(ctx, p.responsePrefix, r);
795 if (r.body != null) {
796 ctx.setAttribute(pp + "httpResponse", r.body);
799 } catch (SvcLogicException e) {
800 log.error("Error sending the request: {}", e.getMessage(), e);
802 r = new HttpResponse();
804 r.message = e.getMessage();
805 String prefix = parseParam(paramMap, responsePrefix, false, null);
806 setResponseStatus(ctx, prefix, r);
810 throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
814 protected HttpResponse sendHttpData(byte[] data, FileParam p) throws SvcLogicException {
816 Client client = ClientBuilder.newBuilder().build();
817 client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
818 client.property(ClientProperties.FOLLOW_REDIRECTS, true);
819 WebTarget webTarget = addAuthType(client, p).target(p.url);
821 log.info("Sending file");
822 long t1 = System.currentTimeMillis();
824 HttpResponse r = new HttpResponse();
827 if (!p.skipSending) {
828 String tt = "application/octet-stream";
829 Invocation.Builder invocationBuilder = webTarget.request(tt).accept(tt);
834 if (p.httpMethod == HttpMethod.POST) {
835 response = invocationBuilder.post(Entity.entity(data, tt));
836 } else if (p.httpMethod == HttpMethod.PUT) {
837 response = invocationBuilder.put(Entity.entity(data, tt));
839 throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
841 } catch (ProcessingException e) {
842 throw new SvcLogicException(requestPostingException +
843 e.getLocalizedMessage(), e);
846 r.code = response.getStatus();
847 r.headers = response.getStringHeaders();
848 EntityTag etag = response.getEntityTag();
850 r.message = etag.getValue();
852 if (response.hasEntity() && r.code != 204) {
853 r.body = response.readEntity(String.class);
857 String newUrl = response.getStringHeaders().getFirst("Location");
859 log.info("Got response code 301. Sending same request to URL: {}", newUrl);
861 webTarget = client.target(newUrl);
862 invocationBuilder = webTarget.request(tt).accept(tt);
865 if (p.httpMethod == HttpMethod.POST) {
866 response = invocationBuilder.post(Entity.entity(data, tt));
867 } else if (p.httpMethod == HttpMethod.PUT) {
868 response = invocationBuilder.put(Entity.entity(data, tt));
870 throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
872 } catch (ProcessingException e) {
873 throw new SvcLogicException(requestPostingException +
874 e.getLocalizedMessage(), e);
877 r.code = response.getStatus();
878 etag = response.getEntityTag();
880 r.message = etag.getValue();
882 if (response.hasEntity() && r.code != 204) {
883 r.body = response.readEntity(String.class);
888 long t2 = System.currentTimeMillis();
889 log.info(responseReceivedMessage, (t2 - t1));
890 log.info(responseHttpCodeMessage, r.code);
891 log.info("HTTP response message: {}", r.message);
892 logHeaders(r.headers);
893 log.info("HTTP response: {}", r.body);
898 private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
899 UebParam p = new UebParam();
900 p.topic = parseParam(paramMap, "topic", true, null);
901 p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
902 p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
903 p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
904 String skipSendingStr = paramMap.get(skipSendingMessage);
905 p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
909 protected void logProperties(Map<String, Object> mm) {
910 List<String> ll = new ArrayList<>();
911 for (Object o : mm.keySet()) {
914 Collections.sort(ll);
916 log.info("Properties:");
917 for (String name : ll) {
918 log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
922 protected void logHeaders(MultivaluedMap<String, String> mm) {
923 log.info("HTTP response headers:");
929 List<String> ll = new ArrayList<>();
930 for (Object o : mm.keySet()) {
933 Collections.sort(ll);
935 for (String name : ll) {
936 log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
940 protected HttpResponse postOnUeb(String request, UebParam p) throws SvcLogicException {
941 String[] urls = uebServers.split(" ");
942 for (int i = 0; i < urls.length; i++) {
943 if (!urls[i].endsWith("/")) {
946 urls[i] += "events/" + p.topic;
949 Client client = ClientBuilder.newBuilder().build();
950 client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
951 WebTarget webTarget = client.target(urls[0]);
953 log.info("UEB URL: {}", urls[0]);
954 log.info("Sending request:");
956 long t1 = System.currentTimeMillis();
958 HttpResponse r = new HttpResponse();
961 if (!p.skipSending) {
962 String tt = "application/json";
963 String tt1 = tt + ";charset=UTF-8";
966 Invocation.Builder invocationBuilder = webTarget.request(tt1).accept(tt);
969 response = invocationBuilder.post(Entity.entity(request, tt1));
970 } catch (ProcessingException e) {
971 throw new SvcLogicException(requestPostingException +
972 e.getLocalizedMessage(), e);
974 r.code = response.getStatus();
975 r.headers = response.getStringHeaders();
976 if (response.hasEntity()) {
977 r.body = response.readEntity(String.class);
981 long t2 = System.currentTimeMillis();
982 log.info(responseReceivedMessage, (t2 - t1));
983 log.info(responseHttpCodeMessage, r.code);
984 logHeaders(r.headers);
985 log.info("HTTP response:\n {}", r.body);
990 public void setUebServers(String uebServers) {
991 this.uebServers = uebServers;
994 public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
995 this.defaultUebTemplateFileName = defaultUebTemplateFileName;
998 private static class FileParam {
1000 public String fileName;
1003 public String password;
1004 public HttpMethod httpMethod;
1005 public String responsePrefix;
1006 public boolean skipSending;
1007 public String oAuthConsumerKey;
1008 public String oAuthConsumerSecret;
1009 public String oAuthSignatureMethod;
1010 public String oAuthVersion;
1011 public AuthType authtype;
1014 private static class UebParam {
1016 public String topic;
1017 public String templateFileName;
1018 public String rootVarName;
1019 public String responsePrefix;
1020 public boolean skipSending;