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.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.gson.Gson;
25 import com.google.gson.reflect.TypeToken;
26 import com.sun.jersey.core.util.MultivaluedMapImpl;
28 public class ReadGbpFlowCacheTask implements Runnable {
30 private static final Logger LOG = LoggerFactory.getLogger(ReadGbpFlowCacheTask.class);
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";
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;
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);
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);
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");
69 if (LOG.isTraceEnabled()) {
70 LOG.trace("Got sFlow reply: {}", result.getJsonResponse());
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());
80 LOG.error("Status code {}, not processing data. Response: {}", result.getStatusCode(),
81 result.getClientResponse().toString());