package org.onap.cli.fw.registrar;
import java.io.IOException;
+import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.onap.cli.fw.cmd.OnapCommand;
+import org.onap.cli.fw.cmd.OnapCommandType;
+import org.onap.cli.fw.cmd.dummy.OnapCommandDummy;
import org.onap.cli.fw.conf.OnapCommandConfig;
import org.onap.cli.fw.conf.OnapCommandConstants;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandHelpFailed;
-import org.onap.cli.fw.error.OnapCommandInvalidRegistration;
import org.onap.cli.fw.error.OnapCommandNotFound;
import org.onap.cli.fw.error.OnapCommandProductVersionInvalid;
import org.onap.cli.fw.error.OnapCommandRegistrationProductInfoMissing;
import org.onap.cli.fw.error.OnapUnsupportedSchemaProfile;
-import org.onap.cli.fw.input.cache.OnapCommandParameterCache;
import org.onap.cli.fw.output.OnapCommandPrintDirection;
import org.onap.cli.fw.output.OnapCommandResult;
import org.onap.cli.fw.output.OnapCommandResultAttribute;
import org.onap.cli.fw.output.OnapCommandResultType;
import org.onap.cli.fw.schema.OnapCommandSchema;
import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
+import org.onap.cli.fw.store.OnapCommandProfileStore;
import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
import org.onap.cli.fw.utils.OnapCommandHelperUtils;
import org.onap.cli.fw.utils.OnapCommandUtils;
*/
public class OnapCommandRegistrar {
- private static Logger LOG = LoggerFactory.getLogger(OnapCommandRegistrar.class);
+ private static Logger log = LoggerFactory.getLogger(OnapCommandRegistrar.class);
private Map<String, Class<? extends OnapCommand>> registry = new HashMap<>();
private boolean isInteractiveMode = false;
- private OnapCommandParameterCache paramCache = OnapCommandParameterCache.getInstance();
+ private String host;
+
+ private int port;
+
+ private OnapCommandProfileStore paramCache = OnapCommandProfileStore.getInstance();
public boolean isInteractiveMode() {
return isInteractiveMode;
}
public Map<String, String> getParamCache() {
- return paramCache.getParams(this.getEnabledProductVersion());
+ return this.getParamCache(this.getEnabledProductVersion());
+ }
+
+ public Map<String, String> getParamCache(String product) {
+ return paramCache.getParams(product);
}
public void addParamCache(String paramName, String paramValue) {
paramCache.remove(this.getEnabledProductVersion(), paramName);
}
- public void setProfile(String profileName) {
+ public void setProfile(String profileName, List<String> includes, List<String> excludes) throws OnapCommandException {
this.paramCache.setProfile(profileName);
+
+ for (String profile : includes) {
+ this.paramCache.includeProfile(profile);
+ }
+
+ for (String profile : excludes) {
+ this.paramCache.excludeProfile(profile);
+ }
+ }
+
+ public List<String> getUserProfiles() {
+ return paramCache.getProfiles();
}
private static OnapCommandRegistrar registrar = null;
/**
* Register the command into registrar and throws OnapInvalidCommandRegistration for invalid command.
*
- * @param name
- * Command Name
- * @param cmd
- * Command Class
- * @throws OnapCommandInvalidRegistration
- * Invalid registration exception
+ * @param name Command Name
+ * @param cmd Command Class
* @throws OnapCommandRegistrationProductInfoMissing
*/
- private void register(String name, String version, Class<? extends OnapCommand> cmd) throws OnapCommandInvalidRegistration, OnapCommandRegistrationProductInfoMissing {
+ private void register(String name, String version, Class<? extends OnapCommand> cmd) throws OnapCommandRegistrationProductInfoMissing {
if (version == null || version.isEmpty()) {
throw new OnapCommandRegistrationProductInfoMissing(name);
}
this.registry.put(name + ":" + version, cmd);
+ log.info("REGISITER : {} : {} = {}", name, version, cmd.getCanonicalName());
this.availableProductVersions.add(version);
}
}
private OnapCommandRegistrar() {
- this.enabledProductVersion = System.getenv(OnapCommandConstants.OPEN_CLI_PRODUCT_IN_USE_ENV_NAME);
- if (this.enabledProductVersion == null) {
- this.enabledProductVersion = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_PRODUCT_NAME);
+ this.enabledProductVersion = System.getenv(OnapCommandConstants.OPEN_CLI_PRODUCT_IN_USE_ENV_NAME); //NOSONAR
+ if (this.enabledProductVersion == null) {
+ this.enabledProductVersion = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_PRODUCT_NAME);
}
}
/**
* Get global registrar.
*
- * @throws OnapCommandException
- * exception
+ * @throws OnapCommandException exception
*/
public static OnapCommandRegistrar getRegistrar() throws OnapCommandException {
if (registrar == null) {
registrar = new OnapCommandRegistrar();
- registrar.autoDiscoverSchemas();
+ registrar.autoDiscoverSchemas(true);
}
return registrar;
}
+ public void resync() throws OnapCommandException {
+ registrar.autoDiscoverSchemas(false);
+ }
+
/**
* Get the list of discovered commands by registrar.
*
return cmds;
}
- for (String cmd: this.registry.keySet()) {
+ for (String cmd : this.registry.keySet()) {
if (cmd.split(":")[1].equalsIgnoreCase(version)) {
cmds.add(cmd.split(":")[0]);
}
* Returns command details.
*
* @return map
- * @throws OnapCommandException
- * exception
+ * @throws OnapCommandException exception
*/
public List<OnapCommandSchemaInfo> listCommandInfo() throws OnapCommandException {
return OnapCommandDiscoveryUtils.discoverSchemas();
/**
* Get the OnapCommand, which CLI main would use to find the command based on the command name.
*
- * @param cmdName
- * Name of command
+ * @param cmdName Name of command
* @return OnapCommand
- * @throws OnapCommandException
- * Exception
+ * @throws OnapCommandException Exception
*/
public OnapCommand get(String cmdName) throws OnapCommandException {
return this.get(cmdName, this.getEnabledProductVersion());
/**
* Get the OnapCommand, which CLI main would use to find the command based on the command name.
*
- * @param cmdName
- * Name of command
- * @param version
- * product version
+ * @param cmdName Name of command
+ * @param version product version
* @return OnapCommand
- * @throws OnapCommandException
- * Exception
+ * @throws OnapCommandException Exception
*/
public OnapCommand get(String cmdName, String version) throws OnapCommandException {
Class<? extends OnapCommand> cls = registry.get(cmdName + ":" + version);
}
OnapCommand cmd = OnapCommandDiscoveryUtils.loadCommandClass(cls);
- String schemaName = OnapCommandDiscoveryUtils.getSchemaInfo(cmdName, version).getSchemaName();
- cmd.initializeSchema(schemaName);
+
+ OnapCommandSchemaInfo info = OnapCommandDiscoveryUtils.getSchemaInfo(cmdName, version);
+
+ cmd.initializeSchema(info);
return cmd;
}
return map;
}
- private void autoDiscoverSchemas() throws OnapCommandException {
- List<OnapCommandSchemaInfo> schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(true);
+ private void autoDiscoverSchemas(boolean refresh) throws OnapCommandException {
+ List<OnapCommandSchemaInfo> schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(refresh);
Map<String, Class<OnapCommand>> plugins = this.autoDiscoverCommandPlugins();
for (OnapCommandSchemaInfo schema : schemas) {
if (schema.isIgnore()) {
- LOG.info("Ignoring schema " + schema.getSchemaURI());
+ log.info("Ignoring schema {}", schema.getSchemaURI());
continue;
}
- //First check if there is an specific plugin exist, otherwise check for profile plugin
- if (plugins.containsKey(schema.getSchemaName())) {
- this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName()));
- } else if (plugins.containsKey(schema.getSchemaProfile())) {
+ //First check if there is an specific plug-in exist, otherwise check for profile plug-in
+ if (schema.isRpc()) {
+ //proxy the schema by using rpc schema, when the schema is marked with rpc
+ this.register(schema.getCmdName(), schema.getProduct(), plugins.get("schema-rpc.yaml"));
+ } else if (plugins.containsKey(schema.getSchemaName())) {
+ this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName()));
+ } else if (plugins.containsKey(schema.getSchemaProfile())) {
this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaProfile()));
} else {
- LOG.info("Ignoring schema " + schema.getSchemaURI());
+ this.register(schema.getCmdName(), schema.getProduct(), OnapCommandDummy.class);
}
}
}
}
String buildTime = OnapCommandHelperUtils.findLastBuildTime();
- if (buildTime!= null && !buildTime.isEmpty()) {
+ if (buildTime != null && !buildTime.isEmpty()) {
buildTime = " [" + buildTime + "]";
} else {
buildTime = "";
String versionInfo = "";
try {
- versionInfo = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(OnapCommandConstants.VERSION_INFO));
- } catch (IOException e) {
- //Never occurs // NOSONAR
+ versionInfo = IOUtils.toString((this.getClass().getClassLoader().getResourceAsStream(OnapCommandConstants.VERSION_INFO)), (Charset) null);
+ } catch (IOException e) { // NOSONAR
+ //Never occurs
}
versionInfo = versionInfo.replaceAll(OnapCommandConstants.VERSION_INFO_PLACE_HOLDER_ENB_PRD_VER, configuredProductVersion);
* Provides the help message in tabular format for all commands registered in this registrar.
*
* @return string
- * @throws OnapCommandHelpFailed
- * Help cmd failed
+ * @throws OnapCommandHelpFailed Help cmd failed
*/
public String getHelp() throws OnapCommandHelpFailed {
return this.getHelp(false);
attrSrv.setScope(OnapCommandResultAttributeScope.SHORT);
help.getRecords().add(attrSrv);
+ OnapCommandResultAttribute attrState = new OnapCommandResultAttribute();
+ attrState.setName(OnapCommandConstants.INFO_STATE.toUpperCase());
+ attrState.setDescription(OnapCommandConstants.INFO_STATE);
+ attrState.setScope(OnapCommandResultAttributeScope.SHORT);
+ help.getRecords().add(attrState);
+
+
OnapCommandResultAttribute attrDesc = new OnapCommandResultAttribute();
attrDesc.setName(OnapCommandConstants.DESCRIPTION.toUpperCase());
attrDesc.setDescription(OnapCommandConstants.DESCRIPTION);
attrDesc.setScope(OnapCommandResultAttributeScope.SHORT);
help.getRecords().add(attrDesc);
- for (String cmdName : isEnabledProductVersionOnly ? OnapCommandUtils.sort(this.listCommandsForEnabledProductVersion()) : OnapCommandUtils.sort(this.listCommands())) {
+ for (String cmdName : isEnabledProductVersionOnly ?
+ OnapCommandUtils.sort(this.listCommandsForEnabledProductVersion()) :
+ OnapCommandUtils.sort(this.listCommands())) {
OnapCommand cmd;
try {
if (!isEnabledProductVersionOnly) {
- String []cmdVer = cmdName.split(":");
+ String[] cmdVer = cmdName.split(":");
cmd = this.get(cmdVer[0], cmdVer[1]);
attr.getValues().add(cmdVer[0]);
attrVer.getValues().add(cmdVer[1]);
attr.getValues().add(cmdName);
}
- attrSrv.getValues().add(cmd.printVersion());
+ //don't expose system commands for user usage
+ if (cmd.getInfo().getCommandType().name().equalsIgnoreCase(OnapCommandType.SYSTEM.name())) {
+ continue;
+ }
+
+ attrSrv.getValues().add(cmd.getInfo().getService());
attrDesc.getValues().add(cmd.getDescription());
+ attrState.getValues().add(cmd.getInfo().getState().name());
} catch (OnapCommandException e) {
throw new OnapCommandHelpFailed(e);
}
throw new OnapCommandHelpFailed(e);
}
}
+
+ public List<Map<String, Object>> getTestSuite(String cmd, String product) throws OnapCommandException {
+ return OnapCommandDiscoveryUtils.createTestSuite(cmd, product);
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
}