cancel
Showing results for 
Search instead for 
Did you mean: 

Adding DASH to any application

Ganesh_Kamath
6 0 5,728

DASH CLI 2.1 supports new options to enable developers to send request query in JSON format to DASH systems and get the output in JSON format. The output can be interpreted by any programming language supporting JSON format. The output JSON is modeled similar to Redfish Schema published by DMTF.

 

In typical usage, DASH CLI is launched with request JSON. DASH CLI processes the request and forms the response in JSON format. This response is sent back to the launching application.

 

JSON is supported by almost all modern programming languages. Hence it will be easier to add DASH capability into any application or tool with few simple steps. Since the application will be launching DASH CLI, the integration with the application is therefore ‘loosely coupled’. DASH communication, DASH standards compliance, DASH security & stability are encapsulated within DASH CLI. The calling application doesn’t have to know anything about DASH standard.

 

There are two new options in DASH CLI:

  • jdo option: With this flag, DASH request input is taken from command-line and output DASH response is written to console.
  • ji/jo option: With this flag, DASH request input is taken from file and output DASH response is written file.

jdo option is faster since it doesn’t involve any file operation.

 

Before starting

  • Download and install DASH CLI 2.1 version. In this blog, DASH CLI is installed in default path, “C:\Program Files (x86)\DASH CLI 2.1\”
  • Examples are illustrated with Visual Studio 2017. But any other IDE or editor can be used.
  • Refer DASH CLI Developer Guide, which is available in DASH CLI installation folder: “C:\Program Files (x86)\DASH CLI 2.1\docs\”, for JSON request & response formats for supported DASH profiles.
  • This blog provides the steps for C# .NET language. Sample code for using -jdo, -ji & -jo options is attached.

JDO Flag usage

Using via direct command line

     dashcli -jdo

When this command is run, DASH CLI waits for input in JSON format. Once the input is provided, it will be executed by DASH CLI and output of which is written back in JSON format, as shown in the screenshot below.

JDO Input Masked.png

Using JDO programmatically

Create a sample C# application by following the steps below:

 

Step 1: Create a new project in Visual Studio by selecting from the Menu “File” > “New” > “Project”

 image001.png

Step 2: Select “Visual C#” -> “Console App (.net Framework)”. Name the application “SampleJDO” and click “OK” button.

 image002.png

 

Step 3: Add the two constant members dashCliDirectory and dashCliPath in “Program.cs” file.

        private static readonly string dashCliDirectory = @"C:\Program Files (x86)\DASH CLI 2.1\bin\";

        private static readonly string dashCliPath = dashCliDirectory + @"dashcli.exe";

 

Step 4: Add the using statements to include “System.Diagnostics” and “System.IO” as these will be referenced in the upcoming code.

 

using System.Diagnostics;

using System.IO;

 

Step 5: Define the functions “JdoExample” and “RunDashCli” as shown below:

 

        static void JdoExample() {

            string memStdIn, memStdOut = String.Empty, memStdErr = String.Empty;

            string inputJsonFile = @"input_json.txt";

            memStdIn = Convert.ToString(File.ReadAllText(inputJsonFile));

            Console.WriteLine("Input: ");

            Console.WriteLine(memStdIn);

            string arguments = @"-jdo";

            int returnCode = RunDASHCli(arguments, memStdIn, out memStdOut, out memStdErr);

            Console.WriteLine("Output: ");

            Console.Write(memStdOut);

            //'memStdout' has the result in JSON format

        }

 

        static int RunDASHCli(string arguments, string memStdIn, out string memStdOut, out string memStdErr) {

            memStdOut = String.Empty;

            memStdErr = String.Empty;

 

            int exitCode = 1;

            Process process = new Process();

            StreamWriter streamWriter = null;

            StreamReader streamOutReader = null;

            StreamReader streamErrReader = null;

 

            process.StartInfo.UseShellExecute = false;

            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

            process.StartInfo.WorkingDirectory = dashCliDirectory;

            process.StartInfo.RedirectStandardInput = true;

            process.StartInfo.RedirectStandardOutput = true;

            process.StartInfo.RedirectStandardError = true;

            process.StartInfo.FileName = dashCliPath;

            process.StartInfo.Arguments = arguments;

            try {

                process.Start();

                streamOutReader = process.StandardOutput;

                streamWriter = process.StandardInput;

                streamErrReader = process.StandardError;

                streamWriter = process.StandardInput;

                streamWriter.WriteLine(memStdIn);

                streamWriter.Close();

                memStdOut = streamOutReader.ReadToEnd();

                memStdErr = streamErrReader.ReadToEnd();

                process.WaitForExit();

                exitCode = process.ExitCode;

                streamOutReader.Close();

                streamErrReader.Close();

            } catch (Exception) {

                exitCode = -1;

            } finally {

                process.Close();

            }

            return exitCode;

        }

 

