Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -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 | |
| 18 | package org.onosproject.cord.gui; |
| 19 | |
| 20 | import com.sun.jersey.api.client.Client; |
| 21 | import com.sun.jersey.api.client.ClientHandlerException; |
| 22 | import com.sun.jersey.api.client.ClientResponse; |
| 23 | import com.sun.jersey.api.client.WebResource; |
| 24 | import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import static com.google.common.net.MediaType.JSON_UTF_8; |
| 28 | import static java.net.HttpURLConnection.*; |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | /** |
| 32 | * Utility RESTful methods for dealing with the XOS server. |
| 33 | */ |
| 34 | public class XosManagerRestUtils { |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 35 | private static final String XOSLIB = "/xoslib"; |
| 36 | private static final String AUTH_USER = "padmin@vicci.org"; |
| 37 | private static final String AUTH_PASS = "letmein"; |
| 38 | |
| 39 | private static final String UTF_8 = JSON_UTF_8.toString(); |
| 40 | |
| 41 | private final Logger log = getLogger(getClass()); |
| 42 | |
| 43 | private final String xosServerAddress; |
| 44 | private final int xosServerPort; |
| 45 | private final String baseUri; |
| 46 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Constructs a utility class, using the supplied server address and port, |
| 50 | * using the given base URI. |
| 51 | * <p> |
| 52 | * Note that the uri should start and end with a slash; for example: |
| 53 | * {@code "/volttenant/"}. This example would result in URIs of the form: |
| 54 | * <pre> |
| 55 | * "http://[server]:[port]/xoslib/volttenant/" |
| 56 | * </pre> |
| 57 | * |
| 58 | * @param xosServerAddress server IP address |
| 59 | * @param xosServerPort server port |
| 60 | * @param baseUri base URI |
| 61 | */ |
| 62 | public XosManagerRestUtils(String xosServerAddress, int xosServerPort, |
| 63 | String baseUri) { |
| 64 | this.xosServerAddress = xosServerAddress; |
| 65 | this.xosServerPort = xosServerPort; |
| 66 | this.baseUri = baseUri; |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 67 | log.info("XMRU:: {}:{}{}", xosServerAddress, xosServerPort, baseUri); |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // build the base URL from the pieces we know... |
| 71 | private String baseUrl() { |
| 72 | return "http://" + xosServerAddress + ":" + |
| 73 | Integer.toString(xosServerPort) + XOSLIB + baseUri; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets a client web resource builder for the base XOS REST API |
| 78 | * with no additional URI. |
| 79 | * |
| 80 | * @return web resource builder |
| 81 | */ |
| 82 | public WebResource.Builder getClientBuilder() { |
| 83 | return getClientBuilder(""); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Gets a client web resource builder for the base XOS REST API |
| 88 | * with an optional additional URI. |
| 89 | * |
| 90 | * @param uri URI suffix to append to base URI |
| 91 | * @return web resource builder |
| 92 | */ |
| 93 | public WebResource.Builder getClientBuilder(String uri) { |
| 94 | Client client = Client.create(); |
| 95 | client.addFilter(new HTTPBasicAuthFilter(AUTH_USER, AUTH_PASS)); |
| 96 | WebResource resource = client.resource(baseUrl() + uri); |
Simon Hunt | 248486d | 2015-06-08 10:56:18 -0700 | [diff] [blame] | 97 | log.info("XOS REST CALL>> {}", resource); |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 98 | return resource.accept(UTF_8).type(UTF_8); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Performs a REST GET operation on the base XOS REST URI. |
| 103 | * |
| 104 | * @return JSON string fetched by the GET operation |
| 105 | */ |
| 106 | public String getRest() { |
| 107 | return getRest(""); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Performs a REST GET operation on the base XOS REST URI with |
| 112 | * an optional additional URI. |
| 113 | * |
| 114 | * @param uri URI suffix to append to base URI |
| 115 | * @return JSON string fetched by the GET operation |
| 116 | */ |
| 117 | public String getRest(String uri) { |
| 118 | WebResource.Builder builder = getClientBuilder(uri); |
| 119 | ClientResponse response = builder.get(ClientResponse.class); |
| 120 | |
| 121 | if (response.getStatus() != HTTP_OK) { |
| 122 | log.info("REST GET request returned error code {}", |
| 123 | response.getStatus()); |
| 124 | } |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 125 | return response.getEntity(String.class); |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Performs a REST PUT operation on the base XOS REST URI. |
| 130 | * |
| 131 | * @return JSON string returned by the PUT operation |
| 132 | */ |
| 133 | public String putRest() { |
| 134 | return putRest(""); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Performs a REST PUT operation on the base XOS REST URI with |
| 139 | * an optional additional URI. |
| 140 | * |
| 141 | * @param uri URI suffix to append to base URI |
| 142 | * @return JSON string returned by the PUT operation |
| 143 | */ |
| 144 | public String putRest(String uri) { |
| 145 | WebResource.Builder builder = getClientBuilder(uri); |
| 146 | ClientResponse response; |
| 147 | |
| 148 | try { |
| 149 | response = builder.put(ClientResponse.class); |
| 150 | } catch (ClientHandlerException e) { |
| 151 | log.warn("Unable to contact REST server: {}", e.getMessage()); |
| 152 | return ""; |
| 153 | } |
| 154 | |
| 155 | if (response.getStatus() != HTTP_OK) { |
| 156 | log.info("REST PUT request returned error code {}", |
| 157 | response.getStatus()); |
| 158 | } |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 159 | return response.getEntity(String.class); |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Performs a REST POST operation of a json string on the base |
| 164 | * XOS REST URI with an optional additional URI. |
| 165 | * |
| 166 | * @param json JSON string to post |
| 167 | */ |
| 168 | public void postRest(String json) { |
| 169 | postRest("", json); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Performs a REST POST operation of a json string on the base |
| 174 | * XOS REST URI with an optional additional URI suffix. |
| 175 | * |
| 176 | * @param uri URI suffix to append to base URI |
| 177 | * @param json JSON string to post |
| 178 | */ |
| 179 | public void postRest(String uri, String json) { |
| 180 | WebResource.Builder builder = getClientBuilder(uri); |
| 181 | ClientResponse response; |
| 182 | |
| 183 | try { |
| 184 | response = builder.post(ClientResponse.class, json); |
| 185 | } catch (ClientHandlerException e) { |
| 186 | log.warn("Unable to contact REST server: {}", e.getMessage()); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (response.getStatus() != HTTP_CREATED) { |
| 191 | log.info("REST POST request returned error code {}", |
| 192 | response.getStatus()); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Performs a REST DELETE operation on the base |
| 198 | * XOS REST URI with an optional additional URI. |
| 199 | * |
| 200 | * @param uri URI suffix to append to base URI |
| 201 | */ |
| 202 | public void deleteRest(String uri) { |
| 203 | WebResource.Builder builder = getClientBuilder(uri); |
| 204 | ClientResponse response = builder.delete(ClientResponse.class); |
| 205 | |
| 206 | if (response.getStatus() != HTTP_NO_CONTENT) { |
| 207 | log.info("REST DELETE request returned error code {}", |
| 208 | response.getStatus()); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | } |