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
10 package org.opendaylight.netvirt.qosservice;
14 import com.google.common.base.Optional;
15 import javax.annotation.PostConstruct;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
21 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
22 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
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.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosNetworkExtension;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.qos.ext.rev160613.QosPortExtension;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public class QosInterfaceStateChangeListener extends AsyncDataTreeChangeListenerBase<Interface,
39 QosInterfaceStateChangeListener> implements
42 private static final Logger LOG = LoggerFactory.getLogger(QosInterfaceStateChangeListener.class);
44 private final DataBroker dataBroker;
45 private final OdlInterfaceRpcService odlInterfaceRpcService;
46 private final INeutronVpnManager neutronVpnManager;
47 private final IMdsalApiManager mdsalUtils;
48 private final UuidUtil uuidUtil;
51 public QosInterfaceStateChangeListener(final DataBroker dataBroker,
52 final OdlInterfaceRpcService odlInterfaceRpcService,
53 final INeutronVpnManager neutronVpnManager,
54 final IMdsalApiManager mdsalUtils) {
55 super(Interface.class, QosInterfaceStateChangeListener.class);
56 this.dataBroker = dataBroker;
57 this.odlInterfaceRpcService = odlInterfaceRpcService;
58 this.neutronVpnManager = neutronVpnManager;
59 this.mdsalUtils = mdsalUtils;
60 this.uuidUtil = new UuidUtil();
61 LOG.info("{} created", getClass().getSimpleName());
67 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
68 LOG.info("{} init and registerListener done", getClass().getSimpleName());
72 protected InstanceIdentifier<Interface> getWildCardPath() {
73 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
77 protected QosInterfaceStateChangeListener getDataTreeChangeListener() {
78 return QosInterfaceStateChangeListener.this;
82 @SuppressWarnings("checkstyle:IllegalCatch")
83 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
85 if (!Tunnel.class.equals(intrf.getType())) {
86 final String interfaceName = intrf.getName();
87 // Guava Optional asSet().forEach() emulates Java 8 Optional ifPresent()
88 getNeutronPort(interfaceName).asSet().forEach(port -> {
89 Network network = neutronVpnManager.getNeutronNetwork(port.getNetworkId());
90 LOG.trace("Qos Service : Received interface {} PORT UP event ", interfaceName);
91 if (port.getAugmentation(QosPortExtension.class) != null) {
92 Uuid portQosUuid = port.getAugmentation(QosPortExtension.class).getQosPolicyId();
93 if (portQosUuid != null) {
94 QosNeutronUtils.addToQosPortsCache(portQosUuid, port);
95 QosNeutronUtils.handleNeutronPortQosAdd(dataBroker, odlInterfaceRpcService, mdsalUtils,
100 if (network.getAugmentation(QosNetworkExtension.class) != null) {
101 Uuid networkQosUuid = network.getAugmentation(QosNetworkExtension.class).getQosPolicyId();
102 if (networkQosUuid != null) {
103 QosNeutronUtils.handleNeutronPortQosAdd(dataBroker, odlInterfaceRpcService, mdsalUtils,
104 port, networkQosUuid);
108 if (QosNeutronUtils.hasBandwidthLimitRule(neutronVpnManager, port)) {
109 QosAlertManager.addToQosAlertCache(port);
113 } catch (Exception e) {
114 LOG.error("Qos:Exception caught in Interface Operational State Up event", e);
118 private Optional<Port> getNeutronPort(String portName) {
119 return uuidUtil.newUuidIfValidPattern(portName)
120 .transform(uuid -> neutronVpnManager.getNeutronPort(uuid));
123 private Optional<Port> getNeutronPortForRemove(Interface intrf) {
124 final String portName = intrf.getName();
125 Optional<Uuid> uuid = uuidUtil.newUuidIfValidPattern(portName);
126 if (uuid.isPresent()) {
127 Port port = neutronVpnManager.getNeutronPort(portName);
129 return uuid.transform(uuid1 -> neutronVpnManager.getNeutronPort(uuid1));
131 LOG.trace("Qos Service : interface {} clearing stale flow entries if any", portName);
132 QosNeutronUtils.removeStaleFlowEntry(dataBroker,mdsalUtils,odlInterfaceRpcService,intrf);
134 return Optional.absent();
138 protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
139 if (!Tunnel.class.equals(intrf.getType())) {
140 final String interfaceName = intrf.getName();
141 // Guava Optional asSet().forEach() emulates Java 8 Optional ifPresent()
142 getNeutronPortForRemove(intrf).asSet().forEach(port -> {
143 LOG.trace("Qos Service : Received interface {} PORT DOWN event ", interfaceName);
145 String lowerLayerIf = intrf.getLowerLayerIf().get(0);
146 LOG.trace("lowerLayerIf {}", lowerLayerIf);
147 QosAlertManager.removeFromQosAlertCache(new NodeConnectorId(lowerLayerIf));
148 QosPortExtension removeQos = port.getAugmentation(QosPortExtension.class);
149 if (removeQos != null) {
150 QosNeutronUtils.handleNeutronPortRemove(dataBroker, odlInterfaceRpcService,
151 mdsalUtils, port, removeQos.getQosPolicyId(), intrf);
152 QosNeutronUtils.removeFromQosPortsCache(removeQos.getQosPolicyId(), port);
154 Network network = neutronVpnManager.getNeutronNetwork(port.getNetworkId());
155 if (network != null && network.getAugmentation(QosNetworkExtension.class) != null) {
156 Uuid networkQosUuid = network.getAugmentation(QosNetworkExtension.class).getQosPolicyId();
157 if (networkQosUuid != null) {
158 QosNeutronUtils.handleNeutronPortRemove(dataBroker, odlInterfaceRpcService,
159 mdsalUtils, port, networkQosUuid, intrf);
168 protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {