Add command type to support auth and catalog 21/24321/1
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Tue, 19 Sep 2017 18:17:08 +0000 (23:47 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Tue, 14 Nov 2017 09:07:26 +0000 (14:37 +0530)
By default all commands are cmd type

Issue-Id: CLI-66

Change-Id: I90567ecba00def1a1e904d2959a53c1a2cfc098b
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
framework/src/main/java/org/onap/cli/fw/OnapCommand.java
framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java
framework/src/main/java/org/onap/cli/fw/OnapCommandSchema.java
framework/src/main/java/org/onap/cli/fw/cmd/CommandType.java [new file with mode: 0644]
framework/src/main/java/org/onap/cli/fw/conf/Constants.java
framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidCommandType.java [new file with mode: 0644]
framework/src/main/java/org/onap/cli/fw/utils/ExternalSchema.java
framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java
framework/src/main/resources/onap.properties
framework/src/test/java/org/onap/cli/fw/error/OnapCommandErrorTest.java

index 61aa2cb..9ca43fd 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Optional;
 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.cmd.CommandType;
 import org.onap.cli.fw.conf.Constants;
 import org.onap.cli.fw.conf.OnapCommandConfg;
 import org.onap.cli.fw.error.OnapCommandException;
@@ -69,6 +70,8 @@ public abstract class OnapCommand {
 
     protected boolean isInitialzied = false;
 
+    protected CommandType type = CommandType.CMD;
+
     public String getSchemaVersion() {
         return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE;
     }
@@ -148,6 +151,14 @@ public abstract class OnapCommand {
         this.cmdSchemaName = schemaName;
     }
 
+    public CommandType getType() {
+        return this.type;
+    }
+
+    public void setType(CommandType type) {
+        this.type = type;
+    }
+
     /**
      * Initialize this command from command schema.
      *
@@ -258,8 +269,11 @@ public abstract class OnapCommand {
 
         try {
             OnapCredentials creds = OnapCommandUtils.fromParameters(this.getParameters());
+
+            // For auth type commands, login and logout logic is not required
             boolean isAuthRequired = !this.onapService.isNoAuth()
-                    && "false".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue());
+                    && "false".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue())
+                    && !this.getType().equals(CommandType.AUTH);
 
             if (!isCommandInternal()) {
                 this.authClient = new OnapAuthClient(
index 28b2f9b..987c3f0 100644 (file)
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.onap.cli.fw.cmd.CommandType;
 import org.onap.cli.fw.cmd.OnapHttpCommand;
 import org.onap.cli.fw.conf.Constants;
 import org.onap.cli.fw.conf.OnapCommandConfg;
index 471ab05..2e3e0a5 100644 (file)
@@ -20,6 +20,8 @@ import java.lang.annotation.Documented;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 
+import org.onap.cli.fw.cmd.CommandType;
+
 /**
  * Provide command name and schema file location, which is placed in the main resources folder (in classpath). It is
  * recommended to keep the name for schema, in the form of onap-[command-name]-schema.yaml, considered this format as
@@ -52,4 +54,11 @@ public @interface OnapCommandSchema {
      * @return
      */
     String schema() default "";
+
+    /**
+     * Command type
+     *
+     * @return
+     */
+    String type() default "cmd";
 }
diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/CommandType.java b/framework/src/main/java/org/onap/cli/fw/cmd/CommandType.java
new file mode 100644 (file)
index 0000000..633a8f3
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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 org.onap.cli.fw.error.OnapCommandInvalidCommandType;
+import org.onap.cli.fw.error.OnapCommandInvalidParameterType;
+
+/**
+ * Command type supported by Onap CLI.
+ *
+ */
+public enum CommandType {
+
+    AUTH,
+    CATALOG,
+    CMD;
+
+    /**
+     * Get parameter type.
+     *
+     * @param name
+     *            type name
+     * @return type
+     * @throws OnapCommandInvalidParameterType
+     *             exception
+     */
+    public static CommandType get(String name) throws OnapCommandInvalidCommandType {
+        if (AUTH.name().equalsIgnoreCase(name)) {
+            return AUTH;
+        } else if (CATALOG.name().equalsIgnoreCase(name)) {
+            return CATALOG;
+        } else if (CMD.name().equalsIgnoreCase(name)) {
+            return CMD;
+        } else {
+            throw new OnapCommandInvalidCommandType(name);
+        }
+    }
+}
index 7dfc0d4..9fccc66 100644 (file)
@@ -82,6 +82,7 @@ public class Constants {
 
     public static final String OPEN_CLI_SCHEMA_VERSION_VALUE = "1.0";
     public static final String DESCRIPTION = "description";
+    public static final String COMMAND_TYPE = "type";
     public static final String SERVICE = "service";
     public static final String PARAMETERS = "parameters";
     public static final String DEFAULT_PARAMETERS = "default_parameters";
diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidCommandType.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidCommandType.java
new file mode 100644 (file)
index 0000000..bdc2ef2
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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;
+
+/**
+ * Command type is invalid.
+ *
+ */
+public class OnapCommandInvalidCommandType extends OnapCommandException {
+
+    private static final long serialVersionUID = 2821256032317061067L;
+
+    public OnapCommandInvalidCommandType(String cmd) {
+        super("0x3003", "Command type " + cmd + " is invalid");
+
+    }
+
+}
index 12f217c..16675c2 100644 (file)
@@ -16,6 +16,8 @@
 
 package org.onap.cli.fw.utils;
 
+import org.onap.cli.fw.cmd.CommandType;
+
 public class ExternalSchema {
 
     private String schemaName;
@@ -23,6 +25,7 @@ public class ExternalSchema {
     private String cmdName;
     private String cmdVersion;
     private String version;
+    private String type = CommandType.CMD.name();
     private String http = "false";
 
     public String getSchemaName() {
@@ -77,4 +80,14 @@ public class ExternalSchema {
         return this.getHttp().equals("true");
     }
 
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+
+
 }
index 77d5017..f891fd0 100644 (file)
@@ -23,6 +23,7 @@ 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;
 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.DEAFULT_PARAMETER_HOST_URL;
@@ -108,6 +109,7 @@ 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.CommandType;
 import org.onap.cli.fw.cmd.OnapHttpCommand;
 import org.onap.cli.fw.cmd.OnapSwaggerCommand;
 import org.onap.cli.fw.conf.Constants;
@@ -138,7 +140,6 @@ 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.log.OnapCommandLogger;
 import org.onap.cli.fw.output.OnapCommandResult;
 import org.onap.cli.fw.output.OnapCommandResultAttribute;
 import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
@@ -394,6 +395,14 @@ public class OnapCommandUtils {
                     }
                     break;
 
+
+                case COMMAND_TYPE:
+                    Object type = values.get(key);
+                    if (type != null) {
+                        cmd.setType(CommandType.get(type.toString()));
+                    }
+                    break;
+
                 case SERVICE:
                     Map<String, String> serviceMap = (Map<String, String>) values.get(key);
 
@@ -1537,6 +1546,11 @@ public class OnapCommandUtils {
                         Object obj = resourceMap.get(OPEN_CLI_SCHEMA_VERSION);
                         schema.setVersion(obj.toString());
                         schema.setCmdVersion(resourceMap.get(Constants.VERSION).toString());
+
+                        if (resourceMap.get(Constants.COMMAND_TYPE) != null) {
+                            schema.setType(resourceMap.get(Constants.COMMAND_TYPE).toString());
+                        }
+
                         if (resourceMap.get(Constants.HTTP) != null) {
                             schema.setHttp("true");
                         }
index 19182ae..786f2d2 100644 (file)
@@ -29,7 +29,7 @@ cli.http.basic.common_headers.sdc.user-id.value=${onap-username}
 #cli.service.auth=aaf
 
 #schema validation
-cli.schema.top_level_params_list=open_cli_schema_version,name,version,description,service,parameters,results,http
+cli.schema.top_level_params_list=open_cli_schema_version,name,version,description,service,parameters,results,http,type
 cli.schema.top_level_mandatory_list=open_cli_schema_version
 
 cli.schema.service_params_list=name,version,auth,mode
index cc12cfc..f50fe56 100644 (file)
@@ -303,4 +303,11 @@ public class OnapCommandErrorTest {
 
         assertEquals("0xc001::Failed to load profile details, error", failed.getMessage());
     }
+
+    @Test
+    public void onapCommandTypeInvalidTest() {
+        OnapCommandInvalidCommandType failed = new OnapCommandInvalidCommandType("test");
+
+        assertEquals("0x3003::Command type test is invalid", failed.getMessage());
+    }    
 }