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 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.reconciliation.service.rev180227.ReconciliationCounter;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
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 InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).build();
45 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
46 Optional<Nodes> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
47 if (result.isPresent()) {
48 nodes = result.get().getNode();
50 } catch (ExecutionException | InterruptedException | NullPointerException e) {
51 LOG.error("Error reading nodes from Inventory DS", e);
54 List<OFNode> nodeList = new ArrayList<>();
55 for (Node node : nodes) {
56 String[] nodeId = node.getId().getValue().split(":");
58 FlowCapableNode flowCapableNode = node.<FlowCapableNode>augmentation(FlowCapableNode.class);
59 if (flowCapableNode != null) {
60 name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
62 LOG.error("Error while converting OFNode: {} to FlowCapableNode", node.getId());
63 return Collections.emptyList();
65 OFNode ofNode = new OFNode(Long.parseLong(nodeId[1]), name);
66 LOG.trace("Added OFNode: {} to the list", ofNode.getNodeId());
69 Collections.sort(nodeList);
72 return Collections.emptyList();
75 public static OFNode getNode(final long nodeId, final DataBroker broker) {
76 OFNode nodeInfo = getNodeInfo(nodeId, broker);
77 if (nodeInfo == null) {
78 LOG.info("No ports exist for this node with nodeId {}", nodeId);
81 List<String> ports = new ArrayList<>();
82 // OFNode State is not provided by plugin, hence using null
83 if (nodeInfo.getPorts() == null) {
84 LOG.info("No ports exist for this node with nodeId {}", nodeId);
87 for (String port : nodeInfo.getPorts()) {
90 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
95 public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
97 InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
98 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
100 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
101 Optional<Node> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
102 if (result.isPresent()) {
103 Node node = result.get();
105 List<NodeConnector> nodeConnectors = null;
106 List<String> portList = new ArrayList<>();
107 FlowCapableNode flowCapableNode = node.<FlowCapableNode>augmentation(FlowCapableNode.class);
108 if (flowCapableNode != null) {
109 name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
111 LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
114 nodeConnectors = node.getNodeConnector();
115 for (NodeConnector nodeConnector : nodeConnectors) {
116 FlowCapableNodeConnector flowCapableNodeConnector =
117 nodeConnector.augmentation(FlowCapableNodeConnector.class);
118 if (flowCapableNodeConnector == null) {
119 LOG.error("Error for OFNode:{} while reading nodeConnectors", node.getId());
122 String portName = flowCapableNodeConnector.getName();
123 portList.add(portName);
126 ofNode = new OFNode(nodeId, name, portList);
128 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
131 } catch (ExecutionException | InterruptedException e) {
132 LOG.error("Error reading node {} from Inventory DS", nodeId, e);
137 public static List<ReconcileCounter> getReconcileCount(final DataBroker dataBroker) {
138 InstanceIdentifier<ReconciliationCounter> instanceIdentifier = InstanceIdentifier
139 .builder(ReconciliationCounter.class).build();
140 List<ReconcileCounter> output = Collections.emptyList();
141 try (ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
142 Optional<ReconciliationCounter> result =
143 tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
144 if (result.isPresent()) {
145 output = result.get().getReconcileCounter();
147 } catch (ExecutionException | InterruptedException | NullPointerException e) {
148 LOG.error("Error reading reconciliation counter from datastore", e);