2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.utils.hwvtep;
10 import static java.util.Objects.requireNonNull;
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;
57 * TODO: Move these API's to ovsdb's utils.hwvtepsouthbound-utils module.
59 public final class HwvtepSouthboundUtils {
60 private HwvtepSouthboundUtils() {
65 * Creates the hwvtep topology instance identifier.
67 * @return the instance identifier
69 public static InstanceIdentifier<Topology> createHwvtepTopologyInstanceIdentifier() {
70 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
71 new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
75 * Creates the instance identifier.
79 * @return the instance identifier
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));
88 public static InstanceIdentifier<TerminationPoint> createTerminationPointId(NodeId nodeId,
89 TerminationPointKey tpKey) {
90 return createInstanceIdentifier(nodeId).child(TerminationPoint.class, tpKey);
94 * Creates the logical switches instance identifier.
98 * @param hwvtepNodeName
99 * the hwvtep node name
100 * @return the instance identifier
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));
109 * Creates the remote ucast macs instance identifier.
113 * @param logicalSwitchName the logical switch name
116 * @return the instance identifier
118 public static InstanceIdentifier<RemoteUcastMacs> createRemoteUcastMacsInstanceIdentifier(NodeId nodeId,
119 String logicalSwitchName,
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));
128 * Creates the local ucast macs instance identifier.
132 * @param logicalSwitchName the logical switch name
135 * @return the instance identifier
137 public static InstanceIdentifier<LocalUcastMacs> createLocalUcastMacsInstanceIdentifier(NodeId nodeId,
138 String logicalSwitchName,
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));
147 * Creates the remote mcast macs instance identifier.
151 * @param logicalSwitchName
152 * the logical switch name
155 * @return the instance identifier
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));
166 * Creates the remote mcast macs instance identifier.
170 * @param remoteMcastMacsKey
171 * the remote mcast macs key
172 * @return the instance identifier
174 public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
175 RemoteMcastMacsKey remoteMcastMacsKey) {
176 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
177 .child(RemoteMcastMacs.class, remoteMcastMacsKey);
181 * Creates the physical locator instance identifier.
185 * @param physicalLocatorAug
186 * the physical locator aug
187 * @return the instance identifier
189 public static InstanceIdentifier<TerminationPoint> createPhysicalLocatorInstanceIdentifier(NodeId nodeId,
190 HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
191 return createInstanceIdentifier(nodeId).child(TerminationPoint.class,
192 getTerminationPointKey(physicalLocatorAug));
196 * Creates the physical port instance identifier.
198 * @param physicalSwitchNodeId
199 * the physical switch node id
202 * @return the instance identifier
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);
212 * Creates the vlan binding instance identifier.
214 * @param physicalSwitchNodeId
215 * the physical switch node id
220 * @return the instance identifier
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)));
229 * Gets the termination point key.
233 * @return the termination point key
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));
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));
255 * Creates the managed node id.
259 * @param physicalSwitchName
260 * the physical switch name
261 * @return the node id
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);
270 * Create logical switch.
278 * @param replicationMode replication mode
279 * @return the logical switches
281 public static LogicalSwitches createLogicalSwitch(String name, String desc, String tunnelKey,
282 String replicationMode) {
283 HwvtepNodeName hwvtepName = new HwvtepNodeName(name);
285 LogicalSwitchesBuilder lsBuilder = new LogicalSwitchesBuilder().setHwvtepNodeDescription(desc)
286 .setHwvtepNodeName(hwvtepName).withKey(new LogicalSwitchesKey(hwvtepName)).setTunnelKey(tunnelKey);
288 if (replicationMode != null && !replicationMode.isEmpty()) {
289 lsBuilder.setReplicationMode(replicationMode);
291 return lsBuilder.build();
295 * Create hwvtep physical locator augmentation.
299 * @return the hwvtep physical locator augmentation
300 * @deprecated Use {@link #createHwvtepPhysicalLocatorAugmentation(IpAddress)} instead.
303 public static HwvtepPhysicalLocatorAugmentation createHwvtepPhysicalLocatorAugmentation(String ipAddress) {
304 return createHwvtepPhysicalLocatorAugmentation(IpAddressBuilder.getDefaultInstance(ipAddress));
308 * Create hwvtep physical locator augmentation.
310 * @param ipAddress the ip address
311 * @return the hwvtep physical locator augmentation
312 * @throws NullPointerException if {@code ipAddress} is null
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))
323 public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
324 requireNonNull(type);
325 if (type.isEmpty()) {
326 return EncapsulationTypeVxlanOverIpv4.class;
328 ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper
329 = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.inverse();
330 return mapper.get(type);
335 * Create remote ucast mac.
343 * @param logicalSwitchName
344 * the logical switch name
345 * @param physicalLocatorAug
346 * the physical locator aug
347 * @return the remote ucast macs
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));
356 RemoteUcastMacs remoteUcastMacs = new RemoteUcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
357 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorRef(phyLocRef).build();
358 return remoteUcastMacs;
362 * Creates the remote mcast mac.
370 * @param logicalSwitchName
371 * the logical switch name
372 * @param lstPhysicalLocatorAug
373 * the lst physical locator aug
374 * @return the remote mcast macs
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)));
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());
388 RemoteMcastMacs remoteMcastMacs = new RemoteMcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
389 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorSet(lstLocatorSet).build();
390 return remoteMcastMacs;
394 * Create vlan binding.
400 * @param logicalSwitchName
401 * the logical switch name
402 * @return the vlan bindings
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());
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();
417 public static String getTerminationPointKeyString(String ipAddress) {
418 String tpKeyStr = null;
419 if (ipAddress != null) {
420 tpKeyStr = HwvtepSouthboundConstants.TEP_PREFIX + ipAddress;