diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-10-14 01:04:20 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-11-21 18:07:19 +0530 |
commit | 8aa599260569b9f063fb85680c95116b00609c31 (patch) | |
tree | dbcaf3e4a5141e801c7b9136240c9d4f7216fac7 /framework/src/main | |
parent | b7500cd5c7caef69f9d8689872eea01876c9fbba (diff) |
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 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'framework/src/main')
26 files changed, 238 insertions, 119 deletions
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<String> initializeSchema(String schema) throws OnapCommandException { + return this.initializeSchema(schema, false); + } + + public List<String> initializeSchema(String schema, boolean validate) throws OnapCommandException { this.setSchemaName(schema); - OnapCommandSchemaLoaderUtils.loadSchema(this, schema, true, false); - this.initializeProfileSchema(); + + List<String> 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<String> 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<String, Class<? extends OnapCommand>> registry = new HashMap<>(); + private Map<String, Class<? extends OnapCommand>> registryProfilePlugins = new HashMap<>(); + private Set<String> availableProductVersions = new HashSet<>(); private String enabledProductVersion = OnapCommandConfg.getEnabledProductVersion(); @@ -111,6 +110,10 @@ public class OnapCommandRegistrar { } + private void registerProfilePlugin(String profile, Class<? extends OnapCommand> cmd) { + this.registryProfilePlugins.put(profile, cmd); + } + /** * Get global registrar. * @@ -156,6 +159,14 @@ public class OnapCommandRegistrar { return cmds; } + public Class<? extends OnapCommand> getProfilePlugin(String profile) throws OnapUnsupportedSchemaProfile { + if (!this.registryProfilePlugins.containsKey(profile)) { + throw new OnapUnsupportedSchemaProfile(profile); + } + + return this.registryProfilePlugins.get(profile); + } + public Set<String> 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<SchemaInfo> schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(); + List<SchemaInfo> schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(true); Map<String, Class<OnapCommand>> 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<String> 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<SchemaInfo> 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<SchemaInfo> 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<String, OnapCommandParameter> 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<String> 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<String> error = cmd.initializeSchema(location, true); List<String> 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<SchemaInfo> list = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(); + List<SchemaInfo> 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<SchemaInfo> discoverOrLoadSchemas() throws OnapCommandException { + public static List<SchemaInfo> discoverOrLoadSchemas(boolean forceRefresh) throws OnapCommandException { List<SchemaInfo> 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<String, ?> schemaYamlMap) { + public static String identitySchemaProfileType(Map<String, ?> 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 <? extends OnapCommand> 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<String, ?> values = null; - try { - values = (Map<String, ?>) new Yaml().load(inputStream); - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); - } + Map<String, ?> 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<String, ?> 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<String, ?> 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<String, ?> 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<String, ?> loadSchema(InputStream stream, String schemaName) throws OnapCommandInvalidSchema { + Map<String, ?> values = null; + try { + values = (Map<String, ?>) 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; + } } diff --git a/framework/src/main/resources/default_input_parameters.yaml b/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml index a8d35eee..e8e08f46 100644 --- a/framework/src/main/resources/default_input_parameters.yaml +++ b/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml @@ -1,4 +1,10 @@ open_cli_schema_version: 1.0 + +info: + product: open-cli + service: default-param + ignore: true + parameters: - name: help type: string diff --git a/framework/src/main/resources/open-cli-schema/basic-login.yaml b/framework/src/main/resources/open-cli-schema/http/basic-login.yaml index dab9652e..dab9652e 100644 --- a/framework/src/main/resources/open-cli-schema/basic-login.yaml +++ b/framework/src/main/resources/open-cli-schema/http/basic-login.yaml diff --git a/framework/src/main/resources/open-cli-schema/basic-logout.yaml b/framework/src/main/resources/open-cli-schema/http/basic-logout.yaml index ef60006c..ef60006c 100644 --- a/framework/src/main/resources/open-cli-schema/basic-logout.yaml +++ b/framework/src/main/resources/open-cli-schema/http/basic-logout.yaml diff --git a/framework/src/main/resources/open-cli-schema/catalog.yaml b/framework/src/main/resources/open-cli-schema/http/catalog.yaml index d3ee9992..d3ee9992 100644 --- a/framework/src/main/resources/open-cli-schema/catalog.yaml +++ b/framework/src/main/resources/open-cli-schema/http/catalog.yaml diff --git a/framework/src/main/resources/default_input_parameters_http.yaml b/framework/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml index 81656ea0..d7fbe03c 100644 --- a/framework/src/main/resources/default_input_parameters_http.yaml +++ b/framework/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml @@ -1,4 +1,10 @@ open_cli_schema_version: 1.0 + +info: + product: open-cli + service: default-param + ignore: true + parameters: - name: host-username type: string diff --git a/framework/src/main/resources/open-cli-schema/schema-validate.yaml b/framework/src/main/resources/open-cli-schema/schema-validate.yaml index 65acfb0e..c17b3a08 100644 --- a/framework/src/main/resources/open-cli-schema/schema-validate.yaml +++ b/framework/src/main/resources/open-cli-schema/schema-validate.yaml @@ -21,6 +21,13 @@ parameters: short_option: i long_option: internal-schema is_optional: false + - name: ocs-version + type: string + description: OCS version + short_option: b + long_option: ocs-version + is_optional: true + default_value: 1.0 results: direction: landscape diff --git a/framework/src/main/resources/open-cli.properties b/framework/src/main/resources/open-cli.properties index 757b0259..813e46dd 100644 --- a/framework/src/main/resources/open-cli.properties +++ b/framework/src/main/resources/open-cli.properties @@ -8,7 +8,7 @@ cli.version=1.0 cli.schema.top_level_params_list=open_cli_schema_version,name,description,parameters,results,http,info cli.schema.top_level_mandatory_list=open_cli_schema_version -cli.schema.info_params_list=product,service,type,author +cli.schema.info_params_list=product,service,type,author,ignore cli.schema.info_params_mandatory_list=product,service cli.schema.input_params_list=name,description,type,short_option,long_option, is_optional,default_value,is_secured,is_include diff --git a/framework/src/main/resources/version.info b/framework/src/main/resources/version.info index fc959d19..edfa61d6 100644 --- a/framework/src/main/resources/version.info +++ b/framework/src/main/resources/version.info @@ -3,5 +3,5 @@ Available products: __AVAILABLE_PRODUCT_VERSIONS__ Enabled product : __ENABLED_PRODUCT_VERSIONS__ To enable a product, use one of following methods: -1. In scripting mode, Set environment variable OPEN_CLI_PRODUCT_IN_USE -2. In interactive mode, set the directive 'use <product>' +1. In scripting mode, set environment variable OPEN_CLI_PRODUCT_IN_USE +2. In interactive mode, use the directive 'use <product>' |