Merge "Ingress/Egress Service Binding Renderers"
[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 boolean isNodePresent(DataBroker dataBroker, NodeConnectorId nodeConnectorId){
71         NodeId nodeID = IfmUtil.getNodeIdFromNodeConnectorId(nodeConnectorId);
72         InstanceIdentifier<Node> nodeInstanceIdentifier = InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeID)).build();
73         Optional<Node> node = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, nodeInstanceIdentifier, dataBroker);
74
75         if(node.isPresent()){
76             return true;
77         }
78         return false;
79     }
80
81     public static InstanceIdentifier<Interface> getInterfaceIdentifier(InterfaceKey interfaceKey) {
82         InstanceIdentifier.InstanceIdentifierBuilder<Interface> interfaceInstanceIdentifierBuilder =
83                 InstanceIdentifier.builder(Interfaces.class).child(Interface.class, interfaceKey);
84         return interfaceInstanceIdentifierBuilder.build();
85     }
86
87     public static List<Interface> getAllTunnelInterfaces(DataBroker dataBroker, InterfaceInfo.InterfaceType interfaceType) {
88         List<Interface> vxlanList = new ArrayList<Interface>();
89         InstanceIdentifier<Interfaces> interfacesInstanceIdentifier =  InstanceIdentifier.builder(Interfaces.class).build();
90         Optional<Interfaces> interfacesOptional  = IfmUtil.read(LogicalDatastoreType.CONFIGURATION, interfacesInstanceIdentifier, dataBroker);
91         if (!interfacesOptional.isPresent()) {
92             return vxlanList;
93         }
94         Interfaces interfaces = interfacesOptional.get();
95         List<Interface> interfacesList = interfaces.getInterface();
96         for (Interface iface : interfacesList) {
97             if(IfmUtil.getInterfaceType(iface) == InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE &&
98                     iface.getAugmentation(IfTunnel.class).isInternal()) {
99                 vxlanList.add(iface);
100             }
101         }
102         return vxlanList;
103     }
104
105     public static Interface getInterfaceFromConfigDS(String interfaceName, DataBroker dataBroker) {
106         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
107         return getInterfaceFromConfigDS(interfaceKey, dataBroker);
108     }
109
110     public static Interface getInterfaceFromConfigDS(InterfaceKey interfaceKey, DataBroker dataBroker) {
111         InstanceIdentifier<Interface> interfaceId = getInterfaceIdentifier(interfaceKey);
112         Optional<Interface> interfaceOptional = IfmUtil.read(LogicalDatastoreType.CONFIGURATION, interfaceId, dataBroker);
113         if (!interfaceOptional.isPresent()) {
114             return null;
115         }
116
117         return interfaceOptional.get();
118     }
119
120     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS(String interfaceName, DataBroker dataBroker) {
121         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
122                 IfmUtil.buildStateInterfaceId(interfaceName);
123         return getInterfaceStateFromOperDS(ifStateId, dataBroker);
124     }
125
126     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS
127             (InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId, DataBroker dataBroker) {
128         Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateOptional =
129                 IfmUtil.read(LogicalDatastoreType.OPERATIONAL, ifStateId, dataBroker);
130         if (!ifStateOptional.isPresent()) {
131             return null;
132         }
133
134         return ifStateOptional.get();
135     }
136     public static void makeTunnelIngressFlow(List<ListenableFuture<Void>> futures, IMdsalApiManager mdsalApiManager,
137                                              IfTunnel tunnel, BigInteger dpnId, long portNo, String interfaceName, int ifIndex, int addOrRemoveFlow) {
138         LOG.debug("make tunnel ingress flow for {}",interfaceName);
139         String flowRef = InterfaceManagerCommonUtils.getTunnelInterfaceFlowRef(dpnId, NwConstants.VLAN_INTERFACE_INGRESS_TABLE, interfaceName);
140         List<MatchInfo> matches = new ArrayList<MatchInfo>();
141         List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
142         if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
143             matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {
144                     dpnId, BigInteger.valueOf(portNo) }));
145             mkInstructions.add(new InstructionInfo(
146                     InstructionType.write_metadata, new BigInteger[] {
147                     MetaDataUtil.getLportTagMetaData(ifIndex).or(BigInteger.ONE),
148                     MetaDataUtil.METADATA_MASK_LPORT_TAG_SH_FLAG}));
149             short tableId = (tunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeMplsOverGre.class)) ? NwConstants.L3_LFIB_TABLE :
150                     tunnel.isInternal() ? NwConstants.INTERNAL_TUNNEL_TABLE : NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL;
151             mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] {tableId}));
152         }
153
154         BigInteger COOKIE_VM_INGRESS_TABLE = new BigInteger("8000001", 16);
155         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.VLAN_INTERFACE_INGRESS_TABLE, flowRef,
156                 IfmConstants.DEFAULT_FLOW_PRIORITY, interfaceName, 0, 0, COOKIE_VM_INGRESS_TABLE, matches, mkInstructions);
157         if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
158             futures.add(mdsalApiManager.installFlow(dpnId, flowEntity));
159         } else {
160             futures.add(mdsalApiManager.removeFlow(dpnId, flowEntity));
161         }
162     }
163     public static String getTunnelInterfaceFlowRef(BigInteger dpnId, short tableId, String ifName) {
164         return new StringBuilder().append(dpnId).append(tableId).append(ifName).toString();
165     }
166
167     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) {
168         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> interfaceId = IfmUtil.buildStateInterfaceId(interfaceName);
169         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setKey(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey(interfaceName));
170         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceData = ifaceBuilder.setOperStatus(opStatus).build();
171         MDSALUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, interfaceId, interfaceData);
172     }
173
174     public static void createInterfaceChildEntry( WriteTransaction t,
175                                                   String parentInterface, String childInterface){
176         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
177         InterfaceChildEntryKey interfaceChildEntryKey = new InterfaceChildEntryKey(childInterface);
178         InstanceIdentifier<InterfaceChildEntry> intfId =
179                 InterfaceMetaUtils.getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
180         InterfaceChildEntryBuilder entryBuilder = new InterfaceChildEntryBuilder().setKey(interfaceChildEntryKey)
181                 .setChildInterface(childInterface);
182         t.put(LogicalDatastoreType.CONFIGURATION, intfId, entryBuilder.build(),true);
183     }
184
185     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus
186     updateStateEntry(Interface interfaceNew, DataBroker dataBroker, WriteTransaction transaction,
187                      org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
188         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus;
189         if (!interfaceNew.isEnabled()) {
190             operStatus = org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down;
191         } else {
192             String ncStr = ifState.getLowerLayerIf().get(0);
193             NodeConnectorId nodeConnectorId = new NodeConnectorId(ncStr);
194             NodeConnector nodeConnector =
195                     InterfaceManagerCommonUtils.getNodeConnectorFromInventoryOperDS(nodeConnectorId, dataBroker);
196             FlowCapableNodeConnector flowCapableNodeConnector =
197                     nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
198             //State state = flowCapableNodeConnector.getState();
199             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;
200         }
201
202         String ifName = interfaceNew.getName();
203         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
204                 IfmUtil.buildStateInterfaceId(interfaceNew.getName());
205         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
206         ifaceBuilder.setOperStatus(operStatus);
207         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(ifName));
208         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
209         return operStatus;
210     }
211
212     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,
213                                         WriteTransaction transaction) {
214         LOG.debug("updating operational status for interface {}",interfaceName);
215         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
216                 IfmUtil.buildStateInterfaceId(interfaceName);
217         InterfaceBuilder ifaceBuilderChild = new InterfaceBuilder();
218         ifaceBuilderChild.setOperStatus(operStatus);
219         ifaceBuilderChild.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
220         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifChildStateId, ifaceBuilderChild.build());
221     }
222
223     public static void addStateEntry(String interfaceName, WriteTransaction transaction, DataBroker dataBroker, IdManagerService idManager,
224                                      org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
225         // allocate lport tag and create interface-if-index map.
226         // This is done even if interface-state is not present, so that there is no throttling
227         // on id allocation even when multiple southbound port_up events come in one shot
228         Integer ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, interfaceName);
229         InterfaceMetaUtils.createLportTagInterfaceMap(transaction, interfaceName, ifIndex);
230         if(ifState == null){
231             LOG.debug("could not retrieve interface state corresponding to {}",interfaceName);
232             return;
233         }
234         LOG.debug("adding interface state for {}",interfaceName);
235         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus = ifState.getOperStatus();
236         PhysAddress physAddress = ifState.getPhysAddress();
237         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus adminStatus = ifState.getAdminStatus();
238         NodeConnectorId nodeConnectorId = new NodeConnectorId(ifState.getLowerLayerIf().get(0));
239         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
240         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface interfaceInfo =
241                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
242
243         if (interfaceInfo != null && !interfaceInfo.isEnabled()) {
244             operStatus = org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down;
245         }
246
247         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
248                 IfmUtil.buildStateInterfaceId(interfaceName);
249         List<String> childLowerLayerIfList = new ArrayList<>();
250         childLowerLayerIfList.add(0, nodeConnectorId.getValue());
251         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setAdminStatus(adminStatus)
252                 .setOperStatus(operStatus).setPhysAddress(physAddress).setLowerLayerIf(childLowerLayerIfList);
253         ifaceBuilder.setIfIndex(ifIndex);
254
255         if(interfaceInfo != null){
256             ifaceBuilder.setType(interfaceInfo.getType());
257         }
258         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
259         transaction.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), true);
260
261         // install ingress flow
262         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
263         long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
264         if(interfaceInfo != null && interfaceInfo.isEnabled() &&
265                 ifState.getOperStatus() == org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Up) {
266             List<MatchInfo> matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo, interfaceInfo);
267             FlowBasedServicesUtils.installVlanFlow(dpId, portNo, interfaceInfo, transaction, matches, ifIndex);
268         }
269     }
270
271     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,
272                                                                                                                                               PhysAddress physAddress, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus,
273                                                                                                                                               org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus adminStatus,
274                                                                                                                                               NodeConnectorId nodeConnectorId) {
275         LOG.debug("adding interface state for {}",portName);
276         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
277         Integer ifIndex = null;
278         if (interfaceInfo != null){
279             if(!interfaceInfo.isEnabled()){
280                 operStatus = org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down;
281             }
282
283             ifaceBuilder.setType(interfaceInfo.getType());
284             // retrieve if-index only for northbound configured interfaces
285             ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, portName);
286             ifaceBuilder.setIfIndex(ifIndex);
287         }
288         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
289                 IfmUtil.buildStateInterfaceId(portName);
290         List<String> childLowerLayerIfList = new ArrayList<>();
291         childLowerLayerIfList.add(0, nodeConnectorId.getValue());
292         ifaceBuilder.setAdminStatus(adminStatus)
293                 .setOperStatus(operStatus).setPhysAddress(physAddress).setLowerLayerIf(childLowerLayerIfList);
294         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(portName));
295         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState = ifaceBuilder.build();
296         transaction.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifState , true);
297         return ifState;
298     }
299
300     public static void deleteStateEntry(String interfaceName, WriteTransaction transaction) {
301         LOG.debug("removing interface state entry for {}",interfaceName);
302         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
303                 IfmUtil.buildStateInterfaceId(interfaceName);
304         transaction.delete(LogicalDatastoreType.OPERATIONAL, ifChildStateId);
305     }
306
307     public static void deleteInterfaceStateInformation(String interfaceName, WriteTransaction transaction, IdManagerService idManagerService) {
308         LOG.debug("removing interface state information for {}",interfaceName);
309         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
310                 IfmUtil.buildStateInterfaceId(interfaceName);
311         transaction.delete(LogicalDatastoreType.OPERATIONAL, ifStateId);
312         InterfaceMetaUtils.removeLportTagInterfaceMap(idManagerService, transaction, interfaceName);
313     }
314
315     // For trunk interfaces, binding to a parent interface which is already bound to another trunk interface should not
316     // be allowed
317     public static boolean createInterfaceChildEntryIfNotPresent( DataBroker dataBroker, WriteTransaction t,
318                                                                  String parentInterface, String childInterface){
319         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
320         InstanceIdentifier<InterfaceParentEntry> interfaceParentEntryIdentifier =
321                 InterfaceMetaUtils.getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
322         InterfaceParentEntry interfaceParentEntry =
323                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryIdentifier, dataBroker);
324
325         if(interfaceParentEntry != null){
326             LOG.error("Trying to bind the same parent interface {} to multiple trunk interfaces. ", parentInterface);
327             return false;
328         }
329
330         LOG.info("First vlan trunk {} bound on parent-interface {}", childInterface, parentInterface);
331         InterfaceChildEntryKey interfaceChildEntryKey = new InterfaceChildEntryKey(childInterface);
332         InstanceIdentifier<InterfaceChildEntry> intfId =
333                 InterfaceMetaUtils.getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
334         InterfaceChildEntryBuilder entryBuilder = new InterfaceChildEntryBuilder().setKey(interfaceChildEntryKey)
335                 .setChildInterface(childInterface);
336         t.put(LogicalDatastoreType.CONFIGURATION, intfId, entryBuilder.build(),true);
337         return true;
338     }
339
340     public static boolean deleteParentInterfaceEntry( WriteTransaction t, String parentInterface){
341         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
342         InstanceIdentifier<InterfaceParentEntry> interfaceParentEntryIdentifier = InterfaceMetaUtils.getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
343         t.delete(LogicalDatastoreType.CONFIGURATION, interfaceParentEntryIdentifier);
344         return true;
345     }
346
347     /*
348      * update operational state of interface based on events like tunnel monitoring
349      */
350     public static void updateOpState(WriteTransaction transaction, String interfaceName,
351                                      org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus){
352         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
353                 IfmUtil.buildStateInterfaceId(interfaceName);
354         LOG.debug("updating tep interface state as {} for {}", operStatus.name(), interfaceName);
355         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setOperStatus(operStatus);
356         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
357         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
358     }
359
360     public static boolean isTunnelInterface(Interface interfaceInfo){
361         if(interfaceInfo != null && interfaceInfo.getAugmentation(IfTunnel.class) != null){
362             return true;
363         }
364         return false;
365     }
366
367     public static boolean isVlanInterface(Interface interfaceInfo){
368         if(interfaceInfo != null && interfaceInfo.getAugmentation(IfL2vlan.class) != null){
369             return true;
370         }
371         return false;
372     }
373
374 }