Using OpenDaylight’s REST API from Go

Its been a while since I’ve had a chance to play with OpenDaylight. Since I’ve been on a Go kick lately I decided to try and interact with ODL with Go. Using Go’s built-in JSON decoder, its pretty easy to grab information from OpenDaylight. Here’s a quick example of accessing the topology API using Go.

First I defined the main package since I’ll just be running this single script, and import the necessary libraries in lines 1-9.

Next I define a bunch of structs that match the JSON API in lines 11-67

Now I can put it to work, lets step through the main function.

I start with building the URL to grab topology information using a base URL string, then joining it with the directory. I print out the URL just as a check.

func main() {
  baseurl := ""
  // The URL to get the topology of the default slice
  url := strings.Join([]string{baseurl, "topology/default"}, "/")

Next I do an HTTP GET. The return value is the response and an error variable, which I check before moving on.

resp, err := http.Get(url)
if err != nil {

Now I take the response, read in the body, and store it in contents.

contents, err := ioutil.ReadAll(resp.Body)

And finally I create an EdgeProperties variable based on the structs above to store the JSON data into, and unmarshal the contents into that variable. Once that is done, I am able to access the various fields of the struct and print them out.

var e EdgeProperties
err = json.Unmarshal(contents, &e)

Now you can run it using go run odl.go.

Go is a fun language to play with. Even though it seems like Python is the preferred network scripting language, its still interesting to see how things work in something like Go. I’d like to keep expanding this to build an OpenDaylight API Go library that could make it easier to use it as an alternative language for interacting with ODL. I have a Github project setup here if you’re interested:


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s