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 java.nio.charset.StandardCharsets;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Class to connect to GNPy tool.
27 * @author Ahmed Triki ( ahmed.triki@orange.com )
31 public class ConnectToGnpyServer {
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";
37 public String returnGnpyResponse(String jsonTxt) throws GnpyException {
38 String jsonRespTxt = null;
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));
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()));
50 InputStreamReader response = new InputStreamReader((conn.getInputStream()),StandardCharsets.UTF_8);
51 if (response.ready()) {
52 jsonRespTxt = CharStreams.toString(response);
55 } catch (IOException e) {
56 throw new GnpyException("In connectToGnpyServer: excpetion",e);
61 public boolean isGnpyURLExist() {
62 boolean exist = false;
64 HttpURLConnection conn = connectToGNPy("HEAD");
66 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
67 LOG.info("In connectToGnpyServer: Gnpy instance is connected to T-PCE");
72 catch (IOException e) {
73 LOG.warn("In connectToGnpyserver: could not connect to GNPy server {}",e.getMessage());
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");
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);