2 * Copyright (c) 2017 Intel Corporation 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
8 package org.opendaylight.netvirt.qosservice;
10 import java.util.Collections;
11 import java.util.Objects;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.genius.mdsalutil.NwConstants;
16 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
17 import org.opendaylight.infrautils.utils.concurrent.Executors;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.netvirt.qosservice.recovery.QosServiceRecoveryHandler;
21 import org.opendaylight.serviceutils.srm.RecoverableListener;
22 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
23 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosPortExtension;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.rev160613.qos.attributes.qos.policies.QosPolicy;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.Uint64;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class QosNeutronPortChangeListener extends AbstractClusteredAsyncDataTreeChangeListener<Port>
37 implements RecoverableListener {
38 private static final Logger LOG = LoggerFactory.getLogger(QosNeutronPortChangeListener.class);
39 private final DataBroker dataBroker;
40 private final QosNeutronUtils qosNeutronUtils;
41 private final QosEosHandler qosEosHandler;
42 private final JobCoordinator jobCoordinator;
45 public QosNeutronPortChangeListener(final DataBroker dataBroker,
46 final QosNeutronUtils qosNeutronUtils, final QosServiceRecoveryHandler qosServiceRecoveryHandler,
47 final ServiceRecoveryRegistry serviceRecoveryRegistry,
48 final QosEosHandler qosEosHandler,
49 final JobCoordinator jobCoordinator) {
50 super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Neutron.class)
51 .child(Ports.class).child(Port.class),
52 Executors.newListeningSingleThreadExecutor("QosNeutronPortChangeListener", LOG));
53 this.dataBroker = dataBroker;
54 this.qosNeutronUtils = qosNeutronUtils;
55 this.qosEosHandler = qosEosHandler;
56 this.jobCoordinator = jobCoordinator;
57 serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
59 LOG.trace("{} created", getClass().getSimpleName());
63 LOG.trace("{} init and registerListener done", getClass().getSimpleName());
70 Executors.shutdownAndAwaitTermination(getExecutorService());
74 public void registerListener() {
79 public void deregisterListener() {
84 public void add(InstanceIdentifier<Port> instanceIdentifier, Port port) {
85 qosNeutronUtils.addToPortCache(port);
89 public void remove(InstanceIdentifier<Port> instanceIdentifier, Port port) {
90 qosNeutronUtils.removeFromPortCache(port);
94 public void update(InstanceIdentifier<Port> instanceIdentifier, Port original, Port update) {
95 qosNeutronUtils.addToPortCache(update);
96 // check for QoS updates
97 QosPortExtension updateQos = update.augmentation(QosPortExtension.class);
98 QosPortExtension originalQos = original.augmentation(QosPortExtension.class);
100 if (originalQos == null && updateQos != null) {
101 // qosservice policy add
102 qosNeutronUtils.addToQosPortsCache(updateQos.getQosPolicyId(), update);
103 qosNeutronUtils.handleNeutronPortQosAdd(update, updateQos.getQosPolicyId());
104 } else if (originalQos != null && updateQos != null
105 && !Objects.equals(originalQos.getQosPolicyId(), updateQos.getQosPolicyId())) {
107 // qosservice policy update
108 qosNeutronUtils.removeFromQosPortsCache(originalQos.getQosPolicyId(), original);
109 qosNeutronUtils.addToQosPortsCache(updateQos.getQosPolicyId(), update);
110 qosNeutronUtils.handleNeutronPortQosUpdate(update, updateQos.getQosPolicyId(),
111 originalQos.getQosPolicyId());
112 } else if (originalQos != null && updateQos == null) {
113 // qosservice policy delete
114 qosNeutronUtils.handleNeutronPortQosRemove(original, originalQos.getQosPolicyId());
115 qosNeutronUtils.removeFromQosPortsCache(originalQos.getQosPolicyId(), original);
118 if (qosEosHandler.isQosClusterOwner()) {
119 checkForPortIpAddressUpdate(original, update);
123 private void checkForPortIpAddressUpdate(Port original, Port update) {
124 QosPolicy qosPolicy = qosNeutronUtils.getQosPolicy(update);
125 if (qosPolicy == null || !qosNeutronUtils.hasDscpMarkingRule(qosPolicy)) {
128 int origAddrMask = qosNeutronUtils.getIpVersions(original);
129 int updateAddrMask = qosNeutronUtils.getIpVersions(update);
131 if (origAddrMask == updateAddrMask) {
134 jobCoordinator.enqueueJob("QosPort-" + update.getUuid().getValue(), () -> {
135 short dscpVal = qosPolicy.getDscpmarkingRules().get(0).getDscpMark().toJava();
136 String ifName = update.getUuid().getValue();
137 Uint64 dpnId = qosNeutronUtils.getDpnForInterface(ifName);
138 if (dpnId.equals(Uint64.ZERO)) {
139 LOG.warn("dpnId not found for intf {}", ifName);
140 return Collections.emptyList();
142 Interface intf = qosNeutronUtils.getInterfaceStateFromOperDS(ifName);
143 if (qosNeutronUtils.hasIpv4Addr(origAddrMask) && !qosNeutronUtils.hasIpv4Addr(updateAddrMask)) {
144 LOG.debug("removing ipv4 flow for port: {}", update.getUuid().getValue());
145 qosNeutronUtils.removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV4, intf);
146 } else if (!qosNeutronUtils.hasIpv4Addr(origAddrMask) && qosNeutronUtils.hasIpv4Addr(updateAddrMask)) {
147 LOG.debug("adding ipv4 flow for port: {}, dscp: {}", update.getUuid().getValue(), dscpVal);
148 qosNeutronUtils.addFlow(dpnId, dscpVal, ifName, NwConstants.ETHTYPE_IPV4, intf);
151 if (qosNeutronUtils.hasIpv6Addr(origAddrMask) && !qosNeutronUtils.hasIpv6Addr(updateAddrMask)) {
152 LOG.debug("removing ipv6 flow for port: {}", update.getUuid().getValue());
153 qosNeutronUtils.removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV6, intf);
154 } else if (!qosNeutronUtils.hasIpv6Addr(origAddrMask) && qosNeutronUtils.hasIpv6Addr(updateAddrMask)) {
155 LOG.debug("adding ipv6 flow for port: {}, dscp: {}", update.getUuid().getValue(), dscpVal);
156 qosNeutronUtils.addFlow(dpnId, dscpVal, ifName, NwConstants.ETHTYPE_IPV6, intf);
158 return Collections.emptyList();