Bug 4988: OF statistics & REST client
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / statistics / ReadGbpFlowCacheTask.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 static com.google.common.base.Preconditions.checkNotNull;
11
12 import java.lang.reflect.Type;
13 import java.math.BigInteger;
14 import java.util.Date;
15 import java.util.List;
16
17 import javax.ws.rs.core.MultivaluedMap;
18
19 import org.opendaylight.groupbasedpolicy.api.StatisticsManager;
20 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.flowcache.FlowCacheData;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.gson.Gson;
25 import com.google.gson.reflect.TypeToken;
26 import com.sun.jersey.core.util.MultivaluedMapImpl;
27
28 public class ReadGbpFlowCacheTask implements Runnable {
29
30     private static final Logger LOG = LoggerFactory.getLogger(ReadGbpFlowCacheTask.class);
31
32     private static final Type LIST_OF_FLOW_CACHE_DATA = new TypeToken<List<FlowCacheData>>() {}.getType();
33     private static final Gson GSON = new Gson();
34     private static final String MAX_FLOWS_PARAM = "maxFlows";
35     private static final String MIN_VALUE_PARAM = "minValue";
36     private static final String AGG_MODE_PARAM = "aggMode";
37
38     private final SFlowRTConnection sFlowRTConnection;
39     private final StatisticsManager statisticsManager;
40     private final String maxFlows;
41     private final String minValue;
42     private final String aggMode;
43     private final String path;
44
45     public ReadGbpFlowCacheTask(String flowCacheName, SFlowRTConnection sFlowRTConnection,
46             StatisticsManager statisticsManager, Integer maxFlows, Double minValue, String aggMode) {
47         this.path = "/activeflows/ALL/" + checkNotNull(flowCacheName) + "/json";
48         this.sFlowRTConnection = checkNotNull(sFlowRTConnection);
49         this.statisticsManager = checkNotNull(statisticsManager);
50         this.maxFlows = String.valueOf(checkNotNull(maxFlows));
51         this.minValue = String.valueOf(checkNotNull(minValue));
52         this.aggMode = checkNotNull(aggMode);
53     }
54
55     @Override
56     public void run() {
57         MultivaluedMap<String, String> params = new MultivaluedMapImpl();
58         params.add(MAX_FLOWS_PARAM, maxFlows);
59         params.add(MIN_VALUE_PARAM, minValue);
60         params.add(AGG_MODE_PARAM, aggMode);
61         JsonRestClientResponse result = sFlowRTConnection.get(path, params);
62
63         if (result != null && result.getJsonResponse() != null) {
64             List<FlowCacheData> dataList = GSON.fromJson(result.getJsonResponse(), LIST_OF_FLOW_CACHE_DATA);
65             if (dataList == null) {
66                 LOG.trace("Empty reply, not processing");
67                 return;
68             }
69             if (LOG.isTraceEnabled()) {
70                 LOG.trace("Got sFlow reply: {}", result.getJsonResponse());
71             }
72
73             if (result.getStatusCode() < 300) {
74                 sFlowRTConnection.getExecutor().execute((new ProcessDataTask(sFlowRTConnection.getFlowCache(), dataList,
75                         BigInteger.valueOf(new Date().getTime()), statisticsManager)));
76             } else if (result.getStatusCode() < 400) {
77                 LOG.warn("Status code {}, not processing data. Response: {}", result.getStatusCode(),
78                         result.getClientResponse().toString());
79             } else {
80                 LOG.error("Status code {}, not processing data. Response: {}", result.getStatusCode(),
81                         result.getClientResponse().toString());
82             }
83         }
84     }
85 }