Imported vpnservice as a subtree
[netvirt.git] / vpnservice / 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     public static InstanceIdentifier<TerminationPoint> createTerminationPointId(NodeId nodeId,
87                                                                                 TerminationPointKey tpKey) {
88         return createInstanceIdentifier(nodeId).child(TerminationPoint.class, tpKey);
89     }
90     /**
91      * Creates the logical switches instance identifier.
92      *
93      * @param nodeId
94      *            the node id
95      * @param hwvtepNodeName
96      *            the hwvtep node name
97      * @return the instance identifier
98      */
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));
103     }
104
105     /**
106      * Creates the remote ucast macs instance identifier.
107      *
108      * @param nodeId
109      *            the node id
110      * @param mac
111      *            the mac
112      * @return the instance identifier
113      */
114     public static InstanceIdentifier<RemoteUcastMacs> createRemoteUcastMacsInstanceIdentifier(NodeId nodeId,
115             String logicalSwitchName,
116             MacAddress mac) {
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));
121     }
122
123     /**
124      * Creates the local ucast macs instance identifier.
125      *
126      * @param nodeId
127      *            the node id
128      * @param mac
129      *            the mac
130      * @return the instance identifier
131      */
132     public static InstanceIdentifier<LocalUcastMacs> createLocalUcastMacsInstanceIdentifier(NodeId nodeId,
133             String logicalSwitchName,
134             MacAddress mac) {
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));
139     }
140
141     /**
142      * Creates the remote mcast macs instance identifier.
143      *
144      * @param nodeId
145      *            the node id
146      * @param logicalSwitchName
147      *            the logical switch name
148      * @param mac
149      *            the mac
150      * @return the instance identifier
151      */
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));
158     }
159
160     /**
161      * Creates the remote mcast macs instance identifier.
162      *
163      * @param nodeId
164      *            the node id
165      * @param remoteMcastMacsKey
166      *            the remote mcast macs key
167      * @return the instance identifier
168      */
169     public static InstanceIdentifier<RemoteMcastMacs> createRemoteMcastMacsInstanceIdentifier(NodeId nodeId,
170             RemoteMcastMacsKey remoteMcastMacsKey) {
171         return createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
172                 .child(RemoteMcastMacs.class, remoteMcastMacsKey);
173     }
174
175     /**
176      * Creates the physical locator instance identifier.
177      *
178      * @param nodeId
179      *            the node id
180      * @param physicalLocatorAug
181      *            the physical locator aug
182      * @return the instance identifier
183      */
184     public static InstanceIdentifier<TerminationPoint> createPhysicalLocatorInstanceIdentifier(NodeId nodeId,
185             HwvtepPhysicalLocatorAugmentation physicalLocatorAug) {
186         return createInstanceIdentifier(nodeId).child(TerminationPoint.class,
187                 getTerminationPointKey(physicalLocatorAug));
188     }
189
190     /**
191      * Creates the physical port instance identifier.
192      *
193      * @param physicalSwitchNodeId
194      *            the physical switch node id
195      * @param phyPortName
196      *            the phy port name
197      * @return the instance identifier
198      */
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);
204     }
205
206     /**
207      * Creates the vlan binding instance identifier.
208      *
209      * @param physicalSwitchNodeId
210      *            the physical switch node id
211      * @param phyPortName
212      *            the phy port name
213      * @param vlanId
214      *            the vlan id
215      * @return the instance identifier
216      */
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)));
221     }
222
223     /**
224      * Gets the termination point key.
225      *
226      * @param phyLocator
227      *            the phy locator
228      * @return the termination point key
229      */
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));
236         }
237         return tpKey;
238     }
239
240     /**
241      * Creates the managed node id.
242      *
243      * @param nodeId
244      *            the node id
245      * @param physicalSwitchName
246      *            the physical switch name
247      * @return the node id
248      */
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);
253     }
254
255     /**
256      * Create logical switch.
257      *
258      * @param name
259      *            the name
260      * @param desc
261      *            the desc
262      * @param tunnelKey
263      *            the tunnel key
264      * @return the logical switches
265      */
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();
271     }
272
273     /**
274      * Create hwvtep physical locator augmentation.
275      *
276      * @param ipAddress
277      *            the ip address
278      * @return the hwvtep physical locator augmentation
279      */
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();
286     }
287
288     public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
289         Preconditions.checkNotNull(type);
290         if (type.isEmpty()) {
291             return EncapsulationTypeVxlanOverIpv4.class;
292         } else {
293             ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper = HwvtepSouthboundConstants.ENCAPS_TYPE_MAP
294                     .inverse();
295             return mapper.get(type);
296         }
297     }
298
299     /**
300      * Create remote ucast mac.
301      *
302      * @param nodeId
303      *            the node id
304      * @param mac
305      *            the mac
306      * @param ipAddress
307      *            the ip address
308      * @param logicalSwitchName
309      *            the logical switch name
310      * @param physicalLocatorAug
311      *            the physical locator aug
312      * @return the remote ucast macs
313      */
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));
320
321         RemoteUcastMacs remoteUcastMacs = new RemoteUcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
322                 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorRef(phyLocRef).build();
323         return remoteUcastMacs;
324     }
325
326     /**
327      * Creates the remote mcast mac.
328      *
329      * @param nodeId
330      *            the node id
331      * @param mac
332      *            the mac
333      * @param ipAddress
334      *            the ip address
335      * @param logicalSwitchName
336      *            the logical switch name
337      * @param lstPhysicalLocatorAug
338      *            the lst physical locator aug
339      * @return the remote mcast macs
340      */
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)));
345
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());
351         }
352
353         RemoteMcastMacs remoteMcastMacs = new RemoteMcastMacsBuilder().setMacEntryKey(new MacAddress(mac))
354                 .setIpaddr(ipAddress).setLogicalSwitchRef(lsRef).setLocatorSet(lstLocatorSet).build();
355         return remoteMcastMacs;
356     }
357
358     /**
359      * Create vlan binding.
360      *
361      * @param nodeId
362      *            the node id
363      * @param vlanId
364      *            the vlan id
365      * @param logicalSwitchName
366      *            the logical switch name
367      * @return the vlan bindings
368      */
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());
374
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();
380     }
381
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));
387         }
388         return tpKey;
389     }
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();
395         }
396         return tpKeyStr;
397     }
398
399 }