6b10cb0c55bad7a1631377c705703ad5528d73ff
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / commons / InterfaceManagerCommonUtils.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.vpnservice.interfacemgr.commons;
10
11 import 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;
34
35 import java.math.BigInteger;
36 import java.util.concurrent.ExecutionException;
37 import java.util.concurrent.Future;
38
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();
47
48         Optional<NodeConnector> nodeConnectorOptional = IfmUtil.read(LogicalDatastoreType.OPERATIONAL,
49                 ncIdentifier, dataBroker);
50         if (!nodeConnectorOptional.isPresent()) {
51             return null;
52         }
53         return nodeConnectorOptional.get();
54     }
55
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);
67     }
68
69     public static void removeInterfaceEntryFromInventoryOperDS(NodeConnectorId nodeConnectorId, long lporttag,
70                                                                String interfaceName, DataBroker dataBroker,
71                                                                WriteTransaction t) {
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);
79     }
80
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.
92     } */
93
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();
98     }
99
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()) {
104             return null;
105         }
106
107         return interfaceOptional.get();
108     }
109
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()) {
116             return null;
117         }
118
119         return ifStateOptional.get();
120     }
121
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();
126
127         try {
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();
133             } else {
134                 LOG.warn("RPC Call to Get Unique Id returned with Errors {}", rpcResult.getErrors());
135             }
136         } catch (NullPointerException | InterruptedException | ExecutionException e) {
137             LOG.warn("Exception when getting Unique Id",e);
138         }
139         return 0;
140     }
141
142     public static String getJobKey(String dpId, String portName) {
143         String jobKey = "";
144         if (dpId != null && !"".equals(dpId)) {
145             jobKey = dpId.toString() + ":";
146         }
147         jobKey = jobKey + portName;
148         return jobKey;
149     }
150
151     public static String getJobKey(BigInteger dpId, String portName) {
152         String jobKey = "";
153         if (dpId != null && dpId.longValue() != 0) {
154             jobKey = dpId.toString() + ":";
155         }
156         jobKey = jobKey + portName;
157         return jobKey;
158     }
159 }