diff options
Diffstat (limited to 'framework/src/main/java')
8 files changed, 137 insertions, 114 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java index b88477c0..fa9bcf02 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java +++ b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java @@ -186,20 +186,8 @@ public class OnapCommandResult { return noOfRecords; } - /** - * Helps to print the result based on the type. - * - * @return string - * @throws OnapCommandOutputFormatNotsupported - * excpetion - * @throws OnapCommandOutputPrintingFailed - * exception - */ - public String print() throws OnapCommandException { - if (this.getType().equals(OnapCommandResultType.TEXT)) { - return this.getOutput().toString(); - } + public OnapCommandPrint createAndLoadPrint() { OnapCommandPrint print = new OnapCommandPrint(); print.setPrintTitle(this.isIncludeTitle()); print.setDirection(this.printDirection); @@ -232,6 +220,24 @@ public class OnapCommandResult { print.addColumn(val.getName(), val.getValues()); } } + return print; + } + + /** + * Helps to print the result based on the type. + * + * @return string + * @throws OnapCommandOutputFormatNotsupported + * excpetion + * @throws OnapCommandOutputPrintingFailed + * exception + */ + public String print() throws OnapCommandException { + if (this.getType().equals(OnapCommandResultType.TEXT)) { + return this.getOutput().toString(); + } + + OnapCommandPrint print = createAndLoadPrint(); if (this.getType().equals(OnapCommandResultType.JSON)) { return print.printJson(); 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 d2bc98be..14e37c3c 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 @@ -98,6 +98,21 @@ public class OnapCommandPrint { return max; } + public List<List<Object>> addTitle(List<List<Object>> rows, boolean isNormalize){ + if (this.isPrintTitle()) { + List<Object> list = new ArrayList<>(); + for (String key : this.data.keySet()) { + if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) { + list.add(splitIntoList(key, MAX_COLUMN_LENGTH)); + } else { + list.add(key); + } + } + rows.add(list); + } + return rows; + } + /** * Helps to form the rows from columns. * @@ -112,17 +127,7 @@ public class OnapCommandPrint { List<List<Object>> rows = new ArrayList<>(); // add title - if (this.isPrintTitle()) { - List<Object> list = new ArrayList<>(); - for (String key : this.data.keySet()) { - if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) { - list.add(splitIntoList(key, MAX_COLUMN_LENGTH)); - } else { - list.add(key); - } - } - rows.add(list); - } + rows = addTitle(rows, isNormalize); // form row for (int i = 0; i < this.findMaxRows(); i++) { @@ -166,7 +171,7 @@ public class OnapCommandPrint { } // new line is converted to space char if (inp.contains("\n")) { - inp = inp.replaceAll("\n", ""); + inp = inp.replace("\n", ""); } StringTokenizer tok = new StringTokenizer(inp, " "); @@ -243,15 +248,20 @@ public class OnapCommandPrint { } } + public JSONObject printPortrait(List<List<Object>> rows){ + JSONObject result = new JSONObject(); + for (int i=1; i<rows.size(); i++) { + if (rows.get(i).get(1) != null) + result.put(rows.get(i).get(0).toString(), this.getJsonNodeOrString(rows.get(i).get(1).toString())); + } + return result; + } + public String printJson() { List<List<Object>> rows = this.formRows(false); if (this.direction.equals(OnapCommandPrintDirection.PORTRAIT)) { - JSONObject result = new JSONObject(); - for (int i=1; i<rows.size(); i++) { - if (rows.get(i).get(1) != null) - result.put(rows.get(i).get(0).toString(), this.getJsonNodeOrString(rows.get(i).get(1).toString())); - } + JSONObject result = printPortrait(rows); return result.toJSONString(); } else { JSONArray array = new JSONArray(); diff --git a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java index 28344def..912ea23d 100644 --- a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java +++ b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java @@ -17,6 +17,7 @@ package org.onap.cli.fw.registrar; import java.io.IOException; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -332,7 +333,7 @@ public class OnapCommandRegistrar { String versionInfo = ""; try { - versionInfo = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(OnapCommandConstants.VERSION_INFO)); + versionInfo = IOUtils.toString((this.getClass().getClassLoader().getResourceAsStream(OnapCommandConstants.VERSION_INFO)), (Charset) null); } catch (IOException e) { // NOSONAR //Never occurs } 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 e051d5dd..71a189e5 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 @@ -183,7 +183,7 @@ public class OnapCommandSchemaLoader { List<String> longOptions = new ArrayList<>(); if (validate) { - OnapCommandUtils.validateTags(exceptionList, (Map<String, Object>) values, OnapCommandConfig.getCommaSeparatedList(TOP_LEVEL_PARAMS_LIST), + OnapCommandUtils.validateTags(exceptionList, values, OnapCommandConfig.getCommaSeparatedList(TOP_LEVEL_PARAMS_LIST), OnapCommandConfig.getCommaSeparatedList(TOP_LEVEL_MANDATORY_LIST), "root level"); } diff --git a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java index 0712603a..7e0d40ee 100644 --- a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java +++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java @@ -51,6 +51,29 @@ public class OnapCommandSchemaMerger { } + public static void mergeWithExistingValue(Object yamlValue, Object existingValue, String key, Map<String, Object> mergedResult){ + if (yamlValue instanceof Map) { + if (existingValue instanceof Map) { + mergeYamlMap((Map<String, Object>) existingValue, (Map<String, Object>) yamlValue); + } else if (existingValue instanceof String) { + throw new IllegalArgumentException("Cannot merge complex element into a simple element: "+key); + } else { + throw unknownValueType(key, yamlValue); + } + } else if (yamlValue instanceof List) { + mergeYamlLists(mergedResult, key, yamlValue); + + } else if (yamlValue instanceof String + || yamlValue instanceof Boolean + || yamlValue instanceof Double + || yamlValue instanceof Integer) { + mergedResult.put(key, yamlValue); + + } else { + throw unknownValueType(key, yamlValue); + } + } + public static void mergeYamlMap(Map<String, Object> mergedResult, Map<String, Object> yamlContents) { if (yamlContents == null) return; @@ -64,27 +87,7 @@ public class OnapCommandSchemaMerger { Object existingValue = mergedResult.get(key); if (existingValue != null) { - if (yamlValue instanceof Map) { - if (existingValue instanceof Map) { - mergeYamlMap((Map<String, Object>) existingValue, (Map<String, Object>) yamlValue); - } else if (existingValue instanceof String) { - throw new IllegalArgumentException("Cannot merge complex element into a simple element: "+key); - } else { - throw unknownValueType(key, yamlValue); - } - } else if (yamlValue instanceof List) { - mergeYamlLists(mergedResult, key, yamlValue); - - } else if (yamlValue instanceof String - || yamlValue instanceof Boolean - || yamlValue instanceof Double - || yamlValue instanceof Integer) { - mergedResult.put(key, yamlValue); - - } else { - throw unknownValueType(key, yamlValue); - } - + mergeWithExistingValue(yamlValue, existingValue, key, mergedResult); } else { if (yamlValue instanceof Map || yamlValue instanceof List @@ -105,6 +108,30 @@ public class OnapCommandSchemaMerger { return new IllegalArgumentException(msg); } + private static void compareWithExistingNames(String nameN, List<Object> originalList, Map<String, Object> oN, Object o){ + if (nameN != null) { + + boolean existing = false; + for (Object e: originalList) { + Map<String, Object> oE = (Map) e; + String nameE = (String)oE.getOrDefault(OnapCommandConstants.NAME, null); + + //Name should be existing in the map, otherwise continue as don't know how to compare + if (nameN.equals(nameE)) { + for (Entry<String, Object> oNe : oN.entrySet()) { + oE.put(oNe.getKey(), oNe.getValue()); + } + existing = true; + break; + } + } + + if (!existing) { + originalList.add(o); + } + } + } + @SuppressWarnings("unchecked") private static void mergeYamlLists(Map<String, Object> mergedResult, String key, Object yamlValue) { if (! (yamlValue instanceof List && mergedResult.get(key) instanceof List)) { @@ -118,27 +145,7 @@ public class OnapCommandSchemaMerger { String nameN = (String)oN.getOrDefault(OnapCommandConstants.NAME, null); //Name should be existing in the map, otherwise continue as don't know how to compare - if (nameN != null) { - - boolean existing = false; - for (Object e: originalList) { - Map<String, Object> oE = (Map) e; - String nameE = (String)oE.getOrDefault(OnapCommandConstants.NAME, null); - - //Name should be existing in the map, otherwise continue as don't know how to compare - if (nameN.equals(nameE)) { - for (Entry<String, Object> oNe : oN.entrySet()) { - oE.put(oNe.getKey(), oNe.getValue()); - } - existing = true; - break; - } - } - - if (!existing) { - originalList.add(o); - } - } + compareWithExistingNames(nameN, originalList, oN, o); } } } diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java index 32ececfa..3b6e7853 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java @@ -18,6 +18,7 @@ package org.onap.cli.fw.store; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.security.MessageDigest; @@ -184,7 +185,7 @@ public class OnapCommandArtifactStore { artifact.setCreateAt(dateFormatter.format(new Date())); artifact.setLastUpdatedAt(artifact.getCreateAt()); - FileUtils.writeStringToFile(new File(storePath), gson.toJson(artifact)); + FileUtils.writeStringToFile(new File(storePath), gson.toJson(artifact), (Charset) null); } catch (Exception e) { // NOSONAR //It is expected that this never occurs log.error("Failed to store the artifact at {}", storePath); @@ -201,7 +202,7 @@ public class OnapCommandArtifactStore { } try { - return gson.fromJson(FileUtils.readFileToString(aFile), Artifact.class); + return gson.fromJson(FileUtils.readFileToString(aFile, (Charset) null), Artifact.class); } catch (Exception e) { // NOSONAR //It is expected that this never occurs log.error("Failed to retrieve the artifact at {}", storePath); @@ -232,12 +233,7 @@ public class OnapCommandArtifactStore { final String SP = searchPattern; //NOSONAR - for (File file: new File(getBasePath()).listFiles(/*new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.matches(SP); - } - }*/)) { + for (File file: new File(getBasePath()).listFiles()) { try (JsonReader jsonReader = new JsonReader(new FileReader(file))){ artifacts.add(gson.fromJson(jsonReader, Artifact.class)); } catch (Exception e) { // NOSONAR @@ -263,6 +259,37 @@ public class OnapCommandArtifactStore { } } + public Artifact setArtifact(Artifact artifact, Artifact existing) throws OnapCommandArtifactNotFound, OnapCommandArtifactContentNotExist, OnapCommandArtifactAlreadyExist, IOException, NoSuchAlgorithmException { + if (artifact.getName() == null) { + artifact.setName(existing.getName()); + } + + if (artifact.getDescription() == null) { + artifact.setDescription(existing.getDescription()); + } + + if (artifact.getCategoty() == null) { + artifact.setCategoty(existing.getCategoty()); + } + + if (artifact.getPath()!= null) { + if (!new File(artifact.getPath()).exists()) { + throw new OnapCommandArtifactContentNotExist(artifact.getPath()); + } + String actual = this.getChecksum(artifact.getPath()); + if (!existing.getChecksum().equals(actual)) { + artifact.setChecksum(actual); + artifact.setSize(new File(artifact.getPath()).length() / 1024); + } + } else { + artifact.setPath(existing.getPath()); + } + + artifact.setCreateAt(existing.getCreateAt()); + artifact.setLastUpdatedAt(dateFormatter.format(new Date())); + return artifact; + } + public Artifact updateArtifact(String name, String category, Artifact artifact) throws OnapCommandArtifactNotFound, OnapCommandArtifactContentNotExist, OnapCommandArtifactAlreadyExist { Artifact existing = this.getArtifact(name, category); String existingStorePath = getArtifactPath(name, category); @@ -273,33 +300,7 @@ public class OnapCommandArtifactStore { } try { - if (artifact.getName() == null) { - artifact.setName(existing.getName()); - } - - if (artifact.getDescription() == null) { - artifact.setDescription(existing.getDescription()); - } - - if (artifact.getCategoty() == null) { - artifact.setCategoty(existing.getCategoty()); - } - - if (artifact.getPath()!= null) { - if (!new File(artifact.getPath()).exists()) { - throw new OnapCommandArtifactContentNotExist(artifact.getPath()); - } - String actual = this.getChecksum(artifact.getPath()); - if (!existing.getChecksum().equals(actual)) { - artifact.setChecksum(actual); - artifact.setSize(new File(artifact.getPath()).length() / 1024); - } - } else { - artifact.setPath(existing.getPath()); - } - - artifact.setCreateAt(existing.getCreateAt()); - artifact.setLastUpdatedAt(dateFormatter.format(new Date())); + artifact = setArtifact(artifact, existing); if (artifact.getMetadata().size() > 0) { //update to existing one for (Map.Entry<String, String> entry: artifact.getMetadata().entrySet()) { @@ -312,7 +313,7 @@ public class OnapCommandArtifactStore { artifact.setMetadata(existing.getMetadata()); } - FileUtils.writeStringToFile(new File(newStorePath), gson.toJson(artifact)); + FileUtils.writeStringToFile(new File(newStorePath), gson.toJson(artifact), (Charset) null); if (!newStorePath.equalsIgnoreCase(existingStorePath)) { this.deleteArtifact(name, category); diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java index 5cbdf86b..7365447b 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java @@ -73,7 +73,6 @@ public class OnapCommandProfileStore { single = new OnapCommandProfileStore(); } - //single.load(); return single; } @@ -190,8 +189,6 @@ public class OnapCommandProfileStore { OnapCommandParamEntity[] list = gson.fromJson(jsonReader, OnapCommandParamEntity[].class); params.addAll(Arrays.asList(list)); -// } else { -// throw new OnapCommandProfileNotFound(profileName); } catch (Exception e) { // NOSONAR throw new OnapCommandProfileLoadFailed(e); } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java index dab6669c..acf44a9b 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java @@ -22,6 +22,7 @@ import static org.onap.cli.fw.conf.OnapCommandConstants.IS_INCLUDE; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -270,7 +271,7 @@ public class OnapCommandUtils { //start to read after file:filepath String fileName = splEntry.substring(5); try { - value = FileUtils.readFileToString(new File(fileName)); + value = FileUtils.readFileToString((new File(fileName)), (Charset) null); } catch (IOException e) { //when file is not found, assign the same file:FILE_PATH //so that it will given hit to user that FILE_PATH to be |