Merge "Consolidation of the GNPy 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 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
34     public String gnpyCnx(String jsonTxt) throws GnpyException  {
35         String jsonRespTxt = null;
36
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 GnpyException(String.format(
54                     "In connectToGnpyServer: could not connect to GNPy - response code: %s",conn.getResponseCode()));
55             }
56             InputStreamReader response = new InputStreamReader((conn.getInputStream()));
57             if (response != null) {
58                 jsonRespTxt = CharStreams.toString(response);
59             }
60             conn.disconnect();
61         } catch (IOException e) {
62             throw new GnpyException("In connectToGnpyServer: excpetion",e);
63         }
64         return jsonRespTxt;
65     }
66
67     public boolean isGnpyURLExist() {
68         boolean exist = false;
69         try {
70             URL url = new URL(URL_GNPY);
71             String userCredentials = "gnpy:gnpy";
72             String basicAuth = "Basic " + new String(java.util.Base64.getEncoder().encode(userCredentials.getBytes()));
73             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
74             conn.setDoOutput(true);
75             conn.setRequestMethod("HEAD");
76             conn.setRequestProperty("Authorization", basicAuth);
77             conn.setRequestProperty("Content-Type", "application/json");
78             conn.connect();
79             if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
80                 LOG.info("In connectToGnpyServer: Gnpy instance is connected to T-PCE");
81                 exist = true;
82             }
83             conn.disconnect();
84         }
85         catch (IOException e) {
86             LOG.warn("In connectToGnpyserver: could not connect to GNPy server {}",e.getMessage());
87             return exist;
88         }
89         return exist;
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 }