Add is_deafult_parm and is_default_attr 79/26679/1
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Thu, 21 Dec 2017 07:14:57 +0000 (12:44 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Thu, 21 Dec 2017 07:14:57 +0000 (12:44 +0530)
Issue-ID: CLI-66

Change-Id: Id6789ffda5d8ae93f4927564844bde36ecd88678
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
13 files changed:
framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java
framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java
framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java
framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java
framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java
framework/src/main/resources/log4j.properties
framework/src/main/resources/open-cli-schema/default_input_parameters.yaml
framework/src/main/resources/open-cli.properties
profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java
profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java
profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java
profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java
profiles/http/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml

index 09e1927..5479b51 100644 (file)
@@ -35,6 +35,8 @@ import org.onap.cli.fw.output.OnapCommandResultType;
 import org.onap.cli.fw.schema.OnapCommandSchemaLoader;
 import org.onap.cli.fw.utils.OnapCommandHelperUtils;
 import org.onap.cli.fw.utils.OnapCommandUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Oclip Command.
@@ -42,6 +44,8 @@ import org.onap.cli.fw.utils.OnapCommandUtils;
  */
 public abstract class OnapCommand {
 
+    private static Logger LOG = LoggerFactory.getLogger(OnapCommand.class);
+
     private String cmdDescription;
 
     private String cmdName;
@@ -127,7 +131,7 @@ public abstract class OnapCommand {
         this.setSchemaName(schema);
 
         List<String> errors = OnapCommandSchemaLoader.loadSchema(this, schema, true, validate);
-        errors.addAll(this.initializeProfileSchema());
+        errors.addAll(this.initializeProfileSchema(validate));
         this.isInitialzied = true;
 
         return errors;
@@ -135,7 +139,7 @@ public abstract class OnapCommand {
     /**
      * Any additional profile based such as http schema could be initialized.
      */
-    protected List<String> initializeProfileSchema() throws OnapCommandException {
+    protected List<String> initializeProfileSchema(boolean validate) throws OnapCommandException {
         return new ArrayList<>();
     }
 
@@ -162,8 +166,12 @@ public abstract class OnapCommand {
             throw new OnapCommandNotInitialized(this.getClass().getName());
         }
 
+        LOG.info("CMD: " + this.getName());
+
         Map<String, OnapCommandParameter> paramMap = this.getParametersMap();
 
+        LOG.info("INPUT: " + paramMap);
+
         // -h or --help is always higher precedence !, user can set this value to get help message
         if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_HELP).getValue())) {
             this.cmdResult.setType(OnapCommandResultType.TEXT);
@@ -208,6 +216,8 @@ public abstract class OnapCommand {
 
         this.run();
 
+        LOG.info("OUTPUT: " + this.cmdResult.getRecords());
+
         return this.cmdResult;
     }
 
index 6f911c2..3b2188f 100644 (file)
@@ -67,6 +67,7 @@ public class OnapCommandConstants {
     public static final String DEFAULT_VALUE = "default_value";
     public static final String IS_SECURED = "is_secured";
     public static final String IS_INCLUDE = "is_include";
+    public static final String IS_DEFAULT_PARAM = "is_default_param";
 
     public static final String PARAMETER_TYPE_JSON = "json";
     public static final String PARAMETER_TYPE_YAML = "yaml";
@@ -84,6 +85,7 @@ public class OnapCommandConstants {
     public static final String DEFAULT_PARAMETER_DEBUG = "debug";
     public static final String DEFAULT_PARAMETER_OUTPUT_FORMAT = "format";
     public static final String DEFAULT_PARAMETER_OUTPUT_ATTR_LONG = "long";
+
     public static final String DEFAULT_PARAMETER_OUTPUT_NO_TITLE = "no-title";
 
     //results
@@ -98,6 +100,7 @@ public class OnapCommandConstants {
     public static final String SCOPE = "scope";
     public static final String RESULT_SCOPE_SHORT = "short";
     public static final String RESULT_SCOPE_LONG = "long";
+    public static final String IS_DEFAULT_ATTR = "is_default_attr";
 
     //print
     public static final String PORTRAINT_COLUMN_NAME_PROPERTY = "property";
index 4b21ed4..00a9372 100644 (file)
@@ -98,6 +98,19 @@ public class OnapCommandParameter {
      */
     private boolean isInclude = true;
 
+    /*
+     * This param is from The default input parameters file
+     */
+    private boolean isDefaultParam = false;
+
+    public boolean isDefaultParam() {
+        return isDefaultParam;
+    }
+
+    public void setDefaultParam(boolean isDefaultParam) {
+        this.isDefaultParam = isDefaultParam;
+    }
+
     public String getName() {
         return cmdName;
     }
@@ -321,4 +334,9 @@ public class OnapCommandParameter {
             return false;
         return true;
     }
+
+    @Override
+    public String toString() {
+        return this.getName() + ": " + this.getValue();
+    }
 }
index 1208a3c..76af42d 100644 (file)
@@ -57,6 +57,19 @@ public class OnapCommandResultAttribute {
 
     private boolean isSecured = false;
 
+    /*
+     * This attr is from The default input parameters file
+     */
+    private boolean isDefaultAttr = false;
+
+    public boolean isDefaultAttr() {
+        return isDefaultAttr;
+    }
+
+    public void setDefaultAttr(boolean isDefaultAttr) {
+        this.isDefaultAttr = isDefaultAttr;
+    }
+
     public void setValues(List<String> values) {
         if (values != null) {
             this.values = values;
@@ -86,6 +99,11 @@ public class OnapCommandResultAttribute {
         return values;
     }
 
+    public void resetValues(String value) {
+        this.values.clear();
+        this.values.add(value);
+    }
+
     public OnapCommandResultAttributeScope getScope() {
         return outScope;
     }
@@ -118,4 +136,8 @@ public class OnapCommandResultAttribute {
         this.defaultValue = defaultValue;
     }
 
+    @Override
+    public String toString() {
+        return this.getName() + ": " + this.getValues();
+    }
 }
index 2859744..0c4af80 100644 (file)
@@ -36,6 +36,8 @@ import static org.onap.cli.fw.conf.OnapCommandConstants.INPUT_PARAMS_MANDATORY_L
 import static org.onap.cli.fw.conf.OnapCommandConstants.IS_INCLUDE;
 import static org.onap.cli.fw.conf.OnapCommandConstants.IS_OPTIONAL;
 import static org.onap.cli.fw.conf.OnapCommandConstants.IS_SECURED;
+import static org.onap.cli.fw.conf.OnapCommandConstants.IS_DEFAULT_ATTR;
+import static org.onap.cli.fw.conf.OnapCommandConstants.IS_DEFAULT_PARAM;
 import static org.onap.cli.fw.conf.OnapCommandConstants.LONG_OPTION;
 import static org.onap.cli.fw.conf.OnapCommandConstants.NAME;
 import static org.onap.cli.fw.conf.OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION;
@@ -384,6 +386,21 @@ public class OnapCommandSchemaLoader {
                                             param.setInclude(false);
                                         }
                                         break;
+
+                                    case IS_DEFAULT_PARAM:
+                                        if (validate) {
+                                            if (!OnapCommandUtils.validateBoolean(String.valueOf(parameter.get(key2)))) {
+                                                exceptionList.add(OnapCommandUtils.invalidBooleanValueMessage(parameter.get(NAME),
+                                                        IS_DEFAULT_PARAM, parameter.get(key2)));
+                                            }
+                                        }
+
+                                        if (BOOLEAN_TRUE.equalsIgnoreCase(String.valueOf(parameter.get(key2)))) {
+                                            param.setDefaultParam(true);
+                                        } else {
+                                            param.setDefaultParam(false);
+                                        }
+                                        break;
                                 }
                             }
 
@@ -399,14 +416,13 @@ public class OnapCommandSchemaLoader {
                 case RESULTS:
                     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();
 
                             switch (key3) {
                                 case DIRECTION:
                                     try {
-                                        result.setPrintDirection(OnapCommandPrintDirection.get((String) valueMap.get(key3)));
+                                        cmd.getResult().setPrintDirection(OnapCommandPrintDirection.get((String) valueMap.get(key3)));
                                     } catch (OnapCommandException ex) {
                                         OnapCommandUtils.throwOrCollect(ex, exceptionList, validate);
                                     }
@@ -477,15 +493,28 @@ public class OnapCommandSchemaLoader {
                                                         attr.setSecured(false);
                                                     }
                                                     break;
+
+                                                case IS_DEFAULT_ATTR:
+                                                    if (validate) {
+                                                        if (!OnapCommandUtils.validateBoolean(String.valueOf(map.get(key4)))) {
+                                                            exceptionList.add(OnapCommandUtils.invalidBooleanValueMessage(ATTRIBUTES,
+                                                                    IS_DEFAULT_ATTR, map.get(key4)));
+                                                        }
+                                                    }
+                                                    if (BOOLEAN_TRUE.equals(String.valueOf(map.get(key4)))) {
+                                                        attr.setDefaultAttr(true);
+                                                    } else {
+                                                        attr.setDefaultAttr(false);
+                                                    }
+                                                    break;
                                             }
 
                                         }
-                                        result.getRecords().add(attr);
+                                        cmd.getResult().getRecords().add(attr);
                                     }
                                     break;
                             }
                         }
-                        cmd.setResult(result);
                     }
                     break;
             }
index 419faf3..d27b1d6 100644 (file)
@@ -2,7 +2,7 @@ log4j.rootLogger=ALL, file
 
 # Redirect log messages to a log file, support file rolling.
 log4j.appender.file=org.apache.log4j.RollingFileAppender
-log4j.appender.file.File=${OPEN_CLI_HOME}/logs/oclip.log
+log4j.appender.file.File=${OPEN_CLI_HOME}/logs/open-cli.log
 log4j.appender.file.MaxFileSize=5MB
 log4j.appender.file.MaxBackupIndex=10
 log4j.appender.file.layout=org.apache.log4j.PatternLayout
index 02e8a71..44c71e5 100644 (file)
@@ -12,33 +12,39 @@ parameters:
     short_option: h
     long_option: help
     default_value: false
+    is_default_param: true
   - name: version
     type: bool
     description: print service version
     short_option: v
     long_option: version
     default_value: false
+    is_default_param: true
   - name: debug
     type: bool
     description: Enable debug output
     short_option: d
     long_option: debug
     default_value: false
+    is_default_param: true
   - name: format
     type: string
     description: Output formats, supported formats such as table, csv, json, yaml
     short_option: f
     long_option: format
     default_value: table
+    is_default_param: true
   - name: long
     type: bool
     description: whether to print all attributes or only short attributes
     short_option: s
     long_option: long
     default_value: false
+    is_default_param: true
   - name: no-title
     type: bool
     description: whether to print title or not
     short_option: t
     long_option: no-title
-    default_value: false
\ No newline at end of file
+    default_value: false
+    is_default_param: true
\ No newline at end of file
index 4971269..0e8b2af 100644 (file)
@@ -9,7 +9,7 @@ cli.schema.top_level_mandatory_list=open_cli_schema_version
 cli.schema.info_params_list=product,service,type,author,ignore
 cli.schema.info_params_mandatory_list=product,service
 
-cli.schema.input_params_list=name,description,type,short_option,long_option, is_optional,default_value,is_secured,is_include
+cli.schema.input_params_list=name,description,type,short_option,long_option, is_optional,default_value,is_secured,is_include,is_default_param
 cli.schema.input_params_mandatory_list=name,description,type
 
 cli.schema.result_params_list=name,description,scope,type,is_secured, default_value
index 4b1752a..aa1368e 100644 (file)
@@ -116,7 +116,7 @@ public class OnapCommandHttpAuthClient {
         if (cmd.getService().isModeDirect()){
             return cmd.getParametersMap().get(OnapCommandHttpConstants.DEAFULT_PARAMETER_HOST_URL).getValue().toString();
         } else { //Catalog mode
-            OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog");
+            OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog", cmd.getInfo().getProduct());
 
             Map<String, String> paramsOverrides = new HashMap<>();
             paramsOverrides.put(OnapCommandHttpConstants.CATALOG_SERVICE_NAME, cmd.getService().getName());
index 7178be8..1230934 100644 (file)
@@ -102,8 +102,8 @@ public class OnapHttpCommand extends OnapCommand {
     }
 
     @Override
-    protected List<String> initializeProfileSchema() throws OnapCommandException {
-        return OnapCommandSchemaHttpLoader.loadHttpSchema(this, this.getSchemaName(), true, false);
+    protected List<String> initializeProfileSchema(boolean validate) throws OnapCommandException {
+        return OnapCommandSchemaHttpLoader.loadHttpSchema(this, this.getSchemaName(), true, validate);
     }
 
     @Override
index 1ce0433..5b6df29 100644 (file)
@@ -65,12 +65,16 @@ import org.apache.http.protocol.HttpContext;
 import org.apache.http.util.EntityUtils;
 import org.onap.cli.fw.http.conf.OnapCommandHttpConstants;
 import org.onap.cli.fw.http.error.OnapCommandHttpFailure;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Helps to make http connection.<br>
  */
 public class OnapHttpConnection {
 
+    private static Logger LOG = LoggerFactory.getLogger(OnapHttpConnection.class);
+
     private HttpClient httpClient = null;
 
     Map<String, String> mapCommonHeaders = new HashMap<String, String> ();
@@ -331,8 +335,10 @@ public class OnapHttpConnection {
         } catch (ParseException | IOException e) {
             throw new OnapCommandHttpFailure(e);
         } finally {
+            String info = input + " " + result;
+            LOG.info(info);
             if (this.debug) {
-                this.debugDetails = input + " " + result;
+                this.debugDetails = info;
             }
         }
 
index e4d2cf0..cd91878 100644 (file)
@@ -38,6 +38,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.jayway.jsonpath.JsonPath;
+import com.jayway.jsonpath.PathNotFoundException;
 
 import net.minidev.json.JSONArray;
 
@@ -169,21 +170,26 @@ public class OnapCommandHttpUtils {
             int idxE = headerProcessedLine.indexOf("}", idxS);
             String jsonPath = headerProcessedLine.substring(idxS + 3, idxE);
             jsonPath = jsonPath.trim();
+            Object value = new Object();
             try {
                 // JSONArray or String
-                Object value = JsonPath.read(resultHttp.getBody(), jsonPath);
-                if (value instanceof JSONArray) {
-                    JSONArray arr = (JSONArray) value;
-                    if (arr.size() > maxRows) {
-                        maxRows = arr.size();
-                    }
-                }
-                bodyProcessedPattern += headerProcessedLine.substring(currentIdx, idxS) + "%s";
-                values.add(value);
-                currentIdx = idxE + 1;
+                value = JsonPath.read(resultHttp.getBody(), jsonPath);
+            } catch (PathNotFoundException e1) {
+                //set to blank for those entries which are missing from the output json
+                value = "";
             } catch (Exception e) {
                 throw new OnapCommandHttpInvalidResponseBody(jsonPath, e);
             }
+
+            if (value instanceof JSONArray) {
+                JSONArray arr = (JSONArray) value;
+                if (arr.size() > maxRows) {
+                    maxRows = arr.size();
+                }
+            }
+            bodyProcessedPattern += headerProcessedLine.substring(currentIdx, idxS) + "%s";
+            values.add(value);
+            currentIdx = idxE + 1;
         }
 
         if (bodyProcessedPattern.isEmpty()) {
index d7fbe03..9029e27 100644 (file)
@@ -13,6 +13,7 @@ parameters:
     long_option: host-username
     default_value: $s{env:OPEN_CLI_HOST_USERNAME}
     is_optional: false
+    is_default_param: true
   - name: host-password
     type: string
     description: Host user password
@@ -21,6 +22,7 @@ parameters:
     default_value: $s{env:OPEN_CLI_HOST_PASSWORD}
     is_secured: true
     is_optional: false
+    is_default_param: true
   - name: host-url
     type: url
     description: host url in http(s)
@@ -28,9 +30,11 @@ parameters:
     long_option: host-url
     is_optional: false
     default_value: $s{env:OPEN_CLI_HOST_URL}
+    is_default_param: true
   - name: no-auth
     type: bool
     description: whether to authenticate user or not
     short_option: a
     long_option: no-auth
-    default_value: false
\ No newline at end of file
+    default_value: false
+    is_default_param: true
\ No newline at end of file