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 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;
33 public final class ShellUtil {
34 private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
36 public static final String NODE_PREFIX = "openflow:";
41 public static List<OFNode> getAllNodes(final DataBroker broker) {
42 List<Node> nodes = null;
43 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
44 InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).build();
46 CheckedFuture<Optional<Nodes>, ReadFailedException> checkedFuture =
47 tx.read(LogicalDatastoreType.OPERATIONAL, path);
48 Optional<Nodes> result = checkedFuture.get();
49 if (result.isPresent()) {
50 nodes = result.get().getNode();
52 } catch (ExecutionException | InterruptedException | NullPointerException e) {
53 LOG.error("Error reading nodes from Inventory DS", e);
56 List<OFNode> nodeList = new ArrayList<>();
57 for (Node node : nodes) {
58 String[] nodeId = node.getId().getValue().split(":");
60 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
61 if (flowCapableNode != null) {
62 name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
64 LOG.error("Error while converting OFNode: {} to FlowCapableNode", node.getId());
67 OFNode ofNode = new OFNode(Long.parseLong(nodeId[1]), name);
69 LOG.trace("Added OFNode: {} to the list", ofNode.getNodeId());
73 Collections.sort(nodeList);
76 return Collections.emptyList();
79 public static OFNode getNode(final long nodeId, final DataBroker broker) {
80 OFNode nodeInfo = getNodeInfo(nodeId, broker);
81 if (nodeInfo == null) {
82 LOG.info("No ports exist for this node with nodeId {}", nodeId);
85 List<String> ports = new ArrayList<>();
86 // OFNode State is not provided by plugin, hence using null
87 if (nodeInfo.getPorts() == null) {
88 LOG.info("No ports exist for this node with nodeId {}", nodeId);
91 for (String port : nodeInfo.getPorts()) {
94 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
99 public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
100 OFNode ofNode = null;
101 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
102 InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
103 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
104 Optional<Node> result;
106 CheckedFuture<Optional<Node>, ReadFailedException> checkedFuture =
107 tx.read(LogicalDatastoreType.OPERATIONAL, path);
108 result = checkedFuture.get();
109 if (result.isPresent()) {
110 Node node = result.get();
112 List<NodeConnector> nodeConnectors = null;
113 List<String> portList = new ArrayList<>();
114 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
115 if (flowCapableNode != null) {
116 name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
118 LOG.error("Error while converting OFNode:{} to FlowCapableNode: {}", node.getId());
121 nodeConnectors = node.getNodeConnector();
122 for (NodeConnector nodeConnector : nodeConnectors) {
123 FlowCapableNodeConnector flowCapableNodeConnector =
124 nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
125 if (flowCapableNodeConnector == null) {
126 LOG.error("Error for OFNode:{} while reading nodeConnectors {}", node.getId());
129 MacAddress hardwareAddress = flowCapableNodeConnector.getHardwareAddress();
130 String portName = flowCapableNodeConnector.getName();
131 portList.add(portName);
134 ofNode = new OFNode(nodeId, name, portList);
136 LOG.error("OFNode with nodeId {} not present Inventory DS: {}", nodeId);
139 } catch (ExecutionException | InterruptedException e) {
140 LOG.error("Error reading node {} from Inventory DS: {}", nodeId, e);