blob: 8b1c0a189fe49d02abd7fecfaf56882339542cfc [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070016package org.onlab.packet;
17
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070018/**
19 * The class representing an IPv6 network address.
20 * This class is immutable.
21 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080022public 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 Radoslavov9de27722014-10-23 20:31:15 -070026
27 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080028 * 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 Radoslavov9de27722014-10-23 20:31:15 -070033 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080034 private Ip6Prefix(Ip6Address address, int prefixLength) {
35 super(address, prefixLength);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070036 }
37
38 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080039 * Returns the IPv6 address value of the prefix.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070040 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080041 * @return the IPv6 address value of the prefix
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070042 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080043 public Ip6Address address() {
44 IpAddress a = super.address();
45 return (Ip6Address) a;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070046 }
47
48 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080049 * Converts a byte array and a prefix length into an IPv6 prefix.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070050 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080051 * @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 Radoslavov9de27722014-10-23 20:31:15 -070055 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080056 public static Ip6Prefix valueOf(byte[] address, int prefixLength) {
57 return new Ip6Prefix(Ip6Address.valueOf(address), prefixLength);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070058 }
59
60 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080061 * Converts an IPv6 address and a prefix length into an IPv6 prefix.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070062 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080063 * @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 Radoslavov9de27722014-10-23 20:31:15 -070067 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080068 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) {
Pavlin Radoslavovd7b45842015-03-20 15:40:59 -070083 String msg = "Malformed IPv6 prefix string: " + address + ". " +
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080084 "Address must take form " +
85 "\"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/y\"";
86 throw new IllegalArgumentException(msg);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070087 }
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080088 Ip6Address ipAddress = Ip6Address.valueOf(parts[0]);
89 int prefixLength = Integer.parseInt(parts[1]);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070090
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080091 return new Ip6Prefix(ipAddress, prefixLength);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070092 }
93}