How to create your own NApp: Part 2

Overview

This tutorial covers the basics on how to create a Network Application (NApp) with an execution loop for Kytos Controller (Kytos).

The average time to go throught it is: 10 min

What you will learn

  • How to Create a NApp with a loop;

  • How to adjust settings.py file for your NApp.

What you will need

Introduction

Now that you have learned how to build a simple NApp, in this tutorial we will help you to develop a more sophisticated one.

Suppose that you need an application that gather some data periodically from the controller or from the switches. In this situation, it is desirable to have a NApp that can run repeatedly without creating complex loops. Kytos provides the necessary tools to quick and easily create such a NApp.

Creating a looping NApp

In this tutorial, we will create a simple NApp, for learning purposes. This NApp will get the controller uptime from time to time and display it.

Also, we are going to introduce you to the NApps settings, a basic Python module where you can store, for example, configuration constants used by your NApp. This way, it is easier to maintain, understand and customize your code.

You can create the NApp structure manually, but Kytos has the kytos-utils project with a few command line utilities that can help you to create it.

Note

Make sure that you have completed your Development Environment setup and that you have enabled your virtual environment in order to have all kytos projects available to you.

Let’s create the structure:

$ cd
$ mkdir tutorials
$ cd tutorials
$ kytos napps create
--------------------------------------------------------------
Welcome to the bootstrap process of your NApp.
--------------------------------------------------------------
In order to answer both the username and the napp name,
You must follow this naming rules:
 - name starts with a letter
 - name contains only letters, numbers or underscores
 - at least three characters
--------------------------------------------------------------

Please, insert your NApps Server username: tutorial
Please, insert your NApp name: loopnapp
Please, insert a brief description for your NApp [optional]: Loop NApp

Congratulations! Your NApp have been bootstrapped!
Now you can go to the directory tutorial/loopnapp and begin to code your NApp.
Have fun!

You will be asked for few questions. Answer them according to your needs, they are very basic questions like username and NApp name. For this tutorial, answer tutorial and loopnapp, respectively.

Tip

If you want to change the answers in the future, just edit the kytos.json file and rename the directories if necessary.

Now you have a bootstrap NApp structure to work on.

During this tutorial, the only files that we need to worry about are the main.py and settings.py. Open with your preferred editor and let’s code:

$ cd ~/tutorials/tutorial/loopnapp
$ gedit main.py settings.py

settings.py

In order to adjust the polling frequency, let’s define a variable in settings.py. For instance, UPTIME_INTERVAL will store a number, in seconds, that we will use to determine our polling interval. In this example, our NApp will get the controller uptime every fifteen seconds.

# Polling frequency
UPTIME_INTERVAL = 15

main.py

Setup

In main.py file, we have to adjust the setup() method. We will add a line to tell the controller that this NApp will run repeatedly.

def setup(self):
    log.info("Loop NApp Loaded!")
    self.execute_as_loop(settings.UPTIME_INTERVAL)

The method execute_as_loop(x) is a Kytos NApp built-in method that instructs the controller to execute the method execute every x seconds.

If you don’t call execute_as_loop, the execute method will be executed only once, right after the setup method is finished.

Execute

In the execute method we code what will be executed every fifteen seconds. In this case, we gather the controller’s uptime and print it in the logs.

def execute(self):
    uptime = self.controller.uptime()
    log.info("Controller Uptime: %s", uptime)
Running Periodically

The entire NApp’s source code of the looping NApp follows:

from kytos.core import KytosNApp, log
from napps.tutorial.loopnapp import settings


class Main(KytosNApp):

    def setup(self):
        log.info("Loop NApp Loaded!")
        self.execute_as_loop(settings.UPTIME_INTERVAL)

    def execute(self):
        uptime = self.controller.uptime()
        log.info("Controller Uptime: %s", uptime)

    def shutdown(self):
        log.info("Loop NApp Unloaded!")

Running your NApp

In order to install and enable your NApp, you have to first run the Kytos controller. Kytos will then be able to recognize and manage installed/enabled NApps. In another terminal window, activate the virtual environment and run:

$ kytosd -f

