1
2
3
4
5
6
7
8 package org.opendaylight.lispflowmapping.lisp.util;
9
10 import com.google.common.net.InetAddresses;
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.regex.Pattern;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AsNumberAfi;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.DistinguishedNameAfi;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.DistinguishedNameType;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6Afi;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6PrefixAfi;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.KeyValueAddressLcaf;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.MacAfi;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.NoAddressAfi;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ServicePathIdType;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ServicePathLcaf;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AsNumberBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.DistinguishedNameBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Builder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6PrefixBuilder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.KeyValueAddress;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.MacBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.NoAddressBuilder;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePathBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.key.value.address.KeyValueAddressBuilder;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePath;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKey;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKeyBuilder;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.ExplicitLocatorPathBuilder;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop.LrsBits;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.HopBuilder;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IetfInetUtil;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72
73 public final class LispAddressUtil {
74 protected static final Logger LOG = LoggerFactory.getLogger(LispAddressUtil.class);
75
76 public static final short STARTING_SERVICE_INDEX = 255;
77 private static final Pattern IP4_PATTERN =
78 Pattern.compile("(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])", Pattern.CASE_INSENSITIVE);
79 private static final Pattern IP6_PATTERN =
80 Pattern.compile("([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}", Pattern.CASE_INSENSITIVE);
81
82
83 private LispAddressUtil() {
84 }
85
86 public static Class<? extends LispAddressFamily> addressTypeFromSimpleAddress(SimpleAddress address) {
87 if (address.getIpAddress() != null) {
88 return addressTypeFromIpAddress(address.getIpAddress());
89 } else if (address.getIpPrefix() != null) {
90 return addressTypeFromIpPrefix(address.getIpPrefix());
91 } else if (address.getMacAddress() != null) {
92 return MacAfi.class;
93 } else if (address.getDistinguishedNameType() != null) {
94 return DistinguishedNameAfi.class;
95 } else if (address.getAsNumber() != null) {
96 return AsNumberAfi.class;
97 }
98 return null;
99 }
100
101 public static Address addressFromSimpleAddress(SimpleAddress address) {
102 if (address.getIpAddress() != null) {
103 return addressFromIpAddress(address.getIpAddress());
104 } else if (address.getIpPrefix() != null) {
105 return addressFromIpPrefix(address.getIpPrefix());
106 } else if (address.getMacAddress() != null) {
107 return addressFromMacAddress(address.getMacAddress());
108 } else if (address.getDistinguishedNameType() != null) {
109 return addressFromDistinguishedName(address.getDistinguishedNameType());
110 } else if (address.getAsNumber() != null) {
111 return addressFromAsNumber(address.getAsNumber());
112 }
113 return null;
114 }
115
116 public static Class<? extends LispAddressFamily> addressTypeFromInet(InetAddress address) {
117 if (address instanceof Inet4Address) {
118 return Ipv4Afi.class;
119 } else if (address instanceof Inet6Address) {
120 return Ipv6Afi.class;
121 }
122 return null;
123 }
124
125 public static Address addressFromInet(InetAddress address) {
126 if (address instanceof Inet4Address) {
127 return (Address) new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder()
128 .setIpv4(IetfInetUtil.INSTANCE.ipv4AddressFor(address)).build();
129 } else if (address instanceof Inet6Address) {
130 return (Address) new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Builder()
131 .setIpv6(IetfInetUtil.INSTANCE.ipv6AddressFor(address)).build();
132 }
133 return null;
134 }
135
136 public static Class<? extends LispAddressFamily> addressTypeFromIpAddress(IpAddress address) {
137 if (address == null) {
138 return null;
139 } else if (address.getIpv4Address() != null) {
140 return Ipv4Afi.class;
141 } else if (address.getIpv6Address() != null) {
142 return Ipv6Afi.class;
143 }
144 return null;
145 }
146
147 public static Address addressFromIpAddress(IpAddress address) {
148 if (address == null) {
149 return null;
150 } else if (address.getIpv4Address() != null) {
151 return (Address) new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder().setIpv4(address.getIpv4Address()).build();
152 } else if (address.getIpv6Address() != null) {
153 return (Address) new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Builder().setIpv6(address.getIpv6Address()).build();
154 }
155 return null;
156 }
157
158 public static Class<? extends LispAddressFamily> addressTypeFromIpPrefix(IpPrefix address) {
159 if (address == null) {
160 return null;
161 } else if (address.getIpv4Prefix() != null) {
162 return Ipv4PrefixAfi.class;
163 } else if (address.getIpv6Prefix() != null) {
164 return Ipv6PrefixAfi.class;
165 }
166 return null;
167 }
168
169 public static Address addressFromIpPrefix(IpPrefix address) {
170 if (address == null) {
171 return null;
172 } else if (address.getIpv4Prefix() != null) {
173 return (Address) new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder().setIpv4Prefix(address.getIpv4Prefix()).build();
174 } else if (address.getIpv6Prefix() != null) {
175 return (Address) new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6PrefixBuilder().setIpv6Prefix(address.getIpv6Prefix()).build();
176 }
177 return null;
178 }
179
180 public static Address addressFromMacAddress(MacAddress address) {
181 if (address == null) {
182 return null;
183 } else {
184 return (Address) new MacBuilder().setMac(address).build();
185 }
186 }
187
188 public static Address addressFromServicePath(ServicePath address) {
189 if (address == null) {
190 return null;
191 } else {
192 return (Address) new ServicePathBuilder().setServicePath(address).build();
193 }
194 }
195
196 public static Address addressFromDistinguishedName(DistinguishedNameType address) {
197 if (address == null) {
198 return null;
199 } else {
200 return (Address) new DistinguishedNameBuilder().setDistinguishedName(address).build();
201 }
202 }
203
204 public static Address addressFromAsNumber(AsNumber address) {
205 if (address == null) {
206 return null;
207 } else {
208 return (Address) new AsNumberBuilder().setAsNumber(address).build();
209 }
210 }
211 public static Rloc toRloc(SimpleAddress address) {
212 RlocBuilder builder = new RlocBuilder();
213 builder.setAddressType(addressTypeFromSimpleAddress(address));
214 builder.setVirtualNetworkId(null);
215 builder.setAddress(addressFromSimpleAddress(address));
216 return builder.build();
217 }
218
219 public static Rloc toRloc(InetAddress address) {
220 RlocBuilder builder = new RlocBuilder();
221 builder.setAddressType(addressTypeFromInet(address));
222 builder.setVirtualNetworkId(null);
223 builder.setAddress(addressFromInet(address));
224 return builder.build();
225 }
226
227 public static Rloc toRloc(Ipv4Address address) {
228 RlocBuilder builder = new RlocBuilder();
229 builder.setAddressType(Ipv4Afi.class);
230 builder.setVirtualNetworkId(null);
231 builder.setAddress((Address) new Ipv4Builder().setIpv4(address).build());
232 return builder.build();
233 }
234
235 public static Rloc toRloc(Ipv6Address address) {
236 RlocBuilder builder = new RlocBuilder();
237 builder.setAddressType(Ipv6Afi.class);
238 builder.setVirtualNetworkId(null);
239 builder.setAddress((Address) new Ipv6Builder().setIpv6(address).build());
240 return builder.build();
241 }
242
243 public static Rloc asIpv4Rloc(String address) {
244 return toRloc(new Ipv4Address(address));
245 }
246
247 public static Rloc asIpv6Rloc(String address) {
248 return toRloc(new Ipv6Address(address));
249 }
250
251 public static Eid toEid(Ipv4Prefix prefix, InstanceIdType vni) {
252 EidBuilder builder = new EidBuilder();
253 builder.setAddressType(Ipv4PrefixAfi.class);
254 builder.setVirtualNetworkId(vni);
255 builder.setAddress((Address) new Ipv4PrefixBuilder().setIpv4Prefix(prefix).build());
256 return builder.build();
257 }
258
259 public static Eid toEid(Ipv4Address prefix, InstanceIdType vni) {
260 EidBuilder builder = new EidBuilder();
261 builder.setAddressType(Ipv4Afi.class);
262 builder.setVirtualNetworkId(vni);
263 builder.setAddress((Address) new Ipv4Builder().setIpv4(prefix).build());
264 return builder.build();
265 }
266
267
268 public static Eid toEidNoVni(IpPrefix prefix) {
269 EidBuilder builder = new EidBuilder();
270 builder.setAddress(addressFromIpPrefix(prefix));
271 builder.setAddressType(addressTypeFromIpPrefix(prefix));
272 return builder.build();
273 }
274
275 public static Eid toEid(IpPrefix prefix, InstanceIdType vni) {
276 EidBuilder builder = new EidBuilder();
277 builder.setAddress(addressFromIpPrefix(prefix));
278 builder.setAddressType(addressTypeFromIpPrefix(prefix));
279 builder.setVirtualNetworkId(vni);
280 return builder.build();
281 }
282
283 public static Eid toIpPrefixEid(IpAddress addr, int vni) {
284
285 int mask = addressTypeFromIpAddress(addr) == Ipv4Afi.class ? 32 : 128;
286 IpPrefix prefix = asIpPrefix(String.valueOf(addr.getValue()), mask);
287
288 return toEidNoVni(prefix);
289 }
290
291 public static Eid asEid(SimpleAddress address, InstanceIdType vni) {
292 EidBuilder builder = new EidBuilder();
293 builder.setAddressType(addressTypeFromSimpleAddress(address));
294 builder.setVirtualNetworkId(vni);
295
296 builder.setAddress(addressFromSimpleAddress(address));
297 return builder.build();
298 }
299
300 public static Eid asIpv4PrefixEid(String prefix) {
301 return toEid(new Ipv4Prefix(prefix), null);
302 }
303
304 public static Eid asIpv4Eid(String address) {
305 return toEid(new Ipv4Address(address), null);
306 }
307
308 public static Eid toEid(Ipv6Prefix prefix, InstanceIdType vni) {
309 EidBuilder builder = new EidBuilder();
310 builder.setAddressType(Ipv6PrefixAfi.class);
311 builder.setVirtualNetworkId(vni);
312 builder.setAddress((Address) new Ipv6PrefixBuilder().setIpv6Prefix(prefix).build());
313 return builder.build();
314 }
315
316 public static Eid toEid(Ipv6Address prefix, InstanceIdType vni) {
317 EidBuilder builder = new EidBuilder();
318 builder.setAddressType(Ipv6Afi.class);
319 builder.setVirtualNetworkId(vni);
320 builder.setAddress((Address) new Ipv6Builder().setIpv6(prefix).build());
321 return builder.build();
322 }
323
324 public static Eid asIpv6Eid(String address, long vni) {
325 return toEid(new Ipv6Address(address), new InstanceIdType(vni));
326 }
327
328 public static Eid asIpv6PrefixEid(String prefix) {
329 return toEid(new Ipv6Prefix(prefix), null);
330 }
331
332 public static Eid asIpv6Eid(String address) {
333 return toEid(new Ipv6Address(address), null);
334 }
335
336 public static Eid asIpv4Eid(String address, long vni) {
337 return toEid(new Ipv4Address(address), new InstanceIdType(vni));
338 }
339
340 public static int ipVersionFromString(String ip) {
341 if (IP4_PATTERN.matcher(ip).matches()) {
342 return 4;
343 } else if (IP6_PATTERN.matcher(ip).matches()) {
344 return 6;
345 } else {
346 return 0;
347 }
348 }
349
350 public static IpPrefix asIpPrefix(String addr, int mask) {
351 int version = ipVersionFromString(addr);
352 if (version == 4 && (mask >= 0 && mask <= 32)) {
353 return new IpPrefix(new Ipv4Prefix(addr + "/" + mask));
354 } else if (version == 6 && (mask >=0 && mask <= 128)) {
355 return new IpPrefix(new Ipv6Prefix(addr + "/" + mask));
356 } else {
357 return null;
358 }
359 }
360
361 public static Eid asIpv4PrefixEid(Ipv4Address addr, InstanceIdType vni) {
362 return toEid(new IpPrefix(IetfInetUtil.INSTANCE.ipv4PrefixFor(addr)), vni);
363 }
364
365 public static Eid asIpv6PrefixEid(Ipv6Address addr, InstanceIdType vni) {
366 return toEid(new IpPrefix(IetfInetUtil.INSTANCE.ipv6PrefixFor(addr)), vni);
367 }
368
369 public static Eid asIpv4PrefixEid(Eid eid, Inet4Address address, short mask) {
370 EidBuilder builder = new EidBuilder();
371 builder.setAddressType(eid.getAddressType());
372 builder.setVirtualNetworkId(eid.getVirtualNetworkId());
373 builder.setAddress(new Ipv4PrefixBuilder().setIpv4Prefix(IetfInetUtil.INSTANCE.ipv4PrefixFor(address, mask)).build());
374 return builder.build();
375 }
376
377 public static Eid asIpv6PrefixEid(Eid eid, Inet6Address address, short mask) {
378 EidBuilder builder = new EidBuilder();
379 builder.setAddressType(eid.getAddressType());
380 builder.setVirtualNetworkId(eid.getVirtualNetworkId());
381 builder.setAddress(new Ipv6PrefixBuilder().setIpv6Prefix(IetfInetUtil.INSTANCE.ipv6PrefixFor(address, mask)).build());
382 return builder.build();
383 }
384
385 public static Eid asServicePathEid(long vni, long servicePathId, short serviceIndex) {
386 EidBuilder eb = new EidBuilder();
387 eb.setAddressType(ServicePathLcaf.class);
388 if (vni >= 0) {
389 eb.setVirtualNetworkId(new InstanceIdType(vni));
390 }
391 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePathBuilder spb = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePathBuilder();
392 spb.setServicePathId(new ServicePathIdType(servicePathId));
393 spb.setServiceIndex(serviceIndex);
394 return eb.setAddress(addressFromServicePath(spb.build())).build();
395 }
396
397 public static Eid toEid(MacAddress mac, InstanceIdType vni) {
398 EidBuilder builder = new EidBuilder();
399 builder.setAddressType(MacAfi.class);
400 builder.setVirtualNetworkId(vni);
401 builder.setAddress((Address) new MacBuilder().setMac(mac).build());
402 return builder.build();
403 }
404
405 public static Eid asMacEid(String address, long vni) {
406 return toEid(new MacAddress(address), new InstanceIdType(vni));
407 }
408
409 public static Eid asMacEid(String address) {
410 return toEid(new MacAddress(address), null);
411 }
412
413 public static Eid toEid(DistinguishedNameType dn, InstanceIdType vni) {
414 EidBuilder builder = new EidBuilder();
415 builder.setAddressType(DistinguishedNameAfi.class);
416 builder.setVirtualNetworkId(vni);
417 builder.setAddress((Address) new DistinguishedNameBuilder().setDistinguishedName(dn).build());
418 return builder.build();
419 }
420
421 public static Eid asDistinguishedNameEid(String address, long vni) {
422 return toEid(new MacAddress(address), new InstanceIdType(vni));
423 }
424
425 public static Eid asDistinguishedNameEid(String address) {
426 return toEid(new DistinguishedNameType(address), null);
427 }
428
429 public static Eid asKeyValueAddressEid(SimpleAddress key, SimpleAddress value) {
430 KeyValueAddressBuilder kvab = new KeyValueAddressBuilder();
431 kvab.setKey(key);
432 kvab.setValue(value);
433 KeyValueAddress address = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.KeyValueAddressBuilder()
434 .setKeyValueAddress(kvab.build()).build();
435 EidBuilder builder = new EidBuilder();
436 builder.setAddressType(KeyValueAddressLcaf.class);
437 builder.setVirtualNetworkId(null);
438 builder.setAddress((Address) address);
439 return builder.build();
440 }
441
442 public static Rloc asKeyValueAddressRloc(SimpleAddress key, SimpleAddress value) {
443 KeyValueAddressBuilder kvab = new KeyValueAddressBuilder();
444 kvab.setKey(key);
445 kvab.setValue(value);
446 KeyValueAddress address = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.KeyValueAddressBuilder()
447 .setKeyValueAddress(kvab.build()).build();
448 RlocBuilder builder = new RlocBuilder();
449 builder.setAddressType(KeyValueAddressLcaf.class);
450 builder.setVirtualNetworkId(null);
451 builder.setAddress((Address) address);
452 return builder.build();
453 }
454
455 public static Rloc asKeyValueAddress(String key, SimpleAddress value) {
456 return asKeyValueAddressRloc(new SimpleAddress(new DistinguishedNameType(key)), value);
457 }
458
459 public static SourceDestKey asSrcDst(String src, String dst, int smask, int dmask) {
460 SourceDestKeyBuilder builder = new SourceDestKeyBuilder();
461 builder.setSource(new SimpleAddress(asIpPrefix(src, smask)));
462 builder.setDest(new SimpleAddress(asIpPrefix(dst, dmask)));
463 return builder.build();
464 }
465
466 public static Eid asSrcDstEid(String src, String dst, int smask, int dmask, int vni) {
467 EidBuilder builder = new EidBuilder();
468 builder.setAddressType(SourceDestKeyLcaf.class);
469 builder.setVirtualNetworkId(new InstanceIdType(Long.valueOf(vni)));
470 builder.setAddress(
471 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKeyBuilder()
472 .setSourceDestKey(asSrcDst(src, dst, smask, dmask)).build());
473
474 return builder.build();
475 }
476
477 public static Eid asSrcDstEid(SourceDestKey sd, InstanceIdType vni) {
478 EidBuilder builder = new EidBuilder();
479 builder.setAddressType(SourceDestKeyLcaf.class);
480 builder.setVirtualNetworkId(vni);
481 builder.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types
482 .rev151105.lisp.address.address.SourceDestKeyBuilder().setSourceDestKey(sd).build());
483 return builder.build();
484 }
485
486 public static Rloc asTeLcafRloc(List<IpAddress> hopList) {
487 RlocBuilder teBuilder = new RlocBuilder();
488 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder elpBuilder =
489 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder();
490 ExplicitLocatorPathBuilder teAddrBuilder = new ExplicitLocatorPathBuilder();
491 teAddrBuilder.setHop(new ArrayList<Hop>());
492 for (IpAddress hop : hopList) {
493 HopBuilder hopBuilder = new HopBuilder();
494 hopBuilder.setAddress(new SimpleAddress(hop));
495 hopBuilder.setHopId("Hop " + teAddrBuilder.getHop().size());
496 hopBuilder.setLrsBits(new LrsBits(false, false, false));
497 teAddrBuilder.getHop().add(hopBuilder.build());
498 }
499
500 elpBuilder.setExplicitLocatorPath(teAddrBuilder.build());
501 teBuilder.setAddress(elpBuilder.build());
502 teBuilder.setAddressType(ExplicitLocatorPathLcaf.class);
503 return teBuilder.build();
504 }
505
506 public static List<LocatorRecord> asLocatorRecords(List<Rloc> locators) {
507 List<LocatorRecord> locatorRecords = new ArrayList<LocatorRecord>();
508 for (Rloc locator : locators) {
509 LocatorRecordBuilder locatorBuilder = new LocatorRecordBuilder();
510 locatorBuilder.setLocalLocator(false).setRlocProbed(false).setWeight((short) 1).setPriority((short) 1)
511 .setMulticastWeight((short) 1).setMulticastPriority((short) 1).setRouted(true)
512 .setRloc(locator).setLocatorId("SFC_LISP").build();
513 locatorRecords.add(locatorBuilder.build());
514 }
515 return locatorRecords;
516 }
517
518 public static Eid getNoAddressEid() {
519 EidBuilder builder = new EidBuilder();
520 builder.setAddressType(NoAddressAfi.class);
521 builder.setVirtualNetworkId(null);
522 builder.setAddress(new NoAddressBuilder().setNoAddress(true).build());
523 return builder.build();
524 }
525
526 public static InetAddress ipAddressToInet(Address addr) {
527 if (addr instanceof Ipv4) {
528 return InetAddresses.forString(((Ipv4) addr).getIpv4().getValue());
529 } else if (addr instanceof Ipv6) {
530 return InetAddresses.forString(((Ipv6) addr).getIpv6().getValue());
531 } else {
532 return null;
533 }
534 }
535
536 public static int compareInetAddresses(InetAddress a, InetAddress b) {
537 int i;
538 if (a instanceof Inet4Address && b instanceof Inet6Address) {
539 return -1;
540 } else if (a instanceof Inet6Address && b instanceof Inet4Address) {
541 return 1;
542 } else if (a instanceof Inet4Address && b instanceof Inet4Address) {
543 byte[] aBytes = ((Inet4Address) a).getAddress();
544 byte[] bBytes = ((Inet4Address) b).getAddress();
545 for (i = 0; i < 4; i++) {
546 if (aBytes[i] < bBytes[i]) {
547 return -1;
548 } else if (aBytes[i] > bBytes[i]) {
549 return 1;
550 }
551 }
552 return 0;
553 } else if (a instanceof Inet6Address && b instanceof Inet6Address) {
554 byte[] aBytes = ((Inet6Address) a).getAddress();
555 byte[] bBytes = ((Inet6Address) b).getAddress();
556 for (i = 0; i < 16; i++) {
557 if (aBytes[i] < bBytes[i]) {
558 return -1;
559 } else if (aBytes[i] > bBytes[i]) {
560 return 1;
561 }
562 }
563 return 0;
564 }
565 return 0;
566 }
567 }