OneCMDB Core Java API
From OneCMDB
OneCMDB provides a number Java interfaces with no remote access. We intend to release language independent, remote access API.
Contents |
Setup
OneCMDB Core uses Spring Framework to startup and instantiate the core objects. The following xml bean definition files controls how OneCMDB will be started.
- Datasource.xml defines the back-end database to use.
- Provider.xml defines the default model to load.
- Onecmdb.xml defines the core interfaces described below.
TODO: Describe jar dependencies.
OneCmdb Overview
Context
Onecmdb.xml defines the IOneCmdbContext. The context is configured with a number of Service objects. The Service objects are used to manage model objects. The way to retrieve services from the context is done by creating a Isession where the used credentials are passed. Currently no authentication module are defined so any user/password is ok.
Services
OneCmdb services enhances OneCMDB Core functionality. Following sections describes existing services.
IModelService
Query the model, holding the Root model object.
ICcb
Controls modification of the model.
IReferenceService
Defines the references type object between model objects.
IPolicyService
Defines the policies.
ICiService
Holds the reference to the Ci Model Object.
IContainerService
[Currently not used]
Adds support for container objects.
IJobService
Adds support to handle scheduled jobs running in onecmdb.
Model - Configuration Items
Configuration Items are the central part of OneCMDB. Ci's are organizied in a hierarchical order meaning that there exists one and only one Root to all CIs.
Code Examples
Startup
// Specify the Onecmdb Spring bean definition file
String[] resources = {"onecmdb.xml"};
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(resources);
// Retreive OneCmdb Context
final IOneCmdbContext cmdb = (IOneCmdbContext) appContext.getBean("onecmdb");
// Specify who you are.
ISession session = cmdb.createSession("some_user", "mypassword");
// The Session contains references to all services.
IModelService modelsvc = (IModelService) session.getService(IModelService.class);
Finding Root Model Object
// Use the Model Service for querying the model
ModelService modelsvc = (IModelService) session.getService(IModelService.class);
// Fetch the root Model Object.
ICI root = modelsvc.getRoot();
// Fetch it's offsprings.
Set<ICI> offsprings = root.getOffsprings();
// Print it out.
for (ICi ci: offsprings) {
System.out.println(“-->ci.getDisplayName()”);
}
Query Model for Objects
// Find a Ci with a specific alias name. No expression can be used.
ICi ci = modelsvc.findCi(new Path<String>("IP"));
if (ci == null) {
System.out.println("No ci with alias name IP found!");
return;
}
// Query offsprings of the IP ci with paging.
QueryCriteria criteria = new QueryCriteria();
// Set the template id.
criteria.setOffspringOfId(ci.getId());
// Set First 10.
criteria.setFirstResult(0);
criteria.setMaxResult(10);
QueryResult<ICi> result = modelsvc.query(criteria);
// Set next page.
criteria.setFirstResult(10);
criteria.setMaxResult(10);
result = modelsvc.query(criteria);
Modifying the model
ISession session = new Setup().getSession("user", "passwd");
ICcb ccb = (ICcb) session.getService(ICcb.class);
IModelService modelSvc = (IModelService)session.getService(IModelService.class);
// Find a template ci.
ICi ci = modelSvc.findCi(new Path<String>("IP"));
// Create an Instance from a IP Template.
ICmdbTransaction tx = ccb.getTx(session);
{
ICiModifiable rootTemplate = tx.getTemplate(ci);
ICiModifiable ipTemplate = rootTemplate.createOffspring();
// NOTE: This method should be rename to setTemplate(boolean);
ipTemplate.setIsBlueprint(false);
// Set the Attribute IpAddress, is a simple attribute
ipTemplate.setDerivedAttributeValue("ipAddress",
0, SimpleTypeFactory.STRING.parseString("192.168.1.1"));
}
// All modification's are asyncronous by nature.
ITicket ticket = ccb.submitTx(tx);
// Wait for completion.
IRfcResult result = ccb.waitForTx(ticket);
// Check for result.
if (result.isRejected()) {
System.out.println("Request was rejected, cause " + result.getRejectCause());
return;
}
// Issue: The result contains no information about the newly created ICi.
// This is a problem due to the transaction can
// contain more than one modification.
System.out.println("Instance added ok.");


