new file for developers guide 96/23496/1
authorDivya <dshetty@brocade.com>
Wed, 20 May 2015 10:05:39 +0000 (03:05 -0700)
committerColin Dixon <colin@colindixon.com>
Mon, 29 Jun 2015 02:08:25 +0000 (02:08 +0000)
Patch Set 2: * Including this into the actual developer's guide.
             * Fixing list numbering/formatting.
             * Other random fixes
Patch Set 3: * Updated archetype to use Lithium release artifacts
             * Minor formatting fixes

Change-Id: I211abaea1d65aecf70dc08e10d482c6c981ade97
Signed-off-by: Divya <dshetty@brocade.com>
(cherry picked from commit da3a15716714dc27aa293f120bc07375ef63ce98)

manuals/developer-guide/src/main/asciidoc/bk-developers-guide.adoc
manuals/developer-guide/src/main/asciidoc/developing-app.adoc [new file with mode: 0755]

index 93cd74bfe2fd9ef035f758c7393bc71ec3f2eab4..b524ca0bf690b0b318064d115f8fcd314dfe6059 100644 (file)
@@ -48,6 +48,8 @@ include::section_Git_and_Gerrit_Setup.adoc[]
 
 include::section_Hacking_from_CLI.adoc[]
 
+include::developing-app.adoc[]
+
 = Project-Specific Development Guides
 
 include::alto/alto-developer-guide.adoc[ALTO]
