2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.qosservice;
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;
35 public class QosInterfaceStateChangeListener extends AsyncClusteredDataTreeChangeListenerBase<Interface,
36 QosInterfaceStateChangeListener> implements RecoverableListener {
38 private static final Logger LOG = LoggerFactory.getLogger(QosInterfaceStateChangeListener.class);
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;
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(),
60 LOG.debug("{} created", getClass().getSimpleName());
66 LOG.debug("{} init and registerListener done", getClass().getSimpleName());
70 public void registerListener() {
71 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
75 protected InstanceIdentifier<Interface> getWildCardPath() {
76 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
80 protected QosInterfaceStateChangeListener getDataTreeChangeListener() {
81 return QosInterfaceStateChangeListener.this;
85 @SuppressWarnings("checkstyle:IllegalCatch")
86 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.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);
100 if (network.augmentation(QosNetworkExtension.class) != null) {
101 Uuid networkQosUuid = network.augmentation(QosNetworkExtension.class).getQosPolicyId();
102 if (networkQosUuid != null) {
103 qosNeutronUtils.handleQosInterfaceAdd(port, networkQosUuid);
107 qosAlertManager.processInterfaceUpEvent(interfaceName);
110 } catch (Exception e) {
111 LOG.error("Qos:Exception caught in Interface Operational State Up event {}", e);
115 private java.util.Optional<Port> getNeutronPort(String portName) {
116 return uuidUtil.newUuidIfValidPattern(portName)
118 .map(qosNeutronUtils::getNeutronPort);
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);
127 return Optional.fromJavaUtil(uuid.toJavaUtil().map(qosNeutronUtils::getNeutronPort));
129 LOG.trace("Qos Service : interface {} clearing stale flow entries if any", portName);
130 qosNeutronUtils.removeStaleFlowEntry(intrf);
132 return Optional.absent();
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);
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);
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);
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);