Bug 4988: OF statistics & REST client
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / statistics / JsonRestClient.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics;
9
10 import javax.ws.rs.core.MediaType;
11 import javax.ws.rs.core.MultivaluedMap;
12
13 import com.google.common.base.Preconditions;
14 import com.sun.jersey.api.client.Client;
15 import com.sun.jersey.api.client.ClientHandlerException;
16 import com.sun.jersey.api.client.ClientResponse;
17 import com.sun.jersey.api.client.WebResource;
18 import com.sun.jersey.api.client.config.ClientConfig;
19 import com.sun.jersey.api.client.config.DefaultClientConfig;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class JsonRestClient {
24
25     private static final Logger LOG = LoggerFactory.getLogger(JsonRestClient.class);
26
27     private String uri;
28     private ClientConfig clientConfig;
29     private Client client;
30     private WebResource webResource;
31
32     public JsonRestClient(String uri, Integer connectTimeout, Integer readTimeout) {
33         Preconditions.checkNotNull(uri);
34
35         this.uri = uri;
36         clientConfig = new DefaultClientConfig();
37         clientConfig.getProperties()
38                 .put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, connectTimeout);
39         clientConfig.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, readTimeout);
40
41         client = Client.create(clientConfig);
42         webResource = client.resource(this.uri);
43     }
44
45     public String getHost() {
46         return webResource.getURI().getHost();
47     }
48
49     public JsonRestClientResponse get(String path) throws ClientHandlerException {
50         return get(path, null);
51     }
52
53     public JsonRestClientResponse get(String path, MultivaluedMap<String, String> params)
54             throws ClientHandlerException {
55         ClientResponse response;
56         WebResource r = this.webResource.path(path);
57         if (params == null) {
58             response = r.accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
59         } else {
60             response = r.queryParams(params)
61                     .accept(MediaType.APPLICATION_JSON_TYPE)
62                     .get(ClientResponse.class);
63         }
64         return new JsonRestClientResponse(response);
65     }
66
67     public JsonRestClientResponse post(String path, String someJson) throws ClientHandlerException {
68         ClientResponse response;
69         response = webResource.path(path)
70                 .accept(MediaType.APPLICATION_JSON_TYPE)
71                 .type(MediaType.APPLICATION_JSON_TYPE)
72                 .post(ClientResponse.class, someJson);
73         return new JsonRestClientResponse(response);
74     }
75
76     public JsonRestClientResponse put(String path, String someJson) throws ClientHandlerException {
77         ClientResponse response;
78         response = webResource.path(path)
79                 .accept(MediaType.APPLICATION_JSON_TYPE)
80                 .type(MediaType.APPLICATION_JSON_TYPE)
81                 .put(ClientResponse.class, someJson);
82         return new JsonRestClientResponse(response);
83     }
84
85     public JsonRestClientResponse delete(String path) throws ClientHandlerException {
86         ClientResponse response;
87         response = webResource.path(path)
88                 .accept(MediaType.APPLICATION_JSON_TYPE)
89                 .delete(ClientResponse.class);
90         return new JsonRestClientResponse(response);
91     }
92
93 }