2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics;
10 import javax.annotation.Nullable;
11 import javax.ws.rs.core.MultivaluedMap;
12 import java.util.concurrent.ScheduledExecutorService;
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;
20 public class SFlowRTConnection {
22 private static final Logger LOG = LoggerFactory.getLogger(SFlowRTConnection.class);
24 private static final String GET = "GET";
25 private static final String PUT = "PUT";
26 private static final String DELETE = "DELETE";
28 static final String EX_MSG_NOT_INITIALIZED = "SFlowRTConnection is not initialized.";
30 private final FlowCache flowCache;
31 private JsonRestClient client;
32 private boolean isInitialized = false;
33 private final ScheduledExecutorService executor;
34 private final String collectorUri;
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);
45 public String getJsonResponse(String path, MultivaluedMap<String, String> params) {
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);
58 public JsonRestClientResponse get(String path,
59 MultivaluedMap<String, String> params) {
60 if (!isInitialized()) {
61 throw new IllegalStateException(EX_MSG_NOT_INITIALIZED);
64 JsonRestClientResponse responce = client.get(path, params);
65 Preconditions.checkNotNull(responce);
67 } catch (ClientHandlerException e) {
68 processClientHandlerException(e);
74 public JsonRestClientResponse put(String path, String someJson) {
75 if (!isInitialized()) {
76 throw new IllegalStateException(EX_MSG_NOT_INITIALIZED);
78 return putWithoutInitCheck(path, someJson);
81 private JsonRestClientResponse putWithoutInitCheck(String path,
84 JsonRestClientResponse responce = client.put(path, someJson);
85 Preconditions.checkNotNull(responce);
86 logStatusCode(PUT, responce.getStatusCode(), path, null);
88 } catch (ClientHandlerException e) {
89 processClientHandlerException(e);
94 public JsonRestClientResponse delete(String path) {
95 if (!isInitialized()) {
96 throw new IllegalStateException(EX_MSG_NOT_INITIALIZED);
99 JsonRestClientResponse responce = client.delete(path);
100 Preconditions.checkNotNull(responce);
101 logStatusCode(DELETE, responce.getStatusCode(), path, null);
103 } catch (ClientHandlerException e) {
104 processClientHandlerException(e);
109 public boolean isInitialized() {
110 return isInitialized;
113 public FlowCache getFlowCache() {
117 public ScheduledExecutorService getExecutor() {
121 public String getCollectorUri() {
125 public void initialize() {
126 if (LOG.isTraceEnabled()) {
127 LOG.trace("Initializing flow {}", flowCache);
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());
137 LOG.error("sFlow connection {} initialization status code {}", getCollectorUri(), initResp.getStatusCode());
139 this.isInitialized = true;
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;
152 private void logStatusCode(String verb, int status, String path,
153 MultivaluedMap<String, String> params) {
154 if (params != null) {
156 LOG.trace("Query {} {} with params {} returned status {}", verb, path, params,
158 } else if (status < 400) {
159 LOG.warn("Query {} {} with params {} returned status {}", verb, path, params,
162 LOG.error("Query {} {} with params {} returned status {}", verb, path, params,
167 LOG.trace("Query {} {} returned status {}", verb, path, status);
168 } else if (status < 400) {
169 LOG.warn("Query {} {} returned status {}", verb, path, status);
171 LOG.error("Query {} {} returned status {}", verb, path, status);