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