Step 6: Call “JdoExample” function in the “Main” function and surround the call with messages to indicate start and stop of call like so:

 

            Console.WriteLine("Trying JdoExample ...");

            JdoExample();

            Console.WriteLine("JdoExample done.");

 

The entire code at this stage should look like so:

 

using System;

using System.Diagnostics;

using System.IO;

 

namespace SampleJDO {

    class Program {

        private static readonly string dashCliDirectory = @"C:\Program Files (x86)\DASH CLI 2.1\bin\";

        private static readonly string dashCliPath = dashCliDirectory + @"dashcli.exe";

        static void Main(string[] args) {

            Console.WriteLine("Trying JdoExample ...");

            JdoExample();

            Console.WriteLine("JdoExample done.");

        }

        static void JdoExample() {

            string memStdIn, memStdOut = String.Empty, memStdErr = String.Empty;

            string inputJsonFile = @"input_json.txt";

            memStdIn = Convert.ToString(File.ReadAllText(inputJsonFile));

            Console.WriteLine("Input: ");

            Console.WriteLine(memStdIn);

            string arguments = @"-jdo";

            int returnCode = RunDASHCli(arguments, memStdIn, out memStdOut, out memStdErr);

            Console.WriteLine("Output: ");

            Console.Write(memStdOut);

            //'memStdout' has the result in JSON format

        }

 

        static int RunDASHCli(string arguments, string memStdIn, out string memStdOut, out string memStdErr) {

            memStdOut = String.Empty;

            memStdErr = String.Empty;

 

            int exitCode = 1;

            Process process = new Process();

            StreamWriter streamWriter = null;

            StreamReader streamOutReader = null;

            StreamReader streamErrReader = null;

 

            process.StartInfo.UseShellExecute = false;

            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

            process.StartInfo.WorkingDirectory = dashCliDirectory;

            process.StartInfo.RedirectStandardInput = true;

            process.StartInfo.RedirectStandardOutput = true;

            process.StartInfo.RedirectStandardError = true;

            process.StartInfo.FileName = dashCliPath;

            process.StartInfo.Arguments = arguments;

            try {

                process.Start();

                streamOutReader = process.StandardOutput;

                streamWriter = process.StandardInput;

                streamErrReader = process.StandardError;

                streamWriter = process.StandardInput;

                streamWriter.WriteLine(memStdIn);

                streamWriter.Close();

                memStdOut = streamOutReader.ReadToEnd();

                memStdErr = streamErrReader.ReadToEnd();

                process.WaitForExit();

                exitCode = process.ExitCode;

                streamOutReader.Close();

                streamErrReader.Close();

            } catch (Exception) {

                exitCode = -1;

            } finally {

                process.Close();

            }

            return exitCode;

        }

    }

}

 

Step 7: In step 5, the added function “JdoExample”, has defined the input JSON File as input_json.txt. This declaration assumes that input_json.txt is in the same path as the location of the executable. As the default configuration in Visual studio points to Debug folder, please navigate to the debug folder. Upon building the project, the Output window gives the location of the Debug folder.

 image003.png

Step 8: Put the following JSON test in input_json.txt file:

 

     {"h":"hp705g4-3","u":"admin","P":"adminPass","Commands":["discover"]}

 

Step 9: Run the SampleJDO.exe application form the console.

 image004.png

Ji JO Flag usage

Using via direct command line

     dashcli -ji input_json.txt -jo output_json.txt

Here, the file ‘input_json.txt’ has the DASH command in JSON format. DASH CLI executes this command and writes the output in JSON format to file specified by -jo option, which is output_json.txt. Usage is shown in the screenshot below.

JIJO Input Masked.png

Using JI/JO programmatically

Create a sample C# application by following the steps below:

 

Step 1: Create a new project in Visual Studio by selecting from the Menu “File” > “New” > “Project”

 image001.png

Step 2: Select “Visual C#” -> “Console App (.net Framework)”. Name the application “SampleJiJO” and click “OK” button.

 image005.png

 

Step 3: Add the two constant members dashCliDirectory and dashCliPath in “Program.cs” file.

        private static readonly string dashCliDirectory = @"C:\Program Files (x86)\DASH CLI 2.1\bin\";

        private static readonly string dashCliPath = dashCliDirectory + @"dashcli.exe";

 

Step 4: Add the using statements to include “System.Diagnostics” and “System.IO” as these will be referenced in the upcoming code.

 

using System.Diagnostics;

using System.IO;

 

Step 5: Define the functions “JiJoExample” and “RunDashCli” as shown below:

 

        static void JiJoExample() {

            string AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\";

            string inputJsonFile = AppPath + @"input_json.txt";

            string outputJsonFile = AppPath + @"output_json.txt";

            Console.WriteLine("Input File: " + inputJsonFile);

            Console.WriteLine("Output File: " + outputJsonFile);

            string arguments = String.Format("-ji {0} -jo {1}", inputJsonFile, outputJsonFile);

            int returnCode = RunDASHCli(arguments);

            //'outputJsonFile' has the result in JSON format

        }

 

        static int RunDASHCli(string arguments, string memStdIn, out string memStdOut, out string memStdErr) {

            memStdOut = String.Empty;

            memStdErr = String.Empty;

 

            int exitCode = 1;

            Process process = new Process();

            StreamWriter streamWriter = null;

            StreamReader streamOutReader = null;

            StreamReader streamErrReader = null;

 

            process.StartInfo.UseShellExecute = false;

            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

            process.StartInfo.WorkingDirectory = dashCliDirectory;

            process.StartInfo.RedirectStandardInput = true;

            process.StartInfo.RedirectStandardOutput = true;

            process.StartInfo.RedirectStandardError = true;

            process.StartInfo.FileName = dashCliPath;

            process.StartInfo.Arguments = arguments;

            try {

                process.Start();

                streamOutReader = process.StandardOutput;

                streamWriter = process.StandardInput;

                streamErrReader = process.StandardError;

                streamWriter = process.StandardInput;

                streamWriter.WriteLine(memStdIn);

                streamWriter.Close();

                memStdOut = streamOutReader.ReadToEnd();

                memStdErr = streamErrReader.ReadToEnd();

                process.WaitForExit();

                exitCode = process.ExitCode;

                streamOutReader.Close();

                streamErrReader.Close();

            } catch (Exception) {

                exitCode = -1;

            } finally {

                process.Close();

            }

            return exitCode;

        }

 

Step 6: Call “JiJoExample” function in the “Main” function and surround the call with messages to indicate start and stop of call like so:

            Console.WriteLine("Trying JiJoExample ...");

            JiJoExample();

            Console.WriteLine("JiJoExample done.");

 

The entire code at this stage should look like so:

 

using System;

