blob: f7510cd21ba06fe9131006a6adef42cca94db527 [file] [log] [blame]
Henry Yu4b4a7eb2016-11-09 20:07:53 -05001/**
2 * Copyright 2016 Open Networking Laboratory
3 * <p>
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 * <p>
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * <p>
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 */
16package org.onosproject.tetopology.management.api.link;
17
18import com.google.common.base.MoreObjects;
19import org.onlab.packet.Ip4Address;
20
21import java.util.Objects;
22
23/**
24 * Implementation of IPv4 address as an element type.
25 */
26public class TeIpv4 implements ElementType {
27 private final Ip4Address v4Address;
28 private final short v4PrefixLength;
29
30 /**
31 * Creates an IPv4 address.
32 *
33 * @param v4Address the IPv4 address
34 * @param v4PrefixLength the length of IPv4 prefix
35 */
36 public TeIpv4(Ip4Address v4Address, short v4PrefixLength) {
37 this.v4Address = v4Address;
38 this.v4PrefixLength = v4PrefixLength;
39 }
40
41 /**
42 * Returns the IPv4 address.
43 *
44 * @return IPv4 address
45 */
46 public Ip4Address v4Address() {
47 return v4Address;
48 }
49
50 /**
51 * Returns the length of the IPv4 address prefix.
52 *
53 * @return IPv4 address prefix length
54 */
55 public short v4PrefixLength() {
56 return v4PrefixLength;
57 }
58
59 @Override
60 public int hashCode() {
61 return Objects.hash(v4Address, v4PrefixLength);
62 }
63
64 @Override
65 public boolean equals(Object obj) {
66 if (this == obj) {
67 return true;
68 }
69 if (obj instanceof TeIpv4) {
70 TeIpv4 other = (TeIpv4) obj;
71 return Objects.equals(v4Address, other.v4Address) &&
72 Objects.equals(v4PrefixLength, other.v4PrefixLength);
73 }
74 return false;
75 }
76
77 @Override
78 public String toString() {
79 return MoreObjects.toStringHelper(getClass())
80 .add("v4Address", v4Address)
81 .add("v4PrefixLength", v4PrefixLength)
82 .toString();
83 }
84
85}