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