Fix findbugs violations in applications
[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 com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.concurrent.ExecutionException;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
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         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
45         InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).build();
46         try {
47             CheckedFuture<Optional<Nodes>, ReadFailedException> checkedFuture =
48                     tx.read(LogicalDatastoreType.OPERATIONAL, path);
49             Optional<Nodes> result = checkedFuture.get();
50             if (result.isPresent()) {
51                 nodes = result.get().getNode();
52             }
53         } catch (ExecutionException | InterruptedException | NullPointerException e) {
54             LOG.error("Error reading nodes from Inventory DS", e);
55         }
56         if (nodes != null) {
57             List<OFNode> nodeList = new ArrayList<>();
58             for (Node node : nodes) {
59                 String[] nodeId = node.getId().getValue().split(":");
60                 String name = null;
61                 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
62                 if (flowCapableNode != null) {
63                     name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
64                 } else {
65                     LOG.error("Error while converting OFNode: {} to FlowCapableNode", node.getId());
66                     return Collections.emptyList();
67                 }
68                 OFNode ofNode = new OFNode(Long.parseLong(nodeId[1]), name);
69                 LOG.trace("Added OFNode: {} to the list", ofNode.getNodeId());
70                 nodeList.add(ofNode);
71             }
72             Collections.sort(nodeList);
73             return nodeList;
74         }
75         return Collections.emptyList();
76     }
77
78     public static OFNode getNode(final long nodeId, final DataBroker broker) {
79         OFNode nodeInfo = getNodeInfo(nodeId, broker);
80         if (nodeInfo == null) {
81             LOG.info("No ports exist for this node with nodeId {}", nodeId);
82             return null;
83         } else {
84             List<String> ports = new ArrayList<>();
85             // OFNode State is not provided by plugin, hence using null
86             if (nodeInfo.getPorts() == null) {
87                 LOG.info("No ports exist for this node with nodeId {}", nodeId);
88                 return null;
89             } else {
90                 for (String port : nodeInfo.getPorts()) {
91                     ports.add(port);
92                 }
93                 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
94             }
95         }
96     }
97
98     public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
99         OFNode ofNode = null;
100         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
101         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
102                 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
103         Optional<Node> result;
104         try {
105             CheckedFuture<Optional<Node>, ReadFailedException> checkedFuture =
106                     tx.read(LogicalDatastoreType.OPERATIONAL, path);
107             result = checkedFuture.get();
108             if (result.isPresent()) {
109                 Node node = result.get();
110                 String name = null;
111                 List<NodeConnector> nodeConnectors = null;
112                 List<String> portList = new ArrayList<>();
113                 FlowCapableNode flowCapableNode = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class);
114                 if (flowCapableNode != null) {
115                     name = node.<FlowCapableNode>getAugmentation(FlowCapableNode.class).getDescription();
116                 } else {
117                     LOG.error("Error while converting OFNode:{} to FlowCapableNode: {}", node.getId());
118                     return null;
119                 }
120                 nodeConnectors = node.getNodeConnector();
121                 for (NodeConnector nodeConnector : nodeConnectors) {
122                     FlowCapableNodeConnector flowCapableNodeConnector =
123                             nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
124                     if (flowCapableNodeConnector == null) {
125                         LOG.error("Error for OFNode:{} while reading nodeConnectors {}", node.getId());
126                         return null;
127                     } else {
128                         String portName = flowCapableNodeConnector.getName();
129                         portList.add(portName);
130                     }
131                 }
132                 ofNode = new OFNode(nodeId, name, portList);
133             } else {
134                 LOG.error("OFNode with nodeId {} not present Inventory DS: {}", nodeId);
135                 return null;
136             }
137         } catch (ExecutionException | InterruptedException e) {
138             LOG.error("Error reading node {} from Inventory DS: {}", nodeId, e);
139         }
140         return ofNode;
141     }
142 }