1 == Developing Apps on the OpenDaylight controller ==
\r
3 // Content is adapted from the https://wiki.opendaylight.org/view/OpenDaylight_Controller:MD-SAL:Startup_Project_Archetype
\r
5 This section provides information that is required to develop apps on the OpenDaylight controller.
\r
7 You can either develop apps within the controller using the model-driven SAL (MD-SAL) archetype or develop external apps and use the RESTCONF to communicate with the controller.
\r
11 This section enables you to get started with app development within the OpenDaylight controller. In this example, you perform the following steps to develop an app.
\r
13 . Create a local repository for the code using a simple build process.
\r
14 . Start the OpenDaylight controller.
\r
15 . Test a simple remote procedure call (RPC) which you have created based on the principle of 'hello world'.
\r
17 === Pre requisites ===
\r
19 This example requires the following.
\r
21 * A development environment with following set up and working correctly from the shell:
\r
22 ** Maven 3.1.1 or later
\r
23 ** Java 7- or Java 8-compliant JDK
\r
24 ** An appropriate Maven settings.xml file. A simple way to get the default OpenDaylight settings.xml file is:
\r
26 cp -n ~/.m2/settings.xml{,.orig} ; \wget -q -O - https://raw.githubusercontent.com/opendaylight/odlparent/stable/lithium/settings.xml > ~/.m2/settings.xml
\r
28 NOTE: If you are using Linux or Mac OS X as your development OS, your local repository is ~/.m2/repository. For other platforms the local repository location will vary.
\r
30 === Building an example module ===
\r
32 To develop an app perform the following steps.
\r
34 . Create an 'Example' project using Maven and an archetype called the 'opendaylight-startup-archetype'. If you are downloading this project for the first time, then it will take sometime to pull all the code from the remote repository.
\r
37 mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype \
\r
38 -DarchetypeRepository=https://nexus.opendaylight.org/content/repositories/public/ \
\r
39 -DarchetypeCatalog=https://nexus.opendaylight.org/content/repositories/public/archetype-catalog.xml
\r
41 . Update the properties values as follows. Ensure that the groupid and the artifactid is lower case.
\r
44 Define value for property 'groupId': : org.opendaylight.example
\r
45 Define value for property 'artifactId': : example
\r
46 Define value for property 'version': 1.0-SNAPSHOT: : 1.0.0-SNAPSHOT
\r
47 Define value for property 'package': org.opendaylight.example: :
\r
48 Define value for property 'classPrefix': ${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)}
\r
49 Define value for property 'copyright': : Copyright (c) 2015 Yoyodyne, Inc.
\r
51 . Accept the default value of classPrefix that is, `(${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)})`.
\r
52 The classPrefix creates a Java Class Prefix by capitalizing the first character of the artifactId.
\r
54 NOTE: In this scenario, the classPrefix used is "Example".
\r
55 Create a top-level directory for the archetype.
\r
68 . Build the 'example' project.
\r
70 NOTE: Depending on your development machine's specification this might take a little while. Ensure that you are in the project's root directory, example/, and then issue the build command, shown below.
\r
75 . Start the 'example' project for the first time.
\r
78 cd karaf/target/assembly/bin
\r
82 . Wait for the karaf cli that appears as follows. Wait for OpenDaylight to fully load all the components. This can take a minute or two after the prompt appears. Check the CPU on your dev machine, specifically the Java process to see when it calms down.
\r
85 opendaylight-user@root>
\r
87 . Verify if the “example” module is built and search for the log entry which includes the entry 'ExampleProvider Session Initiated'.
\r
90 log:display | grep Example
\r
92 . Shutdown the OpenDaylight through the console by using the following command.
\r
97 === Defining a Simple Hello World RPC ===
\r
99 . Run the maven archetype 'opendaylight-startup-archetype', and create the 'hello' project. +
\r
102 mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype \
\r
103 -DarchetypeRepository=http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/ \
\r
104 -DarchetypeCatalog=http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/archetype-catalog.xml
\r
106 . Update the Properties values as follows.
\r
109 Define value for property 'groupId': : org.opendaylight.hello
\r
110 Define value for property 'artifactId': : hello
\r
111 Define value for property 'version': 1.0-SNAPSHOT: : 1.0.0-SNAPSHOT
\r
112 Define value for property 'package': org.opendaylight.hello: :
\r
113 Define value for property 'classPrefix': ${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)}
\r
114 Define value for property 'copyright': : Copyright(c) Yoyodyne, Inc.
\r
116 . View the 'hello' project.
\r
128 . Build 'hello' project by using the following command.
\r
133 . Verify that the project is functioning by executing karaf.
\r
136 cd karaf/target/assembly/bin
\r
139 . The karaf cli appears as follows. +
\r
140 NOTE: Remember to wait for OpenDaylight to load completely. Verify that the Java process CPU has stabilized.+
\r
143 opendaylight-user@root>
\r
145 . Verify that the 'hello' module is loaded by checking the log.
\r
148 log:display | grep Hello
\r
155 . Return to the top of the directory structure:
\r
160 . View the entry point to understand where the log line came from. The entry point is in the impl project:
\r
163 impl/src/main/java/org/opendaylight/hello/impl/HelloProvider.java
\r
165 . Add any new things that you are doing in your implementation by using the HelloProvider.onSessionInitiate method. Its analogous to an Activator.
\r
169 public void onSessionInitiated(ProviderContext session) {
\r
170 LOG.info("HelloProvider Session Initiated");
\r
173 === Add a simple HelloWorld RPC API
\r
175 . Navigate to the file.
\r
179 api/src/main/yang/hello.yang
\r
181 . Edit this file as follows. In the following example, we are adding the code in a YANG module to define the 'hello-world' RPC:
\r
186 namespace "urn:opendaylight:params:xml:ns:yang:hello";
\r
188 revision "2015-01-05" {
\r
189 description "Initial revision of hello model";
\r
205 . Return to the hello/api directory and build your API as follows.
\r
211 === Implement the HelloWorld RPC API
\r
213 . Define the HelloService, which is invoked through the 'hello-world' API.
\r
216 cd ../impl/src/main/java/org/opendaylight/hello/impl/
\r
218 . Create a new file called HelloWorldImpl.java and add in the code below.
\r
221 package org.opendaylight.hello.impl;
\r
222 import java.util.concurrent.Future;
\r
223 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloService;
\r
224 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldInput;
\r
225 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldOutput;
\r
226 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldOutputBuilder;
\r
227 import org.opendaylight.yangtools.yang.common.RpcResult;
\r
228 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
\r
229 public class HelloWorldImpl implements HelloService {
\r
231 public Future<RpcResult<HelloWorldOutput>> helloWorld(HelloWorldInput input) {
\r
232 HelloWorldOutputBuilder helloBuilder = new HelloWorldOutputBuilder();
\r
233 helloBuilder.setGreating("Hello " + input.getName());
\r
234 return RpcResultBuilder.success(helloBuilder.build()).buildFuture();
\r
238 . The HelloProvider.java file is in the current directory. Register the RPC that you created in the 'hello.yang' file in the HelloProvider.java file. You can either edit the HelloProvider.java to match what is below or you can simple replace it with the code below.
\r
243 * Copyright(c) Yoyodyne, Inc. and others. All rights reserved.
\r
245 * This program and the accompanying materials are made available under the
\r
246 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
247 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
249 package org.opendaylight.hello.impl;
\r
251 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
\r
252 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
\r
253 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
\r
254 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloService;
\r
255 import org.slf4j.Logger;
\r
256 import org.slf4j.LoggerFactory;
\r
258 public class HelloProvider implements BindingAwareProvider, AutoCloseable {
\r
259 private static final Logger LOG = LoggerFactory.getLogger(HelloProvider.class);
\r
260 private RpcRegistration<HelloService> helloService;
\r
262 public void onSessionInitiated(ProviderContext session) {
\r
263 LOG.info("HelloProvider Session Initiated");
\r
264 helloService = session.addRpcImplementation(HelloService.class, new HelloWorldImpl());
\r
267 public void close() throws Exception {
\r
268 LOG.info("HelloProvider Closed");
\r
269 if (helloService != null) {
\r
270 helloService.close();
\r
276 . Optionally, you can also build the Java classes which will register the new RPC. This is useful to test the edits you have made to HelloProvider.java and HelloWorldImpl.java.
\r
279 cd ../../../../../../../
\r
282 . Return to the top level directory
\r
287 . Build the entire 'hello' again, which will pickup the changes you have made and build them into your project:
\r
292 === Execute the 'hello' project for the first time
\r
297 cd ../karaf/target/assembly/bin
\r
300 . Wait for the project to load completely. Then view the log to see the loaded 'Hello' Module:
\r
303 log:display | grep Hello
\r
305 === Test the 'hello-world' RPC via REST
\r
307 There are a lot of ways to test your RPC. Following are some examples.
\r
309 . Using the API Explorer through HTTP
\r
310 . Using a browser REST client
\r
312 ==== Using the API Explorer through HTTP
\r
314 . Navigate to http://localhost:8181/apidoc/explorer/index.html[apidoc UI] with your web browser. +
\r
315 NOTE: In the URL mentioned above, Change 'localhost' to the IP/Host name to reflect your development machine's network address.
\r
324 POST /operations/hello:hello-world
\r
326 . Provide the required value.
\r
328 {"hello:input": { "name":"Your Name"}}
\r
330 . Click the button.
\r
331 . Enter the username and password, by default the credentials are admin/admin.
\r
332 . In the response body you should see.
\r
337 "greating": "Hello Your Name"
\r
341 ==== Using a browser REST client
\r
343 For example, use the following information in the Firefox plugin 'RESTClient' [https://github.com/chao/RESTClient} +
\r
345 POST: http://192.168.1.43:8181/restconf/operations/hello:hello-world
\r
359 === Troubleshooting ===
\r
361 If you get a response code 501 while attempting to POST /operations/hello:hello-world, check the file: HelloProvider.java and make sure the helloService member is being set.
\r
362 By not invoking "session.addRpcImplementation()" the REST API will be unable to map /operations/hello:hello-world url to HelloWorldImpl.
\r
364 // == Developing Apps external to the controller ==
\r