Highlighted
Absent Member.. Absent Member..
Absent Member..
2260 views

uCMDB JAVA API question

Hi dear all.

 

What I am trying to do is sample, customer give me a server name and the attribute they want to query, and I need to provide a java program to do this.

Here's my class to get attribute:

package hsbc.moabucmdb;
import com.hp.ucmdb.api.UcmdbService;
import com.hp.ucmdb.api.topology.*;
import com.hp.ucmdb.api.types.TopologyCI;
import com.hp.ucmdb.api.types.TopologyRelation;
import java.util.Collection;

public class GetSingleAttr {
	public String getSingleAttr(String NodeName, String NodeType, String Attribute) throws Exception{
//Create a connection
		UcmdbService ucmdbService = CreateSDKConnection.createSDKConnection();
		
//Getting the topology servcie
		TopologyQueryService queryService = ucmdbService.getTopologyQueryService();
		
//Get the query factory
		TopologyQueryFactory queryFactory = queryService.getFactory();

//Create the query definition
		QueryDefinition queryDefinition = queryFactory.createQueryDefinition("Get Attribute by node name, node type and attribute name");

//The unique name of the query node of type "node"
		String queryNodename = "Node";

		QueryNode queryNode = queryDefinition.addNode(queryNodename).ofType(NodeType).property("name", Operator.EQUALS, NodeName).queryProperty(Attribute);
		Topology topology = queryService.executeQuery(queryDefinition);
		Collection<TopologyCI> nodes = topology.getCIsByName(NodeName);
		if (nodes.isEmpty())
		{
			System.out.println("failed");
			return "failed";
		}
		String value = "fail";
		for (TopologyCI nodeCI : nodes){
			System.out.println(NodeName +":" + Attribute + ":"+ nodeCI.getPropertyValue(Attribute));
			value = nodeCI.getPropertyValue(Attribute).toString();
		}
			return value;
	}
}

 

And the main function:

package hsbc.moabucmdb;

public class GetData {
	public static void main(String[] args) throws Exception{
		GetSingleAttr getdata = new GetSingleAttr();
		getdata.getSingleAttr("TSWDTA5-4", "unix", "discovered_os_name");
	}
}

 

So, I'm trying to get the "discovered_os_name" property of a "unix" server with name "TSWDTA5-4", and it's in uCMDB, but I always got the "failed", which should say if (nodes.isEmpty()) is true..

 

Does anyone know my problem? anyone can give me a typical code segment of query CIs by an attribute value and then output the attribute value of a CI I want, such as give a unix server name, and then query it's discovered_os_name or some other attributes??

Thanks a lot.. 

 

Tags (1)
0 Likes
4 Replies
Highlighted
Absent Member.. Absent Member..
Absent Member..

it should be Collection<TopologyCI> nodes = topology.getCIsByName(queryNodename) instead of Collection<TopologyCI> nodes = topology.getCIsByName(NodeName); (NodeName if name of the server, here should be the name of the querynode).
However, I get the attribute is NULL, which is actually "Linux" in uCMDB..
0 Likes
Highlighted
Honored Contributor.
Honored Contributor.

Put your code in Eclipse and played around with it. The code you posted works ok for me.

 

 

... 
TopologyQueryService queryService = ucmdbService.getTopologyQueryService(); TopologyQueryFactory queryFactory = queryService.getFactory(); QueryDefinition queryDefinition = queryFactory.createQueryDefinition("Get hosts"); String queryNodename = "Node"; QueryNode queryNode = queryDefinition.addNode(queryNodename).ofType(NodeType).property("name", Operator.EQUALS, NodeName).queryProperty(Attribute); Topology topology = queryService.executeQuery(queryDefinition); Collection<TopologyCI> hosts = topology.getCIsByName(queryNodename); String myAttr = "failed"; if (hosts.isEmpty()) { System.out.println("No results"); } else { for (TopologyCI host : hosts){ myAttr = (String) host.getPropertyValue(Attribute); } } return myAttr; } public static void main(String[] args) throws Exception{ GetCIAttr getdata = new GetCIAttr(); System.out.println(getdata.getSingleAttr("el1391", "unix", "discovered_os_name")); }

 Answer: "Linux". Which is correct.

 

Only difference is my use of getPropertyValue.

Can you try changing

value = nodeCI.getPropertyValue(Attribute).toString();

into

value = (String) nodeCI.getPropertyValue(Attribute);

 

 

 

 

 

Highlighted
Absent Member.. Absent Member..
Absent Member..

Hi All,

 

I want to add relations to existing CIs with java Api.Please  help me its bit urgent.

 

Warm regards,

Milap

0 Likes
Highlighted
Visitor.

@MIlap

Could You please try to access the below path in your UCMDB enviroment

Code Example : there are many

%uCMDB root Server%\deploy\ucmdb-docs\docs\eng\APIs\JavaSDK_Samples\Ucmdb_JSdk_DataInSampleExample.html

Documentation : Refer DevelopersGuide

%uCMDB root Server%\deploy\ucmdb-docs\docs\eng\pdfs

Cheers 🙂
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.