Saturday, February 28, 2015

Apache Camel in the IoT world: Eclipse Kura component

Eclipse Kura is an OSGi based framework dedicated for the M2M gateways based on the small computing platforms like Raspberry Pi or BeagleBoard Black. If you consider using Kura in your M2M gateway and and the same time you would like to take advantage of the rich set of Apache Camel components and its EIP capabilities, then Camel Kura component is something for you. Camel Kura component will be available starting from the Camel 2.15.0.

Camel Kura architecture

The common reason to deploy Camel routes into the Eclipse Kura is to provide enterprise integration patterns and Camel components to the messaging M2M gateway. For example you might want to install Kura on Raspberry PI, then read temperature from the sensor attached to that Raspberry PI using Kura services and finally forward the current temperature value to your data center service using Camel EIP and components. The diagram below demonstrates the architecture of the Camel solution deployed into the Eclipse Kura: 

How can I activate my route?

Bundles deployed to the Eclipse Kura are usually developed as bundle activators. So the easiest way to deploy Apache Camel routes into the Kura is to create an OSGi bundle containing the class extending org.apache.camel.kura.KuraRouterclass:


public class MyKuraRouter extends KuraRouter {
  @Override
  public void configure() throws Exception {
    from("timer:trigger").
      to("netty-http:http://app.mydatacenter.com/api");
  }
}

Keep in mind that KuraRouter implements the org.osgi.framework.BundleActivator interface, so you need to register its start and stop lifecycle methods while creating Kura bundle component class.
Kura router starts its own OSGi-aware CamelContext. It means that for every class extending KuraRouter, there will be a dedicated CamelContext instance. Ideally we recommend to deploy one KuraRouter per OSGi bundle.

How can I deploy my KuraRouter

Bundle containing your Kura router class should import the following packages in the OSGi manifest:

Import-Package: org.osgi.framework;version="1.3.0",
  org.slf4j;version="1.6.4",
  org.apache.camel,org.apache.camel.impl,org.apache.camel.core.osgi,org.apache.camel.builder,org.apache.camel.model,
  org.apache.camel.component.kura

Keep in mind that you don't have to import every Camel component bundle you plan to use in your routes, as Camel components are resolved as the services on the runtime level.
Before you deploy your router bundle, be sure that you have deployed (and started) the following Camel core bundles (using Kura GoGo shell)...

install file:///home/user/.m2/repository/org/apache/camel/camel-core/2.15.0/camel-core-2.15.0.jar
start <camel-core-bundle-id>
install file:///home/user/.m2/repository/org/apache/camel/camel-core-osgi/2.15.0/camel-core-osgi-2.15.0.jar
start <camel-core-osgi-bundle-id>
install file:///home/user/.m2/repository/org/apache/camel/camel-kura/2.15.0/camel-kura-2.15.0.jar
start <camel-kura-bundle-id>

...and all the components you plan to use in your routes:

install file:///home/user/.m2/repository/org/apache/camel/camel-stream/2.15.0/camel-stream-2.15.0.jar
start <camel-stream-bundle-id>

Then finally deploy your router bundle:

install file:///home/user/.m2/repository/com/example/myrouter/1.0/myrouter-1.0.jar
start <your-bundle-id>

Some KuraRouter utilities to make your life easier 

 Kura router base class provides many useful utilities. Let 's explore some of them.


SLF4J logger
Kura uses SLF4J facade for logging purposes. Protected member log returns SLF4J logger instance associated with the given Kura router.

public class MyKuraRouter extends KuraRouter {
    @Override
    public void configure() throws Exception {
        log.info("Configuring Camel routes!");
        ...
    }
}

BundleContext
Protected member bundleContext returns bundle context associated with the given Kura router.

public class MyKuraRouter extends KuraRouter {
    @Override
    public void configure() throws Exception {
        ServiceReference serviceRef = bundleContext.getServiceReference(LogService.class.getName());
        MyService myService = content.getService(serviceRef);
        ...
    }
}

CamelContext
Protected member camelContext is the CamelContext associated with the given Kura router.

public class MyKuraRouter extends KuraRouter {
    @Override
    public void configure() throws Exception {
        camelContext.getStatus();
        ...
    }
}

OSGi service resolver

OSGi service resolver (service(Class serviceType)) can be used to easily retrieve service by type from the OSGi bundle context.

public class MyKuraRouter extends KuraRouter {
    @Override
    public void configure() throws Exception {
        MyService myService = service(MyService.class);
        ...
    }
}

How can I configure CamelContext used by KuraRouter?

Kura router comes with the lifecycle callbacks that can be used to customize the way the Camel router works. For example to configure the CamelContext instance associated with the router just before the former is started, override beforeStartmethod of the KuraRouter class:

public class MyKuraRouter extends KuraRouter {
  ...
  protected void beforeStart(CamelContext camelContext) {
    OsgiDefaultCamelContext osgiContext = (OsgiCamelContext) camelContext;
    osgiContext.setName("NameOfTheRouter");
  }
}

What's next?

The current version of the Camel Kura component provides some useful utilities and simplifies Camel deployments into Kura by providing the opinionated Camel router. However there is still a space for the improvements in the regards of Camel Kura functionality. For example I can imagine predefined Camel components providing consumers/producers for the device services provided by the Kura. Or predefined Camel expressions that could be used to perform the content based routing based on the data read from the Raspberry Pi sensors. I plan to add more features to the Camel Kura in the next releases of Camel. Keep also in mind that we do love contributors in the Apache Camel community  - if you think you got something that could be added to the Camel Kura, just drop me a line!