2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sample.l2switch.md.inventory;
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;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
31 * InventoryService provides functions related to Nodes & NodeConnectors.
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;
39 * Construct an InventoryService object with the specified inputs.
40 * @param dataService The DataBrokerService associated with the InventoryService.
42 public InventoryService(DataBrokerService dataService) {
43 this.dataService = dataService;
44 controllerSwitchConnectors = new HashMap<String, NodeConnectorRef>();
47 public HashMap<String, NodeConnectorRef> getControllerSwitchConnectors() {
48 return controllerSwitchConnectors;
51 // ToDo: Improve performance for thousands of switch ports
53 * Get the External NodeConnectors of the network, which are the NodeConnectors connected to hosts.
54 * @return The list of external node connectors.
56 public List<NodeConnectorRef> getExternalNodeConnectors() {
57 // External NodeConnectors = All - Internal
58 ArrayList<NodeConnectorRef> externalNodeConnectors = new ArrayList<NodeConnectorRef>();
59 Set<String> internalNodeConnectors = new HashSet<>();
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());
66 for (Topology topology : networkTopology.getTopology()) {
67 Topology completeTopology =
68 (Topology)dataService.readOperationalData(
69 InstanceIdentifierUtils.generateTopologyInstanceIdentifier(
70 topology.getTopologyId().getValue()));
72 for (Link link : completeTopology.getLink()) {
73 internalNodeConnectors.add(link.getDestination().getDestTp().getValue());
74 internalNodeConnectors.add(link.getSource().getSourceTp().getValue());
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());
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());
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);
95 // Controller-to-switch internal node connectors
97 controllerSwitchConnectors.put(node.getId().getValue(), ncRef);
104 return externalNodeConnectors;