Bug 5199 : DHCP and Transparent code changes
[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
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.idmanager.IdManager;
16 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
17 import org.opendaylight.vpnservice.interfacemgr.commons.AlivenessMonitorUtils;
18 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
19 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
20 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.utilities.SouthboundUtils;
21 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
22 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
23 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorStopInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorStopInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import java.math.BigInteger;
54 import java.util.ArrayList;
55 import java.util.List;
56
57 public class OvsInterfaceConfigRemoveHelper {
58     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceConfigRemoveHelper.class);
59
60     public static List<ListenableFuture<Void>> removeConfiguration(DataBroker dataBroker, AlivenessMonitorService alivenessMonitorService,
61                                                                    Interface interfaceOld,
62                                                                    IdManagerService idManager,
63                                                                    IMdsalApiManager mdsalApiManager,
64                                                                    ParentRefs parentRefs) {
65         List<ListenableFuture<Void>> futures = new ArrayList<>();
66         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
67
68         IfTunnel ifTunnel = interfaceOld.getAugmentation(IfTunnel.class);
69         if (ifTunnel != null) {
70             removeTunnelConfiguration(alivenessMonitorService, parentRefs, dataBroker, interfaceOld,
71                     idManager, mdsalApiManager, futures);
72         }else {
73             removeVlanConfiguration(dataBroker, interfaceOld, t);
74             futures.add(t.submit());
75         }
76         return futures;
77     }
78
79     private static void removeVlanConfiguration(DataBroker dataBroker, Interface interfaceOld, WriteTransaction t) {
80         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
81                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceOld.getName(), dataBroker);
82         if (ifState == null) {
83             return;
84         }
85
86         String ncStr = ifState.getLowerLayerIf().get(0);
87         NodeConnectorId nodeConnectorId = new NodeConnectorId(ncStr);
88         NodeConnector nodeConnector =
89                 InterfaceManagerCommonUtils.getNodeConnectorFromInventoryOperDS(nodeConnectorId, dataBroker);
90         if(nodeConnector != null) {
91             FlowCapableNodeConnector flowCapableNodeConnector =
92                     nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
93             //State state = flowCapableNodeConnector.getState();
94             OperStatus operStatus = flowCapableNodeConnector == null ? OperStatus.Down : OperStatus.Up;
95
96             if (ifState.getOperStatus() != operStatus) {
97                 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
98                         IfmUtil.buildStateInterfaceId(interfaceOld.getName());
99                 InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
100                 ifaceBuilder.setOperStatus(operStatus);
101                 ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceOld.getName()));
102                 t.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
103             }
104         }
105         IfL2vlan ifL2vlan = interfaceOld.getAugmentation(IfL2vlan.class);
106         if (ifL2vlan == null) {
107             return;
108         }
109         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
110         FlowBasedServicesUtils.removeIngressFlow(interfaceOld, dpId, t);
111         if (ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.Trunk) {
112             return;
113         }
114
115         // For Vlan-Trunk Interface, remove the trunk-member operstates as well...
116         InterfaceKey interfaceKey = new InterfaceKey(interfaceOld.getName());
117         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
118                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
119         if (iface == null) {
120             return;
121         }
122
123         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(iface.getName());
124         InterfaceParentEntry interfaceParentEntry =
125                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
126         if (interfaceParentEntry == null) {
127             return;
128         }
129
130         List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.getInterfaceChildEntry();
131         if (interfaceChildEntries == null) {
132             return;
133         }
134
135         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
136         for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
137             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
138                     IfmUtil.buildStateInterfaceId(interfaceChildEntry.getChildInterface());
139             t.delete(LogicalDatastoreType.OPERATIONAL, ifChildStateId);
140             InterfaceKey childIfKey = new InterfaceKey(interfaceChildEntry.getChildInterface());
141             FlowBasedServicesUtils.removeIngressFlow(InterfaceManagerCommonUtils.getInterfaceFromConfigDS(childIfKey, dataBroker), dpId, t);
142         }
143     }
144
145     private static void removeTunnelConfiguration(AlivenessMonitorService alivenessMonitorService, ParentRefs parentRefs,
146                                                   DataBroker dataBroker, Interface interfaceOld,
147                                                   IdManagerService idManager, IMdsalApiManager mdsalApiManager,
148                                                   List<ListenableFuture<Void>> futures) {
149
150         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
151         BigInteger dpId = null;
152         if (parentRefs != null) {
153             dpId = parentRefs.getDatapathNodeIdentifier();
154         }
155
156         if (dpId == null) {
157             return;
158         }
159
160         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpId);
161         InstanceIdentifier<BridgeRefEntry> bridgeRefEntryIid =
162                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
163         BridgeRefEntry bridgeRefEntry =
164                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(bridgeRefEntryIid, dataBroker);
165
166         if (bridgeRefEntry != null) {
167             InstanceIdentifier<?> bridgeIid = bridgeRefEntry.getBridgeReference().getValue();
168             InstanceIdentifier<TerminationPoint> tpIid = SouthboundUtils.createTerminationPointInstanceIdentifier(
169                     InstanceIdentifier.keyOf(bridgeIid.firstIdentifierOf(Node.class)), interfaceOld.getName());
170             t.delete(LogicalDatastoreType.CONFIGURATION, tpIid);
171
172             // delete tunnel ingress flow
173             NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(interfaceOld, dataBroker);
174             long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(ncId));
175             InterfaceManagerCommonUtils.makeTunnelIngressFlow(futures, mdsalApiManager,
176                     interfaceOld.getAugmentation(IfTunnel.class),
177                     dpId, portNo, interfaceOld, -1,
178                     NwConstants.DEL_FLOW);
179         }
180
181         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpId);
182         InstanceIdentifier<BridgeEntry> bridgeEntryIid = InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
183         BridgeEntry bridgeEntry = InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryIid, dataBroker);
184         if (bridgeEntry == null) {
185             return;
186         }
187
188         List<BridgeInterfaceEntry> bridgeInterfaceEntries = bridgeEntry.getBridgeInterfaceEntry();
189         if (bridgeInterfaceEntries == null) {
190             return;
191         }
192
193         if (bridgeInterfaceEntries.size() <= 1) {
194             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeEntryIid);
195         } else {
196             BridgeInterfaceEntryKey bridgeInterfaceEntryKey =
197                     new BridgeInterfaceEntryKey(interfaceOld.getName());
198             InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryIid =
199                     InterfaceMetaUtils.getBridgeInterfaceEntryIdentifier(bridgeEntryKey,
200                             bridgeInterfaceEntryKey);
201             t.delete(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryIid);
202         }
203         futures.add(t.submit());
204         // stop LLDP monitoring for the tunnel interface
205         AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceOld);
206     }
207 }