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.l2switch.inventory;
10 import com.google.common.base.Optional;
11 import java.util.List;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.address.tracker.rev140617.AddressCapableNodeConnector;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.address.tracker.rev140617.address.node.connector.Addresses;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2switch.loopremover.rev140714.StpStatus;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2switch.loopremover.rev140714.StpStatusAwareNodeConnector;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * InventoryReader reads the opendaylight-inventory tree in MD-SAL data store.
31 public class InventoryReader {
33 private static final Logger LOG = LoggerFactory.getLogger(InventoryReader.class);
34 private DataBroker dataService;
37 * Construct an InventoryService object with the specified inputs.
40 * The DataBrokerService associated with the InventoryService.
42 public InventoryReader(DataBroker dataService) {
43 this.dataService = dataService;
47 * Get the NodeConnector on the specified node with the specified MacAddress
51 * InstanceIdentifier for the node on which to search for.
53 * MacAddress to be searched for.
54 * @return NodeConnectorRef that pertains to the NodeConnector containing
55 * the MacAddress observation.
57 public NodeConnectorRef getNodeConnector(InstanceIdentifier<Node> nodeInsId, MacAddress macAddress) {
58 if (nodeInsId == null || macAddress == null) {
62 NodeConnectorRef destNodeConnector = null;
64 ReadOnlyTransaction readOnlyTransaction = dataService.newReadOnlyTransaction();
66 Optional<Node> dataObjectOptional = null;
67 dataObjectOptional = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeInsId).get();
68 if (dataObjectOptional.isPresent()) {
69 Node node = (Node) dataObjectOptional.get();
70 LOG.debug("Looking address{} in node : {}", macAddress, nodeInsId);
71 for (NodeConnector nc : node.getNodeConnector()) {
72 // Don't look for mac in discarding node connectors
73 StpStatusAwareNodeConnector saNodeConnector = nc.getAugmentation(StpStatusAwareNodeConnector.class);
74 if (saNodeConnector != null && StpStatus.Discarding.equals(saNodeConnector.getStatus())) {
77 LOG.debug("Looking address{} in nodeconnector : {}", macAddress, nc.getKey());
78 AddressCapableNodeConnector acnc = nc.getAugmentation(AddressCapableNodeConnector.class);
80 List<Addresses> addressesList = acnc.getAddresses();
81 for (Addresses add : addressesList) {
82 if (macAddress.equals(add.getMac())) {
83 if (add.getLastSeen() > latest) {
84 destNodeConnector = new NodeConnectorRef(
85 nodeInsId.child(NodeConnector.class, nc.getKey()));
86 latest = add.getLastSeen();
87 LOG.debug("Found address{} in nodeconnector : {}", macAddress, nc.getKey());
95 } catch (InterruptedException e) {
96 LOG.error("Failed to read nodes from Operation data store.");
97 readOnlyTransaction.close();
98 throw new RuntimeException("Failed to read nodes from Operation data store.", e);
99 } catch (ExecutionException e) {
100 LOG.error("Failed to read nodes from Operation data store.");
101 readOnlyTransaction.close();
102 throw new RuntimeException("Failed to read nodes from Operation data store.", e);
104 readOnlyTransaction.close();
105 return destNodeConnector;