aed211a47323cc7d7efa5a81d4e375f3d6750173
[netvirt.git] /
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
10 package org.opendaylight.netvirt.qosservice;
11
12
13
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;
36
37 @Singleton
38 public class QosInterfaceStateChangeListener extends AsyncDataTreeChangeListenerBase<Interface,
39         QosInterfaceStateChangeListener> implements
40         AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(QosInterfaceStateChangeListener.class);
43
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;
49
50     @Inject
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());
62     }
63
64     @Override
65     @PostConstruct
66     public void init() {
67         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
68         LOG.info("{} init and registerListener done", getClass().getSimpleName());
69     }
70
71     @Override
72     protected InstanceIdentifier<Interface> getWildCardPath() {
73         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
74     }
75
76     @Override
77     protected QosInterfaceStateChangeListener getDataTreeChangeListener() {
78         return QosInterfaceStateChangeListener.this;
79     }
80
81     @Override
82     @SuppressWarnings("checkstyle:IllegalCatch")
83     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
84         try {
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,
96                                     port, portQosUuid);
97                         }
98
99                     } else {
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);
105                             }
106                         }
107                     }
108                     if (QosNeutronUtils.hasBandwidthLimitRule(neutronVpnManager, port)) {
109                         QosAlertManager.addToQosAlertCache(port);
110                     }
111                 });
112             }
113         } catch (Exception e) {
114             LOG.error("Qos:Exception caught in Interface Operational State Up event", e);
115         }
116     }
117
118     private Optional<Port> getNeutronPort(String portName) {
119         return uuidUtil.newUuidIfValidPattern(portName)
120                 .transform(uuid -> neutronVpnManager.getNeutronPort(uuid));
121     }
122
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);
128             if (port != null) {
129                 return uuid.transform(uuid1 -> neutronVpnManager.getNeutronPort(uuid1));
130             }
131             LOG.trace("Qos Service : interface {} clearing stale flow entries if any", portName);
132             QosNeutronUtils.removeStaleFlowEntry(dataBroker,mdsalUtils,odlInterfaceRpcService,intrf);
133         }
134         return Optional.absent();
135     }
136
137     @Override
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);
144
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);
153                 } else {
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);
160                         }
161                     }
162                 }
163             });
164         }
165     }
166
167     @Override
168     protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
169     }
170 }
171
172