using System.Diagnostics;

using System.IO;

 

namespace SampleJiJO {

    class Program {

        private static readonly string dashCliDirectory = @"C:\Program Files (x86)\DASH CLI 2.1\bin\";

        private static readonly string dashCliPath = dashCliDirectory + @"dashcli.exe";

        static void Main(string[] args) {

            Console.WriteLine("Trying JiJoExample ...");

            JiJoExample();

            Console.WriteLine("JiJoExample done.");

        }

        static void JiJoExample() {

            string AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\";

            string inputJsonFile = AppPath + @"input_json.txt";

            string outputJsonFile = AppPath + @"output_json.txt";

            Console.WriteLine("Input File: " + inputJsonFile);

            Console.WriteLine("Output File: " + outputJsonFile);

            string arguments = String.Format("-ji {0} -jo {1}", inputJsonFile, outputJsonFile);

            int returnCode = RunDASHCli(arguments);

            //'outputJsonFile' has the result in JSON format

        }

        static int RunDASHCli(string arguments) {

            string Out, Err;

            return RunDASHCli(arguments, string.Empty, out Out, out Err);

        }

 

        static int RunDASHCli(string arguments, string memStdIn, out string memStdOut, out string memStdErr) {

            memStdOut = String.Empty;

            memStdErr = String.Empty;

 

            int exitCode = 1;

            Process process = new Process();

            StreamWriter streamWriter = null;

            StreamReader streamOutReader = null;

            StreamReader streamErrReader = null;

 

            process.StartInfo.UseShellExecute = false;

            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

            process.StartInfo.WorkingDirectory = dashCliDirectory;

            process.StartInfo.RedirectStandardInput = true;

            process.StartInfo.RedirectStandardOutput = true;

            process.StartInfo.RedirectStandardError = true;

            process.StartInfo.FileName = dashCliPath;

            process.StartInfo.Arguments = arguments;

            try {

                bool processStarted = process.Start();

                streamOutReader = process.StandardOutput;

                streamWriter = process.StandardInput;

                streamErrReader = process.StandardError;

                streamWriter = process.StandardInput;

                streamWriter.WriteLine(memStdIn);

                streamWriter.Close();

                memStdOut = streamOutReader.ReadToEnd();

                memStdErr = streamErrReader.ReadToEnd();

                process.WaitForExit();

                exitCode = process.ExitCode;

                streamOutReader.Close();

                streamErrReader.Close();

            } catch (Exception) {

                exitCode = -1;

            } finally {

                process.Close();

            }

            return exitCode;

        }

    }

}

 

Step 7: In step 5, the added function “JiJoExample”, has defined the input JSON File as input_json.txt and output JSON File as output_json.txt. This declaration assumes that input_json.txt is in the same path as the location of the executable. As the default configuration in Visual studio points to Debug folder, please navigate to the debug folder. Upon building the project, the Output window gives the location of the Debug folder.

 image006.png

Step 8: Put the following JSON test in input_json.txt file:

 

     {"h":"hp705g4-3","u":"admin","P":"adminPass","Commands":["discover"]}

 

Step 9: Run the “SampleJiJO.exe” application form the console.

 image007.png

Step 10: Check the content of the “output_json.txt” file using “type” command on the console

 image008.png

In this blog, DASH Discovery is illustrated. Similarly, other DASH profiles can be accessed by the application by framing the required JSON request. See the ‘DASH CLI Developer Guide’ for JSON format for other supported DASH profiles.

Attachments:

  1. SampleJDO solution
  2. SampleJIJO solution

 

For any further query, drop a note below or contact via mail dashsupport@amd.com

 

Reference:

  • DASH CLI Developer Guide (Available in DASH CLI installation folder: “C:\Program Files (x86)\DASH CLI 2.1\docs\”)

 

Useful links:

Related Blogs:

About the Author
Providing solutions to DMTF DASH client manageability solutions