OPNFLWPLUG-1071 : Removal of javax.annotation.Nonnnull and replacement of javax.annot...
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / dao / FlowCapableNodeOdlDao.java
1 /*
2  * Copyright (c) 2016 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.openflowplugin.applications.frsync.dao;
9
10 import java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.TimeoutException;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.ReadTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Implementation of data access object for ODL {@link FlowCapableNode}.
29  */
30 public class FlowCapableNodeOdlDao implements FlowCapableNodeDao {
31
32     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableNodeOdlDao.class);
33
34     private static final InstanceIdentifier<Nodes> NODES_IID = InstanceIdentifier.create(Nodes.class);
35     private final DataBroker dataBroker;
36     private final LogicalDatastoreType logicalDatastoreType;
37
38     public FlowCapableNodeOdlDao(DataBroker dataBroker, LogicalDatastoreType logicalDatastoreType) {
39         this.dataBroker = dataBroker;
40         this.logicalDatastoreType = logicalDatastoreType;
41     }
42
43     @Override
44     public Optional<FlowCapableNode> loadByNodeId(@NonNull NodeId nodeId) {
45         try (ReadTransaction roTx = dataBroker.newReadOnlyTransaction()) {
46             final InstanceIdentifier<FlowCapableNode> path =
47                     NODES_IID.child(Node.class, new NodeKey(nodeId)).augmentation(FlowCapableNode.class);
48             return roTx.read(logicalDatastoreType, path).get(5000, TimeUnit.MILLISECONDS);
49         } catch (TimeoutException | InterruptedException | ExecutionException e) {
50             LOG.error("error reading {}", nodeId.getValue(), e);
51         }
52
53         return Optional.empty();
54     }
55
56 }