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