Minor issues fix 15/94515/1
authorKanagaraj Manickam <mkr1481@gmail.com>
Thu, 29 Aug 2019 10:11:53 +0000 (15:41 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Thu, 29 Aug 2019 10:11:53 +0000 (15:41 +0530)
Issue-ID: CLI-166

Change-Id: I97bd303b4e636bfc53e24732c7c6e4062037909b
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
12 files changed:
deployment/zip/src/main/release/conf/open-cli.properties
framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java
framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSample.java
framework/src/main/java/org/onap/cli/fw/error/OnapCommandWarning.java
framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java
framework/src/main/resources/open-cli.properties
grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java
main/src/main/java/org/onap/cli/main/OnapCli.java
profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java
profiles/http/src/main/java/org/onap/cli/fw/http/error/OnapCommandFailedMocoGenerate.java
profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java
validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java

index eff85b8..76048e6 100644 (file)
@@ -20,6 +20,7 @@ cli.artifact.dir=$s{env:OPEN_CLI_HOME}/data/artifacts
 cli.tmp.dir=$s{env:OPEN_CLI_HOME}/data/tmp
 #timeout in seconds
 cli.grpc.client.timeout=60
+cli.execution.search.mode=find
 
 #schema validation
 cli.schema.base.sections=open_cli_schema_version,name,description,parameters,results,info
@@ -38,7 +39,7 @@ cli.schema.boolean_values=true,false
 cli.schema.command.type=cmd,auth,catalog
 
 # moco properties
-cli.sample.gen.enable=false
+cli.sample.gen.enable=true
 cli.sample.gen.target=$s{env:OPEN_CLI_HOME}/open-cli-sample
 
 # mrkanag Move this to db, once exteranl command registration is supported in place of discovery
index 5301802..51a1862 100644 (file)
@@ -43,6 +43,7 @@ public class OnapCommandConstants {
     public static final String OPEN_CLI_DATA_DIR = "cli.data.dir";
     public static final String OPEN_CLI_ARTIFACT_DIR = "cli.artifact.dir";
     public static final String OPEN_CLI_GRPC_CLIENT_TIMEOUT = "cli.grpc.client.timeout";
+    public static final String OPEN_CLI_EXECUTION_SEARCH_MODE = "cli.execution.search.mode";
     //schema
     public static final String OPEN_CLI_SCHEMA_VERSION = "open_cli_schema_version";
     public static final String OPEN_CLI_SCHEMA_VERSION_VALUE_1_0 = "1.0";
index 32e5cf6..9f70220 100644 (file)
@@ -20,7 +20,7 @@ package org.onap.cli.fw.error;
  * Command sample is invalid.
  *
  */
-public class OnapCommandInvalidSample extends OnapCommandException {
+public class OnapCommandInvalidSample extends OnapCommandWarning {
 
     private static final long serialVersionUID = -3387652326582792835L;
 
index 3b49f46..ccd2cee 100644 (file)
@@ -31,4 +31,8 @@ public abstract class OnapCommandWarning extends OnapCommandException {
     public OnapCommandWarning(String errorCode, String errorMessage) {
         super(errorCode, errorMessage);
     }
+
+    public OnapCommandWarning(String errorCode, String errorMessage, Throwable err) {
+        super(errorCode, errorMessage, err);
+    }
 }
index d09e430..d032a99 100644 (file)
@@ -48,12 +48,21 @@ public class OnapCommandExecutionStore {
     private enum SearchMode {
         find,
         file //for developer mode
+
+
     }
+
     private static SearchMode SEARCH_MODE = SearchMode.file;
+    static {
+        String mode = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_EXECUTION_SEARCH_MODE);
+        if (mode.equalsIgnoreCase(SearchMode.find.name()))
+            SEARCH_MODE = SearchMode.find;
+    }
 
     public static class ExecutionStoreContext {
         private String requestId;
         private String executionId;
+        private String profile;
         private String storePath;
         public String getExecutionId() {
             return executionId;
@@ -76,6 +85,12 @@ public class OnapCommandExecutionStore {
             this.requestId = requestId;
              return this;
         }
+        public String getProfile() {
+            return profile;
+        }
+        public void setProfile(String profile) {
+            this.profile = profile;
+        }
     }
 
     public static class Execution {
@@ -219,8 +234,10 @@ public class OnapCommandExecutionStore {
 
             if (input != null)
                 FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "input"), input);
-            if (profile != null)
+            if (profile != null) {
+                context.setProfile(profile);
                 FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "profile"), profile);
+            }
 
             FileUtils.touch(new File(context.getStorePath() + File.separator + "stdout"));
             FileUtils.touch(new File(context.getStorePath() + File.separator + "stderr"));
@@ -371,6 +388,7 @@ public class OnapCommandExecutionStore {
         if (new File(executionStorePath + File.separator + "executionId").exists())
             exectuion.setId(FileUtils.readFileToString(new File(executionStorePath + File.separator + "executionId")));
         exectuion.setProduct(FileUtils.readFileToString(new File(executionStorePath + File.separator + "product")));
+        exectuion.setProfile(FileUtils.readFileToString(new File(executionStorePath + File.separator + "profile")));
         exectuion.setService(FileUtils.readFileToString(new File(executionStorePath + File.separator + "service")));
         exectuion.setCommand(FileUtils.readFileToString(new File(executionStorePath + File.separator + "command")));
         if (new File(executionStorePath + File.separator + "profile").exists())
index 259fd22..bd8a6ac 100644 (file)
@@ -20,7 +20,7 @@ cli.artifact.dir=./data/artifacts
 cli.tmp.dir=./data/tmp
 #timeout in seconds
 cli.grpc.client.timeout=60
-
+cli.execution.search.mode=file
 #schema validation
 cli.schema.base.sections=open_cli_schema_version,name,description,parameters,results,info
 cli.schema.base.sections.mandatory=open_cli_schema_version
index f34df89..9f32506 100644 (file)
@@ -162,18 +162,18 @@ public class OpenInterfaceGrpcServer {
                                 new ArrayList<String>());
 
                         //fill from profile
+                        Map<String, String> cache= OnapCommandRegistrar.getRegistrar().getParamCache(product);
                         for (OnapCommandParameter param: cmd.getParameters()) {
-                            Map<String, String> cache= OnapCommandRegistrar.getRegistrar().getParamCache(product);
                             if (cache.containsKey(
                                     cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption())) {
-                                param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(
+                                param.setValue(cache.get(
                                         cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption()));
                             } else if (cache.containsKey(
                                     cmd.getInfo().getService() + ":" + param.getLongOption())) {
-                                param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(
+                                param.setValue(cache.get(
                                         cmd.getInfo().getService() + ":" + param.getLongOption()));
-                            } else if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey(param.getLongOption())) {
-                                param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(param.getLongOption()));
+                            } else if (cache.containsKey(param.getLongOption())) {
+                                param.setValue(cache.get(param.getLongOption()));
                             }
                         }
                     }
@@ -261,7 +261,6 @@ public class OpenInterfaceGrpcServer {
             } catch (OnapCommandException e) {
                 logger.info(e.getMessage());
 
-
                 Builder reply = Output.newBuilder();
                 reply.putAttrs(OnapCommandConstants.RESULTS, "{}");
                 reply.setSuccess(false);
index 0c7082f..268b23c 100644 (file)
@@ -161,6 +161,10 @@ public class OnapCli {
         System.out.println(msg);
     }
 
+    protected void printerr(String msg) {
+        System.err.println(msg);
+    }
+
     private void print(Throwable throwable) {
         String error = throwable.getMessage() != null ? throwable.getMessage() : "";
         this.print(error);
@@ -349,7 +353,7 @@ public class OnapCli {
             } else {
                 resultAtt.getValues().add(OnapCommandConstants.VERIFY_RESULT_FAIL);
             }
-            this.print(testResult.getDebugInfo());
+            this.printerr(testResult.getDebugInfo());
             this.print("\n***************Expected Output: \n" + expectedOutput);
             this.print("\n***************Actual Output: \n" + actualOutput);
         }
@@ -509,20 +513,28 @@ public class OnapCli {
                 }
 
                 //refer params from profile
-                if (this.profile != null)
+                if (this.profile != null) {
+
+                    Map<String, String> paramCache = new HashMap<>();
+                    if (this.product == null)
+                        paramCache = OnapCommandRegistrar.getRegistrar().getParamCache();
+                    else
+                        paramCache = OnapCommandRegistrar.getRegistrar().getParamCache(this.product);
+
                     for (OnapCommandParameter param: cmd.getParameters()) {
-                        if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey(
+                        if (paramCache.containsKey(
                                 cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption())) {
-                            param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(
+                            param.setValue(paramCache.get(
                                     cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption()));
-                        } else if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey(
+                        } else if (paramCache.containsKey(
                                 cmd.getInfo().getService() + ":" + param.getLongOption())) {
-                            param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(
+                            param.setValue(paramCache.get(
                                     cmd.getInfo().getService() + ":" + param.getLongOption()));
-                        } else if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey(param.getLongOption())) {
-                            param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(param.getLongOption()));
+                        } else if (paramCache.containsKey(param.getLongOption())) {
+                            param.setValue(paramCache.get(param.getLongOption()));
                         }
                     }
+                }
 
                 //load the parameters value from the map read from param-file
                 if (!this.argsParamFile.isEmpty()) {
@@ -558,8 +570,17 @@ public class OnapCli {
                     this.exitFailure();
                 }
             } catch (OnapCommandWarning w) {
+                if (cmd.getExecutionContext() != null) {
+                    OnapCommandExecutionStore.getStore().storeExectutionEnd(
+                            cmd.getExecutionContext(),
+                            w.getMessage(),
+                            null,
+                            cmd.getResult().getDebugInfo(),
+                            cmd.getResult().isPassed());
+                }
+
                 this.print(w);
-                this.print(cmd.getResult().getDebugInfo());
+                this.printerr(cmd.getResult().getDebugInfo());
                 this.exitSuccessfully();
             } catch (Exception e) {
                 if (executionStoreContext != null) {
@@ -572,7 +593,7 @@ public class OnapCli {
                 }
 
                 this.print(e);
-                this.print(cmd.getResult().getDebugInfo());
+                this.printerr(cmd.getResult().getDebugInfo());
                 this.exitFailure();
             }
         }
@@ -580,7 +601,7 @@ public class OnapCli {
 
     public void handleTracking(OnapCommand cmd) throws OnapCommandException {
         if (cmd.getResult().isDebug())
-            this.print(cmd.getResult().getDebugInfo());
+            this.printerr(cmd.getResult().getDebugInfo());
 
         String printOut = cmd.getResult().print();
         this.print(printOut);
index 40ccf5c..bc86452 100644 (file)
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.apache.commons.io.FileUtils;
 import org.onap.cli.fw.cmd.OnapCommand;
 import org.onap.cli.fw.cmd.conf.OnapCommandCmdConstants;
 import org.onap.cli.fw.cmd.error.OnapCommandCmdFailure;
@@ -175,11 +176,13 @@ public class OpenCommandShellCmd extends OnapCommand {
         //add oclip specific environment variables
         if (this.getExecutionContext() != null) {
             envs.add("OPEN_CLI_REQUEST_ID=" + this.getExecutionContext().getRequestId());
+            if (this.getExecutionContext().getProfile() != null) {
+                envs.add("OPEN_CLI_PROFILE=" + this.getExecutionContext().getProfile());
+            }
             if (OnapCommandRegistrar.getRegistrar().getHost() != null) {
                 envs.add("OPEN_CLI_RPC_HOST=" + OnapCommandRegistrar.getRegistrar().getHost());
                 envs.add("OPEN_CLI_RPC_PORT=" + OnapCommandRegistrar.getRegistrar().getPort());
             }
-            //mrkanag set the profile OPEN_CLI_PROFILE
         }
 
         for (String env: this.getEnvs().keySet()) {
@@ -315,7 +318,32 @@ public class OpenCommandShellCmd extends OnapCommand {
             int idxE = line.indexOf("}", idxS);
             String tmpName = line.substring(idxS + 7, idxE);
             tmpName = tmpName.trim();
-            result.put("tmp:" + tmpName, this.getOutputAttributeFilePath(tmpName, true));
+            String tmpTkns[] = tmpName.split(":");
+            String tmpFileName;
+            String paramName;
+            if (tmpTkns.length == 2) {
+                tmpFileName = tmpTkns[0];
+                paramName = tmpTkns[1];
+            } else {
+                tmpFileName = tmpTkns[0];
+                paramName = null;
+            }
+
+            String tmpFilePath = this.getOutputAttributeFilePath(tmpFileName, true);
+            if (paramName != null) {
+                //Write the value of input params into file before passing to command
+                try {
+                    FileUtils.touch(new File(tmpFilePath));
+                    FileUtils.writeStringToFile(new File(tmpFilePath),
+                            this.getParametersMap().get(paramName).getValue().toString());
+                } catch (IOException e) {
+                    // NO SONAR
+                }
+            }
+
+            result.put("tmp:" + tmpFileName, tmpFilePath); //used in output parsing
+            result.put("tmp:" + tmpName, tmpFilePath); //used in line replacement
+
             currentIdx = idxE + 1;
         }
         return result;
index 12a645c..9d6274a 100644 (file)
 package org.onap.cli.fw.http.error;
 
 import org.onap.cli.fw.error.OnapCommandException;
+import org.onap.cli.fw.error.OnapCommandWarning;
 
 /**
  * Invalid data for generating moco json .
  *
  */
-public class OnapCommandFailedMocoGenerate extends OnapCommandException {
+public class OnapCommandFailedMocoGenerate extends OnapCommandWarning {
 
     private static final long serialVersionUID = -5386652726982792831L;
 
index 5cab0a6..d1f88df 100644 (file)
@@ -171,7 +171,7 @@ public class OnapCommandHttpUtils {
         /**
          * In case of empty response body [] or {}
          **/
-        if (resultHttp.getBody().length() <= 2) {
+        if (resultHttp.getBody() != null && resultHttp.getBody().length() <= 2) {
             return result;
         }
 
index d7999be..e49e4d4 100644 (file)
@@ -38,7 +38,7 @@ public class MockResponse {
     }
 
     public void setJson(String json) throws IOException {
-        if (!json.isEmpty()) {
+        if (json != null && !json.isEmpty()) {
             try {
                 ObjectMapper objectMapper = new ObjectMapper();
                 this.json = objectMapper.readTree(json);