From 1db7e72d979afa03bca38d3008f55246f8d6a6c1 Mon Sep 17 00:00:00 2001 From: "priyanka.akhade" Date: Mon, 13 Apr 2020 13:16:29 +0000 Subject: Migrate to Yamlbeans Issue-ID: CLI-248 Signed-off-by: priyanka.akhade Change-Id: I182c7a2e2f41bf09990e5b1ac0344f49af33e0c5 --- .../fw/cmd/product/OnapProductsListCommand.java | 3 +- .../cli/fw/cmd/product/OnapServiceListCommand.java | 3 +- .../cli/fw/schema/OnapCommandSchemaLoader.java | 9 +----- .../cli/fw/utils/OnapCommandDiscoveryUtils.java | 37 ++++++++++++++++++---- 4 files changed, 34 insertions(+), 18 deletions(-) (limited to 'framework/src') diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapProductsListCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapProductsListCommand.java index d0788c16..10d9c448 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapProductsListCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapProductsListCommand.java @@ -28,7 +28,6 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; -import org.yaml.snakeyaml.Yaml; /** * Product list. @@ -58,7 +57,7 @@ public class OnapProductsListCommand extends OnapCommand { "/" + product + OnapCommandConstants.PRODUCT_REGISTRY_YAML); if (stream != null) { - Map map = (Map) new Yaml().load(stream); + Map map = OnapCommandDiscoveryUtils.loadYaml(stream); Map productMap = (Map) map.get("product"); String description = (String) productMap.get(OnapCommandConstants.DESCRIPTION); this.getResult().getRecordsMap().get("description").getValues().add(description.trim()); diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java index fa4f60b9..880f83ea 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java @@ -29,7 +29,6 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; -import org.yaml.snakeyaml.Yaml; /** * Service list. @@ -62,7 +61,7 @@ public class OnapServiceListCommand extends OnapCommand { Map serviceDescs = new HashMap<>(); if (stream != null) { - Map map = (Map) new Yaml().load(stream); + Map map = OnapCommandDiscoveryUtils.loadYaml(stream); if (map.containsKey("services")) { List> services = (List) map.get("services"); diff --git a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java index a1a14745..41be2332 100644 --- a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java +++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java @@ -88,7 +88,6 @@ import org.onap.cli.fw.output.OnapCommandResultAttributeScope; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import org.onap.cli.fw.utils.OnapCommandUtils; import org.springframework.core.io.Resource; -import org.yaml.snakeyaml.Yaml; public class OnapCommandSchemaLoader { @@ -541,13 +540,7 @@ public class OnapCommandSchemaLoader { * exception */ public static Map loadSchema(InputStream stream, String schemaName) throws OnapCommandInvalidSchema { - Map values = null; - try { - values = (Map) new Yaml().load(stream); - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); - } + return OnapCommandDiscoveryUtils.loadYaml(stream); - return values; } } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java index 105f68dc..23c825e2 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java @@ -56,7 +56,8 @@ import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; -import org.yaml.snakeyaml.Yaml; +import com.esotericsoftware.yamlbeans.YamlReader; +import com.esotericsoftware.yamlbeans.YamlException; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -64,7 +65,9 @@ import com.google.gson.stream.JsonReader; import java.io.FileReader; import java.io.Writer; import java.io.FileWriter; - +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.FileInputStream; public class OnapCommandDiscoveryUtils { private static Gson gson = new GsonBuilder().serializeNulls().create(); @@ -216,7 +219,7 @@ public class OnapCommandDiscoveryUtils { public static Map loadSchema(Resource resource) throws OnapCommandInvalidSchema { Map values = null; try { - values = (Map) new Yaml().load(resource.getInputStream()); + values = loadYaml(resource.getInputStream()); } catch (Exception e) { throw new OnapCommandInvalidSchema(resource.getFilename(), e); } @@ -306,7 +309,7 @@ public class OnapCommandDiscoveryUtils { if (deafultResourceMap.containsKey(PARAMETERS)) { List params = new ArrayList<>(); for (Map p: (List>) deafultResourceMap.get(PARAMETERS)) { - if (p.keySet().contains(IS_DEFAULT_PARAM) && !((Boolean) p.get(IS_DEFAULT_PARAM))) { + if (p.keySet().contains(IS_DEFAULT_PARAM) && ! (Boolean.getBoolean(String.valueOf(p.get(IS_DEFAULT_PARAM))))) { params.add(p); } } @@ -542,7 +545,7 @@ public class OnapCommandDiscoveryUtils { public static Map loadYaml(Resource resource) throws OnapCommandInvalidSchema { Map values = null; try { - values = (Map) new Yaml().load(resource.getInputStream()); + values = loadYaml(resource.getInputStream()); } catch (Exception e) { throw new OnapCommandInvalidSchema(resource.getFilename(), e); } @@ -560,11 +563,33 @@ public class OnapCommandDiscoveryUtils { public static Map loadYaml(String filePath) throws OnapCommandInvalidSchema { Map values = null; try { - values = (Map) new Yaml().load(FileUtils.readFileToString(new File(filePath))); + values = loadYaml(new FileInputStream(new File(filePath))); } catch (Exception e) { throw new OnapCommandInvalidSchema(filePath, e); } return values; } + + + /** + * Get schema map. + * + * @param inputStream + * @return map + * @throws OnapCommandInvalidSchema + * exception + */ + public static Map loadYaml(InputStream inputStream) throws OnapCommandInvalidSchema { + Map values = null; + try(InputStreamReader inputStreamReader = new InputStreamReader(inputStream);){ + YamlReader reader = new YamlReader(inputStreamReader); + values = (Map) reader.read(); + } catch (YamlException e) { + throw new OnapCommandInvalidSchema(inputStream.getClass().getName(),e.getMessage()); + } catch (IOException e) { + throw new OnapCommandInvalidSchema(inputStream.getClass().getName(),e.getMessage()); + } + return values; + } } -- cgit 1.2.3-korg