Bug 5182 - Customized name support for VLAN trunk should be supported
[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.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
21 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
22 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
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.OperStatus;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
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.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorStopInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorStopInputBuilder;
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.meta.rev151007.bridge._interface.info.BridgeEntry;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntry;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 import java.math.BigInteger;
53 import java.util.ArrayList;
54 import java.util.List;
55
56 public class OvsInterfaceConfigRemoveHelper {
57     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceConfigRemoveHelper.class);
58
59     public static List<ListenableFuture<Void>> removeConfiguration(DataBroker dataBroker, AlivenessMonitorService alivenessMonitorService,
60                                                                    Interface interfaceOld,
61                                                                    IdManagerService idManager,
62                                                                    IMdsalApiManager mdsalApiManager,
63                                                                    ParentRefs parentRefs) {
64         List<ListenableFuture<Void>> futures = new ArrayList<>();
65         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
66
67         IfTunnel ifTunnel = interfaceOld.getAugmentation(IfTunnel.class);
68         if (ifTunnel != null) {
69             removeTunnelConfiguration(alivenessMonitorService, parentRefs, dataBroker, interfaceOld,
70                     idManager, mdsalApiManager, futures);
71         }else {
72             removeVlanConfiguration(dataBroker, parentRefs, interfaceOld, t);
73             futures.add(t.submit());
74         }
75         return futures;
76     }
77
78     private static void removeVlanConfiguration(DataBroker dataBroker, ParentRefs parentRefs, Interface interfaceOld, WriteTransaction transaction) {
79         IfL2vlan ifL2vlan = interfaceOld.getAugmentation(IfL2vlan.class);
80         if (ifL2vlan == null || ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.Trunk) {
81             return;
82         }
83         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
84                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceOld.getName(), dataBroker);
85         if (ifState == null) {
86             return;
87         }
88
89         InterfaceManagerCommonUtils.updateOperStatus(interfaceOld.getName(), OperStatus.Down, transaction);
90         NodeConnectorId ncId = new NodeConnectorId(ifState.getLowerLayerIf().get(0));
91         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(ncId));
92         FlowBasedServicesUtils.removeIngressFlow(interfaceOld.getName(), dpId, transaction);
93         // For Vlan-Trunk Interface, remove the trunk-member operstates as well...
94
95         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(interfaceOld.getName());
96         InterfaceParentEntry interfaceParentEntry =
97                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
98         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
99             return;
100         }
101
102         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
103         for (InterfaceChildEntry interfaceChildEntry : interfaceParentEntry.getInterfaceChildEntry()) {
104             InterfaceManagerCommonUtils.deleteStateEntry(interfaceChildEntry.getChildInterface(), transaction);
105             FlowBasedServicesUtils.removeIngressFlow(interfaceChildEntry.getChildInterface(), dpId, transaction);
106         }
107     }
108
109     private static void removeTunnelConfiguration(AlivenessMonitorService alivenessMonitorService, ParentRefs parentRefs,
110                                                   DataBroker dataBroker, Interface interfaceOld,
111                                                   IdManagerService idManager, IMdsalApiManager mdsalApiManager,
112                                                   List<ListenableFuture<Void>> futures) {
113
114         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
115         BigInteger dpId = null;
116         if (parentRefs != null) {
117             dpId = parentRefs.getDatapathNodeIdentifier();
118         }
119
120         if (dpId == null) {
121             return;
122         }
123
124         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpId);
125         InstanceIdentifier<BridgeRefEntry> bridgeRefEntryIid =
126                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
127         BridgeRefEntry bridgeRefEntry =
128                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(bridgeRefEntryIid, dataBroker);
129
130         if (bridgeRefEntry != null) {
131             InstanceIdentifier<?> bridgeIid = bridgeRefEntry.getBridgeReference().getValue();
132             InstanceIdentifier<TerminationPoint> tpIid = SouthboundUtils.createTerminationPointInstanceIdentifier(
133                     InstanceIdentifier.keyOf(bridgeIid.firstIdentifierOf(Node.class)), interfaceOld.getName());
134             t.delete(LogicalDatastoreType.CONFIGURATION, tpIid);
135
136             // delete tunnel ingress flow
137             NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(interfaceOld, dataBroker);
138             long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(ncId));
139             InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager,
140                     interfaceOld.getAugmentation(IfTunnel.class),
141                     dpId, portNo, interfaceOld, -1,
142                     NwConstants.DEL_FLOW);
143         }
144
145         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
146         InstanceIdentifier<BridgeEntry> bridgeEntryIid = InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
147         BridgeEntry bridgeEntry = InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryIid, dataBroker);
148         if (bridgeEntry == null) {
149             return;
150         }
151
152         List<BridgeInterfaceEntry> bridgeInterfaceEntries = bridgeEntry.getBridgeInterfaceEntry();
153         if (bridgeInterfaceEntries == null) {
154             return;
155         }
156
157         if (bridgeInterfaceEntries.size() <= 1) {
158             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeEntryIid);
159         } else {
160             BridgeInterfaceEntryKey bridgeInterfaceEntryKey =
161                     new BridgeInterfaceEntryKey(interfaceOld.getName());
162             InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryIid =
163                     InterfaceMetaUtils.getBridgeInterfaceEntryIdentifier(bridgeEntryKey,
164                             bridgeInterfaceEntryKey);
165             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryIid);
166         }
167         futures.add(t.submit());
168         // stop LLDP monitoring for the tunnel interface
169         AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceOld);
170     }
171 }