Bug 5064 : Deletion of TEP is not removing the tunnel ingress flow from the switch
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / confighelpers / OvsInterfaceConfigRemoveHelper.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.vpnservice.interfacemgr.renderer.ovs.confighelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.idmanager.IdManager;
15 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
16 import org.opendaylight.vpnservice.interfacemgr.commons.AlivenessMonitorUtils;
17 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
18 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
19 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.utilities.SouthboundUtils;
20 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
21 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorStopInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorStopInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntry;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import java.math.BigInteger;
52 import java.util.ArrayList;
53 import java.util.List;
54
55 public class OvsInterfaceConfigRemoveHelper {
56     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceConfigRemoveHelper.class);
57
58     public static List<ListenableFuture<Void>> removeConfiguration(DataBroker dataBroker, AlivenessMonitorService alivenessMonitorService,
59                                                                    Interface interfaceOld,
60                                                                    IdManagerService idManager,
61                                                                    IMdsalApiManager mdsalApiManager,
62                                                                    ParentRefs parentRefs) {
63         List<ListenableFuture<Void>> futures = new ArrayList<>();
64         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
65
66         IfTunnel ifTunnel = interfaceOld.getAugmentation(IfTunnel.class);
67         if (ifTunnel != null) {
68             removeTunnelConfiguration(alivenessMonitorService, parentRefs, dataBroker, interfaceOld,
69                     idManager, mdsalApiManager, futures);
70         }else {
71             removeVlanConfiguration(dataBroker, interfaceOld, t);
72             futures.add(t.submit());
73         }
74         return futures;
75     }
76
77     private static void removeVlanConfiguration(DataBroker dataBroker, Interface interfaceOld, WriteTransaction t) {
78         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
79                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceOld.getName(), dataBroker);
80         if (ifState == null) {
81             return;
82         }
83
84         String ncStr = ifState.getLowerLayerIf().get(0);
85         NodeConnectorId nodeConnectorId = new NodeConnectorId(ncStr);
86         NodeConnector nodeConnector =
87                 InterfaceManagerCommonUtils.getNodeConnectorFromInventoryOperDS(nodeConnectorId, dataBroker);
88         if(nodeConnector != null) {
89             FlowCapableNodeConnector flowCapableNodeConnector =
90                     nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
91             //State state = flowCapableNodeConnector.getState();
92             OperStatus operStatus = flowCapableNodeConnector == null ? OperStatus.Down : OperStatus.Up;
93
94             if (ifState.getOperStatus() != operStatus) {
95                 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
96                         IfmUtil.buildStateInterfaceId(interfaceOld.getName());
97                 InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
98                 ifaceBuilder.setOperStatus(operStatus);
99                 ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceOld.getName()));
100                 t.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
101             }
102         }
103         IfL2vlan ifL2vlan = interfaceOld.getAugmentation(IfL2vlan.class);
104         if (ifL2vlan == null || ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.Trunk) {
105             return;
106         }
107
108         // For Vlan-Trunk Interface, remove the trunk-member operstates as well...
109         InterfaceKey interfaceKey = new InterfaceKey(interfaceOld.getName());
110         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
111                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
112         if (iface == null) {
113             return;
114         }
115
116         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(iface.getName());
117         InterfaceParentEntry interfaceParentEntry =
118                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
119         if (interfaceParentEntry == null) {
120             return;
121         }
122
123         List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.getInterfaceChildEntry();
124         if (interfaceChildEntries == null) {
125             return;
126         }
127
128         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
129         for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
130             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
131                     IfmUtil.buildStateInterfaceId(interfaceChildEntry.getChildInterface());
132             t.delete(LogicalDatastoreType.OPERATIONAL, ifChildStateId);
133         }
134     }
135
136     private static void removeTunnelConfiguration(AlivenessMonitorService alivenessMonitorService, ParentRefs parentRefs,
137                                                   DataBroker dataBroker, Interface interfaceOld,
138                                                   IdManagerService idManager, IMdsalApiManager mdsalApiManager,
139                                                   List<ListenableFuture<Void>> futures) {
140
141         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
142         BigInteger dpId = null;
143         if (parentRefs != null) {
144             dpId = parentRefs.getDatapathNodeIdentifier();
145         }
146
147         if (dpId == null) {
148             return;
149         }
150
151         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpId);
152         InstanceIdentifier<BridgeRefEntry> bridgeRefEntryIid =
153                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
154         BridgeRefEntry bridgeRefEntry =
155                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(bridgeRefEntryIid, dataBroker);
156
157         if (bridgeRefEntry != null) {
158             InstanceIdentifier<?> bridgeIid = bridgeRefEntry.getBridgeReference().getValue();
159             InstanceIdentifier<TerminationPoint> tpIid = SouthboundUtils.createTerminationPointInstanceIdentifier(
160                     InstanceIdentifier.keyOf(bridgeIid.firstIdentifierOf(Node.class)), interfaceOld.getName());
161             t.delete(LogicalDatastoreType.CONFIGURATION, tpIid);
162
163             // delete tunnel ingress flow
164             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceOld.getName(), dataBroker);
165             NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(interfaceOld, dataBroker);
166             long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(ncId));
167             InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager,
168                     interfaceOld.getAugmentation(IfTunnel.class),
169                     dpId, portNo, interfaceOld,
170                     NwConstants.DEL_FLOW);
171         }
172
173         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
174         InstanceIdentifier<BridgeEntry> bridgeEntryIid = InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
175         BridgeEntry bridgeEntry = InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryIid, dataBroker);
176         if (bridgeEntry == null) {
177             return;
178         }
179
180         List<BridgeInterfaceEntry> bridgeInterfaceEntries = bridgeEntry.getBridgeInterfaceEntry();
181         if (bridgeInterfaceEntries == null) {
182             return;
183         }
184
185         if (bridgeInterfaceEntries.size() <= 1) {
186             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeEntryIid);
187         } else {
188             BridgeInterfaceEntryKey bridgeInterfaceEntryKey =
189                     new BridgeInterfaceEntryKey(interfaceOld.getName());
190             InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryIid =
191                     InterfaceMetaUtils.getBridgeInterfaceEntryIdentifier(bridgeEntryKey,
192                             bridgeInterfaceEntryKey);
193             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryIid);
194         }
195         futures.add(t.submit());
196         // stop LLDP monitoring for the tunnel interface
197         AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceOld);
198     }
199 }