2017-07-04 16:57:59,351 - INFO [kytos.core.logs] (MainThread) Logging config file "/home/user/test42/etc/kytos/logging.ini" loaded successfully.
2017-07-04 16:57:59,352 - INFO [kytos.core.controller] (MainThread) /home/user/test42/var/run/kytos
2017-07-04 16:57:59,353 - INFO [kytos.core.controller] (MainThread) Starting Kytos - Kytos Controller
2017-07-04 16:57:59,354 - INFO [kytos.core.tcp_server] (TCP server) Kytos listening at 0.0.0.0:6653
2017-07-04 16:57:59,356 - INFO [kytos.core.controller] (RawEvent Handler) Raw Event Handler started
2017-07-04 16:57:59,358 - INFO [kytos.core.controller] (MsgInEvent Handler) Message In Event Handler started
2017-07-04 16:57:59,359 - INFO [kytos.core.controller] (MsgOutEvent Handler) Message Out Event Handler started
2017-07-04 16:57:59,361 - INFO [kytos.core.controller] (AppEvent Handler) App Event Handler started
2017-07-04 16:57:59,362 - INFO [kytos.core.controller] (MainThread) Loading Kytos NApps...
2017-07-04 16:57:59,371 - INFO [kytos.core.napps.napp_dir_listener] (MainThread) NAppDirListener Started...
2017-07-04 16:57:59,373 - INFO [kytos.core.controller] (MainThread) Loading NApp tutorial/helloworld
2017-07-04 16:57:59,507 - INFO [tutorial/helloworld] (MainThread) Hello world! Now, I'm loaded!
2017-07-04 16:57:59,520 - INFO [root] (helloworld) Running NApp: <Main(helloworld, started 139775231104768)>
2017-07-04 16:57:59,527 - INFO [tutorial/helloworld] (helloworld) Hello world! I'm being executed!

(...)

kytos $>

You can now list all NApps, verify which ones are enabled and disable them. Only the new NApp will run this time. Yes, we are not running any other NApp for now, we are disabling everything, including OpenFlow NApps.

$ kytos napps list

Status |          NApp ID          |                      Description
=======+===========================+=======================================================
 [i-]  | kytos/of_core             | OpenFlow Core of Kytos Controller, responsible for ...
 [i-]  | kytos/flow_manager        | Manage switches' flows through a REST API.
 [i-]  | kytos/of_l2ls             | An L2 learning switch application for OpenFlow swit...
 [i-]  | kytos/of_lldp             | Discovers switches and hosts in the network using t...
 [i-]  | kytos/topology            | Keeps track of links between hosts and switches. Re...
 [ie]  | tutorial/helloworld       | Hello, world!

Status: (i)nstalled, (e)nabled

$ kytos napps disable tutorial/helloworld
INFO  NApp tutorial/helloworld:
INFO    Disabling...
INFO    Disabled.

In order to run your NApp, first you have to install it. Again, we are going to use the kytos command line from the kytos-utils package.

$ cd ~/tutorials
$ kytos napps install tutorial/loopnapp
INFO  NApp tutorial/loopnapp:
INFO    Searching local NApp...
INFO    Found and installed.
INFO    Enabling...
INFO    Enabled.

Note

This will try to get this napp from your current directory, then install it into your system. The NApp will also be enabled and immediately executed.

You can now see your NApp installed and enabled by running the command:

$ kytos napps list

Status |          NApp ID          |                      Description
=======+===========================+=======================================================
 [i-]  | kytos/of_core             | OpenFlow Core of Kytos Controller, responsible for ...
 [i-]  | kytos/flow_manager        | Manage switches' flows through a REST API.
 [i-]  | kytos/of_l2ls             | An L2 learning switch application for OpenFlow swit...
 [i-]  | kytos/of_lldp             | Discovers switches and hosts in the network using t...
 [i-]  | kytos/topology            | Keeps track of links between hosts and switches. Re...
 [i-]  | tutorial/helloworld       | Hello, world!
 [ie]  | tutorial/loopnapp         | Loop NApp

Testing your NApp

Back to the Kytos console, we can check the log messages. After seeing several lines with Controller Uptime, type quit to stop the controller.

kytos $> 2017-07-17 23:24:53,128 - INFO [tutorial/loopnapp] (Thread-1) Loop NApp Loaded!
2017-07-17 23:24:53,131 - INFO [root] (loopnapp) Running NApp: <Main(loopnapp, started 140460012750592)>
2017-07-17 23:24:53,134 - INFO [tutorial/loopnapp] (loopnapp) Controller Uptime: 0:01:14.565704
2017-07-17 23:25:08,138 - INFO [tutorial/loopnapp] (loopnapp) Controller Uptime: 0:01:29.569314
2017-07-17 23:25:23,141 - INFO [tutorial/loopnapp] (loopnapp) Controller Uptime: 0:01:44.572042
kytos $> quit
Stopping Kytos daemon... Bye, see you!
2017-07-17 23:25:29,729 - INFO [kytos.core.controller] (MainThread) Stopping Kytos
(...)

As you can see, the uptime was reported several times, at 23:24:53, 23:25:08 and 23:25:23 with an interval of 15 seconds, as expected.

That’s it! With only one line added to the setup method, your code will be running periodically. If you want to change the interval later, modify only the settings.py file and the new value will be used next time the NApp is loaded.

Well done! What’s Next?

Congratulations for finishing this Kytos Tutorial.

If you want to see other tutorials, click here to go to our Tutorials page.