Update to work on Sodium SR1
[l2switch.git] / l2switch-main / src / main / java / org / opendaylight / l2switch / 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.l2switch.inventory;
9
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;
27
28 /**
29  * InventoryReader reads the opendaylight-inventory tree in MD-SAL data store.
30  */
31 public class InventoryReader {
32
33     private static final Logger LOG = LoggerFactory.getLogger(InventoryReader.class);
34     private DataBroker dataService;
35
36     /**
37      * Construct an InventoryService object with the specified inputs.
38      *
39      * @param dataService
40      *            The DataBrokerService associated with the InventoryService.
41      */
42     public InventoryReader(DataBroker dataService) {
43         this.dataService = dataService;
44     }
45
46     /**
47      * Get the NodeConnector on the specified node with the specified MacAddress
48      * observation.
49      *
50      * @param nodeInsId
51      *            InstanceIdentifier for the node on which to search for.
52      * @param macAddress
53      *            MacAddress to be searched for.
54      * @return NodeConnectorRef that pertains to the NodeConnector containing
55      *         the MacAddress observation.
56      */
57     public NodeConnectorRef getNodeConnector(InstanceIdentifier<Node> nodeInsId, MacAddress macAddress) {
58         if (nodeInsId == null || macAddress == null) {
59             return null;
60         }
61
62         NodeConnectorRef destNodeConnector = null;
63         long latest = -1;
64         ReadOnlyTransaction readOnlyTransaction = dataService.newReadOnlyTransaction();
65         try {
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.augmentation(StpStatusAwareNodeConnector.class);
74                     if (saNodeConnector != null && StpStatus.Discarding.equals(saNodeConnector.getStatus())) {
75                         continue;
76                     }
77                     LOG.debug("Looking address{} in nodeconnector : {}", macAddress, nc.key());
78                     AddressCapableNodeConnector acnc = nc.augmentation(AddressCapableNodeConnector.class);
79                     if (acnc != null) {
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.key()));
86                                     latest = add.getLastSeen();
87                                     LOG.debug("Found address{} in nodeconnector : {}", macAddress, nc.key());
88                                     break;
89                                 }
90                             }
91                         }
92                     }
93                 }
94             }
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);
103         }
104         readOnlyTransaction.close();
105         return destNodeConnector;
106     }
107
108 }