move vpnservice and cleanup poms
[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.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.Networks;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosNetworkExtension;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 @Singleton
25 public class QosNeutronNetworkChangeListener extends AsyncClusteredDataTreeChangeListenerBase<Network,
26         QosNeutronNetworkChangeListener> {
27     private static final Logger LOG = LoggerFactory.getLogger(QosNeutronNetworkChangeListener.class);
28     private final DataBroker dataBroker;
29     private final QosAlertManager qosAlertManager;
30     private final QosNeutronUtils qosNeutronUtils;
31
32     @Inject
33     public QosNeutronNetworkChangeListener(final DataBroker dataBroker, final QosAlertManager qosAlertManager,
34             final QosNeutronUtils qosNeutronUtils) {
35         super(Network.class, QosNeutronNetworkChangeListener.class);
36         this.dataBroker = dataBroker;
37         this.qosAlertManager = qosAlertManager;
38         this.qosNeutronUtils = qosNeutronUtils;
39         LOG.debug("{} created",  getClass().getSimpleName());
40     }
41
42     @PostConstruct
43     public void init() {
44         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
45         LOG.debug("{} init and registerListener done", getClass().getSimpleName());
46     }
47
48     @Override
49     protected InstanceIdentifier<Network> getWildCardPath() {
50         return InstanceIdentifier.create(Neutron.class).child(Networks.class).child(Network.class);
51     }
52
53     @Override
54     protected QosNeutronNetworkChangeListener getDataTreeChangeListener() {
55         return QosNeutronNetworkChangeListener.this;
56     }
57
58     @Override
59     protected void remove(InstanceIdentifier<Network> instanceIdentifier, Network network) {
60         if (qosNeutronUtils.hasBandwidthLimitRule(network)) {
61             qosAlertManager.removeFromQosAlertCache(network);
62         }
63     }
64
65     @Override
66     protected void update(InstanceIdentifier<Network> instanceIdentifier, Network original, Network update) {
67         QosNetworkExtension updateQos = update.getAugmentation(QosNetworkExtension.class);
68         QosNetworkExtension originalQos = original.getAugmentation(QosNetworkExtension.class);
69         if (originalQos == null && updateQos != null) {
70             // qosservice policy add
71             qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
72             qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
73             if (qosNeutronUtils.hasBandwidthLimitRule(update)) {
74                 qosAlertManager.addToQosAlertCache(update);
75             }
76         } else if (originalQos != null && updateQos != null
77                 && !originalQos.getQosPolicyId().equals(updateQos.getQosPolicyId())) {
78
79             // qosservice policy update
80
81             qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
82             qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
83             qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
84
85             if (qosNeutronUtils.hasBandwidthLimitRule(original)
86                                              && !qosNeutronUtils.hasBandwidthLimitRule(update)) {
87                 qosAlertManager.removeFromQosAlertCache(original);
88             } else if (!qosNeutronUtils.hasBandwidthLimitRule(original)
89                                               && qosNeutronUtils.hasBandwidthLimitRule(update)) {
90                 qosAlertManager.addToQosAlertCache(update);
91             }
92
93         } else if (originalQos != null && updateQos == null) {
94             // qosservice policy delete
95             if (qosNeutronUtils.hasBandwidthLimitRule(original)) {
96                 qosAlertManager.removeFromQosAlertCache(original);
97             }
98             qosNeutronUtils.handleNeutronNetworkQosRemove(original, originalQos.getQosPolicyId());
99             qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
100         }
101     }
102
103     @Override
104     protected void add(InstanceIdentifier<Network> instanceIdentifier, Network network) {
105         QosNetworkExtension networkQos = network.getAugmentation(QosNetworkExtension.class);
106         if (networkQos != null) {
107             qosNeutronUtils.addToQosNetworksCache(networkQos.getQosPolicyId(), network);
108             qosNeutronUtils.handleNeutronNetworkQosUpdate(network, networkQos.getQosPolicyId());
109             if (qosNeutronUtils.hasBandwidthLimitRule(network)) {
110                 qosAlertManager.addToQosAlertCache(network);
111             }
112
113         }
114     }
115 }
116