2 * Copyright © 2019 Orange, Inc. and others. All rights reserved.
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
9 package org.opendaylight.transportpce.pce.gnpy;
11 import com.google.common.io.CharStreams;
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;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Class to connect to GNPy tool.
25 * @author Ahmed Triki ( ahmed.triki@orange.com )
29 public class ConnectToGnpyServer {
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";
34 public String gnpyCnx(String jsonTxt) throws GnpyException {
35 String jsonRespTxt = null;
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()));
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");
48 // Send the request to the GNPy
49 OutputStream os = conn.getOutputStream();
50 os.write(jsonTxt.getBytes());
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()));
56 InputStreamReader response = new InputStreamReader((conn.getInputStream()));
57 if (response != null) {
58 jsonRespTxt = CharStreams.toString(response);
61 } catch (IOException e) {
62 throw new GnpyException("In connectToGnpyServer: excpetion",e);
67 public boolean isGnpyURLExist() {
68 boolean exist = false;
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");
79 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
80 LOG.info("In connectToGnpyServer: Gnpy instance is connected to T-PCE");
85 catch (IOException e) {
86 LOG.warn("In connectToGnpyserver: could not connect to GNPy server {}",e.getMessage());
92 public String readResponse(InputStreamReader response) throws GnpyException {
94 BufferedReader br = new BufferedReader(response);
96 StringBuilder sb = new StringBuilder();
98 while ((line = br.readLine()) != null) {
101 output = sb.toString();
102 } catch (IOException e) {
103 throw new GnpyException("In connectToGnpyserver: could not read response",e);