In this tutorial we will show you how to generate an ONOS template application using the
onos-create-app tool. This tool relies on the Maven archetypes to generate a base ONOS application as well as several different variants that add CLI command, REST API, and a few means of extending the GUI.
Select ONOS version
Because the tool uses Maven archetypes, we need to first indicate what version of ONOS API we would like to build our application against. To do this, simply export the
ONOS_POM_VERSION environment variable to the desired ONOS version as follows:
If the above environment variable is not explicitly set, Maven will look for the most recent version of the archetypes available in your
~/.m2/repository (and then on Maven central) and this may not be what you intended so it's always best to set this explicitly.
Generate a new base ONOS application project
Let's now generate a skeletal ONOS application project which will be fully compilable and ready to be deployed. When creating the base project, we have to specify the Maven groupId, artifactId and version. These are necessary for locating the application in the Maven coordinate space. Additionally, we will also specify Java package name where the generated code will be located. Let's run the following command:
Alternatively, you could invoke
mvn archetype:generate command directly, but we recommend that you use the
onos-create-app instead. Also, for a real application, you would want to use your own groupId, artifactId, etc., but for the purpose of this tutorial, it is recommended that you use the suggested values.
After this you should see the following output:
Confirm the properties by typing "
y" and you will be presented with the following output:
This has now generated a new project for you. Note that the
2.0.0 was used for this. This is because we explicitly set the
Let's move on to building it and loading it into ONOS.
Compile, install and activate your new ONOS application
Now we are ready to build the ONOS app. We can do that by changing into the root directory of the generated project and invoking Maven as follows:
When the build is complete, both the OSGi bundle and the application archive have been installed in your local maven repository. To install the application into running ONOS instance (or cluster), you can use the
onos-app tool, which uses ONOS REST API within, to upload the
.oar file as shown in the following example. If you need help running ONOS please refer to this page. Note that by using the exclamation mark with the
install parameter, the application will be activated immediately after being installed.
Now, from the ONOS console, you should be able to see the application has been installed and activated,
You can now use the generated code for this test app as a framework for adding your own custom code. If you edit the file
src/main/java/org/foo/app/AppComponent.java, you can see how the application is created, and add your own code to the application.
Generate various project overlays
ONOS applications can hook into the ONOS CLI, REST API and GUI. When generating your application, you can use overlays to generate the classes needed to give your application access to these services.
Command Line Overlay
To allow your application to add commands to the ONOS CLI, overlay the CLI interface like this:
Now, as before, we need to build and install our application. Since we installed it once already, we will use the
reinstall! command to deploy it:
Using the ONOS command line, we now have access to the 'sample' command, which was defined by our overlay:
You can use the CLI overlay to add your own commands to the CLI for your app. Edit the file
src/main/java/org/foo/app/AppCommand.java to see how the sample command is implemented.
REST API Overlay
You can even create a REST interface for your application by overlaying a REST API project facet. Run the following to generate the project overlay and to rebuild and reinstall the application:
Note that ONOS automatically generates SwaggerUI documentation for the URIs exposed in your RESTful application. After activating the application, you can visit the SwaggerUI at http://localhost:8181/v1/docs/ . To view the documentation for the newly generated app REST API, you will need to select Sample app REST API from the SwaggerUI menu bar.
User Interface Overlays
To allow your application to add to the ONOS web UI, you can use one of several different project overlays. Navigate in the shell to the directory in which you have already created your app and run one of the following commands:
... generate a "Custom View" overlay, like this:
... or the "Tabular View" overlay, like this:
... or the "Topology Overlay" overlay, like this:
Now, as before, we need to build and install our application. Since we installed it once already, we will use the reinstall command to deploy it:
Your new forms of application that extends the GUI will now be active. To view the new application page that was just created, point your browser to the ONOS GUI at http://localhost:8181/onos/ui/. In the upper left corner of the home page, click the navigation button to activate the drop down navigation menu. At the bottom of the list, you will see an entry for "Sample Custom" or "Sample Table", depending on which overlay you used. If you select it, you will navigate to the page that was installed by your test application.
You can now use the generated web UI to add your own UI elements.
See the Web UI tutorials for more detailed information.
Publish the artifacts to local repository
Note that, if you wish to build an application against the current ONOS master, i.e. an unreleased version, you will need to first build ONOS yourself and then publish the resulting artifacts in the Maven repository under
~/.m2/repository by running the following command:
Also, you will have to build the current version of the ONOS application archetypes as follows:
Learn more about the Application Subsystem.