1
2
3
4
5
6
7
8 package org.opendaylight.lispflowmapping.lisp.util;
9
10 import java.util.List;
11
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;
32
33 import com.google.common.base.Preconditions;
34
35
36
37
38
39
40
41
42
43 public class LispAddressStringifier {
44
45 private static final String PREFIX_SEPARATOR = ":";
46
47
48
49
50 protected static int noAddrSeq = 0;
51
52
53
54
55
56
57
58
59
60
61
62 protected enum Destination {
63 USER,
64 URI,
65 URL;
66 }
67
68 public static String getString(LispAddress lispAddress) {
69 return getAddrString(Destination.USER, lispAddress);
70 }
71
72 public static String getURIString(LispAddress lispAddress) {
73 return getAddrString(Destination.URI, lispAddress);
74 }
75
76 public static String getURLString(LispAddress lispAddress) {
77 return getAddrString(Destination.URL, lispAddress);
78 }
79
80 private static String getAddrString(Destination dst, LispAddress lispAddress) {
81 Preconditions.checkNotNull(lispAddress, "lispAddress should not be null");
82 Address addr = lispAddress.getAddress();
83 String prefix = "";
84 String vni = "";
85 String address = "";
86
87 if (lispAddress.getVirtualNetworkId() != null) {
88 vni = "[" + lispAddress.getVirtualNetworkId().getValue() + "] ";
89 }
90
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
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);
138 } else {
139 return null;
140 }
141
142 if (dst == Destination.USER) {
143 return vni + address;
144 } else {
145 return prefix + address;
146 }
147
148 }
149
150 private static String getPrefixString(Destination dst, String prefix) {
151 if (dst == Destination.URL) {
152 return prefix.replace("/", "%2f");
153 } else {
154 return prefix;
155 }
156 }
157
158 protected static String getStringFromNoAddress(Destination dst, NoAddress addr) {
159
160 if (dst == Destination.USER) {
161 return "No Address Present";
162 } else {
163 return "" + noAddrSeq++;
164 }
165 }
166
167 protected static String getStringFromIpv4(Destination dst, Ipv4 addr) {
168
169 return addr.getIpv4().getValue();
170 }
171
172 protected static String getStringFromIpv4Prefix(Destination dst, Ipv4Prefix addr) {
173
174 String prefix = addr.getIpv4Prefix().getValue();
175 return getPrefixString(dst, prefix);
176 }
177
178 protected static String getStringFromIpv6(Destination dst, Ipv6 addr) {
179
180 return addr.getIpv6().getValue();
181 }
182
183 protected static String getStringFromIpv6Prefix(Destination dst, Ipv6Prefix addr) {
184
185 return addr.getIpv6Prefix().getValue();
186 }
187
188 protected static String getStringFromDistinguishedName(Destination dst, DistinguishedName addr) {
189
190 return addr.getDistinguishedName().getValue();
191 }
192
193 protected static String getStringFromAsNumber(Destination dst, AsNumber addr) {
194
195 return "AS" + addr.getAsNumber().getValue();
196 }
197 protected static String getStringFromAfiList(Destination dst, AfiList addr) {
198
199
200
201 List<SimpleAddress> addresses = addr.getAfiList().getAddressList();
202 StringBuilder sb = new StringBuilder("{");
203 boolean needComma = false;
204 for (SimpleAddress a : addresses) {
205 if (needComma) {
206 sb.append(",");
207 }
208 sb.append(a.getValue());
209 needComma = true;
210 }
211 sb.append("}");
212 return sb.toString();
213 }
214
215 protected static String getStringFromInstanceId(Destination dst, InstanceId addr) {
216
217
218
219 SimpleAddress pa = addr.getInstanceId().getAddress();
220 if (dst == Destination.USER) {
221 return "[" + addr.getInstanceId().getIid().getValue() + "] "
222 + LispSimpleAddressStringifier.getString(dst, pa);
223 } else {
224 return LispSimpleAddressStringifier.getString(dst, pa);
225 }
226 }
227
228 protected static String getStringFromApplicationData(Destination dst, ApplicationData addr) {
229
230
231
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();
239 }
240
241 protected static String getStringFromExplicitLocatorPath(Destination dst, ExplicitLocatorPath addr) {
242
243
244
245 List<Hop> hops = addr.getExplicitLocatorPath().getHop();
246 StringBuilder sb = new StringBuilder();
247 sb.append("{");
248 boolean needArrow = false;
249 for (Hop hop : hops) {
250 if (needArrow) {
251 sb.append("->");
252 }
253 sb.append(LispSimpleAddressStringifier.getString(dst, hop.getAddress()));
254 LrsBits lrs = hop.getLrsBits();
255 if (lrs.isLookup() || lrs.isRlocProbe() || lrs.isStrict()) {
256 sb.append("|");
257 }
258 if (lrs.isLookup()) {
259 sb.append("l");
260 }
261 if (lrs.isRlocProbe()) {
262 sb.append("p");
263 }
264 if (lrs.isStrict()) {
265 sb.append("s");
266 }
267 needArrow = true;
268 }
269 sb.append("}");
270 return sb.toString();
271 }
272
273 protected static String getStringFromSourceDestKey(Destination dst, SourceDestKey addr) {
274
275
276
277 return getPrefixString(dst, (new String(addr.getSourceDestKey().getSource().getValue()))
278 + "|" + getPrefixString(dst, new String(addr.getSourceDestKey().getDest().getValue())));
279 }
280
281 protected static String getStringFromKeyValueAddress(Destination dst, KeyValueAddress addr) {
282
283
284
285 return getPrefixString(dst, new String(addr.getKeyValueAddress().getKey().getValue())
286 + "=>" + getPrefixString(dst, new String(addr.getKeyValueAddress().getValue().getValue())));
287 }
288
289 protected static String getStringFromMac(Destination dst, Mac addr) {
290
291 return addr.getMac().getValue();
292 }
293
294 protected static String getStringFromServicePath(Destination dst, ServicePath addr) {
295
296
297
298 return getPrefixString(dst, new String(addr.getServicePath().getServicePathId().getValue() + "("
299 + addr.getServicePath().getServiceIndex() + ")"));
300 }
301
302 }