2 * Copyright (c) 2016 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.openflowplugin.applications.frsync.dao;
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 javax.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;
28 * Implementation of data access object for ODL {@link FlowCapableNode}.
30 public class FlowCapableNodeOdlDao implements FlowCapableNodeDao {
32 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableNodeOdlDao.class);
34 private static final InstanceIdentifier<Nodes> NODES_IID = InstanceIdentifier.create(Nodes.class);
35 private final DataBroker dataBroker;
36 private final LogicalDatastoreType logicalDatastoreType;
38 public FlowCapableNodeOdlDao(DataBroker dataBroker, LogicalDatastoreType logicalDatastoreType) {
39 this.dataBroker = dataBroker;
40 this.logicalDatastoreType = logicalDatastoreType;
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);
53 return Optional.empty();