Capacity-Aware Placement Advice API usage examples

The following sections in this topic contain a set of possible usage examples for the Capacity-Aware Placement Advice service:

Example 1 - Evaluating placement advice within an external infrastructure

The Capacity-aware Placement Advice API can be used in connection to another service (for example, BMC Cloud Lifecycle Management) to provide placement advice on a given infrastructure; to do so, it has to lookup all entities according to the given parameters.

Once all external objects (for example, Cloud, Static Pools,..) are mapped to their corresponding BMC TrueSight Capacity Optimization entities (for example, Placement Advice scenarios), BMC TrueSight Capacity Optimization can evaluate and return placement advice information.

Given a set of virtual machines to place according to capacity, the following example describes the creation of this use case:

1. Set up the Java Client connection

// Initializing Http Client
HttpClient client = new HttpClient();
client.getParams().setParameter
("http.useragent", "My REST client");
int HttpRequestOkStatus = 200;
/*
	Example of possible outcome for statusCode:
	statusCode=200 the request has succeeded
	statusCode=400 alerts for a bad input parameter
	statusCode=403 indicates a forbidden error
	statusCode=503 alerts for an internal error in the request
	...
*/

The Java Client contains some utility methods that you can use with BMC TrueSight Capacity Optimization. Some of these methods are are:

public PostMethod getPostMethod(String methodURI){
   String request = "http://<bco_datahub_server>:<wsport>
                     /dh-services/placementAdvice/" + methodURI;
   return new PostMethod(request); 
}

