Add MdsalUtils API stubs
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / MdsalUtils.java
1 /*
2  * Copyright (c) 2015 Red Hat, Inc. 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 package org.opendaylight.ovsdb.openstack.netvirt.impl;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableBiMap;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import java.security.InvalidParameterException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
23 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
24 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * Utility class to wrap mdsal transactions.
54  *
55  * @author Sam Hague (shague@redhat.com)
56  */
57 public class MdsalUtils {
58     private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
59     private static DataBroker databroker = null;
60     private static final int OVSDB_UPDATE_TIMEOUT = 500;
61
62     /**
63      * Class constructor setting the data broker.
64      *
65      * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
66      */
67     public MdsalUtils(DataBroker dataBroker) {
68         this.databroker = dataBroker;
69     }
70
71     /**
72      * Executes delete as a blocking transaction.
73      *
74      * @param store {@link LogicalDatastoreType} which should be modified
75      * @param path {@link InstanceIdentifier} to read from
76      * @param <D> the data object type
77      * @return the result of the request
78      */
79     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
80             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
81         boolean result = false;
82         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
83         transaction.delete(store, path);
84         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
85         try {
86             future.checkedGet();
87             result = true;
88         } catch (TransactionCommitFailedException e) {
89             LOG.warn("Failed to delete {} ", path, e);
90         }
91         return result;
92     }
93
94     /**
95      * Executes merge as a blocking transaction.
96      *
97      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
98      * @param path {@link InstanceIdentifier} for path to read
99      * @param <D> the data object type
100      * @return the result of the request
101      */
102     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean merge(
103             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
104         boolean result = false;
105         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
106         transaction.merge(logicalDatastoreType, path, data, true);
107         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
108         try {
109             future.checkedGet();
110             result = true;
111         } catch (TransactionCommitFailedException e) {
112             LOG.warn("Failed to merge {} ", path, e);
113         }
114         return result;
115     }
116
117     /**
118      * Executes put as a blocking transaction.
119      *
120      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
121      * @param path {@link InstanceIdentifier} for path to read
122      * @param <D> the data object type
123      * @return the result of the request
124      */
125     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean put(
126             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
127         boolean result = false;
128         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
129         transaction.put(logicalDatastoreType, path, data, true);
130         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
131         try {
132             future.checkedGet();
133             result = true;
134         } catch (TransactionCommitFailedException e) {
135             LOG.warn("Failed to put {} ", path, e);
136         }
137         return result;
138     }
139
140     /**
141      * Executes read as a blocking transaction.
142      *
143      * @param store {@link LogicalDatastoreType} to read
144      * @param path {@link InstanceIdentifier} for path to read
145      * @param <D> the data object type
146      * @return the result as the data object requested
147      */
148     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
149             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
150         D result = null;
151         final ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
152         Optional<D> optionalDataObject;
153         CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
154         try {
155             optionalDataObject = future.checkedGet();
156             if (optionalDataObject.isPresent()) {
157                 result = optionalDataObject.get();
158             } else {
159                 LOG.debug("{}: Failed to read {}",
160                         Thread.currentThread().getStackTrace()[1], path);
161             }
162         } catch (ReadFailedException e) {
163             LOG.warn("Failed to read {} ", path, e);
164         }
165         transaction.close();
166         return result;
167     }
168
169     public static String getOptionsValue(List<Options> options, String key) {
170         String value = null;
171         for (Options option : options) {
172             if (option.getKey().equals(key)) {
173                 value = option.getValue();
174             }
175         }
176         return value;
177     }
178
179     public static String getInterfaceExternalIdsValue(OvsdbTerminationPointAugmentation terminationPointAugmentation,
180                                                       String key) {
181         String value = null;
182         List<InterfaceExternalIds> pairs = terminationPointAugmentation.getInterfaceExternalIds();
183         for (InterfaceExternalIds pair : pairs) {
184             if (pair.getKey().equals(key)) {
185                 value = pair.getExternalIdValue();
186             }
187         }
188         return value;
189     }
190
191     public static ConnectionInfo getConnectionInfo(Node node) {
192         ConnectionInfo connectionInfo = null;
193         OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class);
194         if (ovsdbNodeAugmentation != null) {
195             connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
196         }
197         return connectionInfo;
198     }
199
200     public static OvsdbBridgeAugmentation getBridge(Node node, String name) {
201         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = null;
202         ConnectionInfo connectionInfo = getConnectionInfo(node);
203         if (connectionInfo != null) {
204             InstanceIdentifier<Node> bridgeIid =
205                     SouthboundMapper.createInstanceIdentifier(connectionInfo,
206                             new OvsdbBridgeName(name));
207             Node bridgeNode = read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
208             if (bridgeNode != null) {
209                 ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
210             }
211         }
212         return ovsdbBridgeAugmentation;
213     }
214
215     public static Uuid getBridgeUuid(Node node, String name) {
216         Uuid uuid = null;
217         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = getBridge(node, name);
218         if (ovsdbBridgeAugmentation != null) {
219             uuid = ovsdbBridgeAugmentation.getBridgeUuid();
220         }
221         return uuid;
222     }
223
224     public static boolean addBridge(Node ovsdbNode, String bridgeName, String target)
225             throws InterruptedException, InvalidParameterException {
226         boolean result = false;
227
228         LOG.info("addBridge: node: {}, bridgeName: {}, target: {}", ovsdbNode, bridgeName, target);
229         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
230         if (connectionInfo != null) {
231             NodeBuilder bridgeNodeBuilder = new NodeBuilder();
232             InstanceIdentifier<Node> bridgeIid =
233                     SouthboundMapper.createInstanceIdentifier(connectionInfo, new OvsdbBridgeName(bridgeName));
234             NodeId bridgeNodeId = SouthboundMapper.createManagedNodeId(bridgeIid);
235             bridgeNodeBuilder.setNodeId(bridgeNodeId);
236             OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
237             //ovsdbBridgeAugmentationBuilder.setControllerEntry(setControllerEntries(target));
238             ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName));
239             ovsdbBridgeAugmentationBuilder.setProtocolEntry(createMdsalProtocols());
240             ovsdbBridgeAugmentationBuilder.setFailMode(
241                     SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get("secure"));
242             setManagedBy(ovsdbBridgeAugmentationBuilder, connectionInfo);
243             bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
244
245             result = merge(LogicalDatastoreType.CONFIGURATION, bridgeIid, bridgeNodeBuilder.build());
246             LOG.info("addBridge: result: {}", result);
247             Thread.sleep(OVSDB_UPDATE_TIMEOUT);
248             addController(ovsdbNode, bridgeName, target);
249             Thread.sleep(OVSDB_UPDATE_TIMEOUT);
250         } else {
251             throw new InvalidParameterException("Could not find ConnectionInfo");
252         }
253
254         return result;
255     }
256
257     private static void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
258                                      ConnectionInfo connectionInfo) {
259         InstanceIdentifier<Node> connectionNodePath = SouthboundMapper.createInstanceIdentifier(connectionInfo);
260         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
261     }
262
263     private static void addController(Node ovsdbNode, String bridgeName, String targetString) {
264         ConnectionInfo connectionInfo = getConnectionInfo(ovsdbNode);
265         if (connectionInfo != null) {
266             for (ControllerEntry controllerEntry: setControllerEntries(targetString)) {
267                 InstanceIdentifier<ControllerEntry> iid =
268                         SouthboundMapper.createInstanceIdentifier(connectionInfo, new OvsdbBridgeName(bridgeName))
269                                 .augmentation(OvsdbBridgeAugmentation.class)
270                                 .child(ControllerEntry.class, controllerEntry.getKey());
271
272                 boolean result = put(LogicalDatastoreType.CONFIGURATION, iid, controllerEntry);
273                 LOG.info("addController: result: {}", result);
274             }
275         }
276     }
277
278     private static List<ControllerEntry> setControllerEntries(String targetString) {
279         List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
280         ControllerEntryBuilder controllerEntryBuilder = new ControllerEntryBuilder();
281         controllerEntryBuilder.setTarget(new Uri(targetString));
282         controllerEntries.add(controllerEntryBuilder.build());
283         return controllerEntries;
284     }
285
286     private static List<ProtocolEntry> createMdsalProtocols() {
287         List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
288         ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
289                 SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
290         protocolList.add(new ProtocolEntryBuilder().
291                 setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get("OpenFlow13")).build());
292         return protocolList;
293     }
294
295     public static OvsdbTerminationPointAugmentation getTerminationPointAugmentation(Node bridgeNode, String portName) {
296         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
297         if (ovsdbBridgeAugmentation != null) {
298             List<TerminationPoint> terminationPoints = bridgeNode.getTerminationPoint();
299             for(TerminationPoint terminationPoint : terminationPoints) {
300                 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
301                         terminationPoint.getAugmentation( OvsdbTerminationPointAugmentation.class);
302                 if (ovsdbTerminationPointAugmentation != null
303                         && ovsdbTerminationPointAugmentation.getName().equals(portName)) {
304                     return ovsdbTerminationPointAugmentation;
305                 }
306             }
307         }
308         return null;
309     }
310
311     private static Topology getOvsdbTopology() {
312         InstanceIdentifier<Topology> path = InstanceIdentifier
313                 .create(NetworkTopology.class)
314                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
315
316         Topology topology = read(LogicalDatastoreType.OPERATIONAL, path);
317         return topology;
318     }
319
320     public static String getDatapathId(Node node) {
321         String datapathId = null;
322         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = node.getAugmentation(OvsdbBridgeAugmentation.class);
323         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
324             datapathId = node.getAugmentation(OvsdbBridgeAugmentation.class).getDatapathId().getValue();
325         }
326         return datapathId;
327     }
328
329     public static String getDatapathId(OvsdbBridgeAugmentation ovsdbBridgeAugmentation) {
330         String datapathId = null;
331         if (ovsdbBridgeAugmentation != null && ovsdbBridgeAugmentation.getDatapathId() != null) {
332             datapathId = ovsdbBridgeAugmentation.getDatapathId().getValue();
333         }
334         return datapathId;
335     }
336
337     public static String getBridgeName(Node node) {
338         return (node.getAugmentation(OvsdbBridgeAugmentation.class).getBridgeName().getValue());
339     }
340
341     public static List<Node> getBridges(Node node) {
342         return null;
343     }
344
345     public static List<OvsdbTerminationPointAugmentation> getPorts(Node node) {
346         return null;
347     }
348
349     public static Boolean deletePort(Node node, String portName) {
350         return false;
351     }
352
353     public static Boolean addPort(Node node, String bridgeName, String portName) {
354         return false;
355     }
356
357     public static Boolean addPatchPort(Node node, String bridgeName, String portName, String peerPortName) {
358         return false;
359     }
360
361     public static OvsdbTerminationPointAugmentation getPort(Node node, String portName) {
362         return null;
363     }
364
365     public static String getExternalId(Node node, OvsdbTables table, String key) {
366         return null;
367     }
368
369     public static String getOtherConfig(Node node, OvsdbTables table, String key) {
370         return null;
371     }
372 }