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 * Creates the logical switches instance identifier.
90 * @param hwvtepNodeName
91 * the hwvtep node name
92 * @return the instance identifier
94 public static InstanceIdentifier<LogicalSwitches> createLogicalSwitchesInstanceIdentifier(NodeId nodeId,
95 HwvtepNodeName hwvtepNodeName) {
96 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
97 .child(LogicalSwitches.class, new LogicalSwitchesKey(hwvtepNodeName));
101 * Creates the remote ucast macs instance identifier.
107 * @return the instance identifier
109 public static InstanceIdentifier<RemoteUcastMacs> createRemoteUcastMacsInstanceIdentifier(NodeId nodeId,
110 String logicalSwitchName,
112 InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
113 new HwvtepNodeName(logicalSwitchName));
114 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
115 .child(RemoteUcastMacs.class, new RemoteUcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
119 * Creates the local ucast macs instance identifier.
125 * @return the instance identifier
127 public static InstanceIdentifier<LocalUcastMacs> createLocalUcastMacsInstanceIdentifier(NodeId nodeId,
128 String logicalSwitchName,
130 InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
131 new HwvtepNodeName(logicalSwitchName));
132 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class,
133 new LocalUcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
137 * Creates the remote mcast macs instance identifier.
141 * @param logicalSwitchName
142 * the logical switch name
145 * @return the instance identifier
147 public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
148 String logicalSwitchName, MacAddress mac) {
149 InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
150 new HwvtepNodeName(logicalSwitchName));
151 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
152 .child(RemoteMcastMacs.class, new RemoteMcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
156 * Creates the remote mcast macs instance identifier.
160 * @param remoteMcastMacsKey
161 * the remote mcast macs key
162 * @return the instance identifier
164 public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
165 RemoteMcastMacsKey remoteMcastMacsKey) {
166 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
167 .child(RemoteMcastMacs.class, remoteMcastMacsKey);
171 * Creates the physical locator instance identifier.
175 * @param physicalLocatorAug
176 * the physical locator aug
177 * @return the instance identifier
179 public static InstanceIdentifier<TerminationPoint> createPhysicalLocatorInstanceIdentifier(NodeId nodeId,
180 HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
181 return createInstanceIdentifier(nodeId).child(TerminationPoint.class,
182 getTerminationPointKey(physicalLocatorAug));
186 * Creates the physical port instance identifier.
188 * @param physicalSwitchNodeId
189 * the physical switch node id
192 * @return the instance identifier
194 public static InstanceIdentifier<HwvtepPhysicalPortAugmentation> createPhysicalPortInstanceIdentifier(
195 NodeId physicalSwitchNodeId, String phyPortName) {
196 return createInstanceIdentifier(physicalSwitchNodeId)
197 .child(TerminationPoint.class, new TerminationPointKey(new TpId(phyPortName)))
198 .augmentation(HwvtepPhysicalPortAugmentation.class);
202 * Creates the vlan binding instance identifier.
204 * @param physicalSwitchNodeId
205 * the physical switch node id
210 * @return the instance identifier
212 public static InstanceIdentifier<VlanBindings> createVlanBindingInstanceIdentifier(NodeId physicalSwitchNodeId,
213 String phyPortName, Integer vlanId) {
214 return createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName).child(VlanBindings.class,
215 new VlanBindingsKey(new VlanId(vlanId)));
219 * Gets the termination point key.
223 * @return the termination point key
225 public static TerminationPointKey getTerminationPointKey(HwvtepPhysicalLocatorAugmentation phyLocator) {
226 TerminationPointKey tpKey = null;
227 if (phyLocator.getEncapsulationType() != null && phyLocator.getDstIp() != null) {
228 String encapType = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.get(phyLocator.getEncapsulationType());
229 String tpKeyStr = encapType + ":" + String.valueOf(phyLocator.getDstIp().getValue());
230 tpKey = new TerminationPointKey(new TpId(tpKeyStr));
236 * Creates the managed node id.
240 * @param physicalSwitchName
241 * the physical switch name
242 * @return the node id
244 public static NodeId createManagedNodeId(NodeId nodeId, String physicalSwitchName) {
245 String phySwitchNodeId = nodeId.getValue() + "/" + HwvtepSouthboundConstants.PSWITCH_URI_PREFIX + "/"
246 + physicalSwitchName;
247 return new NodeId(phySwitchNodeId);
251 * Create logical switch.
259 * @return the logical switches
261 public static LogicalSwitches createLogicalSwitch(String name, String desc, String tunnelKey) {
262 HwvtepNodeName hwvtepName = new HwvtepNodeName(name);
263 LogicalSwitchesBuilder lsBuilder = new LogicalSwitchesBuilder().setHwvtepNodeDescription(desc)
264 .setHwvtepNodeName(hwvtepName).setKey(new LogicalSwitchesKey(hwvtepName)).setTunnelKey(tunnelKey);
265 return lsBuilder.build();
269 * Create hwvtep physical locator augmentation.
273 * @return the hwvtep physical locator augmentation
275 public static HwvtepPhysicalLocatorAugmentation createHwvtepPhysicalLocatorAugmentation(String ipAddress) {
276 // FIXME: Get encapsulation type dynamically
277 Class<? extends EncapsulationTypeBase> encapTypeClass = createEncapsulationType(StringUtils.EMPTY);
278 HwvtepPhysicalLocatorAugmentationBuilder phyLocBuilder = new HwvtepPhysicalLocatorAugmentationBuilder()
279 .setEncapsulationType(encapTypeClass).setDstIp(new IpAddress(ipAddress.toCharArray()));
280 return phyLocBuilder.build();
283 public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
284 Preconditions.checkNotNull(type);
285 if (type.isEmpty()) {
286 return EncapsulationTypeVxlanOverIpv4.class;
288 ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP
290 return mapper.get(type);
295 * Create remote ucast mac.
303 * @param logicalSwitchName
304 * the logical switch name
305 * @param physicalLocatorAug
306 * the physical locator aug
307 * @return the remote ucast macs
309 public static RemoteUcastMacs createRemoteUcastMac(NodeId nodeId, String mac, IpAddress ipAddress,
310 String logicalSwitchName, HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
311 HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(
312 createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
313 HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
314 createPhysicalLocatorInstanceIdentifier(nodeId, physicalLocatorAug));
316 RemoteUcastMacs remoteUcastMacs = new RemoteUcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
317 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorRef(phyLocRef).build();
318 return remoteUcastMacs;
322 * Creates the remote mcast mac.
330 * @param logicalSwitchName
331 * the logical switch name
332 * @param lstPhysicalLocatorAug
333 * the lst physical locator aug
334 * @return the remote mcast macs
336 public static RemoteMcastMacs createRemoteMcastMac(NodeId nodeId, String mac, IpAddress ipAddress,
337 String logicalSwitchName, List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocatorAug) {
338 HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(
339 createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
341 List<LocatorSet> lstLocatorSet = new ArrayList<>();
342 for (HwvtepPhysicalLocatorAugmentation phyLocatorAug : lstPhysicalLocatorAug) {
343 HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
344 createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug));
345 lstLocatorSet.add(new LocatorSetBuilder().setLocatorRef(phyLocRef).build());
348 RemoteMcastMacs remoteMcastMacs = new RemoteMcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
349 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorSet(lstLocatorSet).build();
350 return remoteMcastMacs;
354 * Create vlan binding.
360 * @param logicalSwitchName
361 * the logical switch name
362 * @return the vlan bindings
364 public static VlanBindings createVlanBinding(NodeId nodeId, int vlanId, String logicalSwitchName) {
365 VlanBindingsBuilder vbBuilder = new VlanBindingsBuilder();
366 VlanBindingsKey vbKey = new VlanBindingsKey(new VlanId(vlanId));
367 vbBuilder.setKey(vbKey);
368 vbBuilder.setVlanIdKey(vbKey.getVlanIdKey());
370 final InstanceIdentifier<LogicalSwitches> lSwitchIid = createLogicalSwitchesInstanceIdentifier(nodeId,
371 new HwvtepNodeName(logicalSwitchName));
372 HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(lSwitchIid);
373 vbBuilder.setLogicalSwitchRef(lsRef);
374 return vbBuilder.build();