2 * Copyright (c) 2017 HPE, 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.netvirt.statistics;
10 import com.google.common.collect.ImmutableSet;
11 import java.math.BigInteger;
12 import java.util.Collection;
13 import java.util.HashMap;
15 import java.util.Map.Entry;
17 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public final class CountersUtils {
23 public static final String BYTE_COUNTER_NAME = "byteCount";
24 public static final String BYTES_GROUP_NAME = "Bytes";
25 public static final String BYTES_RECEIVED_COUNTER_NAME = "bytesReceivedCount";
26 public static final String BYTES_TRANSMITTED_COUNTER_NAME = "bytesTransmittedCount";
27 public static final String DURATION_GROUP_NAME = "Duration";
28 public static final String DURATION_NANO_SECOND_COUNTER_NAME = "durationNanoSecondCount";
29 public static final String DURATION_SECOND_COUNTER_NAME = "durationSecondCount";
30 public static final String OF_DELIMITER = ":";
31 public static final String OF_PREFIX = "openflow:";
32 public static final String PACKET_COUNTER_NAME = "packetCount";
33 public static final String PACKETS_GROUP_NAME = "Packets";
34 public static final String PACKETS_RECEIVED_COUNTER_NAME = "packetsReceivedCount";
35 public static final String PACKETS_TRANSMITTED_COUNTER_NAME = "packetsTransmittedCount";
37 public static final String ELEMENT_COUNTERS_IP_FILTER_GROUP_NAME = "ipFilterGroup";
38 public static final String ELEMENT_COUNTERS_TCP_FILTER_GROUP_NAME = "tcpFilterFroup";
39 public static final String ELEMENT_COUNTERS_UDP_FILTER_GROUP_NAME = "udpFilterFroup";
40 public static final String IP_FILTER_NAME = "ip";
41 public static final String TCP_SRC_PORT_FILTER_NAME = "tcpSrcPort";
42 public static final String TCP_DST_PORT_FILTER_NAME = "tcpDstPort";
43 public static final String UDP_SRC_PORT_FILTER_NAME = "udpSrcPort";
44 public static final String UDP_DST_PORT_FILTER_NAME = "udpDstPort";
45 public static final String TCP_FILTER_NAME = "tcp";
46 public static final String UDP_FILTER_NAME = "udp";
48 private static final Logger LOG = LoggerFactory.getLogger(CountersUtils.class);
50 private static final Collection<String> UNACCUMULATED_COUNTER_GROUPS = ImmutableSet.of("Duration");
52 private CountersUtils() {
56 public static String getNodeId(BigInteger dpId) {
57 return IfmConstants.OF_URI_PREFIX + dpId;
60 public static String getNodeConnectorId(BigInteger dpId, String portNumber) {
61 return IfmConstants.OF_URI_PREFIX + dpId + OF_DELIMITER + portNumber;
64 public static CounterResultDataStructure aggregateCounters(CounterResultDataStructure counters,
65 String aggregatedResultId) {
66 CounterResultDataStructure aggregatedCounters = new CounterResultDataStructure();
67 if (counters.isEmpty()) {
71 Set<String> groupNames = counters.getGroupNames();
72 if (groupNames == null || groupNames.isEmpty()) {
76 aggregatedCounters.addCounterResult(aggregatedResultId);
77 for (String groupName : groupNames) {
78 Map<String, BigInteger> aggregatedGroupCounters = aggregateGroupCounters(groupName, counters);
79 aggregatedCounters.addCounterGroup(aggregatedResultId, groupName, aggregatedGroupCounters);
82 return aggregatedCounters;
85 private static Map<String, BigInteger> aggregateGroupCounters(String groupName,
86 CounterResultDataStructure counters) {
87 Set<String> groupCounterNames = counters.getGroupCounterNames(groupName);
88 if (groupCounterNames == null || groupCounterNames.isEmpty()) {
92 Map<String, BigInteger> aggregatedCounters = new HashMap<>();
93 for (String counterName : groupCounterNames) {
94 aggregatedCounters.put(counterName, BigInteger.valueOf(0));
97 for (String counterResultId : counters.getResults().keySet()) {
98 Map<String, BigInteger> currentResultGroup = counters.getGroups(counterResultId).get(groupName);
99 for (Entry<String, BigInteger> entry : currentResultGroup.entrySet()) {
100 String counterName = entry.getKey();
101 BigInteger aggregated = aggregatedCounters.get(counterName);
102 if (aggregated != null) {
103 aggregatedCounters.put(counterName, aggregated.add(entry.getValue()));
105 LOG.warn("missing counter value for: {}", counterName);
109 if (UNACCUMULATED_COUNTER_GROUPS.contains(groupName)) {
113 return aggregatedCounters;