5ee186d1154cb890528d8243ce7703e3b2bc629e
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / QosAlertPortData.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.qosservice;
10
11 import java.math.BigInteger;
12 import java.util.function.Supplier;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.rev160613.qos.attributes.qos.policies.QosPolicy;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 @NotThreadSafe
21 public class QosAlertPortData {
22     private static final Logger LOG = LoggerFactory.getLogger(QosAlertPortData.class);
23     private static final BigInteger BIG_HUNDRED = new BigInteger("100");
24
25     private final Port port;
26     private final QosNeutronUtils qosNeutronUtils;
27     private final Supplier<BigInteger> alertThreshold;
28     private volatile BigInteger rxPackets;
29     private volatile BigInteger rxDroppedPackets;
30     private volatile boolean statsDataInit;
31
32     public QosAlertPortData(final Port port, final QosNeutronUtils qosNeutronUtils,
33             final Supplier<BigInteger> alertThreshold) {
34         this.port = port;
35         this.qosNeutronUtils = qosNeutronUtils;
36         this.alertThreshold = alertThreshold;
37     }
38
39     public void initPortData() {
40         LOG.trace("Port {} data initialized", port.getUuid().getValue());
41         statsDataInit = false;
42     }
43
44     public void updatePortStatistics(NodeConnectorStatisticsAndPortNumberMap statsData) {
45         LOG.trace("Port {} rx-packets {} tx-packets {} rx-dropped {} tx-dropped {}", port.getUuid().getValue(),
46                            statsData.getPackets().getReceived(), statsData.getPackets().getTransmitted(),
47                            statsData.getReceiveDrops(), statsData.getTransmitDrops());
48         if (statsDataInit) {
49             calculateAlertCondition(statsData);
50         } else {
51             statsDataInit = true;
52         }
53         rxPackets = statsData.getPackets().getReceived();
54         rxDroppedPackets = statsData.getReceiveDrops();
55     }
56
57     private void calculateAlertCondition(NodeConnectorStatisticsAndPortNumberMap statsData)  {
58         BigInteger rxDiff = statsData.getPackets().getReceived().subtract(rxPackets);
59         BigInteger rxDroppedDiff = statsData.getReceiveDrops().subtract(rxDroppedPackets);
60
61         if ((rxDiff.signum() < 0) || (rxDroppedDiff.signum() < 0)) {
62             LOG.debug("Port {} counters reset", port.getUuid().getValue());
63             initPortData(); // counters wrapped. wait for one more poll.
64             return;
65         }
66         BigInteger rxTotalDiff = rxDiff.add(rxDroppedDiff);
67         LOG.trace("Port {} rxDiff:{} rxDropped diff:{} total diff:{}", port.getUuid().getValue(), rxDiff,
68                                                                             rxDroppedDiff, rxTotalDiff);
69         QosPolicy qosPolicy = qosNeutronUtils.getQosPolicy(port);
70
71         if (qosPolicy == null) {
72             return;
73         }
74
75         if (rxDroppedDiff.multiply(BIG_HUNDRED).compareTo(rxTotalDiff.multiply(alertThreshold.get())) > 0) {
76             LOG.trace(QosConstants.ALERT_MSG_FORMAT, qosPolicy.getName(), qosPolicy.getUuid().getValue(),
77                     port.getUuid().getValue(), port.getNetworkId().getValue(), statsData.getPackets().getReceived(),
78                                                                                         statsData.getReceiveDrops());
79
80             QosAlertGenerator.raiseAlert(qosPolicy.getName(), qosPolicy.getUuid().getValue(),
81                     port.getUuid().getValue(), port.getNetworkId().getValue(), statsData.getPackets().getReceived(),
82                                                                                           statsData.getReceiveDrops());
83         }
84
85     }
86 }