exchange with GNPy to check path feasibility
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / ConnectToGnpyServer.java
1 /*
2  * Copyright © 2019 Orange, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.transportpce.pce.gnpy;
10
11 import com.google.common.io.CharStreams;
12
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;
19 import java.net.URL;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Class to connect to GNPy tool.
25  *
26  * @author Ahmed Triki ( ahmed.triki@orange.com )
27  *
28  */
29
30 public class ConnectToGnpyServer {
31
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";
34
35     public String gnpyCnx(String jsonTxt) {
36         String jsonRespTxt = null;
37         try {
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()));
41
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");
47
48             // Send the request to the GNPy
49             OutputStream os = conn.getOutputStream();
50             os.write(jsonTxt.getBytes());
51             os.flush();
52             if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
53                 throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
54             }
55             InputStreamReader response = new InputStreamReader((conn.getInputStream()));
56             if (response != null) {
57                 jsonRespTxt = null;
58                 try {
59                     jsonRespTxt = CharStreams.toString(response);
60                 } catch (IOException e1) {
61                     LOG.warn("Could not read characters of GNPy response: {}", e1.getMessage());
62                 }
63             }
64             conn.disconnect();
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());
69         }
70         return jsonRespTxt;
71     }
72
73     public boolean isGnpyURLExist() {
74         boolean exist = false;
75         try {
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");
84             conn.connect();
85             if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
86                 exist = true;
87             }
88             conn.disconnect();
89         }
90         catch (IOException e) {
91             LOG.warn("Could not connect to GNPy server");
92         }
93         return exist;
94     }
95
96     public String readResponse(InputStreamReader response) {
97         String output = null;
98         BufferedReader br = new BufferedReader(response);
99         String line;
100         StringBuilder sb = new StringBuilder();
101         try {
102             while ((line = br.readLine()) != null) {
103                 sb.append(line);
104             }
105             output = sb.toString();
106         } catch (IOException e) {
107             LOG.warn("IOException when reading GNPy response: {}", e.getMessage());
108         }
109         return output;
110     }
111 }