diff options
author | 2017-09-11 18:39:55 +0530 | |
---|---|---|
committer | 2017-09-11 18:40:41 +0530 | |
commit | ee757a5761156fcbcff3d0bd4f6e75bb0931d536 (patch) | |
tree | df596d0de0261e1c00a814b0922a8bc757adf50c /framework/src/main/java/org/onap | |
parent | 768078d72d2ddefd7db35f269584b6ca38cc5414 (diff) |
CLI profile support
invoke CLI with --profile <name> to persist the
settings across the sessions.
CLI-5
Change-Id: I214334dab11fecdb730c780976d5d9bb2c14509d
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'framework/src/main/java/org/onap')
6 files changed, 217 insertions, 85 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java b/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java index 01d4742e..4e892153 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java @@ -60,7 +60,7 @@ public class OnapCommandRegistrar { private boolean isDevMode = Boolean.getBoolean(System.getenv(Constants.CLI_DEV_MODE)); - private OnapCommandParameterCache paramCache = new OnapCommandParameterCache(); + private OnapCommandParameterCache paramCache = OnapCommandParameterCache.getInstance(); public boolean isInteractiveMode() { return isInteractiveMode; @@ -90,6 +90,9 @@ public class OnapCommandRegistrar { this.isDevMode = isDevMode; } + public void setProfile(String profileName) { + this.paramCache.setProfile(profileName); + } private static OnapCommandRegistrar registrar = null; 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 d7e277c7..00bdb855 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 @@ -147,9 +147,9 @@ public class Constants { public static final String EXTERNAL_YAML_PATTERN = "/**/*.yaml"; public static final String EXTERNAL_JSON_PATTERN = "/**/*.json"; public static final String EXTERNAL_SCHEMA_PATH_PATERN = EXTERNAL_SCHEMA_DIRECTORY + EXTERNAL_YAML_PATTERN; - public static final String EXTERNAL_DISCOVERY_DIRECTORY = "data"; + public static final String DATA_DIRECTORY = "data"; public static final String EXTERNAL_DISCOVERY_FILE = "cli-schema.json"; - public static final String EXTERNAL_DISCOVERY_DIRECTORY_PATTERN = EXTERNAL_DISCOVERY_DIRECTORY + public static final String DATA_DIRECTORY_JSON_PATTERN = DATA_DIRECTORY + EXTERNAL_JSON_PATTERN; public static final String PARAMETER_TYPE_JSON = "json"; @@ -194,7 +194,7 @@ public class Constants { public static final String HTTP_SAMPLE_RESPONSE_FAILED_PARSING = "The http Sample response json is failed to parse."; public static final String USE_DIRECTIVE = "use"; - public static final String PARAM_CACHE_FILE_NAME = "cli.parameter.cache.file"; + public static final String PARAM_CACHE_FILE_NAME = "global-profile"; public static final String CLI_DEV_MODE = "CLI_DEV"; 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 35461a05..b706ea66 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 @@ -161,7 +161,4 @@ public final class OnapCommandConfg { return Arrays.stream(prps.getProperty(key).split(",")).map(String::trim).collect(Collectors.toList()); // NOSONAR } - public static String getParamCacheFileName() { - return prps.getProperty(Constants.PARAM_CACHE_FILE_NAME); - } } diff --git a/framework/src/main/java/org/onap/cli/fw/input/cache/OnapCommandParameterCache.java b/framework/src/main/java/org/onap/cli/fw/input/cache/OnapCommandParameterCache.java index 5c8bdf6d..6ce09179 100644 --- a/framework/src/main/java/org/onap/cli/fw/input/cache/OnapCommandParameterCache.java +++ b/framework/src/main/java/org/onap/cli/fw/input/cache/OnapCommandParameterCache.java @@ -16,13 +16,36 @@ package org.onap.cli.fw.input.cache; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.onap.cli.fw.conf.Constants; +import org.onap.cli.fw.utils.OnapCommandUtils; + public class OnapCommandParameterCache { public Map<String, Map<String, String>> paramCache = new HashMap<>(); + private static OnapCommandParameterCache single = null; + + private String profileName = Constants.PARAM_CACHE_FILE_NAME; + + private OnapCommandParameterCache() { + + } + + public static OnapCommandParameterCache getInstance() { + if (single == null) { + single = new OnapCommandParameterCache(); + } + + single.load(); + return single; + } + + public void add(String productVersion, String paramName, String paramValue) { if (!paramCache.containsKey(productVersion)) { @@ -30,6 +53,8 @@ public class OnapCommandParameterCache { } paramCache.get(productVersion).put(paramName, paramValue); + + this.persist(); } public void remove(String productVersion, String paramName) { @@ -38,6 +63,8 @@ public class OnapCommandParameterCache { paramCache.get(productVersion).remove(paramName); } } + + this.persist(); } public Map<String, String> getParams(String productVersion) { @@ -48,11 +75,33 @@ public class OnapCommandParameterCache { } } - public void persist() { - // mrkana add persistence logic + private void persist() { + List<Param> params = new ArrayList<>(); + for (String p: this.paramCache.keySet()) { + for (String name: this.paramCache.get(p).keySet()) { + + Param param = new Param(); + param.setProduct(p); + param.setName(name); + param.setValue(this.paramCache.get(p).get(name)); + + params.add(param); + } + } + + OnapCommandUtils.persistParams(params, this.profileName); + } + + private void load() { + List<Param> params = OnapCommandUtils.loadParamFromCache(this.profileName); + + for (Param p : params) { + this.add(p.getProduct(), p.getName(), p.getValue()); + } } - public void load() { - // mrkanag add loading cache from persistence + public void setProfile(String profileName) { + this.profileName = profileName; + this.load(); } } diff --git a/framework/src/main/java/org/onap/cli/fw/input/cache/Param.java b/framework/src/main/java/org/onap/cli/fw/input/cache/Param.java new file mode 100644 index 00000000..5a32e886 --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/input/cache/Param.java @@ -0,0 +1,45 @@ +/* + * 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.input.cache; + +public class Param { + + private String product; + + private String name; + + private String value; + + public String getProduct() { + return product; + } + public void setProduct(String product) { + this.product = product; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } +} 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 d850199d..f4fb7515 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,70 +16,6 @@ package org.onap.cli.fw.utils; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.jayway.jsonpath.JsonPath; -import net.minidev.json.JSONArray; -import net.minidev.json.JSONObject; -import org.onap.cli.fw.OnapCommand; -import org.onap.cli.fw.OnapCommandRegistrar; -import org.onap.cli.fw.ad.OnapCredentials; -import org.onap.cli.fw.ad.OnapService; -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; -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.OnapCommandHttpInvalidResultMap; -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; -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.OnapCommandParameterNameConflict; -import org.onap.cli.fw.error.OnapCommandParameterNotFound; -import org.onap.cli.fw.error.OnapCommandParameterOptionConflict; -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.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.onap.cli.fw.output.ResultType; -import org.onap.cli.fw.run.OnapCommandExecutor; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; -import org.yaml.snakeyaml.Yaml; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -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; - 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; @@ -100,8 +36,8 @@ import static org.onap.cli.fw.conf.Constants.DIRECTION; import static org.onap.cli.fw.conf.Constants.ENTITY; import static org.onap.cli.fw.conf.Constants.EXCEPTION; import static org.onap.cli.fw.conf.Constants.EXECUTOR; -import static org.onap.cli.fw.conf.Constants.EXTERNAL_DISCOVERY_DIRECTORY; -import static org.onap.cli.fw.conf.Constants.EXTERNAL_DISCOVERY_DIRECTORY_PATTERN; +import static org.onap.cli.fw.conf.Constants.DATA_DIRECTORY; +import static org.onap.cli.fw.conf.Constants.DATA_DIRECTORY_JSON_PATTERN; import static org.onap.cli.fw.conf.Constants.EXTERNAL_DISCOVERY_FILE; import static org.onap.cli.fw.conf.Constants.EXTERNAL_SCHEMA_DIRECTORY; import static org.onap.cli.fw.conf.Constants.EXTERNAL_SCHEMA_PATH_PATERN; @@ -120,15 +56,15 @@ import static org.onap.cli.fw.conf.Constants.INPUT_PARAMS_MANDATORY_LIST; 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.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.ONAP_CMD_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.MULTIPART_ENTITY_NAME; 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; @@ -150,6 +86,73 @@ 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; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +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; + +import org.onap.cli.fw.OnapCommand; +import org.onap.cli.fw.OnapCommandRegistrar; +import org.onap.cli.fw.ad.OnapCredentials; +import org.onap.cli.fw.ad.OnapService; +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; +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.OnapCommandHttpInvalidResultMap; +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; +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.OnapCommandParameterNameConflict; +import org.onap.cli.fw.error.OnapCommandParameterNotFound; +import org.onap.cli.fw.error.OnapCommandParameterOptionConflict; +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.input.OnapCommandParameter; +import org.onap.cli.fw.input.ParameterType; +import org.onap.cli.fw.input.cache.Param; +import org.onap.cli.fw.output.OnapCommandResult; +import org.onap.cli.fw.output.OnapCommandResultAttribute; +import org.onap.cli.fw.output.OnapCommandResultAttributeScope; +import org.onap.cli.fw.output.PrintDirection; +import org.onap.cli.fw.output.ResultType; +import org.onap.cli.fw.run.OnapCommandExecutor; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.yaml.snakeyaml.Yaml; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.jayway.jsonpath.JsonPath; + +import net.minidev.json.JSONArray; +import net.minidev.json.JSONObject; + /** * Provides helper method to parse Yaml files and produce required objects. * @@ -1609,7 +1612,7 @@ public class OnapCommandUtils { public static void persist(List<ExternalSchema> schemas) throws OnapCommandDiscoveryFailed { if (schemas != null) { try { - Resource[] resources = getExternalResources(EXTERNAL_DISCOVERY_DIRECTORY); + Resource[] resources = getExternalResources(DATA_DIRECTORY); if (resources != null && resources.length == 1) { String path = resources[0].getURI().getPath(); File file = new File(path + File.separator + EXTERNAL_DISCOVERY_FILE); @@ -1617,12 +1620,28 @@ public class OnapCommandUtils { mapper.writerWithDefaultPrettyPrinter().writeValue(file, schemas); } } catch (IOException e1) { - throw new OnapCommandDiscoveryFailed(EXTERNAL_DISCOVERY_DIRECTORY, + throw new OnapCommandDiscoveryFailed(DATA_DIRECTORY, EXTERNAL_DISCOVERY_FILE, e1); } } } + public static void persistParams(List<Param> params, String profileName) { + if (params != null) { + try { + Resource[] resources = getExternalResources(DATA_DIRECTORY); + if (resources != null && resources.length == 1) { + String path = resources[0].getURI().getPath(); + File file = new File(path + File.separator + profileName + ".json"); + ObjectMapper mapper = new ObjectMapper(); + mapper.writerWithDefaultPrettyPrinter().writeValue(file, params); + } + } catch (IOException e1) { + // Never occur // NOSONAR + } + } + } + /** * Check if json file discovered or not. * @@ -1634,12 +1653,12 @@ public class OnapCommandUtils { Resource resource = null; try { resource = getExternalResource(EXTERNAL_DISCOVERY_FILE, - EXTERNAL_DISCOVERY_DIRECTORY_PATTERN); + DATA_DIRECTORY_JSON_PATTERN); if (resource != null) { return true; } } catch (IOException e) { - throw new OnapCommandDiscoveryFailed(EXTERNAL_DISCOVERY_DIRECTORY, + throw new OnapCommandDiscoveryFailed(DATA_DIRECTORY, EXTERNAL_DISCOVERY_FILE, e); } @@ -1665,7 +1684,7 @@ public class OnapCommandUtils { } else { try { Resource resource = getExternalResource(EXTERNAL_DISCOVERY_FILE, - EXTERNAL_DISCOVERY_DIRECTORY_PATTERN); + DATA_DIRECTORY_JSON_PATTERN); if (resource != null) { File file = new File(resource.getURI().getPath()); ObjectMapper mapper = new ObjectMapper(); @@ -1673,7 +1692,7 @@ public class OnapCommandUtils { schemas.addAll(Arrays.asList(list)); } } catch (IOException e) { - throw new OnapCommandDiscoveryFailed(EXTERNAL_DISCOVERY_DIRECTORY, + throw new OnapCommandDiscoveryFailed(DATA_DIRECTORY, EXTERNAL_DISCOVERY_FILE, e); } } @@ -1681,6 +1700,25 @@ public class OnapCommandUtils { return schemas; } + public static List<Param> loadParamFromCache(String profileName) { + List<Param> params = new ArrayList<>(); + + try { + Resource resource = getExternalResource(profileName + ".json", + DATA_DIRECTORY_JSON_PATTERN); + if (resource != null) { + File file = new File(resource.getURI().getPath()); + ObjectMapper mapper = new ObjectMapper(); + Param[] list = mapper.readValue(file, Param[].class); + params.addAll(Arrays.asList(list)); + } + } catch (IOException e) { + // Never occur // NOSONAR + } + + return params; + } + /** * Fetch a particular schema details. * |