aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImpl.java
blob: f115a98e5bbebe526b9e823c01279cb1fef0199c (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
 * Copyright © 2019 iconectiv
 *
 * 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.
 */

package org.openecomp.core.externaltesting.impl;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.PostConstruct;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.sdc.tosca.services.YamlUtil;
import org.openecomp.core.externaltesting.api.ClientConfiguration;
import org.openecomp.core.externaltesting.api.ExternalTestingManager;
import org.openecomp.core.externaltesting.api.RemoteTestingEndpointDefinition;
import org.openecomp.core.externaltesting.api.TestTreeNode;
import org.openecomp.core.externaltesting.api.VtpNameDescriptionPair;
import org.openecomp.core.externaltesting.api.VtpTestCase;
import org.openecomp.core.externaltesting.api.VtpTestExecutionOutput;
import org.openecomp.core.externaltesting.api.VtpTestExecutionRequest;
import org.openecomp.core.externaltesting.api.VtpTestExecutionResponse;
import org.openecomp.core.externaltesting.errors.ExternalTestingException;
import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
import org.openecomp.sdc.versioning.VersioningManager;
import org.openecomp.sdc.versioning.VersioningManagerFactory;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

public class ExternalTestingManagerImpl implements ExternalTestingManager {

    static final String VSP_ID = "vspId";
    static final String VSP_VERSION = "vspVersion";
    private static final String FILE_URL_PREFIX = "file://";
    private static final String HTTP_STATUS = "httpStatus";
    private static final String CODE = "code";
    private static final String ERROR = "error";
    private static final String MESSAGE = "message";
    private static final String DETAIL = "detail";
    private static final String PATH = "path";
    private static final String VTP_SCENARIOS_URI = "%s/v1/vtp/scenarios";
    private static final String VTP_TESTSUITE_URI = "%s/v1/vtp/scenarios/%s/testsuites";
    private static final String VTP_TESTCASES_URI = "%s/v1/vtp/scenarios/%s/testcases";
    private static final String VTP_TESTCASE_URI = "%s/v1/vtp/scenarios/%s/testsuites/%s/testcases/%s";
    private static final String VTP_EXECUTIONS_URI = "%s/v1/vtp/executions";
    private static final String VTP_EXECUTION_URI = "%s/v1/vtp/executions/%s";
    private static final String VTP_EXECUTION_ID_URL = "%s/v1/vtp/executions?requestId=%s";
    private static final String INVALIDATE_STATE_ERROR_CODE = "SDC-TEST-001";
    private static final String NO_ACCESS_CONFIGURATION_DEFINED = "No access configuration defined";
    private static final String NO_SUCH_ENDPOINT_ERROR_CODE = "SDC-TEST-002";
    private static final String ENDPOINT_ERROR_CODE = "SDC-TEST-003";
    private static final String TESTING_HTTP_ERROR_CODE = "SDC-TEST-004";
    private static final String SDC_RESOLVER_ERR = "SDC-TEST-005";
    private static final String VSP_CSAR = "vsp";
    private static final String VSP_HEAT = "vsp-zip";
    private Logger logger = LoggerFactory.getLogger(ExternalTestingManagerImpl.class);
    private VersioningManager versioningManager;
    private VendorSoftwareProductManager vendorSoftwareProductManager;
    private OrchestrationTemplateCandidateManager candidateManager;

    private TestingAccessConfig accessConfig;
    private List<RemoteTestingEndpointDefinition> endpoints;

    private RestTemplate restTemplate;

    public ExternalTestingManagerImpl() {
        restTemplate = new RestTemplate();
    }

    ExternalTestingManagerImpl(VersioningManager versioningManager,
                               VendorSoftwareProductManager vendorSoftwareProductManager,
                               OrchestrationTemplateCandidateManager candidateManager) {
        this();
        this.versioningManager = versioningManager;
        this.vendorSoftwareProductManager = vendorSoftwareProductManager;
        this.candidateManager = candidateManager;
    }

    /**
     * Read the configuration from the yaml file for this bean.  If we get an exception during load, don't force an error starting SDC but log a
     * warning.  Do no warm...
     */
    @PostConstruct
    public void init() {

        if (versioningManager == null) {
            versioningManager = VersioningManagerFactory.getInstance().createInterface();
        }
        if (vendorSoftwareProductManager == null) {
            vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
        }
        if (candidateManager == null) {
            candidateManager = OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface();
        }

        loadConfig();
    }

    private Stream<RemoteTestingEndpointDefinition> mapEndpointString(String ep) {
        RemoteTestingEndpointDefinition rv = new RemoteTestingEndpointDefinition();
        String[] cfg = ep.split(",");
        if (cfg.length < 4) {
            logger.error("invalid endpoint definition {}", ep);
            return Stream.empty();
        } else {
            rv.setId(cfg[0]);
            rv.setTitle(cfg[1]);
            rv.setEnabled("true".equals(cfg[2]));
            rv.setUrl(cfg[3]);
            if (cfg.length > 4) {
                rv.setScenarioFilter(cfg[4]);
            }
            if (cfg.length > 5) {
                rv.setApiKey(cfg[5]);
            }
            return Stream.of(rv);
        }
    }

    /**
     * Load the configuration for this component.  When the SDC onboarding backend runs, it gets a system property called config.location.  We can use
     * that to locate the config-externaltesting.yaml file.
     */
    private void loadConfig() {
        String loc = System.getProperty("config.location");
        File file = new File(loc, "externaltesting-configuration.yaml");
        try (InputStream fileInput = new FileInputStream(file)) {
            YamlUtil yamlUtil = new YamlUtil();
            accessConfig = yamlUtil.yamlToObject(fileInput, TestingAccessConfig.class);

            if (logger.isInfoEnabled()) {
                String s = new ObjectMapper().writeValueAsString(accessConfig);
                logger.info("loaded external testing config {}", s);
            }

            endpoints =
                accessConfig.getEndpoints().stream().flatMap(this::mapEndpointString).collect(Collectors.toList());

            if (logger.isInfoEnabled()) {
                String s = new ObjectMapper().writeValueAsString(endpoints);
                logger.info("processed external testing config {}", s);
            }
        } catch (IOException ex) {
            logger.error("failed to read external testing config.  Disabling the feature", ex);
            accessConfig = new TestingAccessConfig();
            accessConfig.setEndpoints(new ArrayList<>());
            accessConfig.setClient(new ClientConfiguration());
            accessConfig.getClient().setEnabled(false);
            endpoints = new ArrayList<>();
        }
    }

    /**
     * Return the configuration of this feature that we want to expose to the client.  Treated as a JSON blob for flexibility.
     */
    @Override
    public ClientConfiguration getConfig() {
        ClientConfiguration cc = null;
        if (accessConfig != null) {
            cc = accessConfig.getClient();
        }
        if (cc == null) {
            cc = new ClientConfiguration();
            cc.setEnabled(false);
        }
        return cc;
    }

    /**
     * To allow for functional testing, we let a caller invoke a setConfig request to enable/disable the client.  This new value is not persisted.
     *
     * @return new client configuration
     */
    @Override
    public ClientConfiguration setConfig(ClientConfiguration cc) {
        if (accessConfig == null) {
            accessConfig = new TestingAccessConfig();
        }
        accessConfig.setClient(cc);
        return getConfig();
    }

    /**
     * To allow for functional testing, we let a caller invoke a setEndpoints request to configure where the BE makes request to.
     *
     * @return new endpoint definitions.
     */
    @Override
    public List<RemoteTestingEndpointDefinition> setEndpoints(List<RemoteTestingEndpointDefinition> endpoints) {
        this.endpoints = endpoints;
        return this.getEndpoints();
    }


    @Override
    public TestTreeNode getTestCasesAsTree() {
        TestTreeNode root = new TestTreeNode("root", "root");

        // quick out in case of non-configured SDC
        if (endpoints == null) {
            return root;
        }

        for (RemoteTestingEndpointDefinition ep : endpoints) {
            if (ep.isEnabled()) {
                buildTreeFromEndpoint(ep, root);
            }
        }
        return root;
    }

    private void buildTreeFromEndpoint(RemoteTestingEndpointDefinition ep, TestTreeNode root) {
        try {
            logger.debug("process endpoint {}", ep.getId());
            getScenarios(ep.getId()).stream()
                .filter(s -> ((ep.getScenarioFilter() == null) || ep.getScenarioFilterPattern().matcher(s.getName())
                    .matches())).forEach(s -> {
                addScenarioToTree(root, s);
                getTestSuites(ep.getId(), s.getName()).forEach(suite -> addSuiteToTree(root, s, suite));
                getTestCases(ep.getId(), s.getName()).forEach(tc -> {
                    try {
                        VtpTestCase details =
                            getTestCase(ep.getId(), s.getName(), tc.getTestSuiteName(), tc.getTestCaseName());
                        addTestCaseToTree(root, ep.getId(), s.getName(), tc.getTestSuiteName(), details);
                    } catch (@SuppressWarnings("squid:S1166") ExternalTestingException ex) {
                        // Not logging stack trace on purpose.  VTP was throwing exceptions for certain test cases.
                        logger.warn("failed to load test case {}", tc.getTestCaseName());
                    }
                });
            });
        } catch (ExternalTestingException ex) {
            logger.error("unable to contact testing endpoint {}", ep.getId(), ex);
        }
    }

    private Optional<TestTreeNode> findNamedChild(TestTreeNode root, String name) {
        if (root.getChildren() == null) {
            return Optional.empty();
        }
        return root.getChildren().stream().filter(n -> n.getName().equals(name)).findFirst();
    }

    /**
     * Find the place in the tree to add the test case.
     *
     * @param root          root of the tree.
     * @param endpointName  name of the endpoint to assign to the test case.
     * @param scenarioName  scenario to add this case to
     * @param testSuiteName suite in the scenario to add this case to
     * @param tc            test case to add.
     */
    private void addTestCaseToTree(TestTreeNode root, String endpointName, String scenarioName, String testSuiteName,
                                   VtpTestCase tc) {
        // return quickly.
        if (tc == null) {
            return;
        }
        findNamedChild(root, scenarioName)
            .ifPresent(scenarioNode -> findNamedChild(scenarioNode, testSuiteName).ifPresent(suiteNode -> {
                massageTestCaseForUI(tc, endpointName, scenarioName);
                if (suiteNode.getTests() == null) {
                    suiteNode.setTests(new ArrayList<>());
                }
                suiteNode.getTests().add(tc);
            }));
    }

    private void massageTestCaseForUI(VtpTestCase testcase, String endpoint, String scenario) {
        testcase.setEndpoint(endpoint);
        // VTP workaround.
        if (testcase.getScenario() == null) {
            testcase.setScenario(scenario);
        }
    }

    /**
     * Add the test suite to the tree at the appropriate place if it does not already exist in the tree.
     *
     * @param root     root of the tree.
     * @param scenario scenario under which this suite should be placed
     * @param suite    test suite to add.
     */
    private void addSuiteToTree(final TestTreeNode root, final VtpNameDescriptionPair scenario,
                                final VtpNameDescriptionPair suite) {
        findNamedChild(root, scenario.getName()).ifPresent(parent -> {
            if (parent.getChildren() == null) {
                parent.setChildren(new ArrayList<>());
            }
            if (parent.getChildren().stream().noneMatch(n -> StringUtils.equals(n.getName(), suite.getName()))) {
                parent.getChildren().add(new TestTreeNode(suite.getName(), suite.getDescription()));
            }
        });
    }

    /**
     * Add the scenario to the tree if it does not already exist.
     *
     * @param root root of the tree.
     * @param s    scenario to add.
     */
    private void addScenarioToTree(TestTreeNode root, VtpNameDescriptionPair s) {
        logger.debug("addScenario {} to {} with {}", s.getName(), root.getName(), root.getChildren());
        if (root.getChildren() == null) {
            root.setChildren(new ArrayList<>());
        }
        if (root.getChildren().stream().noneMatch(n -> StringUtils.equals(n.getName(), s.getName()))) {
            logger.debug("createScenario {} in {}", s.getName(), root.getName());
            root.getChildren().add(new TestTreeNode(s.getName(), s.getDescription()));
        }
    }

    /**
     * Get the list of endpoints defined to the testing manager.
     *
     * @return list of endpoints or empty list if the manager is not configured.
     */
    public List<RemoteTestingEndpointDefinition> getEndpoints() {
        if (endpoints != null) {
            return endpoints.stream().filter(RemoteTestingEndpointDefinition::isEnabled).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Code shared by getScenarios and getTestSuites.
     */
    private List<VtpNameDescriptionPair> returnNameDescriptionPairFromUrl(String url) {
        ParameterizedTypeReference<List<VtpNameDescriptionPair>> t =
            new ParameterizedTypeReference<List<VtpNameDescriptionPair>>() {
            };
        List<VtpNameDescriptionPair> rv = proxyGetRequestToExternalTestingSite(url, t);
        if (rv == null) {
            rv = new ArrayList<>();
        }
        return rv;
    }

    /**
     * Get the list of scenarios at a given endpoint.
     */
    public List<VtpNameDescriptionPair> getScenarios(final String endpoint) {
        String url = buildEndpointUrl(VTP_SCENARIOS_URI, endpoint, ArrayUtils.EMPTY_STRING_ARRAY);
        return returnNameDescriptionPairFromUrl(url);
    }

    /**
     * Get the list of test suites for an endpoint for the given scenario.
     */
    public List<VtpNameDescriptionPair> getTestSuites(final String endpoint, final String scenario) {
        String url = buildEndpointUrl(VTP_TESTSUITE_URI, endpoint, new String[]{scenario});
        return returnNameDescriptionPairFromUrl(url);
    }

    /**
     * Get the list of test cases under a scenario.  This is the VTP API.  It would seem better to get the list of cases under a test suite but that
     * is not supported.
     */
    @Override
    public List<VtpTestCase> getTestCases(String endpoint, String scenario) {
        String url = buildEndpointUrl(VTP_TESTCASES_URI, endpoint, new String[]{scenario});
        ParameterizedTypeReference<List<VtpTestCase>> t = new ParameterizedTypeReference<List<VtpTestCase>>() {
        };
        List<VtpTestCase> rv = proxyGetRequestToExternalTestingSite(url, t);
        if (rv == null) {
            rv = new ArrayList<>();
        }
        return rv;
    }

    /**
     * Get a test case definition.
     */
    @Override
    public VtpTestCase getTestCase(String endpoint, String scenario, String testSuite, String testCaseName) {
        String url = buildEndpointUrl(VTP_TESTCASE_URI, endpoint, new String[]{scenario, testSuite, testCaseName});
        ParameterizedTypeReference<VtpTestCase> t = new ParameterizedTypeReference<VtpTestCase>() {
        };
        return proxyGetRequestToExternalTestingSite(url, t);
    }

    /**
     * Return the results of a previous test execution.
     *
     * @param endpoint    endpoint to query
     * @param executionId execution to query.
     * @return execution response from testing endpoint.
     */
    @Override
    public VtpTestExecutionResponse getExecution(String endpoint, String executionId) {
        String url = buildEndpointUrl(VTP_EXECUTION_URI, endpoint, new String[]{executionId});
        ParameterizedTypeReference<VtpTestExecutionResponse> t =
            new ParameterizedTypeReference<VtpTestExecutionResponse>() {
            };
        return proxyGetRequestToExternalTestingSite(url, t);
    }


    /**
     * Execute tests splitting them across endpoints and collecting the results.
     *
     * @param testsToRun list of tests to be executed.
     * @return collection of result objects.
     */

    @Override
    public List<VtpTestExecutionResponse> execute(final List<VtpTestExecutionRequest> testsToRun, String vspId,
                                                  String vspVersionId, String requestId, Map<String, byte[]> fileMap) {
        if (endpoints == null) {
            throw new ExternalTestingException(INVALIDATE_STATE_ERROR_CODE, 500, NO_ACCESS_CONFIGURATION_DEFINED);
        }

        // partition the requests by endpoint.
        Map<String, List<VtpTestExecutionRequest>> partitions =
            testsToRun.stream().collect(Collectors.groupingBy(VtpTestExecutionRequest::getEndpoint));

        // process each group and collect the results.
        return partitions.entrySet().stream().flatMap(
            e -> doExecute(e.getKey(), e.getValue(), vspId, vspVersionId, requestId, fileMap).stream())
            .collect(Collectors.toList());
    }

    /**
     * Get the list of Execution by requestId.
     */
    @Override
    public List<VtpTestExecutionOutput> getExecutionIds(String endpoint, String requestId) {
        String url = buildEndpointUrl(VTP_EXECUTION_ID_URL, endpoint, new String[]{requestId});
        ParameterizedTypeReference<List<VtpTestExecutionOutput>> t =
            new ParameterizedTypeReference<List<VtpTestExecutionOutput>>() {
            };
        List<VtpTestExecutionOutput> rv = proxyGetRequestToExternalTestingSite(url, t);
        if (rv == null) {
            rv = new ArrayList<>();
        }
        return rv;
    }

    /**
     * Execute a set of tests at a given endpoint.
     *
     * @param endpointName name of the endpoint
     * @param testsToRun   set of tests to run
     * @return list of execution responses.
     */
    private List<VtpTestExecutionResponse> doExecute(final String endpointName,
                                                     final List<VtpTestExecutionRequest> testsToRun, String vspId, String vspVersionId,
                                                     String requestId,
                                                     Map<String, byte[]> fileMap) {
        if (endpoints == null) {
            throw new ExternalTestingException(INVALIDATE_STATE_ERROR_CODE, 500, NO_ACCESS_CONFIGURATION_DEFINED);
        }

        RemoteTestingEndpointDefinition endpoint =
            endpoints.stream().filter(e -> StringUtils.equals(endpointName, e.getId())).findFirst().orElseThrow(
                () -> new ExternalTestingException(NO_SUCH_ENDPOINT_ERROR_CODE, 400,
                    "No endpoint named " + endpointName + " is defined"));

        // if the endpoint requires an API key, specify it in the headers.
        HttpHeaders headers = new HttpHeaders();
        if (endpoint.getApiKey() != null) {
            headers.add("X-API-Key", endpoint.getApiKey());
        }
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        // build the body.
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();

        for (VtpTestExecutionRequest test : testsToRun) {
            // it will true only noramal validation not for certification
            if ((test.getParameters() != null) && (test.getParameters().containsKey(VSP_CSAR) || test.getParameters()
                .containsKey(
                    VSP_HEAT))) {
                attachArchiveContent(test, body, vspId, vspVersionId);
            }

        }

        attachFileContentInTest(body, fileMap);
        try {
            // remove the endpoint from the test request since that is a FE/BE attribute
            testsToRun.forEach(t -> t.setEndpoint(null));
            String strExecution = new ObjectMapper().writeValueAsString(testsToRun);
            body.add("executions", strExecution);

        } catch (IOException ex) {
            logger.error("exception converting tests to string", ex);
            VtpTestExecutionResponse err = new VtpTestExecutionResponse();
            err.setHttpStatus(500);
            err.setCode(TESTING_HTTP_ERROR_CODE);
            err.setMessage("Execution failed due to " + ex.getMessage());
            return Collections.singletonList(err);
        }

        // form and send request.
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        String url = buildEndpointUrl(VTP_EXECUTIONS_URI, endpointName, ArrayUtils.EMPTY_STRING_ARRAY);
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        if (requestId != null) {
            builder = builder.queryParam("requestId", requestId);
        }
        ParameterizedTypeReference<List<VtpTestExecutionResponse>> t =
            new ParameterizedTypeReference<List<VtpTestExecutionResponse>>() {
            };
        try {
            return proxyRequestToExternalTestingSite(builder.toUriString(), requestEntity, t);
        } catch (ExternalTestingException ex) {
            logger.info("exception caught invoking endpoint {}", endpointName, ex);
            if (ex.getHttpStatus() == 504) {
                return Collections.singletonList(new VtpTestExecutionResponse());
            }
            VtpTestExecutionResponse err = new VtpTestExecutionResponse();
            err.setHttpStatus(ex.getHttpStatus());
            err.setCode(TESTING_HTTP_ERROR_CODE);
            err.setMessage(ex.getMessageCode() + ": " + ex.getDetail());
            return Collections.singletonList(err);
        }
    }

    /**
     * Return URL with endpoint url as prefix.
     *
     * @param format       format string.
     * @param endpointName endpoint to address
     * @param args         args for format.
     * @return qualified url.
     */
    private String buildEndpointUrl(String format, String endpointName, String[] args) {
        if (endpoints != null) {
            RemoteTestingEndpointDefinition ep =
                endpoints.stream().filter(e -> e.isEnabled() && e.getId().equals(endpointName)).findFirst()
                    .orElseThrow(() -> new ExternalTestingException(NO_SUCH_ENDPOINT_ERROR_CODE, 500,
                        "No endpoint named " + endpointName + " is defined"));

            Object[] newArgs = ArrayUtils.add(args, 0, ep.getUrl());
            return String.format(format, newArgs);
        }
        throw new ExternalTestingException(INVALIDATE_STATE_ERROR_CODE, 500, NO_ACCESS_CONFIGURATION_DEFINED);
    }

    /**
     * Proxy a get request to a testing endpoint.
     *
     * @param url          URL to invoke.
     * @param responseType type of response expected.
     * @param <T>          type of response expected
     * @return instance of <T> parsed from the JSON response from endpoint.
     */
    private <T> T proxyGetRequestToExternalTestingSite(String url, ParameterizedTypeReference<T> responseType) {
        return proxyRequestToExternalTestingSite(url, null, responseType);
    }

    /**
     * Make the actual HTTP post (using Spring RestTemplate) to an endpoint.
     *
     * @param url          URL to the endpoint
     * @param request      optional request body to send
     * @param responseType expected type
     * @param <T>          extended type
     * @return instance of expected type
     */
    private <R, T> T proxyRequestToExternalTestingSite(String url, HttpEntity<R> request,
                                                       ParameterizedTypeReference<T> responseType) {
        if (request != null) {
            logger.debug("POST request to {} with {} for {}", url, request, responseType.getType().getTypeName());
        } else {
            logger.debug("GET request to {} for {}", url, responseType.getType().getTypeName());
        }
        SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
        if (rf != null) {
            rf.setReadTimeout(10000);
            rf.setConnectTimeout(10000);
        }
        ResponseEntity<T> re;
        try {
            if (request != null) {
                re = restTemplate.exchange(url, HttpMethod.POST, request, responseType);
            } else {
                re = restTemplate.exchange(url, HttpMethod.GET, null, responseType);
            }
        } catch (HttpStatusCodeException ex) {
            // make my own exception out of this.
            logger.warn("Unexpected HTTP Status from endpoint {}", ex.getRawStatusCode());
            if ((ex.getResponseHeaders().getContentType() != null) && (
                (ex.getResponseHeaders().getContentType().isCompatibleWith(MediaType.APPLICATION_JSON))
                    || (ex.getResponseHeaders().getContentType()
                    .isCompatibleWith(MediaType.parseMediaType("application/problem+json"))))) {
                String s = ex.getResponseBodyAsString();
                logger.warn("endpoint body content is {}", s);
                try {
                    JsonObject o = new GsonBuilder().create().fromJson(s, JsonObject.class);
                    throw buildTestingException(ex.getRawStatusCode(), o);
                } catch (JsonParseException e) {
                    logger.warn("unexpected JSON response", e);
                    throw new ExternalTestingException(ENDPOINT_ERROR_CODE, ex.getStatusCode().value(),
                        ex.getResponseBodyAsString(), ex);
                }
            } else {
                throw new ExternalTestingException(ENDPOINT_ERROR_CODE, ex.getStatusCode().value(),
                    ex.getResponseBodyAsString(), ex);
            }
        } catch (ResourceAccessException ex) {
            throw new ExternalTestingException(ENDPOINT_ERROR_CODE, 500, ex.getMessage(), ex);
        } catch (Exception ex) {
            throw new ExternalTestingException(ENDPOINT_ERROR_CODE, 500, "Generic Exception " + ex.getMessage(), ex);
        }
        if (re != null) {
            logger.debug("http status of {} from external testing entity {}", re.getStatusCodeValue(), url);
            return re.getBody();
        } else {
            logger.error("null response from endpoint");
            return null;
        }
    }

    private void attachFileContentInTest(MultiValueMap<String, Object> body, Map<String, byte[]> fileMap) {
        if (fileMap != null) {
            fileMap.forEach((name, inputStream) -> body.add("file", new NamedByteArrayResource(inputStream, name)));
        }

    }

    /**
     * Errors from the endpoint could conform to the expected ETSI body or not. Here we try to handle various response body elements.
     *
     * @param statusCode http status code in response.
     * @param o          JSON object parsed from the http response body
     * @return Testing error body that should be returned to the caller
     */
    private ExternalTestingException buildTestingException(int statusCode, JsonObject o) {
        String code = null;
        String message = null;

        if (o.has(CODE)) {
            code = o.get(CODE).getAsString();
        } else if (o.has(ERROR)) {
            code = o.get(ERROR).getAsString();
        } else {
            if (o.has(HTTP_STATUS)) {
                code = o.get(HTTP_STATUS).getAsJsonPrimitive().getAsString();
            }
        }
        if (o.has(MESSAGE)) {
            if (!o.get(MESSAGE).isJsonNull()) {
                message = o.get(MESSAGE).getAsString();
            }
        } else if (o.has(DETAIL)) {
            message = o.get(DETAIL).getAsString();
        }
        if (o.has(PATH)) {
            if (message == null) {
                message = o.get(PATH).getAsString();
            } else {
                message = message + " " + o.get(PATH).getAsString();
            }
        }
        return new ExternalTestingException(code, statusCode, message);
    }

    void attachArchiveContent(VtpTestExecutionRequest test, MultiValueMap<String, Object> body, String vspId,
                              String vspVersionId) {
        try {
            extractMetadata(test, body, vspId, vspVersionId);
        } catch (IOException ex) {
            logger.error("metadata extraction failed", ex);
        }
    }

    /**
     * Extract the metadata from the VSP CSAR file.
     *
     * @param requestItem item to add metadata to for processing
     * @param vspId       VSP identifier
     * @param version     VSP version
     */
    private void extractMetadata(VtpTestExecutionRequest requestItem, MultiValueMap<String, Object> body, String vspId,
                                 String version) throws IOException {

        Version ver = new Version(version);
        logger.debug("attempt to retrieve archive for VSP {} version {}", vspId, ver.getId());

        Optional<Pair<String, byte[]>> ozip = candidateManager.get(vspId, ver);
        if (!ozip.isPresent()) {
            ozip = vendorSoftwareProductManager.get(vspId, ver);
        }

        if (!ozip.isPresent()) {
            List<Version> versions = versioningManager.list(vspId);
            String knownVersions = versions.stream()
                .map(v -> String.format("%d.%d: %s (%s)", v.getMajor(), v.getMinor(),
                    v.getStatus(), v.getId())).collect(Collectors.joining("\n"));

            String detail = String.format(
                "Archive processing failed.  Unable to find archive for VSP ID %s and Version %s.  Known versions are:\n%s",
                vspId, version, knownVersions);

            throw new ExternalTestingException(SDC_RESOLVER_ERR, 500, detail);
        }

        // safe here to do get.
        Pair<String, byte[]> zip = ozip.get();
        processArchive(requestItem, body, zip.getRight());
    }

    private void processArchive(final VtpTestExecutionRequest test, final MultiValueMap<String, Object> body,
                                final byte[] zip) {

        // VTP does not support concurrent executions of the same test with the same associated file name.
        // It writes files to /tmp and if we were to send two requests with the same file, the results
        // are unpredictable.
        String key = UUID.randomUUID().toString();
        key = key.substring(0, key.indexOf('-'));

        if (test.getParameters().containsKey(VSP_HEAT)) {
            body.add("file", new NamedByteArrayResource(zip, key + ".heat.zip"));
            test.getParameters().put(VSP_HEAT, FILE_URL_PREFIX + key + ".heat.zip");
        } else {
            body.add("file", new NamedByteArrayResource(zip, key + ".csar"));
            test.getParameters().put(VSP_CSAR, FILE_URL_PREFIX + key + ".csar");
        }
    }

    /**
     * We need to name the byte array we add to the multipart request sent to the VTP.
     */
    @EqualsAndHashCode(callSuper = false)
    protected class NamedByteArrayResource extends ByteArrayResource {

        private String filename;

        NamedByteArrayResource(byte[] bytes, String filename) {
            super(bytes, filename);
            this.filename = filename;
        }

        @Override
        public String getFilename() {
            return this.filename;
        }
    }


}