Re: example uCMDB Java API code wanted (4752 Views)
Reply
Occasional Contributor
Jon Wilson - VUMC
Posts: 5
Registered: ‎09-24-2009
Message 1 of 15 (4,753 Views)

example uCMDB Java API code wanted

[ Edited ]

We're using uCMDB 8.03 and have been trying without much success to write Java code against the API to create some relationships. The sole documention on this is Chapter 3 of the DDM manual... there is a single not overly helpful example of creating CIs. We have no difficulty extracting info from the uCMDB using the Java API.

 

Does someone have a good example of this that they are willing to share?

 

Thanks,

Jon

 

P.S. This thread has been moved from Application Perf Mgmt (BAC / BSM) Support and News Forum to  CMS and Discovery Support and News Forum. -HP Forum Moderator

Occasional Advisor
Jody Roberts
Posts: 70
Registered: ‎03-13-2009
Message 2 of 15 (4,752 Views)

Re: example uCMDB Java API code wanted

Here is an example of creating CIs.

 

Retrieve the API Jar File
Get the ucmdb-api.jar from a BAC or UCMDB server installation. Extract the jar file from ucmdb-api.war, found in the AppServer\webapps directory on the server. Inside the war archive, ucmdb-api.jar is located in theWEB-INF\lib directory.

Compile and run your application with ucmdb-api.jar in the classpath.

 

 /*
 * IDE: NETBEANS 6.0.1
 * JAVA 6
 * UCMDB 8
 * purpose: Creating host and person CI
 * May 20 2009
 * You will need umcdb-api.jar to libraries
 */

package javaapplication1;


import com.hp.ucmdb.api.*;
import com.hp.ucmdb.api.topology.CreateMode;
import com.hp.ucmdb.api.topology.TopologyModificationData;
import com.hp.ucmdb.api.topology.TopologyUpdateFactory;
import com.hp.ucmdb.api.topology.TopologyUpdateService;
import com.hp.ucmdb.api.types.CI;

 

 

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws Exception {
        // TODO code application logic here
       final String HOST_NAME = "you ucmdb server ip";
       final int PORT = 9080; //change this to your port for UCMDB Server   

UcmdbServiceProvider provider = UcmdbServiceFactory.getServiceProvider(HOST_NAME, PORT);


//essential ...Use integration module to create an integration user using jmx-console.
//Check help on ucmdb or see document attached
final String USERNAME = "integration User"; 

final String PASSWORD = "Integration Password";

Credentials credentials = provider.createCredentials(USERNAME, PASSWORD);

ClientContext clientContext = provider.createClientContext("Test");
UcmdbService ucmdbService = provider.connect(credentials, clientContext);

       
TopologyUpdateService topologyUpdateService = ucmdbService.getTopologyUpdateService();

TopologyUpdateFactory topologyUpdateFactory = topologyUpdateService.getFactory();

TopologyModificationData topologyModificationData =

    topologyUpdateFactory.createTopologyModificationData();

 

//If you want to create a CI
/*CI host = topologyModificationData.addCI("host");

host.setPropertyValue("host_key", "test1");

CI ip = topologyModificationData.addCI("ip");

ip.setPropertyValue("ip_address", "127.0.0.10");

ip.setPropertyValue("ip_domain", "DefaultDomain");

topologyModificationData.addRelation("contained", host, ip); */


//Create a Person Record  
CI person = topologyModificationData.addCI("person");
person.setPropertyValue("identification_key", "testing");
person.setPropertyValue("identification_type", 0);
person.setPropertyValue("display_label", "testing");
person.setPropertyValue("Full_Name", "testing");
topologyUpdateService.create (topologyModificationData, CreateMode.IGNORE_EXISTING);

    }

}

Occasional Visitor
Jillika
Posts: 1
Registered: ‎06-24-2010
Message 3 of 15 (4,752 Views)

example uCMDB Java API code wanted

Hi very nice and helpful post.

Could you post some more details about creating topological queries???

 

