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 java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.ReadTransaction;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.openflowplugin.applications.southboundcli.NodeListener;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationCounter;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public final class ShellUtil {
35 private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
37 public static final String NODE_PREFIX = "openflow:";
42 public static List<OFNode> getAllNodes(final NodeListener nodeListener) {
43 List<OFNode> dpnList = new ArrayList<>();
44 for (Map.Entry<Long, String> entry : nodeListener.getDpnIdToNameCache().entrySet()) {
45 OFNode dpn = new OFNode(entry.getKey(), entry.getValue());
47 LOG.trace("Added OFNode: {} to the list", dpn.getNodeId());
49 Collections.sort(dpnList);
53 public static OFNode getNode(final long nodeId, final DataBroker broker) {
54 OFNode nodeInfo = getNodeInfo(nodeId, broker);
55 if (nodeInfo == null) {
56 LOG.info("No ports exist for this node with nodeId {}", nodeId);
59 List<String> ports = new ArrayList<>();
60 // OFNode State is not provided by plugin, hence using null
61 if (nodeInfo.getPorts() == null) {
62 LOG.info("No ports exist for this node with nodeId {}", nodeId);
65 for (String port : nodeInfo.getPorts()) {
68 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
73 public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
75 InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
76 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
78 try (ReadTransaction tx = broker.newReadOnlyTransaction()) {
79 Optional<Node> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
80 if (result.isPresent()) {
81 Node node = result.get();
83 List<NodeConnector> nodeConnectors = null;
84 List<String> portList = new ArrayList<>();
85 FlowCapableNode flowCapableNode = node.<FlowCapableNode>augmentation(FlowCapableNode.class);
86 if (flowCapableNode != null) {
87 name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
89 LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
92 nodeConnectors = node.getNodeConnector();
93 for (NodeConnector nodeConnector : nodeConnectors) {
94 FlowCapableNodeConnector flowCapableNodeConnector =
95 nodeConnector.augmentation(FlowCapableNodeConnector.class);
96 if (flowCapableNodeConnector == null) {
97 LOG.error("Error for OFNode:{} while reading nodeConnectors", node.getId());
100 String portName = flowCapableNodeConnector.getName();
101 portList.add(portName);
104 ofNode = new OFNode(nodeId, name, portList);
106 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
109 } catch (ExecutionException | InterruptedException e) {
110 LOG.error("Error reading node {} from Inventory DS", nodeId, e);
115 public static List<ReconcileCounter> getReconcileCount(final DataBroker dataBroker) {
116 InstanceIdentifier<ReconciliationCounter> instanceIdentifier = InstanceIdentifier
117 .builder(ReconciliationCounter.class).build();
118 List<ReconcileCounter> output = Collections.emptyList();
119 try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
120 Optional<ReconciliationCounter> result =
121 tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
122 if (result.isPresent()) {
123 output = result.get().getReconcileCounter();
125 } catch (ExecutionException | InterruptedException | NullPointerException e) {
126 LOG.error("Error reading reconciliation counter from datastore", e);