X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=pce%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Ftransportpce%2Fpce%2Fgnpy%2FConnectToGnpyServer.java;h=5c88fab8c469ecfdff93f97cda3800ca561a52ce;hb=56dd2add269f0fdc8efdc1e3bfece25301ba6a3f;hp=6a6a0d8860c19c00a99a42afe85134e7dca0839a;hpb=e4a554e661804d03c14c9e65d8332e576af801b4;p=transportpce.git diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/ConnectToGnpyServer.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/ConnectToGnpyServer.java index 6a6a0d886..5c88fab8c 100644 --- a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/ConnectToGnpyServer.java +++ b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/ConnectToGnpyServer.java @@ -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; @@ -30,10 +29,11 @@ import org.slf4j.LoggerFactory; public class ConnectToGnpyServer { private static final Logger LOG = LoggerFactory.getLogger(ConnectToGnpyServer.class); - static final String URL_GNPY = "http://127.0.0.1:5000/gnpy/api/v1.0/files"; + 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; }