fbf8900f60a67815bfdf0632a5b5c9462f63c6ca
[hc2vpp.git] /
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.hc2vpp.common.translate.util;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import java.util.Arrays;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
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.Ipv6Address;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
30
31 /**
32  * Aggregation trait providing logic for converting address based data
33  */
34 public interface AddressTranslator extends Ipv4Translator, Ipv6Translator, MacTranslator {
35
36     AddressTranslator INSTANCE = new AddressTranslator() {
37     };
38
39     default byte[] ipAddressToArray(IpAddress address) {
40         checkNotNull(address, "Cannot resolve null address");
41
42         if (isIpv6(address)) {
43             return ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(address.getIpv6Address()));
44         } else {
45             return ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(address.getIpv4Address()));
46         }
47     }
48
49
50     /**
51      * Converts {@link IpAddress} to array representing {@link Ipv4Address} or {@link Ipv6Address}
52      */
53     default byte[] ipAddressToArray(boolean isIpv6, @Nonnull IpAddress address) {
54         checkNotNull(address, "Cannot convert null Address");
55
56         if (isIpv6) {
57             return ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(address.getIpv6Address()));
58         } else {
59             return ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(address.getIpv4Address()));
60         }
61     }
62
63     default byte[] ipAddressToArray(IpAddressNoZone address) {
64         checkNotNull(address, "Cannot resolve null address");
65
66         if (isIpv6(address)) {
67             return ipv6AddressNoZoneToArray(address.getIpv6AddressNoZone());
68         } else {
69             return ipv4AddressNoZoneToArray(address.getIpv4AddressNoZone());
70         }
71     }
72
73     /**
74      * Converts array bytes to {@link IpAddress}.
75      */
76     @Nonnull
77     default IpAddress arrayToIpAddress(boolean isIpv6, byte[] ip) {
78         if (isIpv6) {
79             return new IpAddress(arrayToIpv6AddressNoZone(ip));
80         } else {
81             return new IpAddress(arrayToIpv4AddressNoZone(ip));
82         }
83     }
84
85     // safest way to compare addresses - prevents returning false while using different types from hierarchy
86     // Ex. Key for MapResolver contains Ipv4Address as value but we translate addresses from binary data to Ipv4AddressNoZone
87     default boolean addressesEqual(final IpAddress left, final IpAddress right) {
88         return Arrays.equals(left.getValue(), right.getValue());
89     }
90
91     /**
92      * Extract mask length from prefixed address
93      */
94     default byte extractPrefix(IpPrefix ipPrefix) {
95         if (isIpv6(ipPrefix)) {
96             return extractPrefix(ipPrefix.getIpv6Prefix());
97         } else {
98             return extractPrefix(ipPrefix.getIpv4Prefix());
99         }
100     }
101
102     /**
103      * Convert address part of prefix to byte array
104      */
105     default byte[] ipPrefixToArray(IpPrefix ipPrefix) {
106         if (isIpv6(ipPrefix)) {
107             return ipv6AddressPrefixToArray(ipPrefix.getIpv6Prefix());
108         } else {
109             return ipv4AddressPrefixToArray(ipPrefix.getIpv4Prefix());
110         }
111     }
112 }