From 89cf755a6e80c5ebd4bd989faad85aba60fc7b69 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam k00365106 Date: Fri, 13 Oct 2017 15:36:44 +0530 Subject: Removed the dead codes Swagger client sdk support is removed. Issue-Id: CLI-66 Change-Id: Ic7b23cfd72f2ae29de7ab44d2cffb35af2db701b Signed-off-by: Kanagaraj Manickam k00365106 --- .../src/main/java/org/onap/cli/fw/OnapCommand.java | 3 +- .../java/org/onap/cli/fw/cmd/OnapHttpCommand.java | 3 +- .../onap/cli/fw/cmd/OnapSchemaValidateCommand.java | 6 +- .../org/onap/cli/fw/cmd/OnapSwaggerCommand.java | 97 --- .../org/onap/cli/fw/run/OnapCommandExecutor.java | 71 --- .../onap/cli/fw/utils/OnapCommandSchemaLoader.java | 686 ++++++++++++++++++++ .../org/onap/cli/fw/utils/OnapCommandUtils.java | 710 +-------------------- 7 files changed, 701 insertions(+), 875 deletions(-) delete mode 100644 framework/src/main/java/org/onap/cli/fw/cmd/OnapSwaggerCommand.java delete mode 100644 framework/src/main/java/org/onap/cli/fw/run/OnapCommandExecutor.java create mode 100644 framework/src/main/java/org/onap/cli/fw/utils/OnapCommandSchemaLoader.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 87c1ba74..3dfb5c37 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -39,6 +39,7 @@ 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.ResultType; +import org.onap.cli.fw.utils.OnapCommandSchemaLoader; import org.onap.cli.fw.utils.OnapCommandUtils; /** @@ -156,7 +157,7 @@ public abstract class OnapCommand { */ public void initializeSchema(String schema) throws OnapCommandException { this.setSchemaName(schema); - OnapCommandUtils.loadSchema(this, schema, true, false); + OnapCommandSchemaLoader.loadSchema(this, schema, true, false); this.initializeProfileSchema(); this.isInitialzied = true; } 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 7ff38b64..01ce6247 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 @@ -34,6 +34,7 @@ import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate; import org.onap.cli.fw.http.HttpInput; import org.onap.cli.fw.http.HttpResult; import org.onap.cli.fw.output.OnapCommandResultAttribute; +import org.onap.cli.fw.utils.OnapCommandSchemaLoader; import org.onap.cli.fw.utils.OnapCommandUtils; import org.onap.cli.http.mock.MockJsonGenerator; import org.onap.cli.http.mock.MockRequest; @@ -98,7 +99,7 @@ public class OnapHttpCommand extends OnapCommand { @Override protected void initializeProfileSchema() throws OnapCommandException { - OnapCommandUtils.loadHttpSchema(this, this.getSchemaName(), true, false); + OnapCommandSchemaLoader.loadHttpSchema(this, this.getSchemaName(), true, false); } @Override 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 b3b9f357..c3088c53 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 @@ -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.input.OnapCommandParameter; -import org.onap.cli.fw.utils.OnapCommandUtils; +import org.onap.cli.fw.utils.OnapCommandSchemaLoader; import java.util.ArrayList; import java.util.List; @@ -43,14 +43,14 @@ public class OnapSchemaValidateCommand extends OnapCommand { location = location.substring(1); } - List error = OnapCommandUtils.loadSchema(new OnapCommand() { + List error = OnapCommandSchemaLoader.loadSchema(new OnapCommand() { @Override protected void run() throws OnapCommandException { } }, location, true, true); - error.addAll(OnapCommandUtils.loadHttpSchema(new OnapHttpCommand(), + error.addAll(OnapCommandSchemaLoader.loadHttpSchema(new OnapHttpCommand(), location, true, true)); List slNumber = new ArrayList<>(); diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapSwaggerCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapSwaggerCommand.java deleted file mode 100644 index 57fe8412..00000000 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapSwaggerCommand.java +++ /dev/null @@ -1,97 +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.cmd; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.List; - -import org.onap.cli.fw.OnapCommand; -import org.onap.cli.fw.error.OnapCommandClientInitialzationFailed; -import org.onap.cli.fw.error.OnapCommandResultInitialzationFailed; -import org.onap.cli.fw.output.OnapCommandResultAttribute; -import org.onap.cli.fw.run.OnapCommandExecutor; -import org.onap.cli.fw.utils.OnapCommandUtils; - -public abstract class OnapSwaggerCommand extends OnapCommand { - - - private OnapCommandExecutor cmdExecutor; - - public OnapCommandExecutor getExecutor() { - return cmdExecutor; - } - - public void setExecutor(OnapCommandExecutor executor) { - this.cmdExecutor = executor; - } - - /** - * Initialize the given ApiClient object with AUTH token and base path. - * - * @param client - * api client - * @throws OnapCommandClientInitialzationFailed - * client initialization failed - */ - protected T initializeApiClient(T client) throws OnapCommandClientInitialzationFailed { - try { - Method basePath = client.getClass().getMethod("setBasePath", String.class); - //mrkanag set the basepath - basePath.invoke(client, "/"); - -// if (this.getAuthToken() != null) { -// Method apiKey = client.getClass().getMethod("setApiKey", String.class); -// apiKey.invoke(client, this.getAuthToken()); -// } - return client; - } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) { - throw new OnapCommandClientInitialzationFailed(this.getName(), e); - } - } - - protected void initializeResult(T obj) throws OnapCommandResultInitialzationFailed { - this.getResult().setOutput(obj); - if (obj instanceof List) { - this.initializeListResult((List) obj); - } else { - this.initializeRow(obj); - } - } - - private void initializeRow(T obj) throws OnapCommandResultInitialzationFailed { - for (OnapCommandResultAttribute row : this.getResult().getRecords()) { - String methodName = OnapCommandUtils.formMethodNameFromAttributeName(row.getName(), "get"); - Method get; - try { - get = obj.getClass().getMethod(methodName); - row.getValues().add(get.invoke(obj).toString()); - } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) { - throw new OnapCommandResultInitialzationFailed(this.getName(), e); - } - } - } - - protected void initializeListResult(List rows) throws OnapCommandResultInitialzationFailed { - this.getResult().setOutput(rows); - for (T row : rows) { - this.initializeRow(row); - } - } -} diff --git a/framework/src/main/java/org/onap/cli/fw/run/OnapCommandExecutor.java b/framework/src/main/java/org/onap/cli/fw/run/OnapCommandExecutor.java deleted file mode 100644 index a3ff7bc8..00000000 --- a/framework/src/main/java/org/onap/cli/fw/run/OnapCommandExecutor.java +++ /dev/null @@ -1,71 +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.run; - -/** - * Oclip Command executor. - * - */ -public class OnapCommandExecutor { - - private String apiName; - private String clientName; - private String entityName; - private String methodName; - private String exceptionName; - - public String getApi() { - return this.apiName; - } - - public void setApi(String api) { - this.apiName = api; - } - - public String getClient() { - return this.clientName; - } - - public void setClient(String client) { - this.clientName = client; - } - - public String getEntity() { - return entityName; - } - - public void setEntity(String entity) { - this.entityName = entity; - } - - public String getMethod() { - return methodName; - } - - public void setMethod(String method) { - this.methodName = method; - } - - public String getException() { - return exceptionName; - } - - public void setException(String exception) { - this.exceptionName = exception; - } - -} diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandSchemaLoader.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandSchemaLoader.java new file mode 100644 index 00000000..439ec227 --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandSchemaLoader.java @@ -0,0 +1,686 @@ +/* + * 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 static org.onap.cli.fw.conf.Constants.ATTRIBUTES; +import static org.onap.cli.fw.conf.Constants.AUTH; +import static org.onap.cli.fw.conf.Constants.AUTH_VALUES; +import static org.onap.cli.fw.conf.Constants.BODY; +import static org.onap.cli.fw.conf.Constants.COMMAND_TYPE_VALUES; +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; +import static org.onap.cli.fw.conf.Constants.DEFAULT_PARAMETER_HTTP_FILE_NAME; +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.HEADERS; +import static org.onap.cli.fw.conf.Constants.HTTP; +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_PARAMS_LIST; +import static org.onap.cli.fw.conf.Constants.INFO_PARAMS_MANDATORY_LIST; +import static org.onap.cli.fw.conf.Constants.INFO_PRODUCT; +import static org.onap.cli.fw.conf.Constants.INFO_SERVICE; +import static org.onap.cli.fw.conf.Constants.INFO_TYPE; +import static org.onap.cli.fw.conf.Constants.INPUT_PARAMS_LIST; +import static org.onap.cli.fw.conf.Constants.INPUT_PARAMS_MANDATORY_LIST; +import static org.onap.cli.fw.conf.Constants.IS_INCLUDE; +import static org.onap.cli.fw.conf.Constants.IS_OPTIONAL; +import static org.onap.cli.fw.conf.Constants.IS_SECURED; +import static org.onap.cli.fw.conf.Constants.LONG_OPTION; +import static org.onap.cli.fw.conf.Constants.METHOD_TYPE; +import static org.onap.cli.fw.conf.Constants.MODE; +import static org.onap.cli.fw.conf.Constants.MODE_VALUES; +import static org.onap.cli.fw.conf.Constants.MULTIPART_ENTITY_NAME; +import static org.onap.cli.fw.conf.Constants.NAME; +import static org.onap.cli.fw.conf.Constants.OPEN_CLI_SCHEMA_VERSION; +import static org.onap.cli.fw.conf.Constants.PARAMETERS; +import static org.onap.cli.fw.conf.Constants.QUERIES; +import static org.onap.cli.fw.conf.Constants.REQUEST; +import static org.onap.cli.fw.conf.Constants.RESULTS; +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_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; +import static org.onap.cli.fw.conf.Constants.SERVICE_PARAMS_MANDATORY_LIST; +import static org.onap.cli.fw.conf.Constants.SHORT_OPTION; +import static org.onap.cli.fw.conf.Constants.SUCCESS_CODES; +import static org.onap.cli.fw.conf.Constants.TOP_LEVEL_MANDATORY_LIST; +import static org.onap.cli.fw.conf.Constants.TOP_LEVEL_PARAMS_LIST; +import static org.onap.cli.fw.conf.Constants.TYPE; +import static org.onap.cli.fw.conf.Constants.URI; +import static org.onap.cli.fw.conf.Constants.VERSION; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +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.OnapCommandConfg; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.error.OnapCommandInvalidSchema; +import org.onap.cli.fw.error.OnapCommandInvalidSchemaVersion; +import org.onap.cli.fw.error.OnapCommandParameterNameConflict; +import org.onap.cli.fw.error.OnapCommandParameterOptionConflict; +import org.onap.cli.fw.error.OnapCommandSchemaNotFound; +import org.onap.cli.fw.info.OnapCommandInfo; +import org.onap.cli.fw.input.OnapCommandParameter; +import org.onap.cli.fw.input.ParameterType; +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.springframework.core.io.Resource; +import org.yaml.snakeyaml.Yaml; + +public class OnapCommandSchemaLoader { + + /** + * Validates 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 + */ + public static Map validateSchemaVersion(String schemaName, String version) throws OnapCommandException { + InputStream inputStream = OnapCommandUtils.class.getClassLoader().getResourceAsStream(schemaName); + + try { + Resource resource = OnapCommandUtils.findResource(schemaName, SCHEMA_PATH_PATERN); + + if (resource != null) { + inputStream = resource.getInputStream(); + } + + } catch (IOException e) { + throw new OnapCommandSchemaNotFound(schemaName, e); + } + if (inputStream == null) { + inputStream = OnapCommandUtils.loadSchemaFromFile(schemaName); + } + + Map values = null; + try { + values = (Map) new Yaml().load(inputStream); + } catch (Exception e) { + throw new OnapCommandInvalidSchema(schemaName, e); + } + String schemaVersion = ""; + if (values.keySet().contains(OPEN_CLI_SCHEMA_VERSION)) { + Object obj = values.get(OPEN_CLI_SCHEMA_VERSION); + schemaVersion = obj.toString(); + } + + if (!version.equals(schemaVersion)) { + throw new OnapCommandInvalidSchemaVersion(schemaVersion); + } + + return values; + } + + /** + * Retrieve OnapCommand from schema. + * + * @param cmd OnapCommand + * @param schemaName schema name + * @param includeDefault include if default + * @param validateSchema flag to represent validation + * @throws OnapCommandException on error + */ + public static List loadSchema(OnapCommand cmd, String schemaName, boolean includeDefault, + boolean validateSchema) throws OnapCommandException { + try { + List errors = new ArrayList<>(); + if (includeDefault) { + Map defaultParameterMap = includeDefault ? + validateSchemaVersion(DEFAULT_PARAMETER_FILE_NAME, cmd.getSchemaVersion()) : new HashMap<>(); + errors.addAll(OnapCommandSchemaLoader.parseSchema(cmd, defaultParameterMap, validateSchema)); + } + + Map>> commandYamlMap = + (Map>>)validateSchemaVersion(schemaName, cmd.getSchemaVersion()); + + errors.addAll(OnapCommandSchemaLoader.parseSchema(cmd, commandYamlMap, validateSchema)); + + return errors; + } catch (OnapCommandException e) { + throw e; + } catch (Exception e) { + throw new OnapCommandInvalidSchema(schemaName, e); + } + } + + public static List loadHttpSchema(OnapHttpCommand cmd, String schemaName, boolean includeDefault, + boolean validateSchema) throws OnapCommandException { + try { + List errors = new ArrayList<>(); + if (includeDefault) { + Map defaultParameterMap = includeDefault ? + validateSchemaVersion(DEFAULT_PARAMETER_HTTP_FILE_NAME, cmd.getSchemaVersion()) : new HashMap<>(); + errors.addAll(OnapCommandSchemaLoader.parseSchema(cmd, defaultParameterMap, validateSchema)); + } + + Map>> commandYamlMap = + (Map>>)validateSchemaVersion(schemaName, cmd.getSchemaVersion()); + + errors.addAll(OnapCommandSchemaLoader.parseHttpSchema(cmd, commandYamlMap, validateSchema)); + + return errors; + + } catch (OnapCommandException e) { + throw e; + } catch (Exception e) { + throw new OnapCommandInvalidSchema(schemaName, e); + } + } + + static List parseSchema(OnapCommand cmd, + final Map values, + boolean validate) throws OnapCommandException { + + List exceptionList = new ArrayList<>(); + List shortOptions = new ArrayList<>(); + List longOptions = new ArrayList<>(); + + if (validate) { + OnapCommandUtils.validateTags(exceptionList, (Map) values, OnapCommandConfg.getSchemaAttrInfo(TOP_LEVEL_PARAMS_LIST), + OnapCommandConfg.getSchemaAttrInfo(TOP_LEVEL_MANDATORY_LIST), "root level"); + } + + + List sections = Arrays.asList(NAME, DESCRIPTION, INFO, PARAMETERS, RESULTS); + + for (String key : sections) { + + switch (key) { + case NAME: + Object val = values.get(key); + if (val != null) { + cmd.setName(val.toString()); + } + break; + + case DESCRIPTION: + Object description = values.get(key); + if (description != null) { + cmd.setDescription(description.toString()); + } + break; + + case INFO: + Map infoMap = (Map) values.get(key); + + if (infoMap != null) { + if (validate) { + OnapCommandUtils.validateTags(exceptionList, (Map) values.get(key), + OnapCommandConfg.getSchemaAttrInfo(INFO_PARAMS_LIST), + OnapCommandConfg.getSchemaAttrInfo(INFO_PARAMS_MANDATORY_LIST), INFO); + + HashMap validationMap = new HashMap<>(); + validationMap.put(INFO_TYPE, COMMAND_TYPE_VALUES); + + for (String secKey : validationMap.keySet()) { + if (infoMap.containsKey(secKey)) { + Object obj = infoMap.get(secKey); + if (obj == null) { + exceptionList.add("Attribute '" + secKey + "' under '" + INFO + "' is empty"); + } else { + String value = String.valueOf(obj); + if (!OnapCommandConfg.getSchemaAttrInfo(validationMap.get(secKey)).contains(value)) { + exceptionList.add("Attribute '" + secKey + "' contains invalid value. Valide values are " + + OnapCommandConfg.getSchemaAttrInfo(validationMap.get(key))); // + } + } + } + } + } + + OnapCommandInfo info = new OnapCommandInfo(); + + for (Map.Entry entry1 : infoMap.entrySet()) { + String key1 = entry1.getKey(); + + switch (key1) { + case INFO_PRODUCT: + info.setProduct(infoMap.get(key1)); + break; + + case INFO_SERVICE: + info.setService(infoMap.get(key1).toString()); + break; + + case INFO_TYPE: + Object obj = infoMap.get(key1); + info.setCommandType(CommandType.get(obj.toString())); + break; + + case INFO_AUTHOR: + Object mode = infoMap.get(key1); + info.setAuthor(mode.toString()); + break; + } + } + + cmd.setInfo(info); + } + break; + + case PARAMETERS: + + List> parameters = (List) values.get(key); + + if (parameters != null) { + Set names = new HashSet<>(); + + //To support overriding of the parameters, if command is already + //having the same named parameters, means same parameter is + //Overridden from included template into current template + Set existingParamNames = cmd.getParametersMap().keySet(); + + for (Map parameter : parameters) { + boolean isOverriding = false; + OnapCommandParameter param = new OnapCommandParameter(); + + //Override the parameters from its base such as default parameters list + if (existingParamNames.contains(parameter.getOrDefault(NAME, ""))) { + param = cmd.getParametersMap().get(parameter.getOrDefault(NAME, "")); + isOverriding = true; + } + + if (validate) { + OnapCommandUtils.validateTags(exceptionList, parameter, OnapCommandConfg.getSchemaAttrInfo(INPUT_PARAMS_LIST), + OnapCommandConfg.getSchemaAttrInfo(INPUT_PARAMS_MANDATORY_LIST), PARAMETERS); + } + + for (Map.Entry entry1 : parameter.entrySet()) { + String key2 = entry1.getKey(); + + switch (key2) { + case NAME: + if (names.contains(parameter.get(key2))) { + OnapCommandUtils.throwOrCollect(new OnapCommandParameterNameConflict(parameter.get(key2)), exceptionList, validate); + } else { + names.add(parameter.get(key2)); + } + + param.setName(parameter.get(key2)); + break; + + case DESCRIPTION: + param.setDescription(parameter.get(key2)); + break; + + case SHORT_OPTION: + if (shortOptions.contains(parameter.get(key2))) { + OnapCommandUtils.throwOrCollect(new OnapCommandParameterOptionConflict(parameter.get(key2)), exceptionList, validate); + } + shortOptions.add(parameter.get(key2)); + param.setShortOption(parameter.get(key2)); + break; + + case LONG_OPTION: + if (longOptions.contains(parameter.get(key2))) { + OnapCommandUtils.throwOrCollect(new OnapCommandParameterOptionConflict(parameter.get(key2)), exceptionList, validate); + } + longOptions.add(parameter.get(key2)); + param.setLongOption(parameter.get(key2)); + break; + + case DEFAULT_VALUE: + Object obj = parameter.get(key2); + param.setDefaultValue(obj.toString()); + break; + + case TYPE: + try { + param.setParameterType(ParameterType.get(parameter.get(key2))); + } catch (OnapCommandException ex) { + OnapCommandUtils.throwOrCollect(ex, exceptionList, validate); + } + break; + + case IS_OPTIONAL: + if (validate) { + if (!OnapCommandUtils.validateBoolean(String.valueOf(parameter.get(key2)))) { + exceptionList.add(OnapCommandUtils.invalidBooleanValueMessage(parameter.get(NAME), + IS_SECURED, parameter.get(key2))); + } + } + if ("true".equalsIgnoreCase(String.valueOf(parameter.get(key2)))) { + param.setOptional(true); + } else { + param.setOptional(false); + } + break; + + case IS_SECURED: + if (validate) { + if (!OnapCommandUtils.validateBoolean(String.valueOf(parameter.get(key2)))) { + exceptionList.add(OnapCommandUtils.invalidBooleanValueMessage(parameter.get(NAME), + IS_SECURED, parameter.get(key2))); + } + } + + if ("true".equalsIgnoreCase(String.valueOf(parameter.get(key2)))) { + param.setSecured(true); + } else { + param.setSecured(false); + } + break; + + case IS_INCLUDE: + if (validate) { + if (!OnapCommandUtils.validateBoolean(String.valueOf(parameter.get(key2)))) { + exceptionList.add(OnapCommandUtils.invalidBooleanValueMessage(parameter.get(NAME), + IS_INCLUDE, parameter.get(key2))); + } + } + + if ("true".equalsIgnoreCase(String.valueOf(parameter.get(key2)))) { + param.setInclude(true); + } else { + param.setInclude(false); + } + break; + } + } + + if ( !isOverriding) { + cmd.getParameters().add(param); + } else { + cmd.getParametersMap().replace(param.getName(), param); + } + } + } + break; + + case RESULTS: + Map valueMap = (Map) values.get(key); + if (valueMap != null) { + OnapCommandResult result = new OnapCommandResult(); + for (Map.Entry entry1 : valueMap.entrySet()) { + String key3 = entry1.getKey(); + + switch (key3) { + case DIRECTION: + try { + result.setPrintDirection(PrintDirection.get((String) valueMap.get(key3))); + } catch (OnapCommandException ex) { + OnapCommandUtils.throwOrCollect(ex, exceptionList, validate); + } + break; + + case ATTRIBUTES: + List> attrs = (ArrayList) valueMap.get(key3); + + for (Map map : attrs) { + OnapCommandResultAttribute attr = new OnapCommandResultAttribute(); + if (validate) { + OnapCommandUtils.validateTags(exceptionList, map, OnapCommandConfg.getSchemaAttrInfo(RESULT_PARAMS_LIST), + OnapCommandConfg.getSchemaAttrInfo(RESULT_PARAMS_MANDATORY_LIST), ATTRIBUTES); + } + + Set resultParamNames = new HashSet<>(); + + for (Map.Entry entry4 : map.entrySet()) { + String key4 = entry4.getKey(); + + switch (key4) { + case NAME: + if (resultParamNames.contains(map.get(key4))) { + exceptionList.add("Attribute name='" + map.get(key4) + "' under '" + + ATTRIBUTES + ":' is already used, Take different one."); + + } else { + attr.setName(map.get(key4)); + resultParamNames.add(map.get(key4)); + } + break; + + case DESCRIPTION: + attr.setDescription(map.get(key4)); + break; + + case SCOPE: + try { + attr.setScope(OnapCommandResultAttributeScope.get(map.get(key4))); + } catch (OnapCommandException ex) { + OnapCommandUtils.throwOrCollect(ex, exceptionList, validate); + } + break; + + case TYPE: + try { + attr.setType(ParameterType.get(map.get(key4))); + } catch (OnapCommandException ex) { + OnapCommandUtils.throwOrCollect(ex, exceptionList, validate); + } + break; + + case DEFAULT_VALUE: + Object obj = map.get(key4); + attr.setDefaultValue(obj.toString()); + break; + + case IS_SECURED: + if (validate) { + if (!OnapCommandUtils.validateBoolean(String.valueOf(map.get(key4)))) { + exceptionList.add(OnapCommandUtils.invalidBooleanValueMessage(ATTRIBUTES, + IS_SECURED, map.get(key4))); + } + } + if ("true".equals(String.valueOf(map.get(key4)))) { + attr.setSecured(true); + } else { + attr.setSecured(false); + } + break; + } + + } + result.getRecords().add(attr); + } + break; + } + } + cmd.setResult(result); + } + break; + } + } + return exceptionList; + } + + /** + * Load the schema. + * + * @param cmd + * OnapHttpCommand + * @param schemaName + * schema name + * @throws OnapCommandException + * on error + */ + static ArrayList parseHttpSchema(OnapHttpCommand cmd, + final Map values, + boolean validate) throws OnapCommandException { + ArrayList errorList = new ArrayList<>(); + try { + Map valMap = (Map) values.get(HTTP); + + if (valMap != null) { + if (validate) { + OnapCommandUtils.validateTags(errorList, valMap, OnapCommandConfg.getSchemaAttrInfo(HTTP_SECTIONS), + OnapCommandConfg.getSchemaAttrInfo(HTTP_MANDATORY_SECTIONS), PARAMETERS); + errorList.addAll(OnapCommandUtils.validateHttpSchemaSection(values)); + } + for (Map.Entry entry1 : valMap.entrySet()) { + String key1 = entry1.getKey(); + + switch (key1) { + case REQUEST: + Map map = (Map) valMap.get(key1); + + for (Map.Entry entry2 : map.entrySet()) { + try { + String key2 = entry2.getKey(); + + switch (key2) { + case URI: + Object obj = map.get(key2); + cmd.getInput().setUri(obj.toString()); + break; + case METHOD_TYPE: + Object method = map.get(key2); + cmd.getInput().setMethod(method.toString()); + break; + case BODY: + Object body = map.get(key2); + cmd.getInput().setBody(body.toString()); + break; + case HEADERS: + Map head = (Map) map.get(key2); + cmd.getInput().setReqHeaders(head); + break; + case QUERIES: + Map query = (Map) map.get(key2); + + cmd.getInput().setReqQueries(query); + break; + case MULTIPART_ENTITY_NAME: + Object multipartEntityName = map.get(key2); + cmd.getInput().setMultipartEntityName(multipartEntityName.toString()); + break; + } + }catch (Exception ex) { + OnapCommandUtils.throwOrCollect(new OnapCommandInvalidSchema(cmd.getSchemaName(), ex), errorList, validate); + } + } + break; + + case SERVICE: + Map serviceMap = (Map) valMap.get(key1); + + if (serviceMap != null) { + if (validate) { + OnapCommandUtils.validateTags(errorList, (Map) valMap.get(key1), + OnapCommandConfg.getSchemaAttrInfo(SERVICE_PARAMS_LIST), + OnapCommandConfg.getSchemaAttrInfo(SERVICE_PARAMS_MANDATORY_LIST), SERVICE); + + HashMap validationMap = new HashMap<>(); + validationMap.put(AUTH, AUTH_VALUES); + validationMap.put(MODE, MODE_VALUES); + + for (String secKey : validationMap.keySet()) { + if (serviceMap.containsKey(secKey)) { + Object obj = serviceMap.get(secKey); + if (obj == null) { + errorList.add("Attribute '" + secKey + "' under '" + SERVICE + "' is empty"); + } else { + String value = String.valueOf(obj); + if (!OnapCommandConfg.getSchemaAttrInfo(validationMap.get(secKey)).contains(value)) { + errorList.add("Attribute '" + secKey + "' contains invalid value. Valide values are " + + OnapCommandConfg.getSchemaAttrInfo(validationMap.get(key1))); // + } + } + } + } + } + + OnapService srv = new OnapService(); + + for (Map.Entry entry : serviceMap.entrySet()) { + String key = entry.getKey(); + + switch (key) { + case NAME: + srv.setName(serviceMap.get(key)); + break; + + case VERSION: + srv.setVersion(serviceMap.get(key).toString()); + break; + + case AUTH: + Object obj = serviceMap.get(key); + srv.setAuthType(obj.toString()); + + //On None type, username, password and no_auth are invalid + if (srv.isNoAuth()) { + cmd.getParametersMap().get(DEAFULT_PARAMETER_USERNAME).setInclude(false); + cmd.getParametersMap().get(DEAFULT_PARAMETER_PASSWORD).setInclude(false); + cmd.getParametersMap().get(DEFAULT_PARAMETER_NO_AUTH).setInclude(false); + } + break; + + case MODE: + Object mode = serviceMap.get(key); + srv.setMode(mode.toString()); + break; + } + } + cmd.setService(srv); + } + break; + + case SUCCESS_CODES: + if (validate) { + OnapCommandUtils.validateHttpSccessCodes(errorList, (List) valMap.get(key1)); + } + cmd.setSuccessStatusCodes((ArrayList) valMap.get(key1)); + break; + + case RESULT_MAP: + if (validate) { + OnapCommandUtils.validateHttpResultMap(errorList, values); + } + cmd.setResultMap((Map) valMap.get(key1)); + break; + + case SAMPLE_RESPONSE: + // (mrkanag) implement sample response handling + break; + } + } + } + }catch (OnapCommandException e) { + OnapCommandUtils.throwOrCollect(e, errorList, validate); + } + return errorList; + } + +} 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 0f64ed3a..9a6f6366 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 @@ -16,56 +16,22 @@ package org.onap.cli.fw.utils; -import static org.onap.cli.fw.conf.Constants.API; import static org.onap.cli.fw.conf.Constants.ATTRIBUTES; -import static org.onap.cli.fw.conf.Constants.AUTH; -import static org.onap.cli.fw.conf.Constants.AUTH_VALUES; import static org.onap.cli.fw.conf.Constants.BODY; 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_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; -import static org.onap.cli.fw.conf.Constants.DEFAULT_PARAMETER_HTTP_FILE_NAME; -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.HEADERS; import static org.onap.cli.fw.conf.Constants.HTTP; import static org.onap.cli.fw.conf.Constants.HTTP_BODY_FAILED_PARSING; import static org.onap.cli.fw.conf.Constants.HTTP_BODY_JSON_EMPTY; -import static org.onap.cli.fw.conf.Constants.HTTP_MANDATORY_SECTIONS; import static org.onap.cli.fw.conf.Constants.HTTP_METHODS; import static org.onap.cli.fw.conf.Constants.HTTP_REQUEST_MANDATORY_PARAMS; import static org.onap.cli.fw.conf.Constants.HTTP_REQUEST_PARAMS; -import static org.onap.cli.fw.conf.Constants.HTTP_SECTIONS; import static org.onap.cli.fw.conf.Constants.HTTP_SUCCESS_CODE_INVALID; -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_PARAMS_LIST; -import static org.onap.cli.fw.conf.Constants.INFO_PARAMS_MANDATORY_LIST; -import static org.onap.cli.fw.conf.Constants.INFO_PRODUCT; -import static org.onap.cli.fw.conf.Constants.INFO_SERVICE; -import static org.onap.cli.fw.conf.Constants.INFO_TYPE; -import static org.onap.cli.fw.conf.Constants.INPUT_PARAMS_LIST; -import static org.onap.cli.fw.conf.Constants.INPUT_PARAMS_MANDATORY_LIST; -import static org.onap.cli.fw.conf.Constants.IS_INCLUDE; -import static org.onap.cli.fw.conf.Constants.IS_OPTIONAL; -import static org.onap.cli.fw.conf.Constants.IS_SECURED; -import static org.onap.cli.fw.conf.Constants.LONG_OPTION; import static org.onap.cli.fw.conf.Constants.METHOD; -import static org.onap.cli.fw.conf.Constants.METHOD_TYPE; -import static org.onap.cli.fw.conf.Constants.MODE; -import static org.onap.cli.fw.conf.Constants.MODE_VALUES; -import static org.onap.cli.fw.conf.Constants.MULTIPART_ENTITY_NAME; import static org.onap.cli.fw.conf.Constants.NAME; import static org.onap.cli.fw.conf.Constants.OPEN_CLI_SCHEMA_VERSION; import static org.onap.cli.fw.conf.Constants.PARAMETERS; @@ -73,24 +39,11 @@ import static org.onap.cli.fw.conf.Constants.QUERIES; import static org.onap.cli.fw.conf.Constants.REQUEST; import static org.onap.cli.fw.conf.Constants.RESULTS; 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; -import static org.onap.cli.fw.conf.Constants.SERVICE_PARAMS_MANDATORY_LIST; -import static org.onap.cli.fw.conf.Constants.SHORT_OPTION; -import static org.onap.cli.fw.conf.Constants.SUCCESS_CODES; -import static org.onap.cli.fw.conf.Constants.TOP_LEVEL_MANDATORY_LIST; -import static org.onap.cli.fw.conf.Constants.TOP_LEVEL_PARAMS_LIST; -import static org.onap.cli.fw.conf.Constants.TYPE; import static org.onap.cli.fw.conf.Constants.URI; -import static org.onap.cli.fw.conf.Constants.VERSION; import java.io.File; import java.io.FileInputStream; @@ -114,10 +67,7 @@ import java.util.jar.Manifest; import java.util.stream.Collectors; 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.cmd.OnapSwaggerCommand; import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.conf.OnapCommandConfg; import org.onap.cli.fw.error.OnapCommandDiscoveryFailed; @@ -126,23 +76,15 @@ 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.OnapCommandHttpInvalidResultMap; -import org.onap.cli.fw.error.OnapCommandInvalidParameterType; import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; -import org.onap.cli.fw.error.OnapCommandInvalidPrintDirection; -import org.onap.cli.fw.error.OnapCommandInvalidResultAttributeScope; import org.onap.cli.fw.error.OnapCommandInvalidSchema; -import org.onap.cli.fw.error.OnapCommandInvalidSchemaVersion; import org.onap.cli.fw.error.OnapCommandLoadProfileFailed; -import org.onap.cli.fw.error.OnapCommandParameterNameConflict; import org.onap.cli.fw.error.OnapCommandParameterNotFound; -import org.onap.cli.fw.error.OnapCommandParameterOptionConflict; import org.onap.cli.fw.error.OnapCommandPersistProfileFailed; import org.onap.cli.fw.error.OnapCommandResultEmpty; import org.onap.cli.fw.error.OnapCommandResultMapProcessingFailed; -import org.onap.cli.fw.error.OnapCommandSchemaNotFound; import org.onap.cli.fw.http.HttpInput; import org.onap.cli.fw.http.HttpResult; -import org.onap.cli.fw.info.OnapCommandInfo; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.input.ParameterType; import org.onap.cli.fw.input.cache.Param; @@ -151,7 +93,6 @@ 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.run.OnapCommandExecutor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; @@ -179,53 +120,7 @@ public class OnapCommandUtils { } - /** - * Validates 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 - */ - public static Map validateSchemaVersion(String schemaName, String version) throws OnapCommandException { - InputStream inputStream = OnapCommandUtils.class.getClassLoader().getResourceAsStream(schemaName); - - try { - Resource resource = findResource(schemaName, SCHEMA_PATH_PATERN); - - if (resource != null) { - inputStream = resource.getInputStream(); - } - - } catch (IOException e) { - throw new OnapCommandSchemaNotFound(schemaName, e); - } - if (inputStream == null) { - inputStream = loadSchemaFromFile(schemaName); - } - - Map values = null; - try { - values = (Map) new Yaml().load(inputStream); - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); - } - String schemaVersion = ""; - if (values.keySet().contains(OPEN_CLI_SCHEMA_VERSION)) { - Object obj = values.get(OPEN_CLI_SCHEMA_VERSION); - schemaVersion = obj.toString(); - } - - if (!version.equals(schemaVersion)) { - throw new OnapCommandInvalidSchemaVersion(schemaVersion); - } - - return values; - } - - private static InputStream loadSchemaFromFile(String schemaLocation) throws OnapCommandInvalidSchema { + static InputStream loadSchemaFromFile(String schemaLocation) throws OnapCommandInvalidSchema { File schemaFile = new File(schemaLocation); try { FileInputStream inputFileStream = new FileInputStream(schemaFile); @@ -242,594 +137,7 @@ public class OnapCommandUtils { } } - /** - * Retrieve OnapCommand from schema. - * - * @param cmd OnapCommand - * @param schemaName schema name - * @param includeDefault include if default - * @param validateSchema flag to represent validation - * @throws OnapCommandException on error - */ - public static List loadSchema(OnapCommand cmd, String schemaName, boolean includeDefault, - boolean validateSchema) throws OnapCommandException { - try { - List errors = new ArrayList<>(); - if (includeDefault) { - Map defaultParameterMap = includeDefault ? - validateSchemaVersion(DEFAULT_PARAMETER_FILE_NAME, cmd.getSchemaVersion()) : new HashMap<>(); - errors.addAll(parseSchema(cmd, defaultParameterMap, validateSchema)); - } - - Map>> commandYamlMap = - (Map>>)validateSchemaVersion(schemaName, cmd.getSchemaVersion()); - - errors.addAll(parseSchema(cmd, commandYamlMap, validateSchema)); - - return errors; - } catch (OnapCommandException e) { - throw e; - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); - } - } - - - public static List loadHttpSchema(OnapHttpCommand cmd, String schemaName, boolean includeDefault, - boolean validateSchema) throws OnapCommandException { - try { - List errors = new ArrayList<>(); - if (includeDefault) { - Map defaultParameterMap = includeDefault ? - validateSchemaVersion(DEFAULT_PARAMETER_HTTP_FILE_NAME, cmd.getSchemaVersion()) : new HashMap<>(); - errors.addAll(parseSchema(cmd, defaultParameterMap, validateSchema)); - } - - Map>> commandYamlMap = - (Map>>)validateSchemaVersion(schemaName, cmd.getSchemaVersion()); - - errors.addAll(parseHttpSchema(cmd, commandYamlMap, validateSchema)); - - return errors; - - } catch (OnapCommandException e) { - throw e; - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); - } - } - - private static List parseSchema(OnapCommand cmd, - final Map values, - boolean validate) throws OnapCommandException { - - List exceptionList = new ArrayList<>(); - List shortOptions = new ArrayList<>(); - List longOptions = new ArrayList<>(); - - if (validate) { - validateTags(exceptionList, (Map) values, OnapCommandConfg.getSchemaAttrInfo(TOP_LEVEL_PARAMS_LIST), - OnapCommandConfg.getSchemaAttrInfo(TOP_LEVEL_MANDATORY_LIST), "root level"); - } - - - List sections = Arrays.asList(NAME, DESCRIPTION, INFO, PARAMETERS, RESULTS); - - for (String key : sections) { - - switch (key) { - case NAME: - Object val = values.get(key); - if (val != null) { - cmd.setName(val.toString()); - } - break; - - case DESCRIPTION: - Object description = values.get(key); - if (description != null) { - cmd.setDescription(description.toString()); - } - break; - - case INFO: - Map infoMap = (Map) values.get(key); - - if (infoMap != null) { - if (validate) { - validateTags(exceptionList, (Map) values.get(key), - OnapCommandConfg.getSchemaAttrInfo(INFO_PARAMS_LIST), - OnapCommandConfg.getSchemaAttrInfo(INFO_PARAMS_MANDATORY_LIST), INFO); - - HashMap validationMap = new HashMap<>(); - validationMap.put(INFO_TYPE, COMMAND_TYPE_VALUES); - - for (String secKey : validationMap.keySet()) { - if (infoMap.containsKey(secKey)) { - Object obj = infoMap.get(secKey); - if (obj == null) { - exceptionList.add("Attribute '" + secKey + "' under '" + INFO + "' is empty"); - } else { - String value = String.valueOf(obj); - if (!OnapCommandConfg.getSchemaAttrInfo(validationMap.get(secKey)).contains(value)) { - exceptionList.add("Attribute '" + secKey + "' contains invalid value. Valide values are " - + OnapCommandConfg.getSchemaAttrInfo(validationMap.get(key))); // - } - } - } - } - } - - OnapCommandInfo info = new OnapCommandInfo(); - - for (Map.Entry entry1 : infoMap.entrySet()) { - String key1 = entry1.getKey(); - - switch (key1) { - case INFO_PRODUCT: - info.setProduct(infoMap.get(key1)); - break; - - case INFO_SERVICE: - info.setService(infoMap.get(key1).toString()); - break; - - case INFO_TYPE: - Object obj = infoMap.get(key1); - info.setCommandType(CommandType.get(obj.toString())); - break; - - case INFO_AUTHOR: - Object mode = infoMap.get(key1); - info.setAuthor(mode.toString()); - break; - } - } - - cmd.setInfo(info); - } - break; - - case PARAMETERS: - - List> parameters = (List) values.get(key); - - if (parameters != null) { - Set names = new HashSet<>(); - - //To support overriding of the parameters, if command is already - //having the same named parameters, means same parameter is - //Overridden from included template into current template - Set existingParamNames = cmd.getParametersMap().keySet(); - - for (Map parameter : parameters) { - boolean isOverriding = false; - OnapCommandParameter param = new OnapCommandParameter(); - - //Override the parameters from its base such as default parameters list - if (existingParamNames.contains(parameter.getOrDefault(NAME, ""))) { - param = cmd.getParametersMap().get(parameter.getOrDefault(NAME, "")); - isOverriding = true; - } - - if (validate) { - validateTags(exceptionList, parameter, OnapCommandConfg.getSchemaAttrInfo(INPUT_PARAMS_LIST), - OnapCommandConfg.getSchemaAttrInfo(INPUT_PARAMS_MANDATORY_LIST), PARAMETERS); - } - - for (Map.Entry entry1 : parameter.entrySet()) { - String key2 = entry1.getKey(); - - switch (key2) { - case NAME: - if (names.contains(parameter.get(key2))) { - throwOrCollect(new OnapCommandParameterNameConflict(parameter.get(key2)), exceptionList, validate); - } else { - names.add(parameter.get(key2)); - } - - param.setName(parameter.get(key2)); - break; - - case DESCRIPTION: - param.setDescription(parameter.get(key2)); - break; - - case SHORT_OPTION: - if (shortOptions.contains(parameter.get(key2))) { - throwOrCollect(new OnapCommandParameterOptionConflict(parameter.get(key2)), exceptionList, validate); - } - shortOptions.add(parameter.get(key2)); - param.setShortOption(parameter.get(key2)); - break; - - case LONG_OPTION: - if (longOptions.contains(parameter.get(key2))) { - throwOrCollect(new OnapCommandParameterOptionConflict(parameter.get(key2)), exceptionList, validate); - } - longOptions.add(parameter.get(key2)); - param.setLongOption(parameter.get(key2)); - break; - - case DEFAULT_VALUE: - Object obj = parameter.get(key2); - param.setDefaultValue(obj.toString()); - break; - - case TYPE: - try { - param.setParameterType(ParameterType.get(parameter.get(key2))); - } catch (OnapCommandException ex) { - throwOrCollect(ex, exceptionList, validate); - } - break; - - case IS_OPTIONAL: - if (validate) { - if (!validateBoolean(String.valueOf(parameter.get(key2)))) { - exceptionList.add(invalidBooleanValueMessage(parameter.get(NAME), - IS_SECURED, parameter.get(key2))); - } - } - if ("true".equalsIgnoreCase(String.valueOf(parameter.get(key2)))) { - param.setOptional(true); - } else { - param.setOptional(false); - } - break; - - case IS_SECURED: - if (validate) { - if (!validateBoolean(String.valueOf(parameter.get(key2)))) { - exceptionList.add(invalidBooleanValueMessage(parameter.get(NAME), - IS_SECURED, parameter.get(key2))); - } - } - - if ("true".equalsIgnoreCase(String.valueOf(parameter.get(key2)))) { - param.setSecured(true); - } else { - param.setSecured(false); - } - break; - - case IS_INCLUDE: - if (validate) { - if (!validateBoolean(String.valueOf(parameter.get(key2)))) { - exceptionList.add(invalidBooleanValueMessage(parameter.get(NAME), - IS_INCLUDE, parameter.get(key2))); - } - } - - if ("true".equalsIgnoreCase(String.valueOf(parameter.get(key2)))) { - param.setInclude(true); - } else { - param.setInclude(false); - } - break; - } - } - - if ( !isOverriding) { - cmd.getParameters().add(param); - } else { - cmd.getParametersMap().replace(param.getName(), param); - } - } - } - break; - - case RESULTS: - Map valueMap = (Map) values.get(key); - if (valueMap != null) { - OnapCommandResult result = new OnapCommandResult(); - for (Map.Entry entry1 : valueMap.entrySet()) { - String key3 = entry1.getKey(); - - switch (key3) { - case DIRECTION: - try { - result.setPrintDirection(PrintDirection.get((String) valueMap.get(key3))); - } catch (OnapCommandException ex) { - throwOrCollect(ex, exceptionList, validate); - } - break; - - case ATTRIBUTES: - List> attrs = (ArrayList) valueMap.get(key3); - - for (Map map : attrs) { - OnapCommandResultAttribute attr = new OnapCommandResultAttribute(); - if (validate) { - validateTags(exceptionList, map, OnapCommandConfg.getSchemaAttrInfo(RESULT_PARAMS_LIST), - OnapCommandConfg.getSchemaAttrInfo(RESULT_PARAMS_MANDATORY_LIST), ATTRIBUTES); - } - - Set resultParamNames = new HashSet<>(); - - for (Map.Entry entry4 : map.entrySet()) { - String key4 = entry4.getKey(); - - switch (key4) { - case NAME: - if (resultParamNames.contains(map.get(key4))) { - exceptionList.add("Attribute name='" + map.get(key4) + "' under '" - + ATTRIBUTES + ":' is already used, Take different one."); - - } else { - attr.setName(map.get(key4)); - resultParamNames.add(map.get(key4)); - } - break; - - case DESCRIPTION: - attr.setDescription(map.get(key4)); - break; - - case SCOPE: - try { - attr.setScope(OnapCommandResultAttributeScope.get(map.get(key4))); - } catch (OnapCommandException ex) { - throwOrCollect(ex, exceptionList, validate); - } - break; - - case TYPE: - try { - attr.setType(ParameterType.get(map.get(key4))); - } catch (OnapCommandException ex) { - throwOrCollect(ex, exceptionList, validate); - } - break; - - case DEFAULT_VALUE: - Object obj = map.get(key4); - attr.setDefaultValue(obj.toString()); - break; - - case IS_SECURED: - if (validate) { - if (!validateBoolean(String.valueOf(map.get(key4)))) { - exceptionList.add(invalidBooleanValueMessage(ATTRIBUTES, - IS_SECURED, map.get(key4))); - } - } - if ("true".equals(String.valueOf(map.get(key4)))) { - attr.setSecured(true); - } else { - attr.setSecured(false); - } - break; - } - - } - result.getRecords().add(attr); - } - break; - } - } - cmd.setResult(result); - } - break; - } - } - return exceptionList; - } - - /** - * Load the schema. - * - * @param cmd - * OnapSwaggerBasedCommand - * @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 - */ - public static void loadSchema(OnapSwaggerCommand cmd, String schemaName) throws OnapCommandException { - try { - Map values = (Map) validateSchemaVersion(schemaName, cmd.getSchemaVersion()); - Map valueMap = (Map) values.get(EXECUTOR); - OnapCommandExecutor exec = new OnapCommandExecutor(); - - for (Map.Entry entry1 : valueMap.entrySet()) { - String key1 = entry1.getKey(); - - if (API.equals(key1)) { - exec.setApi(valueMap.get(key1)); - } else if (CLIENT.equals(key1)) { - exec.setClient(valueMap.get(key1)); - } else if (ENTITY.equals(key1)) { - exec.setEntity(valueMap.get(key1)); - } else if (EXCEPTION.equals(key1)) { - exec.setException(valueMap.get(key1)); - } else if (METHOD.equals(key1)) { - exec.setMethod(valueMap.get(key1)); - } - } - - cmd.setExecutor(exec); - } catch (OnapCommandException e) { - throw e; - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); - } - } - - /** - * Load the schema. - * - * @param cmd - * OnapHttpCommand - * @param schemaName - * schema name - * @throws OnapCommandException - * on error - */ - private static ArrayList parseHttpSchema(OnapHttpCommand cmd, - final Map values, - boolean validate) throws OnapCommandException { - ArrayList errorList = new ArrayList<>(); - try { - Map valMap = (Map) values.get(HTTP); - - if (valMap != null) { - if (validate) { - validateTags(errorList, valMap, OnapCommandConfg.getSchemaAttrInfo(HTTP_SECTIONS), - OnapCommandConfg.getSchemaAttrInfo(HTTP_MANDATORY_SECTIONS), PARAMETERS); - errorList.addAll(validateHttpSchemaSection(values)); - } - for (Map.Entry entry1 : valMap.entrySet()) { - String key1 = entry1.getKey(); - - switch (key1) { - case REQUEST: - Map map = (Map) valMap.get(key1); - - for (Map.Entry entry2 : map.entrySet()) { - try { - String key2 = entry2.getKey(); - - switch (key2) { - case URI: - Object obj = map.get(key2); - cmd.getInput().setUri(obj.toString()); - break; - case METHOD_TYPE: - Object method = map.get(key2); - cmd.getInput().setMethod(method.toString()); - break; - case BODY: - Object body = map.get(key2); - cmd.getInput().setBody(body.toString()); - break; - case HEADERS: - Map head = (Map) map.get(key2); - cmd.getInput().setReqHeaders(head); - break; - case QUERIES: - Map query = (Map) map.get(key2); - - cmd.getInput().setReqQueries(query); - break; - case MULTIPART_ENTITY_NAME: - Object multipartEntityName = map.get(key2); - cmd.getInput().setMultipartEntityName(multipartEntityName.toString()); - break; - } - }catch (Exception ex) { - throwOrCollect(new OnapCommandInvalidSchema(cmd.getSchemaName(), ex), errorList, validate); - } - } - break; - - case SERVICE: - Map serviceMap = (Map) valMap.get(key1); - - if (serviceMap != null) { - if (validate) { - validateTags(errorList, (Map) valMap.get(key1), - OnapCommandConfg.getSchemaAttrInfo(SERVICE_PARAMS_LIST), - OnapCommandConfg.getSchemaAttrInfo(SERVICE_PARAMS_MANDATORY_LIST), SERVICE); - - HashMap validationMap = new HashMap<>(); - validationMap.put(AUTH, AUTH_VALUES); - validationMap.put(MODE, MODE_VALUES); - - for (String secKey : validationMap.keySet()) { - if (serviceMap.containsKey(secKey)) { - Object obj = serviceMap.get(secKey); - if (obj == null) { - errorList.add("Attribute '" + secKey + "' under '" + SERVICE + "' is empty"); - } else { - String value = String.valueOf(obj); - if (!OnapCommandConfg.getSchemaAttrInfo(validationMap.get(secKey)).contains(value)) { - errorList.add("Attribute '" + secKey + "' contains invalid value. Valide values are " - + OnapCommandConfg.getSchemaAttrInfo(validationMap.get(key1))); // - } - } - } - } - } - - OnapService srv = new OnapService(); - - for (Map.Entry entry : serviceMap.entrySet()) { - String key = entry.getKey(); - - switch (key) { - case NAME: - srv.setName(serviceMap.get(key)); - break; - - case VERSION: - srv.setVersion(serviceMap.get(key).toString()); - break; - - case AUTH: - Object obj = serviceMap.get(key); - srv.setAuthType(obj.toString()); - - //On None type, username, password and no_auth are invalid - if (srv.isNoAuth()) { - cmd.getParametersMap().get(DEAFULT_PARAMETER_USERNAME).setInclude(false); - cmd.getParametersMap().get(DEAFULT_PARAMETER_PASSWORD).setInclude(false); - cmd.getParametersMap().get(DEFAULT_PARAMETER_NO_AUTH).setInclude(false); - } - break; - - case MODE: - Object mode = serviceMap.get(key); - srv.setMode(mode.toString()); - break; - } - } - cmd.setService(srv); - } - break; - - case SUCCESS_CODES: - if (validate) { - validateHttpSccessCodes(errorList, (List) valMap.get(key1)); - } - cmd.setSuccessStatusCodes((ArrayList) valMap.get(key1)); - break; - - case RESULT_MAP: - if (validate) { - validateHttpResultMap(errorList, values); - } - cmd.setResultMap((Map) valMap.get(key1)); - break; - - case SAMPLE_RESPONSE: - // (mrkanag) implement sample response handling - break; - } - } - } - }catch (OnapCommandException e) { - throwOrCollect(e, errorList, validate); - } - return errorList; - } - - - private static void throwOrCollect(OnapCommandException ex, List list, boolean shouldCollectException) + static void throwOrCollect(OnapCommandException ex, List list, boolean shouldCollectException) throws OnapCommandException { if (shouldCollectException) { list.add(ex.getMessage()); @@ -838,7 +146,7 @@ public class OnapCommandUtils { } } - private static void validateTags(List schemaErrors, Map yamlMap, List totalParams, + static void validateTags(List schemaErrors, Map yamlMap, List totalParams, List mandatoryParams, String section) { // mrkanag capture invalid entries as well for (String param : totalParams) { @@ -865,7 +173,7 @@ public class OnapCommandUtils { * string * @return boolean */ - protected static boolean validateBoolean(String toValidate) { + static boolean validateBoolean(String toValidate) { return OnapCommandConfg.getSchemaAttrInfo(BOOLEAN_VALUE).contains(toValidate.toLowerCase()); } @@ -873,7 +181,7 @@ public class OnapCommandUtils { return "The section '" + section + ":' cann't be null or empty"; } - private static String invalidBooleanValueMessage(String section, String attribute, String value) { + static String invalidBooleanValueMessage(String section, String attribute, String value) { return "The value '" + value + "' of '" + attribute + "' present under '" + section + "' should be boolean"; } @@ -974,7 +282,7 @@ public class OnapCommandUtils { return set; } - private static void validateHttpResultMap(List errorList, Map values) throws OnapCommandException { + static void validateHttpResultMap(List errorList, Map values) throws OnapCommandException { Map valMap = (Map) values.get(HTTP); List> attributes = (List>) ((Map)values.get(RESULTS)).get(ATTRIBUTES); Set resultMapParams = ((Map) valMap.get(RESULT_MAP)).keySet(); @@ -990,7 +298,7 @@ public class OnapCommandUtils { } } - private static void validateHttpSccessCodes(List errorList, List requestSuccessCodes) { + static void validateHttpSccessCodes(List errorList, List requestSuccessCodes) { if (requestSuccessCodes == null || requestSuccessCodes.isEmpty()) { errorList.add(HTTP_SUCCESS_CODE_INVALID); @@ -1008,9 +316,7 @@ public class OnapCommandUtils { } - - private static ArrayList validateHttpSchemaSection(Map values) { - + static ArrayList validateHttpSchemaSection(Map values) { ArrayList errorList = new ArrayList<>(); Map map = (Map) values.get(HTTP); Map requestMap = (Map) map.get(REQUEST); -- cgit 1.2.3-korg