cancel
Showing results for 
Search instead for 
Did you mean: 

Power State Change using AMA (AMD Manageability API) HOWTO

Bhaskar_Tripathi
0 0 2,266

Introduction to AMA:
AMD Manageability API (AMA) is a RESTful web service that allows users to remotely communicate with DASH systems. AMA takes JSON as input and returns JSON as a response.

NOTE: This blog provides an illustration with power state change command.

Prerequisites:
1. Download and install AMD Manageability API from www.amd.com/DASH .
2. Examples are illustrated with Visual Studio 2019. But any other IDE or editor can be used. Basic C# programming experience is required as this blog provides the steps for C# .NET language. The framework used in this sample application is .NET framework 4.7.2.

NOTE: Refer AMD Manageability API Usage Guide, which is available in the installation folder: “C:\Program Files (x86)\AMD\Manageability API\docs”, for JSON request & response formats for supported DASH profiles.

Creating a Visual Studio sample application:

Step 1: Follow Steps 1-4 from AMA discovery blog.

Adding AMA Request/Response classes:

Step 2: In "Program.cs" add the following classes in “PowerStateChangeUsingAMA” namespace(Rename namespace to PowerStateChangeUsingAMA):

 

// This class is for setting the input JSON.
public class JSONCommand
{
    public string h { get; set; } // Hostname 
    public string S { get; set; } // Scheme [http | https]
    public string p { get; set; } // port
    public int C { get; set; } // 1 to ignore certificate or trust selfsigned certificates otherwise 0.
    public string u { get; set; } // DASH username
    public string P { get; set; } // DASH password
    public string t { get; set; } // Target profile instance: For which instance you are running the command.
    public string[] Commands { get; set; } // commands like discover, enumerate, power on, power off etc.
}

// This class stores the response sent by DASH target for power state change command.
public class PowerStateChangeResponse
{
    public string ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
}

 

Step 3: Add a string member variable AMAURL in “Program.cs” file in Program class inside main function.

 

// AMD Manageability Service URI.
string AMAURL = "https://localhost:8080/";

 

Note: Replace this URL in-case AMA is running on other system or/and other port and ensure firewall is open.

Step 4: Define a function Post that will use https to send JSON POST request to AMA. This is to be defined under Program class in Program.cs file.

 

// The function posts request to AMA. 
public static string Post(string Uri, string Method, string ContentType, string Data)
{
    string response = "";

    // Ignore Certificates on AMA server
    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;
} 

 

Using AMA Power state change classes:
Step 5:
 Inside main function:

    i.  Create an instance PowerStateChangeRequest of JSONCommand class and serialize it to form JSONRequest string.

 

// Step 1: Create request JSON.
// Instantiate PowerStateChangeRequest and fill the properties.
JSONCommand PowerStateChangeRequest = new JSONCommand
{
    h = "dash-system",
    S = "https",
    p = "664",
    C = 1,
    u = "username",
    P = "password",
    t = "computersystem[0]",
    Commands = new[] { "power", "5" }
};

// Serialize PowerStateChangeRequest class instance
string JSONRequest = JsonSerializer.Serialize(PowerStateChangeRequest);
Console.WriteLine(JSONRequest);
​

 

    ii. Send POST request to AMA and get JSONResponse string.

 

// Step 2: Connect to AMA Service and post the JSONRequest and receive response.
string JSONResponse = Post(AMAURL, "POST", "application/json", JSONRequest);

// Print JSON output.
Console.WriteLine(JSONResponse)

 

    iii. Deserialize JSONResponse into PowerStateChangeResponse class to extract values.

 

// Step 3: Deserialize JSONResponse into PowerStateChangeResponse class to extract values.
PowerStateChangeResponse response = JsonSerializer.Deserialize<PowerStateChangeResponse>(JSONResponse);

Console.WriteLine($"Error Code: {response.ErrorCode}");
Console.WriteLine($"Error Message: {response.ErrorMessage}");

