Fixup Augmentable and Identifiable methods changing
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / QosInterfaceStateChangeListener.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.qosservice;
10
11 import com.google.common.base.Optional;
12 import javax.annotation.PostConstruct;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
18 import org.opendaylight.genius.srm.RecoverableListener;
19 import org.opendaylight.genius.srm.ServiceRecoveryRegistry;
20 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
21 import org.opendaylight.netvirt.qosservice.recovery.QosServiceRecoveryHandler;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosNetworkExtension;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosPortExtension;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @Singleton
35 public class QosInterfaceStateChangeListener extends AsyncClusteredDataTreeChangeListenerBase<Interface,
36         QosInterfaceStateChangeListener> implements RecoverableListener {
37
38     private static final Logger LOG = LoggerFactory.getLogger(QosInterfaceStateChangeListener.class);
39
40     private final DataBroker dataBroker;
41     private final UuidUtil uuidUtil;
42     private final QosAlertManager qosAlertManager;
43     private final QosNeutronUtils qosNeutronUtils;
44     private final INeutronVpnManager neutronVpnManager;
45
46     @Inject
47     public QosInterfaceStateChangeListener(final DataBroker dataBroker, final QosAlertManager qosAlertManager,
48                                            final QosNeutronUtils qosNeutronUtils,
49                                            final INeutronVpnManager neutronVpnManager,
50                                            final ServiceRecoveryRegistry serviceRecoveryRegistry,
51                                            final QosServiceRecoveryHandler qosServiceRecoveryHandler) {
52         super(Interface.class, QosInterfaceStateChangeListener.class);
53         this.dataBroker = dataBroker;
54         this.uuidUtil = new UuidUtil();
55         this.qosAlertManager = qosAlertManager;
56         this.qosNeutronUtils = qosNeutronUtils;
57         this.neutronVpnManager = neutronVpnManager;
58         serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
59                 this);
60         LOG.debug("{} created",  getClass().getSimpleName());
61     }
62
63     @PostConstruct
64     public void init() {
65         registerListener();
66         LOG.debug("{} init and registerListener done", getClass().getSimpleName());
67     }
68
69     @Override
70     public void registerListener() {
71         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
72     }
73
74     @Override
75     protected InstanceIdentifier<Interface> getWildCardPath() {
76         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
77     }
78
79     @Override
80     protected QosInterfaceStateChangeListener getDataTreeChangeListener() {
81         return QosInterfaceStateChangeListener.this;
82     }
83
84     @Override
85     @SuppressWarnings("checkstyle:IllegalCatch")
86     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
87         try {
88             if (L2vlan.class.equals(intrf.getType())) {
89                 final String interfaceName = intrf.getName();
90                 getNeutronPort(interfaceName).ifPresent(port -> {
91                     Network network = qosNeutronUtils.getNeutronNetwork(port.getNetworkId());
92                     LOG.trace("Qos Service : Received interface {} PORT UP event ", interfaceName);
93                     if (port.augmentation(QosPortExtension.class) != null) {
94                         Uuid portQosUuid = port.augmentation(QosPortExtension.class).getQosPolicyId();
95                         if (portQosUuid != null) {
96                             qosNeutronUtils.addToQosPortsCache(portQosUuid, port);
97                             qosNeutronUtils.handleQosInterfaceAdd(port, portQosUuid);
98                         }
99                     } else {
100                         if (network.augmentation(QosNetworkExtension.class) != null) {
101                             Uuid networkQosUuid = network.augmentation(QosNetworkExtension.class).getQosPolicyId();
102                             if (networkQosUuid != null) {
103                                 qosNeutronUtils.handleQosInterfaceAdd(port, networkQosUuid);
104                             }
105                         }
106                     }
107                     qosAlertManager.processInterfaceUpEvent(interfaceName);
108                 });
109             }
110         } catch (Exception e) {
111             LOG.error("Qos:Exception caught in Interface Operational State Up event {}", e);
112         }
113     }
114
115     private java.util.Optional<Port> getNeutronPort(String portName) {
116         return uuidUtil.newUuidIfValidPattern(portName)
117                 .toJavaUtil()
118                 .map(qosNeutronUtils::getNeutronPort);
119     }
120
121     private Optional<Port> getNeutronPortForRemove(Interface intrf) {
122         final String portName = intrf.getName();
123         Optional<Uuid> uuid = uuidUtil.newUuidIfValidPattern(portName);
124         if (uuid.isPresent()) {
125             Port port = qosNeutronUtils.getNeutronPort(portName);
126             if (port != null) {
127                 return Optional.fromJavaUtil(uuid.toJavaUtil().map(qosNeutronUtils::getNeutronPort));
128             }
129             LOG.trace("Qos Service : interface {} clearing stale flow entries if any", portName);
130             qosNeutronUtils.removeStaleFlowEntry(intrf);
131         }
132         return Optional.absent();
133     }
134
135     @Override
136     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
137         if (L2vlan.class.equals(intrf.getType())) {
138             final String interfaceName = intrf.getName();
139             // Guava Optional asSet().forEach() emulates Java 8 Optional ifPresent()
140             getNeutronPortForRemove(intrf).asSet().forEach(port -> {
141                 LOG.trace("Qos Service : Received interface {} PORT DOWN event ", interfaceName);
142
143                 String lowerLayerIf = intrf.getLowerLayerIf().get(0);
144                 LOG.trace("lowerLayerIf {}", lowerLayerIf);
145                 qosAlertManager.removeLowerLayerIfFromQosAlertCache(lowerLayerIf);
146                 QosPortExtension removeQos = port.augmentation(QosPortExtension.class);
147                 if (removeQos != null) {
148                     qosNeutronUtils.handleNeutronPortRemove(port, removeQos.getQosPolicyId(), intrf);
149                     qosNeutronUtils.removeFromQosPortsCache(removeQos.getQosPolicyId(), port);
150                 } else {
151                     Network network = qosNeutronUtils.getNeutronNetwork(port.getNetworkId());
152                     if (network != null && network.augmentation(QosNetworkExtension.class) != null) {
153                         Uuid networkQosUuid = network.augmentation(QosNetworkExtension.class).getQosPolicyId();
154                         if (networkQosUuid != null) {
155                             qosNeutronUtils.handleNeutronPortRemove(port, networkQosUuid, intrf);
156                         }
157                     }
158                 }
159             });
160         }
161     }
162
163     @Override
164     protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
165         if (original.getType() == null && L2vlan.class.equals(update.getType())) {
166             // IfType was missing at creation, add it now
167             add(identifier, update);
168         }
169     }
170 }
171
172