cancel
Showing results for 
Search instead for 
Did you mean: 

AMD Manageability Community

Adding DASH to any application using ADS (AMD DASH Service).

Introduction to ADS:

AMD DASH Service(ADS) is a web service that allows users to remotely communicate with DASH systems. Just like DASH CLI "-jdo" option, ADS takes JSON as input and returns JSON as a response. The JSON response is structured similarly to the Redfish Schema, which has been published by the Distributed Management Task Force (DMTF).

Note: JSON (JavaScript Object Notation) is a lightweight and widely-used format for sending data between applications and services.

Prerequisites:
1. Download and install AMD DASH Service. By default it will get installed in "C:\Program Files (x86)\AMD\DASH Service\bin\". The configurations like listening port, SSL certificates are stored in "C:\Program Files (x86)\AMD\DASH Service\config\settings.json". After installation, AMD DASH Service will start that will continuously listen on default port 8080 unless specified otherwise in settings.json and waits for a JSON request to arrive.
2. Examples are illustrated with Visual Studio 2019. But any other IDE or editor can be used.
3. Refer AMD DASH Service Usage Guide, which is available in the installation folder: “C:\Program Files (x86)\AMD\DASH Service\docs”, for JSON request & response formats for supported DASH profiles.
4. This blog provides the steps for C# .NET language. The framework used in this sample application is .NET framework 4.8.1. Sample application code with ADS integration is attached.

Using ADS in Sample Application:

Create a sample C# applications using following steps:
Step 1: Open Visual Studio 2019 and under get started go to "Create a new project".starting VS.png

Step 2: Find and select Console App(.NET Framework).Selection type of application.png

Step 3: In the "configure your project" section:
              Set "Project Name" as SampleADSIntegration and under "framework" select ".NET Framework 4.7.2".name and .net framework.png

Step 4: Add the using statements to include “System.Diagnostics”, “System.IO”, "System.Net" and "System.Text.Json" as these will be referenced in the upcoming code.
Note: Since .NET Framework 4.7.2 doesn't come preinstalled with "System.Text.Json", one must add it from:
            Right click on project -> Select "Manage NuGet Packages...".
            Move to "Browse" window and search "System.Text.Json". Install this, and this will make sure all other dependencies of "System.Text.Json" are installed with it.Manage NuGet Package.pngSystem.Text.Json from NuGet Pacakages.png

Step 5: In "Program.cs" add the following classes:

 

public class JSONCommand
{
    public string h { get; set; }
    public string S { get; set; }
    public string p { get; set; }
    public int C { get; set; }
    public string u { get; set; }
    public string P { get; set; }
    public string[] Commands { get; set; }
} 

public class TargetAttributes
{
    public int DashPort { get; set; }
    public bool Discovered { get; set; }
    public string HostName { get; set; }
}

public class deserializeDiscovery
{
    public List<TargetAttributes> Members { get; set; }
    public string Name { get; set; }
}

 

Step 6: Add a constant member ADSURL in “Program.cs” file inside Program class.

 

private static readonly string ADSURL = "https://localhost:8080/";

 

Note: Replace this URL in-case ADS is running on other system.

Step 7: Define a function POST that will use https to send JSON POST request to ADS.

 

public static string Post(string Uri, string Method, string ContentType, string Data)
{
    string response = "";

    // Ignore Certificates
    ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, 
    sslPolicyErrors) => true;

    // Create HTTP request
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(Uri);
    httpWebRequest.Method = Method;
    httpWebRequest.ContentType = ContentType;

    // Write POST Data
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        streamWriter.Write(Data);
    }

    // Read Response
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        response = streamReader.ReadToEnd();
    }
    return response;
}

 

Step 8: Define printDeserializedJson function that will deserialize the JSON response from ADS and print the respective result.

 

public static void printDeserializedJSON(string JSON)
{
    Console.WriteLine("\n\nDiscovery Command deserialized JSON:");
    deserializeDiscovery instance = JsonSerializer.Deserialize<deserializeDiscovery>(JSON);
    foreach (var member in instance.Members)
    {
        Console.WriteLine($"DASH Port: {instance.Members[0].DashPort}");
        Console.WriteLine($"Discovered: {instance.Members[0].Discovered}");
        Console.WriteLine($"Host Name: {instance.Members[0].HostName}");
        Console.WriteLine();
    }
    Console.WriteLine($"Collection Name: {instance.Name}\n\n");
}

 

