2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.registrar;
19 import java.io.IOException;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
26 import org.apache.commons.io.IOUtils;
27 import org.onap.cli.fw.cmd.OnapCommand;
28 import org.onap.cli.fw.conf.OnapCommandConfig;
29 import org.onap.cli.fw.conf.OnapCommandConstants;
30 import org.onap.cli.fw.error.OnapCommandException;
31 import org.onap.cli.fw.error.OnapCommandHelpFailed;
32 import org.onap.cli.fw.error.OnapCommandInvalidRegistration;
33 import org.onap.cli.fw.error.OnapCommandNotFound;
34 import org.onap.cli.fw.error.OnapCommandProductVersionInvalid;
35 import org.onap.cli.fw.error.OnapCommandRegistrationProductInfoMissing;
36 import org.onap.cli.fw.error.OnapUnsupportedSchemaProfile;
37 import org.onap.cli.fw.input.cache.OnapCommandParameterCache;
38 import org.onap.cli.fw.output.OnapCommandPrintDirection;
39 import org.onap.cli.fw.output.OnapCommandResult;
40 import org.onap.cli.fw.output.OnapCommandResultAttribute;
41 import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
42 import org.onap.cli.fw.output.OnapCommandResultType;
43 import org.onap.cli.fw.schema.OnapCommandSchema;
44 import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
45 import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
46 import org.onap.cli.fw.utils.OnapCommandHelperUtils;
47 import org.onap.cli.fw.utils.OnapCommandUtils;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
53 * Oclip Command registrar provides a common place, where every command would get registered automatically when its
57 public class OnapCommandRegistrar {
59 private static Logger LOG = LoggerFactory.getLogger(OnapCommandRegistrar.class);
61 private Map<String, Class<? extends OnapCommand>> registry = new HashMap<>();
63 private Map<String, Class<? extends OnapCommand>> registryProfilePlugins = new HashMap<>();
65 private Set<String> availableProductVersions = new HashSet<>();
67 private String enabledProductVersion = null;
69 private boolean isInteractiveMode = false;
71 private OnapCommandParameterCache paramCache = OnapCommandParameterCache.getInstance();
73 public boolean isInteractiveMode() {
74 return isInteractiveMode;
77 public void setInteractiveMode(boolean isInteractiveMode) {
78 this.isInteractiveMode = isInteractiveMode;
81 public Map<String, String> getParamCache() {
82 return paramCache.getParams(this.getEnabledProductVersion());
85 public void addParamCache(String paramName, String paramValue) {
86 paramCache.add(this.getEnabledProductVersion(), paramName, paramValue);
89 public void removeParamCache(String paramName) {
90 paramCache.remove(this.getEnabledProductVersion(), paramName);
93 public void setProfile(String profileName, List<String> includes, List<String> excludes) {
94 this.paramCache.setProfile(profileName);
96 for (String profile: includes) {
97 this.paramCache.includeProfile(profile);
100 for (String profile: excludes) {
101 this.paramCache.excludeProfile(profile);
105 public List<String> getUserProfiles() {
106 return paramCache.getProfiles();
109 private static OnapCommandRegistrar registrar = null;
112 * Register the command into registrar and throws OnapInvalidCommandRegistration for invalid command.
118 * @throws OnapCommandInvalidRegistration
119 * Invalid registration exception
120 * @throws OnapCommandRegistrationProductInfoMissing
122 private void register(String name, String version, Class<? extends OnapCommand> cmd) throws OnapCommandInvalidRegistration, OnapCommandRegistrationProductInfoMissing {
123 if (version == null || version.isEmpty()) {
124 throw new OnapCommandRegistrationProductInfoMissing(name);
127 this.registry.put(name + ":" + version, cmd);
128 this.availableProductVersions.add(version);
132 private void registerProfilePlugin(String profile, Class<? extends OnapCommand> cmd) {
133 this.registryProfilePlugins.put(profile, cmd);
136 private OnapCommandRegistrar() {
137 this.enabledProductVersion = System.getenv(OnapCommandConstants.OPEN_CLI_PRODUCT_IN_USE_ENV_NAME);
138 if (this.enabledProductVersion == null) {
139 this.enabledProductVersion = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_PRODUCT_NAME);
144 * Get global registrar.
146 * @throws OnapCommandException
149 public static OnapCommandRegistrar getRegistrar() throws OnapCommandException {
150 if (registrar == null) {
151 registrar = new OnapCommandRegistrar();
152 registrar.autoDiscoverSchemas();
159 * Get the list of discovered commands by registrar.
163 public Set<String> listCommands() {
164 return this.registry.keySet();
168 * Get the list of discovered commands for a given product version in registrar.
172 public Set<String> listCommandsForEnabledProductVersion() {
173 String version = this.getEnabledProductVersion();
175 Set<String> cmds = new HashSet<>();
176 if (!this.availableProductVersions.contains(version)) {
180 for (String cmd: this.registry.keySet()) {
181 if (cmd.split(":")[1].equalsIgnoreCase(version)) {
182 cmds.add(cmd.split(":")[0]);
188 public Class<? extends OnapCommand> getProfilePlugin(String profile) throws OnapUnsupportedSchemaProfile {
189 if (!this.registryProfilePlugins.containsKey(profile)) {
190 throw new OnapUnsupportedSchemaProfile(profile);
193 return this.registryProfilePlugins.get(profile);
196 public Set<String> getAvailableProductVersions() {
197 return this.availableProductVersions;
200 public void setEnabledProductVersion(String version) throws OnapCommandProductVersionInvalid {
201 if (!this.availableProductVersions.contains(version)) {
202 throw new OnapCommandProductVersionInvalid(version, availableProductVersions);
205 this.enabledProductVersion = version;
208 public String getEnabledProductVersion() {
209 return this.enabledProductVersion;
213 * Returns command details.
216 * @throws OnapCommandException
219 public List<OnapCommandSchemaInfo> listCommandInfo() throws OnapCommandException {
220 return OnapCommandDiscoveryUtils.discoverSchemas();
224 * Get the OnapCommand, which CLI main would use to find the command based on the command name.
228 * @return OnapCommand
229 * @throws OnapCommandException
232 public OnapCommand get(String cmdName) throws OnapCommandException {
233 return this.get(cmdName, this.getEnabledProductVersion());
237 * Get the OnapCommand, which CLI main would use to find the command based on the command name.
243 * @return OnapCommand
244 * @throws OnapCommandException
247 public OnapCommand get(String cmdName, String version) throws OnapCommandException {
248 Class<? extends OnapCommand> cls = registry.get(cmdName + ":" + version);
249 //mrkanag: Restrict auth/catalog type commands only available during devMode. in production
250 //don't expose the auth type and catalog type commands
253 throw new OnapCommandNotFound(cmdName, version);
256 OnapCommand cmd = OnapCommandDiscoveryUtils.loadCommandClass(cls);
257 String schemaName = OnapCommandDiscoveryUtils.getSchemaInfo(cmdName, version).getSchemaName();
258 cmd.initializeSchema(schemaName);
263 private Map<String, Class<OnapCommand>> autoDiscoverCommandPlugins() throws OnapCommandException {
264 List<Class<OnapCommand>> cmds = OnapCommandDiscoveryUtils.discoverCommandPlugins();
265 Map<String, Class<OnapCommand>> map = new HashMap<>();
267 for (Class<OnapCommand> cmd : cmds) {
268 if (cmd.isAnnotationPresent(OnapCommandSchema.class)) {
269 OnapCommandSchema ano = cmd.getAnnotation(OnapCommandSchema.class);
270 if (ano.schema() != null && !ano.schema().isEmpty()) {
271 map.put(ano.schema(), cmd);
272 } else if (ano.type() != null && !ano.type().isEmpty()) {
273 this.registerProfilePlugin(ano.type(), cmd);
274 map.put(ano.type(), cmd);
276 throw new OnapUnsupportedSchemaProfile(ano.schema());
284 private void autoDiscoverSchemas() throws OnapCommandException {
285 List<OnapCommandSchemaInfo> schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(true);
287 Map<String, Class<OnapCommand>> plugins = this.autoDiscoverCommandPlugins();
289 for (OnapCommandSchemaInfo schema : schemas) {
290 if (schema.isIgnore()) {
291 LOG.info("Ignoring schema " + schema.getSchemaURI());
295 //First check if there is an specific plugin exist, otherwise check for profile plugin
296 if (plugins.containsKey(schema.getSchemaName())) {
297 this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName()));
298 } else if (plugins.containsKey(schema.getSchemaProfile())) {
299 this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaProfile()));
301 LOG.info("Ignoring schema " + schema.getSchemaURI());
307 * Helps to find the Oclip CLI version, could be used with --version or -v option.
311 public String getVersion() {
312 String version = this.getClass().getPackage().getImplementationVersion();
313 if (version == null) {
314 version = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_VERSION);
317 String buildTime = OnapCommandHelperUtils.findLastBuildTime();
318 if (buildTime!= null && !buildTime.isEmpty()) {
319 buildTime = " [" + buildTime + "]";
324 String configuredProductVersion = this.getEnabledProductVersion();
326 String versionInfo = "";
328 versionInfo = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(OnapCommandConstants.VERSION_INFO));
329 } catch (IOException e) { // NOSONAR
333 versionInfo = versionInfo.replaceAll(OnapCommandConstants.VERSION_INFO_PLACE_HOLDER_ENB_PRD_VER, configuredProductVersion);
334 versionInfo = versionInfo.replaceAll(OnapCommandConstants.VERSION_INFO_PLACE_HOLDER_AVL_PRD_VER, this.availableProductVersions.toString());
335 versionInfo = versionInfo.replaceAll(OnapCommandConstants.VERSION_INFO_PLACE_HOLDER_VERSION + "", version + buildTime);
341 * Provides the help message in tabular format for all commands registered in this registrar.
344 * @throws OnapCommandHelpFailed
347 public String getHelp() throws OnapCommandHelpFailed {
348 return this.getHelp(false);
351 public String getHelpForEnabledProductVersion() throws OnapCommandHelpFailed {
352 return this.getHelp(true);
355 private String getHelp(boolean isEnabledProductVersionOnly) throws OnapCommandHelpFailed {
356 OnapCommandResult help = new OnapCommandResult();
357 help.setType(OnapCommandResultType.TABLE);
358 help.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
360 OnapCommandResultAttribute attr = new OnapCommandResultAttribute();
361 attr.setName(OnapCommandConstants.NAME.toUpperCase());
362 attr.setDescription(OnapCommandConstants.DESCRIPTION);
363 attr.setScope(OnapCommandResultAttributeScope.SHORT);
364 help.getRecords().add(attr);
366 OnapCommandResultAttribute attrVer = new OnapCommandResultAttribute();
367 if (!isEnabledProductVersionOnly) {
368 attrVer.setName(OnapCommandConstants.INFO_PRODUCT.toUpperCase());
369 attrVer.setDescription(OnapCommandConstants.DESCRIPTION);
370 attrVer.setScope(OnapCommandResultAttributeScope.SHORT);
371 help.getRecords().add(attrVer);
374 OnapCommandResultAttribute attrSrv = new OnapCommandResultAttribute();
375 attrSrv.setName(OnapCommandConstants.INFO_SERVICE.toUpperCase());
376 attrSrv.setDescription(OnapCommandConstants.INFO_SERVICE);
377 attrSrv.setScope(OnapCommandResultAttributeScope.SHORT);
378 help.getRecords().add(attrSrv);
380 OnapCommandResultAttribute attrDesc = new OnapCommandResultAttribute();
381 attrDesc.setName(OnapCommandConstants.DESCRIPTION.toUpperCase());
382 attrDesc.setDescription(OnapCommandConstants.DESCRIPTION);
383 attrDesc.setScope(OnapCommandResultAttributeScope.SHORT);
384 help.getRecords().add(attrDesc);
386 for (String cmdName : isEnabledProductVersionOnly ? OnapCommandUtils.sort(this.listCommandsForEnabledProductVersion()) : OnapCommandUtils.sort(this.listCommands())) {
389 if (!isEnabledProductVersionOnly) {
390 String []cmdVer = cmdName.split(":");
391 cmd = this.get(cmdVer[0], cmdVer[1]);
392 attr.getValues().add(cmdVer[0]);
393 attrVer.getValues().add(cmdVer[1]);
395 cmd = this.get(cmdName);
396 attr.getValues().add(cmdName);
399 attrSrv.getValues().add(cmd.printVersion());
400 attrDesc.getValues().add(cmd.getDescription());
401 } catch (OnapCommandException e) {
402 throw new OnapCommandHelpFailed(e);
407 return "\n\nCommands:\n" + help.print() + (isEnabledProductVersionOnly ? "" : "\n" + this.getVersion());
408 } catch (OnapCommandException e) {
409 throw new OnapCommandHelpFailed(e);