OPNFLWPLUG-898 : remove deprecated checkedfuture
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / util / ShellUtil.java
1 /*
2  * Copyright (c) 2017 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.openflowplugin.applications.southboundcli.util;
10
11 import com.google.common.base.Optional;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.concurrent.ExecutionException;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
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.params.xml.ns.yang.openflowplugin.app.admin.reconciliation.service.rev180227.ReconciliationCounter;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.admin.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public final class ShellUtil {
34     private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
35
36     public static final String NODE_PREFIX = "openflow:";
37
38     private ShellUtil() {
39     }
40
41     @Nonnull
42     public static List<OFNode> getAllNodes(final DataBroker broker) {
43         List<Node> nodes = null;
44         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
45         InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).build();
46         try {
47             Optional<Nodes> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
48             if (result.isPresent()) {
49                 nodes = result.get().getNode();
50             }
51         } catch (ExecutionException | InterruptedException | NullPointerException e) {
52             LOG.error("Error reading nodes from Inventory DS", e);
53         }
54         if (nodes != null) {
55             List<OFNode> nodeList = new ArrayList<>();
56             for (Node node : nodes) {
57                 String[] nodeId = node.getId().getValue().split(":");
58                 String name = null;
59                 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
60                 if (flowCapableNode != null) {
61                     name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
62                 } else {
63                     LOG.error("Error while converting OFNode: {} to FlowCapableNode", node.getId());
64                     return Collections.emptyList();
65                 }
66                 OFNode ofNode = new OFNode(Long.parseLong(nodeId[1]), name);
67                 LOG.trace("Added OFNode: {} to the list", ofNode.getNodeId());
68                 nodeList.add(ofNode);
69             }
70             Collections.sort(nodeList);
71             return nodeList;
72         }
73         return Collections.emptyList();
74     }
75
76     public static OFNode getNode(final long nodeId, final DataBroker broker) {
77         OFNode nodeInfo = getNodeInfo(nodeId, broker);
78         if (nodeInfo == null) {
79             LOG.info("No ports exist for this node with nodeId {}", nodeId);
80             return null;
81         } else {
82             List<String> ports = new ArrayList<>();
83             // OFNode State is not provided by plugin, hence using null
84             if (nodeInfo.getPorts() == null) {
85                 LOG.info("No ports exist for this node with nodeId {}", nodeId);
86                 return null;
87             } else {
88                 for (String port : nodeInfo.getPorts()) {
89                     ports.add(port);
90                 }
91                 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
92             }
93         }
94     }
95
96     public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
97         OFNode ofNode = null;
98         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
99         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
100                 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
101
102         try {
103             Optional<Node> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
104             if (result.isPresent()) {
105                 Node node = result.get();
106                 String name = null;
107                 List<NodeConnector> nodeConnectors = null;
108                 List<String> portList = new ArrayList<>();
109                 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
110                 if (flowCapableNode != null) {
111                     name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
112                 } else {
113                     LOG.error("Error while converting OFNode:{} to FlowCapableNode: {}", node.getId());
114                     return null;
115                 }
116                 nodeConnectors = node.getNodeConnector();
117                 for (NodeConnector nodeConnector : nodeConnectors) {
118                     FlowCapableNodeConnector flowCapableNodeConnector =
119                             nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
120                     if (flowCapableNodeConnector == null) {
121                         LOG.error("Error for OFNode:{} while reading nodeConnectors {}", node.getId());
122                         return null;
123                     } else {
124                         String portName = flowCapableNodeConnector.getName();
125                         portList.add(portName);
126                     }
127                 }
128                 ofNode = new OFNode(nodeId, name, portList);
129             } else {
130                 LOG.error("OFNode with nodeId {} not present Inventory DS: {}", nodeId);
131                 return null;
132             }
133         } catch (ExecutionException | InterruptedException e) {
134             LOG.error("Error reading node {} from Inventory DS: {}", nodeId, e);
135         }
136         return ofNode;
137     }
138
139     public static List<ReconcileCounter> getReconcileCount(final DataBroker dataBroker) {
140         ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();
141         InstanceIdentifier<ReconciliationCounter> instanceIdentifier = InstanceIdentifier
142                 .builder(ReconciliationCounter.class).build();
143         List<ReconcileCounter> output = Collections.emptyList();
144         try {
145             Optional<ReconciliationCounter> result =
146                     tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
147             if (result.isPresent()) {
148                 output = result.get().getReconcileCounter();
149             }
150         } catch (ExecutionException | InterruptedException | NullPointerException e) {
151             LOG.error("Error reading reconciliation counter from datastore", e);
152         }
153         return output;
154     }
155 }