itm and mdsal porting
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / utils / hwvtep / HwvtepUtils.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.vpnservice.utils.hwvtep;
10
11 import java.util.List;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
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.vpnservice.mdsalutil.MDSALUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37
38 import com.google.common.base.Optional;
39 import com.google.common.util.concurrent.ListenableFuture;
40
41 /**
42  * Utility class to related to Hardware VTEP devices.
43  */
44 public final class HwvtepUtils {
45
46     // TODO: (eperefr) Move this to HwvtepSouthboundUtils when in place.
47     public static InstanceIdentifier<LocalUcastMacs> getWildCardPathForLocalUcastMacs() {
48         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
49                 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
50     }
51
52     /**
53      * Adds the logical switch into config DS.
54      *
55      * @param broker
56      *            the broker
57      * @param nodeId
58      *            the node id
59      * @param logicalSwitch
60      *            the logical switch
61      * @return the listenable future
62      */
63     public static ListenableFuture<Void> addLogicalSwitch(DataBroker broker, NodeId nodeId,
64             LogicalSwitches logicalSwitch) {
65         WriteTransaction transaction = broker.newWriteOnlyTransaction();
66         putLogicalSwitch(transaction, nodeId, logicalSwitch);
67         return transaction.submit();
68     }
69
70     /**
71      * Put the logical switches in the transaction.
72      *
73      * @param transaction
74      *            the transaction
75      * @param nodeId
76      *            the node id
77      * @param lstSwitches
78      *            the lst switches
79      */
80     public static void putLogicalSwitches(final WriteTransaction transaction, final NodeId nodeId,
81             final List<LogicalSwitches> lstSwitches) {
82         if (lstSwitches != null) {
83             for (LogicalSwitches logicalSwitch : lstSwitches) {
84                 putLogicalSwitch(transaction, nodeId, logicalSwitch);
85             }
86         }
87     }
88
89     /**
90      * Put logical switch in the transaction.
91      *
92      * @param transaction
93      *            the transaction
94      * @param nodeId
95      *            the node id
96      * @param logicalSwitch
97      *            the logical switch
98      */
99     public static void putLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
100             final LogicalSwitches logicalSwitch) {
101         InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
102                 logicalSwitch.getHwvtepNodeName());
103         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, logicalSwitch, true);
104     }
105
106     /**
107      * Delete logical switch from config DS.
108      *
109      * @param broker
110      *            the broker
111      * @param nodeId
112      *            the node id
113      * @param logicalSwitchName
114      *            the logical switch name
115      * @return the listenable future
116      */
117     public static ListenableFuture<Void> deleteLogicalSwitch(DataBroker broker, NodeId nodeId,
118             String logicalSwitchName) {
119         WriteTransaction transaction = broker.newWriteOnlyTransaction();
120         deleteLogicalSwitch(transaction, nodeId, logicalSwitchName);
121         return transaction.submit();
122     }
123
124     /**
125      * Delete logical switch from the transaction.
126      *
127      * @param transaction
128      *            the transaction
129      * @param nodeId
130      *            the node id
131      * @param logicalSwitchName
132      *            the logical switch name
133      */
134     public static void deleteLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
135             final String logicalSwitchName) {
136         transaction.delete(LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils
137                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
138     }
139
140     /**
141      * Gets the logical switch.
142      *
143      * @param nodeId
144      *            the node id
145      * @param logicalSwitchName
146      *            the logical switch name
147      * @return the logical switch
148      */
149     public static LogicalSwitches getLogicalSwitch(DataBroker broker, LogicalDatastoreType datastoreType, NodeId nodeId,
150             String logicalSwitchName) {
151         final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
152                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
153         Optional<LogicalSwitches> optLogicalSwitch = MDSALUtil.read(broker, datastoreType, iid);
154         if (optLogicalSwitch.isPresent()) {
155             return optLogicalSwitch.get();
156         }
157         return null;
158     }
159
160     /**
161      * Get LogicalSwitches for a given hwVtepNodeId.
162      *
163      * @param broker
164      *            the broker
165      * @param hwVtepNodeId
166      *            Hardware VTEP Node Id
167      * @param vni
168      *            virtual network id
169      * @return the logical switches
170      */
171     public static LogicalSwitches getLogicalSwitches(DataBroker broker, String hwVtepNodeId, String vni) {
172         NodeId nodeId = new NodeId(hwVtepNodeId);
173         InstanceIdentifier<LogicalSwitches> logicalSwitchesIdentifier = HwvtepSouthboundUtils
174                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(vni));
175
176         Optional<LogicalSwitches> logicalSwitches = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION,
177                 logicalSwitchesIdentifier);
178         if (!logicalSwitches.isPresent()) {
179             return null;
180         }
181
182         return logicalSwitches.get();
183     }
184
185     /**
186      * Put physical locators in the transaction.
187      *
188      * @param transaction
189      *            the transaction
190      * @param nodeId
191      *            the node id
192      * @param lstPhysicalLocator
193      *            the lst physical locator
194      */
195     public static void putPhysicalLocators(WriteTransaction transaction, NodeId nodeId,
196             List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocator) {
197         if (lstPhysicalLocator != null) {
198             for (HwvtepPhysicalLocatorAugmentation phyLocator : lstPhysicalLocator) {
199                 putPhysicalLocator(transaction, nodeId, phyLocator);
200             }
201         }
202     }
203
204     /**
205      * Put physical locator in the transaction.
206      *
207      * @param transaction
208      *            the transaction
209      * @param nodeId
210      *            the node id
211      * @param phyLocator
212      *            the phy locator
213      */
214     public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
215             final HwvtepPhysicalLocatorAugmentation phyLocator) {
216         InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
217                 phyLocator);
218         TerminationPoint terminationPoint = new TerminationPointBuilder()
219                 .setKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
220                 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocator).build();
221
222         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, terminationPoint, true);
223     }
224
225     /**
226      * Adds the remote ucast macs into config DS.
227      *
228      * @param broker
229      *            the broker
230      * @param nodeId
231      *            the node id
232      * @param lstRemoteUcastMacs
233      *            the lst remote ucast macs
234      * @return the listenable future
235      */
236     public static ListenableFuture<Void> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
237             List<RemoteUcastMacs> lstRemoteUcastMacs) {
238         WriteTransaction transaction = broker.newWriteOnlyTransaction();
239         putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
240         return transaction.submit();
241     }
242
243     /**
244      * Put remote ucast macs in the transaction.
245      *
246      * @param transaction
247      *            the transaction
248      * @param nodeId
249      *            the node id
250      * @param lstRemoteUcastMacs
251      *            the lst remote ucast macs
252      */
253     public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
254             final List<RemoteUcastMacs> lstRemoteUcastMacs) {
255         if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
256             for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
257                 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
258             }
259         }
260     }
261
262     /**
263      * Put remote ucast mac in the transaction.
264      *
265      * @param transaction
266      *            the transaction
267      * @param nodeId
268      *            the node id
269      * @param remoteUcastMac
270      *            the remote ucast mac
271      */
272     public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
273             RemoteUcastMacs remoteUcastMac) {
274         InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
275         .child(RemoteUcastMacs.class, new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
276         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac, true);
277     }
278
279     /**
280      * Delete remote ucast mac from the config DS.
281      *
282      * @param broker
283      *            the broker
284      * @param nodeId
285      *            the node id
286      * @param mac
287      *            the mac
288      * @return the listenable future
289      */
290     public static ListenableFuture<Void> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
291             String logicalSwitchName, MacAddress mac) {
292         WriteTransaction transaction = broker.newWriteOnlyTransaction();
293         deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
294         return transaction.submit();
295     }
296
297     /**
298      * Delete remote ucast macs from the config DS.
299      *
300      * @param broker
301      *            the broker
302      * @param nodeId
303      *            the node id
304      * @param lstMac
305      *            the lst mac
306      * @return the listenable future
307      */
308     public static ListenableFuture<Void> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
309             String logicalSwitchName, List<MacAddress> lstMac) {
310         WriteTransaction transaction = broker.newWriteOnlyTransaction();
311         deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
312         return transaction.submit();
313     }
314
315     /**
316      * Delete remote ucast macs from the transaction.
317      *
318      * @param transaction
319      *            the transaction
320      * @param nodeId
321      *            the node id
322      * @param lstMac
323      *            the lst mac
324      */
325     public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
326             String logicalSwitchName, final List<MacAddress> lstMac) {
327         if (lstMac != null && !lstMac.isEmpty()) {
328             for (MacAddress mac : lstMac) {
329                 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
330             }
331         }
332     }
333
334     /**
335      * Delete remote ucast mac from the transaction.
336      *
337      * @param transaction
338      *            the transaction
339      * @param nodeId
340      *            the node id
341      * @param mac
342      *            the mac
343      */
344     public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
345             String logialSwitchName,
346             final MacAddress mac) {
347         transaction.delete(LogicalDatastoreType.CONFIGURATION,
348                 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
349     }
350
351     /**
352      * Adds the remote mcast macs into config DS.
353      *
354      * @param broker
355      *            the broker
356      * @param nodeId
357      *            the node id
358      * @param lstRemoteMcastMacs
359      *            the lst remote mcast macs
360      * @return the listenable future
361      */
362     public static ListenableFuture<Void> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
363             List<RemoteMcastMacs> lstRemoteMcastMacs) {
364         WriteTransaction transaction = broker.newWriteOnlyTransaction();
365         putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
366         return transaction.submit();
367     }
368
369     /**
370      * Put remote mcast macs in the transaction.
371      *
372      * @param transaction
373      *            the transaction
374      * @param nodeId
375      *            the node id
376      * @param lstRemoteMcastMacs
377      *            the lst remote mcast macs
378      */
379     public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
380             final List<RemoteMcastMacs> lstRemoteMcastMacs) {
381         if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
382             for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
383                 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
384             }
385         }
386     }
387
388     /**
389      * Put remote mcast mac in the transaction.
390      *
391      * @param transaction
392      *            the transaction
393      * @param nodeId
394      *            the node id
395      * @param remoteMcastMac
396      *            the remote mcast mac
397      */
398     public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
399             RemoteMcastMacs remoteMcastMac) {
400         InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
401                 remoteMcastMac.getKey());
402         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac, true);
403     }
404
405     /**
406      * Delete remote mcast mac from config DS.
407      *
408      * @param broker
409      *            the broker
410      * @param nodeId
411      *            the node id
412      * @param remoteMcastMacsKey
413      *            the remote mcast macs key
414      * @return the listenable future
415      */
416     public static ListenableFuture<Void> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
417             RemoteMcastMacsKey remoteMcastMacsKey) {
418         WriteTransaction transaction = broker.newWriteOnlyTransaction();
419         deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
420         return transaction.submit();
421     }
422
423     /**
424      * Delete remote mcast macs from config DS.
425      *
426      * @param broker
427      *            the broker
428      * @param nodeId
429      *            the node id
430      * @param lstRemoteMcastMacsKey
431      *            the lst remote mcast macs key
432      * @return the listenable future
433      */
434     public static ListenableFuture<Void> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
435             List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
436         WriteTransaction transaction = broker.newWriteOnlyTransaction();
437         deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
438         return transaction.submit();
439     }
440
441     /**
442      * Delete remote mcast macs from the transaction.
443      *
444      * @param transaction
445      *            the transaction
446      * @param nodeId
447      *            the node id
448      * @param lstRemoteMcastMacsKey
449      *            the lst remote mcast macs key
450      */
451     public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
452             final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
453         if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
454             for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
455                 deleteRemoteMcastMac(transaction, nodeId, mac);
456             }
457         }
458     }
459
460     /**
461      * Delete remote mcast mac from the transaction.
462      *
463      * @param transaction
464      *            the transaction
465      * @param nodeId
466      *            the node id
467      * @param remoteMcastMacsKey
468      *            the remote mcast macs key
469      */
470     public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
471             final RemoteMcastMacsKey remoteMcastMacsKey) {
472         transaction.delete(LogicalDatastoreType.CONFIGURATION,
473                 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
474     }
475
476     /**
477      * Merge vlan bindings in the transaction.
478      *
479      * @param transaction
480      *            the transaction
481      * @param nodeId
482      *            the node id
483      * @param phySwitchName
484      *            the phy switch name
485      * @param phyPortName
486      *            the phy port name
487      * @param vlanBindings
488      *            the vlan bindings
489      */
490     public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
491             final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
492         NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
493         mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
494     }
495
496     /**
497      * Merge vlan bindings in the transaction.
498      *
499      * @param transaction
500      *            the transaction
501      * @param physicalSwitchNodeId
502      *            the physical switch node id
503      * @param phyPortName
504      *            the phy port name
505      * @param vlanBindings
506      *            the vlan bindings
507      */
508     public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
509             final String phyPortName, final List<VlanBindings> vlanBindings) {
510         HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
511                 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
512
513         final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
514                 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
515         transaction.merge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug, true);
516     }
517
518     /**
519      * Delete vlan binding from transaction.
520      *
521      * @param transaction
522      *            the transaction
523      * @param physicalSwitchNodeId
524      *            the physical switch node id
525      * @param phyPortName
526      *            the phy port name
527      * @param vlanId
528      *            the vlan id
529      */
530     public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
531             Integer vlanId) {
532         InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
533                 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
534         transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
535     }
536
537     /**
538      * Gets the hw vtep node.
539      *
540      * @param dataBroker
541      *            the data broker
542      * @param datastoreType
543      *            the datastore type
544      * @param nodeId
545      *            the node id
546      * @return the hw vtep node
547      */
548     public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId) {
549         Optional<Node> optNode = MDSALUtil.read(dataBroker, datastoreType,
550                 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
551         if (optNode.isPresent()) {
552             return optNode.get();
553         }
554         return null;
555     }
556 }