diff --git a/manuals/developer-guide/src/main/asciidoc/developing-app.adoc b/manuals/developer-guide/src/main/asciidoc/developing-app.adoc
new file mode 100755 (executable)
index 0000000..e3d6090
--- /dev/null
@@ -0,0 +1,364 @@
+== Developing Apps on the OpenDaylight controller ==\r
+\r
+// Content is adapted from the https://wiki.opendaylight.org/view/OpenDaylight_Controller:MD-SAL:Startup_Project_Archetype\r
+\r
+This section provides information that is required to develop apps on the OpenDaylight controller.\r
\r
+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
+\r
+=== Overview ===\r
+\r
+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
+\r
+. Create a local repository for the code using a simple build process. \r
+. Start the OpenDaylight controller. \r
+. Test a simple remote procedure call (RPC) which you have created based on the principle of 'hello world'.\r
+\r
+=== Pre requisites ===\r
+\r
+This example requires the following.\r
+\r
+* A development environment with following set up and working correctly from the shell:\r
+** Maven 3.1.1 or later\r
+** Java 7- or Java 8-compliant JDK\r
+** An appropriate Maven settings.xml file. A simple way to get the default OpenDaylight settings.xml file is:\r
++\r
+ cp -n ~/.m2/settings.xml{,.orig} ; \wget -q -O - https://raw.githubusercontent.com/opendaylight/odlparent/stable/lithium/settings.xml > ~/.m2/settings.xml\r
+\r
+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
+\r
+=== Building an example module ===\r
+\r
+To develop an app perform the following steps.\r
+\r
+. 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
++\r
+[source,shell]\r
+mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype \\r
+-DarchetypeRepository=https://nexus.opendaylight.org/content/repositories/public/ \\r
+-DarchetypeCatalog=https://nexus.opendaylight.org/content/repositories/public/archetype-catalog.xml\r
++\r
+. Update the properties values as follows. Ensure that the groupid and the artifactid is lower case.\r
++\r
+[source,shell]\r
+Define value for property 'groupId': : org.opendaylight.example\r
+Define value for property 'artifactId': : example\r
+Define value for property 'version':  1.0-SNAPSHOT: : 1.0.0-SNAPSHOT\r
+Define value for property 'package':  org.opendaylight.example: : \r
+Define value for property 'classPrefix':  ${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)}\r
+Define value for property 'copyright': : Copyright (c) 2015 Yoyodyne, Inc. \r
++\r
+. Accept the default value of classPrefix that is, `(${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)})`.\r
+The classPrefix creates a Java Class Prefix by capitalizing the first character of the artifactId.\r
++\r
+NOTE: In this scenario, the classPrefix used is "Example".\r
+    Create a top-level directory for the archetype.\r
++\r
+[source,shell]\r
+${artifactId}/\r
+example/\r
+cd example/\r
+api/\r
+artifacts/\r
+features/\r
+impl/\r
+karaf/\r
+pom.xml\r
++\r
+. Build the 'example' project.\r
++\r
+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
++\r
+[source,shell]\r
+mvn clean install\r
++\r
+. Start the 'example' project for the first time.\r
++\r
+[source,shell]\r
+cd karaf/target/assembly/bin\r
+ls\r
+./karaf\r
++\r
+. 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
++\r
+[source,shell]\r
+opendaylight-user@root>\r
++\r
+. Verify if the “example” module is built and search  for the log entry which includes the entry 'ExampleProvider Session Initiated'.\r
++\r
+[source,shell]\r
+log:display | grep Example\r
++\r
+. Shutdown the OpenDaylight through the console by using the following command.\r
++\r
+[source,shell]\r
+shutdown -f\r
+\r
+=== Defining a Simple Hello World RPC ===\r
+\r
+. Run the maven archetype 'opendaylight-startup-archetype', and create the 'hello' project. + \r
++\r
+[source,shell]\r
+mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype \\r
+-DarchetypeRepository=http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/ \\r
+-DarchetypeCatalog=http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/archetype-catalog.xml\r
++\r
+. Update the Properties values as follows.\r
++\r
+[source,shell]\r
+Define value for property 'groupId': : org.opendaylight.hello\r
+Define value for property 'artifactId': : hello\r
+Define value for property 'version':  1.0-SNAPSHOT: : 1.0.0-SNAPSHOT\r
+Define value for property 'package':  org.opendaylight.hello: : \r
+Define value for property 'classPrefix':  ${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)}\r
+Define value for property 'copyright': : Copyright(c) Yoyodyne, Inc.\r
++\r
+. View the 'hello' project.\r
++\r
+[source,shell]\r
+cd hello/\r
+ls -1\r
+api\r
+artifacts\r
+features\r
+impl\r
+karaf\r
+pom.xml\r
++\r
+. Build 'hello' project by using the following command.\r
++\r
+[source,shell]\r
+mvn clean install\r
++\r
+. Verify  that the project is functioning by executing karaf.\r
++\r
+[source,shell]\r
+cd karaf/target/assembly/bin\r
+./karaf\r
++\r
+. The karaf cli appears as follows. + \r
+NOTE: Remember to wait for OpenDaylight to load completely. Verify that the Java process CPU has stabilized.+  \r
++\r
+[source,shell]\r
+opendaylight-user@root>\r
++\r
+. Verify that the 'hello' module is loaded by checking the log.\r
++\r
+[source,shell]\r
+log:display | grep Hello\r
++\r
+. Shutdown karaf.\r
++\r
+[source,shell]\r
+shutdown -f\r
++\r
+. Return to the top of the directory structure:\r
++\r
+[source,shell]\r
+cd ../../../../\r
++\r
+. View the entry point to understand where the log line came from. The entry point is in the impl project:\r
++\r
+[source,shell]\r
+impl/src/main/java/org/opendaylight/hello/impl/HelloProvider.java\r
++\r
+. Add any new things that you are doing in your implementation by using the HelloProvider.onSessionInitiate method. Its analogous to an Activator. \r
++\r
+[source,java]\r
+@Override\r
+    public void onSessionInitiated(ProviderContext session) {\r
+        LOG.info("HelloProvider Session Initiated");\r
+    }\r
+\r
+=== Add a simple HelloWorld RPC API\r
+\r
+. Navigate to the file.\r
++\r
+[source]\r
+Edit\r
+api/src/main/yang/hello.yang\r
++\r
+. 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
++\r
+[source,yang]\r
+module hello {\r
+    yang-version 1;\r
+    namespace "urn:opendaylight:params:xml:ns:yang:hello";\r
+    prefix "hello";\r
+    revision "2015-01-05" {\r
+        description "Initial revision of hello model";\r
+    }\r
+    rpc hello-world {\r
+        input {\r
+            leaf name {\r
+                type string;\r
+            }\r
+        }\r
+        output {\r
+            leaf greating {\r
+                type string;\r
+            }\r
+        }\r
+    }\r
+}\r
++\r
+. Return to the hello/api directory and build your API as follows.     \r
++\r
+[source,shell]\r
+cd ../../../\r
+mvn clean install\r
+\r
+=== Implement the HelloWorld RPC API\r
+\r
+. Define the HelloService, which is invoked through the 'hello-world' API.\r
++\r
+[source,shell]\r
+cd ../impl/src/main/java/org/opendaylight/hello/impl/\r
++\r
+. Create a new file called HelloWorldImpl.java and add in the code below.\r
++\r
+[source,java]\r
+package org.opendaylight.hello.impl;\r
+import java.util.concurrent.Future;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloService;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldInput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldOutput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldOutputBuilder;\r
+import org.opendaylight.yangtools.yang.common.RpcResult;\r
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;\r
+public class HelloWorldImpl implements HelloService {\r
+    @Override\r
+    public Future<RpcResult<HelloWorldOutput>> helloWorld(HelloWorldInput input) {\r
+        HelloWorldOutputBuilder helloBuilder = new HelloWorldOutputBuilder();\r
+        helloBuilder.setGreating("Hello " + input.getName());\r
+        return RpcResultBuilder.success(helloBuilder.build()).buildFuture();\r
+    }\r
+}\r
++\r
+. 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
++\r
+[source,java]\r
+-----\r
+/*\r
+ * Copyright(c) Yoyodyne, Inc. and others.  All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+package org.opendaylight.hello.impl;\r
+\r
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;\r
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;\r
+import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloService;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+public class HelloProvider implements BindingAwareProvider, AutoCloseable {\r
+    private static final Logger LOG = LoggerFactory.getLogger(HelloProvider.class);\r
+    private RpcRegistration<HelloService> helloService;\r
+    @Override\r
+    public void onSessionInitiated(ProviderContext session) {\r
+        LOG.info("HelloProvider Session Initiated");\r
+        helloService = session.addRpcImplementation(HelloService.class, new HelloWorldImpl());\r
+    }\r
+    @Override\r
+    public void close() throws Exception {\r
+        LOG.info("HelloProvider Closed");\r
+        if (helloService != null) {\r
+            helloService.close();\r
+        }\r
+    }\r
+}\r
+-----\r
++\r
+. 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
++\r
+[source,shell]\r
+cd ../../../../../../../\r
+mvn clean install\r
++\r
+. Return to the top level directory\r
++\r
+[source,shell]\r
+cd ../\r
++\r
+. Build the entire 'hello' again, which will pickup the changes you have made and build them into your project:\r
++\r
+[source,shell]\r
+mvn clean install\r
+\r
+=== Execute the 'hello' project for the first time\r
+\r
+. Run karaf\r
++\r
+[source,shell]\r
+cd ../karaf/target/assembly/bin\r
+./karaf\r
++\r
+. Wait for the project to load completely. Then view the log to see the loaded 'Hello' Module:\r
++\r
+[source,shell]\r
+log:display | grep Hello\r
+\r
+=== Test the 'hello-world' RPC via REST\r
+\r
+There are a lot of ways to test your RPC. Following are some examples.\r
+\r
+. Using the API Explorer through HTTP\r
+. Using a browser REST client\r
+\r
+==== Using the API Explorer through HTTP \r
+\r
+. Navigate to http://localhost:8181/apidoc/explorer/index.html[apidoc UI] with your web browser. + \r
+NOTE: In the URL mentioned above, Change 'localhost' to the IP/Host name to reflect your development machine's network address.\r
++\r
+. Select \r
++\r
+[source,shell]\r
+hello(2015-01-05)\r
++\r
+. Select \r
+[source]\r
+POST /operations/hello:hello-world\r
++\r
+. Provide the required value.\r
+[source,json]\r
+{"hello:input": { "name":"Your Name"}}\r
++\r
+. Click the button.\r
+. Enter the username and password, by default the credentials are admin/admin.\r
+. In the response body you should see.\r
++\r
+[source,json]\r
+{\r
+  "output": {\r
+    "greating": "Hello Your Name"\r
+  }\r
+}\r
+\r
+==== Using a browser REST client\r
+\r
+For example, use the following information in the Firefox plugin 'RESTClient' [https://github.com/chao/RESTClient} + \r
+[source]\r
+POST: http://192.168.1.43:8181/restconf/operations/hello:hello-world\r
+\r
+Header:\r
+[source]\r
+application/json\r
+\r
+Body:\r
+[source,json]\r
+{"input": {\r
+    "name": "Andrew"\r
+  }\r
+}\r
+\r
+\r
+=== Troubleshooting ===\r
+\r
+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
+By not invoking "session.addRpcImplementation()" the REST API will be unable to map /operations/hello:hello-world url to HelloWorldImpl.\r
+\r
+// == Developing Apps external to the controller ==\r