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