Showing posts with label OSGi command line. Show all posts
Showing posts with label OSGi command line. Show all posts

Tuesday, 22 October 2013

OSGi framework and Open DayLight

The newest SDN controller OpenDayLight is made on OSGi architecture. It gives you flexibility to load various plugins without stopping complete controller, which is cool actually as you can upgrade your existing plugin without any restart. So service is not affected. OSGi has three variations, OpenDayLight is made on equinox. (FYI eclipse is also made on equinox). OSGi bundles are essentially jar files. its the META-INF file which differentiates them. It has all information about the project. Whenever a project is loaded firstly an Activator class is called. (Note: That's why we give its address in tag bundle-activator in pom.xml). When bundle is loaded Activator first calls init method, and when you remove the bundle destroy method is called. the Now lets build a basic java code for ODL.


package org.opendaylight.controller.samples.userinfo.internal;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import org.opendaylight.controller.sal.core.ComponentActivatorAbstractBase;


public class Activator extends ComponentActivatorAbstractBase {
    protected static final Logger logger = LoggerFactory
            .getLogger(Activator.class);

    /**
     * Function called when the activator starts just after some
     * initializations are done by the
     * ComponentActivatorAbstractBase.
     *
     */
    public void init() {
       logger.info("I have started !!");
    }

    /**
     * Function called when the activator stops just before the
     * cleanup done by ComponentActivatorAbstractBase
     *
     */
    public void destroy() {
              logger.info("Over and out !!");
    }

 }


For more info on pom file click here.
To create a sample application click here


Wednesday, 16 October 2013

Running a custom bundle in Open Day light controller

I have copied one of the sample application int the opendaylight controller and named it as userinfo by changing artifact id in pom file. In this post i will show you how can i add my application while controller is running.
First of all build the bundle by command "mvn install". If everything is well go to command line of controller. But before that let me shed some light on how OSGi works. Life cycle of any app is 1. install 2. resolve 3.uninstall. First and last is self explanatory. A bundle will be in resolved state when all the dependencies are resolved and that's when you can start a service. 

osgi> install file:/home/ubuntu/workspace/controller/opendaylight/samples/userinfo
Bundle id is 150
< output omitted>
Version              0.0.1.SNAPSHOT
BundleDescription    org.opendaylight.controller.samples.userinfo_0.0.1.SNAPSHOT
Framework            org.eclipse.osgi.framework.internal.core.Framework@164e955
ResolutionFailureException org.osgi.framework.BundleException: The state indicates the bundle is resolved
Revisions            [org.opendaylight.controller.samples.userinfo_0.0.1.SNAPSHOT]
BundleContext        null
BundleId             150
StartLevel           1
SymbolicName         org.opendaylight.controller.samples.userinfo
BundleData           org.opendaylight.controller.samples.userinfo_0.0.1.SNAPSHOT
KeyHashCode          150
StateChanging        null

as you can see once installed osgi gives you a bundle id. now you can start or stop this bundle with this id.
To check currently installed bundle and its id/state say 'ss'

osgi> ss
"Framework is launched."


id      State       Bundle
0       ACTIVE      org.eclipse.osgi_3.8.1.v20120830-144521
<OUTPUT ommited>
150     RESOLVED    org.opendaylight.controller.samples.userinfo_0.0.1.SNAPSHOT

As you can see state is resolved. Good now let you can start the bundle by giving command 'start 150'. But it will give you error as the controller don't have binaries. For that you need to copy your binary (i.e. jar) to
/home/ubuntu/workspace/controller/opendaylight/distribution/opendaylight/target/distribution.opendaylight-0.1.0-SNAPSHOT-osgipackage/opendaylight/plugins

ok. Now say start 150 and njoy your code !

To create a simple application click here