summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/common/RestapiCallNode.java
blob: 387fa98aabd27b20fb09e1c9603cdc3b032bd307 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dcaegen2.collectors.restconf
 * ================================================================================
 * Copyright (C) 2018-2019 Huawei. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.dcae.common;

import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.api.client.filter.HTTPDigestAuthFilter;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.client.urlconnection.HTTPSProperties;
import com.sun.jersey.oauth.client.OAuthClientFilter;
import com.sun.jersey.oauth.signature.OAuthParameters;
import com.sun.jersey.oauth.signature.OAuthSecrets;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.*;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MultivaluedMap;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import java.util.*;

import static org.onap.dcae.common.RestapiCallNodeUtil.getParameters;
import static org.onap.dcae.common.RestapiCallNodeUtil.parseParam;

public class RestapiCallNode {
    private static final Logger log = LoggerFactory.getLogger(RestapiCallNode.class);

    public void sendRequest(Map<String, String> paramMap, RestConfContext ctx, Integer retryCount) throws Exception {
        HttpResponse r = new HttpResponse();
        try {
            Parameters p = getParameters(paramMap);
            String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
            String req = null;
            if (p.templateFileName != null) {
                log.info("p.templateFileName " + p.templateFileName);
                String reqTemplate = readFile(p.templateFileName);
                req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
            } else if (p.requestBody != null) {
                req = p.requestBody;
            }

            r = sendHttpRequest(req, p);
            setResponseStatus(ctx, p.responsePrefix, r);

            if (p.dumpHeaders && r.headers != null) {
                for (Map.Entry<String, List<String>> a : r.headers.entrySet()) {
                    ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
                }
            }

            if (p.returnRequestPayload && req != null) {
                ctx.setAttribute(pp + "httpRequest", req);
            }

            if (r.body != null && r.body.trim().length() > 0) {
                ctx.setAttribute(pp + "httpResponse", r.body);

                if (p.convertResponse) {
                    Map<String, String> mm = null;
                    if (p.format == Format.XML) {
                        mm = XmlParser.convertToProperties(r.body, p.listNameList);
                    } else if (p.format == Format.JSON) {
                        mm = JsonParser.convertToProperties(r.body);
                    }

                    if (mm != null) {
                        for (Map.Entry<String, String> entry : mm.entrySet()) {
                            ctx.setAttribute(pp + entry.getKey(), entry.getValue());
                            log.info("ctx.setAttribute :=>  {} value {}  ", pp + entry.getKey(), entry.getValue());
                        }
                    }
                }
            }
        } catch (Exception e) {
            boolean shouldRetry = false;
            if (e.getCause().getCause() instanceof SocketException) {
                shouldRetry = true;
            }

            log.error("Error sending the request: " + e.getMessage(), e);
            String prefix = parseParam(paramMap, "responsePrefix", false, null);
            if (!shouldRetry || (retryCount == null) || (retryCount == 0)) {
                setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
            } else {
                try {
                    retryCount = retryCount - 1;
                    log.debug("This is retry attempt {} ", retryCount);
                    sendRequest(paramMap, ctx, retryCount);
                } catch (Exception ex) {
                    log.error("Could not attempt retry.", ex);
                    String retryErrorMessage =
                            "Retry attempt has failed. No further retry shall be attempted, calling " +
                                    "setFailureResponseStatus.";
                    setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
                }
            }
        }

        if (r != null && r.code >= 300) {
            throw new Exception(String.valueOf(r.code) + ": " + r.message);
        }
    }

    protected String buildXmlJsonRequest(RestConfContext ctx, String template, Format format) throws Exception {
        log.info("Building {} started", format);
        long t1 = System.currentTimeMillis();

        template = expandRepeats(ctx, template, 1);

        Map<String, String> mm = new HashMap<>();
        for (String s : ctx.getAttributeKeySet()) {
            mm.put(s, ctx.getAttribute(s));
        }
        StringBuilder ss = new StringBuilder();
        int i = 0;
        while (i < template.length()) {
            int i1 = template.indexOf("${", i);
            if (i1 < 0) {
                ss.append(template.substring(i));
                break;
            }

            int i2 = template.indexOf('}', i1 + 2);
            if (i2 < 0) {
                throw new Exception("Template error: Matching } not found");
            }

            String var1 = template.substring(i1 + 2, i2);
            String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
            if (value1 == null || value1.trim().length() == 0) {
                // delete the whole element (line)
                int i3 = template.lastIndexOf('\n', i1);
                if (i3 < 0) {
                    i3 = 0;
                }
                int i4 = template.indexOf('\n', i1);
                if (i4 < 0) {
                    i4 = template.length();
                }

                if (i < i3) {
                    ss.append(template.substring(i, i3));
                }
                i = i4;
            } else {
                ss.append(template.substring(i, i1)).append(value1);
                i = i2 + 1;
            }
        }

        String req = format == Format.XML
                ? XmlJsonUtil.removeEmptyStructXml(ss.toString()) : XmlJsonUtil.removeEmptyStructJson(ss.toString());

        if (format == Format.JSON) {
            req = XmlJsonUtil.removeLastCommaJson(req);
        }

        long t2 = System.currentTimeMillis();
        log.info("Building {} completed. Time: {}", format, (t2 - t1));

        return req;
    }

