Migrate to serviceutils/tools and serviceutils/srm
[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.mdsalutil.NwConstants;
19 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
20 import org.opendaylight.netvirt.qosservice.recovery.QosServiceRecoveryHandler;
21 import org.opendaylight.serviceutils.srm.RecoverableListener;
22 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosNetworkExtension;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosPortExtension;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 @Singleton
36 public class QosInterfaceStateChangeListener extends AsyncClusteredDataTreeChangeListenerBase<Interface,
37         QosInterfaceStateChangeListener> implements RecoverableListener {
38
39     private static final Logger LOG = LoggerFactory.getLogger(QosInterfaceStateChangeListener.class);
40
41     private final DataBroker dataBroker;
42     private final UuidUtil uuidUtil;
43     private final QosAlertManager qosAlertManager;
44     private final QosNeutronUtils qosNeutronUtils;
45     private final INeutronVpnManager neutronVpnManager;
46
47     @Inject
48     public QosInterfaceStateChangeListener(final DataBroker dataBroker, final QosAlertManager qosAlertManager,
49                                            final QosNeutronUtils qosNeutronUtils,
50                                            final INeutronVpnManager neutronVpnManager,
51                                            final ServiceRecoveryRegistry serviceRecoveryRegistry,
52                                            final QosServiceRecoveryHandler qosServiceRecoveryHandler) {
53         super(Interface.class, QosInterfaceStateChangeListener.class);
54         this.dataBroker = dataBroker;
55         this.uuidUtil = new UuidUtil();
56         this.qosAlertManager = qosAlertManager;
57         this.qosNeutronUtils = qosNeutronUtils;
58         this.neutronVpnManager = neutronVpnManager;
59         serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
60                 this);
61         LOG.trace("{} created",  getClass().getSimpleName());
62     }
63
64     @PostConstruct
65     public void init() {
66         registerListener();
67         LOG.trace("{} init and registerListener done", getClass().getSimpleName());
68     }
69
70     @Override
71     public void registerListener() {
72         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
73     }
74
75     @Override
76     protected InstanceIdentifier<Interface> getWildCardPath() {
77         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
78     }
79
80     @Override
81     protected QosInterfaceStateChangeListener getDataTreeChangeListener() {
82         return QosInterfaceStateChangeListener.this;
83     }
84
85     @Override
86     @SuppressWarnings("checkstyle:IllegalCatch")
87     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
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.debug("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     }
111
112     private java.util.Optional<Port> getNeutronPort(String portName) {
113         return uuidUtil.newUuidIfValidPattern(portName)
114                 .toJavaUtil()
115                 .map(qosNeutronUtils::getNeutronPort);
116     }
117
118     private Optional<Port> getNeutronPortForRemove(Interface intrf) {
119         final String portName = intrf.getName();
120         Optional<Uuid> uuid = uuidUtil.newUuidIfValidPattern(portName);
121         if (uuid.isPresent()) {
122             Port port = qosNeutronUtils.getNeutronPort(portName);
123             if (port != null) {
124                 return Optional.fromJavaUtil(uuid.toJavaUtil().map(qosNeutronUtils::getNeutronPort));
125             }
126             LOG.trace("Qos Service : interface {} clearing stale flow entries if any", portName);
127             qosNeutronUtils.removeStaleFlowEntry(intrf, NwConstants.ETHTYPE_IPV4);
128             qosNeutronUtils.removeStaleFlowEntry(intrf, NwConstants.ETHTYPE_IPV6);
129         }
130         return Optional.absent();
131     }
132
133     @Override
134     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
135         if (L2vlan.class.equals(intrf.getType())) {
136             final String interfaceName = intrf.getName();
137             // Guava Optional asSet().forEach() emulates Java 8 Optional ifPresent()
138             getNeutronPortForRemove(intrf).asSet().forEach(port -> {
139                 LOG.trace("Qos Service : Received interface {} PORT DOWN event ", interfaceName);
140
141                 String lowerLayerIf = intrf.getLowerLayerIf().get(0);
142                 LOG.trace("lowerLayerIf {}", lowerLayerIf);
143                 qosAlertManager.removeLowerLayerIfFromQosAlertCache(lowerLayerIf);
144                 QosPortExtension removeQos = port.augmentation(QosPortExtension.class);
145                 if (removeQos != null) {
146                     qosNeutronUtils.handleNeutronPortRemove(port, removeQos.getQosPolicyId(), intrf);
147                     qosNeutronUtils.removeFromQosPortsCache(removeQos.getQosPolicyId(), port);
148                 } else {
149                     Network network = qosNeutronUtils.getNeutronNetwork(port.getNetworkId());
150                     if (network != null && network.augmentation(QosNetworkExtension.class) != null) {
151                         Uuid networkQosUuid = network.augmentation(QosNetworkExtension.class).getQosPolicyId();
152                         if (networkQosUuid != null) {
153                             qosNeutronUtils.handleNeutronPortRemove(port, networkQosUuid, intrf);
154                         }
155                     }
156                 }
157             });
158         }
159     }
160
161     @Override
162     protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
163         if (original.getType() == null && L2vlan.class.equals(update.getType())) {
164             // IfType was missing at creation, add it now
165             add(identifier, update);
166         }
167     }
168 }
169
170