19f243e3d9207367a6089718d5f7e79400978cf8
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / QosNeutronPortChangeListener.java
1 /*
2  * Copyright (c) 2017 Intel Corporation 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 package org.opendaylight.netvirt.qosservice;
9
10 import java.util.Collections;
11 import java.util.Objects;
12 import javax.annotation.PostConstruct;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
18 import org.opendaylight.genius.mdsalutil.NwConstants;
19 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
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.ext.rev160613.QosPortExtension;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.rev160613.qos.attributes.qos.policies.QosPolicy;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.Uint64;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @Singleton
35 public class QosNeutronPortChangeListener extends AsyncClusteredDataTreeChangeListenerBase<Port,
36                                              QosNeutronPortChangeListener> implements RecoverableListener {
37     private static final Logger LOG = LoggerFactory.getLogger(QosNeutronPortChangeListener.class);
38     private final DataBroker dataBroker;
39     private final QosNeutronUtils qosNeutronUtils;
40     private final QosEosHandler qosEosHandler;
41     private final JobCoordinator jobCoordinator;
42
43     @Inject
44     public QosNeutronPortChangeListener(final DataBroker dataBroker,
45             final QosNeutronUtils qosNeutronUtils, final QosServiceRecoveryHandler qosServiceRecoveryHandler,
46                                         final ServiceRecoveryRegistry serviceRecoveryRegistry,
47                                         final QosEosHandler qosEosHandler,
48                                         final JobCoordinator jobCoordinator) {
49         super(Port.class, QosNeutronPortChangeListener.class);
50         this.dataBroker = dataBroker;
51         this.qosNeutronUtils = qosNeutronUtils;
52         this.qosEosHandler = qosEosHandler;
53         this.jobCoordinator = jobCoordinator;
54         serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
55                 this);
56         LOG.trace("{} created",  getClass().getSimpleName());
57     }
58
59     @PostConstruct
60     public void init() {
61         registerListener();
62         LOG.trace("{} init and registerListener done", getClass().getSimpleName());
63     }
64
65     @Override
66     protected InstanceIdentifier<Port> getWildCardPath() {
67         return InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class);
68     }
69
70     @Override
71     public void registerListener() {
72         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
73     }
74
75     @Override
76     protected QosNeutronPortChangeListener getDataTreeChangeListener() {
77         return QosNeutronPortChangeListener.this;
78     }
79
80     @Override
81     protected void add(InstanceIdentifier<Port> instanceIdentifier, Port port) {
82         qosNeutronUtils.addToPortCache(port);
83     }
84
85     @Override
86     protected void remove(InstanceIdentifier<Port> instanceIdentifier, Port port) {
87         qosNeutronUtils.removeFromPortCache(port);
88     }
89
90     @Override
91     protected void update(InstanceIdentifier<Port> instanceIdentifier, Port original, Port update) {
92         qosNeutronUtils.addToPortCache(update);
93         // check for QoS updates
94         QosPortExtension updateQos = update.augmentation(QosPortExtension.class);
95         QosPortExtension originalQos = original.augmentation(QosPortExtension.class);
96
97         if (originalQos == null && updateQos != null) {
98             // qosservice policy add
99             qosNeutronUtils.addToQosPortsCache(updateQos.getQosPolicyId(), update);
100             qosNeutronUtils.handleNeutronPortQosAdd(update, updateQos.getQosPolicyId());
101         } else if (originalQos != null && updateQos != null
102                 && !Objects.equals(originalQos.getQosPolicyId(), updateQos.getQosPolicyId())) {
103
104             // qosservice policy update
105             qosNeutronUtils.removeFromQosPortsCache(originalQos.getQosPolicyId(), original);
106             qosNeutronUtils.addToQosPortsCache(updateQos.getQosPolicyId(), update);
107             qosNeutronUtils.handleNeutronPortQosUpdate(update, updateQos.getQosPolicyId(),
108                     originalQos.getQosPolicyId());
109         } else if (originalQos != null && updateQos == null) {
110             // qosservice policy delete
111             qosNeutronUtils.handleNeutronPortQosRemove(original, originalQos.getQosPolicyId());
112             qosNeutronUtils.removeFromQosPortsCache(originalQos.getQosPolicyId(), original);
113         }
114
115         if (qosEosHandler.isQosClusterOwner()) {
116             checkForPortIpAddressUpdate(original, update);
117         }
118     }
119
120     private void checkForPortIpAddressUpdate(Port original, Port update) {
121         QosPolicy qosPolicy = qosNeutronUtils.getQosPolicy(update);
122         if (qosPolicy == null || !qosNeutronUtils.hasDscpMarkingRule(qosPolicy)) {
123             return;
124         }
125         int origAddrMask = qosNeutronUtils.getIpVersions(original);
126         int updateAddrMask = qosNeutronUtils.getIpVersions(update);
127
128         if (origAddrMask == updateAddrMask) {
129             return;
130         }
131         jobCoordinator.enqueueJob("QosPort-" + update.getUuid().getValue(), () -> {
132             short dscpVal = qosPolicy.getDscpmarkingRules().get(0).getDscpMark().toJava();
133             String ifName = update.getUuid().getValue();
134             Uint64 dpnId = qosNeutronUtils.getDpnForInterface(ifName);
135             if (dpnId.equals(Uint64.ZERO)) {
136                 LOG.warn("dpnId not found for intf {}", ifName);
137                 return Collections.emptyList();
138             }
139             Interface intf = qosNeutronUtils.getInterfaceStateFromOperDS(ifName);
140             if (qosNeutronUtils.hasIpv4Addr(origAddrMask) && !qosNeutronUtils.hasIpv4Addr(updateAddrMask)) {
141                 LOG.debug("removing ipv4 flow for port: {}", update.getUuid().getValue());
142                 qosNeutronUtils.removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV4, intf);
143             } else if (!qosNeutronUtils.hasIpv4Addr(origAddrMask) && qosNeutronUtils.hasIpv4Addr(updateAddrMask)) {
144                 LOG.debug("adding ipv4 flow for port: {}, dscp: {}", update.getUuid().getValue(), dscpVal);
145                 qosNeutronUtils.addFlow(dpnId, dscpVal, ifName, NwConstants.ETHTYPE_IPV4, intf);
146             }
147
148             if (qosNeutronUtils.hasIpv6Addr(origAddrMask) && !qosNeutronUtils.hasIpv6Addr(updateAddrMask)) {
149                 LOG.debug("removing ipv6 flow for port: {}", update.getUuid().getValue());
150                 qosNeutronUtils.removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV6, intf);
151             } else if (!qosNeutronUtils.hasIpv6Addr(origAddrMask) && qosNeutronUtils.hasIpv6Addr(updateAddrMask)) {
152                 LOG.debug("adding ipv6 flow for port: {}, dscp: {}", update.getUuid().getValue(), dscpVal);
153                 qosNeutronUtils.addFlow(dpnId, dscpVal, ifName, NwConstants.ETHTYPE_IPV6, intf);
154             }
155             return Collections.emptyList();
156         });
157     }
158 }