    protected String expandRepeats(RestConfContext ctx, String template, int level) throws Exception {
        StringBuilder newTemplate = new StringBuilder();
        int k = 0;
        while (k < template.length()) {
            int i1 = template.indexOf("${repeat:", k);
            if (i1 < 0) {
                newTemplate.append(template.substring(k));
                break;
            }

            int i2 = template.indexOf(':', i1 + 9);
            if (i2 < 0) {
                throw new Exception(
                        "Template error: Context variable name followed by : is required after repeat");
            }

            // Find the closing }, store in i3
            int nn = 1;
            int i3 = -1;
            int i = i2;
            while (nn > 0 && i < template.length()) {
                i3 = template.indexOf('}', i);
                if (i3 < 0) {
                    throw new Exception("Template error: Matching } not found");
                }
                int i32 = template.indexOf('{', i);
                if (i32 >= 0 && i32 < i3) {
                    nn++;
                    i = i32 + 1;
                } else {
                    nn--;
                    i = i3 + 1;
                }
            }

            String var1 = template.substring(i1 + 9, i2);
            String value1 = ctx.getAttribute(var1);
            log.info("     {}:{}", var1, value1);
            int n = 0;
            try {
                n = Integer.parseInt(value1);
            } catch (NumberFormatException e) {
                log.info("value1 not set or not a number, n will remain set at zero");
            }

            newTemplate.append(template.substring(k, i1));

            String rpt = template.substring(i2 + 1, i3);

            for (int ii = 0; ii < n; ii++) {
                String ss = rpt.replaceAll("\\[\\$\\{" + level + "\\}\\]", "[" + ii + "]");
                if (ii == n - 1 && ss.trim().endsWith(",")) {
                    int i4 = ss.lastIndexOf(',');
                    if (i4 > 0) {
                        ss = ss.substring(0, i4) + ss.substring(i4 + 1);
                    }
                }
                newTemplate.append(ss);
            }

            k = i3 + 1;
        }

        if (k == 0) {
            return newTemplate.toString();
        }

        return expandRepeats(ctx, newTemplate.toString(), level + 1);
    }

    protected String readFile(String fileName) throws Exception {
        try {
            byte[] encoded = Files.readAllBytes(Paths.get(fileName));
            return new String(encoded, "UTF-8");
        } catch (IOException | SecurityException e) {
            throw new IOException("Unable to read file " + fileName + e.getLocalizedMessage(), e);
        }
    }

