Unit tests for ofoverlay
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / statistics / SFlowRTConnection.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.annotation.Nullable;
11 import javax.ws.rs.core.MultivaluedMap;
12 import java.util.concurrent.ScheduledExecutorService;
13
14 import com.google.common.base.Preconditions;
15 import com.sun.jersey.api.client.ClientHandlerException;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.flowcache.FlowCache;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class SFlowRTConnection {
21
22     private static final Logger LOG = LoggerFactory.getLogger(SFlowRTConnection.class);
23
24     private static final String GET = "GET";
25     private static final String PUT = "PUT";
26     private static final String DELETE = "DELETE";
27
28     static final String EX_MSG_NOT_INITIALIZED = "SFlowRTConnection is not initialized.";
29
30     private final FlowCache flowCache;
31     private JsonRestClient client;
32     private boolean isInitialized = false;
33     private final ScheduledExecutorService executor;
34     private final String collectorUri;
35
36     public SFlowRTConnection(ScheduledExecutorService executor, String collectorUri, FlowCache flowCache, JsonRestClient client) {
37         this.executor = Preconditions.checkNotNull(executor);
38         this.collectorUri = Preconditions.checkNotNull(collectorUri);
39         this.flowCache = Preconditions.checkNotNull(flowCache);
40         this.client = client;
41         initialize();
42     }
43
44     @Nullable
45     public String getJsonResponse(String path, MultivaluedMap<String, String> params) {
46         try {
47             JsonRestClientResponse responce = client.get(path, params);
48             Preconditions.checkNotNull(responce);
49             logStatusCode(GET, responce.getStatusCode(), path, params);
50             return responce.getJsonResponse();
51         } catch (ClientHandlerException e) {
52             processClientHandlerException(e);
53         }
54         return null;
55     }
56
57     @Nullable
58     public JsonRestClientResponse get(String path,
59             MultivaluedMap<String, String> params) {
60         if (!isInitialized()) {
61             throw new IllegalStateException(EX_MSG_NOT_INITIALIZED);
62         }
63         try {
64             JsonRestClientResponse responce = client.get(path, params);
65             Preconditions.checkNotNull(responce);
66             return responce;
67         } catch (ClientHandlerException e) {
68             processClientHandlerException(e);
69         }
70         return null;
71     }
72
73     @Nullable
74     public JsonRestClientResponse put(String path, String someJson) {
75         if (!isInitialized()) {
76             throw new IllegalStateException(EX_MSG_NOT_INITIALIZED);
77         }
78         return putWithoutInitCheck(path, someJson);
79     }
80
81     private JsonRestClientResponse putWithoutInitCheck(String path,
82             String someJson) {
83         try {
84             JsonRestClientResponse responce = client.put(path, someJson);
85             Preconditions.checkNotNull(responce);
86             logStatusCode(PUT, responce.getStatusCode(), path, null);
87             return responce;
88         } catch (ClientHandlerException e) {
89             processClientHandlerException(e);
90         }
91         return null;
92     }
93
94     public JsonRestClientResponse delete(String path) {
95         if (!isInitialized()) {
96             throw new IllegalStateException(EX_MSG_NOT_INITIALIZED);
97         }
98         try {
99             JsonRestClientResponse responce = client.delete(path);
100             Preconditions.checkNotNull(responce);
101             logStatusCode(DELETE, responce.getStatusCode(), path, null);
102             return responce;
103         } catch (ClientHandlerException e) {
104             processClientHandlerException(e);
105         }
106         return null;
107     }
108
109     public boolean isInitialized() {
110         return isInitialized;
111     }
112
113     public FlowCache getFlowCache() {
114         return flowCache;
115     }
116
117     public ScheduledExecutorService getExecutor() {
118         return executor;
119     }
120
121     public String getCollectorUri() {
122         return collectorUri;
123     }
124
125     public void initialize() {
126         if (LOG.isTraceEnabled()) {
127             LOG.trace("Initializing flow {}", flowCache);
128         }
129         JsonRestClientResponse initResp =
130                 putWithoutInitCheck(flowCache.getPath(), flowCache.getJsonDefinition());
131         Preconditions.checkNotNull(initResp);
132         if (initResp.getStatusCode() < 300) {
133             LOG.info("sFlow connection {} initialization status code {}", getCollectorUri(), initResp.getStatusCode());
134         } else if (initResp.getStatusCode() < 400) {
135             LOG.warn("sFlow connection {} initialization status code {}", getCollectorUri(), initResp.getStatusCode());
136         } else {
137             LOG.error("sFlow connection {} initialization status code {}", getCollectorUri(), initResp.getStatusCode());
138         }
139         this.isInitialized = true;
140     }
141
142     private void processClientHandlerException(ClientHandlerException e) {
143         if (e.getCause() instanceof java.net.SocketTimeoutException || e.getCause() instanceof java.net.ConnectException) {
144             LOG.error("Connection to {} failed: {}", client.getHost(), e.getMessage());
145             this.isInitialized = false;
146             throw e;
147         } else {
148             throw e;
149         }
150     }
151
152     private void logStatusCode(String verb, int status, String path,
153             MultivaluedMap<String, String> params) {
154         if (params != null) {
155             if (status <= 204) {
156                 LOG.trace("Query {} {} with params {} returned status {}", verb, path, params,
157                         status);
158             } else if (status < 400) {
159                 LOG.warn("Query {} {} with params {} returned status {}", verb, path, params,
160                         status);
161             } else {
162                 LOG.error("Query {} {} with params {} returned status {}", verb, path, params,
163                         status);
164             }
165         } else {
166             if (status <= 204) {
167                 LOG.trace("Query {} {} returned status {}", verb, path, status);
168             } else if (status < 400) {
169                 LOG.warn("Query {} {} returned status {}", verb, path, status);
170             } else {
171                 LOG.error("Query {} {} returned status {}", verb, path, status);
172             }
173         }
174     }
175
176 }