Netvirt-QoS: new listener for bw rules programming
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / QosNeutronNetworkChangeListener.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.networks.rev150712.networks.attributes.Networks;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosNetworkExtension;
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 QosNeutronNetworkChangeListener extends AsyncClusteredDataTreeChangeListenerBase<Network,
29         QosNeutronNetworkChangeListener> implements RecoverableListener {
30     private static final Logger LOG = LoggerFactory.getLogger(QosNeutronNetworkChangeListener.class);
31     private final DataBroker dataBroker;
32     private final QosAlertManager qosAlertManager;
33     private final QosNeutronUtils qosNeutronUtils;
34
35     @Inject
36     public QosNeutronNetworkChangeListener(final DataBroker dataBroker, final QosAlertManager qosAlertManager,
37                                            final QosNeutronUtils qosNeutronUtils,
38                                            final ServiceRecoveryRegistry serviceRecoveryRegistry,
39                                            final QosServiceRecoveryHandler qosServiceRecoveryHandler) {
40         super(Network.class, QosNeutronNetworkChangeListener.class);
41         this.dataBroker = dataBroker;
42         this.qosAlertManager = qosAlertManager;
43         this.qosNeutronUtils = qosNeutronUtils;
44         serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
45                 this);
46         LOG.debug("{} created",  getClass().getSimpleName());
47     }
48
49     @PostConstruct
50     public void init() {
51         registerListener();
52         LOG.debug("{} init and registerListener done", getClass().getSimpleName());
53     }
54
55     @Override
56     public void registerListener() {
57         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
58     }
59
60     @Override
61     protected InstanceIdentifier<Network> getWildCardPath() {
62         return InstanceIdentifier.create(Neutron.class).child(Networks.class).child(Network.class);
63     }
64
65     @Override
66     protected QosNeutronNetworkChangeListener getDataTreeChangeListener() {
67         return QosNeutronNetworkChangeListener.this;
68     }
69
70     @Override
71     protected void remove(InstanceIdentifier<Network> instanceIdentifier, Network network) {
72         qosNeutronUtils.removeFromNetworkCache(network);
73         if (qosNeutronUtils.hasBandwidthLimitRule(network)) {
74             qosAlertManager.removeFromQosAlertCache(network);
75         }
76     }
77
78     @Override
79     protected void update(InstanceIdentifier<Network> instanceIdentifier, Network original, Network update) {
80         qosNeutronUtils.addToNetworkCache(update);
81
82         QosNetworkExtension updateQos = update.getAugmentation(QosNetworkExtension.class);
83         QosNetworkExtension originalQos = original.getAugmentation(QosNetworkExtension.class);
84         if (originalQos == null && updateQos != null) {
85             // qosservice policy add
86             qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
87             qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
88             if (qosNeutronUtils.hasBandwidthLimitRule(update)) {
89                 qosAlertManager.addToQosAlertCache(update);
90             }
91         } else if (originalQos != null && updateQos != null
92                 && !originalQos.getQosPolicyId().equals(updateQos.getQosPolicyId())) {
93
94             // qosservice policy update
95
96             qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
97             qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
98             qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
99
100             if (qosNeutronUtils.hasBandwidthLimitRule(original)
101                                              && !qosNeutronUtils.hasBandwidthLimitRule(update)) {
102                 qosAlertManager.removeFromQosAlertCache(original);
103             } else if (!qosNeutronUtils.hasBandwidthLimitRule(original)
104                                               && qosNeutronUtils.hasBandwidthLimitRule(update)) {
105                 qosAlertManager.addToQosAlertCache(update);
106             }
107
108         } else if (originalQos != null && updateQos == null) {
109             // qosservice policy delete
110             if (qosNeutronUtils.hasBandwidthLimitRule(original)) {
111                 qosAlertManager.removeFromQosAlertCache(original);
112             }
113             qosNeutronUtils.handleNeutronNetworkQosRemove(original, originalQos.getQosPolicyId());
114             qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
115         }
116     }
117
118     @Override
119     protected void add(InstanceIdentifier<Network> instanceIdentifier, Network network) {
120         qosNeutronUtils.addToNetworkCache(network);
121
122         QosNetworkExtension networkQos = network.getAugmentation(QosNetworkExtension.class);
123         if (networkQos != null) {
124             qosNeutronUtils.addToQosNetworksCache(networkQos.getQosPolicyId(), network);
125             qosNeutronUtils.handleNeutronNetworkQosUpdate(network, networkQos.getQosPolicyId());
126             if (qosNeutronUtils.hasBandwidthLimitRule(network)) {
127                 qosAlertManager.addToQosAlertCache(network);
128             }
129
130         }
131     }
132 }
133