blob: 0cd9b5c1d786642c7a3f60abc7ae301cdbec2511 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070016package org.onlab.onos.net.host;
17
18import java.util.Objects;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
21
22import static com.google.common.base.MoreObjects.toStringHelper;
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Represents a single IP address information on an interface.
27 *
28 * TODO:
29 * - Add computation for the default broadcast address if it is not
30 * specified
31 * - Add explicit checks that each IP address or prefix belong to the
32 * same IP version: IPv4/IPv6.
33 * - Inside the copy constructor we should use copy constructors for each
34 * field
35 */
36public class InterfaceIpAddress {
37 private final IpAddress ipAddress;
38 private final IpPrefix subnetAddress;
39 private final IpAddress broadcastAddress;
40 private final IpAddress peerAddress;
41
42 /**
43 * Copy constructor.
44 *
45 * @param other the object to copy from
46 */
47 public InterfaceIpAddress(InterfaceIpAddress other) {
48 // TODO: we should use copy constructors for each field
49 this.ipAddress = other.ipAddress;
50 this.subnetAddress = other.subnetAddress;
51 this.broadcastAddress = other.broadcastAddress;
52 this.peerAddress = other.peerAddress;
53 }
54
55 /**
56 * Constructor for a given IP address and a subnet address.
57 *
58 * @param ipAddress the IP address
59 * @param subnetAddress the IP subnet address
60 */
61 public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress) {
62 this.ipAddress = checkNotNull(ipAddress);
63 this.subnetAddress = checkNotNull(subnetAddress);
64 // TODO: Recompute the default broadcast address from the subnet
65 // address
66 this.broadcastAddress = null;
67 this.peerAddress = null;
68 }
69
70 /**
71 * Constructor for a given IP address and a subnet address.
72 *
73 * @param ipAddress the IP address
74 * @param subnetAddress the IP subnet address
75 * @param broadcastAddress the IP broadcast address. It can be used
76 * to specify non-default broadcast address
77 */
78 public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress,
79 IpAddress broadcastAddress) {
80 this.ipAddress = checkNotNull(ipAddress);
81 this.subnetAddress = checkNotNull(subnetAddress);
82 this.broadcastAddress = broadcastAddress;
83 this.peerAddress = null;
84 }
85
86 /**
87 * Constructor for a given IP address and a subnet address.
88 *
89 * @param ipAddress the IP address
90 * @param subnetAddress the IP subnet address
91 * @param broadcastAddress the IP broadcast address. It can be used
92 * to specify non-default broadcast address. It should be null for
93 * point-to-point interfaces with a peer address
94 * @param peerAddress the peer IP address for point-to-point interfaces
95 */
96 public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress,
97 IpAddress broadcastAddress,
98 IpAddress peerAddress) {
99 this.ipAddress = checkNotNull(ipAddress);
100 this.subnetAddress = checkNotNull(subnetAddress);
101 this.broadcastAddress = broadcastAddress;
102 this.peerAddress = peerAddress;
103 }
104
105 /**
106 * Gets the IP address.
107 *
108 * @return the IP address
109 */
110 public IpAddress ipAddress() {
111 return ipAddress;
112 }
113
114 /**
115 * Gets the IP subnet address.
116 *
117 * @return the IP subnet address
118 */
119 public IpPrefix subnetAddress() {
120 return subnetAddress;
121 }
122
123 /**
124 * Gets the subnet IP broadcast address.
125 *
126 * @return the subnet IP broadcast address
127 */
128 public IpAddress broadcastAddress() {
129 return broadcastAddress;
130 }
131
132 /**
133 * Gets the IP point-to-point interface peer address.
134 *
135 * @return the IP point-to-point interface peer address
136 */
137 public IpAddress peerAddress() {
138 return peerAddress;
139 }
140
141 @Override
142 public boolean equals(Object other) {
143 if (other == this) {
144 return true;
145 }
146 if (!(other instanceof InterfaceIpAddress)) {
147 return false;
148 }
149 InterfaceIpAddress otherAddr = (InterfaceIpAddress) other;
150
151 return Objects.equals(this.ipAddress, otherAddr.ipAddress)
152 && Objects.equals(this.subnetAddress, otherAddr.subnetAddress)
153 && Objects.equals(this.broadcastAddress,
154 otherAddr.broadcastAddress)
155 && Objects.equals(this.peerAddress, otherAddr.peerAddress);
156 }
157
158 @Override
159 public int hashCode() {
160 return Objects.hash(ipAddress, subnetAddress, broadcastAddress,
161 peerAddress);
162 }
163
164 @Override
165 public String toString() {
166 return toStringHelper(this).add("ipAddress", ipAddress)
167 .add("subnetAddress", subnetAddress)
168 .add("broadcastAddress", broadcastAddress)
169 .add("peerAddress", peerAddress)
170 .omitNullValues().toString();
171 }
172}