Merge "Porting the modules from vpnservice to genius"
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / renderer / ovs / statehelpers / OvsInterfaceStateRemoveHelper.java
1 /*
2  * Copyright (c) 2016 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.genius.interfacemanager.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.genius.interfacemanager.IfmUtil;
15 import org.opendaylight.genius.interfacemanager.commons.AlivenessMonitorUtils;
16 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.genius.interfacemanager.commons.InterfaceMetaUtils;
18 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
19 import org.opendaylight.genius.mdsalutil.NwConstants;
20 import org.opendaylight.genius.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.genius.alivenessmonitor.rev160411.AlivenessMonitorService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntryKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info._interface.parent.entry.InterfaceChildEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.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                                                            AlivenessMonitorService alivenessMonitorService,
45                                                            InstanceIdentifier<FlowCapableNodeConnector> key,
46                                                            DataBroker dataBroker, String portName, FlowCapableNodeConnector fcNodeConnectorOld) {
47         LOG.debug("Removing interface-state for port: {}", portName);
48         List<ListenableFuture<Void>> futures = new ArrayList<>();
49         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
50
51         InstanceIdentifier<Interface> ifStateId = IfmUtil.buildStateInterfaceId(portName);
52
53         // delete the port entry from interface operational DS
54         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =
55                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(portName, dataBroker);
56         transaction.delete(LogicalDatastoreType.OPERATIONAL, ifStateId);
57
58         InterfaceKey interfaceKey = new InterfaceKey(portName);
59         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
60                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
61
62         NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(key.firstIdentifierOf(NodeConnector.class)).getId();
63         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
64         // If this interface is a tunnel interface, remove the tunnel ingress flow and stop lldp monitoring
65         if(iface != null) {
66             IfTunnel tunnel = iface.getAugmentation(IfTunnel.class);
67             if (tunnel != null) {
68                 long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
69                 InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager, tunnel, dpId, portNo, iface, -1,
70                         NwConstants.DEL_FLOW);
71                 futures.add(transaction.submit());
72                 AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, iface);
73                 return futures;
74             }
75         }
76
77         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(portName);
78         InterfaceParentEntry interfaceParentEntry =
79                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
80         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
81             futures.add(transaction.submit());
82             return futures;
83         }
84
85         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
86         InterfaceChildEntry higherlayerChild = interfaceParentEntry.getInterfaceChildEntry().get(0);
87         InstanceIdentifier<Interface>
88                 higerLayerChildIfStateId = IfmUtil.buildStateInterfaceId(higherlayerChild.getChildInterface());
89         Interface higherLayerIfChildState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(higherlayerChild.getChildInterface(), dataBroker);
90         if (interfaceState != null && higherLayerIfChildState != null) {
91             transaction.delete(LogicalDatastoreType.OPERATIONAL, higerLayerChildIfStateId);
92             FlowBasedServicesUtils.removeIngressFlow(higherLayerIfChildState.getName(), dpId, transaction);
93         }
94         // If this interface maps to a Vlan trunk entity, operational states of all the vlan-trunk-members
95         // should also be created here.
96         InterfaceParentEntryKey higherLayerParentEntryKey = new InterfaceParentEntryKey(higherlayerChild.getChildInterface());
97         InterfaceParentEntry higherLayerParent =
98                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(higherLayerParentEntryKey, dataBroker);
99
100         if(higherLayerParent != null && higherLayerParent.getInterfaceChildEntry() != null) {
101             for (InterfaceChildEntry interfaceChildEntry : higherLayerParent.getInterfaceChildEntry()) {
102                 InstanceIdentifier<Interface> ifChildStateId =
103                         IfmUtil.buildStateInterfaceId(interfaceChildEntry.getChildInterface());
104                 Interface childInterfaceState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceChildEntry.getChildInterface(), dataBroker);
105                 if (childInterfaceState != null) {
106                     transaction.delete(LogicalDatastoreType.OPERATIONAL, ifChildStateId);
107                     FlowBasedServicesUtils.removeIngressFlow(childInterfaceState.getName(), dpId, transaction);
108                 }
109             }
110         }
111
112
113        /* Below code will be needed if we want to update the vlan-trunk in the of-port.
114        NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(key.firstIdentifierOf(NodeConnector.class)).getId();
115         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
116
117         BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
118         InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
119                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
120         BridgeRefEntry bridgeRefEntry =
121                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
122         if (bridgeRefEntry == null) {
123             futures.add(t.submit());
124             return futures;
125         }
126
127         InstanceIdentifier<?> bridgeIid = bridgeRefEntry.getBridgeReference().getValue();
128         InstanceIdentifier<TerminationPoint> tpIid = SouthboundUtils.createTerminationPointInstanceIdentifier(
129                 InstanceIdentifier.keyOf(bridgeIid.firstIdentifierOf(Node.class)), interfaceOld.getName());
130         t.delete(LogicalDatastoreType.CONFIGURATION, tpIid); */
131
132         futures.add(transaction.submit());
133         return futures;
134     }
135 }