Merge "Eliminate {infra,service}utils.version"
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / util / ShellUtil.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.applications.southboundcli.util;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import org.eclipse.jdt.annotation.NonNull;
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.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;
32
33 public final class ShellUtil {
34     private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
35
36     public static final String NODE_PREFIX = "openflow:";
37
38     private ShellUtil() {
39     }
40
41     @NonNull
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 (ReadTransaction tx = broker.newReadOnlyTransaction()) {
46             Optional<Nodes> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
47             if (result.isPresent()) {
48                 nodes = result.get().getNode();
49             }
50         } catch (ExecutionException | InterruptedException | NullPointerException e) {
51             LOG.error("Error reading nodes from Inventory DS", e);
52         }
53         if (nodes != null) {
54             List<OFNode> nodeList = new ArrayList<>();
55             for (Node node : nodes) {
56                 String[] nodeId = node.getId().getValue().split(":");
57                 String name = null;
58                 FlowCapableNode flowCapableNode = node.<FlowCapableNode>augmentation(FlowCapableNode.class);
59                 if (flowCapableNode != null) {
60                     name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
61                 } else {
62                     LOG.error("Error while converting OFNode: {} to FlowCapableNode", node.getId());
63                     return Collections.emptyList();
64                 }
65                 OFNode ofNode = new OFNode(Long.parseLong(nodeId[1]), name);
66                 LOG.trace("Added OFNode: {} to the list", ofNode.getNodeId());
67                 nodeList.add(ofNode);
68             }
69             Collections.sort(nodeList);
70             return nodeList;
71         }
72         return Collections.emptyList();
73     }
74
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);
79             return null;
80         } else {
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);
85                 return null;
86             } else {
87                 for (String port : nodeInfo.getPorts()) {
88                     ports.add(port);
89                 }
90                 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
91             }
92         }
93     }
94
95     public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
96         OFNode ofNode = null;
97         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
98                 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
99
100         try (ReadTransaction tx = broker.newReadOnlyTransaction()) {
101             Optional<Node> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
102             if (result.isPresent()) {
103                 Node node = result.get();
104                 String name = null;
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();
110                 } else {
111                     LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
112                     return null;
113                 }
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());
120                         return null;
121                     } else {
122                         String portName = flowCapableNodeConnector.getName();
123                         portList.add(portName);
124                     }
125                 }
126                 ofNode = new OFNode(nodeId, name, portList);
127             } else {
128                 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
129                 return null;
130             }
131         } catch (ExecutionException | InterruptedException e) {
132             LOG.error("Error reading node {} from Inventory DS", nodeId, e);
133         }
134         return ofNode;
135     }
136
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 (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
142             Optional<ReconciliationCounter> result =
143                     tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
144             if (result.isPresent()) {
145                 output = result.get().getReconcileCounter();
146             }
147         } catch (ExecutionException | InterruptedException | NullPointerException e) {
148             LOG.error("Error reading reconciliation counter from datastore", e);
149         }
150         return output;
151     }
152 }