Fix FindBugs violations and enable enforcement in qosservice
[netvirt.git] / vpnservice / 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 javax.annotation.concurrent.NotThreadSafe;
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;
18
19 @NotThreadSafe
20 public class QosAlertPortData {
21     private static final Logger LOG = LoggerFactory.getLogger(QosAlertPortData.class);
22     private static final BigInteger BIG_HUNDRED = new BigInteger("100");
23
24     private static volatile BigInteger alertThreshold;
25
26     private final Port port;
27     private final QosNeutronUtils qosNeutronUtils;
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         this.port = port;
34         this.qosNeutronUtils = qosNeutronUtils;
35     }
36
37     public static void setAlertThreshold(int threshold) {
38         alertThreshold = BigInteger.valueOf(threshold);
39         LOG.debug("setAlertThreshold:{}", alertThreshold);
40     }
41
42     public void initPortData() {
43         LOG.trace("Port {} data initialized", port.getUuid().getValue());
44         statsDataInit = false;
45     }
46
47     public void updatePortStatistics(NodeConnectorStatisticsAndPortNumberMap statsData) {
48         LOG.trace("Port {} rx-packets {} tx-packets {} rx-dropped {} tx-dropped {}", port.getUuid(),
49                            statsData.getPackets().getReceived(), statsData.getPackets().getTransmitted(),
50                            statsData.getReceiveDrops(), statsData.getTransmitDrops());
51         if (statsDataInit) {
52             calculateAlertCondition(statsData);
53         }
54         rxPackets = statsData.getPackets().getReceived();
55         rxDroppedPackets = statsData.getReceiveDrops();
56         statsDataInit = true;
57     }
58
59     private void calculateAlertCondition(NodeConnectorStatisticsAndPortNumberMap statsData)  {
60         BigInteger rxDiff = statsData.getPackets().getReceived().subtract(rxPackets);
61         BigInteger rxDroppedDiff = statsData.getReceiveDrops().subtract(rxDroppedPackets);
62
63         BigInteger rxTotalDiff = rxDiff.add(rxDroppedDiff);
64         LOG.trace("Port {} rxDiff:{} rxDropped diff:{} total diff:{}", port.getUuid(), rxDiff,
65                                                                             rxDroppedDiff, rxTotalDiff);
66         QosPolicy qosPolicy = qosNeutronUtils.getQosPolicy(port);
67
68         if (qosPolicy == null) {
69             return;
70         }
71
72         if (rxDroppedDiff.multiply(BIG_HUNDRED).compareTo(rxTotalDiff.multiply(alertThreshold)) > 0) {
73             LOG.trace(QosConstants.ALERT_MSG_FORMAT, qosPolicy.getName(), qosPolicy.getUuid().getValue(),
74                     port.getUuid().getValue(), port.getNetworkId().getValue(), statsData.getPackets().getReceived(),
75                                                                                         statsData.getReceiveDrops());
76
77             QosAlertGenerator.raiseAlert(qosPolicy.getName(), qosPolicy.getUuid().getValue(),
78                     port.getUuid().getValue(), port.getNetworkId().getValue(), statsData.getPackets().getReceived(),
79                                                                                           statsData.getReceiveDrops());
80         }
81
82     }
83 }