Consolidation of the GNPy module
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / ConnectToGnpyServer.java
index 94c632fcc1d2670d88a8ccd4a6f649b12240bf5c..5c88fab8c469ecfdff93f97cda3800ca561a52ce 100644 (file)
@@ -15,7 +15,6 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
 import java.net.URL;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,8 +31,9 @@ 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";
 
-    public String gnpyCnx(String jsonTxt) {
+    public String gnpyCnx(String jsonTxt) throws GnpyException  {
         String jsonRespTxt = null;
+
         try {
             URL url = new URL(URL_GNPY);
             String userCredentials = "gnpy:gnpy";
@@ -50,22 +50,16 @@ public class ConnectToGnpyServer {
             os.write(jsonTxt.getBytes());
             os.flush();
             if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
-                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
+                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) {
-                jsonRespTxt = null;
-                try {
-                    jsonRespTxt = CharStreams.toString(response);
-                } catch (IOException e1) {
-                    LOG.warn("Could not read characters of GNPy response: {}", e1.getMessage());
-                }
+                jsonRespTxt = CharStreams.toString(response);
             }
             conn.disconnect();
-        } catch (MalformedURLException e) {
-            LOG.warn("Exception : Malformed GNPy URL");
         } catch (IOException e) {
-            LOG.warn("IOException when connecting to GNPy server: {}", e.getMessage());
+            throw new GnpyException("In connectToGnpyServer: excpetion",e);
         }
         return jsonRespTxt;
     }
@@ -83,17 +77,19 @@ public class ConnectToGnpyServer {
             conn.setRequestProperty("Content-Type", "application/json");
             conn.connect();
             if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
+                LOG.info("In connectToGnpyServer: Gnpy instance is connected to T-PCE");
                 exist = true;
             }
             conn.disconnect();
         }
         catch (IOException e) {
-            LOG.warn("Could not connect to GNPy server");
+            LOG.warn("In connectToGnpyserver: could not connect to GNPy server {}",e.getMessage());
+            return exist;
         }
         return exist;
     }
 
-    public String readResponse(InputStreamReader response) {
+    public String readResponse(InputStreamReader response) throws GnpyException {
         String output = null;
         BufferedReader br = new BufferedReader(response);
         String line;
@@ -104,7 +100,7 @@ public class ConnectToGnpyServer {
             }
             output = sb.toString();
         } catch (IOException e) {
-            LOG.warn("IOException when reading GNPy response: {}", e.getMessage());
+            throw new GnpyException("In connectToGnpyserver: could not read response",e);
         }
         return output;
     }