Fixing issues with idmanager
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / statehelpers / OvsInterfaceStateRemoveHelper.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.statehelpers;
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.vpnservice.interfacemgr.IfmUtil;
15 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
16 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 public class OvsInterfaceStateRemoveHelper {
32     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateRemoveHelper.class);
33
34     public static List<ListenableFuture<Void>> removeState(IdManagerService idManager,
35                                                            InstanceIdentifier<FlowCapableNodeConnector> key,
36                                                            DataBroker dataBroker, String portName, FlowCapableNodeConnector fcNodeConnectorOld) {
37         LOG.debug("Removing interface-state for port: {}", portName);
38         List<ListenableFuture<Void>> futures = new ArrayList<>();
39         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
40
41         InstanceIdentifier<Interface> ifStateId = IfmUtil.buildStateInterfaceId(portName);
42         /* Remove entry from if-index-interface-name map and deallocate Id from Idmanager. */
43         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =
44                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(portName, dataBroker);
45         InterfaceMetaUtils.removeLportTagInterfaceMap(t, idManager, dataBroker, interfaceState.getName(), interfaceState.getIfIndex());
46
47         t.delete(LogicalDatastoreType.OPERATIONAL, ifStateId);
48
49         // For Vlan-Trunk Interface, remove the trunk-member operstates as well...
50         InterfaceKey interfaceKey = new InterfaceKey(portName);
51         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
52                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
53         if (iface == null) {
54             futures.add(t.submit());
55             return futures;
56         }
57
58         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(iface.getName());
59         InterfaceParentEntry interfaceParentEntry =
60                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
61         if (interfaceParentEntry == null) {
62             futures.add(t.submit());
63             return futures;
64         }
65
66         List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.getInterfaceChildEntry();
67         if (interfaceChildEntries == null) {
68             futures.add(t.submit());
69             return futures;
70         }
71
72         //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
73         for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
74             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
75                     IfmUtil.buildStateInterfaceId(interfaceChildEntry.getChildInterface());
76             t.delete(LogicalDatastoreType.OPERATIONAL, ifChildStateId);
77         }
78
79        /* Below code will be needed if we want to update the vlan-trunk in the of-port.
80        NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(key.firstIdentifierOf(NodeConnector.class)).getId();
81         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
82
83         BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
84         InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
85                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
86         BridgeRefEntry bridgeRefEntry =
87                 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
88         if (bridgeRefEntry == null) {
89             futures.add(t.submit());
90             return futures;
91         }
92
93         InstanceIdentifier<?> bridgeIid = bridgeRefEntry.getBridgeReference().getValue();
94         InstanceIdentifier<TerminationPoint> tpIid = SouthboundUtils.createTerminationPointInstanceIdentifier(
95                 InstanceIdentifier.keyOf(bridgeIid.firstIdentifierOf(Node.class)), interfaceOld.getName());
96         t.delete(LogicalDatastoreType.CONFIGURATION, tpIid); */
97
98         futures.add(t.submit());
99         return futures;
100     }
101 }