Software Defined Networking
1819804 Members
2936 Online
109607 Solutions
New Discussion

OSGi Bundle Activation

 
sepbot
Advisor

OSGi Bundle Activation

I am extremely new to OSGi development and still trying to understand the concepts behind it. I am trying to create a very simple OSGi bundle that will listen for Packet_Ins and log them, but I am having some difficulty.

 

I think I understand how to use addPacketListener in ControllerService to add a listener and use SequencedPacketAdapter to look at OpenFlow Packet_in events. But my problem is that the bundle does not seem to get run at all. From what I understand, OSGi bundles need to have a "bundle activator". I have created an activate method which is annotated using @Activate which is provided by org.apache.felix.scr.annotations.Activate. However this method never gets run, and I can also observe that in the Maven produced output the particular .jar file does not contain the OSGI-INF folder that is expected in OSGi bundles.

 

So i guess my question is that what else do I need to do in order to make this bundle OSGi compliant? Do I need to make a service interface first and implement that in my listener class?

 

 

// EDIT

Just a quick update: it would appear that I forgot to include the @Component annotation, upon inclusion of this annotation, my produced output includes the OSGI-INF folder and the <scr:component> tag in the .xml file includes the appropriate active function name, however based on implmentation of SdnLog in the activate method, I can see that this method is still not being run.

4 REPLIES 4
Javed Padinhakara
Respected Contributor

Re: OSGi Bundle Activation

you may want to try adding immediate=true flag to the @Component line..

 

i.e

 

@Component(immediate = true)

 

hope that works!

 

reference: http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html

 

-J

 

sepbot
Advisor

Re: OSGi Bundle Activation

Thanks for your response Javed. Unfortunately it did not change the outcome for me.

sdnindia
Trusted Contributor

Re: OSGi Bundle Activation

Few thoughts as below

The particular.jar file should be there in your working environment for OSGi bundle.

Make sure the “Bundle Activator” and “Bundle MANIFEST.MF” content is right so that the bundle will run.

 

Also please see if the below links are useful

        http://blog.knowhowlab.org/2010/10/osgi-tutorial-4-ways-to-activate-code.html

        http://www.javaworld.com/javaworld/jw-03-2008/jw-03-osgi1.html

sdnindia
Trusted Contributor

Re: OSGi Bundle Activation

Thanks for your query.

 

Please see below steps-

 

Step 1:

--------

Please include the required plugins in POM to generate the bundle instead of plain jar file (This is going to generate OSGI-INF folder).

 

        <dependency>

            <groupId>org.apache.felix</groupId>

            <artifactId>org.apache.felix.scr.annotations</artifactId>

            <version>1.9.4</version>

        </dependency>

 

<plugin>

                <groupId>org.apache.felix</groupId>

                <artifactId>maven-scr-plugin</artifactId>

                <version>1.13.0</version>

                <executions>

                    <execution>

                        <id>generate-scr-srcdescriptor</id>

                        <goals>

                            <goal>scr</goal>

                        </goals>

                    </execution>

                </executions>

                <configuration>

                    <supportedProjectTypes>

                        <supportedProjectType>bundle</supportedProjectType>

                        <supportedProjectType>war</supportedProjectType>

                    </supportedProjectTypes>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.apache.felix</groupId>

                <artifactId>maven-bundle-plugin</artifactId>

                <version>2.3.6</version>

                <extensions>true</extensions>

                <configuration>

                    <instructions>

                       <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>

                    </instructions>

                </configuration>

</plugin>

 

 

The Maven SCR Plugin plugin creates the necessary descriptors for the OSGi Declarative Services. It generates the OSGI-INF folder.

 

The Maven Bundle Plugin handles the jar dependencies.

 

Step 2:

-------

Bundle Activator: This is the simplest and oldest way to activate code in your bundle. There can be only one BundleActivator per bundle. This class should have empty constructor and should be specified as Bundle-Activator in MANIFEST.MF file to be called on OSGi bundle STARTING stage.

 

public class Activator implements BundleActivator {

                public void start(BundleContext context) throws Exception {

                                System.out.println("Sample bundle starting");

                                // Insert bundle activation logic here

                }

 

                public void stop(BundleContext context) throws Exception {

                                System.out.println("Sample bundle stopping");

                                // Insert bundle deactivation logic here

                }

}

 

Another and better alternative is using Declarative Service.

Declarative Services (DS): Declarative Services specification is the better way of activating your code in bundle.  The OSGi declarative services allows you to define and consume services via metadata (XML). Via DS you can define OSGi services without extending or implementing OSGi classes. This allows for these services to be tested independently of the OSGi runtime. There can be any  number of Service Components in a bundle. Every component should have Component Description - XML file with declaration of component. Every Component Description should be listed in MANIFEST OSGi header Service-Component to be available for Service Component Runtime, that manages components and their life cycle. (Above plugin in POM does this).

 

To declare a service via DS,

Eg:

 

        // Component description

        @Component(immediate = true)

        // Service description

        @Service(value = Sample.class)

        public class SampleComponent implements Sample{

            // Reference to PreferencesService

            @Reference(name = "preferencesService", referenceInterface = PreferencesService.class,

                    cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.STATIC)

            private PreferencesService preferencesService;

 

            public String echo(String str) {

                return str;

            }

 

            // Called to bind PreferencesService

            public void bindPreferencesService(PreferencesService preferencesService) {

                System.out.println("PreferencesService is linked");

                this.preferencesService = preferencesService;

            }

 

            // Called to unbind PreferencesService

            public void unbindPreferencesService(PreferencesService preferencesService) {

                this.preferencesService = null;

                System.out.println("PreferencesService is unlinked");

            }

        }

   

'Please let us know if this answers your question by marking this response as an 'accepted solution’ or if it has been helpful click on the purple star to the left to give me kudos'.

 

 

HP SDN Team