blob: 5cd1d7edcb1c5cf89305011cc6ca476d4dfbab5c [file] [log] [blame]
Hyunsun Moon126171d2016-02-09 01:55:48 -08001/*
Brian O'Connor8e57fd52016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Hyunsun Moon126171d2016-02-09 01:55:48 -08003 *
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 */
Hyunsun Moon187bf532017-01-19 10:57:40 +090016package org.opencord.cordvtn.api.net;
Hyunsun Moon126171d2016-02-09 01:55:48 -080017
18import com.google.common.base.MoreObjects;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
21
22import java.util.Objects;
23
24import static com.google.common.base.Preconditions.checkArgument;
25
26/**
Hyunsun Moon187bf532017-01-19 10:57:40 +090027 * Representation of a network address with CIDR notation.
Hyunsun Moon126171d2016-02-09 01:55:48 -080028 */
Hyunsun Moon187bf532017-01-19 10:57:40 +090029public final class CidrAddr {
Hyunsun Moon126171d2016-02-09 01:55:48 -080030 private final IpAddress ip;
31 private final IpPrefix prefix;
32
33 /**
34 * Constructor for a given IP address and prefix.
35 *
Hyunsun Moon187bf532017-01-19 10:57:40 +090036 * @param ip ip address
Hyunsun Moon126171d2016-02-09 01:55:48 -080037 * @param prefix ip prefix
38 */
Hyunsun Moon187bf532017-01-19 10:57:40 +090039 public CidrAddr(IpAddress ip, IpPrefix prefix) {
Hyunsun Moon126171d2016-02-09 01:55:48 -080040 this.ip = ip;
41 this.prefix = prefix;
42 }
43
44 /**
45 * Converts a CIDR notation string into a network address.
46 *
47 * @param cidr cidr
48 * @return network address
49 * @throws IllegalArgumentException if the cidr is not valid
50 */
Hyunsun Moon187bf532017-01-19 10:57:40 +090051 public static CidrAddr valueOf(String cidr) {
Hyunsun Moon126171d2016-02-09 01:55:48 -080052 checkArgument(cidr.contains("/"));
53
54 IpAddress ipAddress = IpAddress.valueOf(cidr.split("/")[0]);
55 IpPrefix ipPrefix = IpPrefix.valueOf(cidr);
56
Hyunsun Moon187bf532017-01-19 10:57:40 +090057 return new CidrAddr(ipAddress, ipPrefix);
Hyunsun Moon126171d2016-02-09 01:55:48 -080058 }
59
60 /**
61 * Returns the IP address value of the network address.
62 *
63 * @return ip address
64 */
65 public IpAddress ip() {
66 return this.ip;
67 }
68
69 /**
70 * Returns the IP prefix value of the network address.
71 *
72 * @return ip prefix
73 */
74 public IpPrefix prefix() {
75 return this.prefix;
76 }
77
78 /**
79 * Converts a network address to a CIDR notation.
80 *
81 * @return cidr notation string
82 */
83 public String cidr() {
84 return ip.toString() + "/" + prefix.prefixLength();
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj) {
90 return true;
91 }
92
Hyunsun Moon187bf532017-01-19 10:57:40 +090093 if (obj instanceof CidrAddr) {
94 CidrAddr that = (CidrAddr) obj;
Hyunsun Moon126171d2016-02-09 01:55:48 -080095 if (Objects.equals(ip, that.ip) && Objects.equals(prefix, that.prefix)) {
96 return true;
97 }
98 }
99 return false;
100 }
101
102 @Override
103 public int hashCode() {
104 return Objects.hash(ip, prefix);
105 }
106
107 @Override
108 public String toString() {
109 return MoreObjects.toStringHelper(getClass())
110 .add("IpAddress", ip)
111 .add("IpPrefix", prefix)
112 .toString();
113 }
114}