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));
136 public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
137 String logicalSwitchName,
139 InstanceIdentifier<LogicalSwitches> logicalSwitch = createLogicalSwitchesInstanceIdentifier(nodeId,
140 new HwvtepNodeName(logicalSwitchName));
141 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class).child(RemoteMcastMacs.class,
142 new RemoteMcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch), mac));
146 * Creates the remote mcast macs instance identifier.
150 * @param remoteMcastMacsKey
151 * the remote mcast macs key
152 * @return the instance identifier
154 public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
155 RemoteMcastMacsKey remoteMcastMacsKey) {
156 return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
157 .child(RemoteMcastMacs.class, remoteMcastMacsKey);
161 * Creates the physical locator instance identifier.
165 * @param physicalLocatorAug
166 * the physical locator aug
167 * @return the instance identifier
169 public static InstanceIdentifier<TerminationPoint> createPhysicalLocatorInstanceIdentifier(NodeId nodeId,
170 HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
171 return createInstanceIdentifier(nodeId).child(TerminationPoint.class,
172 getTerminationPointKey(physicalLocatorAug));
176 * Creates the physical port instance identifier.
178 * @param physicalSwitchNodeId
179 * the physical switch node id
182 * @return the instance identifier
184 public static InstanceIdentifier<HwvtepPhysicalPortAugmentation> createPhysicalPortInstanceIdentifier(
185 NodeId physicalSwitchNodeId, String phyPortName) {
186 return createInstanceIdentifier(physicalSwitchNodeId)
187 .child(TerminationPoint.class, new TerminationPointKey(new TpId(phyPortName)))
188 .augmentation(HwvtepPhysicalPortAugmentation.class);
192 * Creates the vlan binding instance identifier.
194 * @param physicalSwitchNodeId
195 * the physical switch node id
200 * @return the instance identifier
202 public static InstanceIdentifier<VlanBindings> createVlanBindingInstanceIdentifier(NodeId physicalSwitchNodeId,
203 String phyPortName, Integer vlanId) {
204 return createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName).child(VlanBindings.class,
205 new VlanBindingsKey(new VlanId(vlanId)));
209 * Gets the termination point key.
213 * @return the termination point key
215 public static TerminationPointKey getTerminationPointKey(HwvtepPhysicalLocatorAugmentation phyLocator) {
216 TerminationPointKey tpKey = null;
217 if (phyLocator.getEncapsulationType() != null && phyLocator.getDstIp() != null) {
218 String encapType = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.get(phyLocator.getEncapsulationType());
219 String tpKeyStr = encapType + ":" + String.valueOf(phyLocator.getDstIp().getValue());
220 tpKey = new TerminationPointKey(new TpId(tpKeyStr));
226 * Creates the managed node id.
230 * @param physicalSwitchName
231 * the physical switch name
232 * @return the node id
234 public static NodeId createManagedNodeId(NodeId nodeId, String physicalSwitchName) {
235 String phySwitchNodeId = nodeId.getValue() + "/" + HwvtepSouthboundConstants.PSWITCH_URI_PREFIX + "/"
236 + physicalSwitchName;
237 return new NodeId(phySwitchNodeId);
241 * Create logical switch.
249 * @return the logical switches
251 public static LogicalSwitches createLogicalSwitch(String name, String desc, String tunnelKey) {
252 HwvtepNodeName hwvtepName = new HwvtepNodeName(name);
253 LogicalSwitchesBuilder lsBuilder = new LogicalSwitchesBuilder().setHwvtepNodeDescription(desc)
254 .setHwvtepNodeName(hwvtepName).setKey(new LogicalSwitchesKey(hwvtepName)).setTunnelKey(tunnelKey);
255 return lsBuilder.build();
259 * Create hwvtep physical locator augmentation.
263 * @return the hwvtep physical locator augmentation
265 public static HwvtepPhysicalLocatorAugmentation createHwvtepPhysicalLocatorAugmentation(String ipAddress) {
266 // FIXME: Get encapsulation type dynamically
267 Class<? extends EncapsulationTypeBase> encapTypeClass = createEncapsulationType(StringUtils.EMPTY);
268 HwvtepPhysicalLocatorAugmentationBuilder phyLocBuilder = new HwvtepPhysicalLocatorAugmentationBuilder()
269 .setEncapsulationType(encapTypeClass).setDstIp(new IpAddress(ipAddress.toCharArray()));
270 return phyLocBuilder.build();
273 public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
274 Preconditions.checkNotNull(type);
275 if (type.isEmpty()) {
276 return EncapsulationTypeVxlanOverIpv4.class;
278 ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP
280 return mapper.get(type);
285 * Create remote ucast mac.
293 * @param logicalSwitchName
294 * the logical switch name
295 * @param physicalLocatorAug
296 * the physical locator aug
297 * @return the remote ucast macs
299 public static RemoteUcastMacs createRemoteUcastMac(NodeId nodeId, String mac, IpAddress ipAddress,
300 String logicalSwitchName, HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
301 HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(
302 createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
303 HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
304 createPhysicalLocatorInstanceIdentifier(nodeId, physicalLocatorAug));
306 RemoteUcastMacs remoteUcastMacs = new RemoteUcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
307 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorRef(phyLocRef).build();
308 return remoteUcastMacs;
312 * Creates the remote mcast mac.
320 * @param logicalSwitchName
321 * the logical switch name
322 * @param lstPhysicalLocatorAug
323 * the lst physical locator aug
324 * @return the remote mcast macs
326 public static RemoteMcastMacs createRemoteMcastMac(NodeId nodeId, String mac, IpAddress ipAddress,
327 String logicalSwitchName, List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocatorAug) {
328 HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(
329 createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
331 List<LocatorSet> lstLocatorSet = new ArrayList<>();
332 for (HwvtepPhysicalLocatorAugmentation phyLocatorAug : lstPhysicalLocatorAug) {
333 HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
334 createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug));
335 lstLocatorSet.add(new LocatorSetBuilder().setLocatorRef(phyLocRef).build());
338 RemoteMcastMacs remoteMcastMacs = new RemoteMcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
339 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorSet(lstLocatorSet).build();
340 return remoteMcastMacs;
344 * Create vlan binding.
350 * @param logicalSwitchName
351 * the logical switch name
352 * @return the vlan bindings
354 public static VlanBindings createVlanBinding(NodeId nodeId, int vlanId, String logicalSwitchName) {
355 VlanBindingsBuilder vbBuilder = new VlanBindingsBuilder();
356 VlanBindingsKey vbKey = new VlanBindingsKey(new VlanId(vlanId));
357 vbBuilder.setKey(vbKey);
358 vbBuilder.setVlanIdKey(vbKey.getVlanIdKey());
360 final InstanceIdentifier<LogicalSwitches> lSwitchIid = createLogicalSwitchesInstanceIdentifier(nodeId,
361 new HwvtepNodeName(logicalSwitchName));
362 HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(lSwitchIid);
363 vbBuilder.setLogicalSwitchRef(lsRef);
364 return vbBuilder.build();