453bc9f4de19e8aac4279258781832b4ed552294
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / renderer / ovs / confighelpers / OvsInterfaceConfigRemoveHelper.java
1 /*
2  * Copyright (c) 2016, 2017 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.confighelpers;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.ExecutionException;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.apache.aries.blueprint.annotation.service.Reference;
21 import org.opendaylight.genius.infra.Datastore.Configuration;
22 import org.opendaylight.genius.infra.Datastore.Operational;
23 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
24 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
25 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
26 import org.opendaylight.genius.infra.TypedWriteTransaction;
27 import org.opendaylight.genius.interfacemanager.IfmConstants;
28 import org.opendaylight.genius.interfacemanager.IfmUtil;
29 import org.opendaylight.genius.interfacemanager.commons.AlivenessMonitorUtils;
30 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
31 import org.opendaylight.genius.interfacemanager.commons.InterfaceMetaUtils;
32 import org.opendaylight.genius.interfacemanager.renderer.ovs.utilities.SouthboundUtils;
33 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
34 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
35 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
36 import org.opendaylight.mdsal.binding.api.DataBroker;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info._interface.parent.entry.InterfaceChildEntry;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge._interface.info.BridgeEntry;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge._interface.info.BridgeEntryKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeLogicalGroup;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.common.Uint64;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 @Singleton
56 public final class OvsInterfaceConfigRemoveHelper {
57     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceConfigRemoveHelper.class);
58     private static final Logger EVENT_LOGGER = LoggerFactory.getLogger("GeniusEventLogger");
59
60     private final DataBroker dataBroker;
61     private final ManagedNewTransactionRunner txRunner;
62     private final IMdsalApiManager mdsalApiManager;
63     private final JobCoordinator coordinator;
64     private final InterfaceManagerCommonUtils interfaceManagerCommonUtils;
65     private final AlivenessMonitorUtils alivenessMonitorUtils;
66     private final InterfaceMetaUtils interfaceMetaUtils;
67     private final SouthboundUtils southboundUtils;
68
69     @Inject
70     public OvsInterfaceConfigRemoveHelper(@Reference DataBroker dataBroker,
71                                           AlivenessMonitorUtils alivenessMonitorUtils,
72                                           @Reference IMdsalApiManager mdsalApiManager,
73                                           @Reference JobCoordinator coordinator,
74                                           InterfaceManagerCommonUtils interfaceManagerCommonUtils,
75                                           InterfaceMetaUtils interfaceMetaUtils,
76                                           SouthboundUtils southboundUtils) {
77         this.dataBroker = dataBroker;
78         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
79         this.mdsalApiManager = mdsalApiManager;
80         this.coordinator = coordinator;
81         this.interfaceManagerCommonUtils = interfaceManagerCommonUtils;
82         this.alivenessMonitorUtils = alivenessMonitorUtils;
83         this.interfaceMetaUtils = interfaceMetaUtils;
84         this.southboundUtils = southboundUtils;
85     }
86
87     public List<ListenableFuture<Void>> removeConfiguration(Interface interfaceOld, ParentRefs parentRefs) {
88         List<ListenableFuture<Void>> futures = new ArrayList<>();
89         futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, operTx -> {
90             IfTunnel ifTunnel = interfaceOld.augmentation(IfTunnel.class);
91             if (ifTunnel != null) {
92                 futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
93                     confTx -> removeTunnelConfiguration(parentRefs, interfaceOld.getName(), ifTunnel, operTx, confTx)));
94             } else {
95                 removeVlanConfiguration(parentRefs, interfaceOld.getName(),
96                         interfaceOld.augmentation(IfL2vlan.class), operTx, futures);
97             }
98         }));
99         return futures;
100     }
101
102     private void removeVlanConfiguration(ParentRefs parentRefs, String interfaceName,
103             IfL2vlan ifL2vlan, TypedWriteTransaction<Operational> tx,
104             List<ListenableFuture<Void>> futures) {
105         if (parentRefs == null || ifL2vlan == null || IfL2vlan.L2vlanMode.Trunk != ifL2vlan.getL2vlanMode()
106                 && IfL2vlan.L2vlanMode.Transparent != ifL2vlan.getL2vlanMode()) {
107             return;
108         }
109         LOG.info("removing vlan configuration for interface {}", interfaceName);
110         interfaceManagerCommonUtils.deleteInterfaceStateInformation(interfaceName, tx);
111
112         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
113             .ietf.interfaces.rev140508.interfaces.state.Interface ifState = interfaceManagerCommonUtils
114                 .getInterfaceState(interfaceName);
115
116         if (ifState == null) {
117             LOG.debug("could not fetch interface state corresponding to {}, probably already removed as part of port "
118                     + "removal event, proceeding with remaining config cleanups", interfaceName);
119         }
120
121         cleanUpInterfaceWithUnknownState(interfaceName, parentRefs, null, tx);
122         Uint64 dpId = IfmUtil.getDpnFromInterface(ifState);
123         EVENT_LOGGER.debug("IFM-OvsInterfaceConfig,REMOVE {}", interfaceName);
124         FlowBasedServicesUtils.removeIngressFlow(interfaceName, dpId, txRunner, futures);
125
126         interfaceManagerCommonUtils.deleteParentInterfaceEntry(parentRefs.getParentInterface());
127
128         // For Vlan-Trunk Interface, remove the trunk-member operstates as
129         // well...
130
131         InterfaceParentEntry interfaceParentEntry = interfaceMetaUtils
132                 .getInterfaceParentEntryFromConfigDS(interfaceName);
133         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
134             return;
135         }
136
137         VlanMemberStateRemoveWorker vlanMemberStateRemoveWorker = new VlanMemberStateRemoveWorker(txRunner,
138                 interfaceManagerCommonUtils, dpId, interfaceName, interfaceParentEntry);
139         coordinator.enqueueJob(interfaceName, vlanMemberStateRemoveWorker, IfmConstants.JOB_MAX_RETRIES);
140
141     }
142
143     private void removeTunnelConfiguration(ParentRefs parentRefs, String interfaceName, IfTunnel ifTunnel,
144             TypedWriteTransaction<Operational> operTx, TypedReadWriteTransaction<Configuration> confTx)
145             throws ExecutionException, InterruptedException {
146         LOG.info("removing tunnel configuration for interface {}", interfaceName);
147         Uint64 dpId = null;
148         if (parentRefs != null) {
149             dpId = parentRefs.getDatapathNodeIdentifier();
150         }
151
152         if (dpId == null) {
153             return;
154         }
155
156         OvsdbBridgeRef ovsdbBridgeRef = interfaceMetaUtils.getOvsdbBridgeRef(dpId);
157         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
158         InstanceIdentifier<BridgeEntry> bridgeEntryIid = InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
159         BridgeEntry bridgeEntry = interfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryIid);
160         if (bridgeEntry == null) {
161             LOG.debug("Bridge Entry not present for dpn: {}", dpId);
162             return;
163         }
164         List<BridgeInterfaceEntry> bridgeInterfaceEntries = bridgeEntry.getBridgeInterfaceEntry();
165         if (bridgeInterfaceEntries == null) {
166             LOG.debug("Bridge Interface Entries not present for dpn : {}", dpId);
167             return;
168         }
169         String tunnelName = SouthboundUtils.isOfTunnel(ifTunnel) ? SouthboundUtils.generateOfTunnelName(dpId, ifTunnel)
170                 : interfaceName;
171         boolean deleteTunnel = canDeleteTunnelPort(bridgeInterfaceEntries, ifTunnel);
172         if (ovsdbBridgeRef != null && deleteTunnel) {
173             southboundUtils.removeTerminationEndPoint(ovsdbBridgeRef.getValue(), tunnelName);
174         }
175         if (SouthboundUtils.isOfTunnel(ifTunnel)) {
176             if (deleteTunnel) {
177                 interfaceManagerCommonUtils.deleteParentInterfaceEntry(tunnelName);
178             } else {
179                 interfaceManagerCommonUtils.deleteInterfaceChildEntry(tunnelName, interfaceName);
180             }
181         }
182
183         // delete tunnel ingress flow
184         removeTunnelIngressFlow(confTx, interfaceName, ifTunnel, dpId);
185
186         // delete bridge to tunnel interface mappings
187         interfaceMetaUtils.deleteBridgeInterfaceEntry(bridgeEntryKey, bridgeInterfaceEntries, bridgeEntryIid,
188                 interfaceName);
189         int lportTag = interfaceMetaUtils.removeLportTagInterfaceMap(operTx, interfaceName);
190         cleanUpInterfaceWithUnknownState(interfaceName, parentRefs, ifTunnel, operTx);
191         // stop LLDP monitoring for the tunnel interface
192         alivenessMonitorUtils.stopLLDPMonitoring(ifTunnel, interfaceName);
193
194         if (ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class)) {
195             removeMultipleVxlanTunnelsConfiguration(interfaceName, parentRefs);
196
197         } else if (ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeLogicalGroup.class)) {
198             removeLogicalTunnelGroup(dpId, interfaceName, lportTag, operTx, confTx);
199         }
200     }
201
202     private static boolean canDeleteTunnelPort(List<BridgeInterfaceEntry> bridgeInterfaceEntries, IfTunnel ifTunnel) {
203         return !SouthboundUtils.isOfTunnel(ifTunnel) || bridgeInterfaceEntries == null
204                 || bridgeInterfaceEntries.size() <= 1;
205     }
206
207     public void removeTunnelIngressFlow(TypedReadWriteTransaction<Configuration> confTx,
208         String interfaceName, IfTunnel ifTunnel, Uint64 dpId) throws ExecutionException, InterruptedException {
209         NodeConnectorId ncId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(interfaceName,
210                 interfaceManagerCommonUtils);
211         if (ncId == null) {
212             LOG.debug("Node Connector Id is null. Skipping remove tunnel ingress flow.");
213         } else {
214             interfaceManagerCommonUtils.removeTunnelIngressFlow(confTx, ifTunnel, dpId, interfaceName);
215
216             IfmUtil.unbindService(txRunner, coordinator, interfaceName,
217                     FlowBasedServicesUtils.buildDefaultServiceId(interfaceName));
218         }
219     }
220
221     // if the node is shutdown, there will be stale interface state entries,
222     // with unknown op-state, clear them.
223     public org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
224         .ietf.interfaces.rev140508.interfaces.state.Interface cleanUpInterfaceWithUnknownState(
225             String interfaceName, ParentRefs parentRefs, IfTunnel ifTunnel,
226             TypedWriteTransaction<Operational> transaction) {
227         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
228             .interfaces.state.Interface ifState = interfaceManagerCommonUtils.getInterfaceState(interfaceName);
229         if (ifState != null && ifState
230                 .getOperStatus() == org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
231                 .ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Unknown) {
232             String staleInterface = ifTunnel != null ? interfaceName : parentRefs.getParentInterface();
233             LOG.debug("cleaning up parent-interface for {}, since the oper-status is UNKNOWN", interfaceName);
234             interfaceManagerCommonUtils.deleteInterfaceStateInformation(staleInterface, transaction);
235         }
236         return ifState;
237     }
238
239     private static class VlanMemberStateRemoveWorker implements Callable<List<? extends ListenableFuture<?>>> {
240         private final ManagedNewTransactionRunner txRunner;
241         private final InterfaceManagerCommonUtils interfaceManagerCommonUtils;
242         private final Uint64 dpId;
243         private final String interfaceName;
244         private final InterfaceParentEntry interfaceParentEntry;
245
246         VlanMemberStateRemoveWorker(ManagedNewTransactionRunner txRunner,
247                 InterfaceManagerCommonUtils interfaceManagerCommonUtils,
248                 Uint64 dpId, String interfaceName, InterfaceParentEntry interfaceParentEntry) {
249             this.txRunner = txRunner;
250             this.interfaceManagerCommonUtils = interfaceManagerCommonUtils;
251             this.dpId = dpId;
252             this.interfaceName = interfaceName;
253             this.interfaceParentEntry = interfaceParentEntry;
254         }
255
256         @Override
257         public List<ListenableFuture<Void>> call() {
258             List<ListenableFuture<Void>> futures = new ArrayList<>();
259             futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, tx -> {
260                 // FIXME: If the no. of child entries exceeds 100, perform txn
261                 // updates in batches of 100.
262                 for (InterfaceChildEntry interfaceChildEntry : interfaceParentEntry.nonnullInterfaceChildEntry()) {
263                     LOG.debug("removing interface state for vlan trunk member {}",
264                             interfaceChildEntry.getChildInterface());
265                     interfaceManagerCommonUtils.deleteInterfaceStateInformation(interfaceChildEntry.getChildInterface(),
266                             tx);
267                     FlowBasedServicesUtils.removeIngressFlow(interfaceChildEntry.getChildInterface(), dpId, txRunner,
268                             futures);
269                 }
270             }));
271             return futures;
272         }
273
274         @Override
275         public String toString() {
276             return "VlanMemberStateRemoveWorker [dpId=" + dpId + ", interfaceName=" + interfaceName
277                     + ", interfaceParentEntry=" + interfaceParentEntry + "]";
278         }
279     }
280
281     private void removeLogicalTunnelSelectGroup(TypedReadWriteTransaction<Configuration> tx,
282             Uint64 srcDpnId, String interfaceName, int lportTag) throws ExecutionException, InterruptedException {
283         long groupId = IfmUtil.getLogicalTunnelSelectGroupId(lportTag);
284         LOG.debug("MULTIPLE_VxLAN_TUNNELS: group id {} removed for {} srcDpnId {}",
285                 groupId, interfaceName, srcDpnId);
286         mdsalApiManager.removeGroup(tx, srcDpnId, groupId);
287     }
288
289     private void removeLogicalTunnelGroup(Uint64 dpnId, String ifaceName, int lportTag,
290             TypedWriteTransaction<Operational> operTx, TypedReadWriteTransaction<Configuration> confTx)
291             throws ExecutionException, InterruptedException {
292         LOG.debug("MULTIPLE_VxLAN_TUNNELS: unbind & delete Interface State for logic tunnel group {}", ifaceName);
293         IfmUtil.unbindService(txRunner, coordinator, ifaceName,
294                 FlowBasedServicesUtils.buildDefaultServiceId(ifaceName));
295         interfaceManagerCommonUtils.deleteInterfaceStateInformation(ifaceName, operTx);
296         interfaceManagerCommonUtils.deleteParentInterfaceEntry(ifaceName);
297         removeLogicalTunnelSelectGroup(confTx, dpnId, ifaceName, lportTag);
298     }
299
300     private void removeMultipleVxlanTunnelsConfiguration(String ifaceName, ParentRefs parentRef) {
301         //Remove the individual tunnel from interface-child-info model of the tunnel group members
302         String parentInterface = parentRef.getParentInterface();
303         if (parentInterface == null) {
304             return;
305         }
306         LOG.debug("MULTIPLE_VxLAN_TUNNELS: removeMultipleVxlanTunnelsConfiguration for {} in logical group {}",
307                     ifaceName, parentInterface);
308         interfaceManagerCommonUtils.deleteInterfaceChildEntry(parentInterface, ifaceName);
309     }
310 }