2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.vpnservice.interfacemgr.commons;
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.idmanager.IdManager;
15 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
16 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.GetUniqueIdInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.GetUniqueIdInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.GetUniqueIdOutput;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.math.BigInteger;
36 import java.util.concurrent.ExecutionException;
37 import java.util.concurrent.Future;
39 public class InterfaceManagerCommonUtils {
40 private static final Logger LOG = LoggerFactory.getLogger(InterfaceManagerCommonUtils.class);
41 public static NodeConnector getNodeConnectorFromInventoryOperDS(NodeConnectorId nodeConnectorId,
42 DataBroker dataBroker) {
43 NodeId nodeId = IfmUtil.getNodeIdFromNodeConnectorId(nodeConnectorId);
44 InstanceIdentifier<NodeConnector> ncIdentifier = InstanceIdentifier.builder(Nodes.class)
45 .child(Node.class, new NodeKey(nodeId))
46 .child(NodeConnector.class, new NodeConnectorKey(nodeConnectorId)).build();
48 Optional<NodeConnector> nodeConnectorOptional = IfmUtil.read(LogicalDatastoreType.OPERATIONAL,
49 ncIdentifier, dataBroker);
50 if (!nodeConnectorOptional.isPresent()) {
53 return nodeConnectorOptional.get();
56 /*public static void addInterfaceEntryToInventoryOperDS(NodeConnectorId nodeConnectorId, long lporttag, String interfaceName,
57 DataBroker dataBroker, WriteTransaction t) {
58 NodeId nodeId = IfmUtil.getNodeIdFromNodeConnectorId(nodeConnectorId);
59 TunnelInterfaceInventoryInfoKey tunnelInterfaceInventoryInfoKey = new TunnelInterfaceInventoryInfoKey(lporttag);
60 InstanceIdentifier<TunnelInterfaceInventoryInfo> inventoryIdentifier = InstanceIdentifier.builder(Nodes.class)
61 .child(Node.class, new NodeKey(nodeId))
62 .augmentation(TunnelInterfaceNames.class)
63 .child(TunnelInterfaceInventoryInfo.class, tunnelInterfaceInventoryInfoKey).build();
64 TunnelInterfaceInventoryInfoBuilder builder = new TunnelInterfaceInventoryInfoBuilder().setKey(tunnelInterfaceInventoryInfoKey)
65 .setTunIntfName(interfaceName);
66 t.put(LogicalDatastoreType.OPERATIONAL, inventoryIdentifier, builder.build(), true);
69 public static void removeInterfaceEntryFromInventoryOperDS(NodeConnectorId nodeConnectorId, long lporttag,
70 String interfaceName, DataBroker dataBroker,
72 NodeId nodeId = IfmUtil.getNodeIdFromNodeConnectorId(nodeConnectorId);
73 TunnelInterfaceInventoryInfoKey tunnelInterfaceInventoryInfoKey = new TunnelInterfaceInventoryInfoKey(lporttag);
74 InstanceIdentifier<TunnelInterfaceInventoryInfo> inventoryIdentifier = InstanceIdentifier.builder(Nodes.class)
75 .child(Node.class, new NodeKey(nodeId))
76 .augmentation(TunnelInterfaceNames.class)
77 .child(TunnelInterfaceInventoryInfo.class, tunnelInterfaceInventoryInfoKey).build();
78 t.delete(LogicalDatastoreType.OPERATIONAL, inventoryIdentifier);
81 public static void removeInterfaceEntryFromInventoryOperDS(NodeConnectorId nodeConnectorId, long lporttag,
82 DataBroker dataBroker) {
83 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
84 NodeId nodeId = IfmUtil.getNodeIdFromNodeConnectorId(nodeConnectorId);
85 TunnelInterfaceInventoryInfoKey tunnelInterfaceInventoryInfoKey = new TunnelInterfaceInventoryInfoKey(lporttag);
86 InstanceIdentifier<TunnelInterfaceInventoryInfo> inventoryIdentifier = InstanceIdentifier.builder(Nodes.class)
87 .child(Node.class, new NodeKey(nodeId))
88 .augmentation(TunnelInterfaceNames.class)
89 .child(TunnelInterfaceInventoryInfo.class, tunnelInterfaceInventoryInfoKey).build();
90 t.delete(LogicalDatastoreType.OPERATIONAL, inventoryIdentifier);
91 t.submit(); // This is a Best-Effort Deletion. If Node is already removed, this may fail.
94 public static InstanceIdentifier<Interface> getInterfaceIdentifier(InterfaceKey interfaceKey) {
95 InstanceIdentifier.InstanceIdentifierBuilder<Interface> interfaceInstanceIdentifierBuilder =
96 InstanceIdentifier.builder(Interfaces.class).child(Interface.class, interfaceKey);
97 return interfaceInstanceIdentifierBuilder.build();
100 public static Interface getInterfaceFromConfigDS(InterfaceKey interfaceKey, DataBroker dataBroker) {
101 InstanceIdentifier<Interface> interfaceId = getInterfaceIdentifier(interfaceKey);
102 Optional<Interface> interfaceOptional = IfmUtil.read(LogicalDatastoreType.CONFIGURATION, interfaceId, dataBroker);
103 if (!interfaceOptional.isPresent()) {
107 return interfaceOptional.get();
110 public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS(String interfaceName, DataBroker dataBroker) {
111 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
112 IfmUtil.buildStateInterfaceId(interfaceName);
113 Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateOptional =
114 IfmUtil.read(LogicalDatastoreType.OPERATIONAL, ifStateId, dataBroker);
115 if (!ifStateOptional.isPresent()) {
119 return ifStateOptional.get();
122 public static Integer getUniqueId(IdManager idManager, String idKey) {
123 GetUniqueIdInput getIdInput = new GetUniqueIdInputBuilder()
124 .setPoolName(IfmConstants.IFM_LPORT_TAG_IDPOOL_NAME)
125 .setIdKey(idKey).build();
128 Future<RpcResult<GetUniqueIdOutput>> result = idManager.
129 getUniqueId(getIdInput);
130 RpcResult<GetUniqueIdOutput> rpcResult = result.get();
131 if(rpcResult.isSuccessful()) {
132 return rpcResult.getResult().getIdValue().intValue();
134 LOG.warn("RPC Call to Get Unique Id returned with Errors {}", rpcResult.getErrors());
136 } catch (NullPointerException | InterruptedException | ExecutionException e) {
137 LOG.warn("Exception when getting Unique Id",e);
142 public static String getJobKey(String dpId, String portName) {
144 if (dpId != null && !"".equals(dpId)) {
145 jobKey = dpId.toString() + ":";
147 jobKey = jobKey + portName;
151 public static String getJobKey(BigInteger dpId, String portName) {
153 if (dpId != null && dpId.longValue() != 0) {
154 jobKey = dpId.toString() + ":";
156 jobKey = jobKey + portName;