2 * Copyright (c) 2016 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.translate.vpp.util;
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;
27 * IPv4 address range representation.
29 public final class Ipv4AddressRange {
31 private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressRange.class);
33 private final Ipv4AddressNoZone start;
34 private final Ipv4AddressNoZone end;
36 private Ipv4AddressRange(
37 @Nonnull final Ipv4AddressNoZone start,
38 @Nonnull final Ipv4AddressNoZone end) {
43 public Ipv4AddressNoZone getStart() {
47 public Ipv4AddressNoZone getEnd() {
52 public boolean equals(final Object other) {
56 if (other == null || getClass() != other.getClass()) {
59 final Ipv4AddressRange that = (Ipv4AddressRange) other;
60 return Objects.equals(start, that.start)
61 && Objects.equals(end, that.end);
65 public int hashCode() {
66 return Objects.hash(start, end);
70 public String toString() {
71 return "Ipv4AddressRange{"
78 * Create address range from prefix.
80 public static Ipv4AddressRange fromPrefix(@Nonnull final Ipv4Prefix prefix) {
81 final String addressString = prefix.getValue().split("/")[0];
82 byte prefixLength = Ipv4Translator.INSTANCE.extractPrefix(prefix);
84 if (prefixLength == 32) {
85 // 32 Prefix can be handled instantly
86 return new Ipv4AddressRange(new Ipv4AddressNoZone(addressString), new Ipv4AddressNoZone(addressString));
89 final byte[] prefixAddrBytes = Ipv4Translator.INSTANCE.ipv4AddressNoZoneToArray(addressString);
90 final byte[] prefixAddrBytes0 = new byte[prefixAddrBytes.length];
91 final byte[] prefixAddrBytesF = new byte[prefixAddrBytes.length];
94 while (prefixLength >= 8) {
95 prefixAddrBytes0[index] = prefixAddrBytes[index];
96 prefixAddrBytesF[index] = prefixAddrBytes[index];
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);
106 final int maskF = (byte) (Math.pow(2, 8 - prefixLength) - 1);
107 prefixAddrBytesF[index] = (byte) (prefixAddrBytes[index] | maskF);
112 for (int i = index; i < 4; i++) {
113 prefixAddrBytes0[i] = 0;
114 prefixAddrBytesF[i] = (byte) 255;
117 return new Ipv4AddressRange(
118 Ipv4Translator.INSTANCE.arrayToIpv4AddressNoZoneReversed(prefixAddrBytes0),
119 Ipv4Translator.INSTANCE.arrayToIpv4AddressNoZoneReversed(prefixAddrBytesF));