2 * Copyright (c) 2016 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
9 package org.opendaylight.vpnservice.utils.hwvtep;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.apache.commons.lang3.StringUtils;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSetBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import com.google.common.base.Preconditions;
55 import com.google.common.collect.ImmutableBiMap;
58 * TODO: Move these API's to ovsdb's utils.hwvtepsouthbound-utils module.
60 public class HwvtepSouthboundUtils {
63 * Creates the hwvtep topology instance identifier.
65 * @return the instance identifier
67 public static InstanceIdentifier<Topology> createHwvtepTopologyInstanceIdentifier() {
68 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
69 new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
73 * Creates the instance identifier.
77 * @return the instance identifier
79 public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
80 return InstanceIdentifier.create(NetworkTopology.class)
81 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
82 .child(Node.class, new NodeKey(nodeId));
86 public static InstanceIdentifier<TerminationPoint> createTerminationPointId(NodeId nodeId,
87 TerminationPointKey tpKey) {
88 return createInstanceIdentifier(nodeId).child(TerminationPoint.class, tpKey);
91 * Creates the logical switches instance identifier.
95 * @param hwvtepNodeName
96 * the hwvtep node name
97 * @return the instance identifier
99 public static InstanceIdentifier<LogicalSwitches> createLogicalSwitchesInstanceIdentifier(NodeId nodeId,
100 HwvtepNodeName hwvtepNodeName) {
101 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
102 .child(LogicalSwitches.class, new LogicalSwitchesKey(hwvtepNodeName));
106 * Creates the remote ucast macs instance identifier.
112 * @return the instance identifier
114 public static InstanceIdentifier<RemoteUcastMacs> createRemoteUcastMacsInstanceIdentifier(NodeId nodeId,
115 String logicalSwitchName,
117 InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
118 new HwvtepNodeName(logicalSwitchName));
119 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
120 .child(RemoteUcastMacs.class, new RemoteUcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
124 * Creates the local ucast macs instance identifier.
130 * @return the instance identifier
132 public static InstanceIdentifier<LocalUcastMacs> createLocalUcastMacsInstanceIdentifier(NodeId nodeId,
133 String logicalSwitchName,
135 InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
136 new HwvtepNodeName(logicalSwitchName));
137 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class,
138 new LocalUcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
142 * Creates the remote mcast macs instance identifier.
146 * @param logicalSwitchName
147 * the logical switch name
150 * @return the instance identifier
152 public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
153 String logicalSwitchName, MacAddress mac) {
154 InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
155 new HwvtepNodeName(logicalSwitchName));
156 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
157 .child(RemoteMcastMacs.class, new RemoteMcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
161 * Creates the remote mcast macs instance identifier.
165 * @param remoteMcastMacsKey
166 * the remote mcast macs key
167 * @return the instance identifier
169 public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
170 RemoteMcastMacsKey remoteMcastMacsKey) {
171 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
172 .child(RemoteMcastMacs.class, remoteMcastMacsKey);
176 * Creates the physical locator instance identifier.
180 * @param physicalLocatorAug
181 * the physical locator aug
182 * @return the instance identifier
184 public static InstanceIdentifier<TerminationPoint> createPhysicalLocatorInstanceIdentifier(NodeId nodeId,
185 HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
186 return createInstanceIdentifier(nodeId).child(TerminationPoint.class,
187 getTerminationPointKey(physicalLocatorAug));
191 * Creates the physical port instance identifier.
193 * @param physicalSwitchNodeId
194 * the physical switch node id
197 * @return the instance identifier
199 public static InstanceIdentifier<HwvtepPhysicalPortAugmentation> createPhysicalPortInstanceIdentifier(
200 NodeId physicalSwitchNodeId, String phyPortName) {
201 return createInstanceIdentifier(physicalSwitchNodeId)
202 .child(TerminationPoint.class, new TerminationPointKey(new TpId(phyPortName)))
203 .augmentation(HwvtepPhysicalPortAugmentation.class);
207 * Creates the vlan binding instance identifier.
209 * @param physicalSwitchNodeId
210 * the physical switch node id
215 * @return the instance identifier
217 public static InstanceIdentifier<VlanBindings> createVlanBindingInstanceIdentifier(NodeId physicalSwitchNodeId,
218 String phyPortName, Integer vlanId) {
219 return createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName).child(VlanBindings.class,
220 new VlanBindingsKey(new VlanId(vlanId)));
224 * Gets the termination point key.
228 * @return the termination point key
230 public static TerminationPointKey getTerminationPointKey(HwvtepPhysicalLocatorAugmentation phyLocator) {
231 TerminationPointKey tpKey = null;
232 if (phyLocator.getEncapsulationType() != null && phyLocator.getDstIp() != null) {
233 String encapType = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.get(phyLocator.getEncapsulationType());
234 String tpKeyStr = encapType + ":" + String.valueOf(phyLocator.getDstIp().getValue());
235 tpKey = new TerminationPointKey(new TpId(tpKeyStr));
241 * Creates the managed node id.
245 * @param physicalSwitchName
246 * the physical switch name
247 * @return the node id
249 public static NodeId createManagedNodeId(NodeId nodeId, String physicalSwitchName) {
250 String phySwitchNodeId = nodeId.getValue() + "/" + HwvtepSouthboundConstants.PSWITCH_URI_PREFIX + "/"
251 + physicalSwitchName;
252 return new NodeId(phySwitchNodeId);
256 * Create logical switch.
264 * @return the logical switches
266 public static LogicalSwitches createLogicalSwitch(String name, String desc, String tunnelKey) {
267 HwvtepNodeName hwvtepName = new HwvtepNodeName(name);
268 LogicalSwitchesBuilder lsBuilder = new LogicalSwitchesBuilder().setHwvtepNodeDescription(desc)
269 .setHwvtepNodeName(hwvtepName).setKey(new LogicalSwitchesKey(hwvtepName)).setTunnelKey(tunnelKey);
270 return lsBuilder.build();
274 * Create hwvtep physical locator augmentation.
278 * @return the hwvtep physical locator augmentation
280 public static HwvtepPhysicalLocatorAugmentation createHwvtepPhysicalLocatorAugmentation(String ipAddress) {
281 // FIXME: Get encapsulation type dynamically
282 Class<? extends EncapsulationTypeBase> encapTypeClass = createEncapsulationType(StringUtils.EMPTY);
283 HwvtepPhysicalLocatorAugmentationBuilder phyLocBuilder = new HwvtepPhysicalLocatorAugmentationBuilder()
284 .setEncapsulationType(encapTypeClass).setDstIp(new IpAddress(ipAddress.toCharArray()));
285 return phyLocBuilder.build();
288 public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
289 Preconditions.checkNotNull(type);
290 if (type.isEmpty()) {
291 return EncapsulationTypeVxlanOverIpv4.class;
293 ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP
295 return mapper.get(type);
300 * Create remote ucast mac.
308 * @param logicalSwitchName
309 * the logical switch name
310 * @param physicalLocatorAug
311 * the physical locator aug
312 * @return the remote ucast macs
314 public static RemoteUcastMacs createRemoteUcastMac(NodeId nodeId, String mac, IpAddress ipAddress,
315 String logicalSwitchName, HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
316 HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(
317 createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
318 HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
319 createPhysicalLocatorInstanceIdentifier(nodeId, physicalLocatorAug));
321 RemoteUcastMacs remoteUcastMacs = new RemoteUcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
322 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorRef(phyLocRef).build();
323 return remoteUcastMacs;
327 * Creates the remote mcast mac.
335 * @param logicalSwitchName
336 * the logical switch name
337 * @param lstPhysicalLocatorAug
338 * the lst physical locator aug
339 * @return the remote mcast macs
341 public static RemoteMcastMacs createRemoteMcastMac(NodeId nodeId, String mac, IpAddress ipAddress,
342 String logicalSwitchName, List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocatorAug) {
343 HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(
344 createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
346 List<LocatorSet> lstLocatorSet = new ArrayList<>();
347 for (HwvtepPhysicalLocatorAugmentation phyLocatorAug : lstPhysicalLocatorAug) {
348 HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
349 createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug));
350 lstLocatorSet.add(new LocatorSetBuilder().setLocatorRef(phyLocRef).build());
353 RemoteMcastMacs remoteMcastMacs = new RemoteMcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
354 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorSet(lstLocatorSet).build();
355 return remoteMcastMacs;
359 * Create vlan binding.
365 * @param logicalSwitchName
366 * the logical switch name
367 * @return the vlan bindings
369 public static VlanBindings createVlanBinding(NodeId nodeId, int vlanId, String logicalSwitchName) {
370 VlanBindingsBuilder vbBuilder = new VlanBindingsBuilder();
371 VlanBindingsKey vbKey = new VlanBindingsKey(new VlanId(vlanId));
372 vbBuilder.setKey(vbKey);
373 vbBuilder.setVlanIdKey(vbKey.getVlanIdKey());
375 final InstanceIdentifier<LogicalSwitches> lSwitchIid = createLogicalSwitchesInstanceIdentifier(nodeId,
376 new HwvtepNodeName(logicalSwitchName));
377 HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(lSwitchIid);
378 vbBuilder.setLogicalSwitchRef(lsRef);
379 return vbBuilder.build();
382 public static TerminationPointKey getTerminationPointKey(String ipAddress) {
383 TerminationPointKey tpKey = null;
384 String tpKeyStr = getTerminationPointKeyString(ipAddress);
385 if(tpKeyStr != null) {
386 tpKey = new TerminationPointKey(new TpId(tpKeyStr));
390 public static String getTerminationPointKeyString(String ipAddress) {
391 String tpKeyStr = null;
392 if(ipAddress != null) {
393 tpKeyStr = new StringBuilder(HwvtepSouthboundConstants.TEP_PREFIX).
394 append(ipAddress).toString();