72bb09d9448b83862ad7294b396c06d64f208e20
[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     public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
137             String logicalSwitchName,
138             MacAddress mac) {
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));
143     }
144
145     /**
146      * Creates the remote mcast macs instance identifier.
147      *
148      * @param nodeId
149      *            the node id
150      * @param remoteMcastMacsKey
151      *            the remote mcast macs key
152      * @return the instance identifier
153      */
154     public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
155             RemoteMcastMacsKey remoteMcastMacsKey) {
156         return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
157                 .child(RemoteMcastMacs.class, remoteMcastMacsKey);
158     }
159
160     /**
161      * Creates the physical locator instance identifier.
162      *
163      * @param nodeId
164      *            the node id
165      * @param physicalLocatorAug
166      *            the physical locator aug
167      * @return the instance identifier
168      */
169     public static InstanceIdentifier<TerminationPoint> createPhysicalLocatorInstanceIdentifier(NodeId nodeId,
170             HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
171         return createInstanceIdentifier(nodeId).child(TerminationPoint.class,
172                 getTerminationPointKey(physicalLocatorAug));
173     }
174
175     /**
176      * Creates the physical port instance identifier.
177      *
178      * @param physicalSwitchNodeId
179      *            the physical switch node id
180      * @param phyPortName
181      *            the phy port name
182      * @return the instance identifier
183      */
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);
189     }
190
191     /**
192      * Creates the vlan binding instance identifier.
193      *
194      * @param physicalSwitchNodeId
195      *            the physical switch node id
196      * @param phyPortName
197      *            the phy port name
198      * @param vlanId
199      *            the vlan id
200      * @return the instance identifier
201      */
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)));
206     }
207
208     /**
209      * Gets the termination point key.
210      *
211      * @param phyLocator
212      *            the phy locator
213      * @return the termination point key
214      */
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));
221         }
222         return tpKey;
223     }
224
225     /**
226      * Creates the managed node id.
227      *
228      * @param nodeId
229      *            the node id
230      * @param physicalSwitchName
231      *            the physical switch name
232      * @return the node id
233      */
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);
238     }
239
240     /**
241      * Create logical switch.
242      *
243      * @param name
244      *            the name
245      * @param desc
246      *            the desc
247      * @param tunnelKey
248      *            the tunnel key
249      * @return the logical switches
250      */
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();
256     }
257
258     /**
259      * Create hwvtep physical locator augmentation.
260      *
261      * @param ipAddress
262      *            the ip address
263      * @return the hwvtep physical locator augmentation
264      */
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();
271     }
272
273     public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
274         Preconditions.checkNotNull(type);
275         if (type.isEmpty()) {
276             return EncapsulationTypeVxlanOverIpv4.class;
277         } else {
278             ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP
279                     .inverse();
280             return mapper.get(type);
281         }
282     }
283
284     /**
285      * Create remote ucast mac.
286      *
287      * @param nodeId
288      *            the node id
289      * @param mac
290      *            the mac
291      * @param ipAddress
292      *            the ip address
293      * @param logicalSwitchName
294      *            the logical switch name
295      * @param physicalLocatorAug
296      *            the physical locator aug
297      * @return the remote ucast macs
298      */
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));
305
306         RemoteUcastMacs remoteUcastMacs = new RemoteUcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
307                 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorRef(phyLocRef).build();
308         return remoteUcastMacs;
309     }
310
311     /**
312      * Creates the remote mcast mac.
313      *
314      * @param nodeId
315      *            the node id
316      * @param mac
317      *            the mac
318      * @param ipAddress
319      *            the ip address
320      * @param logicalSwitchName
321      *            the logical switch name
322      * @param lstPhysicalLocatorAug
323      *            the lst physical locator aug
324      * @return the remote mcast macs
325      */
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)));
330
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());
336         }
337
338         RemoteMcastMacs remoteMcastMacs = new RemoteMcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
339                 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorSet(lstLocatorSet).build();
340         return remoteMcastMacs;
341     }
342
343     /**
344      * Create vlan binding.
345      *
346      * @param nodeId
347      *            the node id
348      * @param vlanId
349      *            the vlan id
350      * @param logicalSwitchName
351      *            the logical switch name
352      * @return the vlan bindings
353      */
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());
359
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();
365     }
366
367 }