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