JsonStringConverter in GNPy
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / ConnectToGnpyServer.java
index 5c88fab8c469ecfdff93f97cda3800ca561a52ce..a26f1960435e8d5bf9cee14f4e507ddd0717fa75 100644 (file)
@@ -9,13 +9,13 @@
 package org.opendaylight.transportpce.pce.gnpy;
 
 import com.google.common.io.CharStreams;
-
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -30,51 +30,37 @@ public class ConnectToGnpyServer {
 
     private static final Logger LOG = LoggerFactory.getLogger(ConnectToGnpyServer.class);
     static final String URL_GNPY = "http://127.0.0.1:8008/gnpy/api/v1.0/files";
+    static final String USER_CRED = "gnpy:gnpy";
 
-    public String gnpyCnx(String jsonTxt) throws GnpyException  {
+    public String returnGnpyResponse(String jsonTxt) throws GnpyException  {
         String jsonRespTxt = null;
-
+        LOG.debug("Sending request {}",jsonTxt);
         try {
-            URL url = new URL(URL_GNPY);
-            String userCredentials = "gnpy:gnpy";
-            String basicAuth = "Basic " + new String(java.util.Base64.getEncoder().encode(userCredentials.getBytes()));
-
-            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-            conn.setDoOutput(true);
-            conn.setRequestMethod("POST");
-            conn.setRequestProperty("Authorization", basicAuth);
-            conn.setRequestProperty("Content-Type", "application/json");
-
             // Send the request to the GNPy
+            HttpURLConnection conn = connectToGNPy("POST");
             OutputStream os = conn.getOutputStream();
-            os.write(jsonTxt.getBytes());
+            os.write(jsonTxt.getBytes(StandardCharsets.UTF_8));
             os.flush();
             if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                 throw new GnpyException(String.format(
                     "In connectToGnpyServer: could not connect to GNPy - response code: %s",conn.getResponseCode()));
             }
-            InputStreamReader response = new InputStreamReader((conn.getInputStream()));
-            if (response != null) {
+            InputStreamReader response = new InputStreamReader((conn.getInputStream()),StandardCharsets.UTF_8);
+            if (response.ready()) {
                 jsonRespTxt = CharStreams.toString(response);
             }
             conn.disconnect();
         } catch (IOException e) {
             throw new GnpyException("In connectToGnpyServer: excpetion",e);
         }
+        LOG.debug("Receiving response {}",jsonRespTxt);
         return jsonRespTxt;
     }
 
     public boolean isGnpyURLExist() {
         boolean exist = false;
         try {
-            URL url = new URL(URL_GNPY);
-            String userCredentials = "gnpy:gnpy";
-            String basicAuth = "Basic " + new String(java.util.Base64.getEncoder().encode(userCredentials.getBytes()));
-            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-            conn.setDoOutput(true);
-            conn.setRequestMethod("HEAD");
-            conn.setRequestProperty("Authorization", basicAuth);
-            conn.setRequestProperty("Content-Type", "application/json");
+            HttpURLConnection conn = connectToGNPy("HEAD");
             conn.connect();
             if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                 LOG.info("In connectToGnpyServer: Gnpy instance is connected to T-PCE");
@@ -89,6 +75,19 @@ public class ConnectToGnpyServer {
         return exist;
     }
 
+    private HttpURLConnection connectToGNPy(String action) throws IOException {
+        URL url = new URL(URL_GNPY);
+        String basicAuth = "Basic " + new String(java.util.Base64.getEncoder()
+            .encode(USER_CRED.getBytes(StandardCharsets.UTF_8)),StandardCharsets.UTF_8);
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        conn.setDoOutput(true);
+        conn.setRequestMethod(action);
+        conn.setRequestProperty("Authorization", basicAuth);
+        conn.setRequestProperty("Content-Type", "application/json");
+        conn.connect();
+        return conn;
+    }
+
     public String readResponse(InputStreamReader response) throws GnpyException {
         String output = null;
         BufferedReader br = new BufferedReader(response);