2 * Copyright (c) 2017 Intel Corporation and others. All rights reserved.
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
8 package org.opendaylight.netvirt.qosservice;
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;
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;
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(),
46 LOG.debug("{} created", getClass().getSimpleName());
52 LOG.debug("{} init and registerListener done", getClass().getSimpleName());
56 public void registerListener() {
57 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
61 protected InstanceIdentifier<Network> getWildCardPath() {
62 return InstanceIdentifier.create(Neutron.class).child(Networks.class).child(Network.class);
66 protected QosNeutronNetworkChangeListener getDataTreeChangeListener() {
67 return QosNeutronNetworkChangeListener.this;
71 protected void remove(InstanceIdentifier<Network> instanceIdentifier, Network network) {
72 if (qosNeutronUtils.hasBandwidthLimitRule(network)) {
73 qosAlertManager.removeFromQosAlertCache(network);
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);
88 } else if (originalQos != null && updateQos != null
89 && !originalQos.getQosPolicyId().equals(updateQos.getQosPolicyId())) {
91 // qosservice policy update
93 qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
94 qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
95 qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
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);
105 } else if (originalQos != null && updateQos == null) {
106 // qosservice policy delete
107 if (qosNeutronUtils.hasBandwidthLimitRule(original)) {
108 qosAlertManager.removeFromQosAlertCache(original);
110 qosNeutronUtils.handleNeutronNetworkQosRemove(original, originalQos.getQosPolicyId());
111 qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
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);