Install flows in OpenDaylight using Java

Although I had already done a similar post for this here, guess there are a lot of folks who are not able get everything working together without the actual source code.

So I thought it might be helpful if I provide with some ready to use project/code for installing flows using java.

1. Download the source code.

Download the source code of project “OpenDaylightFlowInstall” from my GitHub here. This will give you the following:

  1. FlowManager Class – Responsible for adding/deleting flows
  2. Tester Class – Responsible for actually passing the data & calling FlowManager class to install/delete flows.
  3. ODL Class – Responsible for OpenDaylight settings.

2. Import/Setup the code

You can import the code into eclipse as a java project or setup your command. Whatever you prefer.

To import the code in eclipse use “Import as Java Project” option and make sure that your “Project –> Build Automatically” is switched on.

e1

Also make sure that the two jars in lib folder are in classpath else compilation will fail.

3. Network & OpenDaylight Setup

I have a mininet running with one switch and two hosts which are able to ping each other & connected to OpenDaylight.

2

odl

4 Running the program

If you are using eclipse, simply right clicking on the tester class and saying “Run as Java Application” should run the code. Make sure of two things:

  1. Supply the right Src/Dest IP & Node details in the Tester.java

  2. Correct values in ODL.java under settings.

Once you run the program, the action DROP will be installed and ping will stop working.

3 odl2

Let me know, if you get stuck & i’ll try to help.

Advertisements

Skeleton OpenDaylight Application for Hydrogen

We have been writing OpenDaylight applications for quite some time & I see that there are lot of samples already available, but what I most of the times want is a skeleton project which I could just import in eclipse & its good to go with writing code. So a skeleton project with all the wiring already done is my frequent need.

So I have created a skeleton project for OpenDaylight which has everything bare minimum needed to start with a OpenDaylight application.

Prerequisite

  • You should already have OpenDaylight Hydrogen source code downloaded & working with eclipse.

Setup

  1. Pull the code of a blank/dummy hack application from my git.
  2. Copy this project under “controller/opendaylight/” where other opendaylight modules are present.
  3. Import this project into eclipse as a Maven project.
  4. Start putting in your code & you are good to go.

Testing

Once you build the above project, it will install a jar in target folder of the project. There are two ways of testing.

  1. Copy this jar manually into plugins folder of distribution.
  2. Make a entry into the parent & opendaylight root pom for this new module & rebuild.

Let me know, if you find any issues in deploying this hack or setting it up, in comments below.

OpenDaylight REST NBI Documentation

After the Helium release of OpenDaylight, I had this common problem of “How to refer REST NBI of Hydrogen release”. Wiki doesn’t work, I work with the distribution most of the time & same is with my friends.

So I thought of hosting the REST NBI Documentation of Hydrogen Release of OpenDaylight to help myself and in turn others who want to refer it.

I have put together a basic wiki type page where you can refer the NBI’s, model’s, URL’s etc of all the Hydrogen released northbound interfaces.

Please visit: http://opendaylight.nbi.sdngeeks.com

ODL_NBI

Network Statistics Sample SDN Application With OpenDaylight

I have been working on SDN applications for quite some time now and wanted to create a demo/sample app that could be reference or bootstrap app for people willing to explore more.

So I took some time out to come up with a idea of doing a very basic (some might call it no-brainer) SDN app for learning.

This application helps demonstrate following:
  • Demonstrate how REST API’s of a SDN Controller (in my case OpenDaylight) can be used to build a app.
  • I did not want to use any server-side programming/code to do this. This really shows the power of REST & SDN architecture.
  • Simple app that is easy to understand and replicate for someone learning this stuff.
  • Help you understand some concepts of SDN NBI that people talk about.
What is Network Statistics SDN Application ?

Bundled into a beautiful GUI, Network Statistics SDN Application runs on-top of a OpenDaylight SDN Controller and tracks port & table statistics of devices/nodes managed via OpenDaylight SDN Controller. Pure jQuery/Bootstrap and HTML based application without any server-side code.

Complete set of UI Screens developed for the application.

