7528c5464f3a5fe331bcf09ea13f21a59b3eb398
[honeycomb.git] / vpp-common / vpp-translate-utils / src / main / java / io / fd / honeycomb / translate / vpp / util / Ipv4AddressRange.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 java.util.Objects;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * IPv4 address range representation.
28  */
29 public final class Ipv4AddressRange {
30
31     private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressRange.class);
32
33     private final Ipv4AddressNoZone start;
34     private final Ipv4AddressNoZone end;
35
36     private Ipv4AddressRange(
37                              @Nonnull final Ipv4AddressNoZone start,
38                              @Nonnull final Ipv4AddressNoZone end) {
39         this.start = start;
40         this.end = end;
41     }
42
43     public Ipv4AddressNoZone getStart() {
44         return start;
45     }
46
47     public Ipv4AddressNoZone getEnd() {
48         return end;
49     }
50
51     @Override
52     public boolean equals(final Object other) {
53         if (this == other) {
54             return true;
55         }
56         if (other == null || getClass() != other.getClass()) {
57             return false;
58         }
59         final Ipv4AddressRange that = (Ipv4AddressRange) other;
60         return Objects.equals(start, that.start)
61                 && Objects.equals(end, that.end);
62     }
63
64     @Override
65     public int hashCode() {
66         return Objects.hash(start, end);
67     }
68
69     @Override
70     public String toString() {
71         return "Ipv4AddressRange{"
72                 + "start=" + start
73                 + ", end=" + end
74                 + '}';
75     }
76
77     /**
78      * Create address range from prefix.
79      */
80     public static Ipv4AddressRange fromPrefix(@Nonnull final Ipv4Prefix prefix) {
81         final String addressString = prefix.getValue().split("/")[0];
82         byte prefixLength = Ipv4Translator.INSTANCE.extractPrefix(prefix);
83
84         if (prefixLength == 32) {
85             // 32 Prefix can be handled instantly
86             return new Ipv4AddressRange(new Ipv4AddressNoZone(addressString), new Ipv4AddressNoZone(addressString));
87         }
88
89         final byte[] prefixAddrBytes = Ipv4Translator.INSTANCE.ipv4AddressNoZoneToArray(addressString);
90         final byte[] prefixAddrBytes0 = new byte[prefixAddrBytes.length];
91         final byte[] prefixAddrBytesF = new byte[prefixAddrBytes.length];
92
93         byte index = 0;
94         while (prefixLength >= 8) {
95             prefixAddrBytes0[index] = prefixAddrBytes[index];
96             prefixAddrBytesF[index] = prefixAddrBytes[index];
97             index++;
98             prefixLength -= 8;
99         }
100
101         // Take care of the rest
102         if (prefixLength != 0) {
103             final int mask0 = (byte) (Math.pow(2, prefixLength) - 1) << (8 - prefixLength);
104             prefixAddrBytes0[index] = (byte) (prefixAddrBytes[index] & mask0);
105
106             final int maskF = (byte) (Math.pow(2, 8 - prefixLength) - 1);
107             prefixAddrBytesF[index] = (byte) (prefixAddrBytes[index] | maskF);
108
109             index++;
110         }
111
112         for (int i = index; i < 4; i++) {
113             prefixAddrBytes0[i] = 0;
114             prefixAddrBytesF[i] = (byte) 255;
115         }
116
117         return new Ipv4AddressRange(
118                 Ipv4Translator.INSTANCE.arrayToIpv4AddressNoZoneReversed(prefixAddrBytes0),
119                 Ipv4Translator.INSTANCE.arrayToIpv4AddressNoZoneReversed(prefixAddrBytesF));
120     }
121 }