BUG-1089: migrate InventoryReader
[controller.git] / opendaylight / md-sal / compatibility / inventory-topology-compatibility / src / main / java / org / opendaylight / controller / md / compatibility / inventory / InventoryReader.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.controller.md.compatibility.inventory;
9
10 import java.util.ArrayList;
11 import java.util.Set;
12
13 import org.opendaylight.controller.sal.binding.api.data.RuntimeDataProvider;
14 import org.opendaylight.controller.sal.compatibility.InventoryMapping;
15 import org.opendaylight.controller.switchmanager.ISwitchManager;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class InventoryReader implements RuntimeDataProvider {
28     private static final Logger LOG = LoggerFactory.getLogger(InventoryReader.class);
29     private ISwitchManager switchManager;
30
31     public ISwitchManager getSwitchManager() {
32         return switchManager;
33     }
34
35     public void setSwitchManager(final ISwitchManager switchManager) {
36         this.switchManager = switchManager;
37     }
38
39     @Override
40     public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
41         // Topology and Inventory are operational only
42         return null;
43     }
44
45     @SuppressWarnings("unchecked")
46     @Override
47     public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
48         final Class<? extends DataObject> type = path.getTargetType();
49         if (Nodes.class.equals(type)) {
50             return readNodes(((InstanceIdentifier<Nodes>) path));
51         }
52         if (Node.class.equals(type)) {
53             return readNode(((InstanceIdentifier<Node>) path));
54         }
55         if (NodeConnector.class.equals(type)) {
56             return readNodeConnector(((InstanceIdentifier<NodeConnector>) path));
57         }
58
59         LOG.debug("Unsupported type {}", type);
60         return null;
61     }
62
63     private NodeConnector readNodeConnector(final InstanceIdentifier<NodeConnector> identifier) {
64         return constructNodeConnector(InventoryMapping.toAdNodeConnector(identifier));
65     }
66
67     private Node readNode(final InstanceIdentifier<Node> identifier) {
68         return constructNode(InventoryMapping.toAdNode(identifier));
69     }
70
71     private Node constructNode(final org.opendaylight.controller.sal.core.Node node) {
72         final Set<org.opendaylight.controller.sal.core.NodeConnector> connectors = getSwitchManager().getNodeConnectors(node);
73         final ArrayList<NodeConnector> tpList = new ArrayList<NodeConnector>(connectors.size());
74         for (final org.opendaylight.controller.sal.core.NodeConnector connector : connectors) {
75             tpList.add(constructNodeConnector(connector));
76         }
77
78         return new NodeBuilder()
79         .setKey(InventoryMapping.toNodeKey(node))
80         .setNodeConnector(tpList)
81         .build();
82     }
83
84     private Nodes readNodes(final InstanceIdentifier<Nodes> identifier) {
85         final Set<org.opendaylight.controller.sal.core.Node> nodes = getSwitchManager().getNodes();
86         final ArrayList<Node> nodeList = new ArrayList<Node>(nodes.size());
87         for (final org.opendaylight.controller.sal.core.Node node : nodes) {
88             nodeList.add(constructNode(node));
89         }
90
91         return new NodesBuilder().setNode(nodeList).build();
92     }
93
94     private static NodeConnector constructNodeConnector(final org.opendaylight.controller.sal.core.NodeConnector connector) {
95         return new NodeConnectorBuilder().setKey(InventoryMapping.toNodeConnectorKey(connector)).build();
96     }
97 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.