Bump upstreams for Silicon
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / utils / hwvtep / HwvtepSouthboundUtils.java
1 /*
2  * Copyright (c) 2016, 2017 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 package org.opendaylight.genius.utils.hwvtep;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableBiMap;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.apache.commons.lang3.StringUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSetBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55
56 /**
57  * TODO: Move these API's to ovsdb's utils.hwvtepsouthbound-utils module.
58  */
59 public final class HwvtepSouthboundUtils {
60     private HwvtepSouthboundUtils() {
61
62     }
63
64     /**
65      * Creates the hwvtep topology instance identifier.
66      *
67      * @return the instance identifier
68      */
69     public static InstanceIdentifier<Topology> createHwvtepTopologyInstanceIdentifier() {
70         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
71                 new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
72     }
73
74     /**
75      * Creates the instance identifier.
76      *
77      * @param nodeId
78      *            the node id
79      * @return the instance identifier
80      */
81     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
82         return InstanceIdentifier.create(NetworkTopology.class)
83                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
84                 .child(Node.class, new NodeKey(nodeId));
85     }
86
87
88     public static InstanceIdentifier<TerminationPoint> createTerminationPointId(NodeId nodeId,
89                                                                                 TerminationPointKey tpKey) {
90         return createInstanceIdentifier(nodeId).child(TerminationPoint.class, tpKey);
91     }
92
93     /**
94      * Creates the logical switches instance identifier.
95      *
96      * @param nodeId
97      *            the node id
98      * @param hwvtepNodeName
99      *            the hwvtep node name
100      * @return the instance identifier
101      */
102     public static InstanceIdentifier<LogicalSwitches> createLogicalSwitchesInstanceIdentifier(NodeId nodeId,
103             HwvtepNodeName hwvtepNodeName) {
104         return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
105                 .child(LogicalSwitches.class, new LogicalSwitchesKey(hwvtepNodeName));
106     }
107
108     /**
109      * Creates the remote ucast macs instance identifier.
110      *
111      * @param nodeId
112      *            the node id
113      * @param logicalSwitchName the logical switch name
114      * @param mac
115      *            the mac
116      * @return the instance identifier
117      */
118     public static InstanceIdentifier<RemoteUcastMacs> createRemoteUcastMacsInstanceIdentifier(NodeId nodeId,
119                                                                                               String logicalSwitchName,
120                                                                                               MacAddress mac) {
121         InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
122                 new HwvtepNodeName(logicalSwitchName));
123         return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
124                 .child(RemoteUcastMacs.class, new RemoteUcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
125     }
126
127     /**
128      * Creates the local ucast macs instance identifier.
129      *
130      * @param nodeId
131      *            the node id
132      * @param logicalSwitchName the logical switch name
133      * @param mac
134      *            the mac
135      * @return the instance identifier
136      */
137     public static InstanceIdentifier<LocalUcastMacs> createLocalUcastMacsInstanceIdentifier(NodeId nodeId,
138                                                                                             String logicalSwitchName,
139                                                                                             MacAddress mac) {
140         InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
141                 new HwvtepNodeName(logicalSwitchName));
142         return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class,
143                 new LocalUcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
144     }
145
146     /**
147      * Creates the remote mcast macs instance identifier.
148      *
149      * @param nodeId
150      *            the node id
151      * @param logicalSwitchName
152      *            the logical switch name
153      * @param mac
154      *            the mac
155      * @return the instance identifier
156      */
157     public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
158             String logicalSwitchName, MacAddress mac) {
159         InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
160                 new HwvtepNodeName(logicalSwitchName));
161         return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
162                 .child(RemoteMcastMacs.class, new RemoteMcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
163     }
164
165     /**
166      * Creates the remote mcast macs instance identifier.
167      *
168      * @param nodeId
169      *            the node id
170      * @param remoteMcastMacsKey
171      *            the remote mcast macs key
172      * @return the instance identifier
173      */
174     public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
175             RemoteMcastMacsKey remoteMcastMacsKey) {
176         return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
177                 .child(RemoteMcastMacs.class, remoteMcastMacsKey);
178     }
179
180     /**
181      * Creates the physical locator instance identifier.
182      *
183      * @param nodeId
184      *            the node id
185      * @param physicalLocatorAug
186      *            the physical locator aug
187      * @return the instance identifier
188      */
189     public static InstanceIdentifier<TerminationPoint> createPhysicalLocatorInstanceIdentifier(NodeId nodeId,
190             HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
191         return createInstanceIdentifier(nodeId).child(TerminationPoint.class,
192                 getTerminationPointKey(physicalLocatorAug));
193     }
194
195     /**
196      * Creates the physical port instance identifier.
197      *
198      * @param physicalSwitchNodeId
199      *            the physical switch node id
200      * @param phyPortName
201      *            the phy port name
202      * @return the instance identifier
203      */
204     public static InstanceIdentifier<HwvtepPhysicalPortAugmentation> createPhysicalPortInstanceIdentifier(
205             NodeId physicalSwitchNodeId, String phyPortName) {
206         return createInstanceIdentifier(physicalSwitchNodeId)
207                 .child(TerminationPoint.class, new TerminationPointKey(new TpId(phyPortName)))
208                 .augmentation(HwvtepPhysicalPortAugmentation.class);
209     }
210
211     /**
212      * Creates the vlan binding instance identifier.
213      *
214      * @param physicalSwitchNodeId
215      *            the physical switch node id
216      * @param phyPortName
217      *            the phy port name
218      * @param vlanId
219      *            the vlan id
220      * @return the instance identifier
221      */
222     public static InstanceIdentifier<VlanBindings> createVlanBindingInstanceIdentifier(NodeId physicalSwitchNodeId,
223             String phyPortName, Integer vlanId) {
224         return createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName).child(VlanBindings.class,
225                 new VlanBindingsKey(new VlanId(vlanId)));
226     }
227
228     /**
229      * Gets the termination point key.
230      *
231      * @param phyLocator
232      *            the phy locator
233      * @return the termination point key
234      */
235     public static TerminationPointKey getTerminationPointKey(HwvtepPhysicalLocatorAugmentation phyLocator) {
236         TerminationPointKey tpKey = null;
237         if (phyLocator.getEncapsulationType() != null && phyLocator.getDstIp() != null) {
238             String encapType = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.get(phyLocator.getEncapsulationType());
239             String tpKeyStr = encapType + ":" + phyLocator.getDstIp().stringValue();
240             tpKey = new TerminationPointKey(new TpId(tpKeyStr));
241         }
242         return tpKey;
243     }
244
245     public static TerminationPointKey getTerminationPointKey(String ipAddress) {
246         TerminationPointKey tpKey = null;
247         String tpKeyStr = getTerminationPointKeyString(ipAddress);
248         if (tpKeyStr != null) {
249             tpKey = new TerminationPointKey(new TpId(tpKeyStr));
250         }
251         return tpKey;
252     }
253
254     /**
255      * Creates the managed node id.
256      *
257      * @param nodeId
258      *            the node id
259      * @param physicalSwitchName
260      *            the physical switch name
261      * @return the node id
262      */
263     public static NodeId createManagedNodeId(NodeId nodeId, String physicalSwitchName) {
264         String phySwitchNodeId = nodeId.getValue() + "/" + HwvtepSouthboundConstants.PSWITCH_URI_PREFIX + "/"
265                 + physicalSwitchName;
266         return new NodeId(phySwitchNodeId);
267     }
268
269     /**
270      * Create logical switch.
271      *
272      * @param name
273      *            the name
274      * @param desc
275      *            the desc
276      * @param tunnelKey
277      *            the tunnel key
278      * @param replicationMode replication mode
279      * @return the logical switches
280      */
281     public static LogicalSwitches createLogicalSwitch(String name, String desc, String tunnelKey,
282                                                       String replicationMode) {
283         HwvtepNodeName hwvtepName = new HwvtepNodeName(name);
284
285         LogicalSwitchesBuilder lsBuilder = new LogicalSwitchesBuilder().setHwvtepNodeDescription(desc)
286                 .setHwvtepNodeName(hwvtepName).withKey(new LogicalSwitchesKey(hwvtepName)).setTunnelKey(tunnelKey);
287
288         if (replicationMode != null && !replicationMode.isEmpty()) {
289             lsBuilder.setReplicationMode(replicationMode);
290         }
291         return lsBuilder.build();
292     }
293
294     /**
295      * Create hwvtep physical locator augmentation.
296      *
297      * @param ipAddress
298      *            the ip address
299      * @return the hwvtep physical locator augmentation
300      * @deprecated Use {@link #createHwvtepPhysicalLocatorAugmentation(IpAddress)} instead.
301      */
302     @Deprecated
303     public static HwvtepPhysicalLocatorAugmentation createHwvtepPhysicalLocatorAugmentation(String ipAddress) {
304         return createHwvtepPhysicalLocatorAugmentation(IpAddressBuilder.getDefaultInstance(ipAddress));
305     }
306
307     /**
308      * Create hwvtep physical locator augmentation.
309      *
310      * @param ipAddress the ip address
311      * @return the hwvtep physical locator augmentation
312      * @throws NullPointerException if {@code ipAddress} is null
313      */
314     public static HwvtepPhysicalLocatorAugmentation createHwvtepPhysicalLocatorAugmentation(IpAddress ipAddress) {
315         // FIXME: Get encapsulation type dynamically
316         Class<? extends EncapsulationTypeBase> encapTypeClass = createEncapsulationType(StringUtils.EMPTY);
317         return new HwvtepPhysicalLocatorAugmentationBuilder()
318                 .setEncapsulationType(encapTypeClass)
319                 .setDstIp(requireNonNull(ipAddress))
320                 .build();
321     }
322
323     public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
324         requireNonNull(type);
325         if (type.isEmpty()) {
326             return EncapsulationTypeVxlanOverIpv4.class;
327         } else {
328             ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper
329                 = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.inverse();
330             return mapper.get(type);
331         }
332     }
333
334     /**
335      * Create remote ucast mac.
336      *
337      * @param nodeId
338      *            the node id
339      * @param mac
340      *            the mac
341      * @param ipAddress
342      *            the ip address
343      * @param logicalSwitchName
344      *            the logical switch name
345      * @param physicalLocatorAug
346      *            the physical locator aug
347      * @return the remote ucast macs
348      */
349     public static RemoteUcastMacs createRemoteUcastMac(NodeId nodeId, String mac, IpAddress ipAddress,
350             String logicalSwitchName, HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
351         HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(
352                 createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
353         HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
354                 createPhysicalLocatorInstanceIdentifier(nodeId, physicalLocatorAug));
355
356         RemoteUcastMacs remoteUcastMacs = new RemoteUcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
357                 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorRef(phyLocRef).build();
358         return remoteUcastMacs;
359     }
360
361     /**
362      * Creates the remote mcast mac.
363      *
364      * @param nodeId
365      *            the node id
366      * @param mac
367      *            the mac
368      * @param ipAddress
369      *            the ip address
370      * @param logicalSwitchName
371      *            the logical switch name
372      * @param lstPhysicalLocatorAug
373      *            the lst physical locator aug
374      * @return the remote mcast macs
375      */
376     public static RemoteMcastMacs createRemoteMcastMac(NodeId nodeId, String mac, IpAddress ipAddress,
377             String logicalSwitchName, List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocatorAug) {
378         HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(
379                 createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
380
381         List<LocatorSet> lstLocatorSet = new ArrayList<>();
382         for (HwvtepPhysicalLocatorAugmentation phyLocatorAug : lstPhysicalLocatorAug) {
383             HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
384                     createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug));
385             lstLocatorSet.add(new LocatorSetBuilder().setLocatorRef(phyLocRef).build());
386         }
387
388         RemoteMcastMacs remoteMcastMacs = new RemoteMcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
389                 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorSet(lstLocatorSet).build();
390         return remoteMcastMacs;
391     }
392
393     /**
394      * Create vlan binding.
395      *
396      * @param nodeId
397      *            the node id
398      * @param vlanId
399      *            the vlan id
400      * @param logicalSwitchName
401      *            the logical switch name
402      * @return the vlan bindings
403      */
404     public static VlanBindings createVlanBinding(NodeId nodeId, int vlanId, String logicalSwitchName) {
405         VlanBindingsBuilder vbBuilder = new VlanBindingsBuilder();
406         VlanBindingsKey vbKey = new VlanBindingsKey(new VlanId(vlanId));
407         vbBuilder.withKey(vbKey);
408         vbBuilder.setVlanIdKey(vbKey.getVlanIdKey());
409
410         final InstanceIdentifier<LogicalSwitches> lSwitchIid = createLogicalSwitchesInstanceIdentifier(nodeId,
411                 new HwvtepNodeName(logicalSwitchName));
412         HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(lSwitchIid);
413         vbBuilder.setLogicalSwitchRef(lsRef);
414         return vbBuilder.build();
415     }
416
417     public static String getTerminationPointKeyString(String ipAddress) {
418         String tpKeyStr = null;
419         if (ipAddress != null) {
420             tpKeyStr = HwvtepSouthboundConstants.TEP_PREFIX + ipAddress;
421         }
422         return tpKeyStr;
423     }
424 }