Merge "Having maximum id block size"
[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 java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.concurrent.ConcurrentHashMap;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.genius.interfacemanager.IfmConstants;
23 import org.opendaylight.genius.interfacemanager.IfmUtil;
24 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
25 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
26 import org.opendaylight.genius.mdsalutil.FlowEntity;
27 import org.opendaylight.genius.mdsalutil.InstructionInfo;
28 import org.opendaylight.genius.mdsalutil.InstructionType;
29 import org.opendaylight.genius.mdsalutil.MDSALUtil;
30 import org.opendaylight.genius.mdsalutil.MatchFieldType;
31 import org.opendaylight.genius.mdsalutil.MatchInfo;
32 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
33 import org.opendaylight.genius.mdsalutil.NwConstants;
34 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntryKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info._interface.parent.entry.InterfaceChildEntry;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info._interface.parent.entry.InterfaceChildEntryBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info._interface.parent.entry.InterfaceChildEntryKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeMplsOverGre;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 public class InterfaceManagerCommonUtils {
62     private static final Logger LOG = LoggerFactory.getLogger(InterfaceManagerCommonUtils.class);
63     private static ConcurrentHashMap<String, Interface> interfaceConfigMap = new ConcurrentHashMap<>();
64     private static ConcurrentHashMap<String, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> interfaceStateMap = new ConcurrentHashMap<>();
65
66     public static NodeConnector getNodeConnectorFromInventoryOperDS(NodeConnectorId nodeConnectorId,
67             DataBroker dataBroker) {
68         NodeId nodeId = IfmUtil.getNodeIdFromNodeConnectorId(nodeConnectorId);
69         InstanceIdentifier<NodeConnector> ncIdentifier = InstanceIdentifier.builder(Nodes.class)
70                 .child(Node.class, new NodeKey(nodeId))
71                 .child(NodeConnector.class, new NodeConnectorKey(nodeConnectorId)).build();
72
73         Optional<NodeConnector> nodeConnectorOptional = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, ncIdentifier,
74                 dataBroker);
75         if (!nodeConnectorOptional.isPresent()) {
76             return null;
77         }
78         return nodeConnectorOptional.get();
79     }
80
81     public static boolean isNodePresent(DataBroker dataBroker, NodeConnectorId nodeConnectorId) {
82         NodeId nodeID = IfmUtil.getNodeIdFromNodeConnectorId(nodeConnectorId);
83         InstanceIdentifier<Node> nodeInstanceIdentifier = InstanceIdentifier.builder(Nodes.class)
84                 .child(Node.class, new NodeKey(nodeID)).build();
85         Optional<Node> node = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, nodeInstanceIdentifier, dataBroker);
86
87         if (node.isPresent()) {
88             return true;
89         }
90         return false;
91     }
92
93     public static InstanceIdentifier<Interface> getInterfaceIdentifier(InterfaceKey interfaceKey) {
94         InstanceIdentifier.InstanceIdentifierBuilder<Interface> interfaceInstanceIdentifierBuilder = InstanceIdentifier
95                 .builder(Interfaces.class).child(Interface.class, interfaceKey);
96         return interfaceInstanceIdentifierBuilder.build();
97     }
98
99     public static List<Interface> getAllTunnelInterfaces(DataBroker dataBroker,
100             InterfaceInfo.InterfaceType interfaceType) {
101         List<Interface> vxlanList = new ArrayList<>();
102         InstanceIdentifier<Interfaces> interfacesInstanceIdentifier = InstanceIdentifier.builder(Interfaces.class)
103                 .build();
104         Optional<Interfaces> interfacesOptional = IfmUtil.read(LogicalDatastoreType.CONFIGURATION,
105                 interfacesInstanceIdentifier, dataBroker);
106         if (!interfacesOptional.isPresent()) {
107             return vxlanList;
108         }
109         Interfaces interfaces = interfacesOptional.get();
110         List<Interface> interfacesList = interfaces.getInterface();
111         for (Interface iface : interfacesList) {
112             if (IfmUtil.getInterfaceType(iface) == InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE
113                     && iface.getAugmentation(IfTunnel.class).isInternal()) {
114                 vxlanList.add(iface);
115             }
116         }
117         return vxlanList;
118     }
119
120     /**
121      * Searches for an interface by its name
122      * @param interfaceName name of the interface to search for
123      * @param dataBroker data tree store to start searching for the interface
124      * @return the Interface object
125      *
126      */
127     public static Interface getInterfaceFromConfigDS (String interfaceName, DataBroker dataBroker) {
128         Interface iface = interfaceConfigMap.get(interfaceName);
129         if (iface != null) {
130             return iface;
131         }
132         InstanceIdentifier<Interface> interfaceId = getInterfaceIdentifier(new InterfaceKey(interfaceName));
133         Optional<Interface> interfaceOptional = IfmUtil.read(LogicalDatastoreType.CONFIGURATION, interfaceId,
134                 dataBroker);
135         if (interfaceOptional.isPresent()) {
136             iface = interfaceOptional.get();
137             interfaceConfigMap.put(iface.getName(), iface);
138         }
139         return iface;
140     }
141
142     @Deprecated
143     public static Interface getInterfaceFromConfigDS (InterfaceKey interfaceKey, DataBroker dataBroker) {
144         return getInterfaceFromConfigDS(interfaceKey.getName(), dataBroker);
145     }
146
147     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS(
148             String interfaceName, DataBroker dataBroker) {
149         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState = interfaceStateMap
150                 .get(interfaceName);
151         if (ifState != null) {
152             return ifState;
153         }
154         Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateOptional = IfmUtil
155                 .read(LogicalDatastoreType.OPERATIONAL, IfmUtil.buildStateInterfaceId(interfaceName), dataBroker);
156         if (ifStateOptional.isPresent()) {
157             ifState = ifStateOptional.get();
158             interfaceStateMap.put(ifState.getName(), ifState);
159         }
160         return ifState;
161     }
162
163     @Deprecated
164     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS(
165             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId,
166             DataBroker dataBroker) {
167         Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateOptional = IfmUtil
168                 .read(LogicalDatastoreType.OPERATIONAL, ifStateId, dataBroker);
169         if (!ifStateOptional.isPresent()) {
170             return null;
171         }
172
173         return ifStateOptional.get();
174     }
175
176     public static void makeTunnelIngressFlow(List<ListenableFuture<Void>> futures, IMdsalApiManager mdsalApiManager,
177             IfTunnel tunnel, BigInteger dpnId, long portNo, String interfaceName, int ifIndex, int addOrRemoveFlow) {
178         LOG.debug("make tunnel ingress flow for {}", interfaceName);
179         String flowRef = InterfaceManagerCommonUtils.getTunnelInterfaceFlowRef(dpnId,
180                 NwConstants.VLAN_INTERFACE_INGRESS_TABLE, interfaceName);
181         List<MatchInfo> matches = new ArrayList<>();
182         List<InstructionInfo> mkInstructions = new ArrayList<>();
183         if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
184             matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] { dpnId, BigInteger.valueOf(portNo) }));
185             mkInstructions.add(new InstructionInfo(InstructionType.write_metadata,
186                     new BigInteger[] { MetaDataUtil.getLportTagMetaData(ifIndex).or(BigInteger.ONE),
187                             MetaDataUtil.METADATA_MASK_LPORT_TAG_SH_FLAG }));
188             short tableId = (tunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeMplsOverGre.class))
189                     ? NwConstants.L3_LFIB_TABLE
190                     : tunnel.isInternal() ? NwConstants.INTERNAL_TUNNEL_TABLE : NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL;
191             mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { tableId }));
192         }
193
194         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.VLAN_INTERFACE_INGRESS_TABLE, flowRef,
195                 IfmConstants.DEFAULT_FLOW_PRIORITY, interfaceName, 0, 0, NwConstants.COOKIE_VM_INGRESS_TABLE, matches,
196                 mkInstructions);
197         if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
198             futures.add(mdsalApiManager.installFlow(dpnId, flowEntity));
199         } else {
200             futures.add(mdsalApiManager.removeFlow(dpnId, flowEntity));
201         }
202     }
203
204     public static String getTunnelInterfaceFlowRef(BigInteger dpnId, short tableId, String ifName) {
205         return new StringBuilder().append(dpnId).append(tableId).append(ifName).toString();
206     }
207
208     public static void setOpStateForInterface(DataBroker broker, String interfaceName,
209             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus opStatus) {
210         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> interfaceId = IfmUtil
211                 .buildStateInterfaceId(interfaceName);
212         InterfaceBuilder ifaceBuilder = new InterfaceBuilder()
213                 .setKey(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
214                         interfaceName));
215         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceData = ifaceBuilder
216                 .setOperStatus(opStatus).build();
217         MDSALUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, interfaceId, interfaceData);
218     }
219
220     public static void createInterfaceChildEntry(WriteTransaction t, String parentInterface, String childInterface) {
221         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
222         InterfaceChildEntryKey interfaceChildEntryKey = new InterfaceChildEntryKey(childInterface);
223         InstanceIdentifier<InterfaceChildEntry> intfId = InterfaceMetaUtils
224                 .getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
225         InterfaceChildEntryBuilder entryBuilder = new InterfaceChildEntryBuilder().setKey(interfaceChildEntryKey)
226                 .setChildInterface(childInterface);
227         t.put(LogicalDatastoreType.CONFIGURATION, intfId, entryBuilder.build(), true);
228     }
229
230     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus updateStateEntry(
231             Interface interfaceNew, DataBroker dataBroker, WriteTransaction transaction,
232             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
233         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus;
234         if (!interfaceNew.isEnabled()) {
235             operStatus = org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down;
236         } else {
237             String ncStr = ifState.getLowerLayerIf().get(0);
238             NodeConnectorId nodeConnectorId = new NodeConnectorId(ncStr);
239             NodeConnector nodeConnector = InterfaceManagerCommonUtils
240                     .getNodeConnectorFromInventoryOperDS(nodeConnectorId, dataBroker);
241             FlowCapableNodeConnector flowCapableNodeConnector = nodeConnector
242                     .getAugmentation(FlowCapableNodeConnector.class);
243             // State state = flowCapableNodeConnector.getState();
244             operStatus = flowCapableNodeConnector == null
245                     ? org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down
246                     : org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Up;
247         }
248
249         String ifName = interfaceNew.getName();
250         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId = IfmUtil
251                 .buildStateInterfaceId(interfaceNew.getName());
252         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
253         ifaceBuilder.setOperStatus(operStatus);
254         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(ifName));
255         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
256         return operStatus;
257     }
258
259     public static void updateOperStatus(String interfaceName,
260             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus,
261             WriteTransaction transaction) {
262         LOG.debug("updating operational status for interface {}", interfaceName);
263         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId = IfmUtil
264                 .buildStateInterfaceId(interfaceName);
265         InterfaceBuilder ifaceBuilderChild = new InterfaceBuilder();
266         ifaceBuilderChild.setOperStatus(operStatus);
267         ifaceBuilderChild.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
268         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifChildStateId, ifaceBuilderChild.build());
269     }
270
271     public static void addStateEntry(String interfaceName, WriteTransaction transaction, DataBroker dataBroker,
272             IdManagerService idManager,
273             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
274         // allocate lport tag and create interface-if-index map.
275         // This is done even if interface-state is not present, so that there is
276         // no throttling
277         // on id allocation even when multiple southbound port_up events come in
278         // one shot
279         Integer ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, interfaceName);
280         InterfaceMetaUtils.createLportTagInterfaceMap(transaction, interfaceName, ifIndex);
281         if (ifState == null) {
282             LOG.debug("could not retrieve interface state corresponding to {}", interfaceName);
283             return;
284         }
285         LOG.debug("adding interface state for {}", interfaceName);
286         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus = ifState
287                 .getOperStatus();
288         PhysAddress physAddress = ifState.getPhysAddress();
289         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus adminStatus = ifState
290                 .getAdminStatus();
291         NodeConnectorId nodeConnectorId = new NodeConnectorId(ifState.getLowerLayerIf().get(0));
292         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
293         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface interfaceInfo = InterfaceManagerCommonUtils
294                 .getInterfaceFromConfigDS(interfaceKey, dataBroker);
295
296         if (interfaceInfo != null && !interfaceInfo.isEnabled()) {
297             operStatus = org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down;
298         }
299
300         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId = IfmUtil
301                 .buildStateInterfaceId(interfaceName);
302         List<String> childLowerLayerIfList = new ArrayList<>();
303         childLowerLayerIfList.add(0, nodeConnectorId.getValue());
304         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setAdminStatus(adminStatus).setOperStatus(operStatus)
305                 .setPhysAddress(physAddress).setLowerLayerIf(childLowerLayerIfList);
306         ifaceBuilder.setIfIndex(ifIndex);
307
308         if (interfaceInfo != null) {
309             ifaceBuilder.setType(interfaceInfo.getType());
310         }
311         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
312         transaction.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), true);
313
314         // install ingress flow
315         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
316         long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
317         if (interfaceInfo != null && interfaceInfo.isEnabled() && ifState
318                 .getOperStatus() == org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Up) {
319             List<MatchInfo> matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo,
320                     interfaceInfo);
321             FlowBasedServicesUtils.installVlanFlow(dpId, portNo, interfaceInfo, transaction, matches, ifIndex);
322             FlowBasedServicesUtils.bindDefaultEgressDispatcherService(interfaceInfo, Long.toString(portNo), interfaceName, transaction, ifIndex);
323         }
324     }
325
326     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface addStateEntry(
327             Interface interfaceInfo, String interfaceName, WriteTransaction transaction, IdManagerService idManager,
328             PhysAddress physAddress,
329             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus,
330             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus adminStatus,
331             NodeConnectorId nodeConnectorId) {
332         LOG.debug("adding interface state for {}", interfaceName);
333         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
334         Integer ifIndex = null;
335         if (interfaceInfo != null) {
336             if (!interfaceInfo.isEnabled()) {
337                 operStatus = org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus.Down;
338             }
339
340             ifaceBuilder.setType(interfaceInfo.getType());
341             // retrieve if-index only for northbound configured interfaces
342             ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, interfaceName);
343             ifaceBuilder.setIfIndex(ifIndex);
344             InterfaceMetaUtils.createLportTagInterfaceMap(transaction, interfaceName, ifIndex);
345         }
346         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId = IfmUtil
347                 .buildStateInterfaceId(interfaceName);
348         List<String> childLowerLayerIfList = new ArrayList<>();
349         childLowerLayerIfList.add(0, nodeConnectorId.getValue());
350         ifaceBuilder.setAdminStatus(adminStatus).setOperStatus(operStatus).setPhysAddress(physAddress)
351                 .setLowerLayerIf(childLowerLayerIfList);
352         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
353         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState = ifaceBuilder
354                 .build();
355         transaction.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifState, true);
356         return ifState;
357     }
358
359     public static void deleteStateEntry(String interfaceName, WriteTransaction transaction) {
360         LOG.debug("removing interface state entry for {}", interfaceName);
361         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId = IfmUtil
362                 .buildStateInterfaceId(interfaceName);
363         transaction.delete(LogicalDatastoreType.OPERATIONAL, ifChildStateId);
364     }
365
366     public static void deleteInterfaceStateInformation(String interfaceName, WriteTransaction transaction,
367             IdManagerService idManagerService) {
368         LOG.debug("removing interface state information for {}", interfaceName);
369         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId = IfmUtil
370                 .buildStateInterfaceId(interfaceName);
371         transaction.delete(LogicalDatastoreType.OPERATIONAL, ifStateId);
372         InterfaceMetaUtils.removeLportTagInterfaceMap(idManagerService, transaction, interfaceName);
373     }
374
375     // For trunk interfaces, binding to a parent interface which is already
376     // bound to another trunk interface should not
377     // be allowed
378     public static boolean createInterfaceChildEntryIfNotPresent(DataBroker dataBroker, WriteTransaction t,
379             String parentInterface, String childInterface) {
380         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
381         InstanceIdentifier<InterfaceParentEntry> interfaceParentEntryIdentifier = InterfaceMetaUtils
382                 .getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
383         InterfaceParentEntry interfaceParentEntry = InterfaceMetaUtils
384                 .getInterfaceParentEntryFromConfigDS(interfaceParentEntryIdentifier, dataBroker);
385
386         if (interfaceParentEntry != null) {
387             LOG.error("Trying to bind the same parent interface {} to multiple trunk interfaces. ", parentInterface);
388             return false;
389         }
390
391         LOG.info("First vlan trunk {} bound on parent-interface {}", childInterface, parentInterface);
392         InterfaceChildEntryKey interfaceChildEntryKey = new InterfaceChildEntryKey(childInterface);
393         InstanceIdentifier<InterfaceChildEntry> intfId = InterfaceMetaUtils
394                 .getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
395         InterfaceChildEntryBuilder entryBuilder = new InterfaceChildEntryBuilder().setKey(interfaceChildEntryKey)
396                 .setChildInterface(childInterface);
397         t.put(LogicalDatastoreType.CONFIGURATION, intfId, entryBuilder.build(), true);
398         return true;
399     }
400
401     public static boolean deleteParentInterfaceEntry(WriteTransaction t, String parentInterface) {
402         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentInterface);
403         InstanceIdentifier<InterfaceParentEntry> interfaceParentEntryIdentifier = InterfaceMetaUtils
404                 .getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
405         t.delete(LogicalDatastoreType.CONFIGURATION, interfaceParentEntryIdentifier);
406         return true;
407     }
408
409     /*
410      * update operational state of interface based on events like tunnel
411      * monitoring
412      */
413     public static void updateOpState(WriteTransaction transaction, String interfaceName,
414             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus operStatus) {
415         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId = IfmUtil
416                 .buildStateInterfaceId(interfaceName);
417         LOG.debug("updating tep interface state as {} for {}", operStatus.name(), interfaceName);
418         InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setOperStatus(operStatus);
419         ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceName));
420         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), false);
421     }
422
423     public static boolean isTunnelInterface(Interface interfaceInfo) {
424         if (interfaceInfo != null && interfaceInfo.getAugmentation(IfTunnel.class) != null) {
425             return true;
426         }
427         return false;
428     }
429
430     public static boolean isVlanInterface(Interface interfaceInfo) {
431         if (interfaceInfo != null && interfaceInfo.getAugmentation(IfL2vlan.class) != null) {
432             return true;
433         }
434         return false;
435     }
436
437     // Cache Util methods
438     public static void addInterfaceToCache(Interface iface) {
439         interfaceConfigMap.put(iface.getName(), iface);
440     }
441
442     public static void removeFromInterfaceCache(Interface iface) {
443         interfaceConfigMap.remove(iface.getName());
444     }
445
446     public static void addInterfaceStateToCache(
447             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface iface) {
448         interfaceStateMap.put(iface.getName(), iface);
449     }
450
451     public static void removeFromInterfaceStateCache(
452             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface iface) {
453         interfaceStateMap.remove(iface.getName());
454     }
455
456     public static List<BigInteger> getListOfDpns(DataBroker broker) {
457         List<BigInteger> dpnsList = new LinkedList<>();
458         InstanceIdentifier<Nodes> nodesInstanceIdentifier = InstanceIdentifier.builder(Nodes.class).build();
459         Optional<Nodes> nodesOptional = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
460                 nodesInstanceIdentifier);
461         if (!nodesOptional.isPresent()) {
462             return dpnsList;
463         }
464         Nodes nodes = nodesOptional.get();
465         List<Node> nodeList = nodes.getNode();
466         for (Node node : nodeList) {
467             NodeId nodeId = node.getId();
468             if (nodeId == null) {
469                 continue;
470             }
471             BigInteger dpnId = MDSALUtil.getDpnIdFromNodeName(nodeId);
472             dpnsList.add(dpnId);
473         }
474         return dpnsList;
475     }
476
477     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateForUnknownDpn(
478             String interfaceName, DataBroker dataBroker) {
479         String parentInterface;
480         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState = null;
481         List<BigInteger> listOfDpns = InterfaceManagerCommonUtils.getListOfDpns(dataBroker);
482         for (BigInteger dpnId : listOfDpns) {
483             parentInterface = new StringBuilder().append(dpnId).append(IfmConstants.OF_URI_SEPARATOR)
484                     .append(interfaceName).toString();
485             ifState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(parentInterface, dataBroker);
486             if (ifState != null) {
487                 break;
488             }
489         }
490         return ifState;
491     }
492
493     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromDS(
494             String interfaceName) {
495         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState = null;
496         for (Map.Entry<String, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> entry : interfaceStateMap
497                 .entrySet()) {
498             if (entry.getKey().contains(interfaceName)) {
499                 ifState = entry.getValue();
500             }
501         }
502         return ifState;
503     }
504 }