Merge "HwVTEP JUNITs, and fixing show:vxlan CLI"
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / utils / hwvtep / HwvtepSouthboundUtils.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.vpnservice.utils.hwvtep;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
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;
53
54 import com.google.common.base.Preconditions;
55 import com.google.common.collect.ImmutableBiMap;
56
57 /**
58  * TODO: Move these API's to ovsdb's utils.hwvtepsouthbound-utils module.
59  */
60 public class HwvtepSouthboundUtils {
61
62     /**
63      * Creates the hwvtep topology instance identifier.
64      *
65      * @return the instance identifier
66      */
67     public static InstanceIdentifier<Topology> createHwvtepTopologyInstanceIdentifier() {
68         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
69                 new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
70     }
71
72     /**
73      * Creates the instance identifier.
74      *
75      * @param nodeId
76      *            the node id
77      * @return the instance identifier
78      */
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));
83     }
84
85     /**
86      * Creates the logical switches instance identifier.
87      *
88      * @param nodeId
89      *            the node id
90      * @param hwvtepNodeName
91      *            the hwvtep node name
92      * @return the instance identifier
93      */
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));
98     }
99
100     /**
101      * Creates the remote ucast macs instance identifier.
102      *
103      * @param nodeId
104      *            the node id
105      * @param mac
106      *            the mac
107      * @return the instance identifier
108      */
109     public static InstanceIdentifier<RemoteUcastMacs> createRemoteUcastMacsInstanceIdentifier(NodeId nodeId,
110             String logicalSwitchName,
111             MacAddress mac) {
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));
116     }
117
118     /**
119      * Creates the local ucast macs instance identifier.
120      *
121      * @param nodeId
122      *            the node id
123      * @param mac
124      *            the mac
125      * @return the instance identifier
126      */
127     public static InstanceIdentifier<LocalUcastMacs> createLocalUcastMacsInstanceIdentifier(NodeId nodeId,
128             String logicalSwitchName,
129             MacAddress mac) {
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));
134     }
135
136     /**
137      * Creates the remote mcast macs instance identifier.
138      *
139      * @param nodeId
140      *            the node id
141      * @param logicalSwitchName
142      *            the logical switch name
143      * @param mac
144      *            the mac
145      * @return the instance identifier
146      */
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));
153     }
154
155     /**
156      * Creates the remote mcast macs instance identifier.
157      *
158      * @param nodeId
159      *            the node id
160      * @param remoteMcastMacsKey
161      *            the remote mcast macs key
162      * @return the instance identifier
163      */
164     public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
165             RemoteMcastMacsKey remoteMcastMacsKey) {
166         return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
167                 .child(RemoteMcastMacs.class, remoteMcastMacsKey);
168     }
169
170     /**
171      * Creates the physical locator instance identifier.
172      *
173      * @param nodeId
174      *            the node id
175      * @param physicalLocatorAug
176      *            the physical locator aug
177      * @return the instance identifier
178      */
179     public static InstanceIdentifier<TerminationPoint> createPhysicalLocatorInstanceIdentifier(NodeId nodeId,
180             HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
181         return createInstanceIdentifier(nodeId).child(TerminationPoint.class,
182                 getTerminationPointKey(physicalLocatorAug));
183     }
184
185     /**
186      * Creates the physical port instance identifier.
187      *
188      * @param physicalSwitchNodeId
189      *            the physical switch node id
190      * @param phyPortName
191      *            the phy port name
192      * @return the instance identifier
193      */
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);
199     }
200
201     /**
202      * Creates the vlan binding instance identifier.
203      *
204      * @param physicalSwitchNodeId
205      *            the physical switch node id
206      * @param phyPortName
207      *            the phy port name
208      * @param vlanId
209      *            the vlan id
210      * @return the instance identifier
211      */
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)));
216     }
217
218     /**
219      * Gets the termination point key.
220      *
221      * @param phyLocator
222      *            the phy locator
223      * @return the termination point key
224      */
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));
231         }
232         return tpKey;
233     }
234
235     /**
236      * Creates the managed node id.
237      *
238      * @param nodeId
239      *            the node id
240      * @param physicalSwitchName
241      *            the physical switch name
242      * @return the node id
243      */
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);
248     }
249
250     /**
251      * Create logical switch.
252      *
253      * @param name
254      *            the name
255      * @param desc
256      *            the desc
257      * @param tunnelKey
258      *            the tunnel key
259      * @return the logical switches
260      */
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();
266     }
267
268     /**
269      * Create hwvtep physical locator augmentation.
270      *
271      * @param ipAddress
272      *            the ip address
273      * @return the hwvtep physical locator augmentation
274      */
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();
281     }
282
283     public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
284         Preconditions.checkNotNull(type);
285         if (type.isEmpty()) {
286             return EncapsulationTypeVxlanOverIpv4.class;
287         } else {
288             ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP
289                     .inverse();
290             return mapper.get(type);
291         }
292     }
293
294     /**
295      * Create remote ucast mac.
296      *
297      * @param nodeId
298      *            the node id
299      * @param mac
300      *            the mac
301      * @param ipAddress
302      *            the ip address
303      * @param logicalSwitchName
304      *            the logical switch name
305      * @param physicalLocatorAug
306      *            the physical locator aug
307      * @return the remote ucast macs
308      */
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));
315
316         RemoteUcastMacs remoteUcastMacs = new RemoteUcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
317                 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorRef(phyLocRef).build();
318         return remoteUcastMacs;
319     }
320
321     /**
322      * Creates the remote mcast mac.
323      *
324      * @param nodeId
325      *            the node id
326      * @param mac
327      *            the mac
328      * @param ipAddress
329      *            the ip address
330      * @param logicalSwitchName
331      *            the logical switch name
332      * @param lstPhysicalLocatorAug
333      *            the lst physical locator aug
334      * @return the remote mcast macs
335      */
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)));
340
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());
346         }
347
348         RemoteMcastMacs remoteMcastMacs = new RemoteMcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
349                 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorSet(lstLocatorSet).build();
350         return remoteMcastMacs;
351     }
352
353     /**
354      * Create vlan binding.
355      *
356      * @param nodeId
357      *            the node id
358      * @param vlanId
359      *            the vlan id
360      * @param logicalSwitchName
361      *            the logical switch name
362      * @return the vlan bindings
363      */
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());
369
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();
375     }
376
377 }