Replaced a couple of package imports with class imports
[controller.git] / opendaylight / md-sal / samples / l2switch / implementation / src / main / java / org / opendaylight / controller / sample / l2switch / md / inventory / InventoryService.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.sample.l2switch.md.inventory;
9
10 import org.opendaylight.controller.sample.l2switch.md.util.InstanceIdentifierUtils;
11 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 /**
31  * InventoryService provides functions related to Nodes & NodeConnectors.
32  */
33 public class InventoryService {
34   private DataBrokerService dataService;
35   // Key: SwitchId, Value: NodeConnectorRef that corresponds to NC between controller & switch
36   private HashMap<String, NodeConnectorRef> controllerSwitchConnectors;
37
38   /**
39    * Construct an InventoryService object with the specified inputs.
40    * @param dataService  The DataBrokerService associated with the InventoryService.
41    */
42   public InventoryService(DataBrokerService dataService) {
43     this.dataService = dataService;
44     controllerSwitchConnectors = new HashMap<String, NodeConnectorRef>();
45   }
46
47   public HashMap<String, NodeConnectorRef> getControllerSwitchConnectors() {
48     return controllerSwitchConnectors;
49   }
50
51   // ToDo: Improve performance for thousands of switch ports
52   /**
53    * Get the External NodeConnectors of the network, which are the NodeConnectors connected to hosts.
54    * @return  The list of external node connectors.
55    */
56   public List<NodeConnectorRef> getExternalNodeConnectors() {
57     // External NodeConnectors = All - Internal
58     ArrayList<NodeConnectorRef> externalNodeConnectors = new ArrayList<NodeConnectorRef>();
59     Set<String> internalNodeConnectors = new HashSet<>();
60
61     // Read Topology -- find list of switch-to-switch internal node connectors
62     NetworkTopology networkTopology =
63         (NetworkTopology)dataService.readOperationalData(
64             InstanceIdentifier.<NetworkTopology>builder(NetworkTopology.class).toInstance());
65
66     for (Topology topology : networkTopology.getTopology()) {
67       Topology completeTopology =
68           (Topology)dataService.readOperationalData(
69               InstanceIdentifierUtils.generateTopologyInstanceIdentifier(
70                   topology.getTopologyId().getValue()));
71
72       for (Link link : completeTopology.getLink()) {
73         internalNodeConnectors.add(link.getDestination().getDestTp().getValue());
74         internalNodeConnectors.add(link.getSource().getSourceTp().getValue());
75       }
76     }
77
78     // Read Inventory -- contains list of all nodeConnectors
79     InstanceIdentifier.InstanceIdentifierBuilder<Nodes> nodesInsIdBuilder = InstanceIdentifier.<Nodes>builder(Nodes.class);
80     Nodes nodes = (Nodes)dataService.readOperationalData(nodesInsIdBuilder.toInstance());
81     if (nodes != null) {
82       for (Node node : nodes.getNode()) {
83         Node completeNode = (Node)dataService.readOperationalData(InstanceIdentifierUtils.createNodePath(node.getId()));
84         for (NodeConnector nodeConnector : completeNode.getNodeConnector()) {
85           // NodeConnector isn't switch-to-switch, so it must be controller-to-switch (internal) or external
86           if (!internalNodeConnectors.contains(nodeConnector.getId().getValue())) {
87             NodeConnectorRef ncRef = new NodeConnectorRef(
88                     InstanceIdentifier.<Nodes>builder(Nodes.class).<Node, NodeKey>child(Node.class, node.getKey())
89                             .<NodeConnector, NodeConnectorKey>child(NodeConnector.class, nodeConnector.getKey()).toInstance());
90
91             // External node connectors have "-" in their name for mininet, i.e. "s1-eth1"
92             if (nodeConnector.getAugmentation(FlowCapableNodeConnector.class).getName().contains("-")) {
93               externalNodeConnectors.add(ncRef);
94             }
95             // Controller-to-switch internal node connectors
96             else {
97               controllerSwitchConnectors.put(node.getId().getValue(), ncRef);
98             }
99           }
100         }
101       }
102     }
103
104     return externalNodeConnectors;
105   }
106 }