Service Recover for QoS Service as a whole & QoS Policy Instance.
[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         if (qosNeutronUtils.hasBandwidthLimitRule(network)) {
73             qosAlertManager.removeFromQosAlertCache(network);
74         }
75     }
76
77     @Override
78     protected void update(InstanceIdentifier<Network> instanceIdentifier, Network original, Network update) {
79         QosNetworkExtension updateQos = update.getAugmentation(QosNetworkExtension.class);
80         QosNetworkExtension originalQos = original.getAugmentation(QosNetworkExtension.class);
81         if (originalQos == null && updateQos != null) {
82             // qosservice policy add
83             qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
84             qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
85             if (qosNeutronUtils.hasBandwidthLimitRule(update)) {
86                 qosAlertManager.addToQosAlertCache(update);
87             }
88         } else if (originalQos != null && updateQos != null
89                 && !originalQos.getQosPolicyId().equals(updateQos.getQosPolicyId())) {
90
91             // qosservice policy update
92
93             qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
94             qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
95             qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
96
97             if (qosNeutronUtils.hasBandwidthLimitRule(original)
98                                              && !qosNeutronUtils.hasBandwidthLimitRule(update)) {
99                 qosAlertManager.removeFromQosAlertCache(original);
100             } else if (!qosNeutronUtils.hasBandwidthLimitRule(original)
101                                               && qosNeutronUtils.hasBandwidthLimitRule(update)) {
102                 qosAlertManager.addToQosAlertCache(update);
103             }
104
105         } else if (originalQos != null && updateQos == null) {
106             // qosservice policy delete
107             if (qosNeutronUtils.hasBandwidthLimitRule(original)) {
108                 qosAlertManager.removeFromQosAlertCache(original);
109             }
110             qosNeutronUtils.handleNeutronNetworkQosRemove(original, originalQos.getQosPolicyId());
111             qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
112         }
113     }
114
115     @Override
116     protected void add(InstanceIdentifier<Network> instanceIdentifier, Network network) {
117         QosNetworkExtension networkQos = network.getAugmentation(QosNetworkExtension.class);
118         if (networkQos != null) {
119             qosNeutronUtils.addToQosNetworksCache(networkQos.getQosPolicyId(), network);
120             qosNeutronUtils.handleNeutronNetworkQosUpdate(network, networkQos.getQosPolicyId());
121             if (qosNeutronUtils.hasBandwidthLimitRule(network)) {
122                 qosAlertManager.addToQosAlertCache(network);
123             }
124
125         }
126     }
127 }
128