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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
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;
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;
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;
69 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
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);
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);
91 private boolean isBandwidthRuleCleared(OvsdbTerminationPointAugmentation original,
92 OvsdbTerminationPointAugmentation update) {
93 if ((update.getIngressPolicingRate() == 0 && update.getIngressPolicingBurst() == 0)
94 && (original.getIngressPolicingRate() != 0 || original.getIngressPolicingBurst() != 0)) {
100 private boolean isBandwidthRuleApplied(OvsdbTerminationPointAugmentation tp) {
101 if (tp.getIngressPolicingRate() != 0 || tp.getIngressPolicingBurst() != 0) {
107 private boolean isBandwidthRuleApplied(OvsdbTerminationPointAugmentation original,
108 OvsdbTerminationPointAugmentation update) {
109 if ((original.getIngressPolicingRate() == 0 && original.getIngressPolicingBurst() == 0)
110 && (update.getIngressPolicingRate() != 0 || update.getIngressPolicingBurst() != 0)) {
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);
128 if (!qosEosHandler.isQosClusterOwner()) {
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.
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);
145 setPortBandwidthRule(instanceIdentifier, update, port);
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);
157 if ((ifaceId != null) && qosEosHandler.isQosClusterOwner()) {
158 Port port = qosNeutronUtils.getNeutronPort(ifaceId);
160 LOG.debug("add tp augmentation: iface-id: {}, name: {} ", ifaceId, tpAugment.getName());
161 setPortBandwidthRule(instanceIdentifier, tpAugment, port);
167 protected QosTerminationPointListener getDataTreeChangeListener() {
168 return QosTerminationPointListener.this;
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()) {
178 LOG.debug("setting bandwidth rule for port: {}, {}, qos policy: {}",
179 port.getUuid(), update.getName(), qosPolicy.getName());
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());
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);
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();