Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / confighelpers / OvsInterfaceConfigRemoveHelper.java
1 /*
2  * Copyright (c) 2015 - 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.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         LOG.debug("removing vlan configuration for {}",interfaceOld.getName());
80         IfL2vlan ifL2vlan = interfaceOld.getAugmentation(IfL2vlan.class);
81         if (ifL2vlan == null || (IfL2vlan.L2vlanMode.Trunk != ifL2vlan.getL2vlanMode() && IfL2vlan.L2vlanMode.Transparent != ifL2vlan.getL2vlanMode())) {
82             return;
83         }
84         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
85                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceOld.getName(), dataBroker);
86         if (ifState == null) {
87             LOG.debug("could not fetch interface state corresponding to {}",interfaceOld.getName());
88             return;
89         }
90
91         InterfaceManagerCommonUtils.deleteStateEntry(interfaceOld.getName(), transaction);
92
93         BigInteger dpId = IfmUtil.getDpnFromInterface(ifState);
94         FlowBasedServicesUtils.removeIngressFlow(interfaceOld.getName(), dpId, transaction);
95         InterfaceManagerCommonUtils.deleteParentInterfaceEntry(transaction, parentRefs.getParentInterface());
96         // For Vlan-Trunk Interface, remove the trunk-member operstates as well...
97
98         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(interfaceOld.getName());
99         InterfaceParentEntry interfaceParentEntry =
100                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
101         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
102             return;
103         }
104
105         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
106         for (InterfaceChildEntry interfaceChildEntry : interfaceParentEntry.getInterfaceChildEntry()) {
107             LOG.debug("removing interface state for  vlan trunk member {}",interfaceChildEntry.getChildInterface());
108             InterfaceManagerCommonUtils.deleteStateEntry(interfaceChildEntry.getChildInterface(), transaction);
109             FlowBasedServicesUtils.removeIngressFlow(interfaceChildEntry.getChildInterface(), dpId, transaction);
110         }
111     }
112
113     private static void removeTunnelConfiguration(AlivenessMonitorService alivenessMonitorService, ParentRefs parentRefs,
114                                                   DataBroker dataBroker, Interface interfaceOld,
115                                                   IdManagerService idManager, IMdsalApiManager mdsalApiManager,
116                                                   List<ListenableFuture<Void>> futures) {
117         LOG.debug("removing tunnel configuration for {}",interfaceOld.getName());
118         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
119         BigInteger dpId = null;
120         if (parentRefs != null) {
121             dpId = parentRefs.getDatapathNodeIdentifier();
122         }
123
124         if (dpId == null) {
125             return;
126         }
127
128         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpId);
129         InstanceIdentifier<BridgeRefEntry> bridgeRefEntryIid =
130                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
131         BridgeRefEntry bridgeRefEntry =
132                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(bridgeRefEntryIid, dataBroker);
133
134         if (bridgeRefEntry != null) {
135             LOG.debug("removing termination point for {}",interfaceOld.getName());
136             InstanceIdentifier<?> bridgeIid = bridgeRefEntry.getBridgeReference().getValue();
137             InstanceIdentifier<TerminationPoint> tpIid = SouthboundUtils.createTerminationPointInstanceIdentifier(
138                     InstanceIdentifier.keyOf(bridgeIid.firstIdentifierOf(Node.class)), interfaceOld.getName());
139             t.delete(LogicalDatastoreType.CONFIGURATION, tpIid);
140
141             // delete tunnel ingress flow
142             LOG.debug("removing tunnel ingress flow for {}",interfaceOld.getName());
143             NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(interfaceOld, dataBroker);
144             long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(ncId));
145             InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager,
146                     interfaceOld.getAugmentation(IfTunnel.class),
147                     dpId, portNo, interfaceOld, -1,
148                     NwConstants.DEL_FLOW);
149         }
150
151         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
152         InstanceIdentifier<BridgeEntry> bridgeEntryIid = InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
153         BridgeEntry bridgeEntry = InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryIid, dataBroker);
154         if (bridgeEntry == null) {
155             return;
156         }
157
158         List<BridgeInterfaceEntry> bridgeInterfaceEntries = bridgeEntry.getBridgeInterfaceEntry();
159         if (bridgeInterfaceEntries == null) {
160             return;
161         }
162
163         if (bridgeInterfaceEntries.size() <= 1) {
164             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeEntryIid);
165         } else {
166             BridgeInterfaceEntryKey bridgeInterfaceEntryKey =
167                     new BridgeInterfaceEntryKey(interfaceOld.getName());
168             InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryIid =
169                     InterfaceMetaUtils.getBridgeInterfaceEntryIdentifier(bridgeEntryKey,
170                             bridgeInterfaceEntryKey);
171             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryIid);
172         }
173         futures.add(t.submit());
174         // stop LLDP monitoring for the tunnel interface
175         AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceOld);
176     }
177 }