Open Kilda Java Documentation
StatsService.java
Go to the documentation of this file.
1 package org.openkilda.service;
2 
3 import java.io.IOException;
4 import java.math.BigDecimal;
5 import java.math.RoundingMode;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Service;
28 
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.type.TypeFactory;
31 
37 @Service
38 public class StatsService {
39 
40  private static final Logger LOGGER = LoggerFactory.getLogger(StatsService.class);
41 
42  @Autowired
43  private StatsIntegrationService statsIntegrationService;
44 
45  @Autowired
46  private SwitchIntegrationService switchIntegrationService;
47 
66  public String getSwitchIslStats(String startDate, String endDate, String downsample,
67  String srcSwitch, String srcPort,
68  String dstSwitch, String dstPort, String metric)
69  throws IntegrationException {
70  return statsIntegrationService.getStats(startDate, endDate, downsample, null, null, null, srcSwitch, srcPort, dstSwitch, dstPort, StatsType.ISL, metric, null);
71  }
72 
84  public String getFlowStats(String startDate, String endDate, String downsample, String flowId,
85  String metric) throws IntegrationException {
86  return statsIntegrationService.getStats(startDate, endDate, downsample, null, null, flowId, null, null, null, null,
87  StatsType.FLOW, metric, null);
88  }
89 
102  public String getSwitchPortStats(String startDate, String endDate, String downsample,
103  String switchid, String portnumber, String metric) throws IntegrationException {
104  List<String> switchIds = new ArrayList<String>();
105  switchIds.add(switchid);
106  return statsIntegrationService.getStats(startDate, endDate, downsample, switchIds,
107  portnumber, null, null, null, null, null, StatsType.PORT, metric, null);
108  }
109 
123  public String getSwitchIslLossPacketStats(String startDate, String endDate, String downsample,
124  String srcSwitch, String srcPort, String dstSwitch, String dstPort, String metric) {
125  return statsIntegrationService.getStats(startDate, endDate, downsample, null, null, null,
126  srcSwitch, srcPort, dstSwitch, dstPort, StatsType.ISL_LOSS_PACKET, metric, null);
127  }
128 
141  public String getFlowLossPacketStats(String startDate, String endDate, String downsample,
142  String flowId, String direction) throws IntegrationException {
143  return statsIntegrationService.getStats(startDate, endDate, downsample, null, null, flowId,
144  null, null, null, null, StatsType.FLOW_LOSS_PACKET,
145  Metrics.PEN_FLOW_INGRESS_PACKETS.getTag().replace("Flow_", ""), direction);
146  }
147 
154  public String getFlowPathStats(FlowPathStats flowPathStats) {
155  return statsIntegrationService.getStats(flowPathStats.getStartDate(),
156  flowPathStats.getEndDate(), flowPathStats.getDownsample(),
157  flowPathStats.getSwitches(), null, flowPathStats.getFlowid(), null, null, null,
159  Metrics.PEN_FLOW_RAW_PACKETS.getTag().replace("Flow_", ""),
160  flowPathStats.getDirection());
161  }
162 
172  public List<PortInfo> getSwitchPortsStats(String startDate, String endDate, String downSample,
173  String switchId) {
174  List<String> switchIds = Arrays.asList(switchId);
175  String result = statsIntegrationService.getStats(startDate, endDate, downSample, switchIds, null,
176  null, null, null, null, null, StatsType.SWITCH_PORT, null, null);
177  List<SwitchPortStats> switchPortStats = new ArrayList<SwitchPortStats>();
178  try {
179  ObjectMapper mapper = new ObjectMapper();
180  switchPortStats = mapper.readValue(result, TypeFactory.defaultInstance()
181  .constructCollectionLikeType(List.class, SwitchPortStats.class));
182  } catch (IOException e) {
183  LOGGER.error("Inside getSwitchPortsStats Exception is: " + e.getMessage());
184  }
185  return getSwitchPortStatsReport(switchPortStats, switchId);
186  }
187 
194  private List<PortInfo> getSwitchPortStatsReport(List<SwitchPortStats> switchPortStats, String switchId) {
195  Map<String, Map<String, Double>> portStatsByPortNo = new HashMap<String, Map<String, Double>>();
196  for (SwitchPortStats stats : switchPortStats) {
197  String port = stats.getTags().getPort();
198 
199  if (Integer.parseInt(port) > 0) {
200  if (!portStatsByPortNo.containsKey(port)) {
201  portStatsByPortNo.put(port, new HashMap<String, Double>());
202  }
203  portStatsByPortNo.get(port).put(
204  stats.getMetric().replace("pen.switch.", ""),
205  calculateHighestValue(stats.getDps()));
206  }
207  }
208 
209  return getIslPorts(portStatsByPortNo, switchId);
210  }
211 
218  private double calculateHighestValue(Map<String, Double> dps) {
219  double maxVal = 0.0;
220  if (!dps.isEmpty()) {
221  long maxTimestamp = 0;
222  for (String key : dps.keySet()) {
223  long val = Long.parseLong(key);
224  if (maxTimestamp < val) {
225  maxTimestamp = val;
226  }
227  }
228  maxVal = BigDecimal.valueOf(dps.get(String.valueOf(maxTimestamp)))
229  .setScale(2, RoundingMode.HALF_UP).doubleValue();
230  }
231  return maxVal;
232  }
233 
241  private List<PortInfo> getIslPorts(final Map<String, Map<String, Double>> portStatsByPortNo,
242  String switchid) {
243  List<PortInfo> portInfos = getPortInfo(portStatsByPortNo);
244 
245  List<IslLink> islLinkPorts = switchIntegrationService.getIslLinkPortsInfo();
246  String switchIdInfo = null;
247  if (islLinkPorts != null) {
248  for (IslLink islLink : islLinkPorts) {
249  for (IslPath islPath : islLink.getPath()) {
250  switchIdInfo = ("SW" + islPath.getSwitchId().replaceAll(":", "")).toUpperCase();
251  if (switchIdInfo.equals(switchid)) {
252  for (int i = 0; i < portInfos.size(); i++) {
253  if (portInfos.get(i).getPortNumber()
254  .equals(islPath.getPortNo().toString())) {
255  portInfos.get(i).setInterfacetype("ISL");
256  }
257  }
258  }
259  }
260  }
261  }
262  return portInfos;
263  }
264 
265  private List<PortInfo> getPortInfo(final Map<String, Map<String, Double>> portStatsByPortNo) {
266  List<PortInfo> portInfos = new ArrayList<PortInfo>();
267  for (Map.Entry<String, Map<String, Double>> portStats : portStatsByPortNo.entrySet()) {
268  PortInfo portInfo = new PortInfo();
269  portInfo.setPortNumber(portStats.getKey());
270  portInfo.setInterfacetype("PORT");
271  if (portStats.getValue().containsKey("state")) {
272  portInfo.setStatus(portStats.getValue().get("state") == 0 ? Status.DOWN : Status.UP);
273  portStats.getValue().remove("state");
274  }
275  portInfo.setStats(portStats.getValue());
276  portInfos.add(portInfo);
277  }
278  return portInfos;
279  }
280 }
String getFlowStats(String startDate, String endDate, String downsample, String flowId, String metric)
String getSwitchPortStats(String startDate, String endDate, String downsample, String switchid, String portnumber, String metric)
List< PortInfo > getSwitchPortsStats(String startDate, String endDate, String downSample, String switchId)
String getFlowPathStats(FlowPathStats flowPathStats)
String getFlowLossPacketStats(String startDate, String endDate, String downsample, String flowId, String direction)
String getSwitchIslStats(String startDate, String endDate, String downsample, String srcSwitch, String srcPort, String dstSwitch, String dstPort, String metric)
list result
Definition: plan-d.py:72
String getSwitchIslLossPacketStats(String startDate, String endDate, String downsample, String srcSwitch, String srcPort, String dstSwitch, String dstPort, String metric)