public String getXmlFromFile(String fileName){
   StringBuilder xmlContent = new StringBuilder();
   String readLine;
   String file = <FILE_PATH> + fileName;
   try {
      
      //Open the file for the content of request
      BufferedReader br = 
      new BufferedReader(new FileReader(file));
      
      // read file lines
      while ((readLine = br.readLine()) != null) {
      xmlContent.append(readLine);
   } catch (IOException e) {
	// TODO: handle exception
   }
   return xmlContent.toString();
}

public void printRequestOutput
            (int statusCode, HttpMethodBase method){
   if (statusCode == HttpRequestOkStatus){
   BufferedReader br = new BufferedReader(new InputStreamReader
   (method.getResponseBodyAsStream()));
      while(((readLine = br.readLine()) != null)) {
         System.out.println(readLine);
      }
   } else {
      System.err.println("The Post method failed due to an error, 
                          HTTP Status " + statusCode);
   }
} 

2. Lookup the placement advice scenario mapping the Cloud infrastructure

Important

The default property name for the Cloud identifier is model.cloud.

In this example, the Request body type ("ScenarioIdsInput.xml") is the ScenarioIdsInput Data Access Object (DAO).

PostMethod method = getPostMethod("lookupScenario");
String inputBody = getXmlFromFile("ScenarioIdsInput.xml");
try {
   // Set request body
   method.setRequestEntity(new StringRequestEntity
                          (inputBody , "application/xml", null));
   // Make request and get status code
   int statusCode =  client.executeMethod(method);
   printRequestOutput(statusCode, method);   

} catch (Exception e) {
   // TODO: handle exception
} finally {
   method.releaseConnection();
}

3. Evaluate pool placement advice for the set of virtual machines

Returns a more suitable pool (according to capacity) for the given input. If needed, you can find target pools in this scenario to be used in placement advice request performing a scenario status request, as described in Scenario status.

In this example, the Request body type ("ComputePoolPlacementInput.xml") is the ComputePoolPlacementInput DAO.

String methodPath = "evaluatePlacementAdviceAndCommit" 
                     + "/" + <scenarioid>;
PostMethod method = getPostMethod(methodPath);
String inputBody = getXmlFromFile("ComputePoolPlacementInput.xml");
try {
   // Set request body
   method.setRequestEntity(new StringRequestEntity
          (inputBody , "application/xml", null));
   // Make request and get status code
   int statusCode =  client.executeMethod(method);
   printRequestOutput(statusCode, method);   

} catch (Exception e) {
   // TODO: handle exception
} finally {
   method.releaseConnection();
}

4. Lookup the placement advice scenario mapping pool returned by the previous call

Important

The default property name for Pool identifier is model.poolid.


In this example, the Request body type ("ScenarioIdsInput.xml") is the ScenarioIdsInput DAO, with model.poolid=<value> as the lookup property.

PostMethod method = getPostMethod("lookupScenario");
String inputBody = getXmlFromFile("ScenarioIdsInput.xml");
try {
   method.setRequestEntity(new StringRequestEntity
   (inputBody , "application/xml", null));
   // Make request and get status code
   int statusCode =  client.executeMethod(method);
   printRequestOutput(statusCode, method);   

} catch (Exception e) {
   // TODO: handle exception
} finally {
   method.releaseConnection();
}

5. Evaluate host placement advice of the virtual machine set on a set of targets

Evaluates host placement advice of the set of virtual machines on a set of targets contained in the selected scenario representing the pool. If needed, target machines (for example, hosts) of this scenario to be used in placement advice request can be found performing a scenario status request, as described in Scenario status.

In this example, the Request body type ("HostPlacementInput.xml") is the HostPlacementInput DAO.

String methodPath = 
"evaluatePlacementAdviceAndCommit" + "/" + <scenarioid>;
PostMethod method = getPostMethod(methodPath);
String inputBody = getXmlFromFile("HostPlacementInput.xml");
try {
   method.setRequestEntity(new StringRequestEntity
   // Set request body
   (inputBody , "application/xml", null));
   // Make request and get status code
   int statusCode =  client.executeMethod(method);
   printRequestOutput(statusCode, method);   

} catch (Exception e) {
   // TODO: handle exception
} finally {
   method.releaseConnection();
}

Example 2 - Consolidating an existing infrastructure

Given an existing infrastructure with a set of real targets and real candidates, you can consolidate it using the Capacity-aware Placement Advice service.

The following procedure explains how this is done:

1. Set up the Java Client connection

HttpClient client = new HttpClient(); // Initializing Http Client
client.getParams().setParameter("http.useragent", "My REST client");
int HttpRequestOkStatus = 200;
/*
	Example of possible outcome for statusCode:
	statusCode=200 the request has succeeded
	statusCode=400 alerts for a bad input parameter
	statusCode=403 indicates a forbidden error
	statusCode=503 alerts for an internal error in the request
	...
*/

The Java Client contains some utility methods that you can use with BMC TrueSight Capacity Optimization. Some of these methods are:

public PostMethod getPostMethod(String methodURI){
   String request = "http://<bco_datahub_server>:<wsport>
                     /dh-services/placementAdvice/" + methodURI;
   return new PostMethod(request); 
}

public PutMethod getPutMethod(String methodURI){
   String request = "http://<bco_datahub_server>:<wsport>
                     /dh-services/placementAdvice/" + methodURI;
   return new PuMethod(request); 
}  

public GetMethod getGetMethod (String methodURI){
   String request = "http://<bco_datahub_server>:<wsport>
                     /dh-services/placementAdvice/" + methodURI;
   return new GetMethod (request); 
}  

public String getXmlFromFile(String fileName){
   StringBuilder xmlContent = new StringBuilder();
   String readLine;
   String file = <FILE_PATH> + fileName;
   try {
      //Open the file for the content of request
      BufferedReader br = new BufferedReader
      (new FileReader(file));
      // read file lines
      while ((readLine = br.readLine()) != null) {
      xmlContent.append(readLine);
   } catch (IOException e) {
	// TODO: handle exception
   }
   return xmlContent.toString();
}

public void printRequestOutput
(int statusCode, HttpMethodBase method){
   if (statusCode == HttpRequestOkStatus){
   BufferedReader br = new BufferedReader(new InputStreamReader
   (method.getResponseBodyAsStream()));
      while(((readLine = br.readLine()) != null)) {
         System.out.println(readLine);
      }
   } else {
      System.err.println("The Post method failed due to an error, 
                          HTTP Status " + statusCode);
   }
} 

2. Create a scenario

Specify custom options if needed. Note that the properties field can be filled with values that can be utilized to retrieve the scenario for future uses (for example, infrastructure-consolidation_id=1234).

In this example, the Request body type ("ScenarioInput.xml") is the ScenarioInput DAO.

PostMethod method = getPostMethod("scenario");
String inputBody = getXmlFromFile("ScenarioInput.xml");
try {
   // Set request body
   method.setRequestEntity(new StringRequestEntity
                          (inputBody , "application/xml", null));
   // Make request and get status code
   int statusCode =  client.executeMethod(method);
   printRequestOutput(statusCode, method);   
} catch (Exception e) {
   // TODO: handle exception
} finally {
   method.releaseConnection();
}

3. Add targets to the scenario

In this example, the Request body type ("MachinesInput.xml") is the MachinesInput DAO.

// This scenarioid is the one returned by scenario creation call
String methodPath = "scenario" + "/" + <scenarioid>;
PutMethod method = getPutMethod(methodPath);
String inputBody = getXmlFromFile("MachinesInput.xml");
try {
   // Set request body
   method.setRequestEntity(new StringRequestEntity
                          (inputBody , "application/xml", null));
   // Make request and get status code
   int statusCode =  client.executeMethod(method);
   printRequestOutput(statusCode, method);   
} catch (Exception e) {
   // TODO: handle exception
} finally {
   method.releaseConnection();
}

4. Add candidates to the scenario

In this example, the Request body type ("MachinesInput.xml") is the MachinesInput DAO. This operation can be performed using the same code for adding a target.

5. Evaluate placement advice of these candidates on a specific set of the targets in the scenario

In this example, the Request body type ("HostPlacementInput.xml") is the HostPlacementInput DAO.

String methodPath = "evaluatePlacementAdviceAndCommit" + 
                     "/" + <scenarioid>;
PostMethod method = getPostMethod(methodPath);
String inputBody = getXmlFromFile("HostPlacementInput.xml");
try {
   // Set request body
   method.setRequestEntity(new StringRequestEntity
                          (inputBody , "application/xml", null));
   // Make request and get status code
   int statusCode =  client.executeMethod(method);
   printRequestOutput(statusCode, method);   
} catch (Exception e) {
   // TODO: handle exception
} finally {
   method.releaseConnection();
}

To obtain the BMC TrueSight Capacity Optimization identifiers for candidates and targets, you need to perform a scenario status request call as described in the Scenario status section:

// This scenarioid is the one returned by scenario creation call
String methodPath = "scenarioStatus" + "/" + <scenarioid>;
GetMethod method = getGetMethod(methodPath);
try {
   // Make request and get status code
   int statusCode =  client.executeMethod(method);
   printRequestOutput(statusCode, method);   
} catch (Exception e) {
   // TODO: handle exception
} finally {
   method.releaseConnection();
}

If the infrastructure does not contain any real candidate virtual machines, the load of the set of virtual machines can be simulated by creating the needed candidates as hypothetical systems. This step must be performed before Step 4.

In this example, the Request body type ("CandidateTemplateInfo.xml") is the CandidateTemplateInfo DAO.

PostMethod method = getPostMethod("candidate");
String inputBody = getXmlFromFile("CandidateTemplateInfo.xml");
try {
   // Set request body
   method.setRequestEntity(new StringRequestEntity
                          (inputBody , "application/xml", null));
   // Make request and get status code
   int statusCode =  client.executeMethod(method);
   printRequestOutput(statusCode, method);   
} catch (Exception e) {
   // TODO: handle exception
} finally {
   method.releaseConnection();
}

Related topic

Use cases for placement advice requests

Was this page helpful? Yes No Submitting... Thank you

Comments