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 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
45 InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).build();
47 Optional<Nodes> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
48 if (result.isPresent()) {
49 nodes = result.get().getNode();
51 } catch (ExecutionException | InterruptedException | NullPointerException e) {
52 LOG.error("Error reading nodes from Inventory DS", e);
55 List<OFNode> nodeList = new ArrayList<>();
56 for (Node node : nodes) {
57 String[] nodeId = node.getId().getValue().split(":");
59 FlowCapableNode flowCapableNode = node.<FlowCapableNode>augmentation(FlowCapableNode.class);
60 if (flowCapableNode != null) {
61 name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
63 LOG.error("Error while converting OFNode: {} to FlowCapableNode", node.getId());
64 return Collections.emptyList();
66 OFNode ofNode = new OFNode(Long.parseLong(nodeId[1]), name);
67 LOG.trace("Added OFNode: {} to the list", ofNode.getNodeId());
70 Collections.sort(nodeList);
73 return Collections.emptyList();
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);
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);
88 for (String port : nodeInfo.getPorts()) {
91 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
96 public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
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();
103 Optional<Node> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
104 if (result.isPresent()) {
105 Node node = result.get();
107 List<NodeConnector> nodeConnectors = null;
108 List<String> portList = new ArrayList<>();
109 FlowCapableNode flowCapableNode = node.<FlowCapableNode>augmentation(FlowCapableNode.class);
110 if (flowCapableNode != null) {
111 name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
113 LOG.error("Error while converting OFNode:{} to FlowCapableNode: {}", node.getId());
116 nodeConnectors = node.getNodeConnector();
117 for (NodeConnector nodeConnector : nodeConnectors) {
118 FlowCapableNodeConnector flowCapableNodeConnector =
119 nodeConnector.augmentation(FlowCapableNodeConnector.class);
120 if (flowCapableNodeConnector == null) {
121 LOG.error("Error for OFNode:{} while reading nodeConnectors {}", node.getId());
124 String portName = flowCapableNodeConnector.getName();
125 portList.add(portName);
128 ofNode = new OFNode(nodeId, name, portList);
130 LOG.error("OFNode with nodeId {} not present Inventory DS: {}", nodeId);
133 } catch (ExecutionException | InterruptedException e) {
134 LOG.error("Error reading node {} from Inventory DS: {}", nodeId, e);
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();
145 Optional<ReconciliationCounter> result =
146 tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
147 if (result.isPresent()) {
148 output = result.get().getReconcileCounter();
150 } catch (ExecutionException | InterruptedException | NullPointerException e) {
151 LOG.error("Error reading reconciliation counter from datastore", e);