From 20dc78d226aca6cb390586a9e19e522d68d0821b Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam k00365106 Date: Fri, 6 Oct 2017 22:30:23 +0530 Subject: Normalize OnapCommandSchema discovery is optimized to register the plugins and http commands from one function Issue-Id: CLI-66 Change-Id: I3b813862ffd5640c5218c4cced7d10f914dce4ad Signed-off-by: Kanagaraj Manickam k00365106 --- .../src/main/java/org/onap/cli/fw/OnapCommand.java | 2 +- .../java/org/onap/cli/fw/OnapCommandRegistrar.java | 40 +- .../java/org/onap/cli/fw/OnapCommandSchema.java | 23 +- .../main/java/org/onap/cli/fw/ad/OnapService.java | 5 +- .../org/onap/cli/fw/cmd/BasicAuthLoginCommand.java | 2 +- .../onap/cli/fw/cmd/BasicAuthLogoutCommand.java | 2 +- .../java/org/onap/cli/fw/cmd/CatalogCommand.java | 2 +- .../java/org/onap/cli/fw/cmd/OnapHttpCommand.java | 2 +- .../onap/cli/fw/cmd/OnapSchemaRefreshCommand.java | 12 +- .../onap/cli/fw/cmd/OnapSchemaValidateCommand.java | 2 +- .../main/java/org/onap/cli/fw/conf/Constants.java | 171 ++++---- .../org/onap/cli/fw/conf/OnapCommandConfg.java | 13 - .../cli/fw/error/OnapUnsupportedSchemaProfile.java | 35 ++ .../fw/input/cache/OnapCommandParameterCache.java | 2 +- .../java/org/onap/cli/fw/utils/ExternalSchema.java | 93 ----- .../org/onap/cli/fw/utils/OnapCommandUtils.java | 429 ++++++++++----------- .../java/org/onap/cli/fw/utils/SchemaInfo.java | 116 ++++++ .../org/onap/cli/cmd/sample/OnapCommandSample.java | 2 +- .../org/onap/cli/fw/OnapCommandRegistrarTest.java | 6 +- .../cli/fw/cmd/OnapSchemaRefreshCommandTest.java | 5 - .../onap/cli/fw/utils/OnapCommandUtilsTest.java | 18 +- .../cli/fw/utils/OpenCommandRegistrarTest.java | 1 - .../open-cli-schema/sample-test-schema.yaml | 86 +++++ .../src/test/resources/sample-test-schema.yaml | 86 ----- 24 files changed, 591 insertions(+), 564 deletions(-) create mode 100644 framework/src/main/java/org/onap/cli/fw/error/OnapUnsupportedSchemaProfile.java delete mode 100644 framework/src/main/java/org/onap/cli/fw/utils/ExternalSchema.java create mode 100644 framework/src/main/java/org/onap/cli/fw/utils/SchemaInfo.java create mode 100644 framework/src/test/resources/open-cli-schema/sample-test-schema.yaml delete mode 100644 framework/src/test/resources/sample-test-schema.yaml (limited to 'framework/src') 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 ec7e1338..bc9effba 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -67,7 +67,7 @@ public abstract class OnapCommand { protected boolean isInitialzied = false; public String getSchemaVersion() { - return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE; + return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0; } /** 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 090fae47..1a0514ef 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java @@ -24,7 +24,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -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; @@ -35,14 +34,16 @@ 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; 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.output.PrintDirection; import org.onap.cli.fw.output.ResultType; -import org.onap.cli.fw.utils.ExternalSchema; import org.onap.cli.fw.utils.OnapCommandUtils; +import org.onap.cli.fw.utils.SchemaInfo; + /** * Onap Command registrar provides a common place, where every command would get registered automatically when its @@ -116,8 +117,7 @@ public class OnapCommandRegistrar { public static OnapCommandRegistrar getRegistrar() throws OnapCommandException { if (registrar == null) { registrar = new OnapCommandRegistrar(); - registrar.autoDiscover(); - registrar.autoDiscoverHttpSchemas(); + registrar.autoDiscoverSchemas(); } return registrar; @@ -176,8 +176,8 @@ public class OnapCommandRegistrar { * @throws OnapCommandException * exception */ - public List listCommandInfo() throws OnapCommandException { - return OnapCommandUtils.findAllExternalSchemas(); + public List listCommandInfo() throws OnapCommandException { + return OnapCommandUtils.discoverSchemas(); } /** @@ -209,7 +209,7 @@ public class OnapCommandRegistrar { String schemaName; if (cmd.getClass().equals(OnapHttpCommand.class)) { // NOSONAR - schemaName = OnapCommandUtils.loadExternalSchemaFromJson(cmdName, version).getSchemaName(); + schemaName = OnapCommandUtils.getSchemaInfo(cmdName, version).getSchemaName(); } else { schemaName = this.getSchemaFileName(cls); } @@ -222,22 +222,32 @@ public class OnapCommandRegistrar { return cmd; } - private void autoDiscover() throws OnapCommandInvalidRegistration, OnapCommandRegistrationProductInfoMissing { - List> cmds = OnapCommandUtils.findOnapCommands(); + private Map> autoDiscoverCommandPlugins() throws OnapCommandException { + List> cmds = OnapCommandUtils.discoverCommandPlugins(); + Map> map = new HashMap<>(); for (Class cmd : cmds) { if (cmd.isAnnotationPresent(OnapCommandSchema.class)) { OnapCommandSchema ano = cmd.getAnnotation(OnapCommandSchema.class); - this.register(ano.name(), ano.version(), cmd); + map.put(ano.schema(), cmd); } } + + return map; } - private void autoDiscoverHttpSchemas() throws OnapCommandException { - List schemas = OnapCommandUtils.loadExternalSchemasFromJson(); - for (ExternalSchema schema : schemas) { + private void autoDiscoverSchemas() throws OnapCommandException { + List schemas = OnapCommandUtils.discoverOrLoadSchemas(); + + Map> plugins = this.autoDiscoverCommandPlugins(); + + for (SchemaInfo schema : schemas) { if (schema.isHttp()) { - this.register(schema.getCmdName(), schema.getCmdVersion(), OnapHttpCommand.class); + this.register(schema.getCmdName(), schema.getProduct(), OnapHttpCommand.class); + } else if (plugins.containsKey(schema.getSchemaName())) { + this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName())); + } else { + throw new OnapUnsupportedSchemaProfile(schema.getSchemaURI()); } } } @@ -304,7 +314,7 @@ public class OnapCommandRegistrar { OnapCommandResultAttribute attrVer = new OnapCommandResultAttribute(); if (!isEnabledProductVersionOnly) { - attrVer.setName(Constants.PRODUCT_VERSION.toUpperCase()); + attrVer.setName(Constants.INFO_PRODUCT.toUpperCase()); attrVer.setDescription(Constants.DESCRIPTION); attrVer.setScope(OnapCommandResultAttributeScope.SHORT); help.getRecords().add(attrVer); diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommandSchema.java b/framework/src/main/java/org/onap/cli/fw/OnapCommandSchema.java index 2e3e0a5c..47a84955 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommandSchema.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommandSchema.java @@ -34,31 +34,10 @@ import org.onap.cli.fw.cmd.CommandType; @Retention(RetentionPolicy.RUNTIME) @Documented public @interface OnapCommandSchema { - /** - * Command name - * - * @return - */ - String name(); - - /** - * Command version - * - * @return - */ - String version(); - /** * Schema file name placed under class path * * @return */ - String schema() default ""; - - /** - * Command type - * - * @return - */ - String type() default "cmd"; + String schema(); } diff --git a/framework/src/main/java/org/onap/cli/fw/ad/OnapService.java b/framework/src/main/java/org/onap/cli/fw/ad/OnapService.java index 44b79078..7462bb18 100644 --- a/framework/src/main/java/org/onap/cli/fw/ad/OnapService.java +++ b/framework/src/main/java/org/onap/cli/fw/ad/OnapService.java @@ -17,7 +17,6 @@ package org.onap.cli.fw.ad; import org.onap.cli.fw.conf.Constants; -import org.onap.cli.fw.conf.OnapCommandConfg; /** * Onap Service as reported in api catalog. @@ -42,9 +41,9 @@ public class OnapService { * Another mode is 'direct', in which bastPath will be * same as OnapCredentails.ServiceUrl. */ - private String mode = Constants.MODE_CATALOG; + private String mode = Constants.MODE_DIRECT; - private String authType = OnapCommandConfg.getAuthType();; + private String authType = Constants.AUTH_NONE; public String getMode() { return mode; diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLoginCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLoginCommand.java index e4c89a7a..28a86a9b 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLoginCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLoginCommand.java @@ -25,7 +25,7 @@ import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.input.OnapCommandParameter; -@OnapCommandSchema(name = "basic-login", version = "open-cli", type = "auth", schema = "basic-login.yaml") +@OnapCommandSchema(schema = "basic-login.yaml") public class BasicAuthLoginCommand extends OnapHttpCommand { @Override diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLogoutCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLogoutCommand.java index 085f93c8..553b28f8 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLogoutCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLogoutCommand.java @@ -19,7 +19,7 @@ package org.onap.cli.fw.cmd; import org.onap.cli.fw.OnapCommandSchema; import org.onap.cli.fw.error.OnapCommandException; -@OnapCommandSchema(name = "basic-logout", version = "open-cli", type = "auth", schema = "basic-logout.yaml") +@OnapCommandSchema(schema = "basic-logout.yaml") public class BasicAuthLogoutCommand extends OnapHttpCommand { @Override diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/CatalogCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/CatalogCommand.java index d377b78d..34bf90c2 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/CatalogCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/CatalogCommand.java @@ -19,7 +19,7 @@ package org.onap.cli.fw.cmd; import org.onap.cli.fw.OnapCommandSchema; import org.onap.cli.fw.error.OnapCommandException; -@OnapCommandSchema(name = "catalog", version = "open-cli", type = "catalog", schema = "catalog.yaml") +@OnapCommandSchema(schema = "catalog.yaml") public class CatalogCommand extends OnapHttpCommand { @Override 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 3edcee40..860adce6 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 @@ -61,7 +61,7 @@ public class OnapHttpCommand extends OnapCommand { @Override public String getSchemaVersion() { - return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE; + return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0; } public void setSuccessStatusCodes(List successStatusCodes) { 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 a6e2e2b0..0ea4ef08 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 @@ -20,7 +20,7 @@ import org.onap.cli.fw.OnapCommand; import org.onap.cli.fw.OnapCommandSchema; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.output.OnapCommandResultAttribute; -import org.onap.cli.fw.utils.ExternalSchema; +import org.onap.cli.fw.utils.SchemaInfo; import org.onap.cli.fw.utils.OnapCommandUtils; import java.util.ArrayList; @@ -30,15 +30,15 @@ import java.util.List; * Refresh external schema. * */ -@OnapCommandSchema(name = "schema-refresh", version = "open-cli", schema = "schema-refresh.yaml") +@OnapCommandSchema(schema = "schema-refresh.yaml") public class OnapSchemaRefreshCommand extends OnapCommand { @Override protected void run() throws OnapCommandException { - List schemas = OnapCommandUtils.findAllExternalSchemas(); + List schemas = OnapCommandUtils.discoverSchemas(); // Will override the existing json file - OnapCommandUtils.persist(schemas); + OnapCommandUtils.persistSchemaInfo(schemas); List slNumbers = new ArrayList<>(); List cmdNames = new ArrayList<>(); @@ -47,12 +47,12 @@ public class OnapSchemaRefreshCommand extends OnapCommand { List cmdVersions = new ArrayList<>(); for (int i = 0; i < schemas.size(); i++) { - ExternalSchema schema = schemas.get(i); + SchemaInfo schema = schemas.get(i); slNumbers.add(String.valueOf(i + 1)); cmdNames.add(schema.getCmdName()); cmdFiles.add(schema.getSchemaName()); versions.add(schema.getVersion()); - cmdVersions.add(schema.getCmdVersion()); + cmdVersions.add(schema.getProduct()); } for (OnapCommandResultAttribute attribute : this.getResult().getRecords()) { if ("sl-no".equals(attribute.getName())) { 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 3366cf65..b3b9f357 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 @@ -29,7 +29,7 @@ import java.util.Map; /** * Validate schema command. */ -@OnapCommandSchema(name = "schema-validate", version = "open-cli", schema = "schema-validate.yaml") +@OnapCommandSchema(schema = "schema-validate.yaml") public class OnapSchemaValidateCommand extends OnapCommand { @Override 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 eefe7273..a0133404 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 @@ -22,7 +22,12 @@ package org.onap.cli.fw.conf; */ public class Constants { - // schema validation + //config + public static final String CONF = "open-cli.properties"; + public static final String OPEN_IGNORE_AUTH = "cli.ignore_auth"; + public static final String OPEN_CLI_VERSION = "cli.version"; + public static final String HTTP_API_KEY_USE_COOKIES = "cli.http.api_key_use_cookies"; + public static final String TOP_LEVEL_PARAMS_LIST ="cli.schema.top_level_params_list"; public static final String TOP_LEVEL_MANDATORY_LIST ="cli.schema.top_level_mandatory_list"; public static final String SERVICE_PARAMS_LIST ="cli.schema.service_params_list"; @@ -43,64 +48,32 @@ public class Constants { public static final String MODE_VALUES="cli.schema.mode_values"; public static final String COMMAND_TYPE_VALUES="cli.command.type"; + //http connection public static final String SSLCONTEST_TLS = "TLSV1.2"; public static final String APPLICATION_JSON = "application/json"; public static final String OPEN_CLI_PRODUCT_NAME = "cli.product_name"; + //schema + public static final String OPEN_CLI_SCHEMA_VERSION = "open_cli_schema_version"; + public static final String OPEN_CLI_SCHEMA_VERSION_VALUE_1_0 = "1.0"; - //http - public static final String URI = "uri"; - public static final String BODY = "body"; - public static final String METHOD_TYPE = "method"; - public static final String HEADERS = "headers"; - public static final String QUERIES = "queries"; - public static final String COOKIES = "cookies"; + public static final String NAME = "name"; - public static final String HTTP = "http"; - public static final String REQUEST = "request"; - public static final String SAMPLE_RESPONSE = "sample_response"; - public static final String SUCCESS_CODES = "success_codes"; - public static final String RESULT_MAP = "result_map"; + public static final String DESCRIPTION = "description"; //Info public static final String INFO = "info"; + public static final String INFO_PRODUCT = "product"; + public static final String OPEN_OPEN_CLI_PRODUCT_IN_USE_ENV_NAME = "OPEN_CLI_PRODUCT_IN_USE"; + public static final String INFO_SERVICE = "service"; public static final String INFO_TYPE = "type"; public static final String INFO_AUTHOR = "author"; - //swagger - public static final String EXECUTOR = "exec"; - - public static final String API = "api"; - public static final String CLIENT = "client"; - public static final String ENTITY = "entity"; - public static final String METHOD = "method"; - public static final String MULTIPART_ENTITY_NAME = "multipart_entity_name"; - public static final String EXCEPTION = "exception"; - - public static final String SCOPE = "scope"; - - public static final String OPEN_CLI_SCHEMA_VERSION_VALUE = "1.0"; - public static final String DESCRIPTION = "description"; - public static final String COMMAND_TYPE = "type"; - public static final String SERVICE = "service"; + //parameters public static final String PARAMETERS = "parameters"; - - public static final String RESULTS = "results"; - - public static final String OPEN_CLI_SCHEMA_VERSION = "open_cli_schema_version"; - public static final String NAME = "name"; - public static final String VERSION = "version"; - public static final String BASE_PATH = "base_path"; - public static final String AUTH = "auth"; - public static final String AUTH_NONE = "none"; - public static final String AUTH_BASIC = "basic"; - public static final String MODE = "mode"; - public static final String MODE_DIRECT = "direct"; - public static final String MODE_CATALOG = "catalog"; - public static final String SHORT_OPTION = "short_option"; public static final String LONG_OPTION = "long_option"; public static final String TYPE = "type"; @@ -109,13 +82,19 @@ public class Constants { public static final String IS_SECURED = "is_secured"; public static final String IS_INCLUDE = "is_include"; - public static final String DIRECTION = "direction"; - public static final String ATTRIBUTES = "attributes"; + public static final String PARAMETER_TYPE_JSON = "json"; + public static final String PARAMETER_TYPE_YAML = "yaml"; + public static final String PARAMETER_TYPE_STRING = "string"; + public static final String PARAMETER_TYPE_LONG = "long"; + public static final String PARAMETER_TYPE_URL = "url"; + public static final String PARAMETER_TYPE_BOOL = "bool"; + public static final String PARAMETER_TYPE_ARRAY = "array"; + public static final String PARAMETER_TYPE_BINARY = "binary"; + public static final String PARAMETER_TYPE_MAP = "map"; public static final String DEFAULT_PARAMETER_FILE_NAME = "default_input_parameters.yaml"; public static final String DEFAULT_PARAMETER_HTTP_FILE_NAME = "default_input_parameters_http.yaml"; - // Common parameters used across all commands. public static final String DEAFULT_PARAMETER_USERNAME = "host-username"; public static final String DEAFULT_PARAMETER_PASSWORD = "host-password"; public static final String DEAFULT_PARAMETER_HOST_URL = "host-url"; @@ -127,57 +106,83 @@ public class Constants { public static final String DEFAULT_PARAMETER_OUTPUT_NO_TITLE = "no-title"; public static final String DEFAULT_PARAMETER_NO_AUTH = "no-auth"; - // Configuration properties - public static final String CONF = "open-cli.properties"; - public static final String OPEN_IGNORE_AUTH = "cli.ignore_auth"; - public static final String OPEN_CLI_VERSION = "cli.version"; - public static final String OPEN_OPEN_CLI_PRODUCT_IN_USE_ENV_NAME = "OPEN_CLI_PRODUCT_IN_USE"; - public static final String HTTP_API_KEY_USE_COOKIES = "cli.http.api_key_use_cookies"; + //results + public static final String RESULTS = "results"; - public static final String SERVICE_AUTH = "cli.service.auth"; - public static final String SERVICE_AUTH_BASIC_HTTP_HEADERS = "cli.http.basic.common_headers"; + public static final String DIRECTION = "direction"; + public static final String DIRECTION_PORTRAIT = "portrait"; + public static final String DIRECTION_LANDSCAPE = "landscape"; + + public static final String ATTRIBUTES = "attributes"; + + public static final String SCOPE = "scope"; + public static final String RESULT_SCOPE_SHORT = "short"; + public static final String RESULT_SCOPE_LONG = "long"; - // Used while printing the column name during PORTRAIT mode print + //print public static final String PORTRAINT_COLUMN_NAME_PROPERTY = "property"; public static final String PORTRAINT_COLUMN_NAME_VALUE = "value"; - public static final String EXTERNAL_SCHEMA_DIRECTORY = "open-cli-schema"; - public static final String EXTERNAL_YAML_PATTERN = "/**/*.yaml"; - public static final String EXTERNAL_JSON_PATTERN = "/**/*.json"; - public static final String EXTERNAL_SCHEMA_PATH_PATERN = EXTERNAL_SCHEMA_DIRECTORY + EXTERNAL_YAML_PATTERN; + public static final String SCHEMA_DIRECTORY = "open-cli-schema"; + public static final String YAML_PATTERN = "/**/*.yaml"; + public static final String JSON_PATTERN = "/**/*.json"; + public static final String SCHEMA_PATH_PATERN = SCHEMA_DIRECTORY + YAML_PATTERN; public static final String DATA_DIRECTORY = "data"; - public static final String EXTERNAL_DISCOVERY_FILE = "cli-schema.json"; - public static final String DATA_DIRECTORY_JSON_PATTERN = DATA_DIRECTORY - + EXTERNAL_JSON_PATTERN; + public static final String DISCOVERY_FILE = "cli-schema.json"; + public static final String DATA_PATH_JSON_PATTERN = DATA_DIRECTORY + JSON_PATTERN; + public static final String DISCOVER_ALWAYS = "discover_always"; + public static final String PARAM_CACHE_FILE_NAME = "global-profile"; - public static final String PARAMETER_TYPE_JSON = "json"; - public static final String PARAMETER_TYPE_YAML = "yaml"; - public static final String PARAMETER_TYPE_STRING = "string"; - public static final String PARAMETER_TYPE_LONG = "long"; - public static final String PARAMETER_TYPE_URL = "url"; - public static final String PARAMETER_TYPE_BOOL = "bool"; - public static final String PARAMETER_TYPE_ARRAY = "array"; - public static final String PARAMETER_TYPE_BINARY = "binary"; - public static final String PARAMETER_TYPE_MAP = "map"; + //normal + public static final String BASIC_SCHEMA_PROFILE = "basic-schema"; + public static final String HTTP_SCHEMA_PROFILE = "http-schema"; - public static final String BOOLEAN_TRUE = "true"; - public static final String BOOLEAN_FALSE = "false"; + //http + public static final String HTTP = "http"; - public static final String DIRECTION_PORTRAIT = "portrait"; - public static final String DIRECTION_LANDSCAPE = "landscape"; + public static final String SERVICE = "service"; + public static final String VERSION = "version"; + public static final String BASE_PATH = "base_path"; + public static final String AUTH = "auth"; + public static final String AUTH_NONE = "none"; + public static final String AUTH_BASIC = "basic"; + public static final String MODE = "mode"; + public static final String MODE_DIRECT = "direct"; + public static final String MODE_CATALOG = "catalog"; - public static final String RESULT_SCOPE_SHORT = "short"; - public static final String RESULT_SCOPE_LONG = "long"; + public static final String REQUEST = "request"; + public static final String URI = "uri"; + public static final String BODY = "body"; + public static final String METHOD_TYPE = "method"; public static final String POST = "post"; public static final String GET = "get"; public static final String DELETE = "delete"; public static final String PUT = "put"; public static final String HEAD = "delete"; - public static final String DEFAULT_SCHEMA_FILE_NAME = "default_input_parameters.yaml"; + public static final String HEADERS = "headers"; + public static final String QUERIES = "queries"; + public static final String COOKIES = "cookies"; + + public static final String SUCCESS_CODES = "success_codes"; + + public static final String RESULT_MAP = "result_map"; + + public static final String SAMPLE_RESPONSE = "sample_response"; + + //swagger + public static final String EXECUTOR = "exec"; + + public static final String API = "api"; + public static final String CLIENT = "client"; + public static final String ENTITY = "entity"; + public static final String METHOD = "method"; + public static final String MULTIPART_ENTITY_NAME = "multipart_entity_name"; + public static final String EXCEPTION = "exception"; + public static final String BOOLEAN_TRUE = "true"; + public static final String BOOLEAN_FALSE = "false"; - public static final String PRODUCT_VERSION = "product version"; // Error message public static final String SCHEMA_INVALID_DEFAULT_PARAMS_SECTION = "Invalid default_parameter section"; public static final String SCHEMA_FILE_EMPTY = "The schema file cann't be null or empty"; @@ -192,9 +197,6 @@ public class Constants { public static final String HTTP_SAMPLE_RESPONSE_FAILED_PARSING = "The http Sample response json is failed to parse."; public static final String USE_DIRECTIVE = "use"; - public static final String PARAM_CACHE_FILE_NAME = "global-profile"; - - public static final String DISCOVER_ALWAYS = "discover_always"; public static final String SAMPLE_GEN_ENABLED = "cli.sample.gen.enable"; public static final String SAMPLE_GEN_TARGET_FOLDER = "cli.sample.gen.target"; @@ -202,14 +204,13 @@ public class Constants { public static final String SPL_ENTRY_UUID = "uuid"; public static final String SPL_ENTRY_ENV = "env:"; + //auth plugin public static final String AUTH_SERVICE_AUTHORIZATION = "Authorization"; + //catalog plugin public static final String CATALOG_SERVICE_NAME = "catalog-service-name"; - public static final String CATALOG_SERVICE_VERSION = "catalog-service-version"; - public static final String CATALOG_SERVICE_BASE_PATH = "catalog-service-base-path"; - public static final String CATALOG_SERVICE_HOST_URL = "catalog-service-host-url"; private Constants() { 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 7dc9d5a1..60929358 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 @@ -104,10 +104,6 @@ public final class OnapCommandConfg { return prps.getProperty(Constants.OPEN_CLI_PRODUCT_NAME); } - public static String getAuthType() { - return prps.getProperty(Constants.SERVICE_AUTH, Constants.AUTH_BASIC); - } - private static Map getHeaderValues(String headerKey, Map paramMap) { Map mapHeaders = new HashMap (); if (prps.containsKey(headerKey)) { @@ -129,15 +125,6 @@ public final class OnapCommandConfg { return mapHeaders; } - public static Map getBasicCommonHeaders(Map paramMap) { - return getHeaderValues(Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS, paramMap); - } - - public static Map getServiceHeaders(String serviceName, Map paramMap) { - String serviceHeader = Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS+ "." + serviceName; - return getHeaderValues(serviceHeader, paramMap); - } - //mrkanag move this utils class public static List getSchemaAttrInfo(String key) { return Arrays.stream(prps.getProperty(key).split(",")).map(String::trim).collect(Collectors.toList()); // NOSONAR 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 new file mode 100644 index 00000000..098f04df --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapUnsupportedSchemaProfile.java @@ -0,0 +1,35 @@ +/* + * 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 schema not found. + * + */ +public class OnapUnsupportedSchemaProfile extends OnapCommandException { + + private static final long serialVersionUID = -3919580583845280200L; + + private static final String ERROR_CODE = "0xb004"; + + 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/input/cache/OnapCommandParameterCache.java b/framework/src/main/java/org/onap/cli/fw/input/cache/OnapCommandParameterCache.java index 1d9cb549..32035737 100644 --- a/framework/src/main/java/org/onap/cli/fw/input/cache/OnapCommandParameterCache.java +++ b/framework/src/main/java/org/onap/cli/fw/input/cache/OnapCommandParameterCache.java @@ -93,7 +93,7 @@ public class OnapCommandParameterCache { } try { - OnapCommandUtils.persistParams(params, this.profileName); + OnapCommandUtils.persistProfile(params, this.profileName); } catch (OnapCommandPersistProfileFailed e) { throw new RuntimeException(e); // NOSONAR } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/ExternalSchema.java b/framework/src/main/java/org/onap/cli/fw/utils/ExternalSchema.java deleted file mode 100644 index 16675c26..00000000 --- a/framework/src/main/java/org/onap/cli/fw/utils/ExternalSchema.java +++ /dev/null @@ -1,93 +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.utils; - -import org.onap.cli.fw.cmd.CommandType; - -public class ExternalSchema { - - private String schemaName; - private String schemaURI; - private String cmdName; - private String cmdVersion; - private String version; - private String type = CommandType.CMD.name(); - private String http = "false"; - - public String getSchemaName() { - return schemaName; - } - - public void setSchemaName(String schemaName) { - this.schemaName = schemaName; - } - - public String getCmdName() { - return cmdName; - } - - public void setCmdName(String cmdName) { - this.cmdName = cmdName; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getCmdVersion() { - return cmdVersion; - } - - public void setCmdVersion(String cmdVersion) { - this.cmdVersion = cmdVersion; - } - - public String getSchemaURI() { - return schemaURI; - } - - public void setSchemaURI(String schemaURI) { - this.schemaURI = schemaURI; - } - - public String getHttp() { - return http; - } - - public void setHttp(String internal) { - this.http = internal; - } - - public boolean isHttp() { - return this.getHttp().equals("true"); - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - - -} 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 e056456a..7baae094 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 @@ -25,7 +25,7 @@ import static org.onap.cli.fw.conf.Constants.BOOLEAN_VALUE; import static org.onap.cli.fw.conf.Constants.CLIENT; import static org.onap.cli.fw.conf.Constants.COMMAND_TYPE_VALUES; import static org.onap.cli.fw.conf.Constants.DATA_DIRECTORY; -import static org.onap.cli.fw.conf.Constants.DATA_DIRECTORY_JSON_PATTERN; +import static org.onap.cli.fw.conf.Constants.DATA_PATH_JSON_PATTERN; import static org.onap.cli.fw.conf.Constants.DEAFULT_PARAMETER_PASSWORD; import static org.onap.cli.fw.conf.Constants.DEAFULT_PARAMETER_USERNAME; import static org.onap.cli.fw.conf.Constants.DEFAULT_PARAMETER_FILE_NAME; @@ -34,12 +34,10 @@ import static org.onap.cli.fw.conf.Constants.DEFAULT_PARAMETER_NO_AUTH; import static org.onap.cli.fw.conf.Constants.DEFAULT_VALUE; import static org.onap.cli.fw.conf.Constants.DESCRIPTION; import static org.onap.cli.fw.conf.Constants.DIRECTION; +import static org.onap.cli.fw.conf.Constants.DISCOVERY_FILE; import static org.onap.cli.fw.conf.Constants.ENTITY; import static org.onap.cli.fw.conf.Constants.EXCEPTION; import static org.onap.cli.fw.conf.Constants.EXECUTOR; -import static org.onap.cli.fw.conf.Constants.EXTERNAL_DISCOVERY_FILE; -import static org.onap.cli.fw.conf.Constants.EXTERNAL_SCHEMA_DIRECTORY; -import static org.onap.cli.fw.conf.Constants.EXTERNAL_SCHEMA_PATH_PATERN; import static org.onap.cli.fw.conf.Constants.HEADERS; import static org.onap.cli.fw.conf.Constants.HTTP; import static org.onap.cli.fw.conf.Constants.HTTP_BODY_FAILED_PARSING; @@ -78,8 +76,10 @@ import static org.onap.cli.fw.conf.Constants.RESULT_MAP; import static org.onap.cli.fw.conf.Constants.RESULT_PARAMS_LIST; import static org.onap.cli.fw.conf.Constants.RESULT_PARAMS_MANDATORY_LIST; import static org.onap.cli.fw.conf.Constants.SAMPLE_RESPONSE; +import static org.onap.cli.fw.conf.Constants.SCHEMA_DIRECTORY; import static org.onap.cli.fw.conf.Constants.SCHEMA_FILE_NOT_EXIST; import static org.onap.cli.fw.conf.Constants.SCHEMA_FILE_WRONG_EXTN; +import static org.onap.cli.fw.conf.Constants.SCHEMA_PATH_PATERN; import static org.onap.cli.fw.conf.Constants.SCOPE; import static org.onap.cli.fw.conf.Constants.SERVICE; import static org.onap.cli.fw.conf.Constants.SERVICE_PARAMS_LIST; @@ -149,6 +149,8 @@ import org.onap.cli.fw.output.OnapCommandResultAttributeScope; import org.onap.cli.fw.output.PrintDirection; import org.onap.cli.fw.output.ResultType; import org.onap.cli.fw.run.OnapCommandExecutor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; @@ -166,6 +168,7 @@ import net.minidev.json.JSONObject; */ public class OnapCommandUtils { + private static Logger LOG = LoggerFactory.getLogger(OnapCommandUtils.class); /** * Private constructor. */ @@ -187,7 +190,7 @@ public class OnapCommandUtils { InputStream inputStream = OnapCommandUtils.class.getClassLoader().getResourceAsStream(schemaName); try { - Resource resource = getExternalResource(schemaName, EXTERNAL_SCHEMA_PATH_PATERN); + Resource resource = getExternalResource(schemaName, SCHEMA_PATH_PATERN); if (resource != null) { inputStream = resource.getInputStream(); @@ -243,14 +246,7 @@ public class OnapCommandUtils { * @param schemaName schema name * @param includeDefault include if default * @param validateSchema flag to represent validation - * @throws OnapCommandParameterNameConflict param name conflict exception - * @throws OnapCommandParameterOptionConflict param option conflict exception - * @throws OnapCommandInvalidParameterType invalid param type exception - * @throws OnapCommandInvalidPrintDirection invalid print direction exception - * @throws OnapCommandInvalidResultAttributeScope invalid scope exception - * @throws OnapCommandSchemaNotFound schema not found - * @throws OnapCommandInvalidSchema invalid schema - * @throws OnapCommandInvalidSchemaVersion invalid schema version + * @throws OnapCommandException on error */ public static List loadSchema(OnapCommand cmd, String schemaName, boolean includeDefault, boolean validateSchema) throws OnapCommandException { @@ -300,47 +296,6 @@ public class OnapCommandUtils { } } - private static void throwOrCollect(OnapCommandException ex, List list, - boolean shouldCollectException) throws OnapCommandException { - if (shouldCollectException) { - list.add(ex.getMessage()); - } else { - throw ex; - } - } - - private static void validateTags(List schemaErrors, Map yamlMap, - List totalParams, List mandatoryParams, - String section) { - //mrkanag capture invalid entries as well - for (String param : totalParams) { - boolean isMandatory = mandatoryParams.contains(param); - boolean isYamlContains = yamlMap.containsKey(param); - if (isMandatory) { - if (!isYamlContains) { - schemaErrors.add("Mandatory attribute '" + param + "' is missing under '" + section + "'"); - } else { - String value = String.valueOf(yamlMap.get(param)); - if (value == null || value.isEmpty()) { - schemaErrors.add("Mandatory attribute '" + param + "' under '" + section - + "' shouldn't be null or empty"); - } - } - } - } - } - - /** - * Validate Boolean. - * - * @param toValidate - * string - * @return boolean - */ - protected static boolean validateBoolean(String toValidate) { - return OnapCommandConfg.getSchemaAttrInfo(BOOLEAN_VALUE).contains(toValidate.toLowerCase()); - } - private static List parseSchema(OnapCommand cmd, final Map values, boolean validate) throws OnapCommandException { @@ -658,111 +613,6 @@ public class OnapCommandUtils { return exceptionList; } - private static String emptySection(String section) { - return "The section '" + section + ":' cann't be null or empty"; - } - - private static String invalidBooleanValueMessage(String section, String attribute, String value) { - return "The value '" + value + "' of '" + attribute + "' present under '" + section + "' should be boolean"; - } - - private static Set validateHttpQueries(Map requestMap) { - Map queries = (Map) requestMap.get(QUERIES); - Set queryParamNames = new HashSet<>(); - if (queries != null) { - for (Entry entry : queries.entrySet()) { - parseParameters(String.valueOf(entry.getValue()), queryParamNames); - } - } - return queryParamNames; - } - - - private static Set validateHttpHeaders(Map requestMap) { - - Map headers = (Map) requestMap.get(HEADERS); - Set headerParamNames = new HashSet<>(); - if (headers != null) { - for (Entry entry : headers.entrySet()) { - parseParameters(String.valueOf(entry.getValue()), headerParamNames); - } - } - return headerParamNames; - } - - private static Set validateHttpBody(List errorList, Map requestMap) { - Set bodyParamNames = new HashSet<>(); - Object bodyString = requestMap.get(BODY); - if (bodyString == null) { - return bodyParamNames; - } - - String body = String.valueOf(bodyString); - JSONObject obj = null; - try { - obj = new ObjectMapper().readValue(body, JSONObject.class); - } catch (IOException e1) { // NOSONAR - errorList.add(HTTP_BODY_FAILED_PARSING); - } - if (obj == null || "".equals(obj.toString())) { - errorList.add(HTTP_BODY_JSON_EMPTY); - } - parseParameters(body, bodyParamNames); - - return bodyParamNames; - } - - private static Set validateHttpUri(List errorList, Map requestMap) { - Set uriParamNames = new HashSet<>(); - String uri = (String) requestMap.get(URI); - if (uri == null || uri.isEmpty()) { - errorList.add(emptySection(URI)); - return uriParamNames; - } - parseParameters(uri, uriParamNames); - return uriParamNames; - } - - private static void parseParameters(String line, Set paramNames) { - - int currentIdx = 0; - while (currentIdx < line.length()) { - int idxS = line.indexOf("${", currentIdx); - if (idxS == -1) { - break; - } - int idxE = line.indexOf("}", idxS); - String paramName = line.substring(idxS + 2, idxE); - paramNames.add(paramName.trim()); - - currentIdx = idxE + 1; - } - - } - - private static Set getRequestParams(Map yamlMap) { - - Set set = new HashSet<>(); - - @SuppressWarnings("unchecked") - List> inputParams = (List>) yamlMap.get(PARAMETERS); - - if (inputParams != null) { - for (Map map : inputParams) { - for (Entry entry : map.entrySet()) { - Object key = entry.getKey(); - - if (NAME.equals(key)) { - set.add(String.valueOf(entry.getValue())); - break; - } - } - } - } - - return set; - } - /** * Load the schema. * @@ -824,22 +674,8 @@ public class OnapCommandUtils { * OnapHttpCommand * @param schemaName * schema name - * @throws OnapCommandParameterNameConflict - * param name conflict exception - * @throws OnapCommandParameterOptionConflict - * param option conflict exception - * @throws OnapCommandInvalidParameterType - * invalid param type exception - * @throws OnapCommandInvalidPrintDirection - * invalid print direction exception - * @throws OnapCommandInvalidResultAttributeScope - * invalid scope exception - * @throws OnapCommandSchemaNotFound - * schema not found - * @throws OnapCommandInvalidSchema - * invalid schema - * @throws OnapCommandInvalidSchemaVersion - * invalid schema version + * @throws OnapCommandException + * on error */ private static ArrayList parseHttpSchema(OnapHttpCommand cmd, final Map values, @@ -989,6 +825,152 @@ public class OnapCommandUtils { return errorList; } + + private static void throwOrCollect(OnapCommandException ex, List list, boolean shouldCollectException) + throws OnapCommandException { + if (shouldCollectException) { + list.add(ex.getMessage()); + } else { + throw ex; + } + } + + private static void validateTags(List schemaErrors, Map yamlMap, List totalParams, + List mandatoryParams, String section) { + // mrkanag capture invalid entries as well + for (String param : totalParams) { + boolean isMandatory = mandatoryParams.contains(param); + boolean isYamlContains = yamlMap.containsKey(param); + if (isMandatory) { + if (!isYamlContains) { + schemaErrors.add("Mandatory attribute '" + param + "' is missing under '" + section + "'"); + } else { + String value = String.valueOf(yamlMap.get(param)); + if (value == null || value.isEmpty()) { + schemaErrors.add("Mandatory attribute '" + param + "' under '" + section + + "' shouldn't be null or empty"); + } + } + } + } + } + + /** + * Validate Boolean. + * + * @param toValidate + * string + * @return boolean + */ + protected static boolean validateBoolean(String toValidate) { + return OnapCommandConfg.getSchemaAttrInfo(BOOLEAN_VALUE).contains(toValidate.toLowerCase()); + } + + private static String emptySection(String section) { + return "The section '" + section + ":' cann't be null or empty"; + } + + private static String invalidBooleanValueMessage(String section, String attribute, String value) { + return "The value '" + value + "' of '" + attribute + "' present under '" + section + "' should be boolean"; + } + + private static Set validateHttpQueries(Map requestMap) { + Map queries = (Map) requestMap.get(QUERIES); + Set queryParamNames = new HashSet<>(); + if (queries != null) { + for (Entry entry : queries.entrySet()) { + parseParameters(String.valueOf(entry.getValue()), queryParamNames); + } + } + return queryParamNames; + } + + + private static Set validateHttpHeaders(Map requestMap) { + + Map headers = (Map) requestMap.get(HEADERS); + Set headerParamNames = new HashSet<>(); + if (headers != null) { + for (Entry entry : headers.entrySet()) { + parseParameters(String.valueOf(entry.getValue()), headerParamNames); + } + } + return headerParamNames; + } + + private static Set validateHttpBody(List errorList, Map requestMap) { + Set bodyParamNames = new HashSet<>(); + Object bodyString = requestMap.get(BODY); + if (bodyString == null) { + return bodyParamNames; + } + + String body = String.valueOf(bodyString); + JSONObject obj = null; + try { + obj = new ObjectMapper().readValue(body, JSONObject.class); + } catch (IOException e1) { // NOSONAR + errorList.add(HTTP_BODY_FAILED_PARSING); + } + if (obj == null || "".equals(obj.toString())) { + errorList.add(HTTP_BODY_JSON_EMPTY); + } + parseParameters(body, bodyParamNames); + + return bodyParamNames; + } + + private static Set validateHttpUri(List errorList, Map requestMap) { + Set uriParamNames = new HashSet<>(); + String uri = (String) requestMap.get(URI); + if (uri == null || uri.isEmpty()) { + errorList.add(emptySection(URI)); + return uriParamNames; + } + parseParameters(uri, uriParamNames); + return uriParamNames; + } + + private static void parseParameters(String line, Set paramNames) { + + int currentIdx = 0; + while (currentIdx < line.length()) { + int idxS = line.indexOf("${", currentIdx); + if (idxS == -1) { + break; + } + int idxE = line.indexOf("}", idxS); + String paramName = line.substring(idxS + 2, idxE); + paramNames.add(paramName.trim()); + + currentIdx = idxE + 1; + } + + } + + private static Set getRequestParams(Map yamlMap) { + + Set set = new HashSet<>(); + + @SuppressWarnings("unchecked") + List> inputParams = (List>) yamlMap.get(PARAMETERS); + + if (inputParams != null) { + for (Map map : inputParams) { + for (Entry entry : map.entrySet()) { + Object key = entry.getKey(); + + if (NAME.equals(key)) { + set.add(String.valueOf(entry.getValue())); + break; + } + } + } + } + + return set; + } + private static void validateHttpResultMap(List errorList, Map values) throws OnapCommandException { Map valMap = (Map) values.get(HTTP); List> attributes = (List>) ((Map)values.get(RESULTS)).get(ATTRIBUTES); @@ -1226,7 +1208,7 @@ public class OnapCommandUtils { * * @return list */ - public static List> findOnapCommands() { + public static List> discoverCommandPlugins() { ServiceLoader loader = ServiceLoader.load(OnapCommand.class); List> clss = new ArrayList<>(); for (OnapCommand implClass : loader) { @@ -1539,7 +1521,6 @@ public class OnapCommandUtils { inp.getReqQueries().put(h, replaceLineFromInputParameters(value, params)); } - //mrkanag replaceLineFromInputParameters for result_map, to support input param in result output return inp; } @@ -1620,44 +1601,62 @@ public class OnapCommandUtils { * @throws OnapCommandInvalidSchema * exception */ - public static List findAllExternalSchemas() throws OnapCommandException { - List extSchemas = new ArrayList<>(); + public static List discoverSchemas() throws OnapCommandException { + List extSchemas = new ArrayList<>(); try { - Resource[] res = getExternalResources(EXTERNAL_SCHEMA_PATH_PATERN); + Resource[] res = getExternalResources(SCHEMA_PATH_PATERN); if (res != null && res.length > 0) { Map resourceMap; + for (Resource resource : res) { - resourceMap = getExternalSchemaMap(resource); + resourceMap = loadSchema(resource); + if (resourceMap != null && resourceMap.size() > 0) { - ExternalSchema schema = new ExternalSchema(); - schema.setSchemaName(resource.getFilename()); + SchemaInfo schema = new SchemaInfo(); + schema.setSchemaURI(resource.getURI().toString()); - schema.setCmdName((String) resourceMap.get(NAME)); + Object obj = resourceMap.get(OPEN_CLI_SCHEMA_VERSION); schema.setVersion(obj.toString()); + if (!schema.getVersion().equalsIgnoreCase(Constants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0)) { + LOG.info("Unsupported Schema version found " + schema.getSchemaURI()); + continue; + } + + schema.setSchemaName(resource.getFilename()); + schema.setCmdName((String) resourceMap.get(NAME)); + Map infoMap = (Map) resourceMap.get(Constants.INFO); - if (infoMap != null && infoMap.get(Constants.COMMAND_TYPE) != null) { - schema.setType(infoMap.get(Constants.COMMAND_TYPE).toString()); + if (infoMap != null && infoMap.get(Constants.INFO_TYPE) != null) { + schema.setType(infoMap.get(Constants.INFO_TYPE).toString()); } + if (infoMap != null && infoMap.get(Constants.INFO_PRODUCT) != null) { - schema.setCmdVersion(infoMap.get(Constants.INFO_PRODUCT).toString()); + schema.setProduct(infoMap.get(Constants.INFO_PRODUCT).toString()); } - if (resourceMap.get(Constants.HTTP) != null) { - schema.setHttp("true"); - } + schema.setSchemaProfile(identitySchemaProfileType(resourceMap)); + extSchemas.add(schema); } } } } catch (IOException e) { - throw new OnapCommandDiscoveryFailed(EXTERNAL_SCHEMA_DIRECTORY, e); + throw new OnapCommandDiscoveryFailed(SCHEMA_DIRECTORY, e); } return extSchemas; } + private static String identitySchemaProfileType(Map schemaYamlMap) { + if (schemaYamlMap.get(Constants.HTTP) != null) { + return Constants.HTTP_SCHEMA_PROFILE; + } + + return Constants.BASIC_SCHEMA_PROFILE; + } + /** * Returns all resources available under certain directory in class-path. * @@ -1704,7 +1703,7 @@ public class OnapCommandUtils { * @throws OnapCommandInvalidSchema * exception */ - public static Map getExternalSchemaMap(Resource resource) throws OnapCommandInvalidSchema { + public static Map loadSchema(Resource resource) throws OnapCommandInvalidSchema { Map values = null; try { values = (Map) new Yaml().load(resource.getInputStream()); @@ -1722,24 +1721,24 @@ public class OnapCommandUtils { * @throws OnapCommandDiscoveryFailed * exception */ - public static void persist(List schemas) throws OnapCommandDiscoveryFailed { + public static void persistSchemaInfo(List schemas) throws OnapCommandDiscoveryFailed { if (schemas != null) { try { Resource[] resources = getExternalResources(DATA_DIRECTORY); if (resources != null && resources.length == 1) { String path = resources[0].getURI().getPath(); - File file = new File(path + File.separator + EXTERNAL_DISCOVERY_FILE); + File file = new File(path + File.separator + DISCOVERY_FILE); ObjectMapper mapper = new ObjectMapper(); mapper.writerWithDefaultPrettyPrinter().writeValue(file, schemas); } } catch (IOException e1) { throw new OnapCommandDiscoveryFailed(DATA_DIRECTORY, - EXTERNAL_DISCOVERY_FILE, e1); + DISCOVERY_FILE, e1); } } } - public static void persistParams(List params, String profileName) throws OnapCommandPersistProfileFailed { + public static void persistProfile(List params, String profileName) throws OnapCommandPersistProfileFailed { if (params != null) { try { Resource[] resources = getExternalResources(DATA_DIRECTORY); @@ -1762,17 +1761,17 @@ public class OnapCommandUtils { * @throws OnapCommandDiscoveryFailed * exception */ - public static boolean isJsonFileDiscovered() throws OnapCommandDiscoveryFailed { + public static boolean isAlreadyDiscovered() throws OnapCommandDiscoveryFailed { Resource resource = null; try { - resource = getExternalResource(EXTERNAL_DISCOVERY_FILE, - DATA_DIRECTORY_JSON_PATTERN); + resource = getExternalResource(DISCOVERY_FILE, + DATA_PATH_JSON_PATTERN); if (resource != null) { return true; } } catch (IOException e) { throw new OnapCommandDiscoveryFailed(DATA_DIRECTORY, - EXTERNAL_DISCOVERY_FILE, e); + DISCOVERY_FILE, e); } return false; @@ -1787,26 +1786,26 @@ public class OnapCommandUtils { * @throws OnapCommandDiscoveryFailed * exception */ - public static List loadExternalSchemasFromJson() throws OnapCommandException { - List schemas = new ArrayList<>(); - if (OnapCommandConfg.isDiscoverAlways() || !isJsonFileDiscovered()) { - schemas = findAllExternalSchemas(); + public static List discoverOrLoadSchemas() throws OnapCommandException { + List schemas = new ArrayList<>(); + if (OnapCommandConfg.isDiscoverAlways() || !isAlreadyDiscovered()) { + schemas = discoverSchemas(); if (!schemas.isEmpty()) { - persist(schemas); + persistSchemaInfo(schemas); } } else { try { - Resource resource = getExternalResource(EXTERNAL_DISCOVERY_FILE, - DATA_DIRECTORY_JSON_PATTERN); + Resource resource = getExternalResource(DISCOVERY_FILE, + DATA_PATH_JSON_PATTERN); if (resource != null) { File file = new File(resource.getURI().getPath()); ObjectMapper mapper = new ObjectMapper(); - ExternalSchema[] list = mapper.readValue(file, ExternalSchema[].class); + SchemaInfo[] list = mapper.readValue(file, SchemaInfo[].class); schemas.addAll(Arrays.asList(list)); } } catch (IOException e) { throw new OnapCommandDiscoveryFailed(DATA_DIRECTORY, - EXTERNAL_DISCOVERY_FILE, e); + DISCOVERY_FILE, e); } } @@ -1818,7 +1817,7 @@ public class OnapCommandUtils { try { Resource resource = getExternalResource(profileName + ".json", - DATA_DIRECTORY_JSON_PATTERN); + DATA_PATH_JSON_PATTERN); if (resource != null) { File file = new File(resource.getURI().getPath()); ObjectMapper mapper = new ObjectMapper(); @@ -1843,12 +1842,12 @@ public class OnapCommandUtils { * @throws OnapCommandDiscoveryFailed * exception */ - public static ExternalSchema loadExternalSchemaFromJson(String cmd, String version) throws OnapCommandException { - List list = loadExternalSchemasFromJson(); - ExternalSchema schemaStr = null; + public static SchemaInfo getSchemaInfo(String cmd, String version) throws OnapCommandException { + List list = discoverOrLoadSchemas(); + SchemaInfo schemaStr = null; if (list != null) { - for (ExternalSchema schema : list) { - if (cmd.equals(schema.getCmdName()) && version.equals(schema.getCmdVersion())) { + for (SchemaInfo schema : list) { + if (cmd.equals(schema.getCmdName()) && version.equals(schema.getProduct())) { schemaStr = schema; break; } 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 new file mode 100644 index 00000000..462fcceb --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/utils/SchemaInfo.java @@ -0,0 +1,116 @@ +/* + * 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.utils; + +import org.onap.cli.fw.cmd.CommandType; +import org.onap.cli.fw.conf.Constants; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +/** + * SchemaInfo is used in discovery caching. + * + */ +public class SchemaInfo { + + /** + * Name of the schema file name + */ + private String schemaName; + + /** + * Schema location in complete path + */ + private String schemaURI; + + private String cmdName; + + private String product; + + /** + * OCS version + */ + private String version; + + private String type = CommandType.CMD.name(); + + private String schemaProfile = Constants.BASIC_SCHEMA_PROFILE; + + public String getSchemaName() { + return schemaName; + } + + public void setSchemaName(String schemaName) { + this.schemaName = schemaName; + } + + public String getCmdName() { + return cmdName; + } + + public void setCmdName(String cmdName) { + this.cmdName = cmdName; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getProduct() { + return product; + } + + public void setProduct(String cmdVersion) { + this.product = cmdVersion; + } + + public String getSchemaURI() { + return schemaURI; + } + + public void setSchemaURI(String schemaURI) { + this.schemaURI = schemaURI; + } + + public String getSchemaProfile() { + return schemaProfile; + } + + public void setSchemaProfile(String internal) { + this.schemaProfile = internal; + } + + @JsonIgnore + public boolean isHttp() { + return this.getSchemaProfile().equals(Constants.HTTP_SCHEMA_PROFILE); + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + + +} diff --git a/framework/src/test/java/org/onap/cli/cmd/sample/OnapCommandSample.java b/framework/src/test/java/org/onap/cli/cmd/sample/OnapCommandSample.java index 87527f07..c76df290 100644 --- a/framework/src/test/java/org/onap/cli/cmd/sample/OnapCommandSample.java +++ b/framework/src/test/java/org/onap/cli/cmd/sample/OnapCommandSample.java @@ -25,7 +25,7 @@ import org.onap.cli.fw.error.OnapCommandExecutionFailed; * This command helps to test the Command functionalities. * */ -@OnapCommandSchema(name = "sample-test", version="open-cli", schema = "sample-test-schema.yaml") +@OnapCommandSchema(schema = "sample-test-schema.yaml") public class OnapCommandSample extends OnapCommand { public OnapCommandSample() { diff --git a/framework/src/test/java/org/onap/cli/fw/OnapCommandRegistrarTest.java b/framework/src/test/java/org/onap/cli/fw/OnapCommandRegistrarTest.java index af9cc524..03744345 100644 --- a/framework/src/test/java/org/onap/cli/fw/OnapCommandRegistrarTest.java +++ b/framework/src/test/java/org/onap/cli/fw/OnapCommandRegistrarTest.java @@ -90,7 +90,7 @@ public class OnapCommandRegistrarTest { @Test public void onapCommandRegistrationFailedTest() throws OnapCommandException { - @OnapCommandSchema(name = "Test2", version= "open-cli", schema = "sample-test-schema.yaml") + @OnapCommandSchema(schema = "sample-test-schema.yaml") class Test extends OnapCommand { @Override @@ -161,7 +161,7 @@ public class OnapCommandRegistrarTest { } } -@OnapCommandSchema(name = OnapCommandTest.CMD_NAME, version = "open-cli", schema = "sample-test-schema.yaml") +@OnapCommandSchema(schema = "sample-test-schema.yaml") class OnapCommandTest extends OnapCommand { public OnapCommandTest() { @@ -176,7 +176,7 @@ class OnapCommandTest extends OnapCommand { } -@OnapCommandSchema(name = OnapCommandTest1.CMD_NAME, version = "open-cli", schema = "test-schema.yaml") +@OnapCommandSchema(schema = "test-schema.yaml") class OnapCommandTest1 extends OnapCommand { public OnapCommandTest1() { diff --git a/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommandTest.java b/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommandTest.java index 8c96facd..b9345f77 100644 --- a/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommandTest.java +++ b/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommandTest.java @@ -42,10 +42,5 @@ public class OnapSchemaRefreshCommandTest { String version = onapCommandResultAttribute.get(4).getValues().get(0); assertTrue(s1Number.equalsIgnoreCase("1")); - assertTrue(cmdName.equalsIgnoreCase("sample-test1")); - assertTrue(cmdFile.equalsIgnoreCase("sample-test1-schema-http.yaml")); - assertTrue(version.equalsIgnoreCase("1.0")); - assertTrue(cmdVer.equalsIgnoreCase("open-cli")); - } } diff --git a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java index 82869fa3..db94a555 100644 --- a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java +++ b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java @@ -81,7 +81,7 @@ public class OnapCommandUtilsTest { @Test public void externalSchemaTest() { - ExternalSchema schema = new ExternalSchema(); + SchemaInfo schema = new SchemaInfo(); schema.setCmdName("cmdName"); schema.setSchemaName("schemaName"); schema.setVersion("version"); @@ -254,7 +254,7 @@ public class OnapCommandUtilsTest { @Test public void findOnapCommandsTest() { - List> cmds = OnapCommandUtils.findOnapCommands(); + List> cmds = OnapCommandUtils.discoverCommandPlugins(); assertTrue(cmds.size() == 6); } @@ -405,21 +405,21 @@ public class OnapCommandUtilsTest { public void zendExceptionTest4() throws OnapCommandException { mockExternalResources(); - OnapCommandUtils.loadExternalSchemasFromJson(); + OnapCommandUtils.discoverOrLoadSchemas(); } @Test(expected = OnapCommandException.class) public void zendExceptionTest5() throws OnapCommandException { mockExternalResources(); - OnapCommandUtils.findAllExternalSchemas(); + OnapCommandUtils.discoverSchemas(); } @Test(expected = OnapCommandException.class) public void zendExceptionTest6() throws OnapCommandException { mockExternalResources(); - OnapCommandUtils.persist(new ArrayList()); + OnapCommandUtils.persistSchemaInfo(new ArrayList()); } @Test(expected = OnapCommandHelpFailed.class) @@ -442,21 +442,21 @@ public class OnapCommandUtilsTest { assert info != null; } - @OnapCommandSchema(name = "sample-test-info", version = "open-cli", schema = "sample-test-info.yaml") + @OnapCommandSchema(schema = "sample-test-info.yaml") class OnapCommandSampleInfo extends OnapCommand { @Override protected void run() throws OnapCommandException { } } - @OnapCommandSchema(name = "sample-test", version = "open-cli", schema = "sample-test-schema.yaml") + @OnapCommandSchema(schema = "sample-test-schema.yaml") class OnapCommandSample extends OnapCommand { @Override protected void run() throws OnapCommandException { } } - @OnapCommandSchema(name = "sample-swagger-test", version = "open-cli", schema = "sample-test-schema-swagger.yaml") + @OnapCommandSchema(schema = "sample-test-schema-swagger.yaml") class OnapSwaggerBasedCommandSample extends OnapSwaggerCommand { @Override @@ -464,7 +464,7 @@ public class OnapCommandUtilsTest { } } - @OnapCommandSchema(name = "sample-http-test", version = "open-cli", schema = "sample-test-schema-http.yaml") + @OnapCommandSchema(schema = "sample-test-schema-http.yaml") class OnapHttpCommandSample extends OnapHttpCommand { @Override diff --git a/framework/src/test/java/org/onap/cli/fw/utils/OpenCommandRegistrarTest.java b/framework/src/test/java/org/onap/cli/fw/utils/OpenCommandRegistrarTest.java index f7ba2355..648746ca 100644 --- a/framework/src/test/java/org/onap/cli/fw/utils/OpenCommandRegistrarTest.java +++ b/framework/src/test/java/org/onap/cli/fw/utils/OpenCommandRegistrarTest.java @@ -36,7 +36,6 @@ public class OpenCommandRegistrarTest { OnapCommand cmd = registrar.get("sample-test"); cmd.printVersion(); registrar.listCommands(); - } } diff --git a/framework/src/test/resources/open-cli-schema/sample-test-schema.yaml b/framework/src/test/resources/open-cli-schema/sample-test-schema.yaml new file mode 100644 index 00000000..e3b415b1 --- /dev/null +++ b/framework/src/test/resources/open-cli-schema/sample-test-schema.yaml @@ -0,0 +1,86 @@ +open_cli_schema_version: 1.0 +name: sample-test +description: Onap sample command to test the command features +info: + product: open-cli + service: test + type: cmd + author: Kanagaraj Manickam mkr1481@gmail.com +parameters: + - name: bool-param + type: bool + description: Onap boolean param, by default its always false. + short_option: b + long_option: bool + is_optional: true + default_value: false + is_include: true + - name: secure-param + type: string + description: Onap secure param such as password + short_option: x + long_option: secure + is_secured: true + is_optional: true + default_Value: pass123# + - name: string-param + type: string + description: Onap string param + long_option: string-param + short_option: c + is_optional: true + default_Value: test + - name: yaml-param + type: json + description: Onap yaml file location param + long_option: yaml-param + short_option: y + is_optional: true + - name: json-param + type: json + description: Onap json file location param + long_option: json-param + short_option: j + is_optional: true + is_secured: false + - name: long-param + type: digit + description: Onap long param + short_option: l + long_option: long-opt + is_optional: true + default_value: 10 + - name: url-param + type: url + description: Onap url param + short_option: r + long_option: url + is_optional: true + default_value: http://localhost:8082/file.txt + - name: env-param + type: string + description: Onap env param. + short_option: z + long_option: env + is_optional: true + default_value: ${ENV_VAR} + - name: positional-args + type: string + description: Onap positional args, if no short option and no long option given for it + is_optional: true + default_value: http://localhost:8082/file.txt +results: + direction: portrait + attributes: + - name: output-1 + description: Onap output attribute marked in short + scope: short + type: string + is_secured: true + default_value: req-$s{uuid} + - name: output-2 + description: Onap output attribute marked in long + scope: short + is_secured: false + type: string + default_value: Hello ${string-param} ! \ No newline at end of file diff --git a/framework/src/test/resources/sample-test-schema.yaml b/framework/src/test/resources/sample-test-schema.yaml deleted file mode 100644 index e3b415b1..00000000 --- a/framework/src/test/resources/sample-test-schema.yaml +++ /dev/null @@ -1,86 +0,0 @@ -open_cli_schema_version: 1.0 -name: sample-test -description: Onap sample command to test the command features -info: - product: open-cli - service: test - type: cmd - author: Kanagaraj Manickam mkr1481@gmail.com -parameters: - - name: bool-param - type: bool - description: Onap boolean param, by default its always false. - short_option: b - long_option: bool - is_optional: true - default_value: false - is_include: true - - name: secure-param - type: string - description: Onap secure param such as password - short_option: x - long_option: secure - is_secured: true - is_optional: true - default_Value: pass123# - - name: string-param - type: string - description: Onap string param - long_option: string-param - short_option: c - is_optional: true - default_Value: test - - name: yaml-param - type: json - description: Onap yaml file location param - long_option: yaml-param - short_option: y - is_optional: true - - name: json-param - type: json - description: Onap json file location param - long_option: json-param - short_option: j - is_optional: true - is_secured: false - - name: long-param - type: digit - description: Onap long param - short_option: l - long_option: long-opt - is_optional: true - default_value: 10 - - name: url-param - type: url - description: Onap url param - short_option: r - long_option: url - is_optional: true - default_value: http://localhost:8082/file.txt - - name: env-param - type: string - description: Onap env param. - short_option: z - long_option: env - is_optional: true - default_value: ${ENV_VAR} - - name: positional-args - type: string - description: Onap positional args, if no short option and no long option given for it - is_optional: true - default_value: http://localhost:8082/file.txt -results: - direction: portrait - attributes: - - name: output-1 - description: Onap output attribute marked in short - scope: short - type: string - is_secured: true - default_value: req-$s{uuid} - - name: output-2 - description: Onap output attribute marked in long - scope: short - is_secured: false - type: string - default_value: Hello ${string-param} ! \ No newline at end of file -- cgit 1.2.3-korg