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;
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;
18 import java.nio.charset.StandardCharsets;
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";
33 static final String USER_CRED = "gnpy:gnpy";
35 public String returnGnpyResponse(String jsonTxt) throws GnpyException {
36 String jsonRespTxt = null;
37 LOG.debug("Sending request {}",jsonTxt);
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));
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()));
48 InputStreamReader response = new InputStreamReader((conn.getInputStream()),StandardCharsets.UTF_8);
49 if (response.ready()) {
50 jsonRespTxt = CharStreams.toString(response);
53 } catch (IOException e) {
54 throw new GnpyException("In connectToGnpyServer: excpetion",e);
56 LOG.debug("Receiving response {}",jsonRespTxt);
60 public boolean isGnpyURLExist() {
61 boolean exist = false;
63 HttpURLConnection conn = connectToGNPy("HEAD");
65 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
66 LOG.info("In connectToGnpyServer: Gnpy instance is connected to T-PCE");
71 catch (IOException e) {
72 LOG.warn("In connectToGnpyserver: could not connect to GNPy server {}",e.getMessage());
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");
91 public String readResponse(InputStreamReader response) throws GnpyException {
93 BufferedReader br = new BufferedReader(response);
95 StringBuilder sb = new StringBuilder();
97 while ((line = br.readLine()) != null) {
100 output = sb.toString();
101 } catch (IOException e) {
102 throw new GnpyException("In connectToGnpyserver: could not read response",e);