2 * Copyright (c) 2017 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.openflowplugin.applications.southboundcli.util;
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 javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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;
33 public final class ShellUtil {
34 private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
36 public static final String NODE_PREFIX = "openflow:";
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();
47 CheckedFuture<Optional<Nodes>, ReadFailedException> checkedFuture =
48 tx.read(LogicalDatastoreType.OPERATIONAL, path);
49 Optional<Nodes> result = checkedFuture.get();
50 if (result.isPresent()) {
51 nodes = result.get().getNode();
53 } catch (ExecutionException | InterruptedException | NullPointerException e) {
54 LOG.error("Error reading nodes from Inventory DS", e);
57 List<OFNode> nodeList = new ArrayList<>();
58 for (Node node : nodes) {
59 String[] nodeId = node.getId().getValue().split(":");
61 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
62 if (flowCapableNode != null) {
63 name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
65 LOG.error("Error while converting OFNode: {} to FlowCapableNode", node.getId());
66 return Collections.emptyList();
68 OFNode ofNode = new OFNode(Long.parseLong(nodeId[1]), name);
69 LOG.trace("Added OFNode: {} to the list", ofNode.getNodeId());
72 Collections.sort(nodeList);
75 return Collections.emptyList();
78 public static OFNode getNode(final long nodeId, final DataBroker broker) {
79 OFNode nodeInfo = getNodeInfo(nodeId, broker);
80 if (nodeInfo == null) {
81 LOG.info("No ports exist for this node with nodeId {}", nodeId);
84 List<String> ports = new ArrayList<>();
85 // OFNode State is not provided by plugin, hence using null
86 if (nodeInfo.getPorts() == null) {
87 LOG.info("No ports exist for this node with nodeId {}", nodeId);
90 for (String port : nodeInfo.getPorts()) {
93 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
98 public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
100 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
101 InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
102 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
103 Optional<Node> result;
105 CheckedFuture<Optional<Node>, ReadFailedException> checkedFuture =
106 tx.read(LogicalDatastoreType.OPERATIONAL, path);
107 result = checkedFuture.get();
108 if (result.isPresent()) {
109 Node node = result.get();
111 List<NodeConnector> nodeConnectors = null;
112 List<String> portList = new ArrayList<>();
113 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
114 if (flowCapableNode != null) {
115 name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
117 LOG.error("Error while converting OFNode:{} to FlowCapableNode: {}", node.getId());
120 nodeConnectors = node.getNodeConnector();
121 for (NodeConnector nodeConnector : nodeConnectors) {
122 FlowCapableNodeConnector flowCapableNodeConnector =
123 nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
124 if (flowCapableNodeConnector == null) {
125 LOG.error("Error for OFNode:{} while reading nodeConnectors {}", node.getId());
128 String portName = flowCapableNodeConnector.getName();
129 portList.add(portName);
132 ofNode = new OFNode(nodeId, name, portList);
134 LOG.error("OFNode with nodeId {} not present Inventory DS: {}", nodeId);
137 } catch (ExecutionException | InterruptedException e) {
138 LOG.error("Error reading node {} from Inventory DS: {}", nodeId, e);