Remove logging passwords in clear text
[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  * 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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.ccsdk.sli.plugins.restapicall;
24
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.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.net.SocketException;
32 import java.net.URI;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.security.KeyStore;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Map.Entry;
44 import java.util.Properties;
45 import java.util.Set;
46 import javax.net.ssl.HttpsURLConnection;
47 import javax.net.ssl.KeyManagerFactory;
48 import javax.net.ssl.SSLContext;
49 import javax.ws.rs.ProcessingException;
50 import javax.ws.rs.client.Client;
51 import javax.ws.rs.client.ClientBuilder;
52 import javax.ws.rs.client.Entity;
53 import javax.ws.rs.client.Invocation;
54 import javax.ws.rs.client.WebTarget;
55 import javax.ws.rs.core.EntityTag;
56 import javax.ws.rs.core.Feature;
57 import javax.ws.rs.core.MediaType;
58 import javax.ws.rs.core.MultivaluedMap;
59 import javax.ws.rs.core.Response;
60 import org.apache.commons.lang3.StringUtils;
61 import org.codehaus.jettison.json.JSONException;
62 import org.codehaus.jettison.json.JSONObject;
63 import org.glassfish.jersey.client.ClientProperties;
64 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
65 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
66 import org.glassfish.jersey.client.oauth1.ConsumerCredentials;
67 import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
68 import org.glassfish.jersey.media.multipart.MultiPart;
69 import org.glassfish.jersey.media.multipart.MultiPartFeature;
70 import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
71 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
72 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
73 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76
77 public class RestapiCallNode implements SvcLogicJavaPlugin {
78
79     protected static final String PARTNERS_FILE_NAME = "partners.json";
80     protected static final String UEB_PROPERTIES_FILE_NAME = "ueb.properties";
81     protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
82     protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
83
84     private static final Logger log = LoggerFactory.getLogger(RestapiCallNode.class);
85     private String uebServers;
86     private String defaultUebTemplateFileName = "/opt/bvc/restapi/templates/default-ueb-message.json";
87
88     private String responseReceivedMessage = "Response received. Time: {}";
89     private String responseHttpCodeMessage = "HTTP response code: {}";
90     private String requestPostingException = "Exception while posting http request to client ";
91     protected static final String skipSendingMessage = "skipSending";
92     protected static final String responsePrefix = "responsePrefix";
93     protected static final String restapiUrlString = "restapiUrl";
94     protected static final String restapiUserKey = "restapiUser";
95     protected static final String restapiPasswordKey = "restapiPassword";
96
97     protected HashMap<String, PartnerDetails> partnerStore;
98
99     public RestapiCallNode() {
100         String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
101         try {
102             String jsonString = readFile(configDir + "/" + PARTNERS_FILE_NAME);
103             JSONObject partners = new JSONObject(jsonString);
104             partnerStore = new HashMap<>();
105             loadPartners(partners);
106             log.info("Partners support enabled");
107         } catch (Exception e) {
108             log.warn("Partners file could not be read, Partner support will not be enabled.", e);
109         }
110
111         try (FileInputStream in = new FileInputStream(configDir + "/" + UEB_PROPERTIES_FILE_NAME)) {
112             Properties props = new Properties();
113             props.load(in);
114             uebServers = props.getProperty("servers");
115             log.info("UEB support enabled");
116         } catch (Exception e) {
117             log.warn("UEB properties could not be read, UEB support will not be enabled.", e);
118         }
119     }
120
121     protected void loadPartners(JSONObject partners) {
122         Iterator<String> keys = partners.keys();
123         String partnerUserKey = "user";
124         String partnerPasswordKey = "password";
125         String partnerUrlKey = "url";
126
127         while (keys.hasNext()) {
128             String partnerKey = keys.next();
129             try {
130                 JSONObject partnerObject = (JSONObject) partners.get(partnerKey);
131                 if (partnerObject.has(partnerUserKey) && partnerObject.has(partnerPasswordKey)) {
132                     String url = null;
133                     if (partnerObject.has(partnerUrlKey)) {
134                         url = partnerObject.getString(partnerUrlKey);
135                     }
136                     String userName = partnerObject.getString(partnerUserKey);
137                     String password = partnerObject.getString(partnerPasswordKey);
138                     PartnerDetails details = new PartnerDetails(userName, password, url);
139                     partnerStore.put(partnerKey, details);
140                     log.info("mapped partner using partner key " + partnerKey);
141                 } else {
142                     log.info("Partner " + partnerKey + " is missing required keys, it won't be mapped");
143                 }
144             } catch (JSONException e) {
145                 log.info("Couldn't map the partner using partner key " + partnerKey, e);
146             }
147         }
148     }
149
150     /**
151      * Returns parameters from the parameter map.
152      *
153      * @param paramMap parameter map
154      * @param p parameters instance
155      * @return parameters filed instance
156      * @throws SvcLogicException when svc logic exception occurs
157      */
158     public static Parameters getParameters(Map<String, String> paramMap, Parameters p) throws SvcLogicException {
159
160         p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
161         p.requestBody = parseParam(paramMap, "requestBody", false, null);
162         p.restapiUrl = parseParam(paramMap, restapiUrlString, true, null);
163         validateUrl(p.restapiUrl);
164         p.restapiUrlSuffix = parseParam(paramMap, "restapiUrlSuffix", false, null);
165         p.restapiUser = parseParam(paramMap, restapiUserKey, false, null);
166         p.restapiPassword = parseParam(paramMap, restapiPasswordKey, false, null);
167         if (p.restapiUrlSuffix != null) {
168             p.restapiUrl = p.restapiUrl + p.restapiUrlSuffix;
169             validateUrl(p.restapiUrl);
170         }
171         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
172         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
173         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
174         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
175         p.contentType = parseParam(paramMap, "contentType", false, null);
176         p.format = Format.fromString(parseParam(paramMap, "format", false, "json"));
177         p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
178         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
179         p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
180         p.listNameList = getListNameList(paramMap);
181         String skipSendingStr = paramMap.get(skipSendingMessage);
182         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
183         p.convertResponse = valueOf(parseParam(paramMap, "convertResponse", false, "true"));
184         p.trustStoreFileName = parseParam(paramMap, "trustStoreFileName", false, null);
185         p.trustStorePassword = parseParam(paramMap, "trustStorePassword", false, null);
186         p.keyStoreFileName = parseParam(paramMap, "keyStoreFileName", false, null);
187         p.keyStorePassword = parseParam(paramMap, "keyStorePassword", false, null);
188         p.ssl = p.trustStoreFileName != null && p.trustStorePassword != null && p.keyStoreFileName != null
189                 && p.keyStorePassword != null;
190         p.customHttpHeaders = parseParam(paramMap, "customHttpHeaders", false, null);
191         p.partner = parseParam(paramMap, "partner", false, null);
192         p.dumpHeaders = valueOf(parseParam(paramMap, "dumpHeaders", false, null));
193         p.returnRequestPayload = valueOf(parseParam(paramMap, "returnRequestPayload", false, null));
194         p.accept = parseParam(paramMap, "accept", false, null);
195         p.multipartFormData = valueOf(parseParam(paramMap, "multipartFormData", false, "false"));
196         p.multipartFile = parseParam(paramMap, "multipartFile", false, null);
197         return p;
198     }
199
200     /**
201      * Validates the given URL in the parameters.
202      *
203      * @param restapiUrl rest api URL
204      * @throws SvcLogicException when URL validation fails
205      */
206     private static void validateUrl(String restapiUrl) throws SvcLogicException {
207         if (restapiUrl.contains(",")) {
208             String[] urls = restapiUrl.split(",");
209             for (String url : urls) {
210                 validateUrl(url);
211             }
212         } else {
213             try {
214                 URI.create(restapiUrl);
215             } catch (IllegalArgumentException e) {
216                 throw new SvcLogicException("Invalid input of url " + e.getLocalizedMessage(), e);
217             }
218         }
219     }
220
221     /**
222      * Returns the list of list name.
223      *
224      * @param paramMap parameters map
225      * @return list of list name
226      */
227     private static Set<String> getListNameList(Map<String, String> paramMap) {
228         Set<String> ll = new HashSet<>();
229         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
230             if (entry.getKey().startsWith("listName")) {
231                 ll.add(entry.getValue());
232             }
233         }
234         return ll;
235     }
236
237     /**
238      * Parses the parameter string map of property, validates if required, assigns default value if
239      * present and returns the value.
240      *
241      * @param paramMap string param map
242      * @param name name of the property
243      * @param required if value required
244      * @param def default value
245      * @return value of the property
246      * @throws SvcLogicException if required parameter value is empty
247      */
248     public static String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
249             throws SvcLogicException {
250         String s = paramMap.get(name);
251
252         if (s == null || s.trim().length() == 0) {
253             if (!required) {
254                 return def;
255             }
256             throw new SvcLogicException("Parameter " + name + " is required in RestapiCallNode");
257         }
258
259         s = s.trim();
260         StringBuilder value = new StringBuilder();
261         int i = 0;
262         int i1 = s.indexOf('%');
263         while (i1 >= 0) {
264             int i2 = s.indexOf('%', i1 + 1);
265             if (i2 < 0) {
266                 break;
267             }
268
269             String varName = s.substring(i1 + 1, i2);
270             String varValue = System.getenv(varName);
271             if (varValue == null) {
272                 varValue = "%" + varName + "%";
273             }
274
275             value.append(s.substring(i, i1));
276             value.append(varValue);
277
278             i = i2 + 1;
279             i1 = s.indexOf('%', i);
280         }
281         value.append(s.substring(i));
282
283         log.info("Parameter {}: [{}]", name, maskPassword(name, value));
284
285         return value.toString();
286     }
287
288     private static Object maskPassword(String name, Object value) {
289         String[] pwdNames = {"pwd", "passwd", "password", "Pwd", "Passwd", "Password"};
290         for (String pwdName : pwdNames) {
291             if (name.contains(pwdName)) {
292                 return "**********";
293             }
294         }
295         return value;
296     }
297
298     /**
299      * Allows Directed Graphs the ability to interact with REST APIs.
300      *
301      * @param paramMap HashMap<String,String> of parameters passed by the DG to this function
302      *        <table border="1">
303      *        <thead>
304      *        <th>parameter</th>
305      *        <th>Mandatory/Optional</th>
306      *        <th>description</th>
307      *        <th>example values</th></thead> <tbody>
308      *        <tr>
309      *        <td>templateFileName</td>
310      *        <td>Optional</td>
311      *        <td>full path to template file that can be used to build a request</td>
312      *        <td>/sdncopt/bvc/restapi/templates/vnf_service-configuration-operation_minimal.json</td>
313      *        </tr>
314      *        <tr>
315      *        <td>restapiUrl</td>
316      *        <td>Mandatory</td>
317      *        <td>url to send the request to</td>
318      *        <td>https://sdncodl:8543/restconf/operations/L3VNF-API:create-update-vnf-request</td>
319      *        </tr>
320      *        <tr>
321      *        <td>restapiUser</td>
322      *        <td>Optional</td>
323      *        <td>user name to use for http basic authentication</td>
324      *        <td>sdnc_ws</td>
325      *        </tr>
326      *        <tr>
327      *        <td>restapiPassword</td>
328      *        <td>Optional</td>
329      *        <td>unencrypted password to use for http basic authentication</td>
330      *        <td>plain_password</td>
331      *        </tr>
332      *        <tr>
333      *        <td>oAuthConsumerKey</td>
334      *        <td>Optional</td>
335      *        <td>Consumer key to use for http oAuth authentication</td>
336      *        <td>plain_key</td>
337      *        </tr>
338      *        <tr>
339      *        <td>oAuthConsumerSecret</td>
340      *        <td>Optional</td>
341      *        <td>Consumer secret to use for http oAuth authentication</td>
342      *        <td>plain_secret</td>
343      *        </tr>
344      *        <tr>
345      *        <td>oAuthSignatureMethod</td>
346      *        <td>Optional</td>
347      *        <td>Consumer method to use for http oAuth authentication</td>
348      *        <td>method</td>
349      *        </tr>
350      *        <tr>
351      *        <td>oAuthVersion</td>
352      *        <td>Optional</td>
353      *        <td>Version http oAuth authentication</td>
354      *        <td>version</td>
355      *        </tr>
356      *        <tr>
357      *        <td>contentType</td>
358      *        <td>Optional</td>
359      *        <td>http content type to set in the http header</td>
360      *        <td>usually application/json or application/xml</td>
361      *        </tr>
362      *        <tr>
363      *        <td>format</td>
364      *        <td>Optional</td>
365      *        <td>should match request body format</td>
366      *        <td>json or xml</td>
367      *        </tr>
368      *        <tr>
369      *        <td>httpMethod</td>
370      *        <td>Optional</td>
371      *        <td>http method to use when sending the request</td>
372      *        <td>get post put delete patch</td>
373      *        </tr>
374      *        <tr>
375      *        <td>responsePrefix</td>
376      *        <td>Optional</td>
377      *        <td>location the response will be written to in context memory</td>
378      *        <td>tmp.restapi.result</td>
379      *        </tr>
380      *        <tr>
381      *        <td>listName[i]</td>
382      *        <td>Optional</td>
383      *        <td>Used for processing XML responses with repeating
384      *        elements.</td>vpn-information.vrf-details
385      *        <td></td>
386      *        </tr>
387      *        <tr>
388      *        <td>skipSending</td>
389      *        <td>Optional</td>
390      *        <td></td>
391      *        <td>true or false</td>
392      *        </tr>
393      *        <tr>
394      *        <td>convertResponse</td>
395      *        <td>Optional</td>
396      *        <td>whether the response should be converted</td>
397      *        <td>true or false</td>
398      *        </tr>
399      *        <tr>
400      *        <td>customHttpHeaders</td>
401      *        <td>Optional</td>
402      *        <td>a list additional http headers to be passed in, follow the format in the example</td>
403      *        <td>X-CSI-MessageId=messageId,headerFieldName=headerFieldValue</td>
404      *        </tr>
405      *        <tr>
406      *        <td>dumpHeaders</td>
407      *        <td>Optional</td>
408      *        <td>when true writes http header content to context memory</td>
409      *        <td>true or false</td>
410      *        </tr>
411      *        <tr>
412      *        <td>partner</td>
413      *        <td>Optional</td>
414      *        <td>used to retrieve username, password and url if partner store exists</td>
415      *        <td>aaf</td>
416      *        </tr>
417      *        <tr>
418      *        <td>returnRequestPayload</td>
419      *        <td>Optional</td>
420      *        <td>used to return payload built in the request</td>
421      *        <td>true or false</td>
422      *        </tr>
423      *        </tbody>
424      *        </table>
425      * @param ctx Reference to context memory
426      * @throws SvcLogicException
427      * @since 11.0.2
428      * @see String#split(String, int)
429      */
430     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
431         sendRequest(paramMap, ctx, null);
432     }
433
434     protected void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx, RetryPolicy retryPolicy)
435             throws SvcLogicException {
436
437         HttpResponse r = new HttpResponse();
438         try {
439             handlePartner(paramMap);
440             Parameters p = getParameters(paramMap, new Parameters());
441             if (p.restapiUrl.contains(",") && retryPolicy == null) {
442                 String[] urls = p.restapiUrl.split(",");
443                 retryPolicy = new RetryPolicy(urls, urls.length * 2);
444                 p.restapiUrl = urls[0];
445             }
446             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
447
448             String req = null;
449             if (p.templateFileName != null) {
450                 String reqTemplate = readFile(p.templateFileName);
451                 req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
452             } else if (p.requestBody != null) {
453                 req = p.requestBody;
454             }
455             r = sendHttpRequest(req, p);
456             setResponseStatus(ctx, p.responsePrefix, r);
457
458             if (p.dumpHeaders && r.headers != null) {
459                 for (Entry<String, List<String>> a : r.headers.entrySet()) {
460                     ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
461                 }
462             }
463
464             if (p.returnRequestPayload && req != null) {
465                 ctx.setAttribute(pp + "httpRequest", req);
466             }
467
468             if (r.body != null && r.body.trim().length() > 0) {
469                 ctx.setAttribute(pp + "httpResponse", r.body);
470
471                 if (p.convertResponse) {
472                     Map<String, String> mm = null;
473                     if (p.format == Format.XML) {
474                         mm = XmlParser.convertToProperties(r.body, p.listNameList);
475                     } else if (p.format == Format.JSON) {
476                         mm = JsonParser.convertToProperties(r.body);
477                     }
478
479                     if (mm != null) {
480                         for (Map.Entry<String, String> entry : mm.entrySet()) {
481                             ctx.setAttribute(pp + entry.getKey(), entry.getValue());
482                         }
483                     }
484                 }
485             }
486         } catch (SvcLogicException e) {
487             boolean shouldRetry = false;
488             if (e.getCause().getCause() instanceof SocketException) {
489                 shouldRetry = true;
490             }
491
492             log.error("Error sending the request: " + e.getMessage(), e);
493             String prefix = parseParam(paramMap, responsePrefix, false, null);
494             if (retryPolicy == null || !shouldRetry) {
495                 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
496             } else {
497                 log.debug(retryPolicy.getRetryMessage());
498                 try {
499                     // calling getNextHostName increments the retry count so it should be called before shouldRetry
500                     String retryString = retryPolicy.getNextHostName();
501                     if (retryPolicy.shouldRetry()) {
502                         paramMap.put(restapiUrlString, retryString);
503                         log.debug("retry attempt {} will use the retry url {}", retryPolicy.getRetryCount(),
504                                 retryString);
505                         sendRequest(paramMap, ctx, retryPolicy);
506                     } else {
507                         log.debug("Maximum retries reached, won't attempt to retry. Calling setFailureResponseStatus.");
508                         setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
509                     }
510                 } catch (Exception ex) {
511                     String retryErrorMessage = "Retry attempt " + retryPolicy.getRetryCount()
512                             + "has failed with error message " + ex.getMessage();
513                     setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
514                 }
515             }
516         }
517
518         if (r != null && r.code >= 300) {
519             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
520         }
521     }
522
523     protected void handlePartner(Map<String, String> paramMap) {
524         String partner = paramMap.get("partner");
525         if (partner != null && partner.length() > 0) {
526             PartnerDetails details = partnerStore.get(partner);
527             paramMap.put(restapiUserKey, details.username);
528             paramMap.put(restapiPasswordKey, details.password);
529             if (paramMap.get(restapiUrlString) == null) {
530                 paramMap.put(restapiUrlString, details.url);
531             }
532         }
533     }
534
535     protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format) throws SvcLogicException {
536         log.info("Building {} started", format);
537         long t1 = System.currentTimeMillis();
538         String originalTemplate = template;
539
540         template = expandRepeats(ctx, template, 1);
541
542         Map<String, String> mm = new HashMap<>();
543         for (String s : ctx.getAttributeKeySet()) {
544             mm.put(s, ctx.getAttribute(s));
545         }
546
547         StringBuilder ss = new StringBuilder();
548         int i = 0;
549         while (i < template.length()) {
550             int i1 = template.indexOf("${", i);
551             if (i1 < 0) {
552                 ss.append(template.substring(i));
553                 break;
554             }
555
556             int i2 = template.indexOf('}', i1 + 2);
557             if (i2 < 0) {
558                 throw new SvcLogicException("Template error: Matching } not found");
559             }
560
561             String var1 = template.substring(i1 + 2, i2);
562             String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
563             if (value1 == null || value1.trim().length() == 0) {
564                 // delete the whole element (line)
565                 int i3 = template.lastIndexOf('\n', i1);
566                 if (i3 < 0) {
567                     i3 = 0;
568                 }
569                 int i4 = template.indexOf('\n', i1);
570                 if (i4 < 0) {
571                     i4 = template.length();
572                 }
573
574                 if (i < i3) {
575                     ss.append(template.substring(i, i3));
576                 }
577                 i = i4;
578             } else {
579                 ss.append(template.substring(i, i1)).append(value1);
580                 i = i2 + 1;
581             }
582         }
583
584         String req = format == Format.XML ? XmlJsonUtil.removeEmptyStructXml(ss.toString())
585                 : XmlJsonUtil.removeEmptyStructJson(originalTemplate, ss.toString());
586
587         if (format == Format.JSON) {
588             req = XmlJsonUtil.removeLastCommaJson(req);
589         }
590
591         long t2 = System.currentTimeMillis();
592         log.info("Building {} completed. Time: {}", format, t2 - t1);
593
594         return req;
595     }
596
597     protected String expandRepeats(SvcLogicContext ctx, String template, int level) throws SvcLogicException {
598         StringBuilder newTemplate = new StringBuilder();
599         int k = 0;
600         while (k < template.length()) {
601             int i1 = template.indexOf("${repeat:", k);
602             if (i1 < 0) {
603                 newTemplate.append(template.substring(k));
604                 break;
605             }
606
607             int i2 = template.indexOf(':', i1 + 9);
608             if (i2 < 0) {
609                 throw new SvcLogicException(
610                         "Template error: Context variable name followed by : is required after repeat");
611             }
612
613             // Find the closing }, store in i3
614             int nn = 1;
615             int i3 = -1;
616             int i = i2;
617             while (nn > 0 && i < template.length()) {
618                 i3 = template.indexOf('}', i);
619                 if (i3 < 0) {
620                     throw new SvcLogicException("Template error: Matching } not found");
621                 }
622                 int i32 = template.indexOf('{', i);
623                 if (i32 >= 0 && i32 < i3) {
624                     nn++;
625                     i = i32 + 1;
626                 } else {
627                     nn--;
628                     i = i3 + 1;
629                 }
630             }
631
632             String var1 = template.substring(i1 + 9, i2);
633             String value1 = ctx.getAttribute(var1);
634             log.info("     {}:{}", var1, value1);
635             int n = 0;
636             try {
637                 n = Integer.parseInt(value1);
638             } catch (NumberFormatException e) {
639                 log.info("value1 not set or not a number, n will remain set at zero");
640             }
641
642             newTemplate.append(template.substring(k, i1));
643
644             String rpt = template.substring(i2 + 1, i3);
645
646             for (int ii = 0; ii < n; ii++) {
647                 String ss = rpt.replaceAll("\\[\\$\\{" + level + "\\}\\]", "[" + ii + "]");
648                 if (ii == n - 1 && ss.trim().endsWith(",")) {
649                     int i4 = ss.lastIndexOf(',');
650                     if (i4 > 0) {
651                         ss = ss.substring(0, i4) + ss.substring(i4 + 1);
652                     }
653                 }
654                 newTemplate.append(ss);
655             }
656
657             k = i3 + 1;
658         }
659
660         if (k == 0) {
661             return newTemplate.toString();
662         }
663
664         return expandRepeats(ctx, newTemplate.toString(), level + 1);
665     }
666
667     protected String readFile(String fileName) throws SvcLogicException {
668         try {
669             byte[] encoded = Files.readAllBytes(Paths.get(fileName));
670             return new String(encoded, "UTF-8");
671         } catch (IOException | SecurityException e) {
672             throw new SvcLogicException("Unable to read file " + fileName + e.getLocalizedMessage(), e);
673         }
674     }
675
676     protected Client addAuthType(Client c, FileParam fp) throws SvcLogicException {
677         Parameters p = new Parameters();
678         p.restapiUser = fp.user;
679         p.restapiPassword = fp.password;
680         p.oAuthConsumerKey = fp.oAuthConsumerKey;
681         p.oAuthVersion = fp.oAuthVersion;
682         p.oAuthConsumerSecret = fp.oAuthConsumerSecret;
683         p.oAuthSignatureMethod = fp.oAuthSignatureMethod;
684         p.authtype = fp.authtype;
685         return addAuthType(c, p);
686     }
687
688     public Client addAuthType(Client client, Parameters p) throws SvcLogicException {
689         if (p.authtype == AuthType.Unspecified) {
690             if (p.restapiUser != null && p.restapiPassword != null) {
691                 client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
692             } else if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
693                 Feature oAuth1Feature =
694                         OAuth1ClientSupport.builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
695                                 .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
696                 client.register(oAuth1Feature);
697
698             }
699         } else {
700             if (p.authtype == AuthType.DIGEST) {
701                 if (p.restapiUser != null && p.restapiPassword != null) {
702                     client.register(HttpAuthenticationFeature.digest(p.restapiUser, p.restapiPassword));
703                 } else {
704                     throw new SvcLogicException(
705                             "oAUTH authentication type selected but all restapiUser and restapiPassword "
706                                     + "parameters doesn't exist",
707                             new Throwable());
708                 }
709             } else if (p.authtype == AuthType.BASIC) {
710                 if (p.restapiUser != null && p.restapiPassword != null) {
711                     client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
712                 } else {
713                     throw new SvcLogicException(
714                             "oAUTH authentication type selected but all restapiUser and restapiPassword "
715                                     + "parameters doesn't exist",
716                             new Throwable());
717                 }
718             } else if (p.authtype == AuthType.OAUTH) {
719                 if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
720                     Feature oAuth1Feature = OAuth1ClientSupport
721                             .builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
722                             .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
723                     client.register(oAuth1Feature);
724                 } else {
725                     throw new SvcLogicException(
726                             "oAUTH authentication type selected but all oAuthConsumerKey, oAuthConsumerSecret "
727                                     + "and oAuthSignatureMethod parameters doesn't exist",
728                             new Throwable());
729                 }
730             }
731         }
732         return client;
733     }
734
735     /**
736      * Receives the http response for the http request sent.
737      *
738      * @param request request msg
739      * @param p parameters
740      * @return HTTP response
741      * @throws SvcLogicException when sending http request fails
742      */
743     public HttpResponse sendHttpRequest(String request, Parameters p) throws SvcLogicException {
744
745         SSLContext ssl = null;
746         if (p.ssl && p.restapiUrl.startsWith("https")) {
747             ssl = createSSLContext(p);
748         }
749         Client client;
750
751         if (ssl != null) {
752             HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());
753             client = ClientBuilder.newBuilder().sslContext(ssl).hostnameVerifier((s, sslSession) -> true).build();
754         } else {
755             client = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true).build();
756         }
757         client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
758         // Needed to support additional HTTP methods such as PATCH
759         client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
760
761         WebTarget webTarget = addAuthType(client, p).target(p.restapiUrl);
762
763         log.info("Sending request below to url " + p.restapiUrl);
764         log.info(request);
765         long t1 = System.currentTimeMillis();
766
767         HttpResponse r = new HttpResponse();
768         r.code = 200;
769         String accept = p.accept;
770         if (accept == null) {
771             accept = p.format == Format.XML ? "application/xml" : "application/json";
772         }
773
774         String contentType = p.contentType;
775         if (contentType == null) {
776             contentType = accept + ";charset=UTF-8";
777         }
778
779         if (!p.skipSending && !p.multipartFormData) {
780
781             Invocation.Builder invocationBuilder = webTarget.request(contentType).accept(accept);
782
783             if (p.format == Format.NONE) {
784                 invocationBuilder.header("", "");
785             }
786
787             if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
788                 String[] keyValuePairs = p.customHttpHeaders.split(",");
789                 for (String singlePair : keyValuePairs) {
790                     int equalPosition = singlePair.indexOf('=');
791                     invocationBuilder.header(singlePair.substring(0, equalPosition),
792                             singlePair.substring(equalPosition + 1, singlePair.length()));
793                 }
794             }
795
796             invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
797
798             invocationBuilder.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
799
800             Response response;
801
802             try {
803                 response = invocationBuilder.method(p.httpMethod.toString(), entity(request, contentType));
804             } catch (ProcessingException | IllegalStateException e) {
805                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
806             }
807
808             r.code = response.getStatus();
809             r.headers = response.getStringHeaders();
810             EntityTag etag = response.getEntityTag();
811             if (etag != null) {
812                 r.message = etag.getValue();
813             }
814             if (response.hasEntity() && r.code != 204) {
815                 r.body = response.readEntity(String.class);
816             }
817         } else if (!p.skipSending && p.multipartFormData) {
818
819             WebTarget wt = client.register(MultiPartFeature.class).target(p.restapiUrl);
820
821             MultiPart multiPart = new MultiPart();
822             multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
823
824             FileDataBodyPart fileDataBodyPart =
825                     new FileDataBodyPart("file", new File(p.multipartFile), MediaType.APPLICATION_OCTET_STREAM_TYPE);
826             multiPart.bodyPart(fileDataBodyPart);
827
828
829             Invocation.Builder invocationBuilder = wt.request(contentType).accept(accept);
830
831             if (p.format == Format.NONE) {
832                 invocationBuilder.header("", "");
833             }
834
835             if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
836                 String[] keyValuePairs = p.customHttpHeaders.split(",");
837                 for (String singlePair : keyValuePairs) {
838                     int equalPosition = singlePair.indexOf('=');
839                     invocationBuilder.header(singlePair.substring(0, equalPosition),
840                             singlePair.substring(equalPosition + 1, singlePair.length()));
841                 }
842             }
843
844             invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
845
846             Response response;
847
848             try {
849                 response =
850                         invocationBuilder.method(p.httpMethod.toString(), entity(multiPart, multiPart.getMediaType()));
851             } catch (ProcessingException | IllegalStateException e) {
852                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
853             }
854
855             r.code = response.getStatus();
856             r.headers = response.getStringHeaders();
857             EntityTag etag = response.getEntityTag();
858             if (etag != null) {
859                 r.message = etag.getValue();
860             }
861             if (response.hasEntity() && r.code != 204) {
862                 r.body = response.readEntity(String.class);
863             }
864
865         }
866
867         long t2 = System.currentTimeMillis();
868         log.info(responseReceivedMessage, t2 - t1);
869         log.info(responseHttpCodeMessage, r.code);
870         log.info("HTTP response message: {}", r.message);
871         logHeaders(r.headers);
872         log.info("HTTP response: {}", r.body);
873
874         return r;
875     }
876
877     protected SSLContext createSSLContext(Parameters p) {
878         try (FileInputStream in = new FileInputStream(p.keyStoreFileName)) {
879             System.setProperty("jsse.enableSNIExtension", "false");
880             System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
881             System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);
882
883             HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);
884
885             KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
886             KeyStore ks = KeyStore.getInstance("PKCS12");
887             char[] pwd = p.keyStorePassword.toCharArray();
888             ks.load(in, pwd);
889             kmf.init(ks, pwd);
890
891             SSLContext ctx = SSLContext.getInstance("TLS");
892             ctx.init(kmf.getKeyManagers(), null, null);
893             return ctx;
894         } catch (Exception e) {
895             log.error("Error creating SSLContext: {}", e.getMessage(), e);
896         }
897         return null;
898     }
899
900     protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage,
901             HttpResponse resp) {
902         resp.code = 500;
903         resp.message = errorMessage;
904         String pp = prefix != null ? prefix + '.' : "";
905         ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
906         ctx.setAttribute(pp + "response-message", resp.message);
907     }
908
909     protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
910         String pp = prefix != null ? prefix + '.' : "";
911         ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
912         ctx.setAttribute(pp + "response-message", r.message);
913     }
914
915     public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
916         HttpResponse r = null;
917         try {
918             FileParam p = getFileParameters(paramMap);
919             byte[] data = Files.readAllBytes(Paths.get(p.fileName));
920
921             r = sendHttpData(data, p);
922             setResponseStatus(ctx, p.responsePrefix, r);
923
924         } catch (SvcLogicException | IOException e) {
925             log.error("Error sending the request: {}", e.getMessage(), e);
926
927             r = new HttpResponse();
928             r.code = 500;
929             r.message = e.getMessage();
930             String prefix = parseParam(paramMap, responsePrefix, false, null);
931             setResponseStatus(ctx, prefix, r);
932         }
933
934         if (r != null && r.code >= 300) {
935             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
936         }
937     }
938
939     private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
940         FileParam p = new FileParam();
941         p.fileName = parseParam(paramMap, "fileName", true, null);
942         p.url = parseParam(paramMap, "url", true, null);
943         p.user = parseParam(paramMap, "user", false, null);
944         p.password = parseParam(paramMap, "password", false, null);
945         p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
946         p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
947         String skipSendingStr = paramMap.get(skipSendingMessage);
948         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
949         p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
950         p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
951         p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
952         p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
953         p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
954         return p;
955     }
956
957     public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
958         HttpResponse r;
959         try {
960             UebParam p = getUebParameters(paramMap);
961
962             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
963
964             String req;
965
966             if (p.templateFileName == null) {
967                 log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName);
968                 p.templateFileName = defaultUebTemplateFileName;
969             }
970
971             String reqTemplate = readFile(p.templateFileName);
972             reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
973             req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
974
975             r = postOnUeb(req, p);
976             setResponseStatus(ctx, p.responsePrefix, r);
977             if (r.body != null) {
978                 ctx.setAttribute(pp + "httpResponse", r.body);
979             }
980
981         } catch (SvcLogicException e) {
982             log.error("Error sending the request: {}", e.getMessage(), e);
983
984             r = new HttpResponse();
985             r.code = 500;
986             r.message = e.getMessage();
987             String prefix = parseParam(paramMap, responsePrefix, false, null);
988             setResponseStatus(ctx, prefix, r);
989         }
990
991         if (r.code >= 300) {
992             throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
993         }
994     }
995
996     protected HttpResponse sendHttpData(byte[] data, FileParam p) throws SvcLogicException {
997
998         Client client = ClientBuilder.newBuilder().build();
999         client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
1000         client.property(ClientProperties.FOLLOW_REDIRECTS, true);
1001         WebTarget webTarget = addAuthType(client, p).target(p.url);
1002
1003         log.info("Sending file");
1004         long t1 = System.currentTimeMillis();
1005
1006         HttpResponse r = new HttpResponse();
1007         r.code = 200;
1008
1009         if (!p.skipSending) {
1010             String tt = "application/octet-stream";
1011             Invocation.Builder invocationBuilder = webTarget.request(tt).accept(tt);
1012
1013             Response response;
1014
1015             try {
1016                 if (p.httpMethod == HttpMethod.POST) {
1017                     response = invocationBuilder.post(Entity.entity(data, tt));
1018                 } else if (p.httpMethod == HttpMethod.PUT) {
1019                     response = invocationBuilder.put(Entity.entity(data, tt));
1020                 } else {
1021                     throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
1022                 }
1023             } catch (ProcessingException e) {
1024                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
1025             }
1026
1027             r.code = response.getStatus();
1028             r.headers = response.getStringHeaders();
1029             EntityTag etag = response.getEntityTag();
1030             if (etag != null) {
1031                 r.message = etag.getValue();
1032             }
1033             if (response.hasEntity() && r.code != 204) {
1034                 r.body = response.readEntity(String.class);
1035             }
1036
1037             if (r.code == 301) {
1038                 String newUrl = response.getStringHeaders().getFirst("Location");
1039
1040                 log.info("Got response code 301. Sending same request to URL: {}", newUrl);
1041
1042                 webTarget = client.target(newUrl);
1043                 invocationBuilder = webTarget.request(tt).accept(tt);
1044
1045                 try {
1046                     if (p.httpMethod == HttpMethod.POST) {
1047                         response = invocationBuilder.post(Entity.entity(data, tt));
1048                     } else if (p.httpMethod == HttpMethod.PUT) {
1049                         response = invocationBuilder.put(Entity.entity(data, tt));
1050                     } else {
1051                         throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
1052                     }
1053                 } catch (ProcessingException e) {
1054                     throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
1055                 }
1056
1057                 r.code = response.getStatus();
1058                 etag = response.getEntityTag();
1059                 if (etag != null) {
1060                     r.message = etag.getValue();
1061                 }
1062                 if (response.hasEntity() && r.code != 204) {
1063                     r.body = response.readEntity(String.class);
1064                 }
1065             }
1066         }
1067
1068         long t2 = System.currentTimeMillis();
1069         log.info(responseReceivedMessage, t2 - t1);
1070         log.info(responseHttpCodeMessage, r.code);
1071         log.info("HTTP response message: {}", r.message);
1072         logHeaders(r.headers);
1073         log.info("HTTP response: {}", r.body);
1074
1075         return r;
1076     }
1077
1078     private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
1079         UebParam p = new UebParam();
1080         p.topic = parseParam(paramMap, "topic", true, null);
1081         p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
1082         p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
1083         p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
1084         String skipSendingStr = paramMap.get(skipSendingMessage);
1085         p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
1086         return p;
1087     }
1088
1089     protected void logProperties(Map<String, Object> mm) {
1090         List<String> ll = new ArrayList<>();
1091         for (Object o : mm.keySet()) {
1092             ll.add((String) o);
1093         }
1094         Collections.sort(ll);
1095
1096         log.info("Properties:");
1097         for (String name : ll) {
1098             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
1099         }
1100     }
1101
1102     protected void logHeaders(MultivaluedMap<String, String> mm) {
1103         log.info("HTTP response headers:");
1104
1105         if (mm == null) {
1106             return;
1107         }
1108
1109         List<String> ll = new ArrayList<>();
1110         for (Object o : mm.keySet()) {
1111             ll.add((String) o);
1112         }
1113         Collections.sort(ll);
1114
1115         for (String name : ll) {
1116             log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
1117         }
1118     }
1119
1120     protected HttpResponse postOnUeb(String request, UebParam p) throws SvcLogicException {
1121         String[] urls = uebServers.split(" ");
1122         for (int i = 0; i < urls.length; i++) {
1123             if (!urls[i].endsWith("/")) {
1124                 urls[i] += "/";
1125             }
1126             urls[i] += "events/" + p.topic;
1127         }
1128
1129         Client client = ClientBuilder.newBuilder().build();
1130         client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
1131         WebTarget webTarget = client.target(urls[0]);
1132
1133         log.info("UEB URL: {}", urls[0]);
1134         log.info("Sending request:");
1135         log.info(request);
1136         long t1 = System.currentTimeMillis();
1137
1138         HttpResponse r = new HttpResponse();
1139         r.code = 200;
1140
1141         if (!p.skipSending) {
1142             String tt = "application/json";
1143             String tt1 = tt + ";charset=UTF-8";
1144
1145             Response response;
1146             Invocation.Builder invocationBuilder = webTarget.request(tt1).accept(tt);
1147
1148             try {
1149                 response = invocationBuilder.post(Entity.entity(request, tt1));
1150             } catch (ProcessingException e) {
1151                 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
1152             }
1153             r.code = response.getStatus();
1154             r.headers = response.getStringHeaders();
1155             if (response.hasEntity()) {
1156                 r.body = response.readEntity(String.class);
1157             }
1158         }
1159
1160         long t2 = System.currentTimeMillis();
1161         log.info(responseReceivedMessage, t2 - t1);
1162         log.info(responseHttpCodeMessage, r.code);
1163         logHeaders(r.headers);
1164         log.info("HTTP response:\n {}", r.body);
1165
1166         return r;
1167     }
1168
1169     public void setUebServers(String uebServers) {
1170         this.uebServers = uebServers;
1171     }
1172
1173     public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
1174         this.defaultUebTemplateFileName = defaultUebTemplateFileName;
1175     }
1176
1177     private static class FileParam {
1178
1179         public String fileName;
1180         public String url;
1181         public String user;
1182         public String password;
1183         public HttpMethod httpMethod;
1184         public String responsePrefix;
1185         public boolean skipSending;
1186         public String oAuthConsumerKey;
1187         public String oAuthConsumerSecret;
1188         public String oAuthSignatureMethod;
1189         public String oAuthVersion;
1190         public AuthType authtype;
1191     }
1192
1193     private static class UebParam {
1194
1195         public String topic;
1196         public String templateFileName;
1197         public String rootVarName;
1198         public String responsePrefix;
1199         public boolean skipSending;
1200     }
1201 }