64e7957139d3001f3e1a5932f3408623b3bf5ec0
[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         LOG.debug("removing vlan configuration for {}",interfaceOld.getName());
80         IfL2vlan ifL2vlan = interfaceOld.getAugmentation(IfL2vlan.class);
81         if (ifL2vlan == null || ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.Trunk) {
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.updateOperStatus(interfaceOld.getName(), OperStatus.Down, transaction);
92         NodeConnectorId ncId = new NodeConnectorId(ifState.getLowerLayerIf().get(0));
93         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(ncId));
94         FlowBasedServicesUtils.removeIngressFlow(interfaceOld.getName(), dpId, transaction);
95         // For Vlan-Trunk Interface, remove the trunk-member operstates as well...
96
97         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(interfaceOld.getName());
98         InterfaceParentEntry interfaceParentEntry =
99                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
100         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
101             return;
102         }
103
104         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
105         for (InterfaceChildEntry interfaceChildEntry : interfaceParentEntry.getInterfaceChildEntry()) {
106             LOG.debug("removing interface state for  vlan trunk member {}",interfaceChildEntry.getChildInterface());
107             InterfaceManagerCommonUtils.deleteStateEntry(interfaceChildEntry.getChildInterface(), transaction);
108             FlowBasedServicesUtils.removeIngressFlow(interfaceChildEntry.getChildInterface(), dpId, transaction);
109         }
110     }
111
112     private static void removeTunnelConfiguration(AlivenessMonitorService alivenessMonitorService, ParentRefs parentRefs,
113                                                   DataBroker dataBroker, Interface interfaceOld,
114                                                   IdManagerService idManager, IMdsalApiManager mdsalApiManager,
115                                                   List<ListenableFuture<Void>> futures) {
116         LOG.debug("removing tunnel configuration for {}",interfaceOld.getName());
117         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
118         BigInteger dpId = null;
119         if (parentRefs != null) {
120             dpId = parentRefs.getDatapathNodeIdentifier();
121         }
122
123         if (dpId == null) {
124             return;
125         }
126
127         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpId);
128         InstanceIdentifier<BridgeRefEntry> bridgeRefEntryIid =
129                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
130         BridgeRefEntry bridgeRefEntry =
131                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(bridgeRefEntryIid, dataBroker);
132
133         if (bridgeRefEntry != null) {
134             LOG.debug("removing termination point for {}",interfaceOld.getName());
135             InstanceIdentifier<?> bridgeIid = bridgeRefEntry.getBridgeReference().getValue();
136             InstanceIdentifier<TerminationPoint> tpIid = SouthboundUtils.createTerminationPointInstanceIdentifier(
137                     InstanceIdentifier.keyOf(bridgeIid.firstIdentifierOf(Node.class)), interfaceOld.getName());
138             t.delete(LogicalDatastoreType.CONFIGURATION, tpIid);
139
140             // delete tunnel ingress flow
141             LOG.debug("removing tunnel ingress flow for {}",interfaceOld.getName());
142             NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(interfaceOld, dataBroker);
143             long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(ncId));
144             InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager,
145                     interfaceOld.getAugmentation(IfTunnel.class),
146                     dpId, portNo, interfaceOld, -1,
147                     NwConstants.DEL_FLOW);
148         }
149
150         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
151         InstanceIdentifier<BridgeEntry> bridgeEntryIid = InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
152         BridgeEntry bridgeEntry = InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryIid, dataBroker);
153         if (bridgeEntry == null) {
154             return;
155         }
156
157         List<BridgeInterfaceEntry> bridgeInterfaceEntries = bridgeEntry.getBridgeInterfaceEntry();
158         if (bridgeInterfaceEntries == null) {
159             return;
160         }
161
162         if (bridgeInterfaceEntries.size() <= 1) {
163             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeEntryIid);
164         } else {
165             BridgeInterfaceEntryKey bridgeInterfaceEntryKey =
166                     new BridgeInterfaceEntryKey(interfaceOld.getName());
167             InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryIid =
168                     InterfaceMetaUtils.getBridgeInterfaceEntryIdentifier(bridgeEntryKey,
169                             bridgeInterfaceEntryKey);
170             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryIid);
171         }
172         futures.add(t.submit());
173         // stop LLDP monitoring for the tunnel interface
174         AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceOld);
175     }
176 }