Switch to JDT annotations for Nullable and NonNull
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / QosTerminationPointListener.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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12
13 import java.util.Collections;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
23 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
24 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.rev160613.qos.attributes.qos.policies.QosPolicy;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.rev160613.qos.attributes.qos.policies.qos.policy.BandwidthLimitRules;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @Singleton
41 public class QosTerminationPointListener extends
42         AsyncClusteredDataTreeChangeListenerBase<OvsdbTerminationPointAugmentation, QosTerminationPointListener> {
43     private static final Logger LOG = LoggerFactory.getLogger(QosTerminationPointListener.class);
44     private static final String EXTERNAL_ID_INTERFACE_ID = "iface-id";
45     private final DataBroker dataBroker;
46     private final QosNeutronUtils qosNeutronUtils;
47     private final QosEosHandler qosEosHandler;
48     private final QosAlertManager qosAlertManager;
49     private final ManagedNewTransactionRunner txRunner;
50     private final JobCoordinator jobCoordinator;
51
52     @Inject
53     public QosTerminationPointListener(final DataBroker dataBroker,
54                                        final QosNeutronUtils qosNeutronUtils,
55                                        final QosEosHandler qosEosHandler,
56                                        final QosAlertManager qosAlertManager,
57                                        final JobCoordinator jobCoordinator) {
58         super(OvsdbTerminationPointAugmentation.class, QosTerminationPointListener.class);
59         this.dataBroker = dataBroker;
60         this.qosNeutronUtils = qosNeutronUtils;
61         this.qosEosHandler = qosEosHandler;
62         this.qosAlertManager = qosAlertManager;
63         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
64         this.jobCoordinator = jobCoordinator;
65     }
66
67     @PostConstruct
68     public void init() {
69         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
70     }
71
72     @Override
73     protected InstanceIdentifier<OvsdbTerminationPointAugmentation> getWildCardPath() {
74         return InstanceIdentifier.create(NetworkTopology.class)
75                 .child(Topology.class, new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
76                 .child(Node.class).child(TerminationPoint.class)
77                 .augmentation(OvsdbTerminationPointAugmentation.class);
78     }
79
80     @Override
81     protected void remove(InstanceIdentifier<OvsdbTerminationPointAugmentation> instanceIdentifier,
82                           OvsdbTerminationPointAugmentation tp) {
83         if (isBandwidthRuleApplied(tp)) {
84             String ifaceId = getIfaceId(tp);
85             if (ifaceId != null) {
86                 qosAlertManager.removeInterfaceIdFromQosAlertCache(ifaceId);
87             }
88         }
89     }
90
91     private boolean isBandwidthRuleCleared(OvsdbTerminationPointAugmentation original,
92                                          OvsdbTerminationPointAugmentation update) {
93         if ((update.getIngressPolicingRate() == 0 && update.getIngressPolicingBurst() == 0)
94                 && (original.getIngressPolicingRate() != 0 || original.getIngressPolicingBurst() != 0)) {
95             return true;
96         }
97         return false;
98     }
99
100     private boolean isBandwidthRuleApplied(OvsdbTerminationPointAugmentation tp) {
101         if (tp.getIngressPolicingRate() != 0 || tp.getIngressPolicingBurst() != 0) {
102             return true;
103         }
104         return false;
105     }
106
107     private boolean isBandwidthRuleApplied(OvsdbTerminationPointAugmentation original,
108                                            OvsdbTerminationPointAugmentation update) {
109         if ((original.getIngressPolicingRate() == 0 && original.getIngressPolicingBurst() == 0)
110                 && (update.getIngressPolicingRate() != 0 || update.getIngressPolicingBurst() != 0)) {
111             return true;
112         }
113         return false;
114     }
115
116     @Override
117     protected void update(InstanceIdentifier<OvsdbTerminationPointAugmentation> instanceIdentifier,
118                           OvsdbTerminationPointAugmentation original,
119                           OvsdbTerminationPointAugmentation update) {
120         String ifaceId = getIfaceId(update);
121         if (ifaceId != null) {
122             if (isBandwidthRuleCleared(original, update)) {
123                 qosAlertManager.removeInterfaceIdFromQosAlertCache(ifaceId);
124             } else if (isBandwidthRuleApplied(original, update)) {
125                 qosAlertManager.addInterfaceIdInQoSAlertCache(ifaceId);
126             }
127         }
128         if (!qosEosHandler.isQosClusterOwner()) {
129             return;
130         }
131         // switch restart scenario with openstack newton onwards results in deletion and addition
132         // of vhu ports with ovs-dpdk and as a side effect of that, qos parameters for rate limiting
133         // get cleared from the port.
134         // To resolve the issue, in TP update event, check is done to see if old port configuration
135         // has qos parameters set but cleared in updated port configuration and qos policy with
136         // bandwidth rules is present for the port, then reapply the qos policy configuration.
137
138         if (ifaceId != null && isBandwidthRuleCleared(original, update)) {
139             LOG.debug("update tp augment: iface-id: {}, name: {}, old bw rate, burst = {}, {}, "
140                             + "updated bw rate, burst = {}, {}", ifaceId, update.getName(),
141                     original.getIngressPolicingRate(), original.getIngressPolicingBurst(),
142                     update.getIngressPolicingRate(), update.getIngressPolicingBurst());
143             Port port = qosNeutronUtils.getNeutronPort(ifaceId);
144             if (port != null) {
145                 setPortBandwidthRule(instanceIdentifier, update, port);
146             }
147         }
148     }
149
150     @Override
151     protected void add(InstanceIdentifier<OvsdbTerminationPointAugmentation> instanceIdentifier,
152                        OvsdbTerminationPointAugmentation tpAugment) {
153         String ifaceId = getIfaceId(tpAugment);
154         if ((ifaceId != null) && isBandwidthRuleApplied(tpAugment)) {
155             qosAlertManager.addInterfaceIdInQoSAlertCache(ifaceId);
156         }
157         if ((ifaceId != null) && qosEosHandler.isQosClusterOwner()) {
158             Port port = qosNeutronUtils.getNeutronPort(ifaceId);
159             if (port != null) {
160                 LOG.debug("add tp augmentation: iface-id: {}, name: {} ", ifaceId, tpAugment.getName());
161                 setPortBandwidthRule(instanceIdentifier, tpAugment, port);
162             }
163         }
164     }
165
166     @Override
167     protected QosTerminationPointListener getDataTreeChangeListener() {
168         return QosTerminationPointListener.this;
169     }
170
171     private void setPortBandwidthRule(InstanceIdentifier<OvsdbTerminationPointAugmentation> identifier,
172                                       OvsdbTerminationPointAugmentation update, Port port) {
173         QosPolicy qosPolicy = qosNeutronUtils.getQosPolicy(port);
174         if (qosPolicy == null || qosPolicy.getBandwidthLimitRules() == null
175                 || qosPolicy.getBandwidthLimitRules().isEmpty()) {
176             return;
177         }
178         LOG.debug("setting bandwidth rule for port: {}, {}, qos policy: {}",
179                 port.getUuid(), update.getName(), qosPolicy.getName());
180
181         jobCoordinator.enqueueJob("QosPort-" + port.getUuid(), () ->
182                 Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
183                     BandwidthLimitRules bwRule = qosPolicy.getBandwidthLimitRules().get(0);
184                     OvsdbTerminationPointAugmentationBuilder tpAugmentationBuilder =
185                             new OvsdbTerminationPointAugmentationBuilder();
186                     tpAugmentationBuilder.setName(update.getName());
187                     tpAugmentationBuilder.setIngressPolicingRate(bwRule.getMaxKbps().longValue());
188                     tpAugmentationBuilder.setIngressPolicingBurst(bwRule.getMaxBurstKbps().longValue());
189
190                     tx.merge(InstanceIdentifier.create(NetworkTopology.class)
191                             .child(Topology.class, new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
192                             .child(Node.class, identifier.firstKeyOf(Node.class))
193                             .child(TerminationPoint.class, identifier.firstKeyOf(TerminationPoint.class))
194                             .augmentation(OvsdbTerminationPointAugmentation.class),
195                             tpAugmentationBuilder.build(), true);
196
197                 })));
198     }
199
200     @Nullable
201     private String getIfaceId(OvsdbTerminationPointAugmentation tpAugmentation) {
202         if (tpAugmentation.getInterfaceExternalIds() != null) {
203             for (InterfaceExternalIds entry: tpAugmentation.getInterfaceExternalIds()) {
204                 if (EXTERNAL_ID_INTERFACE_ID.equals(entry.getExternalIdKey())) {
205                     return entry.getExternalIdValue();
206                 }
207             }
208         }
209         return null;
210     }
211 }