Step 9: Define CompleteADSRequest function that will complete a whole cycle of serializing into JSON, sending JSON request, getting back the JSON response and deserializing it.

 

public static void CompleteADSRequest(JSONCommand Input)
{
    // Serialize request
    string JSONString = JsonSerializer.Serialize(Input);
    Console.Write(JSONString);
    Console.WriteLine();

    // POST to ADS and Receive Response.
    string ADSJSONOutput = Post(ADSURL, "POST", "application/json", JSONString);

    // Dump JSON output.
    Console.Write(ADSJSONOutput);
    Console.WriteLine();
    printDeserializedJSON(ADSJSONOutput);
    Console.ReadKey();
}

 

Step 10: Inside main function:

i. Create an instance DiscoveryInput of JSONCommand class

 

// Discovery Input
JSONCommand DiscoveryInput = new JSONCommand
{
    h = "10.138.89.41",
    S = "http",
    p = "623",
    Commands = new[] { "discover" }
};

 

ii. Call CompleteADSRequest and pass this JSONCommand class object as input.

 

CompleteADSRequest(DiscoveryInput);

 

Step 11: After adding all the lines of code your complete code should look like the following in "Program.cs":

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.Json;

namespace SampleADSIntegration
{
    public class JSONCommand
    {
        public string h { get; set; }
        public string S { get; set; }
        public string p { get; set; }
        public int C { get; set; }
        public string u { get; set; }
        public string P { get; set; }
        public string[] Commands { get; set; }
    }

    public class TargetAttributes
    {
        public int DashPort { get; set; }
        public bool Discovered { get; set; }
        public string HostName { get; set; }
    }

    public class deserializeDiscovery
    {
        public List<TargetAttributes> Members { get; set; }
        public string Name { get; set; }
    }


    class Program
    {
        private static readonly string ADSURL = "https://10.138.142.24:8080/";


        public static string Post(string Uri, string Method, string ContentType, string Data)
        {
            string response = "";

            // Ignore Certificates
            ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain,
            sslPolicyErrors) => true;

            // Create HTTP request
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(Uri);
            httpWebRequest.Method = Method;
            httpWebRequest.ContentType = ContentType;

            // Write POST Data
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(Data);
            }

            // Read Response
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                response = streamReader.ReadToEnd();
            }
            return response;
        }


        public static void printDeserializedJSON(string JSON)
        {
            Console.WriteLine("\n\nDiscovery Command deserialized JSON:");
            deserializeDiscovery instance = JsonSerializer.Deserialize<deserializeDiscovery>(JSON);
            foreach (var member in instance.Members)
            {
                Console.WriteLine($"DASH Port: {instance.Members[0].DashPort}");
                Console.WriteLine($"Discovered: {instance.Members[0].Discovered}");
                Console.WriteLine($"Host Name: {instance.Members[0].HostName}");
                Console.WriteLine();
            }
            Console.WriteLine($"Collection Name: {instance.Name}\n\n");
        }


        public static void CompleteADSRequest(JSONCommand Input)
        {
            // Serialize request
            string JSONString = JsonSerializer.Serialize(Input);
            Console.Write(JSONString);
            Console.WriteLine();

            // POST to ADS and Receive Response.
            string ADSJSONOutput = Post(ADSURL, "POST", "application/json", JSONString);

            // Dump JSON output.
            Console.Write(ADSJSONOutput);
            Console.WriteLine();
            printDeserializedJSON(ADSJSONOutput);
            Console.ReadKey();
        }

        static void Main(string[] args)
        {

            // Discovery Input
            JSONCommand DiscoveryInput = new JSONCommand
            {
                h = "10.138.143.54",
                S = "http",
                p = "623",
                Commands = new[] { "discover" }
            };

            CompleteADSRequest(DiscoveryInput);
        }
    }
}

 

Step 12: Build the project.
OutputWindow.png

Step 13: Run the executable SampleADSIntegration.exe from console. This executable will be placed inside the path visible in the output window.

SampleADSIntegration Console launch.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 ‘AMD DASH Service Usage Guide’ for JSON format for other supported DASH profiles.

References:

1. Refer AMD DASH Service Usage Guide from C:\Program Files (x86)\AMD\DASH Service\docs.

Useful links:

 

0 Replies