Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / base / database / HtDatabaseWebAPIClient.java
1 package org.opendaylight.mwtn.base.database;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.HttpURLConnection;
7 import java.net.URL;
8 import java.net.URLConnection;
9
10 import org.json.JSONObject;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14
15 public class HtDatabaseWebAPIClient {
16
17         private static Logger LOG = LoggerFactory.getLogger(HtDatabaseWebAPIClient.class);
18         private static final int BUFSIZE = 1024;
19         private static final String CHARSET = "UTF-8";
20         private final String host;
21         private final int port;
22         public HtDatabaseWebAPIClient()
23         {
24                 this.host="http://localhost";
25                 this.port=9200;
26         }
27
28         public String sendRequest(String uri,String method,JSONObject body) throws IOException
29         {
30                 LOG.debug("try to send request with uri="+uri+" as method="+method);
31                 if(body!=null)
32                         LOG.trace("body:"+body.toString());
33                 String surl=String.format("%s:%d%s",this.host,this.port,uri);
34                 URL url = new URL(surl);
35                 URLConnection http = url.openConnection();
36                 ((HttpURLConnection) http).setRequestMethod(method);
37                 http.setDoOutput(true);
38                 http.setRequestProperty("Content-Type", "application/json");
39                 byte[] buffer = new byte[BUFSIZE];
40                 int len = 0, lensum = 0;
41                 // send request
42                 // Send the message to destination
43                 if(!method.equals("GET"))
44                 {
45                         try (OutputStream output = http.getOutputStream()) {
46                                 output.write(body.toString().getBytes(CHARSET));
47                         }
48                 }
49                 int responseCode = ((HttpURLConnection) http).getResponseCode();
50                 // Receive answer
51                 InputStream response;
52                 if (responseCode >= 200 && responseCode < 300)
53                         response = http.getInputStream();
54                 else
55                 {
56                         response = ((HttpURLConnection) http).getErrorStream();
57                         if(response==null)
58                                 http.getInputStream();
59                 }
60                 String sresponse="";
61                 if(response!=null)
62                 {
63                         while (true) {
64                                 len = response.read(buffer, 0, BUFSIZE);
65                                 if (len <= 0)
66                                         break;
67                                 lensum+=len;
68                                 sresponse+=new String(buffer,0,len,CHARSET);
69                         }
70                         response.close();
71                 }
72                 else
73                         LOG.debug("response is null");
74                 LOG.debug("ResponseCode: " + responseCode);
75                 LOG.trace("Response: " + sresponse);
76
77                 return sresponse;
78         }
79 }