Fixing issues with batching transactions during bind service
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / statehelpers / OvsInterfaceStateRemoveHelper.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.statehelpers;
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.vpnservice.interfacemgr.IfmUtil;
15 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
16 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
17 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
18 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
19 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.math.BigInteger;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 public class OvsInterfaceStateRemoveHelper {
41     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateRemoveHelper.class);
42
43     public static List<ListenableFuture<Void>> removeState(IdManagerService idManager, IMdsalApiManager mdsalApiManager,
44                                                            InstanceIdentifier<FlowCapableNodeConnector> key,
45                                                            DataBroker dataBroker, String portName, FlowCapableNodeConnector fcNodeConnectorOld) {
46         LOG.debug("Removing interface-state for port: {}", portName);
47         List<ListenableFuture<Void>> futures = new ArrayList<>();
48         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
49
50         InstanceIdentifier<Interface> ifStateId = IfmUtil.buildStateInterfaceId(portName);
51         /* Remove entry from if-index-interface-name map and deallocate Id from Idmanager. */
52         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =
53                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(portName, dataBroker);
54         InterfaceMetaUtils.removeLportTagInterfaceMap(transaction, idManager, dataBroker, interfaceState.getName(), interfaceState.getIfIndex());
55
56         transaction.delete(LogicalDatastoreType.OPERATIONAL, ifStateId);
57
58         // For Vlan-Trunk Interface, remove the trunk-member operstates as well...
59         InterfaceKey interfaceKey = new InterfaceKey(portName);
60         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
61                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
62         if (iface == null) {
63             futures.add(transaction.submit());
64             return futures;
65         }
66
67         // If this interface is a tunnel interface, remove the tunnel ingress flow
68         IfTunnel tunnel = iface.getAugmentation(IfTunnel.class);
69         if(tunnel != null){
70             NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(key.firstIdentifierOf(NodeConnector.class)).getId();
71             BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
72             long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
73             InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager, tunnel, dpId, portNo, iface,
74                     NwConstants.DEL_FLOW);
75             return futures;
76         }
77
78         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(iface.getName());
79         InterfaceParentEntry interfaceParentEntry =
80                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
81         if (interfaceParentEntry == null) {
82             futures.add(transaction.submit());
83             return futures;
84         }
85
86         List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.getInterfaceChildEntry();
87         if (interfaceChildEntries == null) {
88             futures.add(transaction.submit());
89             return futures;
90         }
91
92         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
93         for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
94             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
95                     IfmUtil.buildStateInterfaceId(interfaceChildEntry.getChildInterface());
96             transaction.delete(LogicalDatastoreType.OPERATIONAL, ifChildStateId);
97         }
98
99        /* Below code will be needed if we want to update the vlan-trunk in the of-port.
100        NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(key.firstIdentifierOf(NodeConnector.class)).getId();
101         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
102
103         BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
104         InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
105                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
106         BridgeRefEntry bridgeRefEntry =
107                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
108         if (bridgeRefEntry == null) {
109             futures.add(t.submit());
110             return futures;
111         }
112
113         InstanceIdentifier<?> bridgeIid = bridgeRefEntry.getBridgeReference().getValue();
114         InstanceIdentifier<TerminationPoint> tpIid = SouthboundUtils.createTerminationPointInstanceIdentifier(
115                 InstanceIdentifier.keyOf(bridgeIid.firstIdentifierOf(Node.class)), interfaceOld.getName());
116         t.delete(LogicalDatastoreType.CONFIGURATION, tpIid); */
117
118         futures.add(transaction.submit());
119         return futures;
120     }
121 }