Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 17 | package org.onosproject.virtualbng; |
| 18 | |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Pingping Lin | e10ece0 | 2015-06-11 19:26:24 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 25 | import javax.ws.rs.client.Client; |
| 26 | import javax.ws.rs.client.ClientBuilder; |
| 27 | import javax.ws.rs.client.Invocation; |
| 28 | import javax.ws.rs.client.WebTarget; |
| 29 | import javax.ws.rs.core.Response; |
| 30 | import java.io.IOException; |
| 31 | |
| 32 | import static com.google.common.net.MediaType.JSON_UTF_8; |
| 33 | import static java.net.HttpURLConnection.HTTP_OK; |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 36 | public class RestClient { |
| 37 | private final Logger log = getLogger(getClass()); |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 38 | private static final String UTF_8 = JSON_UTF_8.toString(); |
| 39 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
Pingping Lin | e10ece0 | 2015-06-11 19:26:24 -0700 | [diff] [blame] | 40 | private final String url; |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Pingping Lin | e10ece0 | 2015-06-11 19:26:24 -0700 | [diff] [blame] | 43 | * 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 Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 53 | * Gets a client web resource builder. |
| 54 | * |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 55 | * @param localUrl the URL to access remote resource |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 56 | * @return web resource builder |
| 57 | */ |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 58 | 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 Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 63 | } |
| 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 71 | Invocation.Builder builder = getClientBuilder(url); |
| 72 | Response response = builder.get(); |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 73 | |
| 74 | if (response.getStatus() != HTTP_OK) { |
| 75 | log.info("REST GET request returned error code {}", |
| 76 | response.getStatus()); |
| 77 | return null; |
| 78 | } |
| 79 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 80 | String jsonString = builder.get(String.class); |
Pingping Lin | 53ae34f | 2015-06-09 10:07:08 -0700 | [diff] [blame] | 81 | 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 | } |