blob: bd9f5aeb45b55e1f35f7c469292fe7a0285e8430 [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 }
123 String jsonString = response.getEntity(String.class);
124 log.info("JSON read:\n{}", jsonString);
125
126 return jsonString;
127 }
128
129 /**
130 * Performs a REST PUT operation on the base XOS REST URI.
131 *
132 * @return JSON string returned by the PUT operation
133 */
134 public String putRest() {
135 return putRest("");
136 }
137
138 /**
139 * Performs a REST PUT operation on the base XOS REST URI with
140 * an optional additional URI.
141 *
142 * @param uri URI suffix to append to base URI
143 * @return JSON string returned by the PUT operation
144 */
145 public String putRest(String uri) {
146 WebResource.Builder builder = getClientBuilder(uri);
147 ClientResponse response;
148
149 try {
150 response = builder.put(ClientResponse.class);
151 } catch (ClientHandlerException e) {
152 log.warn("Unable to contact REST server: {}", e.getMessage());
153 return "";
154 }
155
156 if (response.getStatus() != HTTP_OK) {
157 log.info("REST PUT request returned error code {}",
158 response.getStatus());
159 }
160 String jsonString = response.getEntity(String.class);
161 log.info("JSON read:\n{}", jsonString);
162
163 return jsonString;
164 }
165
166 /**
167 * Performs a REST POST operation of a json string on the base
168 * XOS REST URI with an optional additional URI.
169 *
170 * @param json JSON string to post
171 */
172 public void postRest(String json) {
173 postRest("", json);
174 }
175
176 /**
177 * Performs a REST POST operation of a json string on the base
178 * XOS REST URI with an optional additional URI suffix.
179 *
180 * @param uri URI suffix to append to base URI
181 * @param json JSON string to post
182 */
183 public void postRest(String uri, String json) {
184 WebResource.Builder builder = getClientBuilder(uri);
185 ClientResponse response;
186
187 try {
188 response = builder.post(ClientResponse.class, json);
189 } catch (ClientHandlerException e) {
190 log.warn("Unable to contact REST server: {}", e.getMessage());
191 return;
192 }
193
194 if (response.getStatus() != HTTP_CREATED) {
195 log.info("REST POST request returned error code {}",
196 response.getStatus());
197 }
198 }
199
200 /**
201 * Performs a REST DELETE operation on the base
202 * XOS REST URI with an optional additional URI.
203 *
204 * @param uri URI suffix to append to base URI
205 */
206 public void deleteRest(String uri) {
207 WebResource.Builder builder = getClientBuilder(uri);
208 ClientResponse response = builder.delete(ClientResponse.class);
209
210 if (response.getStatus() != HTTP_NO_CONTENT) {
211 log.info("REST DELETE request returned error code {}",
212 response.getStatus());
213 }
214 }
215
216}