61029f17d95c48cb975b7c48f12ff3cc68182bb7
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / QosNeutronPortChangeListener.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.ports.rev150712.ports.attributes.Ports;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosPortExtension;
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 QosNeutronPortChangeListener extends AsyncClusteredDataTreeChangeListenerBase<Port,
29                                              QosNeutronPortChangeListener> implements RecoverableListener {
30     private static final Logger LOG = LoggerFactory.getLogger(QosNeutronPortChangeListener.class);
31     private final DataBroker dataBroker;
32     private final QosNeutronUtils qosNeutronUtils;
33
34     @Inject
35     public QosNeutronPortChangeListener(final DataBroker dataBroker,
36             final QosNeutronUtils qosNeutronUtils, final QosServiceRecoveryHandler qosServiceRecoveryHandler,
37                                         final ServiceRecoveryRegistry serviceRecoveryRegistry) {
38         super(Port.class, QosNeutronPortChangeListener.class);
39         this.dataBroker = dataBroker;
40         this.qosNeutronUtils = qosNeutronUtils;
41         serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
42                 this);
43         LOG.debug("{} created",  getClass().getSimpleName());
44     }
45
46     @PostConstruct
47     public void init() {
48         registerListener();
49         LOG.debug("{} init and registerListener done", getClass().getSimpleName());
50     }
51
52     @Override
53     protected InstanceIdentifier<Port> getWildCardPath() {
54         return InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class);
55     }
56
57     @Override
58     public void registerListener() {
59         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
60     }
61
62     @Override
63     protected QosNeutronPortChangeListener getDataTreeChangeListener() {
64         return QosNeutronPortChangeListener.this;
65     }
66
67     @Override
68     protected void add(InstanceIdentifier<Port> instanceIdentifier, Port port) {
69         qosNeutronUtils.addToPortCache(port);
70     }
71
72     @Override
73     protected void remove(InstanceIdentifier<Port> instanceIdentifier, Port port) {
74         qosNeutronUtils.removeFromPortCache(port);
75     }
76
77     @Override
78     protected void update(InstanceIdentifier<Port> instanceIdentifier, Port original, Port update) {
79         qosNeutronUtils.addToPortCache(update);
80         // check for QoS updates
81         QosPortExtension updateQos = update.getAugmentation(QosPortExtension.class);
82         QosPortExtension originalQos = original.getAugmentation(QosPortExtension.class);
83
84         if (originalQos == null && updateQos != null) {
85             // qosservice policy add
86             qosNeutronUtils.addToQosPortsCache(updateQos.getQosPolicyId(), update);
87             qosNeutronUtils.handleNeutronPortQosAdd(update, updateQos.getQosPolicyId());
88         } else if (originalQos != null && updateQos != null
89                 && !originalQos.getQosPolicyId().equals(updateQos.getQosPolicyId())) {
90
91             // qosservice policy update
92             qosNeutronUtils.removeFromQosPortsCache(originalQos.getQosPolicyId(), original);
93             qosNeutronUtils.addToQosPortsCache(updateQos.getQosPolicyId(), update);
94             qosNeutronUtils.handleNeutronPortQosUpdate(update, updateQos.getQosPolicyId(),
95                     originalQos.getQosPolicyId());
96         } else if (originalQos != null && updateQos == null) {
97             // qosservice policy delete
98             qosNeutronUtils.handleNeutronPortQosRemove(original, originalQos.getQosPolicyId());
99             qosNeutronUtils.removeFromQosPortsCache(originalQos.getQosPolicyId(), original);
100         }
101
102     }
103 }