Thank you.

 

Regular Visitor
chris3121
Posts: 5
Registered: ‎09-01-2010
Message 4 of 15 (4,752 Views)

Re: example uCMDB Java API code wanted

Could either of you please point me in the right direction to setup an environment to start coding with the webservices API?

 

I have been using this very old PDF since I cant find any new information on setting this up.

 

http://support.openview.hp.com/selfsolve/document/KM201409?searchIdentifier=-60267ec0%3a12acfceb21e%...

 

This is very outdated and uCDMB version 8.3 is using axis2 now so I can't get it setup properly.

Occasional Advisor
HuberJ
Posts: 7
Registered: ‎06-04-2009
Message 5 of 15 (4,752 Views)

Re: example uCMDB Java API code wanted

Excellent example..but....

what if the CIs already exist and you just want to relate them.... 

 

How  would you  fll the two "CI" variables "host" & "ip":

 

topologyModificationData.addRelation("contained", host, ip)

 

with a CI if you had only the CMBDIDs?

 

Regular Visitor
chris3121
Posts: 5
Registered: ‎09-01-2010
Message 6 of 15 (4,752 Views)

Re: example uCMDB Java API code wanted

How do you go about setting up the Java API to use Webservices instead of direct API Access? Because from my understanding to Run code on the API you need to run it on the server itself?

Trusted Contributor
Rutger
Posts: 167
Registered: ‎10-12-2010
Message 7 of 15 (4,752 Views)

Re: example uCMDB Java API code wanted

 

Tested the below with JDK 1.5 and 1.6 in combination with uCMDB 8.0.4. Works fine.

If you need more query examples, have a look at the API docs which are located in the same folder as the jar file. See below.


/*

  * IDE: Notepad++ :smileyhappy:

  * JAVA 5 (1.5.0_22), 6 (1.6.0_21)

  * UCMDB 8.0.4 CP7

  * purpose: listing host CIs

  * October 14th 2010

  * Change [HOSTNAME], [PORT], [USER] and [PASSWORD] to suit your environment

  * Change CLASSPATH and PATH to point to the proper JDK as well as the place where this script is located.

  * You will need uCMDB API libs from umcdb-api.jar (get it from \hp\UCMDB\UCMDBServer\api), extract to disk.

  * Save this script as ListCIs.java, compile using javac and run with java.

*/ 

 

import com.hp.ucmdb.api.*;

import com.hp.ucmdb.api.topology.*;

import com.hp.ucmdb.api.types.*;

import java.util.Collection;


public class ListCIs {

 

    public static void main(String[] args)throws Exception {

        // TODO code application logic here

       final String HOST_NAME = "[HOSTNAME]";

       final int PORT = [PORT]; //change this to your port for UCMDB Server 

  

  UcmdbServiceProvider provider = UcmdbServiceFactory.getServiceProvider(HOST_NAME, PORT);


  String USERNAME = "[USER]"; 

  final String PASSWORD = "[PASSWORD]"; 

  Credentials credentials = provider.createCredentials(USERNAME, PASSWORD); 

  ClientContext clientContext = provider.createClientContext("Test");

       UcmdbService ucmdbService = provider.connect(credentials, clientContext);

  

  TopologyQueryService queryService = ucmdbService.getTopologyQueryService();

       TopologyQueryFactory queryFactory = queryService.getFactory();

  

  QueryDefinition queryDefinition = queryFactory.createQueryDefinition("Get hosts with more than one network interface");

       String hostNodeName = "Host";

       QueryNode hostNode = queryDefinition.addNode(hostNodeName).ofType("host").queryProperty("display_label");

       QueryNode ipNode = queryDefinition.addNode("IP").ofType("ip").queryProperty("ip_address");

       hostNode.linkedTo(ipNode).withLinkOfType("contained").atLeast(2);

       Topology topology = queryService.executeQuery(queryDefinition);

       Collection myhosts = topology.getCIsByName(hostNodeName);

  for (TopologyCI myhost : topology.getCIsByName(hostNodeName)) {

           System.out.println("Host " + myhost.getPropertyValue("display_label"));

           for (TopologyRelation myrelation : myhost.getOutgoingRelations()) {

               System.out.println("  has IP " + myrelation.getEnd2CI().getPropertyValue("ip_address"));

           }

       }

    }

}

 

