2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.qosservice;
11 import java.math.BigInteger;
12 import java.util.function.Supplier;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.rev160613.qos.attributes.qos.policies.QosPolicy;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * This class is ThreadSafe.
22 public class QosAlertPortData {
23 private static final Logger LOG = LoggerFactory.getLogger(QosAlertPortData.class);
24 private static final BigInteger BIG_HUNDRED = new BigInteger("100");
26 private final Port port;
27 private final QosNeutronUtils qosNeutronUtils;
28 private final Supplier<BigInteger> alertThreshold;
29 private volatile BigInteger rxPackets;
30 private volatile BigInteger rxDroppedPackets;
31 private volatile boolean statsDataInit;
33 public QosAlertPortData(final Port port, final QosNeutronUtils qosNeutronUtils,
34 final Supplier<BigInteger> alertThreshold) {
36 this.qosNeutronUtils = qosNeutronUtils;
37 this.alertThreshold = alertThreshold;
40 public void initPortData() {
41 LOG.trace("Port {} data initialized", port.getUuid().getValue());
42 statsDataInit = false;
45 public void updatePortStatistics(NodeConnectorStatisticsAndPortNumberMap statsData) {
46 LOG.trace("Port {} rx-packets {} tx-packets {} rx-dropped {} tx-dropped {}", port.getUuid().getValue(),
47 statsData.getPackets().getReceived(), statsData.getPackets().getTransmitted(),
48 statsData.getReceiveDrops(), statsData.getTransmitDrops());
50 calculateAlertCondition(statsData);
54 rxPackets = statsData.getPackets().getReceived();
55 rxDroppedPackets = statsData.getReceiveDrops();
58 private void calculateAlertCondition(NodeConnectorStatisticsAndPortNumberMap statsData) {
59 BigInteger rxDiff = statsData.getPackets().getReceived().subtract(rxPackets);
60 BigInteger rxDroppedDiff = statsData.getReceiveDrops().subtract(rxDroppedPackets);
62 if ((rxDiff.signum() < 0) || (rxDroppedDiff.signum() < 0)) {
63 LOG.debug("Port {} counters reset", port.getUuid().getValue());
64 initPortData(); // counters wrapped. wait for one more poll.
67 BigInteger rxTotalDiff = rxDiff.add(rxDroppedDiff);
68 LOG.trace("Port {} rxDiff:{} rxDropped diff:{} total diff:{}", port.getUuid().getValue(), rxDiff,
69 rxDroppedDiff, rxTotalDiff);
70 QosPolicy qosPolicy = qosNeutronUtils.getQosPolicy(port);
72 if (qosPolicy == null) {
76 if (rxDroppedDiff.multiply(BIG_HUNDRED).compareTo(rxTotalDiff.multiply(alertThreshold.get())) > 0) {
77 LOG.trace(QosConstants.ALERT_MSG_FORMAT, qosPolicy.getName(), qosPolicy.getUuid().getValue(),
78 port.getUuid().getValue(), port.getNetworkId().getValue(), statsData.getPackets().getReceived(),
79 statsData.getReceiveDrops());
81 QosAlertGenerator.raiseAlert(qosPolicy.getName(), qosPolicy.getUuid().getValue(),
82 port.getUuid().getValue(), port.getNetworkId().getValue(), statsData.getPackets().getReceived(),
83 statsData.getReceiveDrops());