Fixes in interface manager and aliveness monitor
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / commons / InterfaceManagerCommonUtils.java
1 /*
2  * Copyright (c) 2015 - 2016 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
9 package org.opendaylight.vpnservice.interfacemgr.commons;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
19 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
20 import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo;
21 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
22 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
23 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
24 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
25 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
26 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
27 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
28 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
29 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
30 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntryBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntryKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnelBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeMplsOverGre;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 import com.google.common.base.Optional;
60 import com.google.common.util.concurrent.ListenableFuture;
61
62 public class InterfaceManagerCommonUtils {
63     private static final Logger LOG = LoggerFactory.getLogger(InterfaceManagerCommonUtils.class);
64     public static NodeConnector getNodeConnectorFromInventoryOperDS(NodeConnectorId nodeConnectorId,
65                                                                     DataBroker dataBroker) {
66         NodeId nodeId = IfmUtil.getNodeIdFromNodeConnectorId(nodeConnectorId);
67         InstanceIdentifier<NodeConnector> ncIdentifier = InstanceIdentifier.builder(Nodes.class)
68                 .child(Node.class, new NodeKey(nodeId))
69                 .child(NodeConnector.class, new NodeConnectorKey(nodeConnectorId)).build();
70
71         Optional<NodeConnector> nodeConnectorOptional = IfmUtil.read(LogicalDatastoreType.OPERATIONAL,
72                 ncIdentifier, dataBroker);
73         if (!nodeConnectorOptional.isPresent()) {
74             return null;
75         }
76         return nodeConnectorOptional.get();
77     }
78
79     public static InstanceIdentifier<Interface> getInterfaceIdentifier(InterfaceKey interfaceKey) {
80         InstanceIdentifier.InstanceIdentifierBuilder<Interface> interfaceInstanceIdentifierBuilder =
81                 InstanceIdentifier.builder(Interfaces.class).child(Interface.class, interfaceKey);
82         return interfaceInstanceIdentifierBuilder.build();
83     }
84
85     public static List<Interface> getAllTunnelInterfaces(DataBroker dataBroker, InterfaceInfo.InterfaceType interfaceType) {
86         List<Interface> vxlanList = new ArrayList<Interface>();
87         InstanceIdentifier<Interfaces> interfacesInstanceIdentifier =  InstanceIdentifier.builder(Interfaces.class).build();
88         Optional<Interfaces> interfacesOptional  = IfmUtil.read(LogicalDatastoreType.CONFIGURATION, interfacesInstanceIdentifier, dataBroker);
89         if (!interfacesOptional.isPresent()) {
90             return vxlanList;
91         }
92         Interfaces interfaces = interfacesOptional.get();
93         List<Interface> interfacesList = interfaces.getInterface();
94         for (Interface iface : interfacesList) {
95             if(IfmUtil.getInterfaceType(iface) == InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE &&
96                     iface.getAugmentation(IfTunnel.class).isInternal()) {
97                 vxlanList.add(iface);
98             }
99         }
100         return vxlanList;
101     }
102
103     public static Interface getInterfaceFromConfigDS(String interfaceName, DataBroker dataBroker) {
104         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
105         return getInterfaceFromConfigDS(interfaceKey, dataBroker);
106     }
107
108     public static Interface getInterfaceFromConfigDS(InterfaceKey interfaceKey, DataBroker dataBroker) {
109         InstanceIdentifier<Interface> interfaceId = getInterfaceIdentifier(interfaceKey);
110         Optional<Interface> interfaceOptional = IfmUtil.read(LogicalDatastoreType.CONFIGURATION, interfaceId, dataBroker);
111         if (!interfaceOptional.isPresent()) {
112             return null;
113         }
114
115         return interfaceOptional.get();
116     }
117
118     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS(String interfaceName, DataBroker dataBroker) {
119         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
120                 IfmUtil.buildStateInterfaceId(interfaceName);
121         return getInterfaceStateFromOperDS(ifStateId, dataBroker);
122     }
123
124     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS
125             (InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId, DataBroker dataBroker) {
126         Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateOptional =
127                 IfmUtil.read(LogicalDatastoreType.OPERATIONAL, ifStateId, dataBroker);
128         if (!ifStateOptional.isPresent()) {
129             return null;
130         }
131
132         return ifStateOptional.get();
133     }
134     public static void makeTunnelIngressFlow(List<ListenableFuture<Void>> futures, IMdsalApiManager mdsalApiManager,
135                                              IfTunnel tunnel, BigInteger dpnId, long portNo, Interface iface, int ifIndex, int addOrRemoveFlow) {
136         LOG.debug("make tunnel ingress flow for {}",iface.getName());
137         String flowRef = InterfaceManagerCommonUtils.getTunnelInterfaceFlowRef(dpnId, NwConstants.VLAN_INTERFACE_INGRESS_TABLE, iface.getName());
138         List<MatchInfo> matches = new ArrayList<MatchInfo>();
139         List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
140         if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
141             matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {
142                     dpnId, BigInteger.valueOf(portNo) }));
143             mkInstructions.add(new InstructionInfo(
144                     InstructionType.write_metadata, new BigInteger[] {
145                             MetaDataUtil.getLportTagMetaData(ifIndex).or(BigInteger.ONE),
146                             MetaDataUtil.METADATA_MASK_LPORT_TAG_SH_FLAG}));
147             short tableId = tunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeMplsOverGre.class) ? NwConstants.L3_LFIB_TABLE :
148                     tunnel.isInternal() ? NwConstants.INTERNAL_TUNNEL_TABLE : NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL;
149             mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] {tableId}));
150         }
151
152         BigInteger COOKIE_VM_INGRESS_TABLE = new BigInteger("8000001", 16);
153         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.VLAN_INTERFACE_INGRESS_TABLE, flowRef,
154                 IfmConstants.DEFAULT_FLOW_PRIORITY, iface.getName(), 0, 0, COOKIE_VM_INGRESS_TABLE, matches, mkInstructions);
155         if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
156             futures.add(mdsalApiManager.installFlow(dpnId, flowEntity));
157         } else {
158             futures.add(mdsalApiManager.removeFlow(dpnId, flowEntity));
159         }
160     }
161     public static String getTunnelInterfaceFlowRef(BigInteger dpnId, short tableId, String ifName) {
162         return new StringBuilder().append(dpnId).append(tableId).append(ifName).toString();
163     }
164
165     public static void setOpStateForInterface(DataBroker broker, String interfaceName, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus opStatus) {
166         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> interfaceId = IfmUtil.buildStateInterfaceId(interfaceName);
167         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setKey(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey(interfaceName));
168         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceData = ifaceBuilder.setOperStatus(opStatus).build();
169         MDSALUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, interfaceId, interfaceData);
170     }
171
172     public static void createInterfaceChildEntry( WriteTransaction t,
173                                                   String parentInterface, String childInterface){
174         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
175         InterfaceChildEntryKey interfaceChildEntryKey = new InterfaceChildEntryKey(childInterface);
176         InstanceIdentifier<InterfaceChildEntry> intfId =
177                 InterfaceMetaUtils.getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
178         InterfaceChildEntryBuilder entryBuilder = new InterfaceChildEntryBuilder().setKey(interfaceChildEntryKey)
179                 .setChildInterface(childInterface);
180         t.put(LogicalDatastoreType.CONFIGURATION, intfId, entryBuilder.build(),true);
181     }
182
183     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus
184            updateStateEntry(Interface interfaceNew, DataBroker dataBroker, WriteTransaction transaction,
185                           org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
186         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus;
187         if (!interfaceNew.isEnabled()) {
188             operStatus = org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down;
189         } else {
190             String ncStr = ifState.getLowerLayerIf().get(0);
191             NodeConnectorId nodeConnectorId = new NodeConnectorId(ncStr);
192             NodeConnector nodeConnector =
193                     InterfaceManagerCommonUtils.getNodeConnectorFromInventoryOperDS(nodeConnectorId, dataBroker);
194             FlowCapableNodeConnector flowCapableNodeConnector =
195                     nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
196             //State state = flowCapableNodeConnector.getState();
197             operStatus = flowCapableNodeConnector == null ? org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down : org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Up;
198         }
199
200         String ifName = interfaceNew.getName();
201         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
202                 IfmUtil.buildStateInterfaceId(interfaceNew.getName());
203         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
204         ifaceBuilder.setOperStatus(operStatus);
205         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(ifName));
206         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
207         return operStatus;
208     }
209
210     public static void updateOperStatus(String interfaceName, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus,
211                                         WriteTransaction transaction) {
212         LOG.debug("updating operational status for interface {}",interfaceName);
213         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
214                 IfmUtil.buildStateInterfaceId(interfaceName);
215         InterfaceBuilder ifaceBuilderChild = new InterfaceBuilder();
216         ifaceBuilderChild.setOperStatus(operStatus);
217         ifaceBuilderChild.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
218         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifChildStateId, ifaceBuilderChild.build());
219     }
220
221     public static void addStateEntry(String interfaceName, WriteTransaction transaction, DataBroker dataBroker, IdManagerService idManager,
222                                      org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
223         LOG.debug("adding interface state for {}",interfaceName);
224         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus = ifState.getOperStatus();
225         PhysAddress physAddress = ifState.getPhysAddress();
226         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus adminStatus = ifState.getAdminStatus();
227         NodeConnectorId nodeConnectorId = new NodeConnectorId(ifState.getLowerLayerIf().get(0));
228         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
229         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface interfaceInfo =
230                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
231
232         if (interfaceInfo != null && !interfaceInfo.isEnabled()) {
233             operStatus = org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down;
234         }
235
236         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
237                 IfmUtil.buildStateInterfaceId(interfaceName);
238         List<String> childLowerLayerIfList = new ArrayList<>();
239         childLowerLayerIfList.add(0, nodeConnectorId.getValue());
240         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setAdminStatus(adminStatus)
241                 .setOperStatus(operStatus).setPhysAddress(physAddress).setLowerLayerIf(childLowerLayerIfList);
242
243         Integer ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, interfaceName);
244         ifaceBuilder.setIfIndex(ifIndex);
245
246         if(interfaceInfo != null){
247             ifaceBuilder.setType(interfaceInfo.getType());
248         }
249         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
250         transaction.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), true);
251
252         // create lportTag Interface Map
253         InterfaceMetaUtils.createLportTagInterfaceMap(transaction, interfaceName, ifIndex);
254
255         // install ingress flow
256         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
257         long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
258         if(interfaceInfo != null && interfaceInfo.isEnabled() && ifState.getOperStatus() == org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Up) {
259             List<MatchInfo> matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo, interfaceInfo);
260             FlowBasedServicesUtils.installVlanFlow(dpId, portNo, interfaceInfo, transaction, matches, ifIndex);
261         }
262     }
263
264     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface
265                   addStateEntry(Interface interfaceInfo, String portName, WriteTransaction transaction, IdManagerService idManager,
266                                      PhysAddress physAddress, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus,
267                                      org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus adminStatus,
268                                      NodeConnectorId nodeConnectorId) {
269         LOG.debug("adding interface state for {}",portName);
270         if (interfaceInfo != null && !interfaceInfo.isEnabled()) {
271             operStatus = org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down;
272         }
273         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
274                 IfmUtil.buildStateInterfaceId(portName);
275         List<String> childLowerLayerIfList = new ArrayList<>();
276         childLowerLayerIfList.add(0, nodeConnectorId.getValue());
277         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setAdminStatus(adminStatus)
278                 .setOperStatus(operStatus).setPhysAddress(physAddress).setLowerLayerIf(childLowerLayerIfList);
279
280         Integer ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, portName);
281         ifaceBuilder.setIfIndex(ifIndex);
282
283         if(interfaceInfo != null){
284             ifaceBuilder.setType(interfaceInfo.getType());
285         }
286         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(portName));
287         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState = ifaceBuilder.build();
288         transaction.put(LogicalDatastoreType.OPERATIONAL, ifStateId,ifState , true);
289
290         // allocate lport tag and set in if-index
291         InterfaceMetaUtils.createLportTagInterfaceMap(transaction, portName, ifIndex);
292         return ifState;
293     }
294
295     public static void deleteStateEntry(String interfaceName, WriteTransaction transaction) {
296         LOG.debug("removing interface state for {}",interfaceName);
297         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
298                 IfmUtil.buildStateInterfaceId(interfaceName);
299         transaction.delete(LogicalDatastoreType.OPERATIONAL, ifChildStateId);
300     }
301
302     // For trunk interfaces, binding to a parent interface which is already bound to another trunk interface should not
303     // be allowed
304     public static boolean createInterfaceChildEntryIfNotPresent( DataBroker dataBroker, WriteTransaction t,
305                                                String parentInterface, String childInterface){
306         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
307         InstanceIdentifier<InterfaceParentEntry> interfaceParentEntryIdentifier =
308                 InterfaceMetaUtils.getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
309         InterfaceParentEntry interfaceParentEntry =
310                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryIdentifier, dataBroker);
311
312         if(interfaceParentEntry != null){
313             LOG.error("Trying to bind the same parent interface {} to multiple trunk interfaces. ", parentInterface);
314             return false;
315         }
316
317         LOG.info("First vlan trunk {} bound on parent-interface {}", childInterface, parentInterface);
318         InterfaceChildEntryKey interfaceChildEntryKey = new InterfaceChildEntryKey(childInterface);
319         InstanceIdentifier<InterfaceChildEntry> intfId =
320                 InterfaceMetaUtils.getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
321         InterfaceChildEntryBuilder entryBuilder = new InterfaceChildEntryBuilder().setKey(interfaceChildEntryKey)
322                 .setChildInterface(childInterface);
323         t.put(LogicalDatastoreType.CONFIGURATION, intfId, entryBuilder.build(),true);
324         return true;
325     }
326
327     public static boolean deleteParentInterfaceEntry( WriteTransaction t, String parentInterface){
328         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
329         InstanceIdentifier<InterfaceParentEntry> interfaceParentEntryIdentifier = InterfaceMetaUtils.getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
330         t.delete(LogicalDatastoreType.CONFIGURATION, interfaceParentEntryIdentifier);
331         return true;
332     }
333
334     public static void deleteInterfaceChildEntry(WriteTransaction transaction, DataBroker dataBroker, String parentInterface, String childInterface){
335         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
336         InstanceIdentifier<InterfaceParentEntry> interfaceParentEntryIid =
337                 InterfaceMetaUtils.getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
338         InterfaceParentEntry interfaceParentEntry =
339                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryIid, dataBroker);
340
341         if(interfaceParentEntry == null){
342             return;
343         }
344
345         List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.getInterfaceChildEntry();
346         if (interfaceChildEntries.size() <= 1) {
347             transaction.delete(LogicalDatastoreType.CONFIGURATION, interfaceParentEntryIid);
348         } else {
349             InterfaceChildEntryKey interfaceChildEntryKey = new InterfaceChildEntryKey(childInterface);
350             InstanceIdentifier<InterfaceChildEntry> interfaceChildEntryIid =
351                     InterfaceMetaUtils.getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
352             transaction.delete(LogicalDatastoreType.CONFIGURATION, interfaceChildEntryIid);
353         }
354     }
355
356     /*
357      * update operational state of interface based on events like tunnel monitoring
358      */
359     public static void updateOpState(WriteTransaction transaction, String interfaceName,
360                                      org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus){
361         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
362                 IfmUtil.buildStateInterfaceId(interfaceName);
363         LOG.debug("updating tep interface state as {} for {}", operStatus.name(), interfaceName);
364         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setOperStatus(operStatus);
365         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
366         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
367     }
368
369 }