From e45e668a4f6bdeca4ddc4071c4f37822792d65ed Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Mon, 31 Jul 2017 00:45:06 +0530 Subject: Fix invalid help parameters Introduce new section "default_parameter" to add "include" and "exclude" parameter from defalut parameter list. Following is a example for the parameter section to use it: xyz.yaml: ... default_parameters: include: - parameter1 - parameter2 ... exclude: - parameter3 - parameter4 ... parameters: - parameter5 .... .... Issue-Id: CLI-20 Change-Id: I99fd91a130739f2007fdd85a23c76d4e1b30c542 Signed-off-by: subhash kumar singh --- .../src/main/java/org/onap/cli/fw/OnapCommand.java | 24 ++- .../java/org/onap/cli/fw/ad/OnapAuthClient.java | 2 +- .../main/java/org/onap/cli/fw/conf/Constants.java | 10 +- .../org/onap/cli/fw/conf/OnapCommandConfg.java | 12 ++ .../error/OnapCommandInvalidDefaultParameter.java | 35 ++++ .../cli/fw/error/OnapCommandInvalidSchema.java | 4 + .../org/onap/cli/fw/utils/OnapCommandUtils.java | 183 ++++++++++++++------- framework/src/main/resources/onap.properties | 6 +- framework/src/main/resources/schema-refresh.yaml | 6 + framework/src/main/resources/schema-validate.yaml | 6 + 10 files changed, 214 insertions(+), 74 deletions(-) create mode 100644 framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java (limited to 'framework/src/main') 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 d67538cc..6cee09de 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -20,6 +20,7 @@ import org.onap.cli.fw.ad.OnapAuthClient; import org.onap.cli.fw.ad.OnapCredentials; import org.onap.cli.fw.ad.OnapService; 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.OnapCommandHelpFailed; import org.onap.cli.fw.error.OnapCommandInvalidParameterType; @@ -90,6 +91,11 @@ public abstract class OnapCommand { this.cmdName = name; } + public boolean isCommandInternal() { + return onapService.getName() != null + && onapService.getName().equalsIgnoreCase(OnapCommandConfg.getInternalCmd()); + } + /* * Onap service, this command uses to execute it. , defined by derived command */ @@ -171,7 +177,6 @@ public abstract class OnapCommand { * Any additional profile based such as http/swagger schema could be initialized. */ protected void initializeProfileSchema() throws OnapCommandException { - } /* @@ -235,10 +240,14 @@ public abstract class OnapCommand { try { // login OnapCredentials creds = OnapCommandUtils.fromParameters(this.getParameters()); - this.authClient = new OnapAuthClient(creds, this.getResult().isDebug()); + boolean isAuthRequired = !this.onapService.isNoAuth() + && "true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue()); + + if (!isCommandInternal()) { + this.authClient = new OnapAuthClient(creds, this.getResult().isDebug()); + } - if (!this.onapService.isNoAuth() - && !"true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue())) { + if (isAuthRequired) { this.authClient.login(); } @@ -246,16 +255,15 @@ public abstract class OnapCommand { this.run(); // logout - if (!this.onapService.isNoAuth() - && !"true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue())) { + if (isAuthRequired) { this.authClient.logout(); } - if (this.cmdResult.isDebug()) { + if (this.cmdResult.isDebug() && authClient != null) { this.cmdResult.setDebugInfo(this.authClient.getDebugInfo()); } } catch (OnapCommandException e) { - if (this.cmdResult.isDebug()) { + if (this.cmdResult.isDebug() && authClient != null) { this.cmdResult.setDebugInfo(this.authClient.getDebugInfo()); } throw e; diff --git a/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java b/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java index 0150ee98..9c7e4a66 100644 --- a/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java +++ b/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java @@ -135,7 +135,7 @@ public class OnapAuthClient { * http request failed */ public String getServiceBasePath(OnapService srv) throws OnapCommandException { - if (srv.getName().equals(Constants.MSB)) { + if (srv.getName().equals(OnapCommandConfg.getApiGateway())) { return this.getMsbUrl(); } 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 928cbc79..8886de55 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 @@ -26,13 +26,14 @@ public class Constants { public static final String APPLICATION_JSON = "application/json"; public static final String X_AUTH_TOKEN = "X-Auth-Token"; - public static final String AUTH_SERVICE = "auth"; + public static final String AUTH_SERVICE = "cli.auth_service"; public static final String AUTH_SERVICE_VERSION = "v1"; public static final String TOKEN = "{\"userName\": \"%s\",\"password\": \"%s\"}"; public static final String MSB_URI = "/api/microservices/v1"; public static final String MSB_SERVICE_URI = MSB_URI + "/services/%s/version/%s"; - public static final String MSB = "msb"; + public static final String API_GATEWAY = "cli.api_gateway"; + public static final String SERVICE_NAME = "cli.service_name"; //http public static final String URI = "uri"; @@ -63,6 +64,10 @@ public class Constants { public static final String DESCRIPTION = "description"; public static final String SERVICE = "service"; public static final String PARAMETERS = "parameters"; + public static final String DEFAULT_PARAMETERS = "default_parameters"; + public static final String DEFAULT_PARAMETERS_INCLUDE = "include"; + public static final String DEFAULT_PARAMETERS_EXCLUDE = "exclude"; + public static final String RESULTS = "results"; public static final String ONAP_CMD_SCHEMA_VERSION = "onap_cmd_schema_version"; @@ -143,6 +148,7 @@ public class Constants { public static final String DEFAULT_SCHEMA_FILE_NAME = "default_input_parameters.yaml"; // 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"; public static final String SCHEMA_FILE_WRONG_EXTN = "Schema file should be '.yaml' extension"; public static final String SCHEMA_FILE_NOT_EXIST = "Schema file doesn't exist"; 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 d5025e1b..bcfd6dfb 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 @@ -76,4 +76,16 @@ public final class OnapCommandConfg { return prps.getProperty(Constants.HTTP_X_AUTH_TOKEN, "X-Auth-Token"); } + public static String getInternalCmd() { + return prps.getProperty(Constants.SERVICE_NAME); + } + + public static String getApiGateway() { + return prps.getProperty(Constants.API_GATEWAY); + } + + public static String getAuthService() { + return prps.getProperty(Constants.AUTH_SERVICE); + } + } diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java new file mode 100644 index 00000000..4ebf04d3 --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.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; + +import java.util.List; + +/** + * Invalid default parameter exception. + */ +public class OnapCommandInvalidDefaultParameter extends OnapCommandException { + private static final long serialVersionUID = -1833571383961748514L; + + /** + * Invalid default argument exception. + * + * @param invalidParamsList message + */ + public OnapCommandInvalidDefaultParameter(List invalidParamsList) { + super("0x0024", "Invalid default parameter: " + invalidParamsList.toString()); + } +} diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSchema.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSchema.java index a32a8f2a..4c05d741 100644 --- a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSchema.java +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSchema.java @@ -24,6 +24,10 @@ public class OnapCommandInvalidSchema extends OnapCommandException { private static final long serialVersionUID = -3387652326582792833L; + public OnapCommandInvalidSchema(String error) { + this("", error); + } + public OnapCommandInvalidSchema(String schema, String error) { super("0x0007", "Command schema " + schema + " is invalid, " + error); } 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 79b9596c..1ad588de 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 @@ -19,7 +19,6 @@ package org.onap.cli.fw.utils; import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.JsonPath; import net.minidev.json.JSONArray; - import org.onap.cli.fw.OnapCommand; import org.onap.cli.fw.ad.OnapCredentials; import org.onap.cli.fw.ad.OnapService; @@ -31,6 +30,7 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandHelpFailed; import org.onap.cli.fw.error.OnapCommandHttpHeaderNotFound; import org.onap.cli.fw.error.OnapCommandHttpInvalidResponseBody; +import org.onap.cli.fw.error.OnapCommandInvalidDefaultParameter; import org.onap.cli.fw.error.OnapCommandInvalidParameterType; import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; import org.onap.cli.fw.error.OnapCommandInvalidPrintDirection; @@ -65,11 +65,14 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.ServiceLoader; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Provides helper method to parse Yaml files and produce required objects. @@ -87,17 +90,12 @@ public class OnapCommandUtils { /** * Validates schema version. * - * @param schemaName - * schema name - * @param version - * schema version + * @param schemaName schema name + * @param version schema version * @return map - * @throws OnapCommandInvalidSchemaVersion - * invalid schema version exception - * @throws OnapCommandInvalidSchema - * invalid schema - * @throws OnapCommandSchemaNotFound - * schema not found + * @throws OnapCommandInvalidSchemaVersion invalid schema version exception + * @throws OnapCommandInvalidSchema invalid schema + * @throws OnapCommandSchemaNotFound schema not found */ public static Map validateSchemaVersion(String schemaName, String version) throws OnapCommandException { InputStream inputStream = OnapCommandUtils.class.getClassLoader().getResourceAsStream(schemaName); @@ -138,59 +136,72 @@ public class OnapCommandUtils { /** * Retrieve OnapCommand from schema. * - * @param cmd - * OnapCommand - * @param schemaName - * schema name - * @param includeDefault - * include if default - * @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 + * @param cmd OnapCommand + * @param schemaName schema name + * @param includeDefault include if default + * @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 */ public static void loadSchema(OnapCommand cmd, String schemaName, boolean includeDefault) throws OnapCommandException { - List shortOptions = new ArrayList<>(); - List longOptions = new ArrayList<>(); - List names = new ArrayList<>(); + try { + Map defaultParameterMap = includeDefault ? + validateSchemaVersion(Constants.DEFAULT_PARAMETER_FILE_NAME, cmd.getSchemaVersion()) : new HashMap<>(); + Map>> commandYamlMap = (Map>>)validateSchemaVersion(schemaName, cmd.getSchemaVersion()); - if (includeDefault) { - loadSchema(cmd, Constants.DEFAULT_PARAMETER_FILE_NAME, shortOptions, longOptions, names); - } + List defParams = new ArrayList<>(); - loadSchema(cmd, schemaName, shortOptions, longOptions, names); + if (includeDefault) { + if (commandYamlMap.get(Constants.PARAMETERS) == null) { + commandYamlMap.put(Constants.PARAMETERS, (List>) defaultParameterMap.get(Constants.PARAMETERS)); + } else { + commandYamlMap.get(Constants.PARAMETERS).addAll((List>) defaultParameterMap.get(Constants.PARAMETERS)); + } + defParams = ((List>) defaultParameterMap.get(Constants.PARAMETERS)).stream() + .map(p -> p.get(Constants.NAME)).collect(Collectors.toList()); + } + parseSchema(cmd, commandYamlMap, defParams); + } catch (OnapCommandException e) { + throw e; + } catch (Exception e) { + throw new OnapCommandInvalidSchema(schemaName, e); + } } - private static void loadSchema(OnapCommand cmd, String schemaName, List shortOptions, - List longOptions, List names) throws OnapCommandException { - try { - Map values = validateSchemaVersion(schemaName, cmd.getSchemaVersion()); + private static void parseSchema(OnapCommand cmd, + final Map values, + final List defaultParamNames) throws OnapCommandException { + + List shortOptions = new ArrayList<>(); + List longOptions = new ArrayList<>(); + List names = new ArrayList<>(); + Set filteredDefaultParams = new HashSet<>(); + + List sections = Arrays.asList(Constants.NAME, Constants.DESCRIPTION, Constants.SERVICE, + Constants.DEFAULT_PARAMETERS, Constants.PARAMETERS, Constants.RESULTS); - for (Map.Entry entry : values.entrySet()) { - String key = entry.getKey(); + for (String key : sections) { - if (Constants.NAME.equals(key)) { - Object val = values.get(key); + if (Constants.NAME.equals(key)) { + Object val = values.get(key); + if (val != null) { cmd.setName(val.toString()); - } else if (Constants.DESCRIPTION.equals(key)) { - Object val = values.get(key); + } + } else if (Constants.DESCRIPTION.equals(key)) { + Object val = values.get(key); + if (val != null) { cmd.setDescription(val.toString()); - } else if (Constants.SERVICE.equals(key)) { - Map map = (Map) values.get(key); + } + } else if (Constants.SERVICE.equals(key)) { + Map map = (Map) values.get(key); + if (map != null) { OnapService srv = new OnapService(); for (Map.Entry entry1 : map.entrySet()) { @@ -207,10 +218,55 @@ public class OnapCommandUtils { } cmd.setService(srv); - } else if (Constants.PARAMETERS.equals(key)) { - List> list = (ArrayList) values.get(key); + } + } else if (Constants.DEFAULT_PARAMETERS.equals(key)) { + + Map> defParameters = (Map) values.get(Constants.DEFAULT_PARAMETERS); + + if (values.containsKey(Constants.DEFAULT_PARAMETERS) && defParameters == null) { + // if default parameter section is available then it must have either include + // or exclude sub-section. + throw new OnapCommandInvalidSchema(Constants.SCHEMA_INVALID_DEFAULT_PARAMS_SECTION); + } + + if (defParameters != null) { + // validate default parameters + List includeParams = defParameters.containsKey(Constants.DEFAULT_PARAMETERS_INCLUDE) ? + defParameters.get(Constants.DEFAULT_PARAMETERS_INCLUDE) : new ArrayList<>(); - for (Map map : list) { + List invInclude = includeParams.stream() + .filter(p -> !defaultParamNames.contains(p)) + .collect(Collectors.toList()); + + List excludeParams = defParameters.containsKey(Constants.DEFAULT_PARAMETERS_EXCLUDE) ? + defParameters.get(Constants.DEFAULT_PARAMETERS_EXCLUDE) : new ArrayList<>(); + + List invExclude = excludeParams.stream() + .filter(p -> !defaultParamNames.contains(p)) + .collect(Collectors.toList()); + + if (!invExclude.isEmpty() || !invInclude.isEmpty()) { + throw new OnapCommandInvalidDefaultParameter(Stream.concat(invInclude.stream(), invExclude.stream()) + .collect(Collectors.toList())); + } + + if (!includeParams.isEmpty()) { + filteredDefaultParams.addAll(includeParams); + } else if (!excludeParams.isEmpty()) { + defaultParamNames.stream().filter(p -> !excludeParams.contains(p)) + .forEach(filteredDefaultParams::add); + } + } else { + filteredDefaultParams.addAll(defaultParamNames); + + } + + } else if (Constants.PARAMETERS.equals(key)) { + + List> parameters = (List) values.get(key); + + if (parameters != null) { + for (Map map : parameters) { OnapCommandParameter param = new OnapCommandParameter(); for (Map.Entry entry1 : map.entrySet()) { @@ -255,11 +311,18 @@ public class OnapCommandUtils { } } } - cmd.getParameters().add(param); + // Add the element to command : + // 1. if parameter is available in filtered parameter list. + // 2. otherwise, parameter p is available in command yaml file. + if (filteredDefaultParams.contains(param.getName()) || !defaultParamNames.contains(param.getName())) { + cmd.getParameters().add(param); + } } - } else if (Constants.RESULTS.equals(key)) { - Map valueMap = (Map) values.get(key); + } + } else if (Constants.RESULTS.equals(key)) { + Map valueMap = (Map) values.get(key); + if (valueMap != null) { OnapCommandResult result = new OnapCommandResult(); for (Map.Entry entry1 : valueMap.entrySet()) { String key3 = entry1.getKey(); @@ -298,10 +361,6 @@ public class OnapCommandUtils { cmd.setResult(result); } } - } catch (OnapCommandException e) { - throw e; - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); } } diff --git a/framework/src/main/resources/onap.properties b/framework/src/main/resources/onap.properties index 61049cd4..bf1cdae5 100644 --- a/framework/src/main/resources/onap.properties +++ b/framework/src/main/resources/onap.properties @@ -1,4 +1,8 @@ cli.ignore_auth=false cli.version=1.0 http.api_key=X-Auth-Token -http.api_key_use_cookies=true \ No newline at end of file +http.api_key_use_cookies=true + +cli.service_name=onap-cli +cli.api_gateway=msb +cli.auth_service=auth \ No newline at end of file diff --git a/framework/src/main/resources/schema-refresh.yaml b/framework/src/main/resources/schema-refresh.yaml index f64b0545..ffa32424 100644 --- a/framework/src/main/resources/schema-refresh.yaml +++ b/framework/src/main/resources/schema-refresh.yaml @@ -5,6 +5,12 @@ service: no-auth: true name: onap-cli version: 1.0.0 +default_parameters: + exclude: + - onap-username + - onap-password + - msb-url + - no-auth results: direction: landscape attributes: diff --git a/framework/src/main/resources/schema-validate.yaml b/framework/src/main/resources/schema-validate.yaml index 1571fb3d..06bb6563 100644 --- a/framework/src/main/resources/schema-validate.yaml +++ b/framework/src/main/resources/schema-validate.yaml @@ -5,6 +5,12 @@ service: no-auth: true name: onap-cli version: 1.0.0 +default_parameters: + exclude: + - onap-username + - onap-password + - msb-url + - no-auth parameters: - name: schema-location type: url -- cgit 1.2.3-korg