blob: 5152b0436fd4a02697f5123aae64bd92f312c6ca [file] [log] [blame]
Pingping Lin53ae34f2015-06-09 10:07:08 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Pingping Lin53ae34f2015-06-09 10:07:08 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.onosproject.virtualbng;
18
Pingping Lin53ae34f2015-06-09 10:07:08 -070019import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Pingping Line10ece02015-06-11 19:26:24 -070022import org.onlab.packet.IpAddress;
Pingping Lin53ae34f2015-06-09 10:07:08 -070023import org.slf4j.Logger;
24
Jian Li9d616492016-03-09 10:52:49 -080025import javax.ws.rs.client.Client;
26import javax.ws.rs.client.ClientBuilder;
27import javax.ws.rs.client.Invocation;
28import javax.ws.rs.client.WebTarget;
29import javax.ws.rs.core.Response;
30import java.io.IOException;
31
32import static com.google.common.net.MediaType.JSON_UTF_8;
33import static java.net.HttpURLConnection.HTTP_OK;
34import static org.slf4j.LoggerFactory.getLogger;
35
Pingping Lin53ae34f2015-06-09 10:07:08 -070036public class RestClient {
37 private final Logger log = getLogger(getClass());
Pingping Lin53ae34f2015-06-09 10:07:08 -070038 private static final String UTF_8 = JSON_UTF_8.toString();
39 private static final ObjectMapper MAPPER = new ObjectMapper();
Pingping Line10ece02015-06-11 19:26:24 -070040 private final String url;
Pingping Lin53ae34f2015-06-09 10:07:08 -070041
42 /**
Pingping Line10ece02015-06-11 19:26:24 -070043 * Constructor.
44 *
45 * @param xosServerIpAddress the IP address of the XOS server
46 * @param xosServerPort the port for the REST service on XOS server
47 */
48 RestClient(IpAddress xosServerIpAddress, int xosServerPort) {
49 this.url = "http://" + xosServerIpAddress.toString() + ":"
50 + xosServerPort + "/xoslib/rs/vbng_mapping/";
51 }
52 /**
Pingping Lin53ae34f2015-06-09 10:07:08 -070053 * Gets a client web resource builder.
54 *
Jian Li9d616492016-03-09 10:52:49 -080055 * @param localUrl the URL to access remote resource
Pingping Lin53ae34f2015-06-09 10:07:08 -070056 * @return web resource builder
57 */
Jian Li9d616492016-03-09 10:52:49 -080058 public Invocation.Builder getClientBuilder(String localUrl) {
59 log.info("URL: {}", localUrl);
60 Client client = ClientBuilder.newClient();
61 WebTarget wt = client.target(localUrl);
62 return wt.request(UTF_8);
Pingping Lin53ae34f2015-06-09 10:07:08 -070063 }
64
65 /**
66 * Builds a REST client and fetches XOS mapping data in JSON format.
67 *
68 * @return the vBNG map if REST GET succeeds, otherwise return null
69 */
70 public ObjectNode getRest() {
Jian Li9d616492016-03-09 10:52:49 -080071 Invocation.Builder builder = getClientBuilder(url);
72 Response response = builder.get();
Pingping Lin53ae34f2015-06-09 10:07:08 -070073
74 if (response.getStatus() != HTTP_OK) {
75 log.info("REST GET request returned error code {}",
76 response.getStatus());
77 return null;
78 }
79
Jian Li9d616492016-03-09 10:52:49 -080080 String jsonString = builder.get(String.class);
Pingping Lin53ae34f2015-06-09 10:07:08 -070081 log.info("Fetched JSON string: {}", jsonString);
82
83 JsonNode node;
84 try {
85 node = MAPPER.readTree(jsonString);
86 } catch (IOException e) {
87 log.error("Failed to read JSON string", e);
88 return null;
89 }
90
91 return (ObjectNode) node;
92 }
93}