From 8aa599260569b9f063fb85680c95116b00609c31 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam k00365106 Date: Sat, 14 Oct 2017 01:04:20 +0530 Subject: Make schema-validate independent of schema profile Now schema-validate is able to validate schema of any given type supported. Issue-Id: CLI-66 Change-Id: I47ac5e81e9b12dd575e0a4cdc7e8e8bad0f87474 Signed-off-by: Kanagaraj Manickam k00365106 --- .../src/main/java/org/onap/cli/fw/OnapCommand.java | 23 ++++--- .../java/org/onap/cli/fw/OnapCommandRegistrar.java | 45 ++++++++------ .../java/org/onap/cli/fw/cmd/OnapHttpCommand.java | 4 +- .../onap/cli/fw/cmd/OnapSchemaRefreshCommand.java | 16 ++--- .../onap/cli/fw/cmd/OnapSchemaValidateCommand.java | 41 +++++++----- .../main/java/org/onap/cli/fw/conf/Constants.java | 1 + .../org/onap/cli/fw/conf/OnapCommandConfg.java | 1 - .../fw/error/OnapCommandExecutorInfoMissing.java | 30 --------- .../error/OnapCommandParameterOptionConflict.java | 4 +- .../cli/fw/error/OnapUnsupportedSchemaProfile.java | 2 +- .../java/org/onap/cli/fw/info/OnapCommandInfo.java | 12 ++++ .../onap/cli/fw/input/OnapCommandParameter.java | 12 ++-- .../org/onap/cli/fw/output/OnapCommandResult.java | 10 +-- .../cli/fw/output/OnapCommandResultAttribute.java | 4 +- .../onap/cli/fw/output/print/OnapCommandPrint.java | 10 +-- .../cli/fw/utils/OnapCommandDiscoveryUtils.java | 32 ++++++++-- .../cli/fw/utils/OnapCommandSchemaLoaderUtils.java | 72 +++++++++++++++++++--- .../java/org/onap/cli/fw/utils/SchemaInfo.java | 13 ++++ 18 files changed, 216 insertions(+), 116 deletions(-) delete mode 100644 framework/src/main/java/org/onap/cli/fw/error/OnapCommandExecutorInfoMissing.java (limited to 'framework/src/main/java/org') diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java index fe6adf25..d931aaf3 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -135,7 +135,7 @@ public abstract class OnapCommand { } /** - * Initialize this command from command schema. + * Initialize this command from command schema and assumes schema is already validated. * * @throws OnapCommandRegistrationFailed * Command Registration Exception @@ -155,18 +155,27 @@ public abstract class OnapCommand { * ParameterNameConflict Exception * @throws OnapCommandInvalidSchemaVersion * InvalidSchemaVersion Exception + * + * @return List of error strings */ - public void initializeSchema(String schema) throws OnapCommandException { + public List initializeSchema(String schema) throws OnapCommandException { + return this.initializeSchema(schema, false); + } + + public List initializeSchema(String schema, boolean validate) throws OnapCommandException { this.setSchemaName(schema); - OnapCommandSchemaLoaderUtils.loadSchema(this, schema, true, false); - this.initializeProfileSchema(); + + List errors = OnapCommandSchemaLoaderUtils.loadSchema(this, schema, true, validate); + errors.addAll(this.initializeProfileSchema()); this.isInitialzied = true; - } + return errors; + } /** - * Any additional profile based such as http/swagger schema could be initialized. + * Any additional profile based such as http schema could be initialized. */ - protected void initializeProfileSchema() throws OnapCommandException { + protected List initializeProfileSchema() throws OnapCommandException { + return new ArrayList<>(); } /* diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java b/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java index b44279fb..9d1d9d07 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java @@ -17,8 +17,6 @@ package org.onap.cli.fw; import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -33,7 +31,6 @@ import org.onap.cli.fw.error.OnapCommandHelpFailed; import org.onap.cli.fw.error.OnapCommandInvalidRegistration; import org.onap.cli.fw.error.OnapCommandNotFound; import org.onap.cli.fw.error.OnapCommandProductVersionInvalid; -import org.onap.cli.fw.error.OnapCommandRegistrationFailed; import org.onap.cli.fw.error.OnapCommandRegistrationProductInfoMissing; import org.onap.cli.fw.error.OnapUnsupportedSchemaProfile; import org.onap.cli.fw.input.cache.OnapCommandParameterCache; @@ -56,6 +53,8 @@ import org.onap.cli.fw.utils.SchemaInfo; public class OnapCommandRegistrar { private Map> registry = new HashMap<>(); + private Map> registryProfilePlugins = new HashMap<>(); + private Set availableProductVersions = new HashSet<>(); private String enabledProductVersion = OnapCommandConfg.getEnabledProductVersion(); @@ -111,6 +110,10 @@ public class OnapCommandRegistrar { } + private void registerProfilePlugin(String profile, Class cmd) { + this.registryProfilePlugins.put(profile, cmd); + } + /** * Get global registrar. * @@ -156,6 +159,14 @@ public class OnapCommandRegistrar { return cmds; } + public Class getProfilePlugin(String profile) throws OnapUnsupportedSchemaProfile { + if (!this.registryProfilePlugins.containsKey(profile)) { + throw new OnapUnsupportedSchemaProfile(profile); + } + + return this.registryProfilePlugins.get(profile); + } + public Set getAvailableProductVersions() { return this.availableProductVersions; } @@ -205,18 +216,9 @@ public class OnapCommandRegistrar { throw new OnapCommandNotFound(cmdName, version); } - OnapCommand cmd; - try { - Constructor constr = cls.getConstructor(); - cmd = (OnapCommand) constr.newInstance(); - - String schemaName = OnapCommandDiscoveryUtils.getSchemaInfo(cmdName, version).getSchemaName(); - - cmd.initializeSchema(schemaName); - } catch (OnapCommandException | NoSuchMethodException | SecurityException | InstantiationException - | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - throw new OnapCommandRegistrationFailed(cmdName, e); - } + OnapCommand cmd = OnapCommandDiscoveryUtils.loadCommandClass(cls); + String schemaName = OnapCommandDiscoveryUtils.getSchemaInfo(cmdName, version).getSchemaName(); + cmd.initializeSchema(schemaName); return cmd; } @@ -231,6 +233,7 @@ public class OnapCommandRegistrar { if (ano.schema() != null && !ano.schema().isEmpty()) { map.put(ano.schema(), cmd); } else if (ano.type() != null && !ano.type().isEmpty()) { + this.registerProfilePlugin(ano.type(), cmd); map.put(ano.type(), cmd); } else { throw new OnapUnsupportedSchemaProfile(ano.schema()); @@ -242,15 +245,19 @@ public class OnapCommandRegistrar { } private void autoDiscoverSchemas() throws OnapCommandException { - List schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(); + List schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(true); Map> plugins = this.autoDiscoverCommandPlugins(); for (SchemaInfo schema : schemas) { - if (plugins.containsKey(schema.getSchemaProfile())) { + if (schema.isIgnore()) { + continue; + } + + if (plugins.containsKey(schema.getSchemaName())) { + this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName())); + } else if (plugins.containsKey(schema.getSchemaProfile())) { this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaProfile())); - } else if (plugins.containsKey(schema.getSchemaName())) { - this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName())); } else { throw new OnapUnsupportedSchemaProfile(schema.getSchemaURI()); } diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java index 11fc71f3..892f367a 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java @@ -98,8 +98,8 @@ public class OnapHttpCommand extends OnapCommand { } @Override - protected void initializeProfileSchema() throws OnapCommandException { - OnapCommandSchemaLoaderUtils.loadHttpSchema(this, this.getSchemaName(), true, false); + protected List initializeProfileSchema() throws OnapCommandException { + return OnapCommandSchemaLoaderUtils.loadHttpSchema(this, this.getSchemaName(), true, false); } @Override diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommand.java index 3319f03d..40d8eee5 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommand.java @@ -34,13 +34,15 @@ public class OnapSchemaRefreshCommand extends OnapCommand { @Override protected void run() throws OnapCommandException { - List schemas = OnapCommandDiscoveryUtils.discoverSchemas(); - // Will override the existing json file - OnapCommandDiscoveryUtils.persistSchemaInfo(schemas); - - for (int i = 0; i < schemas.size(); i++) { - SchemaInfo schema = schemas.get(i); - this.getResult().getRecordsMap().get("sr.no").getValues().add(String.valueOf(i + 1)); + List schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(true); + int i = 0; + for (SchemaInfo schema : schemas) { + if (schema.isIgnore()) { + continue; + } + + i++; + this.getResult().getRecordsMap().get("sr.no").getValues().add(String.valueOf(i)); this.getResult().getRecordsMap().get("command").getValues().add(schema.getCmdName()); this.getResult().getRecordsMap().get("schema").getValues().add(schema.getSchemaName()); this.getResult().getRecordsMap().get("ocs-version").getValues().add(schema.getVersion()); diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaValidateCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaValidateCommand.java index 15e3775a..ff3f1c98 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaValidateCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaValidateCommand.java @@ -16,16 +16,19 @@ package org.onap.cli.fw.cmd; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + import org.onap.cli.fw.OnapCommand; +import org.onap.cli.fw.OnapCommandRegistrar; import org.onap.cli.fw.OnapCommandSchema; +import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.input.OnapCommandParameter; +import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import org.onap.cli.fw.utils.OnapCommandSchemaLoaderUtils; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - /** * Validate schema command. */ @@ -35,30 +38,40 @@ public class OnapSchemaValidateCommand extends OnapCommand { @Override protected void run() throws OnapCommandException { Map paramMap = getParametersMap(); + OnapCommandParameter locationParam = paramMap.get("schema-location"); String location = String.valueOf(locationParam.getValue()); + OnapCommandParameter interSchemaParam = paramMap.get("internal-schema"); boolean isInternalSchema = Boolean.valueOf(String.valueOf(interSchemaParam.getValue())); - if (isInternalSchema) { + if (isInternalSchema && location.startsWith("/")) { location = location.substring(1); } - List error = OnapCommandSchemaLoaderUtils.loadSchema(new OnapCommand() { - @Override - protected void run() throws OnapCommandException { - } - }, location, true, true); + OnapCommandParameter versionParam = paramMap.get("ocs-version"); + String ocsVersion = String.valueOf(versionParam.getValue()); + String type = OnapCommandDiscoveryUtils.identitySchemaProfileType( + OnapCommandSchemaLoaderUtils.validateSchemaVersion(location, ocsVersion)); - error.addAll(OnapCommandSchemaLoaderUtils.loadHttpSchema(new OnapHttpCommand(), - location, true, true)); + OnapCommand cmd = null; + if (type.equals(Constants.BASIC_SCHEMA_PROFILE)) { + cmd = new OnapCommand() { + @Override + protected void run() throws OnapCommandException { + } + }; + } else { + cmd = OnapCommandDiscoveryUtils.loadCommandClass(OnapCommandRegistrar.getRegistrar().getProfilePlugin(type)); + } + List error = cmd.initializeSchema(location, true); List slNumber = new ArrayList<>(); for (int i = 1; i <= error.size(); i++) { slNumber.add(String.valueOf(i)); } - this.getResult().getRecords().get(0).setValues(slNumber); - this.getResult().getRecords().get(1).setValues(error); + this.getResult().getRecordsMap().get("sl-no").setValues(slNumber); + this.getResult().getRecordsMap().get("error").setValues(error); } } diff --git a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java index 248ea068..4c7581b7 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java @@ -71,6 +71,7 @@ public class Constants { public static final String INFO_SERVICE = "service"; public static final String INFO_TYPE = "type"; public static final String INFO_AUTHOR = "author"; + public static final String INFO_IGNORE = "ignore"; //parameters public static final String PARAMETERS = "parameters"; diff --git a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java index bc01a769..19e7b48b 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java @@ -22,7 +22,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandExecutorInfoMissing.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandExecutorInfoMissing.java deleted file mode 100644 index 3de7438e..00000000 --- a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandExecutorInfoMissing.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.error; - -/** - * Command result initialization failed. - * - */ -public class OnapCommandExecutorInfoMissing extends OnapCommandException { - - private static final long serialVersionUID = 8580121615330415456L; - - public OnapCommandExecutorInfoMissing(String cmd) { - super("0x6002", "Command " + cmd + " excutor info is missing from schema"); - } -} diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandParameterOptionConflict.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandParameterOptionConflict.java index 1ee0a8c3..e7c9e40a 100644 --- a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandParameterOptionConflict.java +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandParameterOptionConflict.java @@ -24,8 +24,8 @@ public class OnapCommandParameterOptionConflict extends OnapCommandException { private static final long serialVersionUID = -3107017890769007297L; - public OnapCommandParameterOptionConflict(String name) { - super("0x7006", "Parameter option " + name + " is in conflict, only one option is allowed with given name"); + public OnapCommandParameterOptionConflict(String schemaName, String name) { + super("0x7006", "In " + schemaName + ", Parameter option " + name + " is in conflict, only one option is allowed with given name"); } } diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapUnsupportedSchemaProfile.java b/framework/src/main/java/org/onap/cli/fw/error/OnapUnsupportedSchemaProfile.java index 098f04df..10375594 100644 --- a/framework/src/main/java/org/onap/cli/fw/error/OnapUnsupportedSchemaProfile.java +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapUnsupportedSchemaProfile.java @@ -26,7 +26,7 @@ public class OnapUnsupportedSchemaProfile extends OnapCommandException { private static final String ERROR_CODE = "0xb004"; - private static final String ERROR_MSG = "Unsupported schema profile"; + private static final String ERROR_MSG = "Unsupported schema profile "; public OnapUnsupportedSchemaProfile(String schema) { super(ERROR_CODE, ERROR_MSG + schema); diff --git a/framework/src/main/java/org/onap/cli/fw/info/OnapCommandInfo.java b/framework/src/main/java/org/onap/cli/fw/info/OnapCommandInfo.java index f3f3bfe1..c52965f9 100644 --- a/framework/src/main/java/org/onap/cli/fw/info/OnapCommandInfo.java +++ b/framework/src/main/java/org/onap/cli/fw/info/OnapCommandInfo.java @@ -31,6 +31,8 @@ public class OnapCommandInfo { private CommandType type = CommandType.CMD; + private boolean ignore = false; + public String getProduct() { return product; } @@ -62,4 +64,14 @@ public class OnapCommandInfo { public void setCommandType(CommandType type) { this.type = type; } + + public boolean isIgnore() { + return ignore; + } + + public void setIgnore(boolean ignore) { + this.ignore = ignore; + } + + } \ No newline at end of file diff --git a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java index 05b5fa29..ac2e8521 100644 --- a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java +++ b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java @@ -16,18 +16,18 @@ package org.onap.cli.fw.input; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.File; +import java.util.List; +import java.util.Map; +import java.util.UUID; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; import org.onap.cli.fw.error.OnapCommandParameterMissing; import org.onap.cli.fw.utils.OnapCommandUtils; -import java.io.File; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; /** * Oclip Command's input parameter. 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 678bcfc9..ae5d86ca 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 @@ -16,6 +16,11 @@ package org.onap.cli.fw.output; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandOutputFormatNotsupported; @@ -24,11 +29,6 @@ import org.onap.cli.fw.input.ParameterType; import org.onap.cli.fw.output.print.OnapCommandPrint; import org.onap.cli.fw.utils.OnapCommandUtils; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** * Oclip Command result holds the final output of the command. * diff --git a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java index ba4d5433..fe4ed32e 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java +++ b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java @@ -16,12 +16,12 @@ package org.onap.cli.fw.output; -import org.onap.cli.fw.input.ParameterType; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.onap.cli.fw.input.ParameterType; + /** * Oclip command output records, helps to define the title and its description while command is defined and during run * time, it captures the value of the output as well. 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 beb2330f..96ce59b2 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 @@ -16,11 +16,6 @@ package org.onap.cli.fw.output.print; -import org.apache.commons.csv.CSVFormat; -import org.apache.commons.csv.CSVPrinter; -import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed; -import org.onap.cli.fw.output.PrintDirection; - import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; @@ -31,6 +26,11 @@ import java.util.List; import java.util.Map; import java.util.StringTokenizer; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed; +import org.onap.cli.fw.output.PrintDirection; + /** * Oclip Command Table print. * 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 69fd2ac4..37220434 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 @@ -26,6 +26,8 @@ import static org.onap.cli.fw.conf.Constants.SCHEMA_PATH_PATERN; import java.io.File; import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -37,6 +39,7 @@ import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.conf.OnapCommandConfg; import org.onap.cli.fw.error.OnapCommandDiscoveryFailed; import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.error.OnapCommandInstantiationFailed; import org.onap.cli.fw.error.OnapCommandInvalidSchema; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; @@ -59,7 +62,7 @@ public class OnapCommandDiscoveryUtils { * exception */ public static SchemaInfo getSchemaInfo(String cmd, String version) throws OnapCommandException { - List list = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(); + List list = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(false); SchemaInfo schemaInfo = null; if (list != null) { for (SchemaInfo schema : list) { @@ -81,9 +84,9 @@ public class OnapCommandDiscoveryUtils { * @throws OnapCommandDiscoveryFailed * exception */ - public static List discoverOrLoadSchemas() throws OnapCommandException { + public static List discoverOrLoadSchemas(boolean forceRefresh) throws OnapCommandException { List schemas = new ArrayList<>(); - if (OnapCommandConfg.isDiscoverAlways() || !OnapCommandDiscoveryUtils.isAlreadyDiscovered()) { + if (forceRefresh || OnapCommandConfg.isDiscoverAlways() || !OnapCommandDiscoveryUtils.isAlreadyDiscovered()) { schemas = OnapCommandDiscoveryUtils.discoverSchemas(); if (!schemas.isEmpty()) { OnapCommandDiscoveryUtils.persistSchemaInfo(schemas); @@ -211,7 +214,7 @@ public class OnapCommandDiscoveryUtils { return resolver.getResources("classpath*:" + pattern); } - static String identitySchemaProfileType(Map schemaYamlMap) { + public static String identitySchemaProfileType(Map schemaYamlMap) { for (String schemeType : OnapCommandConfg.getSchemaAttrInfo(Constants.SCHEMA_TYPES_SUPPORTED)) { if (schemaYamlMap.get(schemeType) != null) { @@ -240,9 +243,9 @@ public class OnapCommandDiscoveryUtils { for (Resource resource : res) { try { - resourceMap = loadSchema(resource); + resourceMap = OnapCommandSchemaLoaderUtils.loadSchema(resource); } catch (OnapCommandException e) { - OnapCommandUtils.LOG.error("Invalid schema " + resource.getURI().toString(), e); + OnapCommandUtils.LOG.error("Ignores invalid schema " + resource.getURI().toString(), e); continue; } @@ -271,6 +274,9 @@ public class OnapCommandDiscoveryUtils { schema.setProduct(infoMap.get(Constants.INFO_PRODUCT).toString()); } + if (infoMap != null && infoMap.get(Constants.INFO_IGNORE) != null) { + schema.setIgnore(infoMap.get(Constants.INFO_IGNORE).toString()); + } schema.setSchemaProfile(identitySchemaProfileType(resourceMap)); extSchemas.add(schema); @@ -299,4 +305,18 @@ public class OnapCommandDiscoveryUtils { return clss; } + /** + * Instantiate command plugin + * @throws OnapCommandInstantiationFailed + */ + public static OnapCommand loadCommandClass(Class cls) throws OnapCommandInstantiationFailed { + try { + Constructor constr = cls.getConstructor(); + return (OnapCommand) constr.newInstance(); + } catch (NoSuchMethodException | SecurityException | InstantiationException + | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new OnapCommandInstantiationFailed(cls.getName(), e); + } + + } } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandSchemaLoaderUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandSchemaLoaderUtils.java index a33b98ac..e115fc02 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandSchemaLoaderUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandSchemaLoaderUtils.java @@ -35,6 +35,7 @@ import static org.onap.cli.fw.conf.Constants.HTTP_MANDATORY_SECTIONS; import static org.onap.cli.fw.conf.Constants.HTTP_SECTIONS; import static org.onap.cli.fw.conf.Constants.INFO; import static org.onap.cli.fw.conf.Constants.INFO_AUTHOR; +import static org.onap.cli.fw.conf.Constants.INFO_IGNORE; import static org.onap.cli.fw.conf.Constants.INFO_PARAMS_LIST; import static org.onap.cli.fw.conf.Constants.INFO_PARAMS_MANDATORY_LIST; import static org.onap.cli.fw.conf.Constants.INFO_PRODUCT; @@ -92,6 +93,7 @@ import org.onap.cli.fw.OnapCommand; import org.onap.cli.fw.ad.OnapService; import org.onap.cli.fw.cmd.CommandType; import org.onap.cli.fw.cmd.OnapHttpCommand; +import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.conf.OnapCommandConfg; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandInvalidSchema; @@ -138,12 +140,7 @@ public class OnapCommandSchemaLoaderUtils { inputStream = OnapCommandSchemaLoaderUtils.loadSchemaFromFile(schemaName); } - Map values = null; - try { - values = (Map) new Yaml().load(inputStream); - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); - } + Map values = OnapCommandSchemaLoaderUtils.loadSchema(inputStream, schemaName); String schemaVersion = ""; if (values.keySet().contains(OPEN_CLI_SCHEMA_VERSION)) { Object obj = values.get(OPEN_CLI_SCHEMA_VERSION); @@ -173,6 +170,11 @@ public class OnapCommandSchemaLoaderUtils { if (includeDefault) { Map defaultParameterMap = includeDefault ? validateSchemaVersion(DEFAULT_PARAMETER_FILE_NAME, cmd.getSchemaVersion()) : new HashMap<>(); + //mrkanag default_parameter is supported only for parameters. + if (defaultParameterMap.containsKey(INFO)) { + defaultParameterMap.remove(Constants.INFO); + } + errors.addAll(OnapCommandSchemaLoaderUtils.parseSchema(cmd, defaultParameterMap, validateSchema)); } @@ -196,6 +198,12 @@ public class OnapCommandSchemaLoaderUtils { if (includeDefault) { Map defaultParameterMap = includeDefault ? validateSchemaVersion(DEFAULT_PARAMETER_HTTP_FILE_NAME, cmd.getSchemaVersion()) : new HashMap<>(); + + //mrkanag default_parameter is supported only for parameters. + if (defaultParameterMap.containsKey(INFO)) { + defaultParameterMap.remove(Constants.INFO); + } + errors.addAll(OnapCommandSchemaLoaderUtils.parseSchema(cmd, defaultParameterMap, validateSchema)); } @@ -297,6 +305,11 @@ public class OnapCommandSchemaLoaderUtils { Object mode = infoMap.get(key1); info.setAuthor(mode.toString()); break; + + case INFO_IGNORE: + Object ignore = infoMap.get(key1); + info.setIgnore(ignore.toString().equalsIgnoreCase(Constants.BOOLEAN_TRUE)); + break; } } @@ -351,7 +364,9 @@ public class OnapCommandSchemaLoaderUtils { case SHORT_OPTION: if (shortOptions.contains(parameter.get(key2))) { - OnapCommandUtils.throwOrCollect(new OnapCommandParameterOptionConflict(parameter.get(key2)), exceptionList, validate); + OnapCommandUtils.throwOrCollect(new OnapCommandParameterOptionConflict( + cmd.getSchemaName(), + parameter.get(key2)), exceptionList, validate); } shortOptions.add(parameter.get(key2)); param.setShortOption(parameter.get(key2)); @@ -359,7 +374,9 @@ public class OnapCommandSchemaLoaderUtils { case LONG_OPTION: if (longOptions.contains(parameter.get(key2))) { - OnapCommandUtils.throwOrCollect(new OnapCommandParameterOptionConflict(parameter.get(key2)), exceptionList, validate); + OnapCommandUtils.throwOrCollect(new OnapCommandParameterOptionConflict( + cmd.getSchemaName(), + parameter.get(key2)), exceptionList, validate); } longOptions.add(parameter.get(key2)); param.setLongOption(parameter.get(key2)); @@ -688,7 +705,7 @@ public class OnapCommandSchemaLoaderUtils { return errorList; } - static InputStream loadSchemaFromFile(String schemaLocation) throws OnapCommandInvalidSchema { + public static InputStream loadSchemaFromFile(String schemaLocation) throws OnapCommandInvalidSchema { File schemaFile = new File(schemaLocation); try { FileInputStream inputFileStream = new FileInputStream(schemaFile); @@ -705,4 +722,41 @@ public class OnapCommandSchemaLoaderUtils { } } + /** + * Get schema map. + * + * @param resource + * resource obj + * @return map + * @throws OnapCommandInvalidSchema + * exception + */ + public static Map loadSchema(Resource resource) throws OnapCommandInvalidSchema { + try { + return OnapCommandSchemaLoaderUtils.loadSchema(resource.getInputStream(), resource.getFilename()); + } catch (IOException e) { + throw new OnapCommandInvalidSchema(resource.getFilename(), e); + } + + } + + /** + * Get schema map. + * + * @param resource + * resource obj + * @return map + * @throws OnapCommandInvalidSchema + * 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 values; + } } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/SchemaInfo.java b/framework/src/main/java/org/onap/cli/fw/utils/SchemaInfo.java index 85d6b1f8..beb02a37 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/SchemaInfo.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/SchemaInfo.java @@ -48,6 +48,8 @@ public class SchemaInfo { private String schemaProfile = Constants.BASIC_SCHEMA_PROFILE; + private String ignore = Constants.BOOLEAN_FALSE; + public String getSchemaName() { return schemaName; } @@ -104,6 +106,17 @@ public class SchemaInfo { this.type = type; } + public boolean isIgnore() { + return Constants.BOOLEAN_TRUE.equalsIgnoreCase(this.getIgnore()); + } + + public String getIgnore() { + return ignore; + } + + public void setIgnore(String ignore) { + this.ignore = ignore; + } } -- cgit 1.2.3-korg