blob: 39730217ab9903693c8ec6641e31027d7a16fad8 [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 IPv4 network address.
20 * This class is immutable.
21 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080022public final class Ip4Prefix extends IpPrefix {
23 public static final IpAddress.Version VERSION = IpAddress.Version.INET;
24 // Maximum network mask length
25 public static final int MAX_MASK_LENGTH = IpPrefix.MAX_INET_MASK_LENGTH;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070026
27 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080028 * Constructor for given IPv4 address, and a prefix length.
29 *
30 * @param address the IPv4 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 Ip4Prefix(Ip4Address 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 IPv4 address value of the prefix.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070040 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080041 * @return the IPv4 address value of the prefix
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070042 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080043 public Ip4Address address() {
44 IpAddress a = super.address();
45 return (Ip4Address) a;
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070046 }
47
48 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080049 * Converts an integer and a prefix length into an IPv4 prefix.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070050 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080051 * @param address an integer representing the IPv4 address
52 * @param prefixLength the prefix length
53 * @return an IPv4 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 Ip4Prefix valueOf(int address, int prefixLength) {
57 return new Ip4Prefix(Ip4Address.valueOf(address), prefixLength);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070058 }
59
60 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080061 * Converts a byte array and a prefix length into an IPv4 prefix.
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070062 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080063 * @param address the IPv4 address value stored in network byte order
64 * @param prefixLength the prefix length
65 * @return an IPv4 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 Ip4Prefix valueOf(byte[] address, int prefixLength) {
69 return new Ip4Prefix(Ip4Address.valueOf(address), prefixLength);
70 }
71
72 /**
73 * Converts an IPv4 address and a prefix length into an IPv4 prefix.
74 *
75 * @param address the IPv4 address
76 * @param prefixLength the prefix length
77 * @return an IPv4 prefix
78 * @throws IllegalArgumentException if the prefix length value is invalid
79 */
80 public static Ip4Prefix valueOf(Ip4Address address, int prefixLength) {
81 return new Ip4Prefix(address, prefixLength);
82 }
83
84 /**
85 * Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16")
86 * into an IPv4 prefix.
87 *
88 * @param address an IP prefix in string form (e.g., "10.1.0.0/16")
89 * @return an IPv4 prefix
90 * @throws IllegalArgumentException if the arguments are invalid
91 */
92 public static Ip4Prefix valueOf(String address) {
93 final String[] parts = address.split("/");
94 if (parts.length != 2) {
Pavlin Radoslavovd7b45842015-03-20 15:40:59 -070095 String msg = "Malformed IPv4 prefix string: " + address + ". " +
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080096 "Address must take form \"x.x.x.x/y\"";
97 throw new IllegalArgumentException(msg);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070098 }
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080099 Ip4Address ipAddress = Ip4Address.valueOf(parts[0]);
100 int prefixLength = Integer.parseInt(parts[1]);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700101
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800102 return new Ip4Prefix(ipAddress, prefixLength);
Pavlin Radoslavov9de27722014-10-23 20:31:15 -0700103 }
104}