Implementation for Restconf api call node
[ccsdk/sli/plugins.git] / restapi-call-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / restapicall / RestapiCallNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.plugins.restapicall;
23
24 import com.sun.jersey.api.client.Client;
25 import com.sun.jersey.api.client.ClientHandlerException;
26 import com.sun.jersey.api.client.ClientResponse;
27 import com.sun.jersey.api.client.UniformInterfaceException;
28 import com.sun.jersey.api.client.WebResource;
29 import com.sun.jersey.api.client.config.ClientConfig;
30 import com.sun.jersey.api.client.config.DefaultClientConfig;
31 import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
32 import com.sun.jersey.api.client.filter.HTTPDigestAuthFilter;
33 import com.sun.jersey.client.urlconnection.HTTPSProperties;
34 import com.sun.jersey.oauth.client.OAuthClientFilter;
35 import com.sun.jersey.oauth.signature.OAuthParameters;
36 import com.sun.jersey.oauth.signature.OAuthSecrets;
37 import org.apache.commons.lang3.StringUtils;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import javax.net.ssl.HostnameVerifier;
45 import javax.net.ssl.HttpsURLConnection;
46 import javax.net.ssl.KeyManagerFactory;
47 import javax.net.ssl.SSLContext;
48 import javax.ws.rs.core.EntityTag;
49 import javax.ws.rs.core.MultivaluedMap;
50 import javax.ws.rs.core.UriBuilder;
51 import java.io.FileInputStream;
52 import java.io.IOException;
53 import java.net.SocketException;
54 import java.net.URI;
55 import java.nio.file.Files;
56 import java.nio.file.Paths;
57 import java.security.KeyStore;
58 import java.util.ArrayList;
59 import java.util.Collections;
60 import java.util.HashMap;
61 import java.util.HashSet;
62 import java.util.List;
63 import java.util.Map;
64 import java.util.Map.Entry;
65 import java.util.Properties;
66 import java.util.Set;
67
68 import static java.lang.Boolean.valueOf;
69 import static org.onap.ccsdk.sli.plugins.restapicall.AuthType.fromString;
70
71 public class RestapiCallNode implements SvcLogicJavaPlugin {
72
73     private static final Logger log = LoggerFactory.getLogger(RestapiCallNode.class);
74
75     private String uebServers;
76     private String defaultUebTemplateFileName = "/opt/bvc/restapi/templates/default-ueb-message.json";
77     protected RetryPolicyStore retryPolicyStore;
78     protected static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";
79     protected static final String UEB_PROPERTIES_FILE_NAME = "ueb.properties";
80     protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
81     protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
82
83     public RetryPolicyStore getRetryPolicyStore() {
84         return retryPolicyStore;
85     }
86
87     public void setRetryPolicyStore(RetryPolicyStore retryPolicyStore) {
88         this.retryPolicyStore = retryPolicyStore;
89     }
90
91     public RestapiCallNode() {
92         String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
93
94         try (FileInputStream in = new FileInputStream(configDir + "/" + DME2_PROPERTIES_FILE_NAME)) {
95             Properties props = new Properties();
96             props.load(in);
97             this.retryPolicyStore = new RetryPolicyStore();
98             this.retryPolicyStore.setProxyServers(props.getProperty("proxyUrl"));
99             log.info("DME2 support enabled");
100         } catch (Exception e) {
101             log.warn("DME2 properties could not be read, DME2 support will not be enabled.", e);
102         }
103
104         try (FileInputStream in = new FileInputStream(configDir + "/" + UEB_PROPERTIES_FILE_NAME)) {
105             Properties props = new Properties();
106             props.load(in);
107             this.uebServers = props.getProperty("servers");
108             log.info("UEB support enabled");
109         } catch (Exception e) {
110             log.warn("UEB properties could not be read, UEB support will not be enabled.", e);
111         }
112     }
113
114      /**
115      * Allows Directed Graphs  the ability to interact with REST APIs.
116      * @param paramMap HashMap<String,String> of parameters passed by the DG to this function
117      * <table border="1">
118      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th><th>example values</th></thead>
119      *  <tbody>
120      *      <tr><td>templateFileName</td><td>Optional</td><td>full path to template file that can be used to build a request</td><td>/sdncopt/bvc/restapi/templates/vnf_service-configuration-operation_minimal.json</td></tr>
121      *      <tr><td>restapiUrl</td><td>Mandatory</td><td>url to send the request to</td><td>https://sdncodl:8543/restconf/operations/L3VNF-API:create-update-vnf-request</td></tr>
122      *      <tr><td>restapiUser</td><td>Optional</td><td>user name to use for http basic authentication</td><td>sdnc_ws</td></tr>
123      *      <tr><td>restapiPassword</td><td>Optional</td><td>unencrypted password to use for http basic authentication</td><td>plain_password</td></tr>
124      *      <tr><td>oAuthConsumerKey</td><td>Optional</td><td>Consumer key to use for http oAuth authentication</td><td>plain_key</td></tr>
125      *      <tr><td>oAuthConsumerSecret</td><td>Optional</td><td>Consumer secret to use for http oAuth authentication</td><td>plain_secret</td></tr>
126      *      <tr><td>oAuthSignatureMethod</td><td>Optional</td><td>Consumer method to use for http oAuth authentication</td><td>method</td></tr>
127      *      <tr><td>oAuthVersion</td><td>Optional</td><td>Version http oAuth authentication</td><td>version</td></tr>
128      *      <tr><td>contentType</td><td>Optional</td><td>http content type to set in the http header</td><td>usually application/json or application/xml</td></tr>
129      *      <tr><td>format</td><td>Optional</td><td>should match request body format</td><td>json or xml</td></tr>
130      *      <tr><td>httpMethod</td><td>Optional</td><td>http method to use when sending the request</td><td>get post put delete patch</td></tr>
131      *      <tr><td>responsePrefix</td><td>Optional</td><td>location the response will be written to in context memory</td><td>tmp.restapi.result</td></tr>
132      *      <tr><td>listName[i]</td><td>Optional</td><td>Used for processing XML responses with repeating elements.</td>vpn-information.vrf-details<td></td></tr>
133      *      <tr><td>skipSending</td><td>Optional</td><td></td><td>true or false</td></tr>
134      *      <tr><td>convertResponse </td><td>Optional</td><td>whether the response should be converted</td><td>true or false</td></tr>
135      *      <tr><td>customHttpHeaders</td><td>Optional</td><td>a list additional http headers to be passed in, follow the format in the example</td><td>X-CSI-MessageId=messageId,headerFieldName=headerFieldValue</td></tr>
136      *      <tr><td>dumpHeaders</td><td>Optional</td><td>when true writes http header content to context memory</td><td>true or false</td></tr>
137      *      <tr><td>partner</td><td>Optional</td><td>needed for DME2 calls</td><td>dme2proxy</td></tr>
138      *      <tr><td>returnRequestPayload</td><td>Optional</td><td>used to return payload built in the request</td><td>true or false</td></tr>
139      *  </tbody>
140      * </table>
141      * @param ctx Reference to context memory
142      * @throws SvcLogicException
143      * @since 11.0.2
144      * @see String#split(String, int)
145      */
146     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
147         sendRequest(paramMap, ctx, null);
148     }
149
150     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx, Integer retryCount)
151             throws SvcLogicException {
152
153         RetryPolicy retryPolicy = null;
154         HttpResponse r = new HttpResponse();
155         try {
156             Parameters p = getParameters(paramMap, new Parameters());
157             if (p.partner != null) {
158                 retryPolicy = retryPolicyStore.getRetryPolicy(p.partner);
159             }
160             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
161
162             String req = null;
163             if (p.templateFileName != null) {
164                 String reqTemplate = readFile(p.templateFileName);
165                 req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
166             } else if (p.requestBody != null) {
167                 req = p.requestBody;
168             }
169             r = sendHttpRequest(req, p);
170             setResponseStatus(ctx, p.responsePrefix, r);
171
172             if (p.dumpHeaders && r.headers != null) {
173                 for (Entry<String, List<String>> a : r.headers.entrySet()) {
174                     ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
175                 }
176             }
177             
178             if (p.returnRequestPayload && req != null) {
179                 ctx.setAttribute(pp + "httpRequest", req);
180             }
181
182             if (r.body != null && r.body.trim().length() > 0) {
183                 ctx.setAttribute(pp + "httpResponse", r.body);
184
185                 if (p.convertResponse) {
186                     Map<String, String> mm = null;
187                     if (p.format == Format.XML)
188                         mm = XmlParser.convertToProperties(r.body, p.listNameList);
189                     else if (p.format == Format.JSON)
190                         mm = JsonParser.convertToProperties(r.body);
191
192                     if (mm != null)
193                         for (Map.Entry<String,String> entry : mm.entrySet())
194                             ctx.setAttribute(pp + entry.getKey(), entry.getValue());
195                 }
196             }
197         } catch (SvcLogicException e) {
198             boolean shouldRetry = false;
199             if (e.getCause().getCause() instanceof SocketException) {
200                 shouldRetry = true;
201             }
202
203             log.error("Error sending the request: " + e.getMessage(), e);
204             String prefix = parseParam(paramMap, "responsePrefix", false, null);
205             if (retryPolicy == null || shouldRetry == false) {
206                 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
207             } else {
208                 if (retryCount == null) {
209                     retryCount = 0;
210                 }
211                 String retryMessage = retryCount + " attempts were made out of " + retryPolicy.getMaximumRetries() +
212                         " maximum retries.";
213                 log.debug(retryMessage);
214                 try {
215                     retryCount = retryCount + 1;
216                     if (retryCount < retryPolicy.getMaximumRetries() + 1) {
217                         URI uri = new URI(paramMap.get("restapiUrl"));
218                         String hostname = uri.getHost();
219                         String retryString = retryPolicy.getNextHostName(uri.toString());
220                         URI uriTwo = new URI(retryString);
221                         URI retryUri = UriBuilder.fromUri(uri).host(uriTwo.getHost()).port(uriTwo.getPort()).scheme(
222                                 uriTwo.getScheme()).build();
223                         paramMap.put("restapiUrl", retryUri.toString());
224                         log.debug("URL was set to {}", retryUri.toString());
225                         log.debug("Failed to communicate with host {}. Request will be re-attempted using the host {}.",
226                             hostname, retryString);
227                         log.debug("This is retry attempt {} out of {}", retryCount, retryPolicy.getMaximumRetries());
228                         sendRequest(paramMap, ctx, retryCount);
229                     } else {
230                         log.debug("Maximum retries reached, calling setFailureResponseStatus.");
231                         setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
232                     }
233                 } catch (Exception ex) {
234                     log.error("Could not attempt retry.", ex);
235                     String retryErrorMessage =
236                             "Retry attempt has failed. No further retry shall be attempted, calling " +
237                                 "setFailureResponseStatus.";
238                     setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
239                 }
240             }
241         }
242
243         if (r != null && r.code >= 300)
244             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
245     }
246
247     /**
248      * Returns parameters from the parameter map.
249      *
250      * @param paramMap parameter map
251      * @param p        parameters instance
252      * @return parameters filed instance
253      * @throws SvcLogicException when svc logic exception occurs
254      */
255     public static Parameters getParameters(Map<String, String> paramMap,
256                                            Parameters p)
257             throws SvcLogicException {
258         p.templateFileName = parseParam(paramMap, "templateFileName",
259                                         false, null);
260         p.requestBody = parseParam(paramMap, "requestBody", false, null);
261         p.restapiUrl = parseParam(paramMap, "restapiUrl", true, null);
262         validateUrl(p.restapiUrl);
263         p.restapiUser = parseParam(paramMap, "restapiUser", false, null);
264         p.restapiPassword = parseParam(paramMap, "restapiPassword", false,
265                                        null);
266         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey",
267                                         false, null);
268         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret",
269                                            false, null);
270         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod",
271                                             false, null);
272         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
273         p.contentType = parseParam(paramMap, "contentType", false, null);
274         p.format = Format.fromString(parseParam(paramMap, "format", false,
275                                                 "json"));
276         p.authtype = fromString(parseParam(paramMap, "authType", false,
277                                            "unspecified"));
278         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod",
279                                                         false, "post"));
280         p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
281         p.listNameList = getListNameList(paramMap);
282         String skipSendingStr = paramMap.get("skipSending");
283         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
284         p.convertResponse = valueOf(parseParam(paramMap, "convertResponse",
285                                                false, "true"));
286         p.trustStoreFileName = parseParam(paramMap, "trustStoreFileName",
287                                           false, null);
288         p.trustStorePassword = parseParam(paramMap, "trustStorePassword",
289                                           false, null);
290         p.keyStoreFileName = parseParam(paramMap, "keyStoreFileName",
291                                         false, null);
292         p.keyStorePassword = parseParam(paramMap, "keyStorePassword",
293                                         false, null);
294         p.ssl = p.trustStoreFileName != null && p.trustStorePassword != null
295                 && p.keyStoreFileName != null && p.keyStorePassword != null;
296         p.customHttpHeaders = parseParam(paramMap, "customHttpHeaders",
297                                          false, null);
298         p.partner = parseParam(paramMap, "partner", false, null);
299         p.dumpHeaders = valueOf(parseParam(paramMap, "dumpHeaders",
300                                            false, null));
301         p.returnRequestPayload = valueOf(parseParam(
302                 paramMap, "returnRequestPayload", false, null));
303         return p;
304     }
305
306     /**
307      * Validates the given URL in the parameters.
308      *
309      * @param restapiUrl rest api URL
310      * @throws SvcLogicException when URL validation fails
311      */
312     private static void validateUrl(String restapiUrl)
313             throws SvcLogicException {
314         try {
315             URI.create(restapiUrl);
316         } catch (IllegalArgumentException e) {
317             throw new SvcLogicException("Invalid input of url "
318                                                 + e.getLocalizedMessage(), e);
319         }
320     }
321
322     /**
323      * Returns the list of list name.
324      *
325      * @param paramMap parameters map
326      * @return list of list name
327      */
328     private static Set<String> getListNameList(Map<String, String> paramMap) {
329         Set<String> ll = new HashSet<>();
330         for (Map.Entry<String,String> entry : paramMap.entrySet())
331             if (entry.getKey().startsWith("listName"))
332                 ll.add(entry.getValue());
333         return ll;
334     }
335
336     /**
337      * Parses the parameter string map of property, validates if required,
338      * assigns default value if present and returns the value.
339      *
340      * @param paramMap string param map
341      * @param name     name of the property
342      * @param required if value required
343      * @param def      default value
344      * @return value of the property
345      * @throws SvcLogicException if required parameter value is empty
346      */
347     public static String parseParam(Map<String, String> paramMap, String name,
348                                     boolean required, String def)
349             throws SvcLogicException {
350         String s = paramMap.get(name);
351
352         if (s == null || s.trim().length() == 0) {
353             if (!required)
354                 return def;
355             throw new SvcLogicException("Parameter " + name + " is required in RestapiCallNode");
356         }
357
358         s = s.trim();
359         StringBuilder value = new StringBuilder();
360         int i = 0;
361         int i1 = s.indexOf('%');
362         while (i1 >= 0) {
363             int i2 = s.indexOf('%', i1 + 1);
364             if (i2 < 0)
365                 break;
366
367             String varName = s.substring(i1 + 1, i2);
368             String varValue = System.getenv(varName);
369             if (varValue == null)
370                 varValue = "%" + varName + "%";
371
372             value.append(s.substring(i, i1));
373             value.append(varValue);
374
375             i = i2 + 1;
376             i1 = s.indexOf('%', i);
377         }
378         value.append(s.substring(i));
379
380         log.info("Parameter {}: [{}]", name, value);
381         return value.toString();
382     }
383
384     protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format)
385         throws SvcLogicException {
386         log.info("Building {} started", format);
387         long t1 = System.currentTimeMillis();
388
389         template = expandRepeats(ctx, template, 1);
390
391         Map<String, String> mm = new HashMap<>();
392         for (String s : ctx.getAttributeKeySet())
393             mm.put(s, ctx.getAttribute(s));
394
395         StringBuilder ss = new StringBuilder();
396         int i = 0;
397         while (i < template.length()) {
398             int i1 = template.indexOf("${", i);
399             if (i1 < 0) {
400                 ss.append(template.substring(i));
401                 break;
402             }
403
404             int i2 = template.indexOf('}', i1 + 2);
405             if (i2 < 0)
406                 throw new SvcLogicException("Template error: Matching } not found");
407
408             String var1 = template.substring(i1 + 2, i2);
409             String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
410             // log.info(" " + var1 + ": " + value1);
411             if (value1 == null || value1.trim().length() == 0) {
412                 // delete the whole element (line)
413                 int i3 = template.lastIndexOf('\n', i1);
414                 if (i3 < 0)
415                     i3 = 0;
416                 int i4 = template.indexOf('\n', i1);
417                 if (i4 < 0)
418                     i4 = template.length();
419
420                 if (i < i3)
421                     ss.append(template.substring(i, i3));
422                 i = i4;
423             } else {
424                 ss.append(template.substring(i, i1)).append(value1);
425                 i = i2 + 1;
426             }
427         }
428
429         String req = format == Format.XML
430                 ? XmlJsonUtil.removeEmptyStructXml(ss.toString()) : XmlJsonUtil.removeEmptyStructJson(ss.toString());
431
432         if (format == Format.JSON)
433             req = XmlJsonUtil.removeLastCommaJson(req);
434
435         long t2 = System.currentTimeMillis();
436         log.info("Building {} completed. Time: {}", format, (t2 - t1));
437
438         return req;
439     }
440
441     protected String expandRepeats(SvcLogicContext ctx, String template, int level) throws SvcLogicException {
442         StringBuilder newTemplate = new StringBuilder();
443         int k = 0;
444         while (k < template.length()) {
445             int i1 = template.indexOf("${repeat:", k);
446             if (i1 < 0) {
447                 newTemplate.append(template.substring(k));
448                 break;
449             }
450
451             int i2 = template.indexOf(':', i1 + 9);
452             if (i2 < 0)
453                 throw new SvcLogicException(
454                         "Template error: Context variable name followed by : is required after repeat");
455
456             // Find the closing }, store in i3
457             int nn = 1;
458             int i3 = -1;
459             int i = i2;
460             while (nn > 0 && i < template.length()) {
461                 i3 = template.indexOf('}', i);
462                 if (i3 < 0)
463                     throw new SvcLogicException("Template error: Matching } not found");
464                 int i32 = template.indexOf('{', i);
465                 if (i32 >= 0 && i32 < i3) {
466                     nn++;
467                     i = i32 + 1;
468                 } else {
469                     nn--;
470                     i = i3 + 1;
471                 }
472             }
473
474             String var1 = template.substring(i1 + 9, i2);
475             String value1 = ctx.getAttribute(var1);
476             log.info("     {}:{}", var1, value1);
477             int n = 0;
478             try {
479                 n = Integer.parseInt(value1);
480             } catch (NumberFormatException e) {
481                 log.info("value1 not set or not a number, n will remain set at zero");
482             }
483
484             newTemplate.append(template.substring(k, i1));
485
486             String rpt = template.substring(i2 + 1, i3);
487
488             for (int ii = 0; ii < n; ii++) {
489                 String ss = rpt.replaceAll("\\[\\$\\{" + level + "\\}\\]", "[" + ii + "]");
490                 if (ii == n - 1 && ss.trim().endsWith(",")) {
491                     int i4 = ss.lastIndexOf(',');
492                     if (i4 > 0)
493                         ss = ss.substring(0, i4) + ss.substring(i4 + 1);
494                 }
495                 newTemplate.append(ss);
496             }
497
498             k = i3 + 1;
499         }
500
501         if (k == 0)
502             return newTemplate.toString();
503
504         return expandRepeats(ctx, newTemplate.toString(), level + 1);
505     }
506
507     protected String readFile(String fileName) throws SvcLogicException {
508         try {
509             byte[] encoded = Files.readAllBytes(Paths.get(fileName));
510             return new String(encoded, "UTF-8");
511         } catch (IOException | SecurityException e) {
512             throw new SvcLogicException("Unable to read file " + fileName + e.getLocalizedMessage(), e);
513         }
514     }
515
516     protected Client addAuthType(Client c, FileParam fp) throws SvcLogicException {
517         Parameters p = new Parameters();
518         p.restapiUser = fp.user;
519         p.restapiPassword = fp.password;
520         p.oAuthConsumerKey = fp.oAuthConsumerKey;
521         p.oAuthVersion = fp.oAuthVersion;
522         p.oAuthConsumerSecret = fp.oAuthConsumerSecret;
523         p.oAuthSignatureMethod = fp.oAuthSignatureMethod;
524         p.authtype = fp.authtype;
525         return addAuthType(c,p);
526     }
527
528     protected Client addAuthType(Client client, Parameters p) throws SvcLogicException {
529         if (p.authtype == AuthType.Unspecified){
530             if (p.restapiUser != null && p.restapiPassword != null)
531                 client.addFilter(new HTTPBasicAuthFilter(p.restapiUser, p.restapiPassword));
532             else if(p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null
533                     && p.oAuthSignatureMethod != null) {
534                 OAuthParameters params = new OAuthParameters()
535                         .signatureMethod(p.oAuthSignatureMethod)
536                         .consumerKey(p.oAuthConsumerKey)
537                         .version(p.oAuthVersion);
538
539                 OAuthSecrets secrets = new OAuthSecrets()
540                         .consumerSecret(p.oAuthConsumerSecret);
541                 client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets));
542             }
543         } else {
544             if (p.authtype == AuthType.DIGEST) {
545                 if (p.restapiUser != null && p.restapiPassword != null) {
546                     client.addFilter(new HTTPDigestAuthFilter(p.restapiUser, p.restapiPassword));
547                 } else {
548                     throw new SvcLogicException("oAUTH authentication type selected but all restapiUser and restapiPassword " +
549                                                         "parameters doesn't exist", new Throwable());
550                 }
551             } else if (p.authtype == AuthType.BASIC){
552                 if (p.restapiUser != null && p.restapiPassword != null) {
553                     client.addFilter(new HTTPBasicAuthFilter(p.restapiUser, p.restapiPassword));
554                 } else {
555                     throw new SvcLogicException("oAUTH authentication type selected but all restapiUser and restapiPassword " +
556                                                         "parameters doesn't exist", new Throwable());
557                 }
558             } else if(p.authtype == AuthType.OAUTH ) {
559                 if(p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
560                     OAuthParameters params = new OAuthParameters()
561                             .signatureMethod(p.oAuthSignatureMethod)
562                             .consumerKey(p.oAuthConsumerKey)
563                             .version(p.oAuthVersion);
564
565                     OAuthSecrets secrets = new OAuthSecrets()
566                             .consumerSecret(p.oAuthConsumerSecret);
567                     client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets));
568                 } else {
569                     throw new SvcLogicException("oAUTH authentication type selected but all oAuthConsumerKey, oAuthConsumerSecret " +
570                                                         "and oAuthSignatureMethod parameters doesn't exist", new Throwable());
571                 }
572             }
573         }
574         return client;
575     }
576
577     /**
578      * Receives the http response for the http request sent.
579      *
580      * @param request request msg
581      * @param p       parameters
582      * @return HTTP response
583      * @throws SvcLogicException when sending http request fails
584      */
585     public HttpResponse sendHttpRequest(String request, Parameters p)
586             throws SvcLogicException {
587
588         ClientConfig config = new DefaultClientConfig();
589         SSLContext ssl = null;
590         if (p.ssl && p.restapiUrl.startsWith("https"))
591             ssl = createSSLContext(p);
592         if (ssl != null) {
593             HostnameVerifier hostnameVerifier = (hostname, session) -> true;
594
595             config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
596                     new HTTPSProperties(hostnameVerifier, ssl));
597         }
598
599         logProperties(config.getProperties());
600
601         Client client = Client.create(config);
602         client.setConnectTimeout(5000);
603         WebResource webResource = addAuthType(client,p).resource(p.restapiUrl);
604
605         log.info("Sending request:");
606         log.info(request);
607         long t1 = System.currentTimeMillis();
608
609         HttpResponse r = new HttpResponse();
610         r.code = 200;
611
612         if (!p.skipSending) {
613             String tt = p.format == Format.XML ? "application/xml" : "application/json";
614             String tt1 = tt + ";charset=UTF-8";
615             if (p.contentType != null) {
616                 tt = p.contentType;
617                 tt1 = p.contentType;
618             }
619
620             WebResource.Builder webResourceBuilder = webResource.accept(tt).type(tt1);
621             if(p.format == Format.NONE){
622                 webResourceBuilder = webResource.header("","");
623             }
624
625             if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
626                 String[] keyValuePairs = p.customHttpHeaders.split(",");
627                 for (String singlePair : keyValuePairs) {
628                     int equalPosition = singlePair.indexOf('=');
629                     webResourceBuilder.header(singlePair.substring(0, equalPosition),
630                             singlePair.substring(equalPosition + 1, singlePair.length()));
631                 }
632             }
633
634             webResourceBuilder.header("X-ECOMP-RequestID",org.slf4j.MDC.get("X-ECOMP-RequestID"));
635
636             ClientResponse response;
637
638             try {
639                 response = webResourceBuilder.method(p.httpMethod.toString(), ClientResponse.class, request);
640             } catch (UniformInterfaceException | ClientHandlerException e) {
641                 throw new SvcLogicException("Exception while sending http request to client "
642                     + e.getLocalizedMessage(), e);
643             }
644
645             r.code = response.getStatus();
646             r.headers = response.getHeaders();
647             EntityTag etag = response.getEntityTag();
648             if (etag != null)
649                 r.message = etag.getValue();
650             if (response.hasEntity() && r.code != 204)
651                 r.body = response.getEntity(String.class);
652         }
653
654         long t2 = System.currentTimeMillis();
655         log.info("Response received. Time: {}", (t2 - t1));
656         log.info("HTTP response code: {}", r.code);
657         log.info("HTTP response message: {}", r.message);
658         logHeaders(r.headers);
659         log.info("HTTP response: {}", r.body);
660
661         return r;
662     }
663
664     protected SSLContext createSSLContext(Parameters p) {
665         try (FileInputStream in = new FileInputStream(p.keyStoreFileName)) {
666             System.setProperty("jsse.enableSNIExtension", "false");
667             System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
668             System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);
669
670             HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);
671
672             KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
673             KeyStore ks = KeyStore.getInstance("PKCS12");
674             char[] pwd = p.keyStorePassword.toCharArray();
675             ks.load(in, pwd);
676             kmf.init(ks, pwd);
677
678             SSLContext ctx = SSLContext.getInstance("TLS");
679             ctx.init(kmf.getKeyManagers(), null, null);
680             return ctx;
681         } catch (Exception e) {
682             log.error("Error creating SSLContext: {}", e.getMessage(), e);
683         }
684         return null;
685     }
686
687     protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage,
688         HttpResponse resp) {
689         resp.code = 500;
690         resp.message = errorMessage;
691         String pp = prefix != null ? prefix + '.' : "";
692         ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
693         ctx.setAttribute(pp + "response-message", resp.message);
694     }
695
696     protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
697         String pp = prefix != null ? prefix + '.' : "";
698         ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
699         ctx.setAttribute(pp + "response-message", r.message);
700     }
701
702     public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
703         HttpResponse r = null;
704         try {
705             FileParam p = getFileParameters(paramMap);
706             byte[] data = Files.readAllBytes(Paths.get(p.fileName));
707
708             r = sendHttpData(data, p);
709             setResponseStatus(ctx, p.responsePrefix, r);
710
711         } catch (SvcLogicException | IOException e) {
712             log.error("Error sending the request: {}", e.getMessage(), e);
713
714             r = new HttpResponse();
715             r.code = 500;
716             r.message = e.getMessage();
717             String prefix = parseParam(paramMap, "responsePrefix", false, null);
718             setResponseStatus(ctx, prefix, r);
719         }
720
721         if (r != null && r.code >= 300)
722             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
723     }
724
725     private static class FileParam {
726
727         public String fileName;
728         public String url;
729         public String user;
730         public String password;
731         public HttpMethod httpMethod;
732         public String responsePrefix;
733         public boolean skipSending;
734         public String oAuthConsumerKey;
735         public String oAuthConsumerSecret;
736         public String oAuthSignatureMethod;
737         public String oAuthVersion;
738         public AuthType authtype;
739     }
740
741     private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
742         FileParam p = new FileParam();
743         p.fileName = parseParam(paramMap, "fileName", true, null);
744         p.url = parseParam(paramMap, "url", true, null);
745         p.user = parseParam(paramMap, "user", false, null);
746         p.password = parseParam(paramMap, "password", false, null);
747         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
748         p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
749         String skipSendingStr = paramMap.get("skipSending");
750         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
751         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
752         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
753         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
754         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
755         p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
756         return p;
757     }
758
759     protected HttpResponse sendHttpData(byte[] data, FileParam p) throws SvcLogicException {
760         Client client = Client.create();
761         client.setConnectTimeout(5000);
762         client.setFollowRedirects(true);
763         WebResource webResource = addAuthType(client,p).resource(p.url);
764
765         log.info("Sending file");
766         long t1 = System.currentTimeMillis();
767
768         HttpResponse r = new HttpResponse();
769         r.code = 200;
770
771         if (!p.skipSending) {
772             String tt = "application/octet-stream";
773
774             ClientResponse response;
775             try {
776                 if (p.httpMethod == HttpMethod.POST)
777                     response = webResource.accept(tt).type(tt).post(ClientResponse.class, data);
778                 else if (p.httpMethod == HttpMethod.PUT)
779                     response = webResource.accept(tt).type(tt).put(ClientResponse.class, data);
780                 else
781                     throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
782             } catch (UniformInterfaceException | ClientHandlerException e) {
783                 throw new SvcLogicException("Exception while sending http request to client " +
784                     e.getLocalizedMessage(), e);
785             }
786
787             r.code = response.getStatus();
788             r.headers = response.getHeaders();
789             EntityTag etag = response.getEntityTag();
790             if (etag != null)
791                 r.message = etag.getValue();
792             if (response.hasEntity() && r.code != 204)
793                 r.body = response.getEntity(String.class);
794
795             if (r.code == 301) {
796                 String newUrl = response.getHeaders().getFirst("Location");
797
798                 log.info("Got response code 301. Sending same request to URL: {}", newUrl);
799
800                 webResource = client.resource(newUrl);
801
802                 try {
803                     if (p.httpMethod == HttpMethod.POST)
804                         response = webResource.accept(tt).type(tt).post(ClientResponse.class, data);
805                     else if (p.httpMethod == HttpMethod.PUT)
806                         response = webResource.accept(tt).type(tt).put(ClientResponse.class, data);
807                     else
808                         throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
809                 } catch (UniformInterfaceException | ClientHandlerException e) {
810                     throw new SvcLogicException("Exception while sending http request to client " +
811                         e.getLocalizedMessage(), e);
812                 }
813
814                 r.code = response.getStatus();
815                 etag = response.getEntityTag();
816                 if (etag != null)
817                     r.message = etag.getValue();
818                 if (response.hasEntity() && r.code != 204)
819                     r.body = response.getEntity(String.class);
820             }
821         }
822
823         long t2 = System.currentTimeMillis();
824         log.info("Response received. Time: {}", (t2 - t1));
825         log.info("HTTP response code: {}", r.code);
826         log.info("HTTP response message: {}", r.message);
827         logHeaders(r.headers);
828         log.info("HTTP response: {}", r.body);
829
830         return r;
831     }
832
833     public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
834         HttpResponse r;
835         try {
836             UebParam p = getUebParameters(paramMap);
837
838             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
839
840             String req;
841
842             if (p.templateFileName == null) {
843                 log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName);
844                 p.templateFileName = defaultUebTemplateFileName;
845             }
846
847             String reqTemplate = readFile(p.templateFileName);
848             reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
849             req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
850
851             r = postOnUeb(req, p);
852             setResponseStatus(ctx, p.responsePrefix, r);
853             if (r.body != null)
854                 ctx.setAttribute(pp + "httpResponse", r.body);
855
856         } catch (SvcLogicException e) {
857             log.error("Error sending the request: {}", e.getMessage(), e);
858
859             r = new HttpResponse();
860             r.code = 500;
861             r.message = e.getMessage();
862             String prefix = parseParam(paramMap, "responsePrefix", false, null);
863             setResponseStatus(ctx, prefix, r);
864         }
865
866         if (r.code >= 300)
867             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
868     }
869
870     private static class UebParam {
871
872         public String topic;
873         public String templateFileName;
874         public String rootVarName;
875         public String responsePrefix;
876         public boolean skipSending;
877     }
878
879     private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
880         UebParam p = new UebParam();
881         p.topic = parseParam(paramMap, "topic", true, null);
882         p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
883         p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
884         p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
885         String skipSendingStr = paramMap.get("skipSending");
886         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
887         return p;
888     }
889
890     protected HttpResponse postOnUeb(String request, UebParam p) throws SvcLogicException {
891         String[] urls = uebServers.split(" ");
892         for (int i = 0; i < urls.length; i++) {
893             if (!urls[i].endsWith("/"))
894                 urls[i] += "/";
895             urls[i] += "events/" + p.topic;
896         }
897
898         Client client = Client.create();
899         client.setConnectTimeout(5000);
900         WebResource webResource = client.resource(urls[0]);
901
902         log.info("UEB URL: {}", urls[0]);
903         log.info("Sending request:");
904         log.info(request);
905         long t1 = System.currentTimeMillis();
906
907         HttpResponse r = new HttpResponse();
908         r.code = 200;
909
910         if (!p.skipSending) {
911             String tt = "application/json";
912             String tt1 = tt + ";charset=UTF-8";
913
914             ClientResponse response;
915
916             try {
917                 response = webResource.accept(tt).type(tt1).post(ClientResponse.class, request);
918             } catch (UniformInterfaceException | ClientHandlerException e) {
919                 throw new SvcLogicException("Exception while posting http request to client " +
920                     e.getLocalizedMessage(), e);
921             }
922
923             r.code = response.getStatus();
924             r.headers = response.getHeaders();
925             if (response.hasEntity())
926                 r.body = response.getEntity(String.class);
927         }
928
929         long t2 = System.currentTimeMillis();
930         log.info("Response received. Time: {}", (t2 - t1));
931         log.info("HTTP response code: {}", r.code);
932         logHeaders(r.headers);
933         log.info("HTTP response:\n {}", r.body);
934
935         return r;
936     }
937
938     protected void logProperties(Map<String, Object> mm) {
939         List<String> ll = new ArrayList<>();
940         for (Object o : mm.keySet())
941             ll.add((String) o);
942         Collections.sort(ll);
943
944         log.info("Properties:");
945         for (String name : ll)
946             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
947     }
948
949     protected void logHeaders(MultivaluedMap<String, String> mm) {
950         log.info("HTTP response headers:");
951
952         if (mm == null)
953             return;
954
955         List<String> ll = new ArrayList<>();
956         for (Object o : mm.keySet())
957             ll.add((String) o);
958         Collections.sort(ll);
959
960         for (String name : ll)
961             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
962     }
963
964     public void setUebServers(String uebServers) {
965         this.uebServers = uebServers;
966     }
967
968     public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
969         this.defaultUebTemplateFileName = defaultUebTemplateFileName;
970     }
971 }