0b7faba31bdd7381586bcc4f9c7eb92d1cf7b13b
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / util / EidTranslator.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.lisp.translate.util;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType;
21 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.IPV4;
22 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.IPV6;
23 import static io.fd.honeycomb.lisp.translate.read.dump.executor.params.MappingsDumpParams.EidType.MAC;
24
25 import io.fd.honeycomb.translate.vpp.util.AddressTranslator;
26 import java.util.Arrays;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
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.LispAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.MacAfi;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Builder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Mac;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.MacBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.adjacencies.grouping.adjacencies.adjacency.LocalEid;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.adjacencies.grouping.adjacencies.adjacency.RemoteEid;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.Eid;
45
46
47 /**
48  * Trait providing converting logic for eid's
49  */
50 public interface EidTranslator extends AddressTranslator, EidMetadataProvider {
51
52
53     default byte getPrefixLength(LocalEid address) {
54         return resolverPrefixLength(address.getAddress());
55     }
56
57     default byte getPrefixLength(RemoteEid address) {
58         return resolverPrefixLength(address.getAddress());
59     }
60
61     default byte getPrefixLength(
62             org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid address) {
63         return resolverPrefixLength(address.getAddress());
64     }
65
66     default byte getPrefixLength(
67             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.Eid address) {
68         return resolverPrefixLength(address.getAddress());
69     }
70
71     default byte getPrefixLength(
72             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid address) {
73         return resolverPrefixLength(address.getAddress());
74     }
75
76     static byte resolverPrefixLength(Address address) {
77
78         switch (resolveType(address)) {
79             case IPV4:
80                 return 32;
81             case IPV6:
82                 return (byte) 128;
83             case MAC:
84                 return 0;
85             default:
86                 throw new IllegalArgumentException("Illegal type");
87         }
88     }
89
90     default Eid getArrayAsEidLocal(@Nonnull final EidType type, final byte[] address, final int vni) {
91
92         switch (type) {
93             case IPV4: {
94                 return newLocalEidBuilder(Ipv4Afi.class, vni).setAddress(
95                         new Ipv4Builder().setIpv4(arrayToIpv4AddressNoZoneReversed(address)).build())
96                         .build();
97             }
98             case IPV6: {
99                 return newLocalEidBuilder(Ipv6Afi.class, vni).setAddress(
100                         new Ipv6Builder().setIpv6(arrayToIpv6AddressNoZoneReversed(address)).build())
101                         .build();
102             }
103             case MAC: {
104                 return newLocalEidBuilder(MacAfi.class, vni).setAddress(
105                         new MacBuilder().setMac(new MacAddress(byteArrayToMacSeparated(address)))
106                                 .build()).build();
107             }
108             default: {
109                 throw new IllegalStateException("Unknown type detected");
110             }
111         }
112     }
113
114     default org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid getArrayAsEidRemote(
115             @Nonnull final EidType type, final byte[] address, final int vni) {
116
117         switch (type) {
118             case IPV4: {
119                 return newRemoteEidBuilder(Ipv4Afi.class, vni)
120                         .setAddress(
121                                 new Ipv4Builder().setIpv4(arrayToIpv4AddressNoZoneReversed(address))
122                                         .build())
123                         .build();
124             }
125             case IPV6: {
126                 return newRemoteEidBuilder(Ipv6Afi.class, vni)
127                         .setAddress(
128                                 new Ipv6Builder().setIpv6(arrayToIpv6AddressNoZoneReversed(address))
129                                         .build())
130                         .build();
131             }
132             case MAC: {
133                 return newRemoteEidBuilder(MacAfi.class, vni)
134                         .setAddress(
135                                 new MacBuilder().setMac(new MacAddress(byteArrayToMacSeparated(address)))
136                                         .build()).build();
137             }
138             default: {
139                 throw new IllegalStateException("Unknown type detected");
140             }
141         }
142     }
143
144     default LocalEid getArrayAsLocalEid(@Nonnull final EidType type, final byte[] address, final int vni) {
145         switch (type) {
146             case IPV4: {
147                 return newEidBuilderLocal(Ipv4Afi.class, vni)
148                         .setAddress(
149                                 new Ipv4Builder().setIpv4(arrayToIpv4AddressNoZoneReversed(address))
150                                         .build())
151                         .build();
152             }
153             case IPV6: {
154                 return newEidBuilderLocal(Ipv6Afi.class, vni)
155                         .setAddress(
156                                 new Ipv6Builder().setIpv6(arrayToIpv6AddressNoZoneReversed(address))
157                                         .build())
158                         .build();
159             }
160             case MAC: {
161                 return newEidBuilderLocal(MacAfi.class, vni)
162                         .setAddress(
163                                 new MacBuilder().setMac(new MacAddress(byteArrayToMacSeparated(address)))
164                                         .build()).build();
165             }
166             default: {
167                 throw new IllegalStateException("Unknown type detected");
168             }
169         }
170     }
171
172     default RemoteEid getArrayAsRemoteEid(@Nonnull final EidType type, final byte[] address, final int vni) {
173         switch (type) {
174             case IPV4: {
175                 return newEidBuilderRemote(Ipv4Afi.class, vni)
176                         .setAddress(
177                                 new Ipv4Builder().setIpv4(arrayToIpv4AddressNoZoneReversed(address))
178                                         .build())
179                         .build();
180             }
181             case IPV6: {
182                 return newEidBuilderRemote(Ipv6Afi.class, vni)
183                         .setAddress(
184                                 new Ipv6Builder().setIpv6(arrayToIpv6AddressNoZoneReversed(address))
185                                         .build())
186                         .build();
187             }
188             case MAC: {
189                 return newEidBuilderRemote(MacAfi.class, vni)
190                         .setAddress(
191                                 new MacBuilder().setMac(new MacAddress(byteArrayToMacSeparated(address)))
192                                         .build()).build();
193             }
194             default: {
195                 throw new IllegalStateException("Unknown type detected");
196             }
197         }
198     }
199
200     default String getArrayAsEidString(
201             EidType type, byte[] address) {
202         switch (type) {
203             case IPV4: {
204                 return arrayToIpv4AddressNoZoneReversed(address).getValue();
205             }
206             case IPV6: {
207                 return arrayToIpv6AddressNoZoneReversed(address).getValue();
208             }
209             case MAC: {
210                 //as wrong as it looks ,its right(second param is not end index,but count)
211                 return byteArrayToMacSeparated(Arrays.copyOfRange(address, 0, 6));
212             }
213             default: {
214                 throw new IllegalStateException("Unknown type detected");
215             }
216         }
217     }
218
219
220     default EidType getEidType(
221             org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid address) {
222         checkNotNull(address, "SimpleAddress cannot be null");
223
224         return resolveType(address.getAddress());
225     }
226
227     default EidType getEidType(
228             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.Eid address) {
229         checkNotNull(address, "SimpleAddress cannot be null");
230
231         return resolveType(address.getAddress());
232     }
233
234
235     default EidType getEidType(
236             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid address) {
237         checkNotNull(address, "Address cannot be null");
238
239         return resolveType(address.getAddress());
240     }
241
242
243     default EidType getEidType(final LocalEid address) {
244         checkNotNull(address, "Address cannot be null");
245
246         return resolveType(address.getAddress());
247     }
248
249     default EidType getEidType(final RemoteEid address) {
250         checkNotNull(address, "Address cannot be null");
251
252         return resolveType(address.getAddress());
253     }
254
255     static EidType resolveType(
256             Address address) {
257
258         if (address instanceof Ipv4) {
259             return IPV4;
260         } else if (address instanceof Ipv6) {
261             return IPV6;
262         } else if (address instanceof Mac) {
263             return MAC;
264         } else {
265             throw new IllegalStateException("Unknown type detected");
266         }
267     }
268
269     default byte[] getEidAsByteArray(
270             org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid address) {
271         checkNotNull(address, "Eid cannot be null");
272
273         return resolveByteArray(getEidType(address), address.getAddress());
274     }
275
276     default byte[] getEidAsByteArray(
277             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.local.mappings.local.mapping.Eid address) {
278         checkNotNull(address, "Eid cannot be null");
279
280         return resolveByteArray(getEidType(address), address.getAddress());
281     }
282
283     default byte[] getEidAsByteArray(
284             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid address) {
285         checkNotNull(address, "Eid cannot be null");
286
287         return resolveByteArray(getEidType(address), address.getAddress());
288     }
289
290     default byte[] getEidAsByteArray(final LocalEid address) {
291         checkNotNull(address, "Eid cannot be null");
292
293         return resolveByteArray(getEidType(address), address.getAddress());
294     }
295
296
297     default byte[] getEidAsByteArray(final RemoteEid address) {
298         checkNotNull(address, "Eid cannot be null");
299
300         return resolveByteArray(getEidType(address), address.getAddress());
301     }
302
303     default byte[] resolveByteArray(EidType type, Address address) {
304         switch (type) {
305             case IPV4:
306                 return ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(((Ipv4) address).getIpv4()));
307             case IPV6:
308                 return ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(((Ipv6) address).getIpv6()));
309             case MAC:
310                 return parseMac(((Mac) address).getMac().getValue());
311             default:
312                 throw new IllegalArgumentException("Unsupported type");
313         }
314     }
315
316     default boolean compareEids(
317             LispAddress first,
318             LispAddress second) {
319
320         return compareAddresses(checkNotNull(first, "First eid is null").getAddress(),
321                 checkNotNull(second, "Second eid is null").getAddress());
322     }
323
324     default boolean compareAddresses(Address firstAddress, Address secondAddress) {
325
326         checkNotNull(firstAddress, "First address is null");
327         checkNotNull(secondAddress, "Second address is null");
328
329         if (firstAddress instanceof Ipv4 && secondAddress instanceof Ipv4) {
330             return ((Ipv4) firstAddress).getIpv4().getValue().equals(((Ipv4) secondAddress).getIpv4().getValue());
331         }
332
333         if (firstAddress instanceof Ipv6 && secondAddress instanceof Ipv6) {
334             return ((Ipv6) firstAddress).getIpv6().getValue().equals(((Ipv6) secondAddress).getIpv6().getValue());
335         }
336
337         if (firstAddress instanceof Mac && secondAddress instanceof Mac) {
338             return ((Mac) firstAddress).getMac().getValue().equals(((Mac) secondAddress).getMac().getValue());
339         }
340
341         return false;
342     }
343 }