    protected Client addAuthType(Client client, Parameters p) throws Exception {
        if (p.authtype == AuthType.Unspecified) {
            if (p.restapiUser != null && p.restapiPassword != null) {
                client.addFilter(new HTTPBasicAuthFilter(p.restapiUser, p.restapiPassword));
            } else if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null
                    && p.oAuthSignatureMethod != null) {
                OAuthParameters params = new OAuthParameters()
                        .signatureMethod(p.oAuthSignatureMethod)
                        .consumerKey(p.oAuthConsumerKey)
                        .version(p.oAuthVersion);

                OAuthSecrets secrets = new OAuthSecrets()
                        .consumerSecret(p.oAuthConsumerSecret);
                client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets));
            }
        } else {
            if (p.authtype == AuthType.DIGEST) {
                if (p.restapiUser != null && p.restapiPassword != null) {
                    client.addFilter(new HTTPDigestAuthFilter(p.restapiUser, p.restapiPassword));
                } else {
                    throw new SecurityException("oAUTH authentication type selected but all restapiUser and restapiPassword " +
                            "parameters doesn't exist", new Throwable());
                }
            } else if (p.authtype == AuthType.BASIC) {
                if (p.restapiUser != null && p.restapiPassword != null) {
                    client.addFilter(new HTTPBasicAuthFilter(p.restapiUser, p.restapiPassword));
                } else {
                    throw new SecurityException("oAUTH authentication type selected but all restapiUser and restapiPassword " +
                            "parameters doesn't exist", new Throwable());
                }
            } else if (p.authtype == AuthType.OAUTH) {
                if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
                    OAuthParameters params = new OAuthParameters()
                            .signatureMethod(p.oAuthSignatureMethod)
                            .consumerKey(p.oAuthConsumerKey)
                            .version(p.oAuthVersion);

                    OAuthSecrets secrets = new OAuthSecrets()
                            .consumerSecret(p.oAuthConsumerSecret);
                    client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets));
                } else {
                    throw new SecurityException("oAUTH authentication type selected but all oAuthConsumerKey, voAuthConsumerSecret " +
                            "and oAuthSignatureMethod parameters doesn't exist", new Throwable());
                }
            }
        }
        return client;
    }

    protected HttpResponse sendHttpRequest(String request, Parameters p) throws Exception {
        /* Enable this code if external controller's keyStore file not availabale */
        ClientConfig config = new DefaultClientConfig();
        if (!p.disableSsl) {
            SSLContext ssl = null;
            if (p.ssl && p.restapiUrl.startsWith("https")) {
                ssl = createSSLContext(p);
            }
            if (ssl != null) {
                HostnameVerifier hostnameVerifier = (hostname, session) -> true;

                config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
                        new HTTPSProperties(hostnameVerifier, ssl));
            }
        } else {

            /* Create a trust manager that does not validate certificate chains */
            TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
            };

            /* Install the all-trusting trust manager */
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            /* Create all-trusting host name verifier */
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            /* Install the all-trusting host verifier*/
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        }
        logProperties(config.getProperties());

        Client client = Client.create(config);
        client.setConnectTimeout(5000);
        client.addFilter(new LoggingFilter());
        WebResource webResource = addAuthType(client, p).resource(p.restapiUrl);

        log.info("Sending request:");
        log.info(request);
        log.info("URL: " + p.restapiUrl + " method " + p.httpMethod.toString() + " Custom header " + p.customHttpHeaders);

        long t1 = System.currentTimeMillis();

        HttpResponse r = new HttpResponse();
        r.code = 200;

        if (!p.skipSending) {
            String tt = p.format == Format.XML ? "application/xml" : "application/json";
            String tt1 = tt + ";charset=UTF-8";
            if (p.contentType != null) {
                tt = p.contentType;
                tt1 = p.contentType;
            }

            WebResource.Builder webResourceBuilder = webResource.accept(tt).type(tt1);
            if (p.format == Format.NONE) {
                webResourceBuilder = webResource.header("", "");
            }

            if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
                String[] keyValuePairs = p.customHttpHeaders.split(",");
                for (String singlePair : keyValuePairs) {
                    int equalPosition = singlePair.indexOf('=');
                    webResourceBuilder.header(singlePair.substring(0, equalPosition),
                            singlePair.substring(equalPosition + 1, singlePair.length()));
                }
            }

            ClientResponse response;

            try {
                response = webResourceBuilder.method(p.httpMethod.toString(), ClientResponse.class, request);
            } catch (UniformInterfaceException | ClientHandlerException e) {
                throw new Exception("Exception while sending http request to client "
                        + e.getLocalizedMessage(), e);
            }

            r.code = response.getStatus();
            r.headers = response.getHeaders();
            EntityTag etag = response.getEntityTag();
            if (etag != null) {
                r.message = etag.getValue();
            }
            if (response.hasEntity() && r.code != 204) {
                r.body = response.getEntity(String.class);
            }
        }

        long t2 = System.currentTimeMillis();
        log.info("Response received. Time: {}", (t2 - t1));
        log.info("HTTP response code: {}", r.code);
        log.info("HTTP response message: {}", r.message);
        logHeaders(r.headers);
        log.info("HTTP response: {}", r.body);

        return r;
    }

    protected void setFailureResponseStatus(RestConfContext ctx, String prefix, String errorMessage,
                                            HttpResponse resp) {
        resp.code = 500;
        resp.message = errorMessage;
        String pp = prefix != null ? prefix + '.' : "";
        ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
        ctx.setAttribute(pp + "response-message", resp.message);
    }

    protected void setResponseStatus(RestConfContext ctx, String prefix, HttpResponse r) {
        String pp = prefix != null ? prefix + '.' : "";
        ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
        ctx.setAttribute(pp + "response-message", r.message);
    }

    protected SSLContext createSSLContext(Parameters p) {
        try (FileInputStream in = new FileInputStream(p.keyStoreFileName)) {
            System.setProperty("jsse.enableSNIExtension", "false");
            System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
            System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);

            HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            KeyStore ks = KeyStore.getInstance("PKCS12");
            char[] pwd = p.keyStorePassword.toCharArray();
            log.info("pwd " + pwd + " " + p.keyStorePassword);
            ks.load(in, pwd);
            kmf.init(ks, pwd);

            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(kmf.getKeyManagers(), null, null);
            return ctx;
        } catch (Exception e) {
            log.error("Error creating SSLContext: {}", e.getMessage(), e);
        }
        return null;
    }

    protected void logProperties(Map<String, Object> mm) {
        List<String> ll = new ArrayList<>();
        for (Object o : mm.keySet())
            ll.add((String) o);
        Collections.sort(ll);

        log.info("Properties:");
        for (String name : ll)
            log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
    }

    protected void logHeaders(MultivaluedMap<String, String> mm) {
        log.info("HTTP response headers:");

        if (mm == null) {
            return;
        }

        List<String> ll = new ArrayList<>();
        for (Object o : mm.keySet())
            ll.add((String) o);
        Collections.sort(ll);

        for (String name : ll)
            log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
    }
}