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