2 * Copyright (c) 2015 Cisco Systems, Inc. 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
8 package org.opendaylight.lispflowmapping.lisp.util;
10 import java.util.List;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiList;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ApplicationData;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AsNumber;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.DistinguishedName;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.InstanceId;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.KeyValueAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Mac;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.NoAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
29 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;
30 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;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
33 import com.google.common.base.Preconditions;
36 * Utility class with static methods returning string representations of
37 * supported LISP address types, both for use in URLs and for user friendly
40 * @author Lorand Jakab
43 public class LispAddressStringifier {
45 private static final String PREFIX_SEPARATOR = ":";
47 * In the unlikely event that a AFI 0 address is used a key, we use a
48 * sequence number to ensure uniqueness
50 protected static int noAddrSeq = 0;
52 * There are three possible destinations for rendering an address as a
55 * * General user interaction, like log and CLI output, GUI
56 * representation, etc.
57 * * As a URI string that is the entry key in the MD-SAL datastore
58 * * As a URL string that is the same as the URI string, except for
59 * characters not accepted in URLs are percent encoded (e.g. '/' is
62 protected enum Destination {
68 public static String getString(LispAddress lispAddress) {
69 return getAddrString(Destination.USER, lispAddress);
72 public static String getURIString(LispAddress lispAddress) {
73 return getAddrString(Destination.URI, lispAddress);
76 public static String getURLString(LispAddress lispAddress) {
77 return getAddrString(Destination.URL, lispAddress);
80 private static String getAddrString(Destination dst, LispAddress lispAddress) {
81 Preconditions.checkNotNull(lispAddress, "lispAddress should not be null");
82 Address addr = lispAddress.getAddress();
87 if (lispAddress.getVirtualNetworkId() != null) {
88 vni = "[" + lispAddress.getVirtualNetworkId().getValue() + "] ";
91 if (addr instanceof Ipv4) {
92 prefix = "ipv4" + PREFIX_SEPARATOR;
93 address = getStringFromIpv4(dst, (Ipv4) addr);
94 } else if (addr instanceof Ipv4Prefix) {
95 prefix = "ipv4" + PREFIX_SEPARATOR;
96 address = getStringFromIpv4Prefix(dst, (Ipv4Prefix) addr);
97 } else if (addr instanceof Ipv6) {
98 prefix = "ipv6" + PREFIX_SEPARATOR;
99 address = getStringFromIpv6(dst, (Ipv6) addr);
100 } else if (addr instanceof Ipv6Prefix) {
101 prefix = "ipv6" + PREFIX_SEPARATOR;
102 address = getStringFromIpv6Prefix(dst, (Ipv6Prefix) addr);
103 } else if (addr instanceof Mac) {
104 prefix = "mac" + PREFIX_SEPARATOR;
105 address = getStringFromMac(dst, (Mac) addr);
106 } else if (addr instanceof InstanceId) {
107 SimpleAddress pa = ((InstanceId)addr).getInstanceId().getAddress();
108 // Instance ID is a separate parent hierarchy, so we use the simple address prefix
109 prefix = LispSimpleAddressStringifier.getURLPrefix(pa) + PREFIX_SEPARATOR;
110 address = getStringFromInstanceId(dst, (InstanceId) addr);
111 } else if (addr instanceof NoAddress) {
112 prefix = "no" + PREFIX_SEPARATOR;
113 address = getStringFromNoAddress(dst, (NoAddress) addr);
114 } else if (addr instanceof DistinguishedName) {
115 prefix = "dn" + PREFIX_SEPARATOR;
116 address = getStringFromDistinguishedName(dst, (DistinguishedName) addr);
117 } else if (addr instanceof AsNumber) {
118 prefix = "as" + PREFIX_SEPARATOR;
119 address = getStringFromAsNumber(dst, (AsNumber) addr);
120 } else if (addr instanceof AfiList) {
121 prefix = "list" + PREFIX_SEPARATOR;
122 address = getStringFromAfiList(dst, (AfiList) addr);
123 } else if (addr instanceof ApplicationData) {
124 prefix = "appdata" + PREFIX_SEPARATOR;
125 address = getStringFromApplicationData(dst, (ApplicationData) addr);
126 } else if (addr instanceof ExplicitLocatorPath) {
127 prefix = "elp" + PREFIX_SEPARATOR;
128 address = getStringFromExplicitLocatorPath(dst, (ExplicitLocatorPath) addr);
129 } else if (addr instanceof SourceDestKey) {
130 prefix = "srcdst" + PREFIX_SEPARATOR;
131 address = getStringFromSourceDestKey(dst, (SourceDestKey) addr);
132 } else if (addr instanceof KeyValueAddress) {
133 prefix = "kv" + PREFIX_SEPARATOR;
134 address = getStringFromKeyValueAddress(dst, (KeyValueAddress) addr);
135 } else if (addr instanceof ServicePath) {
136 prefix = "sp" + PREFIX_SEPARATOR;
137 address = getStringFromServicePath(dst, (ServicePath) addr);
142 if (dst == Destination.USER) {
143 return vni + address;
145 return prefix + address;
150 private static String getPrefixString(Destination dst, String prefix) {
151 if (dst == Destination.URL) {
152 return prefix.replace("/", "%2f");
158 protected static String getStringFromNoAddress(Destination dst, NoAddress addr) {
160 if (dst == Destination.USER) {
161 return "No Address Present";
163 return "" + noAddrSeq++;
167 protected static String getStringFromIpv4(Destination dst, Ipv4 addr) {
169 return addr.getIpv4().getValue();
172 protected static String getStringFromIpv4Prefix(Destination dst, Ipv4Prefix addr) {
174 String prefix = addr.getIpv4Prefix().getValue();
175 return getPrefixString(dst, prefix);
178 protected static String getStringFromIpv6(Destination dst, Ipv6 addr) {
180 return addr.getIpv6().getValue();
183 protected static String getStringFromIpv6Prefix(Destination dst, Ipv6Prefix addr) {
185 return addr.getIpv6Prefix().getValue();
188 protected static String getStringFromDistinguishedName(Destination dst, DistinguishedName addr) {
189 // AFI = 17; Distinguished Name
190 return addr.getDistinguishedName().getValue();
193 protected static String getStringFromAsNumber(Destination dst, AsNumber addr) {
194 // AFI = 18; Autonomous System Number
195 return "AS" + addr.getAsNumber().getValue();
197 protected static String getStringFromAfiList(Destination dst, AfiList addr) {
198 // AFI 16387, LCAF Type 1; Address List
199 // Example rendering:
200 // {192.0.2.1,192.0.2.2,2001:db8::1}
201 List<SimpleAddress> addresses = addr.getAfiList().getAddressList();
202 StringBuilder sb = new StringBuilder("{");
203 boolean needComma = false;
204 for (SimpleAddress a : addresses) {
208 sb.append(a.getValue());
212 return sb.toString();
215 protected static String getStringFromInstanceId(Destination dst, InstanceId addr) {
216 // AFI = 16387, LCAF Type 2; Instance ID
217 // Example rendering:
218 // [223] 192.0.2.0/24
219 SimpleAddress pa = addr.getInstanceId().getAddress();
220 if (dst == Destination.USER) {
221 return "[" + addr.getInstanceId().getIid().getValue() + "] "
222 + LispSimpleAddressStringifier.getString(dst, pa);
224 return LispSimpleAddressStringifier.getString(dst, pa);
228 protected static String getStringFromApplicationData(Destination dst, ApplicationData addr) {
229 // AFI = 16387, LCAF Type 4; Application Data
230 // Example rendering:
231 // 192.0.2.1!128!17!80-81!6667-7000
232 return LispSimpleAddressStringifier.getString(dst, addr.getApplicationData().getAddress())
233 + "!" + addr.getApplicationData().getIpTos()
234 + "!" + addr.getApplicationData().getProtocol()
235 + "!" + addr.getApplicationData().getLocalPortLow()
236 + "-" + addr.getApplicationData().getLocalPortHigh()
237 + "!" + addr.getApplicationData().getRemotePortLow()
238 + "-" + addr.getApplicationData().getRemotePortHigh();
241 protected static String getStringFromExplicitLocatorPath(Destination dst, ExplicitLocatorPath addr) {
242 // AFI = 16387, LCAF Type 10, Explicit Locator Path
243 // Example rendering:
244 // {192.0.2.1->192.0.2.2|lps->192.0.2.3}
245 List<Hop> hops = addr.getExplicitLocatorPath().getHop();
246 StringBuilder sb = new StringBuilder();
248 boolean needArrow = false;
249 for (Hop hop : hops) {
253 sb.append(LispSimpleAddressStringifier.getString(dst, hop.getAddress()));
254 LrsBits lrs = hop.getLrsBits();
255 if (lrs.isLookup() || lrs.isRlocProbe() || lrs.isStrict()) {
258 if (lrs.isLookup()) {
261 if (lrs.isRlocProbe()) {
264 if (lrs.isStrict()) {
270 return sb.toString();
273 protected static String getStringFromSourceDestKey(Destination dst, SourceDestKey addr) {
274 // AFI = 16387, LCAF Type 12, Source/Destination Key
275 // Example rendering:
276 // 192.0.2.1/32|192.0.2.2/32
277 return getPrefixString(dst, (new String(addr.getSourceDestKey().getSource().getValue()))
278 + "|" + getPrefixString(dst, new String(addr.getSourceDestKey().getDest().getValue())));
281 protected static String getStringFromKeyValueAddress(Destination dst, KeyValueAddress addr) {
282 // AFI = 16387, LCAF Type 15, Key/Value Address Pair
283 // Example rendering:
284 // 192.0.2.1=>192.0.2.2
285 return getPrefixString(dst, new String(addr.getKeyValueAddress().getKey().getValue())
286 + "=>" + getPrefixString(dst, new String(addr.getKeyValueAddress().getValue().getValue())));
289 protected static String getStringFromMac(Destination dst, Mac addr) {
291 return addr.getMac().getValue();
294 protected static String getStringFromServicePath(Destination dst, ServicePath addr) {
295 // AFI = 16387; LCAF http://tools.ietf.org/html/draft-ermagan-lisp-nsh-00
296 // Example rendering:
298 return getPrefixString(dst, new String(addr.getServicePath().getServicePathId().getValue() + "("
299 + addr.getServicePath().getServiceIndex() + ")"));