InventoryReader implements DataTreeChangeListener<DataObject> { public NodeConnectorRef getNodeConnector(InstanceIdentifier<Node> nodeInsId, MacAddress macAddress) { if (nodeInsId == null || macAddress == null) { return null; } NodeConnectorRef destNodeConnector = null; long latest = -1; ReadOnlyTransaction readOnlyTransaction = dataService.newReadOnlyTransaction(); try { Optional<Node> dataObjectOptional = null; dataObjectOptional = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodeInsId).get(); if (dataObjectOptional.isPresent()) { Node node = dataObjectOptional.get(); LOG.debug("Looking address{} in node : {}", macAddress, nodeInsId); if (node.getNodeConnector() != null) { for (NodeConnector nc : node.getNodeConnector()) { StpStatusAwareNodeConnector saNodeConnector = nc .getAugmentation(StpStatusAwareNodeConnector.class); if (saNodeConnector != null && StpStatus.Discarding.equals(saNodeConnector.getStatus())) { continue; } LOG.debug("Looking address{} in nodeconnector : {}", macAddress, nc.getKey()); AddressCapableNodeConnector acnc = nc.getAugmentation(AddressCapableNodeConnector.class); if (acnc != null) { List<Addresses> addressesList = acnc.getAddresses(); for (Addresses add : addressesList) { if (macAddress.equals(add.getMac())) { if (add.getLastSeen() > latest) { destNodeConnector = new NodeConnectorRef( nodeInsId.child(NodeConnector.class, nc.getKey())); latest = add.getLastSeen(); LOG.debug("Found address{} in nodeconnector : {}", macAddress, nc.getKey()); break; } } } } } } else { LOG.debug("Node connectors data is not present for node {}", node.getId()); } } } catch (InterruptedException e) { LOG.error("Failed to read nodes from Operation data store."); readOnlyTransaction.close(); throw new RuntimeException("Failed to read nodes from Operation data store.", e); } catch (ExecutionException e) { LOG.error("Failed to read nodes from Operation data store."); readOnlyTransaction.close(); throw new RuntimeException("Failed to read nodes from Operation data store.", e); } readOnlyTransaction.close(); return destNodeConnector; } InventoryReader(DataBroker dataService); void setRefreshData(boolean refreshData); HashMap<String, NodeConnectorRef> getControllerSwitchConnectors(); HashMap<String, List<NodeConnectorRef>> getSwitchNodeConnectors(); @Override void onDataTreeChanged(Collection<DataTreeModification<DataObject>> changes); void close(); void readInventory(); NodeConnectorRef getNodeConnector(InstanceIdentifier<Node> nodeInsId, MacAddress macAddress);  }
@Test public void testGetNodeConnector() throws Exception { List<Addresses> addressesList = new ArrayList<Addresses>(); addressesList.add(new AddressesBuilder().setLastSeen(0L).setMac(new MacAddress("aa:bb:cc:dd:ee:ff")).build()); AddressCapableNodeConnector addressCapableNodeConnector = new AddressCapableNodeConnectorBuilder() .setAddresses(addressesList).build(); StpStatusAwareNodeConnector stpStatusAwareNodeConnector = new StpStatusAwareNodeConnectorBuilder() .setStatus(StpStatus.Forwarding).build(); NodeConnector nodeConnector = new NodeConnectorBuilder() .addAugmentation(StpStatusAwareNodeConnector.class, stpStatusAwareNodeConnector) .addAugmentation(AddressCapableNodeConnector.class, addressCapableNodeConnector).build(); List<NodeConnector> nodeConnectors = new ArrayList<NodeConnector>(); nodeConnectors.add(nodeConnector); Node node = new NodeBuilder().setNodeConnector(nodeConnectors).build(); Optional<Node> optionalNode = Optional.of(node); ReadOnlyTransaction readOnlyTransaction = Mockito.mock(ReadOnlyTransaction.class); CheckedFuture checkedFuture = Mockito.mock(CheckedFuture.class); when(checkedFuture.get()).thenReturn(optionalNode); when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))) .thenReturn(checkedFuture); when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); assertNotNull( inventoryReader .getNodeConnector( InstanceIdentifier.builder(Nodes.class) .child(Node.class, new NodeKey(new NodeId("openflow:1"))).build(), new MacAddress("aa:bb:cc:dd:ee:ff"))); verify(readOnlyTransaction, times(1)).close(); }
@Test public void testGetNodeConnector_NullNodeInsId() throws Exception { assertNull(inventoryReader.getNodeConnector(null, Mockito.mock(MacAddress.class))); verify(dataBroker, times(0)).newReadOnlyTransaction(); }
@Test public void testGetNodeConnector_NullMacAddress() throws Exception { assertNull(inventoryReader.getNodeConnector(Mockito.mock(InstanceIdentifier.class), null)); verify(dataBroker, times(0)).newReadOnlyTransaction(); }