1 package org.openkilda.service;
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;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Service;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.type.TypeFactory;
40 private static final Logger LOGGER = LoggerFactory.getLogger(
StatsService.class);
67 String srcSwitch, String srcPort,
68 String dstSwitch, String dstPort, String metric)
70 return statsIntegrationService.getStats(startDate, endDate, downsample, null, null, null, srcSwitch, srcPort, dstSwitch, dstPort,
StatsType.
ISL, metric, null);
84 public String
getFlowStats(String startDate, String endDate, String downsample, String flowId,
86 return statsIntegrationService.getStats(startDate, endDate, downsample, null, null, flowId, null, null, null, null,
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);
124 String srcSwitch, String srcPort, String dstSwitch, String dstPort, String metric) {
125 return statsIntegrationService.getStats(startDate, endDate, downsample, null, null, null,
143 return statsIntegrationService.getStats(startDate, endDate, downsample, null, null, flowId,
155 return statsIntegrationService.getStats(flowPathStats.
getStartDate(),
174 List<String> switchIds = Arrays.asList(switchId);
175 String
result = statsIntegrationService.getStats(startDate, endDate, downSample, switchIds, null,
177 List<SwitchPortStats> switchPortStats =
new ArrayList<SwitchPortStats>();
179 ObjectMapper mapper =
new ObjectMapper();
180 switchPortStats = mapper.readValue(
result, TypeFactory.defaultInstance()
182 }
catch (IOException e) {
183 LOGGER.error(
"Inside getSwitchPortsStats Exception is: " + e.getMessage());
185 return getSwitchPortStatsReport(switchPortStats, switchId);
194 private List<PortInfo> getSwitchPortStatsReport(List<SwitchPortStats> switchPortStats, String switchId) {
195 Map<String, Map<String, Double>> portStatsByPortNo =
new HashMap<String, Map<String, Double>>();
197 String
port = stats.getTags().getPort();
199 if (Integer.parseInt(
port) > 0) {
200 if (!portStatsByPortNo.containsKey(
port)) {
201 portStatsByPortNo.put(
port,
new HashMap<String, Double>());
203 portStatsByPortNo.get(
port).put(
204 stats.getMetric().replace(
"pen.switch.",
""),
205 calculateHighestValue(stats.getDps()));
209 return getIslPorts(portStatsByPortNo, switchId);
218 private double calculateHighestValue(Map<String, Double> dps) {
220 if (!dps.isEmpty()) {
221 long maxTimestamp = 0;
222 for (String key : dps.keySet()) {
223 long val = Long.parseLong(key);
224 if (maxTimestamp < val) {
228 maxVal = BigDecimal.valueOf(dps.get(String.valueOf(maxTimestamp)))
229 .setScale(2, RoundingMode.HALF_UP).doubleValue();
241 private List<PortInfo> getIslPorts(
final Map<String, Map<String, Double>> portStatsByPortNo,
243 List<PortInfo> portInfos = getPortInfo(portStatsByPortNo);
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");
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");
275 portInfo.setStats(portStats.getValue());
276 portInfos.add(portInfo);
String getFlowStats(String startDate, String endDate, String downsample, String flowId, String metric)
List< String > getSwitches()
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)
String getSwitchIslLossPacketStats(String startDate, String endDate, String downsample, String srcSwitch, String srcPort, String dstSwitch, String dstPort, String metric)