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