4eb6735ec9cefda426016097e859210820857bb9
[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.Map;
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;
33
34 public final class ShellUtil {
35     private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
36
37     public static final String NODE_PREFIX = "openflow:";
38
39     private ShellUtil() {
40     }
41
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());
46             dpnList.add(dpn);
47             LOG.trace("Added OFNode: {} to the list", dpn.getNodeId());
48         }
49         Collections.sort(dpnList);
50         return dpnList;
51     }
52
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);
57             return null;
58         } else {
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);
63                 return null;
64             } else {
65                 for (String port : nodeInfo.getPorts()) {
66                     ports.add(port);
67                 }
68                 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
69             }
70         }
71     }
72
73     public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
74         OFNode ofNode = null;
75         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
76                 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
77
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();
82                 String name = null;
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();
88                 } else {
89                     LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
90                     return null;
91                 }
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());
98                         return null;
99                     } else {
100                         String portName = flowCapableNodeConnector.getName();
101                         portList.add(portName);
102                     }
103                 }
104                 ofNode = new OFNode(nodeId, name, portList);
105             } else {
106                 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
107                 return null;
108             }
109         } catch (ExecutionException | InterruptedException e) {
110             LOG.error("Error reading node {} from Inventory DS", nodeId, e);
111         }
112         return ofNode;
113     }
114
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();
124             }
125         } catch (ExecutionException | InterruptedException | NullPointerException e) {
126             LOG.error("Error reading reconciliation counter from datastore", e);
127         }
128         return output;
129     }
130 }