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 java.util.Objects;
11 import javax.annotation.PreDestroy;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.infrautils.utils.concurrent.Executors;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.netvirt.qosservice.recovery.QosServiceRecoveryHandler;
18 import org.opendaylight.serviceutils.srm.RecoverableListener;
19 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
20 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.Networks;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosNetworkExtension;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 public class QosNeutronNetworkChangeListener extends AbstractClusteredAsyncDataTreeChangeListener<Network>
31 implements RecoverableListener {
32 private static final Logger LOG = LoggerFactory.getLogger(QosNeutronNetworkChangeListener.class);
33 private final DataBroker dataBroker;
34 private final QosNeutronUtils qosNeutronUtils;
37 public QosNeutronNetworkChangeListener(final DataBroker dataBroker,
38 final QosNeutronUtils qosNeutronUtils,
39 final ServiceRecoveryRegistry serviceRecoveryRegistry,
40 final QosServiceRecoveryHandler qosServiceRecoveryHandler) {
41 super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Neutron.class)
42 .child(Networks.class).child(Network.class),
43 Executors.newListeningSingleThreadExecutor("QosNeutronNetworkChangeListener", LOG));
44 this.dataBroker = dataBroker;
45 this.qosNeutronUtils = qosNeutronUtils;
46 serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
48 LOG.trace("{} created", getClass().getSimpleName());
52 LOG.trace("{} init and registerListener done", getClass().getSimpleName());
59 Executors.shutdownAndAwaitTermination(getExecutorService());
63 public void registerListener() {
68 public void deregisterListener() {
73 public void remove(InstanceIdentifier<Network> instanceIdentifier, Network network) {
74 qosNeutronUtils.removeFromNetworkCache(network);
78 public void update(InstanceIdentifier<Network> instanceIdentifier, Network original, Network update) {
79 qosNeutronUtils.addToNetworkCache(update);
81 QosNetworkExtension updateQos = update.augmentation(QosNetworkExtension.class);
82 QosNetworkExtension originalQos = original.augmentation(QosNetworkExtension.class);
83 if (originalQos == null && updateQos != null) {
84 // qosservice policy add
85 qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
86 qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
87 } else if (originalQos != null && updateQos != null
88 && !Objects.equals(originalQos.getQosPolicyId(), updateQos.getQosPolicyId())) {
89 // qosservice policy update
90 qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
91 qosNeutronUtils.addToQosNetworksCache(updateQos.getQosPolicyId(), update);
92 qosNeutronUtils.handleNeutronNetworkQosUpdate(update, updateQos.getQosPolicyId());
93 } else if (originalQos != null && updateQos == null) {
94 // qosservice policy delete
95 qosNeutronUtils.handleNeutronNetworkQosRemove(original, originalQos.getQosPolicyId());
96 qosNeutronUtils.removeFromQosNetworksCache(originalQos.getQosPolicyId(), original);
101 public void add(InstanceIdentifier<Network> instanceIdentifier, Network network) {
102 qosNeutronUtils.addToNetworkCache(network);
104 QosNetworkExtension networkQos = network.augmentation(QosNetworkExtension.class);
105 if (networkQos != null) {
106 qosNeutronUtils.addToQosNetworksCache(networkQos.getQosPolicyId(), network);
107 qosNeutronUtils.handleNeutronNetworkQosUpdate(network, networkQos.getQosPolicyId());