From 4be0e47c656ce32a2bc93e4ca4205a806744d35f Mon Sep 17 00:00:00 2001 From: "Cooper.Wu" Date: Fri, 4 Sep 2020 15:39:00 +0800 Subject: Scenario active management Issue-ID: VNFSDK-613 Change-Id: Id2873cc618b198d63a9218f775c6d9cfce3882c0 Signed-off-by: Cooper.Wu --- .../onap/vnfsdk/marketplace/common/FileUtil.java | 54 +++++++++-- .../org/onap/vtp/scenario/VTPScenarioResource.java | 106 +++++++++++---------- 2 files changed, 102 insertions(+), 58 deletions(-) diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java index 777080f1..4b008395 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java @@ -17,8 +17,11 @@ package org.onap.vnfsdk.marketplace.common; import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -30,8 +33,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gson.Gson; import com.google.gson.stream.JsonReader; -import java.io.FileWriter; -import java.io.FileReader; + import java.nio.file.Files; import java.nio.file.Paths; @@ -73,12 +75,12 @@ public final class FileUtil { boolean isFileDeleted=false; try { if (file.exists()){ - Files.delete(Paths.get(file.getPath())); - fileAbsPath = file.getAbsolutePath(); - logger.info("delete {} {}" ,hintInfo, fileAbsPath); + Files.delete(Paths.get(file.getPath())); + fileAbsPath = file.getAbsolutePath(); + logger.info("delete {} {}" ,hintInfo, fileAbsPath); } else{ - logger.info("file not exist. no need delete {} {}" ,hintInfo , fileAbsPath); + logger.info("file not exist. no need delete {} {}" ,hintInfo , fileAbsPath); } isFileDeleted=true; @@ -118,7 +120,7 @@ public final class FileUtil { } try (InputStream input = zipFile.getInputStream(entry); - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));) { + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));) { int length = 0; while ((length = input.read(buffer)) != -1) { bos.write(buffer, 0, length); @@ -188,7 +190,7 @@ public final class FileUtil { public static boolean deleteDirectory(File file) { if (!file.exists()) { - return true; + return true; } if (file.isDirectory()) { for (File f : file.listFiles()) { @@ -246,4 +248,40 @@ public final class FileUtil { return true; } + + /** + * Search file in folder + * @param folder + * @param keyword + * @return + */ + public static List searchFiles(File folder, String keyword) { + List result = new ArrayList(); + if (folder.isFile()) + result.add(folder); + + File[] subFolders = folder.listFiles(new FileFilter() { + @Override + public boolean accept(File file) { + if (file.isDirectory()) { + return true; + } + if (file.getName().toLowerCase().contains(keyword)) { + return true; + } + return false; + } + }); + + if (subFolders != null) { + for (File file : subFolders) { + if (file.isFile()) { + result.add(file); + } else { + result.addAll(searchFiles(file, keyword)); + } + } + } + return result; + } } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java index 2eff61f8..274cacd9 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java @@ -16,12 +16,26 @@ package org.onap.vtp.scenario; -import java.io.*; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.text.MessageFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.regex.Matcher; -import javax.ws.rs.*; +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; @@ -33,6 +47,7 @@ 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; @@ -70,7 +85,7 @@ public class VTPScenarioResource extends VTPResource{ args.addAll(Arrays.asList( PRODUCT_ARG, OPEN_CLI, "product-list", FORMAT, "json" - )); + )); JsonElement results = null; @@ -84,18 +99,18 @@ public class VTPScenarioResource extends VTPResource{ if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) { JsonArray resultsArray = results.getAsJsonArray(); - for (Iterator it = resultsArray.iterator(); it.hasNext();) { - JsonElement jsonElement = it.next(); - JsonObject n = jsonElement.getAsJsonObject(); - if (n.entrySet().iterator().hasNext()) { - String name = n.get("product").getAsString(); + for (Iterator 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; + if (OPEN_CLI.equalsIgnoreCase(name)) + continue; - list.getScenarios().add(new VTPTestScenario().setName(name).setDescription( - n.get(DESCRIPTION).getAsString())); - } + list.getScenarios().add(new VTPTestScenario().setName(name).setDescription( + n.get(DESCRIPTION).getAsString())); + } } } @@ -119,7 +134,7 @@ public class VTPScenarioResource extends VTPResource{ args.addAll(Arrays.asList( PRODUCT_ARG, OPEN_CLI, "service-list", PRODUCT_ARG, scenario, FORMAT, "json" - )); + )); JsonElement results = null; try { @@ -132,14 +147,14 @@ public class VTPScenarioResource extends VTPResource{ if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) { JsonArray resultsArray = results.getAsJsonArray(); - for (Iterator 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())); - } + for (Iterator 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; @@ -164,7 +179,7 @@ public class VTPScenarioResource extends VTPResource{ args.addAll(Arrays.asList( PRODUCT_ARG, OPEN_CLI, "schema-list", PRODUCT_ARG, scenario, FORMAT, "json" - )); + )); if (testSuiteName != null) { args.add("--service"); args.add(testSuiteName); @@ -181,15 +196,15 @@ public class VTPScenarioResource extends VTPResource{ if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) { JsonArray resultsArray = results.getAsJsonArray(); - for (Iterator 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())); - } + for (Iterator 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; @@ -204,9 +219,9 @@ public class VTPScenarioResource extends VTPResource{ 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 { + @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(); } @@ -215,7 +230,7 @@ public class VTPScenarioResource extends VTPResource{ List 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); @@ -282,7 +297,7 @@ public class VTPScenarioResource extends VTPResource{ @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 { + throws VTPException { return Response.ok(this.getTestcaseHandler(scenario, testSuiteName, testCaseName).toString(), MediaType.APPLICATION_JSON).build(); } @@ -311,8 +326,8 @@ public class VTPScenarioResource extends VTPResource{ try { FileUtils.deleteQuietly(yamlFile); FileUtils.deleteDirectory(scenarioDir); - FileUtils.copyInputStreamToFile(entity.getInputStream(), yamlFile); FileUtils.forceMkdir(scenarioDir); + FileUtils.copyInputStreamToFile(entity.getInputStream(), yamlFile); } catch (IOException e) { LOG.error("Save yaml {} failed", fileName, e); } @@ -333,7 +348,7 @@ public class VTPScenarioResource extends VTPResource{ } for (Object cmd : (List) serviceMap.get("commands")) { File source = new File(VTP_YAML_STORE, cmd.toString().replaceAll("::", Matcher.quoteReplacement(File.separator))); - if (!cn.hutool.core.io.FileUtil.isFile(source)) { + if (!source.isFile()) { LOG.error("Source {} is not a yaml file !!!", source.getName()); continue; } @@ -370,16 +385,7 @@ public class VTPScenarioResource extends VTPResource{ 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 yamls = cn.hutool.core.io.FileUtil.loopFiles(scenarioDir, new FileFilter() { - @Override - public boolean accept(File pathname) { - final String path = pathname.getPath(); - if (null != path && path.endsWith(CommonConstant.YAML_SUFFIX)) { - return true; - } - return false; - } - }); + List yamls = FileUtil.searchFiles(scenarioDir, CommonConstant.YAML_SUFFIX); if (!CollectionUtils.isEmpty(yamls)) { LOG.error("The scenario yaml {} has sub testcase yamls, delete failed", scenarioName); throw new VTPException( @@ -435,4 +441,4 @@ public class VTPScenarioResource extends VTPResource{ }); return Response.ok("Save success", MediaType.APPLICATION_JSON).build(); } -} +} \ No newline at end of file -- cgit 1.2.3-korg