Things you can do in this application:

  1. Supply the URL/Username/Password of your OpenDaylight SDN Controller. Start tracking Nodes!
  2. View all the nodes managed by OpenDaylight in Network Devices section.
  3. View Port and Table statistics of each node found.

This is kind of no-brainier, but you can come up with more use cases around it etc, what this app if for? Its just for the kick-start.

If you are interested in running it:
  1. You can get the complete code from my git or if that’s too much work and you don’t intend to change code & just want to see it in action, download the file of this application from here (do “Save Link As“, this is .png, rename it to .war).
  2. Run the application either from code or by dropping the .war into tomcat/jetty/jboss (some web server)
  3. Supply your pre-installed OpenDaylight credentials and see it in action at http://localhost:8080/NetworkStatsApplication/
Assumptions:
  • I assume that you have a mininet and OpenDaylight setup working already as that is a per-requisite to building any application.
  • I prefer you read Brent’s blog on this which I refer whenever I’m stuck at things he has written about.

How I built this application and the code walk thru I have explained it in part-2 of this post as this blog was getting too long for everything.

Network Statistics Sample SDN Application With OpenDaylight Code Walk Thru

Continuing my earlier blog about the sample SDN application that I have created called Network Statistics SDN Application, here I’ll walk you thru the code and how I developed it.

Technologies used in this project are HTML, Twitter Bootstrap CSS, JQuery, JavaScript and WAR Packaging. This project is available at my git for download.

First, create a Java Dynamic Web Project. Here in this project although I do not use server-side code, but based on my confort in this tech-stack, that is what I chose.

18

 

 

 

 

 

 

 

 

Second, I searched online for some Bootstrap CSS based theme that would suffice this project & selected Freelancer Free Theme which I downloaded and copied into the WebContent of the above created project.

I have created the UI like this, with this theme.

Third, I changed the html file as per my needs and create four sections in it.

  1. Section 1: Place where I take input for SDN Controller releated credentials.
  2. Section 2: Place where we display Network Devices.
  3. Section 3: Place where we display selected device’s Port Statistics.
  4. Section 4: Place where we display selected device’s Table Statistics.

Hence, the complete HTML for above sections looks something like this:

<html lang="en">
<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>Network Stats SDN Application | SDN{Geeks}</title>

<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/freelancer.css" rel="stylesheet">
<link href="font-awesome-4.1.0/css/font-awesome.min.css"
    rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Montserrat:400,700"
    rel="stylesheet" type="text/css">
<link
    href="http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic"
    rel="stylesheet" type="text/css">
</head>

