Upgrade ietf-{inet,yang}-types to 2013-07-15
[genius.git] / alivenessmonitor / alivenessmonitor-impl / src / main / java / org / opendaylight / genius / alivenessmonitor / internal / InventoryReader.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.genius.alivenessmonitor.internal;
9
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 class InventoryReader {
23     private Logger LOG = LoggerFactory.getLogger(InventoryReader.class);
24     private DataBroker dataService;
25
26     public InventoryReader(DataBroker dataService) {
27         this.dataService = dataService;
28     }
29
30     public String getMacAddress(InstanceIdentifier<NodeConnector> nodeConnectorId)  {
31         //TODO: Use mdsal apis to read
32         Optional<NodeConnector> optNc = read(LogicalDatastoreType.OPERATIONAL, nodeConnectorId);
33         if(optNc.isPresent()) {
34             NodeConnector nc = optNc.get();
35             FlowCapableNodeConnector fcnc = nc.getAugmentation(FlowCapableNodeConnector.class);
36             MacAddress macAddress = fcnc.getHardwareAddress();
37             return macAddress.getValue();
38         }
39         return null;
40     }
41
42     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
43                                                     InstanceIdentifier<T> path) {
44
45         ReadOnlyTransaction tx = dataService.newReadOnlyTransaction();
46
47         Optional<T> result = Optional.absent();
48         try {
49             result = tx.read(datastoreType, path).get();
50         } catch (Exception e) {
51             throw new RuntimeException(e);
52         }
53         tx.close();
54
55         return result;
56     }
57 }