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 qosNeutronUtils.removeFromNetworkCache(network);
73 if (qosNeutronUtils.hasBandwidthLimitRule(network)) {
74 qosAlertManager.removeFromQosAlertCache(network);
79 protected void update(InstanceIdentifier<Network> instanceIdentifier, Network original, Network update) {
80 qosNeutronUtils.addToNetworkCache(update);
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);
91 } else if (originalQos != null && updateQos != null
92 && !originalQos.getQosPolicyId().equals(updateQos.getQosPolicyId())) {
94 // qosservice policy update
96 qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
97 qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
98 qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
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);
108 } else if (originalQos != null && updateQos == null) {
109 // qosservice policy delete
110 if (qosNeutronUtils.hasBandwidthLimitRule(original)) {
111 qosAlertManager.removeFromQosAlertCache(original);
113 qosNeutronUtils.handleNeutronNetworkQosRemove(original, originalQos.getQosPolicyId());
114 qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
119 protected void add(InstanceIdentifier<Network> instanceIdentifier, Network network) {
120 qosNeutronUtils.addToNetworkCache(network);
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);