OpenDaylight with Scala

I’ve always been interested in learning functional programming, and wanted to try learning a few of the languages such as Scala.  Since Scala is a JVM based language, it should be able to interoperate with OSGi pretty well.  I figured if I can build a bundle jar with the appropriate manifest, then it will be all good.  So here’s my attempt at making Scala work with OSGi and OpenDaylight.

The first piece is SBT (Scala Build Tool), and the appropriate plugins.  To use SBT, first we create the directory structure:

| mystats-scala
| - project
| - src
    | - main
    | - scala

To handle some of the OSGi stuff I used the SBT BND plugin, which is added to the project/plugins.sbt file:

addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.4.0")

You can find out more about this plugin here:

Next I created a build.sbt in the root of my project directory.  In this file I declare my dependencies (OSGi core, Apache Felix, and parts of ODL), and tell it to look at my local Maven repository to resolve them (since I’ve already done a mvn build install of the ODL stuff):

name := "mystats"

version := "1.0.0"

libraryDependencies ++= Seq(
	"org.osgi" % "org.osgi.core" % "4.3.0" % "provided",
	"org.apache.felix" % "org.apache.felix.framework" % "4.0.3" % "runtime",
	"org.opendaylight.controller" % "sal" % "0.4.0-SNAPSHOT",
	"org.opendaylight.controller" % "statisticsmanager" % "0.4.0-SNAPSHOT"

resolvers += "Local Maven Repository" at "file:///"+Path.userHome+"/.m2/repository"

Thanks to the plugin above, I can now add in OSGi information:


OsgiKeys.bundleActivator := Option("com.example.mystats.Activator")

OsgiKeys.importPackage := Seq(

OsgiKeys.exportPackage := Seq("com.example.mystats")

OsgiKeys.additionalHeaders := Map(
    "Service-Component" -> "*",
      "Conditional-Package" -> "scala.*"

Now I can write my OSGi application in Scala. In my src/main/scala directory I create my Activator.scala file:

You’ll see that I have to do some Java conversions to create a Scala Set out of the Java Set, but overall its a pretty clean translation.
To get my jar file I use sbt:

$ sbt osgi-bundle

This creates my jar in the target/scala-2.9.2 directory. I can now install my file into the OSGi framework

osgi> install file:///home/fhsu/code/scala/hello/target/scala-2.9.2/mystats_2.9.2-1.0.0.jar

Then I can start the bundle and see the results

osgi> start 150
Start OSGi Bundle...
Start class
interface org.opendaylight.controller.switchmanager.ISwitchManager
Node: OF|00:00:00:00:00:00:00:07
Node: OF|00:00:00:00:00:00:00:06
Node: OF|00:00:00:00:00:00:00:05
Node: OF|00:00:00:00:00:00:00:04
Node: OF|00:00:00:00:00:00:00:03
Node: OF|00:00:00:00:00:00:00:02
Node: OF|00:00:00:00:00:00:00:01

I think this shows yet another cool way OSGi allows for modularity. Now we can even mix and match different languages! You can find the project code here:

2 thoughts on “OpenDaylight with Scala

  1. Glad to see some interest in Scala. If you want to be truly functional though, you can replace your for loop by:

    switchManager.getNodes().map( x => println(“Node: ” + x))


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s