From 8c79df8d4835e2cded87f60d78e5bf05e3cd6b16 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam k00365106 Date: Mon, 25 Dec 2017 15:28:52 +0530 Subject: Merge default params files Issue-ID: CLI-66 Change-Id: I320e21ff3b25d0c38c196b49f2ed320174396e4a Signed-off-by: Kanagaraj Manickam k00365106 --- .../main/java/org/onap/cli/fw/cmd/OnapCommand.java | 30 ++++- .../cli/fw/registrar/OnapCommandRegistrar.java | 8 +- .../cli/fw/schema/OnapCommandSchemaLoader.java | 23 +--- .../cli/fw/schema/OnapCommandSchemaMerger.java | 146 +++++++++++++++++++++ .../cli/fw/utils/OnapCommandDiscoveryUtils.java | 2 + .../org/onap/cli/fw/utils/OnapCommandUtils.java | 1 + 6 files changed, 186 insertions(+), 24 deletions(-) create mode 100644 framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java (limited to 'framework/src/main/java') diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java index 5479b51b..8db3ab79 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java @@ -33,6 +33,7 @@ import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.output.OnapCommandResultAttributeScope; import org.onap.cli.fw.output.OnapCommandResultType; import org.onap.cli.fw.schema.OnapCommandSchemaLoader; +import org.onap.cli.fw.schema.OnapCommandSchemaMerger; import org.onap.cli.fw.utils.OnapCommandHelperUtils; import org.onap.cli.fw.utils.OnapCommandUtils; import org.slf4j.Logger; @@ -58,8 +59,21 @@ public abstract class OnapCommand { private OnapCommandResult cmdResult = new OnapCommandResult(); + private List defaultSchemas = new ArrayList<>(); + protected boolean isInitialzied = false; + protected OnapCommand() { + this.addDefaultSchemas(OnapCommandConstants.DEFAULT_PARAMETER_FILE_NAME); + } + + public List getSchemas() throws OnapCommandException { + List schemas = new ArrayList<>(); + schemas.addAll(this.defaultSchemas); + schemas.add(this.getSchemaName()); + return schemas; + } + public String getSchemaVersion() { return OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0; } @@ -116,6 +130,14 @@ public abstract class OnapCommand { this.cmdSchemaName = schemaName; } + protected void addDefaultSchemas(String schema) { + this.defaultSchemas.add(schema); + } + + public List getDefaultSchema() { + return this.defaultSchemas; + } + /** * Initialize this command from command schema and assumes schema is already validated. * @@ -127,11 +149,13 @@ public abstract class OnapCommand { return this.initializeSchema(schema, false); } + public List initializeSchema(String schema, boolean validate) throws OnapCommandException { this.setSchemaName(schema); - List errors = OnapCommandSchemaLoader.loadSchema(this, schema, true, validate); - errors.addAll(this.initializeProfileSchema(validate)); + Map schemaMap = OnapCommandSchemaMerger.mergeSchemas(this); + List errors = OnapCommandSchemaLoader.parseSchema(this, schemaMap, validate); + errors.addAll(this.initializeProfileSchema(schemaMap, validate)); this.isInitialzied = true; return errors; @@ -139,7 +163,7 @@ public abstract class OnapCommand { /** * Any additional profile based such as http schema could be initialized. */ - protected List initializeProfileSchema(boolean validate) throws OnapCommandException { + protected List initializeProfileSchema(Map schemaMap, boolean validate) throws OnapCommandException { return new ArrayList<>(); } 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 e9dc288b..19dc3f56 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 @@ -45,6 +45,8 @@ import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import org.onap.cli.fw.utils.OnapCommandHelperUtils; import org.onap.cli.fw.utils.OnapCommandUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** @@ -53,6 +55,9 @@ import org.onap.cli.fw.utils.OnapCommandUtils; * */ public class OnapCommandRegistrar { + + private static Logger LOG = LoggerFactory.getLogger(OnapCommandRegistrar.class); + private Map> registry = new HashMap<>(); private Map> registryProfilePlugins = new HashMap<>(); @@ -271,6 +276,7 @@ public class OnapCommandRegistrar { for (OnapCommandSchemaInfo schema : schemas) { if (schema.isIgnore()) { + LOG.info("Ignoring schema " + schema.getSchemaURI()); continue; } @@ -280,7 +286,7 @@ public class OnapCommandRegistrar { } else if (plugins.containsKey(schema.getSchemaProfile())) { this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaProfile())); } else { - throw new OnapUnsupportedSchemaProfile(schema.getSchemaURI()); + LOG.info("Ignoring schema " + schema.getSchemaURI()); } } } 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 0c4af809..bcf93269 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 @@ -33,11 +33,11 @@ import static org.onap.cli.fw.conf.OnapCommandConstants.INFO_SERVICE; import static org.onap.cli.fw.conf.OnapCommandConstants.INFO_TYPE; import static org.onap.cli.fw.conf.OnapCommandConstants.INPUT_PARAMS_LIST; import static org.onap.cli.fw.conf.OnapCommandConstants.INPUT_PARAMS_MANDATORY_LIST; +import static org.onap.cli.fw.conf.OnapCommandConstants.IS_DEFAULT_ATTR; +import static org.onap.cli.fw.conf.OnapCommandConstants.IS_DEFAULT_PARAM; import static org.onap.cli.fw.conf.OnapCommandConstants.IS_INCLUDE; import static org.onap.cli.fw.conf.OnapCommandConstants.IS_OPTIONAL; import static org.onap.cli.fw.conf.OnapCommandConstants.IS_SECURED; -import static org.onap.cli.fw.conf.OnapCommandConstants.IS_DEFAULT_ATTR; -import static org.onap.cli.fw.conf.OnapCommandConstants.IS_DEFAULT_PARAM; import static org.onap.cli.fw.conf.OnapCommandConstants.LONG_OPTION; import static org.onap.cli.fw.conf.OnapCommandConstants.NAME; import static org.onap.cli.fw.conf.OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION; @@ -81,7 +81,6 @@ import org.onap.cli.fw.info.OnapCommandInfo; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.input.OnapCommandParameterType; import org.onap.cli.fw.output.OnapCommandPrintDirection; -import org.onap.cli.fw.output.OnapCommandResult; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.output.OnapCommandResultAttributeScope; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; @@ -272,21 +271,9 @@ public class OnapCommandSchemaLoader { if (parameters != null) { Set names = new HashSet<>(); - //To support overriding of the parameters, if command is already - //having the same named parameters, means same parameter is - //Overridden from included template into current template - Set existingParamNames = cmd.getParametersMap().keySet(); - for (Map parameter : parameters) { - boolean isOverriding = false; OnapCommandParameter param = new OnapCommandParameter(); - //Override the parameters from its base such as default parameters list - if (existingParamNames.contains(parameter.getOrDefault(NAME, ""))) { - param = cmd.getParametersMap().get(parameter.getOrDefault(NAME, "")); - isOverriding = true; - } - if (validate) { OnapCommandUtils.validateTags(exceptionList, parameter, OnapCommandConfig.getCommaSeparatedList(INPUT_PARAMS_LIST), OnapCommandConfig.getCommaSeparatedList(INPUT_PARAMS_MANDATORY_LIST), PARAMETERS); @@ -404,11 +391,7 @@ public class OnapCommandSchemaLoader { } } - if ( !isOverriding) { - cmd.getParameters().add(param); - } else { - cmd.getParametersMap().replace(param.getName(), param); - } + cmd.getParameters().add(param); } } break; 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 new file mode 100644 index 00000000..97ca4c15 --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java @@ -0,0 +1,146 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.cli.fw.schema; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.onap.cli.fw.cmd.OnapCommand; +import org.onap.cli.fw.conf.OnapCommandConstants; +import org.onap.cli.fw.error.OnapCommandException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Helps to merge two yaml schemas + * + */ +public class OnapCommandSchemaMerger { + + static Logger LOG = LoggerFactory.getLogger(OnapCommandSchemaMerger.class); + + public static Map mergeSchemas(OnapCommand cmd) throws OnapCommandException { + Map mergedResult = new LinkedHashMap(); + + for (String schema: cmd.getSchemas()) { + Map schemaMap = (Map) OnapCommandSchemaLoader.validateSchemaVersion(schema, cmd.getSchemaVersion()); + mergeYamlMap(mergedResult, schemaMap); + } + + return mergedResult; + + } + + public static void mergeYamlMap(Map mergedResult, Map yamlContents) { + if (yamlContents == null) return; + + for (String key : yamlContents.keySet()) { + + Object yamlValue = yamlContents.get(key); + if (yamlValue == null) { + mergedResult.put(key, yamlValue); + continue; + } + + Object existingValue = mergedResult.get(key); + if (existingValue != null) { + if (yamlValue instanceof Map) { + if (existingValue instanceof Map) { + mergeYamlMap((Map) existingValue, (Map) 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); + } + + } else { + if (yamlValue instanceof Map + || yamlValue instanceof List + || yamlValue instanceof String + || yamlValue instanceof Boolean + || yamlValue instanceof Integer + || yamlValue instanceof Double) { + mergedResult.put(key, yamlValue); + } else { + throw unknownValueType(key, yamlValue); + } + } + } + } + + private static IllegalArgumentException unknownValueType(String key, Object yamlValue) { + final String msg = "Cannot merge element of unknown type: " + key + ": " + yamlValue.getClass().getName(); + return new IllegalArgumentException(msg); + } + + @SuppressWarnings("unchecked") + private static void mergeYamlLists(Map mergedResult, String key, Object yamlValue) { + if (! (yamlValue instanceof List && mergedResult.get(key) instanceof List)) { + throw new IllegalArgumentException("Cannot merge a list with a non-list: "+key); + } + + List originalList = (List) mergedResult.get(key); + + for (Object o: (List) yamlValue) { + Map oN = (Map) o; + 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) { + continue; + } + + boolean existing = false; + for (Object e: originalList) { + Map 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 (nameE == null) { + continue; + } + + if (nameN.equals(nameE)) { + for (Entry oNe : oN.entrySet()) { + oE.put(oNe.getKey(), oNe.getValue()); + } + existing = true; + break; + } + } + + if (!existing) { + originalList.add(o); + } + } + } +} + 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 7d305a86..30a7ac62 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 @@ -30,9 +30,11 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.ServiceLoader; +import java.util.Map.Entry; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.conf.OnapCommandConfig; 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 383a5b99..93a34577 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 @@ -336,5 +336,6 @@ public class OnapCommandUtils { } } } + } -- cgit 1.2.3-korg