2 * Copyright (c) 2015 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.lisp.translate.util;
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;
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.rev160520.adjacencies.grouping.adjacencies.adjacency.LocalEid;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.adjacencies.grouping.adjacencies.adjacency.RemoteEid;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.local.mapping.Eid;
48 * Trait providing converting logic for eid's
50 public interface EidTranslator extends AddressTranslator, EidMetadataProvider {
53 default byte getPrefixLength(LocalEid address) {
54 return resolverPrefixLength(address.getAddress());
57 default byte getPrefixLength(RemoteEid address) {
58 return resolverPrefixLength(address.getAddress());
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());
66 default byte getPrefixLength(
67 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.local.mapping.Eid address) {
68 return resolverPrefixLength(address.getAddress());
71 default byte getPrefixLength(
72 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.remote.mappings.remote.mapping.Eid address) {
73 return resolverPrefixLength(address.getAddress());
76 static byte resolverPrefixLength(Address address) {
78 switch (resolveType(address)) {
86 throw new IllegalArgumentException("Illegal type");
90 default Eid getArrayAsEidLocal(@Nonnull final EidType type, final byte[] address, final int vni) {
94 return newLocalEidBuilder(Ipv4Afi.class, vni).setAddress(
95 new Ipv4Builder().setIpv4(arrayToIpv4AddressNoZoneReversed(address)).build())
99 return newLocalEidBuilder(Ipv6Afi.class, vni).setAddress(
100 new Ipv6Builder().setIpv6(arrayToIpv6AddressNoZoneReversed(address)).build())
104 return newLocalEidBuilder(MacAfi.class, vni).setAddress(
105 new MacBuilder().setMac(new MacAddress(byteArrayToMacSeparated(address)))
109 throw new IllegalStateException("Unknown type detected");
114 default org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.remote.mappings.remote.mapping.Eid getArrayAsEidRemote(
115 @Nonnull final EidType type, final byte[] address, final int vni) {
119 return newRemoteEidBuilder(Ipv4Afi.class, vni)
121 new Ipv4Builder().setIpv4(arrayToIpv4AddressNoZoneReversed(address))
126 return newRemoteEidBuilder(Ipv6Afi.class, vni)
128 new Ipv6Builder().setIpv6(arrayToIpv6AddressNoZoneReversed(address))
133 return newRemoteEidBuilder(MacAfi.class, vni)
135 new MacBuilder().setMac(new MacAddress(byteArrayToMacSeparated(address)))
139 throw new IllegalStateException("Unknown type detected");
144 default String getArrayAsEidString(
145 EidType type, byte[] address) {
148 return arrayToIpv4AddressNoZoneReversed(address).getValue();
151 return arrayToIpv6AddressNoZoneReversed(address).getValue();
154 //as wrong as it looks ,its right(second param is not end index,but count)
155 return byteArrayToMacSeparated(Arrays.copyOfRange(address, 0, 6));
158 throw new IllegalStateException("Unknown type detected");
164 default EidType getEidType(
165 org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid address) {
166 checkNotNull(address, "SimpleAddress cannot be null");
168 return resolveType(address.getAddress());
171 default EidType getEidType(
172 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.local.mapping.Eid address) {
173 checkNotNull(address, "SimpleAddress cannot be null");
175 return resolveType(address.getAddress());
179 default EidType getEidType(
180 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.remote.mappings.remote.mapping.Eid address) {
181 checkNotNull(address, "Address cannot be null");
183 return resolveType(address.getAddress());
187 default EidType getEidType(final LocalEid address) {
188 checkNotNull(address, "Address cannot be null");
190 return resolveType(address.getAddress());
193 default EidType getEidType(final RemoteEid address) {
194 checkNotNull(address, "Address cannot be null");
196 return resolveType(address.getAddress());
199 static EidType resolveType(
202 if (address instanceof Ipv4) {
204 } else if (address instanceof Ipv6) {
206 } else if (address instanceof Mac) {
209 throw new IllegalStateException("Unknown type detected");
213 default byte[] getEidAsByteArray(
214 org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.Eid address) {
215 checkNotNull(address, "Eid cannot be null");
217 return resolveByteArray(getEidType(address), address.getAddress());
220 default byte[] getEidAsByteArray(
221 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.local.mapping.Eid address) {
222 checkNotNull(address, "Eid cannot be null");
224 return resolveByteArray(getEidType(address), address.getAddress());
227 default byte[] getEidAsByteArray(
228 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.remote.mappings.remote.mapping.Eid address) {
229 checkNotNull(address, "Eid cannot be null");
231 return resolveByteArray(getEidType(address), address.getAddress());
234 default byte[] getEidAsByteArray(final LocalEid address) {
235 checkNotNull(address, "Eid cannot be null");
237 return resolveByteArray(getEidType(address), address.getAddress());
241 default byte[] getEidAsByteArray(final RemoteEid address) {
242 checkNotNull(address, "Eid cannot be null");
244 return resolveByteArray(getEidType(address), address.getAddress());
247 default byte[] resolveByteArray(EidType type, Address address) {
250 return ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(((Ipv4) address).getIpv4()));
252 return ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(((Ipv6) address).getIpv6()));
254 return parseMac(((Mac) address).getMac().getValue());
256 throw new IllegalArgumentException("Unsupported type");
260 default boolean compareEids(
262 LispAddress second) {
264 return compareAddresses(checkNotNull(first, "First eid is null").getAddress(),
265 checkNotNull(second, "Second eid is null").getAddress());
268 default boolean compareAddresses(Address firstAddress, Address secondAddress) {
270 checkNotNull(firstAddress, "First address is null");
271 checkNotNull(secondAddress, "Second address is null");
273 if (firstAddress instanceof Ipv4 && secondAddress instanceof Ipv4) {
274 return ((Ipv4) firstAddress).getIpv4().getValue().equals(((Ipv4) secondAddress).getIpv4().getValue());
277 if (firstAddress instanceof Ipv6 && secondAddress instanceof Ipv6) {
278 return ((Ipv6) firstAddress).getIpv6().getValue().equals(((Ipv6) secondAddress).getIpv6().getValue());
281 if (firstAddress instanceof Mac && secondAddress instanceof Mac) {
282 return ((Mac) firstAddress).getMac().getValue().equals(((Mac) secondAddress).getMac().getValue());