HONEYCOMB-58 - Routing Api
[honeycomb.git] / vpp-common / vpp-translate-utils / src / main / java / io / fd / honeycomb / translate / vpp / util / AddressTranslator.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.translate.vpp.util;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import javax.annotation.Nonnull;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
27
28 /**
29  * Aggregation trait providing logic for converting address based data
30  */
31 public interface AddressTranslator extends Ipv4Translator, Ipv6Translator, MacTranslator {
32
33     default byte[] ipAddressToArray(IpAddress address) {
34         checkNotNull(address, "Cannot resolve null adddress");
35
36         if (isIpv6(address)) {
37             return ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(address.getIpv6Address()));
38         } else {
39             return ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(address.getIpv4Address()));
40         }
41     }
42
43
44     /**
45      * Converts {@link IpAddress} to array representing {@link Ipv4Address} or {@link Ipv6Address}
46      */
47     default byte[] ipAddressToArray(boolean isIpv6, @Nonnull IpAddress address) {
48         checkNotNull(address, "Cannot convert null Address");
49
50         if (isIpv6) {
51             return ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(address.getIpv6Address()));
52         } else {
53             return ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(address.getIpv4Address()));
54         }
55     }
56
57     /**
58      * Converts array bytes to {@link IpAddress}.
59      */
60     @Nonnull
61     default IpAddress arrayToIpAddress(boolean isIpv6, byte[] ip) {
62         if (isIpv6) {
63             return new IpAddress(arrayToIpv6AddressNoZone(ip));
64         } else {
65             return new IpAddress(arrayToIpv4AddressNoZone(ip));
66         }
67     }
68
69     /**
70      * Converts array bytes to {@link IpAddress}
71      */
72     @Nonnull
73     default IpAddress arrayToIpAddressReversed(boolean isIpv6, byte[] ip) {
74         if (isIpv6) {
75             return new IpAddress(arrayToIpv6AddressNoZoneReversed(ip));
76         } else {
77             return new IpAddress(arrayToIpv4AddressNoZoneReversed(ip));
78         }
79     }
80
81     default IpAddress reverseAddress(@Nonnull final IpAddress address) {
82         //arrayToIpAdddress internaly reverts order
83         return arrayToIpAddress(isIpv6(address), ipAddressToArray(address));
84     }
85 }