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 static com.google.common.base.Preconditions.checkNotNull;
12 import java.lang.reflect.Type;
13 import java.math.BigInteger;
14 import java.util.Date;
15 import java.util.List;
17 import javax.ws.rs.core.MultivaluedMap;
19 import org.opendaylight.groupbasedpolicy.api.StatisticsManager;
20 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.flowcache.FlowCacheData;
21 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.util.SFlowQueryParams;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.gson.Gson;
26 import com.google.gson.reflect.TypeToken;
27 import com.sun.jersey.core.util.MultivaluedMapImpl;
29 public class ReadGbpFlowCacheTask implements Runnable {
31 private static final Logger LOG = LoggerFactory.getLogger(ReadGbpFlowCacheTask.class);
33 private static final Type LIST_OF_FLOW_CACHE_DATA = new TypeToken<List<FlowCacheData>>() {}.getType();
34 private static final Gson GSON = new Gson();
36 private final SFlowRTConnection sFlowRTConnection;
37 private final StatisticsManager statisticsManager;
38 private final String maxFlows;
39 private final String minValue;
40 private final String aggMode;
41 private final String path;
43 public ReadGbpFlowCacheTask(String flowCacheName, SFlowRTConnection sFlowRTConnection,
44 StatisticsManager statisticsManager, Integer maxFlows, Double minValue, String aggMode) {
45 this.path = "/activeflows/ALL/" + checkNotNull(flowCacheName) + "/json";
46 this.sFlowRTConnection = checkNotNull(sFlowRTConnection);
47 this.statisticsManager = checkNotNull(statisticsManager);
48 this.maxFlows = String.valueOf(checkNotNull(maxFlows));
49 this.minValue = String.valueOf(checkNotNull(minValue));
50 this.aggMode = checkNotNull(aggMode);
55 MultivaluedMap<String, String> params = new MultivaluedMapImpl();
56 params.add(SFlowQueryParams.MAX_FLOWS, maxFlows);
57 params.add(SFlowQueryParams.MIN_VALUE, minValue);
58 params.add(SFlowQueryParams.AGG_MODE, aggMode);
59 JsonRestClientResponse result = sFlowRTConnection.get(path, params);
61 if (result != null && result.getJsonResponse() != null) {
62 List<FlowCacheData> dataList = GSON.fromJson(result.getJsonResponse(), LIST_OF_FLOW_CACHE_DATA);
63 if (dataList == null) {
64 LOG.trace("Empty reply, not processing");
67 if (LOG.isTraceEnabled()) {
68 LOG.trace("Got sFlow reply: {}", result.getJsonResponse());
71 if (result.getStatusCode() < 300) {
72 sFlowRTConnection.getExecutor().execute((new ProcessDataTask(sFlowRTConnection.getFlowCache(), dataList,
73 BigInteger.valueOf(new Date().getTime()), statisticsManager)));
74 } else if (result.getStatusCode() < 400) {
75 LOG.warn("Status code {}, not processing data. Response: {}", result.getStatusCode(),
76 result.getClientResponse().toString());
78 LOG.error("Status code {}, not processing data. Response: {}", result.getStatusCode(),
79 result.getClientResponse().toString());