2 * Copyright (c) 2016 CableLabs 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
9 package org.opendaylight.unimgr.utils;
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.binding.api.ReadTransaction;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.common.base.Optional;
25 import com.google.common.util.concurrent.CheckedFuture;
27 public class MdsalUtils {
29 private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
31 private MdsalUtils() {
32 throw new AssertionError("Instantiating utility class.");
36 * Read a specific datastore type and return a DataObject as a casted
38 * @param dataBroker The dataBroker instance to create transactions
39 * @param store The store type to query
40 * @param path The generic path to query
41 * @return The DataObject as a casted Object
43 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
44 DataBroker dataBroker,
45 final LogicalDatastoreType store,
46 final InstanceIdentifier<D> path) {
48 final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
49 Optional<D> optionalDataObject;
50 final CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
52 optionalDataObject = future.checkedGet();
53 if (optionalDataObject.isPresent()) {
54 result = optionalDataObject.get();
56 LOG.debug("{}: Failed to read {}",
57 Thread.currentThread().getStackTrace()[1], path);
59 } catch (final ReadFailedException e) {
60 LOG.warn("Failed to read {} ", path, e);
67 * Read a specific node from the Operational Data store by default.
68 * @param dataBroker The dataBroker instance to create transactions
69 * @param genericNode The Instance Identifier of the Node
70 * @return The Optional Node instance
72 public static final Optional<Node> readNode(DataBroker dataBroker,
73 InstanceIdentifier<?> genericNode) {
74 final ReadTransaction read = dataBroker.newReadOnlyTransaction();
75 final InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
76 final CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture =
77 read.read(LogicalDatastoreType.OPERATIONAL,
80 return nodeFuture.checkedGet();
81 } catch (final ReadFailedException e) {
82 LOG.info("Unable to read node with Iid {}", nodeIid);
84 return Optional.absent();
88 * Generic function to delete a node on a specific dataStore
89 * @param dataBroker The instance of the data broker to create transactions.
90 * @param genericNode The instance identifier of a generic node
91 * @param store The dataStore where to send and submit the delete call.
93 public static boolean deleteNode(DataBroker dataBroker,
94 InstanceIdentifier<?> genericNode,
95 LogicalDatastoreType store) {
96 LOG.info("Received a request to delete node {}", genericNode);
97 boolean result = false;
98 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
99 transaction.delete(store, genericNode);
101 transaction.submit().checkedGet();
103 } catch (final TransactionCommitFailedException e) {
104 LOG.error("Unable to remove node with Iid {} from store {}.", genericNode, store);
110 * Read a specific Link from a specific datastore
111 * @param dataBroker The dataBroker instance to create transactions
112 * @param store The datastore type.
113 * @param genericNode The Instance Identifier of the Link
114 * @return An Optional Link instance
116 public static final Optional<Link> readLink(DataBroker dataBroker,
117 LogicalDatastoreType store,
118 InstanceIdentifier<?> genericNode) {
119 final ReadTransaction read = dataBroker.newReadOnlyTransaction();
120 final InstanceIdentifier<Link> linkIid = genericNode.firstIdentifierOf(Link.class);
121 final CheckedFuture<Optional<Link>, ReadFailedException> linkFuture = read.read(store, linkIid);
123 return linkFuture.checkedGet();
124 } catch (final ReadFailedException e) {
125 LOG.info("Unable to read node with Iid {}", linkIid);
127 return Optional.absent();
131 * Read a specific node from a specific data store type.
132 * @param dataBroker The dataBroker instance to create transactions
133 * @param store The data store type
134 * @param genericNode The Instance Identifier of a specific Node
135 * @return An Optional Node instance
137 public static final Optional<Node> readNode(DataBroker dataBroker,
138 LogicalDatastoreType store,
139 InstanceIdentifier<?> genericNode) {
140 final ReadTransaction read = dataBroker.newReadOnlyTransaction();
141 final InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
142 final CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = read
143 .read(store, nodeIid);
145 return nodeFuture.checkedGet();
146 } catch (final ReadFailedException e) {
147 LOG.info("Unable to read node with Iid {}", nodeIid);
149 return Optional.absent();