Cli to display all the connected DPNs
[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 com.google.common.util.concurrent.CheckedFuture;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.concurrent.ExecutionException;
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.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class ShellUtil {
34     private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
35
36     public static final String NODE_PREFIX = "openflow:";
37
38     public static List<OFNode> getAllNodes(final DataBroker broker) {
39         List<Node> nodes = null;
40         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
41         InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).build();
42         try {
43             CheckedFuture<Optional<Nodes>, ReadFailedException> checkedFuture =
44                     tx.read(LogicalDatastoreType.OPERATIONAL, path);
45             Optional<Nodes> result = checkedFuture.get();
46             if (result.isPresent()) {
47                 nodes = result.get().getNode();
48             }
49         } catch (ExecutionException | InterruptedException | NullPointerException e) {
50             LOG.error("Error reading nodes from Inventory DS", e);
51         }
52         if (nodes != null) {
53             List<OFNode> nodeList = new ArrayList<>();
54             for (Node node : nodes) {
55                 String[] nodeId = node.getId().getValue().split(":");
56                 String name = null;
57                 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
58                 if (flowCapableNode != null) {
59                     name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
60                 } else {
61                     LOG.error("Error while converting OFNode: {} to FlowCapableNode", node.getId());
62                     return null;
63                 }
64                 OFNode ofNode = new OFNode(Long.parseLong(nodeId[1]), name);
65                 if (ofNode != null) {
66                     LOG.trace("Added OFNode: {} to the list", ofNode.getNodeId());
67                     nodeList.add(ofNode);
68                 }
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         Optional<Node> result;
102         try {
103             CheckedFuture<Optional<Node>, ReadFailedException> checkedFuture =
104                     tx.read(LogicalDatastoreType.OPERATIONAL, path);
105             result = checkedFuture.get();
106             if (result.isPresent()) {
107                 Node node = result.get();
108                 String name = null;
109                 List<NodeConnector> nodeConnectors = null;
110                 List<String> portList = new ArrayList<>();
111                 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
112                 if (flowCapableNode != null) {
113                     name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
114                 } else {
115                     LOG.error("Error while converting OFNode:{} to FlowCapableNode: {}", node.getId());
116                     return null;
117                 }
118                 nodeConnectors = node.getNodeConnector();
119                 for (NodeConnector nodeConnector : nodeConnectors) {
120                     FlowCapableNodeConnector flowCapableNodeConnector =
121                             nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
122                     if (flowCapableNodeConnector == null) {
123                         LOG.error("Error for OFNode:{} while reading nodeConnectors {}", node.getId());
124                         return null;
125                     } else {
126                         MacAddress hardwareAddress = flowCapableNodeConnector.getHardwareAddress();
127                         String portName = flowCapableNodeConnector.getName();
128                         portList.add(portName);
129                     }
130                 }
131                 ofNode = new OFNode(nodeId, name, portList);
132             } else {
133                 LOG.error("OFNode with nodeId {} not present Inventory DS: {}", nodeId);
134                 return null;
135             }
136         } catch (ExecutionException | InterruptedException e) {
137             LOG.error("Error reading node {} from Inventory DS: {}", nodeId, e);
138         }
139         return ofNode;
140     }
141 }