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