HONEYCOMB-75 - Lisp implemetation
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / util / EidConverter.java
1 /*
2  * Copyright (c) 2015 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.v3po.util.TranslateUtils;
26 import java.util.Arrays;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Builder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Mac;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.MacBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.adjacencies.adjacency.LocalEid;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.adjacencies.adjacency.RemoteEid;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.local.mapping.Eid;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.local.mapping.EidBuilder;
42
43
44 /**
45  * Helper class that converts {@code SimpleAddress} to eid format for vpp
46  */
47 public final class EidConverter {
48
49     private EidConverter() {
50         throw new UnsupportedOperationException("Cannot instantiate utility class " + EidConverter.class.getName());
51     }
52
53     public static byte getPrefixLength(
54             org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid address) {
55         return resolverPrefixLength(address.getAddress());
56     }
57
58     public static byte getPrefixLength(
59             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.local.mapping.Eid address) {
60         return resolverPrefixLength(address.getAddress());
61     }
62
63     public static byte getPrefixLength(
64             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid address) {
65         return resolverPrefixLength(address.getAddress());
66     }
67
68     private static byte resolverPrefixLength(Address address) {
69
70         switch (resolveType(address)) {
71             case IPV4:
72                 return 32;
73             case IPV6:
74                 return (byte) 128;
75             case MAC:
76                 return 0;
77             default:
78                 throw new IllegalArgumentException("Illegal type");
79         }
80     }
81
82     public static Eid getArrayAsEidLocal(EidType type, byte[] address) {
83
84         switch (type) {
85             case IPV4: {
86                 return new EidBuilder().setAddress(
87                         new Ipv4Builder().setIpv4(TranslateUtils.arrayToIpv4AddressNoZoneReversed(address)).build())
88                         .build();
89             }
90             case IPV6: {
91                 return new EidBuilder().setAddress(
92                         new Ipv6Builder().setIpv6(TranslateUtils.arrayToIpv6AddressNoZoneReversed(address)).build())
93                         .build();
94             }
95             case MAC: {
96                 return new EidBuilder().setAddress(
97                         new MacBuilder().setMac(new MacAddress(TranslateUtils.byteArrayToMacSeparated(address)))
98                                 .build()).build();
99             }
100             default: {
101                 throw new IllegalStateException("Unknown type detected");
102             }
103         }
104     }
105
106     public static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid getArrayAsEidRemote(
107             EidType type, byte[] address) {
108
109         switch (type) {
110             case IPV4: {
111                 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.EidBuilder()
112                         .setAddress(
113                                 new Ipv4Builder().setIpv4(TranslateUtils.arrayToIpv4AddressNoZoneReversed(address))
114                                         .build())
115                         .build();
116             }
117             case IPV6: {
118                 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.EidBuilder()
119                         .setAddress(
120                                 new Ipv6Builder().setIpv6(TranslateUtils.arrayToIpv6AddressNoZoneReversed(address))
121                                         .build())
122                         .build();
123             }
124             case MAC: {
125                 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.EidBuilder()
126                         .setAddress(
127                                 new MacBuilder().setMac(new MacAddress(TranslateUtils.byteArrayToMacSeparated(address)))
128                                         .build()).build();
129             }
130             default: {
131                 throw new IllegalStateException("Unknown type detected");
132             }
133         }
134     }
135
136     public static String getArrayAsEidString(
137             EidType type, byte[] address) {
138         switch (type) {
139             case IPV4: {
140                 return TranslateUtils.arrayToIpv4AddressNoZoneReversed(address).getValue();
141             }
142             case IPV6: {
143                 return TranslateUtils.arrayToIpv6AddressNoZoneReversed(address).getValue();
144             }
145             case MAC: {
146                 //as wrong as it looks ,its right(second param is not end index,but count)
147                 return TranslateUtils.byteArrayToMacSeparated(Arrays.copyOfRange(address, 0, 6));
148             }
149             default: {
150                 throw new IllegalStateException("Unknown type detected");
151             }
152         }
153     }
154
155
156     public static EidType getEidType(
157             org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid address) {
158         checkNotNull(address, "SimpleAddress cannot be null");
159
160         return resolveType(address.getAddress());
161     }
162
163     public static EidType getEidType(
164             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.local.mapping.Eid address) {
165         checkNotNull(address, "SimpleAddress cannot be null");
166
167         return resolveType(address.getAddress());
168     }
169
170
171     public static EidType getEidType(
172             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid address) {
173         checkNotNull(address, "Address cannot be null");
174
175         return resolveType(address.getAddress());
176     }
177
178
179     public static EidType getEidType(final LocalEid address) {
180         checkNotNull(address, "Address cannot be null");
181
182         return resolveType(address.getAddress());
183     }
184
185     public static EidType getEidType(final RemoteEid address) {
186         checkNotNull(address, "Address cannot be null");
187
188         return resolveType(address.getAddress());
189     }
190
191     private static EidType resolveType(
192             Address address) {
193
194         if (address instanceof Ipv4) {
195             return IPV4;
196         } else if (address instanceof Ipv6) {
197             return IPV6;
198         } else if (address instanceof Mac) {
199             return MAC;
200         } else {
201             //TODO - other types
202             throw new IllegalStateException("Unknown type detected");
203         }
204     }
205
206     public static byte[] getEidAsByteArray(
207             org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid address) {
208         checkNotNull(address, "Eid cannot be null");
209
210         return resolveByteArray(getEidType(address), address.getAddress());
211     }
212
213     public static byte[] getEidAsByteArray(
214             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.local.mappings.local.mapping.Eid address) {
215         checkNotNull(address, "Eid cannot be null");
216
217         return resolveByteArray(getEidType(address), address.getAddress());
218     }
219
220     public static byte[] getEidAsByteArray(
221             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid address) {
222         checkNotNull(address, "Eid cannot be null");
223
224         return resolveByteArray(getEidType(address), address.getAddress());
225     }
226
227     public static byte[] getEidAsByteArray(final LocalEid address) {
228         checkNotNull(address, "Eid cannot be null");
229
230         return resolveByteArray(getEidType(address), address.getAddress());
231     }
232
233
234     public static byte[] getEidAsByteArray(final RemoteEid address) {
235         checkNotNull(address, "Eid cannot be null");
236
237         return resolveByteArray(getEidType(address), address.getAddress());
238     }
239
240     private static byte[] resolveByteArray(EidType type, Address address) {
241         switch (type) {
242             case IPV4:
243                 return TranslateUtils
244                         .ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(((Ipv4) address).getIpv4()));
245             case IPV6:
246                 return TranslateUtils
247                         .ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(((Ipv6) address).getIpv6()));
248             case MAC:
249                 return TranslateUtils.parseMac(((Mac) address).getMac().getValue());
250             default:
251                 //TODO - other types
252                 throw new IllegalArgumentException("Unsupported type");
253         }
254     }
255
256     public static boolean compareEids(
257             LispAddress first,
258             LispAddress second) {
259
260         return compareAddresses(checkNotNull(first, "First eid is null").getAddress(),
261                 checkNotNull(second, "Second eid is null").getAddress());
262     }
263
264     public static boolean compareAddresses(Address firstAddress, Address secondAddress) {
265
266         checkNotNull(firstAddress, "First address is null");
267         checkNotNull(secondAddress, "Second address is null");
268
269         if (firstAddress instanceof Ipv4 && secondAddress instanceof Ipv4) {
270             return ((Ipv4) firstAddress).getIpv4().getValue().equals(((Ipv4) secondAddress).getIpv4().getValue());
271         }
272
273         if (firstAddress instanceof Ipv6 && secondAddress instanceof Ipv6) {
274             return ((Ipv6) firstAddress).getIpv6().getValue().equals(((Ipv6) secondAddress).getIpv6().getValue());
275         }
276
277         if (firstAddress instanceof Mac && secondAddress instanceof Mac) {
278             return ((Mac) firstAddress).getMac().getValue().equals(((Mac) secondAddress).getMac().getValue());
279         }
280
281         throw new IllegalArgumentException("Unsupported eid type " + firstAddress.getClass());
282     }
283 }