aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java
blob: 1a8de3452302543e86cda7e1747e0ce32b76ca81 (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
/**
 * Copyright 2018 Huawei Technologies Co., Ltd.
 *
 * 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.onap.vtp.scenario;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.google.common.collect.Maps;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.common.util.CollectionUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.glassfish.jersey.media.multipart.BodyPartEntity;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.onap.vnfsdk.marketplace.common.CommonConstant;
import org.onap.vnfsdk.marketplace.common.FileUtil;
import org.onap.vnfsdk.marketplace.common.ToolUtil;
import org.onap.vtp.VTPResource;
import org.onap.vtp.error.VTPError;
import org.onap.vtp.error.VTPError.VTPException;
import org.onap.vtp.manager.DistManager;
import org.onap.vtp.scenario.model.VTPTestCase;
import org.onap.vtp.scenario.model.VTPTestScenario;
import org.onap.vtp.scenario.model.VTPTestSuite;
import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseInput;
import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseList;
import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseOutput;
import org.onap.vtp.scenario.model.VTPTestScenario.VTPTestScenarioList;
import org.onap.vtp.scenario.model.VTPTestSuite.VTPTestSuiteList;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Path("/vtp")
@Api(tags = {"VTP Scenario"})
public class VTPScenarioResource extends VTPResource{
    private static final String DESCRIPTION = "description";
    private static final String PRODUCT_ARG="--product";
    private static final String OPEN_CLI="open-cli";
    private static final String FORMAT="--format";
    private static final String IO_EXCEPTION_OCCURS ="IOException occurs";
    private static final String SERVICE="service";
    private static final String PRODUCT = "product";
    private DistManager distManagerVtpScenarioResource = new DistManager();
    public VTPTestScenarioList listTestScenariosHandler() throws VTPException {
        List<String> args = new ArrayList<>();

        args.addAll(Arrays.asList(
                PRODUCT_ARG, OPEN_CLI, "product-list", FORMAT, "json"
        ));


        JsonElement results = null;
        if (isDistMode()) {
            String endPoint="/manager/scenarios";
            return  distManagerVtpScenarioResource.getScenarioListFromManager(endPoint);
        }
        else{
            try {
                results = this.makeRpcAndGetJson(args);
            } catch (IOException e) {
                LOG.error(IO_EXCEPTION_OCCURS, e);
            }
        }

        VTPTestScenarioList list = new VTPTestScenarioList();

        if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
            JsonArray resultsArray = results.getAsJsonArray();
            for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
                JsonElement jsonElement = it.next();
                JsonObject n = jsonElement.getAsJsonObject();
                if (n.entrySet().iterator().hasNext()) {
                    String name = n.get(PRODUCT).getAsString();

                    if (OPEN_CLI.equalsIgnoreCase(name))
                        continue;

                    list.getScenarios().add(new VTPTestScenario().setName(name).setDescription(
                            n.get(DESCRIPTION).getAsString()));
                }
            }
        }

        return list;
    }
    private String loggerPatternBreaking(String loggerInput) {
        return Objects.nonNull(loggerInput) ? loggerInput.replaceAll("[\n\r\t]", "_") : StringUtils.EMPTY;
    }

    @Path("/scenarios")
    @GET
    @ApiOperation(tags = "VTP Scenario", value = " List available test scenarios", response = VTPTestScenario.class, responseContainer = "List")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
                    message = "Failed to perform the operation",
                    response = VTPError.class) })
    public Response listTestScenarios() throws VTPException {
        return Response.ok(this.listTestScenariosHandler().getScenarios().toString(), MediaType.APPLICATION_JSON).build();
    }

    public VTPTestSuiteList listTestSutiesHandler(String scenario) throws VTPException {
        List<String> args = new ArrayList<>();

        args.addAll(Arrays.asList(
                PRODUCT_ARG, OPEN_CLI, "service-list", PRODUCT_ARG, scenario, FORMAT, "json"
        ));

        JsonElement results = null;
        if (isDistMode()) {
            String url="/manager/scenarios/"+scenario+"/testsuites";
            return distManagerVtpScenarioResource.getSuiteListFromManager(url);
        }else {
            try {
                results = this.makeRpcAndGetJson(args);
            } catch (IOException e) {
                LOG.error(IO_EXCEPTION_OCCURS,e);
            }
        }

        VTPTestSuiteList list = new VTPTestSuiteList();

        if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
            JsonArray resultsArray = results.getAsJsonArray();
            for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
                JsonElement jsonElement = it.next();
                JsonObject n = jsonElement.getAsJsonObject();
                if (n.entrySet().iterator().hasNext()) {
                    list.getSuites().add(new VTPTestSuite().setName(n.get(SERVICE).getAsString()).setDescription(
                            n.get(DESCRIPTION).getAsString()));
                }
            }
        }

        return list;
    }

    @Path("/scenarios/{scenario}/testsuites")
    @GET
    @ApiOperation(tags = "VTP Scenario",  value = " List available test suties in given scenario", response = VTPTestSuite.class, responseContainer = "List")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
                    message = "Failed to perform the operation",
                    response = VTPError.class) })
    public Response listTestSuties(
            @ApiParam("Test scenario name") @PathParam("scenario") String scenario) throws VTPException {

        return Response.ok(this.listTestSutiesHandler(scenario).getSuites().toString(), MediaType.APPLICATION_JSON).build();
    }

    public VTPTestCaseList listTestcasesHandler(String testSuiteName, String scenario) throws VTPException {
        List<String> args = new ArrayList<>();

        args.addAll(Arrays.asList(
                PRODUCT_ARG, OPEN_CLI, "schema-list", PRODUCT_ARG, scenario, FORMAT, "json"
        ));
        if (testSuiteName != null) {
            args.add("--service");
            args.add(testSuiteName);
        }

        JsonElement results = null;
        if (isDistMode()) {
            String url = "/manager/scenarios/" + scenario + "/testcases";
            return distManagerVtpScenarioResource.getTestCaseListFromManager(url);
        } else {
            try {
                results = this.makeRpcAndGetJson(args);
            } catch (IOException e) {
                LOG.error(IO_EXCEPTION_OCCURS, e);
            }
        }

        VTPTestCaseList list = new VTPTestCaseList();

        if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
            JsonArray resultsArray = results.getAsJsonArray();
            for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
                JsonElement jsonElement = it.next();
                JsonObject n = jsonElement.getAsJsonObject();
                if (n.entrySet().iterator().hasNext())
                    list.getTestCases().add(
                            new VTPTestCase().setTestCaseName(
                                    n.get("command").getAsString()).setTestSuiteName(
                                    n.get(SERVICE).getAsString()));
            }
        }

        return list;
    }

    @Path("/scenarios/{scenario}/testcases")
    @GET
    @ApiOperation(tags = "VTP Scenario", value = " List available test cases", response = VTPTestCase.class, responseContainer = "List")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
                    message = "Failed to perform the operation",
                    response = VTPError.class) })
    public Response listTestcases(
            @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
            @ApiParam("Test suite name") @QueryParam("testSuiteName") String testSuiteName
    ) throws VTPException {

        return Response.ok(this.listTestcasesHandler(testSuiteName, scenario).getTestCases().toString(), MediaType.APPLICATION_JSON).build();
    }

    public VTPTestCase getTestcaseHandler(String scenario, String testSuiteName, String testCaseName) throws VTPException {
        List<String> args = new ArrayList<>();
        args.addAll(Arrays.asList(
                PRODUCT_ARG, OPEN_CLI, "schema-show", PRODUCT_ARG, scenario, "--service", testSuiteName, "--command", testCaseName , FORMAT, "json"
        ));
        JsonElement results = null;
        try {
            results = this.makeRpcAndGetJson(args);
        } catch (IOException e) {
            LOG.error(IO_EXCEPTION_OCCURS,e);
        }

        JsonObject schema = results.getAsJsonObject().getAsJsonObject("schema");

        VTPTestCase tc = new VTPTestCase();
        tc.setTestCaseName(schema.get("name").getAsString());
        tc.setDescription(schema.get(DESCRIPTION).getAsString());
        tc.setTestSuiteName(schema.get(SERVICE).getAsString());
        tc.setAuthor(schema.get("author").getAsString());
        JsonElement inputsJson = schema.get("inputs");
        if (inputsJson != null && inputsJson.isJsonArray()) {
            formatResponseData(tc, inputsJson);
        }

        JsonElement outputsJson = schema.get("outputs");
        if (outputsJson != null && outputsJson.isJsonArray() && outputsJson.getAsJsonArray().size()>0) {
            for (final JsonElement jsonElement: outputsJson.getAsJsonArray()) {
                JsonObject outputJson = jsonElement.getAsJsonObject();
                VTPTestCaseOutput output = new VTPTestCaseOutput();
                output.setName(outputJson.get("name").getAsString());
                output.setDescription(outputJson.get(DESCRIPTION).getAsString());
                output.setType(outputJson.get("type").getAsString());

                tc.getOutputs().add(output);
            }
        }

        return tc;
    }

	private void formatResponseData(VTPTestCase tc, JsonElement inputsJson) {
		for (final JsonElement jsonElement: inputsJson.getAsJsonArray()) {
		    JsonObject inputJson  = jsonElement.getAsJsonObject();
		    VTPTestCaseInput input = new VTPTestCaseInput();

		    input.setName(inputJson.get("name").getAsString());
		    input.setDescription(inputJson.get(DESCRIPTION).getAsString());
		    input.setType(inputJson.get("type").getAsString());

		    if (inputJson.get("is_optional") != null)
		        input.setIsOptional(inputJson.get("is_optional").getAsBoolean());

		    if (inputJson.get("default_value") != null)
		        input.setDefaultValue(inputJson.get("default_value").getAsString());

		    if (inputJson.get("metadata") != null)
		        input.setMetadata(inputJson.get("metadata"));

		    tc.getInputs().add(input);
		}
	}

    @Path("/scenarios/{scenario}/testsuites/{testSuiteName}/testcases/{testCaseName}")
    @GET
    @ApiOperation(tags = "VTP Scenario",  value = "Retrieve test cases details like inputs outputs and test suite name", response = VTPTestCase.class)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
                    message = "Failed to perform the operation", response = VTPError.class),
            @ApiResponse(code = HttpStatus.NOT_FOUND_404,
                    message = "Test case does not exist", response = VTPError.class)})
    public Response getTestcase(
            @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
            @ApiParam(value = "Test case name") @PathParam("testSuiteName") String testSuiteName,
            @ApiParam(value = "Test case name") @PathParam("testCaseName") String testCaseName)
            throws VTPException {

        return Response.ok(this.getTestcaseHandler(scenario, testSuiteName, testCaseName).toString(), MediaType.APPLICATION_JSON).build();
    }

    @Path("/scenarios")
    @POST
    @ApiOperation(tags = "VTP Scenario", value = "Create Scenario")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to perform the operation", response = VTPError.class)})
    public Response storageScenarios(@ApiParam(value = "file form data body parts", required = true)
                                     @FormDataParam("files") List<FormDataBodyPart> bodyParts) throws VTPException {
        bodyParts.forEach(bodyPart -> {
            BodyPartEntity entity = (BodyPartEntity) bodyPart.getEntity();
            String fileName = bodyPart.getContentDisposition().getFileName();
            if (!ToolUtil.isYamlFile(new File(fileName))) {
                LOG.error("The fileName {} is not yaml !!!", fileName);
                return;
            }
            String scenario = fileName.substring(0, fileName.indexOf("-registry"));
            File scenarioDir = new File(VTP_YAML_STORE, scenario);
            File yamlFile = new File(VTP_YAML_STORE, fileName);

            // 1、store the scenario yaml file and create the scenario dir
            try {
                FileUtils.deleteQuietly(yamlFile);
                FileUtils.deleteDirectory(scenarioDir);
                FileUtils.forceMkdir(scenarioDir);
                FileUtils.copyInputStreamToFile(entity.getInputStream(), yamlFile);
            } catch (IOException e) {
                LOG.error("Save yaml {} failed", fileName, e);
            }

            // 2、create the testsuits dir and copy the testcase to current scenarios by commands
            try {
                Map<String, Object> yamlInfos = Maps.newHashMap();
                try (FileReader fileReader = new FileReader(yamlFile)) {
                    yamlInfos = snakeYaml().load(fileReader);
                }
                for (Object service : (List) yamlInfos.get("services")) {
                    processCurrentScenarioCommands(scenario, scenarioDir, service);
                }
            } catch (Exception e) {
                LOG.error("Parse testcase yaml failed !!!", e);
            }
        });
        return Response.ok("Save yaml success", MediaType.APPLICATION_JSON).build();
    }

	private void processCurrentScenarioCommands(String scenario, File scenarioDir, Object service)
			throws IOException, FileNotFoundException {
		Map<String, Object> serviceMap = (Map<String, Object>) service;
		String testsuite = serviceMap.get("name").toString();
		File testsuiteDir = new File(scenarioDir, testsuite);
		FileUtils.forceMkdir(testsuiteDir);
		if (!serviceMap.containsKey("commands")) {
		    return;
		}
		for (Object cmd : (List) serviceMap.get("commands")) {
		    File source = new File(VTP_YAML_STORE, cmd.toString().replaceAll("::", Matcher.quoteReplacement(File.separator)));
		    if (!source.isFile()) {
		        LOG.error("Source {} is not a yaml file !!!", source.getName());
		        continue;
		    }
		    File dest = new File(testsuiteDir, cmd.toString().substring(cmd.toString().lastIndexOf("::") + 2));
		    FileUtils.copyFile(source, dest);

		    // 3、modify the testcase scenario and testsuite
		    Map<String, Object> result = Maps.newHashMap();
		    try (FileReader fileReader = new FileReader(dest)) {
		        result = snakeYaml().load(fileReader);
		    }
		    Map<String, Object> info = (Map<String, Object>) result.get("info");
		    info.put(PRODUCT, scenario);
		    info.put(SERVICE, testsuite);
		    try (FileWriter fileWriter = new FileWriter(dest)) {
		        snakeYaml().dump(result, fileWriter);
		    }
		}
	}


    @Path("/scenarios/{scenarioName}")
    @DELETE
    @ApiOperation(tags = "VTP Scenario", value = "Delete yaml string")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to perform the operation", response = VTPError.class)})
    public Response deleteScenario(@ApiParam("Test scenario yaml") @PathParam("scenarioName") String scenarioName) throws VTPException {
        String scenario = scenarioName.substring(0, scenarioName.indexOf("-registry"));
        File scenarioDir = new File(VTP_YAML_STORE, scenario);
        List<File> yamls =  FileUtil.searchFiles(scenarioDir, CommonConstant.YAML_SUFFIX);
        if (!CollectionUtils.isEmpty(yamls)) {
        if (LOG.isInfoEnabled()) {
            LOG.error("The scenario yaml {} has sub testcase yamls, delete failed", loggerPatternBreaking(scenarioName));
        }
            LOG.error("The scenario yaml {} has sub testcase yamls, delete failed", scenarioName);
            throw new VTPException(
                    new VTPError().setMessage(MessageFormat.format("The scenario yaml {0} has sub testcase yamls, delete failed !!!", scenarioName))
                            .setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR_500));
        }

        try {
            if(FileUtils.directoryContains(new File(VTP_YAML_STORE), new File(scenarioName))) {
                FileUtils.deleteQuietly(new File(VTP_YAML_STORE, scenarioName));
            }
            if(FileUtils.directoryContains(new File(VTP_YAML_STORE), scenarioDir)) {
                FileUtils.deleteDirectory(scenarioDir);
            }
        } catch (IOException e) {
            LOG.error("Delete scenario yaml {} failed", scenarioName, e);
            throw new VTPException(
                    new VTPError().setMessage("Delete yaml failed !!!").setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR_500));
        }
        return Response.ok("Delete yaml success", MediaType.APPLICATION_JSON).build();
    }

    @Path("/testcases")
    @POST
    @ApiOperation(tags = "VTP Scenario", value = "Create test case")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to perform the operation", response = VTPError.class)})
    public Response storageTestcases(@ApiParam(value = "file form data body parts", required = true)
                                     @FormDataParam("files") List<FormDataBodyPart> bodyParts) throws VTPException {
        bodyParts.forEach(bodyPart -> {
            BodyPartEntity entity = (BodyPartEntity) bodyPart.getEntity();
            String fileName = bodyPart.getContentDisposition().getFileName();
            if (ToolUtil.isYamlFile(new File(fileName))) {
                // 1、store the testcase yaml file
                Map<String, Object> result = snakeYaml().load(entity.getInputStream());
                Map<String, Object> info = (Map<String, Object>) result.get("info");

                File yamlFile = new File(VTP_YAML_STORE, info.get(PRODUCT) + File.separator + info.get(SERVICE) + File.separator + fileName);
                try {
                    FileUtils.deleteQuietly(yamlFile);
                    FileUtils.copyInputStreamToFile(entity.getInputStream(), yamlFile);
                } catch (IOException e) {
                    LOG.error("Save testcase yaml {} failed", yamlFile.getName(), e);
                }
            } else {
                // 2、store the testcase script file
                File scriptFile = new File(VTP_SCRIPT_STORE, fileName);
                try {
                    FileUtils.deleteQuietly(scriptFile);
                    FileUtils.copyInputStreamToFile(entity.getInputStream(), scriptFile);
                } catch (IOException e) {
                    LOG.error("Save testcase script {} failed", scriptFile.getName(), e);
                }
            }
        });
        return Response.ok("Save success", MediaType.APPLICATION_JSON).build();
    }
}