Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 18 | /** |
| 19 | * The class representing an IPv6 network address. |
| 20 | * This class is immutable. |
| 21 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 22 | public final class Ip6Prefix extends IpPrefix { |
| 23 | public static final IpAddress.Version VERSION = IpAddress.Version.INET6; |
| 24 | // Maximum network mask length |
| 25 | public static final int MAX_MASK_LENGTH = IpPrefix.MAX_INET6_MASK_LENGTH; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 28 | * Constructor for given IPv6 address, and a prefix length. |
| 29 | * |
| 30 | * @param address the IPv6 address |
| 31 | * @param prefixLength the prefix length |
| 32 | * @throws IllegalArgumentException if the prefix length value is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 33 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 34 | private Ip6Prefix(Ip6Address address, int prefixLength) { |
| 35 | super(address, prefixLength); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 39 | * Returns the IPv6 address value of the prefix. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 40 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 41 | * @return the IPv6 address value of the prefix |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 42 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 43 | public Ip6Address address() { |
| 44 | IpAddress a = super.address(); |
| 45 | return (Ip6Address) a; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 49 | * Converts a byte array and a prefix length into an IPv6 prefix. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 50 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 51 | * @param address the IPv6 address value stored in network byte order |
| 52 | * @param prefixLength the prefix length |
| 53 | * @return an IPv6 prefix |
| 54 | * @throws IllegalArgumentException if the prefix length value is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 55 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 56 | public static Ip6Prefix valueOf(byte[] address, int prefixLength) { |
| 57 | return new Ip6Prefix(Ip6Address.valueOf(address), prefixLength); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 61 | * Converts an IPv6 address and a prefix length into an IPv6 prefix. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 62 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 63 | * @param address the IPv6 address |
| 64 | * @param prefixLength the prefix length |
| 65 | * @return an IPv6 prefix |
| 66 | * @throws IllegalArgumentException if the prefix length value is invalid |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 67 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 68 | public static Ip6Prefix valueOf(Ip6Address address, int prefixLength) { |
| 69 | return new Ip6Prefix(address, prefixLength); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Converts a CIDR (slash) notation string (e.g., "1111:2222::/64") |
| 74 | * into an IPv6 prefix. |
| 75 | * |
| 76 | * @param address an IP prefix in string form (e.g.,"1111:2222::/64") |
| 77 | * @return an IPv6 prefix |
| 78 | * @throws IllegalArgumentException if the arguments are invalid |
| 79 | */ |
| 80 | public static Ip6Prefix valueOf(String address) { |
| 81 | final String[] parts = address.split("/"); |
| 82 | if (parts.length != 2) { |
| 83 | String msg = "Malformed IPv6 prefix string: " + address + "." + |
| 84 | "Address must take form " + |
| 85 | "\"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/y\""; |
| 86 | throw new IllegalArgumentException(msg); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 87 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 88 | Ip6Address ipAddress = Ip6Address.valueOf(parts[0]); |
| 89 | int prefixLength = Integer.parseInt(parts[1]); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 90 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 91 | return new Ip6Prefix(ipAddress, prefixLength); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 92 | } |
| 93 | } |