aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src
diff options
context:
space:
mode:
authorsharath reddy <bs.reddy@huawei.com>2022-03-28 20:03:30 +0530
committersharath reddy <bs.reddy@huawei.com>2022-03-30 18:06:41 +0530
commit98799b7c586dee5236d885078f6b0a9990a80713 (patch)
treeaed21220078077b04342d8e9b03a0b8a000f4ca3 /framework/src
parent2073da6514668ce17e7359900439354fb3432f4e (diff)
Reduced code complexity
Issue-ID: CLI-439 Signed-off-by: sharath reddy <bs.reddy@huawei.com> Change-Id: I9b7e6838d4f856cfeeafd3f71825ceb534bed43e Signed-off-by: sharath reddy <bs.reddy@huawei.com>
Diffstat (limited to 'framework/src')
-rw-r--r--framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java91
1 files changed, 49 insertions, 42 deletions
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);
}
}
}