fix import extra separations
[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
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         return jsonRespTxt;
57     }
58
59     public boolean isGnpyURLExist() {
60         boolean exist = false;
61         try {
62             HttpURLConnection conn = connectToGNPy("HEAD");
63             conn.connect();
64             if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
65                 LOG.info("In connectToGnpyServer: Gnpy instance is connected to T-PCE");
66                 exist = true;
67             }
68             conn.disconnect();
69         }
70         catch (IOException e) {
71             LOG.warn("In connectToGnpyserver: could not connect to GNPy server {}",e.getMessage());
72             return exist;
73         }
74         return exist;
75     }
76
77     private HttpURLConnection connectToGNPy(String action) throws IOException {
78         URL url = new URL(URL_GNPY);
79         String basicAuth = "Basic " + new String(java.util.Base64.getEncoder()
80             .encode(USER_CRED.getBytes(StandardCharsets.UTF_8)),StandardCharsets.UTF_8);
81         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
82         conn.setDoOutput(true);
83         conn.setRequestMethod(action);
84         conn.setRequestProperty("Authorization", basicAuth);
85         conn.setRequestProperty("Content-Type", "application/json");
86         conn.connect();
87         return conn;
88     }
89
90     public String readResponse(InputStreamReader response) throws GnpyException {
91         String output = null;
92         BufferedReader br = new BufferedReader(response);
93         String line;
94         StringBuilder sb = new StringBuilder();
95         try {
96             while ((line = br.readLine()) != null) {
97                 sb.append(line);
98             }
99             output = sb.toString();
100         } catch (IOException e) {
101             throw new GnpyException("In connectToGnpyserver: could not read response",e);
102         }
103         return output;
104     }
105 }