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