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 java.util.Collections;
12 import java.util.Optional;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.genius.mdsalutil.NwConstants;
17 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
18 import org.opendaylight.infrautils.utils.concurrent.Executors;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
22 import org.opendaylight.netvirt.qosservice.recovery.QosServiceRecoveryHandler;
23 import org.opendaylight.serviceutils.srm.RecoverableListener;
24 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
25 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosNetworkExtension;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosPortExtension;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 public class QosInterfaceStateChangeListener extends AbstractClusteredAsyncDataTreeChangeListener<Interface>
40 implements RecoverableListener {
42 private static final Logger LOG = LoggerFactory.getLogger(QosInterfaceStateChangeListener.class);
44 private final DataBroker dataBroker;
45 private final UuidUtil uuidUtil;
46 private final QosAlertManager qosAlertManager;
47 private final QosNeutronUtils qosNeutronUtils;
48 private final INeutronVpnManager neutronVpnManager;
49 private final JobCoordinator jobCoordinator;
52 public QosInterfaceStateChangeListener(final DataBroker dataBroker, final QosAlertManager qosAlertManager,
53 final QosNeutronUtils qosNeutronUtils,
54 final INeutronVpnManager neutronVpnManager,
55 final ServiceRecoveryRegistry serviceRecoveryRegistry,
56 final QosServiceRecoveryHandler qosServiceRecoveryHandler,
57 final JobCoordinator jobCoordinator) {
58 super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(InterfacesState.class)
59 .child(Interface.class),
60 Executors.newListeningSingleThreadExecutor("QosInterfaceStateChangeListener", LOG));
61 this.dataBroker = dataBroker;
62 this.uuidUtil = new UuidUtil();
63 this.qosAlertManager = qosAlertManager;
64 this.qosNeutronUtils = qosNeutronUtils;
65 this.neutronVpnManager = neutronVpnManager;
66 this.jobCoordinator = jobCoordinator;
67 serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
69 LOG.trace("{} created", getClass().getSimpleName());
73 LOG.trace("{} init and registerListener done", getClass().getSimpleName());
80 Executors.shutdownAndAwaitTermination(getExecutorService());
84 public void registerListener() {
89 public void deregisterListener() {
95 @SuppressWarnings("checkstyle:IllegalCatch")
96 public void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
97 if (L2vlan.class.equals(intrf.getType())) {
98 final String interfaceName = intrf.getName();
99 getNeutronPort(interfaceName).ifPresent(port -> {
100 Network network = qosNeutronUtils.getNeutronNetwork(port.getNetworkId());
101 LOG.debug("Qos Service : Received interface {} PORT UP event ", interfaceName);
102 if (port.augmentation(QosPortExtension.class) != null) {
103 Uuid portQosUuid = port.augmentation(QosPortExtension.class).getQosPolicyId();
104 if (portQosUuid != null) {
105 qosNeutronUtils.addToQosPortsCache(portQosUuid, port);
106 qosNeutronUtils.handleQosInterfaceAdd(port, portQosUuid);
109 if (network.augmentation(QosNetworkExtension.class) != null) {
110 Uuid networkQosUuid = network.augmentation(QosNetworkExtension.class).getQosPolicyId();
111 if (networkQosUuid != null) {
112 qosNeutronUtils.handleQosInterfaceAdd(port, networkQosUuid);
116 qosAlertManager.processInterfaceUpEvent(interfaceName);
121 private java.util.Optional<Port> getNeutronPort(String portName) {
122 return uuidUtil.newUuidIfValidPattern(portName)
123 .map(qosNeutronUtils::getNeutronPort);
126 private Optional<Port> getNeutronPortForRemove(Interface intrf) {
127 final String portName = intrf.getName();
128 Optional<Uuid> uuid = uuidUtil.newUuidIfValidPattern(portName);
129 if (uuid.isPresent()) {
130 Port port = qosNeutronUtils.getNeutronPort(portName);
132 return Optional.ofNullable(uuid.map(qosNeutronUtils::getNeutronPort).orElse(null));
134 if (qosNeutronUtils.isBindServiceDone(uuid)) {
135 LOG.trace("Qos Service : interface {} clearing stale flow entries if any", portName);
136 jobCoordinator.enqueueJob("QosPort-" + portName, () -> {
137 qosNeutronUtils.removeStaleFlowEntry(intrf, NwConstants.ETHTYPE_IPV4);
138 qosNeutronUtils.removeStaleFlowEntry(intrf, NwConstants.ETHTYPE_IPV6);
139 qosNeutronUtils.unbindservice(portName);
140 qosNeutronUtils.removeInterfaceInQosConfiguredPorts(uuid);
141 return Collections.emptyList();
145 return Optional.empty();
149 public void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
150 if (L2vlan.class.equals(intrf.getType())) {
151 final String interfaceName = intrf.getName();
152 // Guava Optional asSet().forEach() emulates Java 8 Optional ifPresent()
153 getNeutronPortForRemove(intrf).stream().forEach(port -> {
154 LOG.trace("Qos Service : Received interface {} PORT DOWN event ", interfaceName);
156 String lowerLayerIf = intrf.getLowerLayerIf().get(0);
157 LOG.trace("lowerLayerIf {}", lowerLayerIf);
158 qosAlertManager.removeLowerLayerIfFromQosAlertCache(lowerLayerIf);
159 QosPortExtension removeQos = port.augmentation(QosPortExtension.class);
160 if (removeQos != null) {
161 qosNeutronUtils.handleNeutronPortRemove(port, removeQos.getQosPolicyId(), intrf);
162 qosNeutronUtils.removeFromQosPortsCache(removeQos.getQosPolicyId(), port);
164 Network network = qosNeutronUtils.getNeutronNetwork(port.getNetworkId());
165 if (network != null && network.augmentation(QosNetworkExtension.class) != null) {
166 Uuid networkQosUuid = network.augmentation(QosNetworkExtension.class).getQosPolicyId();
167 if (networkQosUuid != null) {
168 qosNeutronUtils.handleNeutronPortRemove(port, networkQosUuid, intrf);
177 public void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
178 if (original.getType() == null && L2vlan.class.equals(update.getType())) {
179 // IfType was missing at creation, add it now
180 add(identifier, update);