aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/main/java
diff options
context:
space:
mode:
authorsubhash kumar singh <subhash.kumar.singh@huawei.com>2017-07-31 00:45:06 +0530
committersubhash kumar singh <subhash.kumar.singh@huawei.com>2017-08-04 15:47:47 +0530
commite45e668a4f6bdeca4ddc4071c4f37822792d65ed (patch)
tree3d0d24e02a96e56ef4c68bbfdad539ced6b6bab0 /framework/src/main/java
parent31439b63b92b8124588a6262e1d9d0a89cdd46e9 (diff)
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 <subhash.kumar.singh@huawei.com>
Diffstat (limited to 'framework/src/main/java')
-rw-r--r--framework/src/main/java/org/onap/cli/fw/OnapCommand.java24
-rw-r--r--framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java2
-rw-r--r--framework/src/main/java/org/onap/cli/fw/conf/Constants.java10
-rw-r--r--framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java12
-rw-r--r--framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java35
-rw-r--r--framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSchema.java4
-rw-r--r--framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java183
7 files changed, 197 insertions, 73 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java
index 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<String> 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<String, ?> 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<String> shortOptions = new ArrayList<>();
- List<String> longOptions = new ArrayList<>();
- List<String> names = new ArrayList<>();
+ try {
+ Map<String, ?> defaultParameterMap = includeDefault ?
+ validateSchemaVersion(Constants.DEFAULT_PARAMETER_FILE_NAME, cmd.getSchemaVersion()) : new HashMap<>();
+ Map<String, List<Map<String, String>>> commandYamlMap = (Map<String, List<Map<String, String>>>)validateSchemaVersion(schemaName, cmd.getSchemaVersion());
- if (includeDefault) {
- loadSchema(cmd, Constants.DEFAULT_PARAMETER_FILE_NAME, shortOptions, longOptions, names);
- }
+ List<String> defParams = new ArrayList<>();
- loadSchema(cmd, schemaName, shortOptions, longOptions, names);
+ if (includeDefault) {
+ if (commandYamlMap.get(Constants.PARAMETERS) == null) {
+ commandYamlMap.put(Constants.PARAMETERS, (List<Map<String, String>>) defaultParameterMap.get(Constants.PARAMETERS));
+ } else {
+ commandYamlMap.get(Constants.PARAMETERS).addAll((List<Map<String, String>>) defaultParameterMap.get(Constants.PARAMETERS));
+ }
+ defParams = ((List<Map<String, String>>) 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<String> shortOptions,
- List<String> longOptions, List<String> names) throws OnapCommandException {
- try {
- Map<String, ?> values = validateSchemaVersion(schemaName, cmd.getSchemaVersion());
+ private static void parseSchema(OnapCommand cmd,
+ final Map<String, ?> values,
+ final List<String> defaultParamNames) throws OnapCommandException {
+
+ List<String> shortOptions = new ArrayList<>();
+ List<String> longOptions = new ArrayList<>();
+ List<String> names = new ArrayList<>();
+ Set<String> filteredDefaultParams = new HashSet<>();
+
+ List<String> sections = Arrays.asList(Constants.NAME, Constants.DESCRIPTION, Constants.SERVICE,
+ Constants.DEFAULT_PARAMETERS, Constants.PARAMETERS, Constants.RESULTS);
- for (Map.Entry<String, ?> 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<String, String> map = (Map<String, String>) values.get(key);
+ }
+ } else if (Constants.SERVICE.equals(key)) {
+ Map<String, String> map = (Map<String, String>) values.get(key);
+ if (map != null) {
OnapService srv = new OnapService();
for (Map.Entry<String, String> entry1 : map.entrySet()) {
@@ -207,10 +218,55 @@ public class OnapCommandUtils {
}
cmd.setService(srv);
- } else if (Constants.PARAMETERS.equals(key)) {
- List<Map<String, String>> list = (ArrayList) values.get(key);
+ }
+ } else if (Constants.DEFAULT_PARAMETERS.equals(key)) {
+
+ Map<String, List<String>> 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<String> includeParams = defParameters.containsKey(Constants.DEFAULT_PARAMETERS_INCLUDE) ?
+ defParameters.get(Constants.DEFAULT_PARAMETERS_INCLUDE) : new ArrayList<>();
- for (Map<String, String> map : list) {
+ List<String> invInclude = includeParams.stream()
+ .filter(p -> !defaultParamNames.contains(p))
+ .collect(Collectors.toList());
+
+ List<String> excludeParams = defParameters.containsKey(Constants.DEFAULT_PARAMETERS_EXCLUDE) ?
+ defParameters.get(Constants.DEFAULT_PARAMETERS_EXCLUDE) : new ArrayList<>();
+
+ List<String> 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<Map<String, String>> parameters = (List) values.get(key);
+
+ if (parameters != null) {
+ for (Map<String, String> map : parameters) {
OnapCommandParameter param = new OnapCommandParameter();
for (Map.Entry<String, String> 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<String, ?> valueMap = (Map<String, ?>) values.get(key);
+ }
+ } else if (Constants.RESULTS.equals(key)) {
+ Map<String, ?> valueMap = (Map<String, ?>) values.get(key);
+ if (valueMap != null) {
OnapCommandResult result = new OnapCommandResult();
for (Map.Entry<String, ?> 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);
}
}