Merge "Next Topology component"
[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.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.util.SFlowQueryParams;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.Gson;
26 import com.google.gson.reflect.TypeToken;
27 import com.sun.jersey.core.util.MultivaluedMapImpl;
28
29 public class ReadGbpFlowCacheTask implements Runnable {
30
31     private static final Logger LOG = LoggerFactory.getLogger(ReadGbpFlowCacheTask.class);
32
33     private static final Type LIST_OF_FLOW_CACHE_DATA = new TypeToken<List<FlowCacheData>>() {}.getType();
34     private static final Gson GSON = new Gson();
35
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;
42
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);
51     }
52
53     @Override
54     public void run() {
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);
60
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");
65                 return;
66             }
67             if (LOG.isTraceEnabled()) {
68                 LOG.trace("Got sFlow reply: {}", result.getJsonResponse());
69             }
70
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());
77             } else {
78                 LOG.error("Status code {}, not processing data. Response: {}", result.getStatusCode(),
79                         result.getClientResponse().toString());
80             }
81         }
82     }
83 }