Adding QoS display command
[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.util.function.Supplier;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.rev160613.qos.attributes.qos.policies.QosPolicy;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
15 import org.opendaylight.yangtools.yang.common.Uint64;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * This class is ThreadSafe.
21  */
22 public class QosAlertPortData {
23     private static final Logger LOG = LoggerFactory.getLogger(QosAlertPortData.class);
24     private static final Uint64 BIG_HUNDRED = Uint64.valueOf("100").intern();
25
26     private final Port port;
27     private final QosNeutronUtils qosNeutronUtils;
28     private final Supplier<Uint64> alertThreshold;
29     private volatile Uint64 rxPackets;
30     private volatile Uint64 rxDroppedPackets;
31     private volatile boolean statsDataInit;
32
33     public QosAlertPortData(final Port port, final QosNeutronUtils qosNeutronUtils,
34             final Supplier<Uint64> alertThreshold) {
35         this.port = port;
36         this.qosNeutronUtils = qosNeutronUtils;
37         this.alertThreshold = alertThreshold;
38     }
39
40     public void initPortData() {
41         LOG.trace("Port {} data initialized", port.getUuid().getValue());
42         statsDataInit = false;
43     }
44
45     public String toString() {
46         return "Uuid: " + this.port.getUuid().getValue() + ", rx_packets: " + rxPackets
47                 + ", rx_dropped_packets: " + rxDroppedPackets;
48     }
49
50     public void updatePortStatistics(NodeConnectorStatisticsAndPortNumberMap statsData) {
51         LOG.trace("Port {} rx-packets {} tx-packets {} rx-dropped {} tx-dropped {}", port.getUuid().getValue(),
52                            statsData.getPackets().getReceived(), statsData.getPackets().getTransmitted(),
53                            statsData.getReceiveDrops(), statsData.getTransmitDrops());
54         if (statsDataInit) {
55             calculateAlertCondition(statsData);
56         } else {
57             statsDataInit = true;
58         }
59         rxPackets = statsData.getPackets().getReceived();
60         rxDroppedPackets = statsData.getReceiveDrops();
61     }
62
63     private void calculateAlertCondition(NodeConnectorStatisticsAndPortNumberMap statsData)  {
64         Uint64 rxDiff = Uint64.valueOf(statsData.getPackets().getReceived().toJava().subtract(rxPackets.toJava()));
65         Uint64 rxDroppedDiff = Uint64.valueOf(statsData.getReceiveDrops().toJava().subtract(rxDroppedPackets.toJava()));
66
67         if ((rxDiff.toJava().signum() < 0) || (rxDroppedDiff.toJava().signum() < 0)) {
68             LOG.debug("Port {} counters reset", port.getUuid().getValue());
69             initPortData(); // counters wrapped. wait for one more poll.
70             return;
71         }
72         Uint64 rxTotalDiff = Uint64.valueOf(rxDiff.toJava().add(rxDroppedDiff.toJava()));
73         LOG.trace("Port {} rxDiff:{} rxDropped diff:{} total diff:{}", port.getUuid().getValue(), rxDiff,
74                                                                             rxDroppedDiff, rxTotalDiff);
75         QosPolicy qosPolicy = qosNeutronUtils.getQosPolicy(port);
76
77         if (qosPolicy == null) {
78             return;
79         }
80
81         if (rxDroppedDiff.toJava().multiply(BIG_HUNDRED.toJava())
82                 .compareTo(rxTotalDiff.toJava().multiply(alertThreshold.get().toJava())) > 0) {
83             LOG.trace(QosConstants.ALERT_MSG_FORMAT, qosPolicy.getName(), qosPolicy.getUuid().getValue(),
84                     port.getUuid().getValue(), port.getNetworkId().getValue(), statsData.getPackets().getReceived(),
85                                                                                         statsData.getReceiveDrops());
86
87             QosAlertGenerator.raiseAlert(qosPolicy.getName(), qosPolicy.getUuid().getValue(),
88                     port.getUuid().getValue(), port.getNetworkId().getValue(),
89                     statsData.getPackets().getReceived(), statsData.getReceiveDrops());
90         }
91
92     }
93 }