23595cb58c200dba6a51e22fc353e6520c274e8d
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / utils / MdsalUtils.java
1 /*
2  * Copyright (c) 2016 CableLabs 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
9 package org.opendaylight.unimgr.utils;
10
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;
23
24 import com.google.common.base.Optional;
25 import com.google.common.util.concurrent.CheckedFuture;
26
27 public class MdsalUtils {
28
29     private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
30
31     private MdsalUtils() {
32         throw new AssertionError("Instantiating utility class.");
33     }
34
35     /**
36      * Read a specific datastore type and return a DataObject as a casted
37      * class type Object.
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
42      */
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)  {
47         D result = null;
48         final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
49         Optional<D> optionalDataObject;
50         final CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
51         try {
52             optionalDataObject = future.checkedGet();
53             if (optionalDataObject.isPresent()) {
54                 result = optionalDataObject.get();
55             } else {
56                 LOG.debug("{}: Failed to read {}",
57                         Thread.currentThread().getStackTrace()[1], path);
58             }
59         } catch (final ReadFailedException e) {
60             LOG.warn("Failed to read {} ", path, e);
61         }
62         transaction.close();
63         return result;
64     }
65
66     /**
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
71      */
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,
78                                                                         nodeIid);
79         try {
80             return nodeFuture.checkedGet();
81         } catch (final ReadFailedException e) {
82             LOG.info("Unable to read node with Iid {}", nodeIid);
83         }
84         return Optional.absent();
85     }
86
87     /**
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.
92      */
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);
100         try {
101             transaction.submit().checkedGet();
102             result = true;
103         } catch (final TransactionCommitFailedException e) {
104             LOG.error("Unable to remove node with Iid {} from store {}.", genericNode, store);
105         }
106         return result;
107     }
108
109     /**
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
115      */
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);
122         try {
123             return linkFuture.checkedGet();
124         } catch (final ReadFailedException e) {
125             LOG.info("Unable to read node with Iid {}", linkIid);
126         }
127         return Optional.absent();
128     }
129
130     /**
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
136      */
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);
144         try {
145             return nodeFuture.checkedGet();
146         } catch (final ReadFailedException e) {
147             LOG.info("Unable to read node with Iid {}", nodeIid);
148         }
149         return Optional.absent();
150     }
151 }