diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-12-13 14:39:55 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-12-13 14:39:55 +0530 |
commit | 7cf506ee2b43dccd08c3d1159485e6f6729d31ed (patch) | |
tree | d68ca0160d6a273543a81180fa6807cd6ed16dde /framework | |
parent | 352ae4ea02fd0e30e20b49b320bc601f45fd7560 (diff) |
Add yaml format support for printing result
Issue-ID: CLI-66
Change-Id: I38cee498c98b78a1767a700c029dbcb310a6f727
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'framework')
3 files changed, 33 insertions, 13 deletions
diff --git a/framework/pom.xml b/framework/pom.xml index acf82f45..5230beae 100644 --- a/framework/pom.xml +++ b/framework/pom.xml @@ -82,7 +82,12 @@ <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> - <version>2.6.3</version> + <version>2.9.3</version> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.dataformat</groupId> + <artifactId>jackson-dataformat-yaml</artifactId> + <version>2.9.3</version> </dependency> </dependencies> </project> diff --git a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java index 389aee36..0c0c5a12 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java +++ b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java @@ -28,12 +28,16 @@ import java.util.StringTokenizer; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; +import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed; import org.onap.cli.fw.output.OnapCommandPrintDirection; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; + import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; - /** * Oclip Command Table print. * @@ -249,19 +253,30 @@ public class OnapCommandPrint { JSONArray array = new JSONArray(); //skip first row title + List<Object> titleRow = rows.get(0); + for (int i=1; i<rows.size(); i++) { JSONObject rowO = new JSONObject(); - for (Object col: rows.get(0)) { - rowO.put(col.toString(), rows.get(i).toString()); + + for (int j=0; j<titleRow.size(); j++) { + rowO.put(titleRow.get(j).toString(), rows.get(i).get(j).toString()); } + array.add(rowO); } - return array.toJSONString(); + JSONObject json = new JSONObject(); + json.put(OnapCommandConstants.RESULTS, array); + return json.toJSONString(); } - public String printYaml() { - // (mrkanag) print in yaml - return null; + public String printYaml() throws OnapCommandOutputPrintingFailed { + try { + return new YAMLMapper().writeValueAsString(new ObjectMapper().readTree(this.printJson())); + } catch (JsonProcessingException e) { + throw new OnapCommandOutputPrintingFailed(e); // NOSONAR + } catch (IOException e) { + throw new OnapCommandOutputPrintingFailed(e); // NOSONAR + } } } 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 8aea883e..28597443 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 @@ -113,10 +113,10 @@ public class OnapCommandSchemaLoader { throw new OnapCommandSchemaNotFound(schemaName, e); } if (inputStream == null) { - inputStream = OnapCommandSchemaLoader.loadSchemaFromFile(schemaName); + inputStream = loadSchemaFromFile(schemaName); } - Map<String, ?> values = OnapCommandSchemaLoader.loadSchema(inputStream, schemaName); + Map<String, ?> values = loadSchema(inputStream, schemaName); String schemaVersion = ""; if (values.keySet().contains(OPEN_CLI_SCHEMA_VERSION)) { Object obj = values.get(OPEN_CLI_SCHEMA_VERSION); @@ -151,13 +151,13 @@ public class OnapCommandSchemaLoader { defaultParameterMap.remove(OnapCommandConstants.INFO); } - errors.addAll(OnapCommandSchemaLoader.parseSchema(cmd, defaultParameterMap, validateSchema)); + errors.addAll(parseSchema(cmd, defaultParameterMap, validateSchema)); } Map<String, List<Map<String, String>>> commandYamlMap = (Map<String, List<Map<String, String>>>)validateSchemaVersion(schemaName, cmd.getSchemaVersion()); - errors.addAll(OnapCommandSchemaLoader.parseSchema(cmd, commandYamlMap, validateSchema)); + errors.addAll(parseSchema(cmd, commandYamlMap, validateSchema)); return errors; } catch (OnapCommandException e) { @@ -521,7 +521,7 @@ public class OnapCommandSchemaLoader { */ public static Map<String, ?> loadSchema(Resource resource) throws OnapCommandInvalidSchema { try { - return OnapCommandSchemaLoader.loadSchema(resource.getInputStream(), resource.getFilename()); + return loadSchema(resource.getInputStream(), resource.getFilename()); } catch (IOException e) { throw new OnapCommandInvalidSchema(resource.getFilename(), e); } |