blob: daa72d8071e38b7cccaa2d515a28598d95df3ad6 [file] [log] [blame]
Simon Hunt8483e9d2015-05-26 18:22:07 -07001/*
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
18package org.onosproject.cord.gui;
19
20import com.sun.jersey.api.client.Client;
21import com.sun.jersey.api.client.ClientHandlerException;
22import com.sun.jersey.api.client.ClientResponse;
23import com.sun.jersey.api.client.WebResource;
24import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
25import org.slf4j.Logger;
26
27import static com.google.common.net.MediaType.JSON_UTF_8;
28import static java.net.HttpURLConnection.*;
29import static org.slf4j.LoggerFactory.getLogger;
30
31/**
32 * Utility RESTful methods for dealing with the XOS server.
33 */
34public class XosManagerRestUtils {
Simon Hunt8483e9d2015-05-26 18:22:07 -070035 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 Hunt8483e9d2015-05-26 18:22:07 -070047
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;
67 }
68
69 // build the base URL from the pieces we know...
70 private String baseUrl() {
71 return "http://" + xosServerAddress + ":" +
72 Integer.toString(xosServerPort) + XOSLIB + baseUri;
73 }
74
75 /**
76 * Gets a client web resource builder for the base XOS REST API
77 * with no additional URI.
78 *
79 * @return web resource builder
80 */
81 public WebResource.Builder getClientBuilder() {
82 return getClientBuilder("");
83 }
84
85 /**
86 * Gets a client web resource builder for the base XOS REST API
87 * with an optional additional URI.
88 *
89 * @param uri URI suffix to append to base URI
90 * @return web resource builder
91 */
92 public WebResource.Builder getClientBuilder(String uri) {
93 Client client = Client.create();
94 client.addFilter(new HTTPBasicAuthFilter(AUTH_USER, AUTH_PASS));
95 WebResource resource = client.resource(baseUrl() + uri);
96 return resource.accept(UTF_8).type(UTF_8);
97 }
98
99 /**
100 * Performs a REST GET operation on the base XOS REST URI.
101 *
102 * @return JSON string fetched by the GET operation
103 */
104 public String getRest() {
105 return getRest("");
106 }
107
108 /**
109 * Performs a REST GET operation on the base XOS REST URI with
110 * an optional additional URI.
111 *
112 * @param uri URI suffix to append to base URI
113 * @return JSON string fetched by the GET operation
114 */
115 public String getRest(String uri) {
116 WebResource.Builder builder = getClientBuilder(uri);
117 ClientResponse response = builder.get(ClientResponse.class);
118
119 if (response.getStatus() != HTTP_OK) {
120 log.info("REST GET request returned error code {}",
121 response.getStatus());
122 }
Simon Hunt2739e6f82015-06-05 16:27:45 -0700123 return response.getEntity(String.class);
Simon Hunt8483e9d2015-05-26 18:22:07 -0700124 }
125
126 /**
127 * Performs a REST PUT operation on the base XOS REST URI.
128 *
129 * @return JSON string returned by the PUT operation
130 */
131 public String putRest() {
132 return putRest("");
133 }
134
135 /**
136 * Performs a REST PUT operation on the base XOS REST URI with
137 * an optional additional URI.
138 *
139 * @param uri URI suffix to append to base URI
140 * @return JSON string returned by the PUT operation
141 */
142 public String putRest(String uri) {
143 WebResource.Builder builder = getClientBuilder(uri);
144 ClientResponse response;
145
146 try {
147 response = builder.put(ClientResponse.class);
148 } catch (ClientHandlerException e) {
149 log.warn("Unable to contact REST server: {}", e.getMessage());
150 return "";
151 }
152
153 if (response.getStatus() != HTTP_OK) {
154 log.info("REST PUT request returned error code {}",
155 response.getStatus());
156 }
Simon Hunt2739e6f82015-06-05 16:27:45 -0700157 return response.getEntity(String.class);
Simon Hunt8483e9d2015-05-26 18:22:07 -0700158 }
159
160 /**
161 * Performs a REST POST operation of a json string on the base
162 * XOS REST URI with an optional additional URI.
163 *
164 * @param json JSON string to post
165 */
166 public void postRest(String json) {
167 postRest("", json);
168 }
169
170 /**
171 * Performs a REST POST operation of a json string on the base
172 * XOS REST URI with an optional additional URI suffix.
173 *
174 * @param uri URI suffix to append to base URI
175 * @param json JSON string to post
176 */
177 public void postRest(String uri, String json) {
178 WebResource.Builder builder = getClientBuilder(uri);
179 ClientResponse response;
180
181 try {
182 response = builder.post(ClientResponse.class, json);
183 } catch (ClientHandlerException e) {
184 log.warn("Unable to contact REST server: {}", e.getMessage());
185 return;
186 }
187
188 if (response.getStatus() != HTTP_CREATED) {
189 log.info("REST POST request returned error code {}",
190 response.getStatus());
191 }
192 }
193
194 /**
195 * Performs a REST DELETE operation on the base
196 * XOS REST URI with an optional additional URI.
197 *
198 * @param uri URI suffix to append to base URI
199 */
200 public void deleteRest(String uri) {
201 WebResource.Builder builder = getClientBuilder(uri);
202 ClientResponse response = builder.delete(ClientResponse.class);
203
204 if (response.getStatus() != HTTP_NO_CONTENT) {
205 log.info("REST DELETE request returned error code {}",
206 response.getStatus());
207 }
208 }
209
210}