<body id="page-top" class="index">

    <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header page-scroll">
                <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span> <span
                        class="icon-bar"></span> <span class="icon-bar"></span> <span
                        class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#page-top">Network Stats SDN
                    Application</a>
            </div>

            <div class="collapse navbar-collapse"
                id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li class="hidden"><a href="#page-top"></a></li>
                    <li class="page-scroll"><a href="#nodes">Network Devices</a></li>
                    <li class="page-scroll"><a href="#port">Port Stats</a></li>
                    <li class="page-scroll"><a href="#table">Table Stats</a></li>
                </ul>
            </div>
        </div>
    </nav>

    <header>
        <div class="container">
            <div class="row">
                <div class="col-lg-10 col-lg-offset-1">
                    <p>
                        Network Statistics SDN Application runs on-top of a OpenDaylight
                        SDN Controller. <br />It tracks port, table and flow statistics
                        of all the nodes added to OpenDaylight. Run this application on
                        tomcat locally and supply the credentials below to see it in
                        action.
                    </p>
                    <p>This is a pure jQuery/Bootstrap and HTML based application
                        without any backend in itself.</p>
                </div>

                <div class="col-lg-12">
                    <br /> <br />
                    <div class="col-lg-3"></div>
                    <div class="col-lg-6">
                        <input type="text" class="form-control"
                            placeholder="SDN Controller URL" id="url"> <br /> <input
                            type="text" class="form-control"
                            placeholder="SDN Controller Username" id="username"> <br />
                        <input type="password" class="form-control"
                            placeholder="SDN Controller Password" id="password"> <br />
                        <br />
                        <div class="col-lg-8 col-lg-offset-2 text-center">
                            <a href="javascript:trackNetworkStatisticsClicked();"
                                class="btn btn-lg btn-outline"> <i class="fa fa-tachometer"></i>
                                Track Network Statistics
                            </a>
                        </div>

                    </div>
                    <div class="col-lg-3"></div>
                </div>
            </div>
        </div>
    </header>

    <section id="nodes">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 text-center">
                    <h2>Network Devices</h2>
                    <hr />
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12" align="center">
                    <p>This section shows all the device loaded into OpenDaylight.
                        I have created a thumbnail for these devices.</p>
                    <p>You can very well use any JS plotting frameworks to show the
                        network topology also</p>
                    <br /> <br />

                </div>
            </div>

            <div class="row" id="nodesButton">
                <div class="col-md-12" align="center">
                    <a href="#page-top" class="btn btn-success"> Please enter
                        OpenDaylight credentials first. </a> <br /> <br />
                </div>
            </div>

            <div class="row hidden" id="nodesDiv">
            </div>
        </div>
    </section>

    <section id="port" style="background: #C8D1DA;">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 text-center">
                    <h2>Port Statistics</h2>
                    <hr />
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12" align="center">
                    <p>This section shows the port statistics of the selected
                        device.</p>
                    <p>
                        You can intelligently show which ports are running maximum traffic
                        and are at risk etc. <br />There are numerous use cases that open
                        up with this data
                    </p>
                    <br /> <br />
                </div>
            </div>

            <div class="row" id="portButton">
                <div class="col-md-12" align="center">
                    <a href="#nodes" class="btn btn-success"> Please select a
                        device to view port statistics. </a> <br /> <br />
                </div>
            </div>

            <div class="row hidden" id="portDiv">
            </div>
        </div>
    </section>

    <section id="table">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 text-center">
                    <h2>Table Statistics</h2>
                    <hr>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12" align="center">
                    <p>This section shows the OpenFlow table statistics of the
                        selected device.</p>
                    <p>This information is helpul in pre-configuring the switch and
                        knowing the switch capacity.</p>
                    <br /> <br />
                </div>
            </div>

            <div class="row" id="tableButton">
                <div class="col-md-12" align="center">
                    <a href="#nodes" class="btn btn-success"> Please select a
                        device to view table statistics. </a> <br /> <br />
                </div>
            </div>

            <div class="row hidden" id="tableDiv">
            </div>

        </div>
    </section>

    <footer class="text-center">
        <div class="footer-below">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">Network Statistics SDN Application by
                        Saurabh Agarwal</div>
                </div>
            </div>
        </div>
    </footer>

    <script src="js/jquery-1.11.0.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script
        src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
    <script src="js/classie.js"></script>
    <script src="js/cbpAnimatedHeader.js"></script>
    <script src="js/jqBootstrapValidation.js"></script>
    <script src="js/contact_me.js"></script>
    <script src="js/freelancer.js"></script>
    
    <!-- This is where all the magic happen -->
    <script src="scripts/NetworkStats.js"></script>

</body>

</html>

Fourth, let’s move on to the javascript where the actual calls are made. I have created NetworkStats.js inside script folder and included in the above html.

<!-- This is where all the magic happen -->
<script src="scripts/NetworkStats.js"></script>

Fifth, for each action on the html page, I have written a corresponding javascript functions.

  1. Track Network Status” action – Here we take input from user for OpenDaylight credentials and fetch nodes via REST API.
  2. Port Stats” action – Here we take the node id and name of the device & fetch port statistics via REST API.
  3. Table Stats” action – Here we take the node id and name of the device & fetch port statistics via REST API.

Hence the complete javascript looks like this:

//Global Variables
var sdnControllerURL = null;
var sdnControllerUserName = null;
var sdnControllerPassword = null;
var base64EncodedPassword = null;

