In order to illustrate how to create a resource, a sample resource bundle has been created.

Sample Resource

Sample resource is an example included in the source code, with very simple functionality.

A sample resource has only one available capability, called "example" with a single "sayHello" operation. We strongly recommend to read the readme file and navigate the code to get familiar with it.

Using sample resource

When launching OpenNaas, the Sample Resource bundle is loaded, as can be seen with the list command:

OpenNaaS>list
          
     [147] [Active] [Created] [ ] [60] OpenNaaS :: Sample Resource (0.12.0.SNAPSHOT)

Having this bundle activated allows OpenNaaS to operate with resources of this type.

To create a resource, a resource descriptor is required. There is a sample resource descriptor in opennaas/utils/examples/descriptors 

<resourceDescriptor>
  <!-- Capability information -->
     <capabilityDescriptors>
                 <information><type>example</type></information>
      </capabilityDescriptors>
  <!-- Resource information. It specify type and name-->
     <information>
                 <type>sampleresource</type>
                 <name>resource1</name>
     </information>
 <properties/>
</resourceDescriptor>

 

If  a resource with the above descriptor (type=sampleresource, name=resource1) has been created and started, its example capability should be available:

OpenNaaS>example:sayhello sampleresource:resource1 Adam
[INFO] sampleresource:resource1 says : Hello Adam

 

Developing from sample resource

Sample resource can be cloned and used to create your own resource types.

Publishing Capabilities

After defining capabilities for your resource and implement them, you'd commonly want them to be exported to OpenNaaS user interfaces, that is the server console and the remote API.

To publish them in the remote API, you'll need to publish the instance of the capability as an OSGi service using a set of pre-defined options. For that, we recommend to override following lifecycle methods and introduce registration calls:

	@Override
	public void activate() throws CapabilityException {
		registerService(Activator.getContext(), CAPABILITY_TYPE, getResourceType(), getResourceName(), IIPCapability.class.getName());
		super.activate();
	}

	@Override
	public void deactivate() throws CapabilityException {
		registration.unregister();
		super.deactivate();
	}

 

To make your capabilities available through the server console, you'll need to craft you own commands and publish them as OSGi services for Karaf shell to find them. ExampleCommand will serve you as an implementation guide. For publishing them, please refer to sample resource blueprint file. To know more about this file, there is a guide to publishing OSGi services that can be found here.

Putting things together

A part from capabilities, other components must be registered as OSGi services in order to interact with OpenNaaS platform. This is the case of resource repository and bootstrapper, and also any driver you may need. All these components are registered using blueprint configuration file in the bundle they belong to. Again, check sample resource blueprint file.

Drivers will be commonly placed in a different bundle from the resource one. Please, don't forget to register them as OSGi services (a new blueprint file will do the work). Although sample resource has no drivers, you can check junos one for an example.

 

  • No labels