Console.ReadKey();​

 

Step 6: Build the project.

Untitled.png

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

result.png

In the above console window, a DASH command was sent to AMA in JSON format (first line). Subsequent lines show the JSON response which was received from AMA. The last two lines displays the deserialized output of the received JSON response.
The command was sent to perform a power state change operation on DASH system “10.138.141.122” using https and port 664. “C” option is for ignoring certificates.
The JSON response shows the “Error Code” which can be either 0 or -1, “Error Message” which explains the command result.

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

 

// DASHCLI equivalent command:
// dashcli -h dash-system -p 664 -S https -u username -P password -C -t computersystem[0] power <<options>>
// <<options>> : [ on | off | cycle | restart | reset | offgraceful ]
// Code for each option: [ 2 | 8 | 5 | 14 | 10 | 12 ] Please refer DASH CLI Developer guide for more codes.
// Below code implements the above DASHCLI command through AMA Service. 

// Step 1: Create request JSON.
// Step 2: Connect to AMA Service and post the request JSON.
// Step 3: Receive response JSON from AMA, and deserialize to extract values.

// NOTE: This file uses System.Text.Json for serialization and deserialization of JSON. Make sure you add this package by browsing NuGet packages. 
// Steps : Right click the project -> "Manage NuGet Packages..." -> Browse tab -> search and install System.Text.Json.

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

namespace PowerStateChangeUsingAMA
{
    // This class is for setting the input JSON.
    public class JSONCommand
    {
        public string h { get; set; } // Hostname 
        public string S { get; set; } // Scheme [http | https]
        public string p { get; set; } // port
        public int C { get; set; } // 1 to ignore certificate or trust selfsigned certificates otherwise 0.
        public string u { get; set; } // DASH username
        public string P { get; set; } // DASH password
        public string t { get; set; } // Target profile instance: For which instance you are running the command.
        public string[] Commands { get; set; } // commands like discover, enumerate, power on, power off etc.
    }

    // This class stores the response sent by DASH target for power state change command.
    public class PowerStateChangeResponse
    {
        public int ErrorCode { get; set; }
        public string ErrorMessage { get; set; }
    }
    class Program
    {
        // The function posts request to AMA. 
        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;
        }
        static void Main(string[] args)
        {
            // AMD Manageability Service URI.
            string AMAURL = "https://localhost:8080/";

            // Step 1: Create request JSON.
            // Instantiate PowerStateChangeRequest and fill the properties.
            JSONCommand PowerStateChangeRequest = new JSONCommand
            {
                h = "dash-system",
                S = "https",
                p = "664",
                C = 1,
                u = "username",
                P = "password",
                t = "computersystem[0]",
                Commands = new[] { "power", "5" }
            };

            // Serialize PowerStateChangeRequest class instance
            string JSONRequest = JsonSerializer.Serialize(PowerStateChangeRequest);
            Console.WriteLine(JSONRequest);

            // Step 2: Connect to AMA Service and post the JSONRequest and receive response.
            string JSONResponse = Post(AMAURL, "POST", "application/json", JSONRequest);

            // Print JSON output.
            Console.WriteLine(JSONResponse);

            // Step 3: Deserialize JSONResponse into PowerStateChangeResponse class to extract values.
            PowerStateChangeResponse response = JsonSerializer.Deserialize<PowerStateChangeResponse>(JSONResponse);

            Console.WriteLine($"Error Code: {response.ErrorCode}");
            Console.WriteLine($"Error Message: {response.ErrorMessage}");

            Console.ReadKey();
        }
    }
}

 

In this blog, DASH Power state change command is illustrated. Similarly, other DASH profiles can be accessed by the application by framing the required JSON request. Please refer ‘AMD Manageability API Usage Guide’ to get JSON format for other supported DASH profiles.

About the Author
From AMD client Manageability team we develop DMTF DASH solutions. These solutions can be used by IT Admins to manage enterprise systems.