// Get the values supplied
// Store them in global valiables
// Populate Network Device section
// Show Network Devices
function trackNetworkStatisticsClicked() {

    sdnControllerURL = $("#url").val();
    sdnControllerUserName = $("#username").val();
    sdnControllerPassword = $("#password").val();
    base64EncodedPassword = "Basic "
            + btoa(sdnControllerUserName + ":" + sdnControllerPassword);

    console.log(sdnControllerURL);
    console.log(sdnControllerUserName);
    console.log(sdnControllerPassword);
    console.log(base64EncodedPassword);

    if (ifCredentialsNotNull(sdnControllerUserName, sdnControllerPassword)) {
        populateNetworkNodes();
    } else {
        alert("Trying error cases huhh..? Please enter createndials.");
    }
}

// Fetch network nodes from OpenDaylight
// Populate Network Device section
// Show Network Devices
function populateNetworkNodes() {
    var nodes = null;
    $
            .ajax({
                url : sdnControllerURL
                        + "/controller/nb/v2/switchmanager/default/nodes",
                type : "GET",
                async : false,
                contentType : "application/json",
                success : function(data, textStatus, jqXHR) {
                    console.log(data);
                    nodes = data;
                },
                error : function(jqXHR, textStatus, errorThrown) {
                    alert("Unable to fetch OpenDaylight Nodes.\nDid you supply the credentials correct?");
                },
                beforeSend : function(xhr) {
                    // Default Base64 Encoding for (admin/admin)
                    xhr
                            .setRequestHeader("Authorization",
                                    base64EncodedPassword);
                }
            });

    if (nodes != null && nodes != undefined) {
        // Construct divs
        $.each(nodes.nodeProperties, function(index, value) {
            var div = getNetworkDeviceDiv(value.properties.description.value,
                    value.node.id, value.properties.macAddress.value,
                    value.properties.timeStamp.value);
            $("#nodesDiv").append(div);
        });

        $("#nodesDiv").removeClass("hidden").addClass("visible");
        $("#nodesButton").removeClass("visible").addClass("hidden");

    }

}

// Method to create the network device div programatically
// No logic here, just plain factory generating divs on supplied inputs
function getNetworkDeviceDiv(name, id, mac, upTime) {
    var div = '<div class="col-sm-6 col-md-4"><div class="thumbnail"><br /> <br /> <img src="img/device.png"><div class="caption">';
    div += '<h4>' + name + '</h4>';
    div += '<ul class="list-group">';
    div += '<li class="list-group-item"><b>Id:</b> ' + id + '</li>';
    div += '<li class="list-group-item"><b>Name:</b> ' + name + '</li>';
    div += '<li class="list-group-item"><b>MAC:</b> ' + mac + '</li>';
    div += '<li class="list-group-item"><b>Connected Since:</b> ' + upTime
            + '</li>';
    div += '</ul><p>';
    div += '<a href="javascript:showSwitchPortStats(\''
            + name
            + '\',\''
            + id
            + '\')" class="btn btn-success" role="button">Port Stats</a> &nbsp;&nbsp;';
    div += '<a href="javascript:showSwitchTableStats(\'' + name + '\',\'' + id
            + '\')" class="btn btn-success" role="button">Table Stats</a>';
    div += '</p></div></div></div>';

    return div;
}

// Fetch ports for a node from OpenDaylight
// Populate ports stats section
function showSwitchPortStats(name, id) {
    var ports = null;
    $
            .ajax({
                url : sdnControllerURL
                        + "/controller/nb/v2/statistics/default/port/node/OF/"
                        + id,
                type : "GET",
                async : false,
                contentType : "application/json",
                success : function(data, textStatus, jqXHR) {
                    console.log(data);
                    ports = data;
                },
                error : function(jqXHR, textStatus, errorThrown) {
                    alert("Unable to fetch OpenDaylight Node Ports.\nDid you supply the credentials correct?");
                },
                beforeSend : function(xhr) {
                    // Default Base64 Encoding for (admin/admin)
                    xhr
                            .setRequestHeader("Authorization",
                                    base64EncodedPassword);
                }
            });

    if (ports != null && ports != undefined) {
        // Construct divs
        var finalDiv = '<div class="col-lg-12"><div class="panel panel-success"><div class="panel-heading"><h4>';
        finalDiv += name + ' : Port Statistics</h4>';
        finalDiv += '</div><div class="panel-body">';

        // For each port create a sub element in the final div
        $.each(ports.portStatistic, function(index, value) {
            var div = getPortsDiv(value.nodeConnector.id, value.receivePackets,
                    value.transmitPackets, value.receiveBytes,
                    value.transmitBytes, value.receiveDrops,
                    value.transmitDrops);
            finalDiv += div;
        });

        finalDiv += '</div></div></div>';
        $("#portDiv").append(finalDiv);
        $("#portDiv").removeClass("hidden").addClass("visible");
        $("#portButton").removeClass("visible").addClass("hidden");

    }

}

// Method to create the port stats div programatically
// No logic here, just plain factory generating divs on supplied inputs
function getPortsDiv(portId, receivePackets, transmitPackets, receiveBytes,
        transmitBytes, receiveDrops, transmitDrops) {
    var div = '<div class="col-sm-2 col-md-2"><div class="thumbnail"><img src="img/port.png" alt="..." height="60" width="60"><div class="caption">';
    div += '<p align="center">Port ' + portId + '</p></div>';
    div += 'Packet Rx: ' + receivePackets + '<br /> Packet Tx: '
            + transmitPackets + '<br />Byte Rx: ' + receiveBytes
            + '<br />Byte Tx: ' + transmitBytes + '<br />Drop Rx: '
            + receiveDrops + '<br />Drop Tx: ' + transmitDrops + '</div></div>';
    return div;
}

// Fetch table stats for a node from OpenDaylight
// Populate table stats section
function showSwitchTableStats(name, id) {
    var tableStats = null;
    $
            .ajax({
                url : sdnControllerURL
                        + "/controller/nb/v2/statistics/default/table/node/OF/"
                        + id,
                type : "GET",
                async : false,
                contentType : "application/json",
                success : function(data, textStatus, jqXHR) {
                    console.log(data);
                    tableStats = data;
                },
                error : function(jqXHR, textStatus, errorThrown) {
                    alert("Unable to fetch OpenDaylight Node Table Stats.\nDid you supply the credentials correct?");
                },
                beforeSend : function(xhr) {
                    // Default Base64 Encoding for (admin/admin)
                    xhr
                            .setRequestHeader("Authorization",
                                    base64EncodedPassword);
                }
            });

    if (tableStats != null && tableStats != undefined) {
        // Construct divs
        var firstTable = null;
        var finalDiv = '<div class="col-lg-12"><div class="panel panel-success"><div class="panel-heading">';
        finalDiv += '<h4>' + name + ' : Table Statistics</h4>';

        // I'm only considering one table for this demo project.
        // You can actually iterate thru them to have multiple as per OF spec
        $.each(tableStats.tableStatistic, function(index, value) {
            firstTable = value;
            return;
        });

        finalDiv += '</div><div class="panel-body"><p>Table Id: '
                + firstTable.nodeTable.id + '</p></div>';
        finalDiv += '<ul class="list-group">';
        finalDiv += '<li class="list-group-item"><span class="badge badge-success">'
                + firstTable.activeCount + '</span>Active Count</li>';
        finalDiv += '<li class="list-group-item"><span class="badge badge-success">'
                + firstTable.lookupCount + '</span>Lookup Count</li>';
        finalDiv += '<li class="list-group-item"><span class="badge badge-success">'
                + firstTable.matchedCount + '</span>Matched Count</li>';
        finalDiv += '<li class="list-group-item"><span class="badge badge-success">'
                + firstTable.maximumEntries
                + '</span>Maximum Supported Entries</li>';
        finalDiv += '</ul></div></div>';

        $("#tableDiv").append(finalDiv);
        $("#tableDiv").removeClass("hidden").addClass("visible");
        $("#tableButton").removeClass("visible").addClass("hidden");

    }

}

// Utility function to check not null user name password
function ifCredentialsNotNull(username, password) {
    if (username != null && password != null && username != ''
            && password != '') {
        return true;
    }
    return false;
}

Now, deploy this application into tomcat/jetty/jboss or any other web server of choice & you should see it running.

Sixth, you can verify the application, by enabling firebug or browser web developer tools to see if REST API calls are going thru, some sample calls as I get them are below.

6 11 8

 

Hope you are able to build and run the application by yourself using the above steps.

Let me know if you find any issues & i’ll try to help.