2 * Copyright © 2019 Orange, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.transportpce.pce.gnpy;
11 import com.google.common.io.CharStreams;
13 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.InputStreamReader;
16 import java.io.OutputStream;
17 import java.net.HttpURLConnection;
18 import java.net.MalformedURLException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Class to connect to GNPy tool.
26 * @author Ahmed Triki ( ahmed.triki@orange.com )
30 public class ConnectToGnpyServer {
32 private static final Logger LOG = LoggerFactory.getLogger(ConnectToGnpyServer.class);
33 static final String URL_GNPY = "http://127.0.0.1:5000/gnpy/api/v1.0/files";
35 public String gnpyCnx(String jsonTxt) {
36 String jsonRespTxt = null;
38 URL url = new URL(URL_GNPY);
39 String userCredentials = "gnpy:gnpy";
40 String basicAuth = "Basic " + new String(java.util.Base64.getEncoder().encode(userCredentials.getBytes()));
42 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
43 conn.setDoOutput(true);
44 conn.setRequestMethod("POST");
45 conn.setRequestProperty("Authorization", basicAuth);
46 conn.setRequestProperty("Content-Type", "application/json");
48 // Send the request to the GNPy
49 OutputStream os = conn.getOutputStream();
50 os.write(jsonTxt.getBytes());
52 if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
53 throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
55 InputStreamReader response = new InputStreamReader((conn.getInputStream()));
56 if (response != null) {
59 jsonRespTxt = CharStreams.toString(response);
60 } catch (IOException e1) {
61 LOG.warn("Could not read characters of GNPy response: {}", e1.getMessage());
65 } catch (MalformedURLException e) {
66 LOG.warn("Exception : Malformed GNPy URL");
67 } catch (IOException e) {
68 LOG.warn("IOException when connecting to GNPy server: {}", e.getMessage());
73 public boolean isGnpyURLExist() {
74 boolean exist = false;
76 URL url = new URL(URL_GNPY);
77 String userCredentials = "gnpy:gnpy";
78 String basicAuth = "Basic " + new String(java.util.Base64.getEncoder().encode(userCredentials.getBytes()));
79 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
80 conn.setDoOutput(true);
81 conn.setRequestMethod("HEAD");
82 conn.setRequestProperty("Authorization", basicAuth);
83 conn.setRequestProperty("Content-Type", "application/json");
85 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
90 catch (IOException e) {
91 LOG.warn("Could not connect to GNPy server");
96 public String readResponse(InputStreamReader response) {
98 BufferedReader br = new BufferedReader(response);
100 StringBuilder sb = new StringBuilder();
102 while ((line = br.readLine()) != null) {
105 output = sb.toString();
106 } catch (IOException e) {
107 LOG.warn("IOException when reading GNPy response: {}", e.getMessage());