OpenDaylight Rest API with Java

Santiago V picture Santiago V · Apr 21, 2014 · Viewed 7.8k times · Source

Before posting this question I search alot to be sure how to ask.

I am trying to connect to the OpenDaylight controller with Java, I am trying to connect by consuming the rest services given by the controller. My problem is, when I send the http request I cannot get any further than the login, I am not sure if its possible. Instead of getting the topology or other answer from the controller, I am getting the html of the login form.

Also, I am not sure if I should be connecting like this.

Any help/guidance is very appreciated. :)

My code for creating the connection is:

public String getContent(String urls) throws  IOException {

    String cont="";

    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(urls);

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();

    nvps.add(new BasicNameValuePair("j_username", "username"));

    nvps.add(new BasicNameValuePair("j_password", "password"));

    httpPost.setEntity(new UrlEncodedFormEntity(nvps));

    CloseableHttpResponse response2 = httpclient.execute(httpPost);

    try {

        System.out.println(response2.getStatusLine());

        HttpEntity entity2 = response2.getEntity();

        BufferedReader reader =new BufferedReader(new InputStreamReader(entity2.getContent()));

        String line="";

        while((line=reader.readLine())!=null){
            cont+=line+"\n";
        }


    } finally {
        response2.close();

    }           

    return cont;
}

When I run the code, this is what is printed:

HTTP/1.1 200 OK

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OpenDaylight - Login</title>
<script src="/js/bootstrap.min.js"></script>    
<script type="text/javascript">
    less = {
        env: "production"
    };
</script>
<script src="/js/less-1.3.3.min.js"></script>
</head>
<body>
<form action="j_security_check;jsessionid=LONGID" id="form" method="post">
<div class="container">
 <div class="content">
   <div class="login-form">
     <div id="logo"></div>
       <fieldset>
         <div class="control-group">
           <input type="text" name="j_username" placeholder="Username">
         </div>
         <div class="control-group">
           <input type="password" name="j_password" placeholder="Password">
         </div>
         <button class="btn btn-primary" type="submit" value="Log In" >
        <div class="icon-login"></div> Log In</button>
       </fieldset>
   </div>
 </div>
 </div> 
 </form>
 </body>
 </html>

Answer

vais picture vais · May 8, 2014

The problem seems to be with authentication. Username and password must be encoded to Base64. Please try the sample code below, which get the flow details in JSON format. You can to try getting topology details in the same way.

You can download the commons-codec library from here

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.codec.binary.Base64;

public class OpenDayLightUtils {

    public static void getFlowDetails() {

    String user = "admin";
    String password = "admin";
    String baseURL = "http://192.168.100.1:8080/controller/nb/v2/flowprogrammer";
    String containerName = "default";

    try {

        // Create URL = base URL + container
        URL url = new URL(baseURL + "/" + containerName);

        // Create authentication string and encode it to Base64
        String authStr = user + ":" + password;
        String encodedAuthStr = Base64.encodeBase64String(authStr.getBytes());

        // Create Http connection
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // Set connection properties
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Authorization", "Basic " + encodedAuthStr);
        connection.setRequestProperty("Accept", "application/json");

        // Get the response from connection's inputStream
        InputStream content = (InputStream) connection.getInputStream();

        BufferedReader in = new BufferedReader(new InputStreamReader(content));
        String line = "";
        while ((line = in.readLine()) != null) {
        System.out.println(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
}