Netvirt-QoS: new listener for bw rules programming
[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 javax.annotation.PostConstruct;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
16 import org.opendaylight.genius.srm.RecoverableListener;
17 import org.opendaylight.genius.srm.ServiceRecoveryRegistry;
18 import org.opendaylight.netvirt.qosservice.recovery.QosServiceRecoveryHandler;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosPortExtension;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 @Singleton
28 public class QosNeutronPortChangeListener extends AsyncClusteredDataTreeChangeListenerBase<Port,
29                                              QosNeutronPortChangeListener> implements RecoverableListener {
30     private static final Logger LOG = LoggerFactory.getLogger(QosNeutronPortChangeListener.class);
31     private final DataBroker dataBroker;
32     private final QosAlertManager qosAlertManager;
33     private final QosNeutronUtils qosNeutronUtils;
34
35     @Inject
36     public QosNeutronPortChangeListener(final DataBroker dataBroker, final QosAlertManager qosAlertManager,
37             final QosNeutronUtils qosNeutronUtils, final QosServiceRecoveryHandler qosServiceRecoveryHandler,
38                                         final ServiceRecoveryRegistry serviceRecoveryRegistry) {
39         super(Port.class, QosNeutronPortChangeListener.class);
40         this.dataBroker = dataBroker;
41         this.qosAlertManager = qosAlertManager;
42         this.qosNeutronUtils = qosNeutronUtils;
43         serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
44                 this);
45         LOG.debug("{} created",  getClass().getSimpleName());
46     }
47
48     @PostConstruct
49     public void init() {
50         registerListener();
51         LOG.debug("{} init and registerListener done", getClass().getSimpleName());
52     }
53
54     @Override
55     protected InstanceIdentifier<Port> getWildCardPath() {
56         return InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class);
57     }
58
59     @Override
60     public void registerListener() {
61         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
62     }
63
64     @Override
65     protected QosNeutronPortChangeListener getDataTreeChangeListener() {
66         return QosNeutronPortChangeListener.this;
67     }
68
69     @Override
70     protected void add(InstanceIdentifier<Port> instanceIdentifier, Port port) {
71         qosNeutronUtils.addToPortCache(port);
72         if (qosNeutronUtils.hasBandwidthLimitRule(port)) {
73             qosAlertManager.addToQosAlertCache(port);
74         }
75     }
76
77     @Override
78     protected void remove(InstanceIdentifier<Port> instanceIdentifier, Port port) {
79         qosNeutronUtils.removeFromPortCache(port);
80         if (qosNeutronUtils.hasBandwidthLimitRule(port)) {
81             qosAlertManager.removeFromQosAlertCache(port);
82         }
83     }
84
85     @Override
86     protected void update(InstanceIdentifier<Port> instanceIdentifier, Port original, Port update) {
87         qosNeutronUtils.addToPortCache(update);
88         // check for QoS updates
89         QosPortExtension updateQos = update.getAugmentation(QosPortExtension.class);
90         QosPortExtension originalQos = original.getAugmentation(QosPortExtension.class);
91
92         if (originalQos == null && updateQos != null) {
93             // qosservice policy add
94             qosNeutronUtils.addToQosPortsCache(updateQos.getQosPolicyId(), update);
95             qosNeutronUtils.handleNeutronPortQosAdd(update, updateQos.getQosPolicyId());
96         } else if (originalQos != null && updateQos != null
97                 && !originalQos.getQosPolicyId().equals(updateQos.getQosPolicyId())) {
98
99             // qosservice policy update
100             qosNeutronUtils.removeFromQosPortsCache(originalQos.getQosPolicyId(), original);
101             qosNeutronUtils.addToQosPortsCache(updateQos.getQosPolicyId(), update);
102             qosNeutronUtils.handleNeutronPortQosUpdate(update, updateQos.getQosPolicyId(),
103                     originalQos.getQosPolicyId());
104         } else if (originalQos != null && updateQos == null) {
105             // qosservice policy delete
106             qosNeutronUtils.handleNeutronPortQosRemove(original, originalQos.getQosPolicyId());
107             qosNeutronUtils.removeFromQosPortsCache(originalQos.getQosPolicyId(), original);
108         }
109
110         if (qosNeutronUtils.hasBandwidthLimitRule(original)
111                                         && !qosNeutronUtils.hasBandwidthLimitRule(update)) {
112             qosAlertManager.removeFromQosAlertCache(original);
113         } else if (!qosNeutronUtils.hasBandwidthLimitRule(original)
114                                           && qosNeutronUtils.hasBandwidthLimitRule(update)) {
115             qosAlertManager.addToQosAlertCache(update);
116         }
117     }
118 }