-----------------------------------------------------------------------------
o If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
o Click the KUDOS star on the left to say 'Thanks'
Trusted Contributor
Rutger
Posts: 167
Registered: ‎10-12-2010
Message 8 of 15 (4,752 Views)

Re: example uCMDB Java API code wanted

No, you don't need to run the code on the server. Just get a recent Java development kit (1.5+, maybe it even works with 1.4?), copy the API (ucmdb-api.jar) from the server's UCMDBServer\api folder to local disk and compile on your workstation. The API will communicate with your server using your "integration user" (can also be your admin user if you're doing this on a dev system) and talks to port 8080. If your server talks SSL you will need to build in support for that. Don't know exactly what, but I plan to find out.

 

-----------------------------------------------------------------------------
o If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
o Click the KUDOS star on the left to say 'Thanks'
Trusted Contributor
Rutger
Posts: 167
Registered: ‎10-12-2010
Message 9 of 15 (4,752 Views)

Re: example uCMDB Java API code wanted

You can query the webservices using the AXIS2 service. 

The webservices as well as the API are in the process of being documented properly.

Don't know when we can expect the docs to become GA, I'm expecting Q4 or Q1 when CP8 is delivered.

 

-----------------------------------------------------------------------------
o If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution.
o Click the KUDOS star on the left to say 'Thanks'
Occasional Visitor
bla123
Posts: 3
Registered: ‎07-31-2011
Message 10 of 15 (4,677 Views)

Re: example uCMDB Java API code wanted

Hi,

Please explain what's the difference between working with the direct java api in the client (i mean the CMDB API) or working with the webservices API? I read the documentation but in this post i can see that  client can work with the interfaces (WS ofcourse and also the java API). i'm a little confused about the difference between them and what should i work with? i have a server and many clients..

Thx!!!!

Occasional Advisor
RohitVerma
Posts: 16
Registered: ‎10-20-2011
Message 11 of 15 (4,527 Views)

Re: example uCMDB Java API code wanted

Hi,

 

can you please tell me how to import TQL into ucmdb using the JAVA api.

 

PLZ help

Occasional Visitor
PHX2010
Posts: 1
Registered: ‎09-28-2012
Message 12 of 15 (3,393 Views)

Re: example uCMDB Java API code wanted

Using API's you can do anything you like.

 

When you use web services you are restricted as you can use only the features/functionality exposed by them

 

 

 

 

Occasional Visitor
shridhar_life
Posts: 2
Registered: ‎05-15-2013
Message 13 of 15 (2,745 Views)

Re: example uCMDB Java API code wanted

Hi,
I am working on integration from uCMDB to Atrium CMDB. I want to fetch the CI's from uCMDB, and then insert into a XML. Please let me know how can I do this. Is there any API that can fetch the data without the need of TQL.

Frequent Advisor
Milap
Posts: 60
Registered: ‎02-06-2012
Message 14 of 15 (1,350 Views)

Re: example uCMDB Java API code wanted

Hi All,

 

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

 

Warm regards,

Milap

Occasional Visitor
parvezp
Posts: 1
Registered: ‎04-23-2014
Message 15 of 15 (1,039 Views)

Re: example uCMDB Java API code wanted

You can create links between CIs in the following manner

 

 

//server node
QueryNode serverNode = queryDefinition.addNode("ServerNode").ofType("node");

//disk node
QueryNode diskNode = queryDefinition.addNode("DiskNode").ofType("disk_device");

//Now define the relationships in the TQL
serverNode.linkedTo(diskNode).withLinkOfType("composition").atLeast(0);

 

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.