JsonStringConverter in GNPy
[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 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.OutputStream;
16 import java.net.HttpURLConnection;
17 import java.net.URL;
18 import java.nio.charset.StandardCharsets;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Class to connect to GNPy tool.
24  *
25  * @author Ahmed Triki ( ahmed.triki@orange.com )
26  *
27  */
28
29 public class ConnectToGnpyServer {
30
31     private static final Logger LOG = LoggerFactory.getLogger(ConnectToGnpyServer.class);
32     static final String URL_GNPY = "http://127.0.0.1:8008/gnpy/api/v1.0/files";
33     static final String USER_CRED = "gnpy:gnpy";
34
35     public String returnGnpyResponse(String jsonTxt) throws GnpyException  {
36         String jsonRespTxt = null;
37         LOG.debug("Sending request {}",jsonTxt);
38         try {
39             // Send the request to the GNPy
40             HttpURLConnection conn = connectToGNPy("POST");
41             OutputStream os = conn.getOutputStream();
42             os.write(jsonTxt.getBytes(StandardCharsets.UTF_8));
43             os.flush();
44             if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
45                 throw new GnpyException(String.format(
46                     "In connectToGnpyServer: could not connect to GNPy - response code: %s",conn.getResponseCode()));
47             }
48             InputStreamReader response = new InputStreamReader((conn.getInputStream()),StandardCharsets.UTF_8);
49             if (response.ready()) {
50                 jsonRespTxt = CharStreams.toString(response);
51             }
52             conn.disconnect();
53         } catch (IOException e) {
54             throw new GnpyException("In connectToGnpyServer: excpetion",e);
55         }
56         LOG.debug("Receiving response {}",jsonRespTxt);
57         return jsonRespTxt;
58     }
59
60     public boolean isGnpyURLExist() {
61         boolean exist = false;
62         try {
63             HttpURLConnection conn = connectToGNPy("HEAD");
64             conn.connect();
65             if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
66                 LOG.info("In connectToGnpyServer: Gnpy instance is connected to T-PCE");
67                 exist = true;
68             }
69             conn.disconnect();
70         }
71         catch (IOException e) {
72             LOG.warn("In connectToGnpyserver: could not connect to GNPy server {}",e.getMessage());
73             return exist;
74         }
75         return exist;
76     }
77
78     private HttpURLConnection connectToGNPy(String action) throws IOException {
79         URL url = new URL(URL_GNPY);
80         String basicAuth = "Basic " + new String(java.util.Base64.getEncoder()
81             .encode(USER_CRED.getBytes(StandardCharsets.UTF_8)),StandardCharsets.UTF_8);
82         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
83         conn.setDoOutput(true);
84         conn.setRequestMethod(action);
85         conn.setRequestProperty("Authorization", basicAuth);
86         conn.setRequestProperty("Content-Type", "application/json");
87         conn.connect();
88         return conn;
89     }
90
91     public String readResponse(InputStreamReader response) throws GnpyException {
92         String output = null;
93         BufferedReader br = new BufferedReader(response);
94         String line;
95         StringBuilder sb = new StringBuilder();
96         try {
97             while ((line = br.readLine()) != null) {
98                 sb.append(line);
99             }
100             output = sb.toString();
101         } catch (IOException e) {
102             throw new GnpyException("In connectToGnpyserver: could not read response",e);
103         }
104         return output;
105     }
106 }