Refactoring of cisco-xr-driver and impl modules.
[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.onf.core.network.module.rev160630.g_forwardingconstruct.FcPort;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.common.base.Optional;
33 import com.google.common.util.concurrent.CheckedFuture;
34
35 public class MdsalUtils {
36
37     private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
38
39     private MdsalUtils() {
40         throw new AssertionError("Instantiating utility class.");
41     }
42
43     /**
44      * Read a specific datastore type and return a DataObject as a casted
45      * class type Object.
46      * @param dataBroker The dataBroker instance to create transactions
47      * @param store The store type to query
48      * @param path The generic path to query
49      * @return The DataObject as a casted Object
50      */
51     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
52             DataBroker dataBroker,
53             final LogicalDatastoreType store,
54             final InstanceIdentifier<D> path)  {
55         D result = null;
56         final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
57         Optional<D> optionalDataObject;
58         final CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
59         try {
60             optionalDataObject = future.checkedGet();
61             if (optionalDataObject.isPresent()) {
62                 result = optionalDataObject.get();
63             } else {
64                 LOG.debug("{}: Failed to read {}",
65                         Thread.currentThread().getStackTrace()[1], path);
66             }
67         } catch (final ReadFailedException e) {
68             LOG.warn("Failed to read {} ", path, e);
69         }
70         transaction.close();
71         return result;
72     }
73
74     /**
75      * Read a specific datastore type and return a optional of DataObject
76      * @param dataBroker The dataBroker instance to create transactions
77      * @param store The store type to query
78      * @param path The generic path to query
79      * @return Read object optional
80      */
81     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readOptional(
82             DataBroker dataBroker,
83             final LogicalDatastoreType store,
84             final InstanceIdentifier<D> path) {
85
86         final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
87         Optional<D> optionalDataObject = Optional.absent();
88         final CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
89         try {
90             optionalDataObject = future.checkedGet();
91         } catch (final ReadFailedException e) {
92             LOG.warn("Failed to read {} ", path, e);
93         }
94
95         transaction.close();
96         return optionalDataObject;
97     }
98
99     /**
100      * Read a specific node from the Operational Data store by default.
101      * @param dataBroker The dataBroker instance to create transactions
102      * @param genericNode The Instance Identifier of the Node
103      * @return The Optional Node instance
104      */
105     public static final Optional<Node> readNode(DataBroker dataBroker,
106                                                 InstanceIdentifier<?> genericNode) {
107         final ReadTransaction read = dataBroker.newReadOnlyTransaction();
108         final InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
109         final CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture =
110                                                               read.read(LogicalDatastoreType.OPERATIONAL,
111                                                                         nodeIid);
112         try {
113             return nodeFuture.checkedGet();
114         } catch (final ReadFailedException e) {
115             LOG.error("Unable to read node with Iid {}", nodeIid, e);
116         }
117         return Optional.absent();
118     }
119
120     /**
121      * Read a specific node from a specific data store type.
122      * @param dataBroker The dataBroker instance to create transactions
123      * @param store The data store type
124      * @param genericNode The Instance Identifier of a specific Node
125      * @return An Optional Node instance
126      */
127     public static final Optional<Node> readNode(DataBroker dataBroker,
128                                                 LogicalDatastoreType store,
129                                                 InstanceIdentifier<?> genericNode) {
130         final ReadTransaction read = dataBroker.newReadOnlyTransaction();
131         final InstanceIdentifier<Node> nodeIid = genericNode.firstIdentifierOf(Node.class);
132         final CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = read
133                 .read(store, nodeIid);
134         try {
135             return nodeFuture.checkedGet();
136         } catch (final ReadFailedException e) {
137             LOG.info("Unable to read node with Iid {}", nodeIid, e);
138         }
139         return Optional.absent();
140     }
141
142     /**
143      * Generic function to delete a node on a specific dataStore
144      * @param dataBroker The instance of the data broker to create transactions.
145      * @param genericNode The instance identifier of a generic node
146      * @param store The dataStore where to send and submit the delete call.
147      */
148     public static boolean deleteNode(DataBroker dataBroker,
149                                   InstanceIdentifier<?> genericNode,
150                                   LogicalDatastoreType store) {
151         LOG.info("Received a request to delete node {}", genericNode);
152         boolean result = false;
153         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
154         transaction.delete(store, genericNode);
155         try {
156             transaction.submit().checkedGet();
157             result = true;
158         } catch (final TransactionCommitFailedException e) {
159             LOG.error("Unable to remove node with Iid {} from store {}", genericNode, store, e);
160         }
161         return result;
162     }
163
164     /**
165      * Read a specific Link from a specific datastore
166      * @param dataBroker The dataBroker instance to create transactions
167      * @param store The datastore type.
168      * @param genericNode The Instance Identifier of the Link
169      * @return An Optional Link instance
170      */
171     public static final Optional<Link> readLink(DataBroker dataBroker,
172                                                 LogicalDatastoreType store,
173                                                 InstanceIdentifier<?> genericNode) {
174         final ReadTransaction read = dataBroker.newReadOnlyTransaction();
175         final InstanceIdentifier<Link> linkIid = genericNode.firstIdentifierOf(Link.class);
176         final CheckedFuture<Optional<Link>, ReadFailedException> linkFuture = read.read(store, linkIid);
177         try {
178             return linkFuture.checkedGet();
179         } catch (final ReadFailedException e) {
180             LOG.info("Unable to read node with Iid {}", linkIid, e);
181         }
182         return Optional.absent();
183     }
184
185     /**
186      * Read a specific Link from a specific datastore
187      * @param dataBroker The dataBroker instance to create transactions
188      * @param store The datastore type.
189      * @param topologyName The topology name.
190      * @return An Optional Link instance
191      */
192     public static final Optional<Topology> readTopology(DataBroker dataBroker,
193                                                         LogicalDatastoreType store,
194                                                         String topologyName) {
195         final ReadTransaction read = dataBroker.newReadOnlyTransaction();
196         final TopologyId topologyId = new TopologyId(topologyName);
197         InstanceIdentifier<Topology> topologyInstanceId
198             = InstanceIdentifier.builder(NetworkTopology.class)
199                                 .child(Topology.class, new TopologyKey(topologyId))
200                                 .build();
201         final CheckedFuture<Optional<Topology>, ReadFailedException> topologyFuture = read.read(store, topologyInstanceId);
202
203         try {
204             return topologyFuture.checkedGet();
205         } catch (final ReadFailedException e) {
206             LOG.info("Unable to read topology with Iid {}", topologyInstanceId, e);
207         }
208         return Optional.absent();
209     }
210
211     /**
212      * Read a TerminationPoint from datastore used in given FcPort
213      * @param dataBroker The dataBroker instance to create transactions
214      * @param store The datastore type.
215      * @param port FcPort data
216      * @return An Optional TerminationPoint instance
217      */
218     public static Optional<TerminationPoint> readTerminationPoint(DataBroker dataBroker, LogicalDatastoreType store, FcPort port) {
219         InstanceIdentifier tpIid = InstanceIdentifier.builder(NetworkTopology.class)
220                 .child(Topology.class, new TopologyKey(port.getTopology()))
221                 .child(Node.class, new NodeKey(port.getNode()))
222                 .child(TerminationPoint.class, new TerminationPointKey(port.getTp()))
223                 .build();
224
225         return MdsalUtils.readOptional(dataBroker, store, tpIid);
226     }
227 }