blob: 6bb4a06ce8bd3ce5158ea511bf21b2d2a6010967 [file] [log] [blame]
Hyunsun Moon395542a2016-09-01 13:53:08 -07001/*
Brian O'Connor80dff972017-08-03 22:46:30 -07002 * Copyright 2016-present Open Networking Foundation
Hyunsun Moon395542a2016-09-01 13:53:08 -07003 *
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 Moonfd5a24e2016-10-19 19:15:48 -070016package org.opencord.cordvtn.api.net;
Hyunsun Moon395542a2016-09-01 13:53:08 -070017
18import com.google.common.base.MoreObjects;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21
22import java.util.Objects;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Representation of the IP and MAC address pair.
28 */
29public final class AddressPair {
30
31 private final IpAddress ip;
32 private final MacAddress mac;
33
34 private AddressPair(IpAddress ip, MacAddress mac) {
35 this.ip = ip;
36 this.mac = mac;
37 }
38
39 /**
40 * Returns the IP address.
41 *
42 * @return ip address
43 */
44 public IpAddress ip() {
45 return ip;
46 }
47
48 /**
49 * Returns the MAC address.
50 *
51 * @return mac address
52 */
53 public MacAddress mac() {
54 return mac;
55 }
56
57 /**
58 * Returns an address pair instance with the given ip and mac address.
59 *
60 * @param ip ip address
61 * @param mac mac address
62 * @return address pair
63 */
64 public static AddressPair of(IpAddress ip, MacAddress mac) {
65 checkNotNull(ip, "AddressPair IP cannot be null");
66 checkNotNull(mac, "AddressPair MAC cannot be null");
67
68 return new AddressPair(ip, mac);
69 }
70
71 @Override
72 public boolean equals(Object obj) {
73 if (this == obj) {
74 return true;
75 }
76
77 if (obj instanceof AddressPair) {
78 AddressPair that = (AddressPair) obj;
79 if (Objects.equals(ip, that.ip) && Objects.equals(mac, that.mac)) {
80 return true;
81 }
82 }
83 return false;
84 }
85
86 @Override
87 public int hashCode() {
88 return Objects.hash(ip, mac);
89 }
90
91 @Override
92 public String toString() {
93 return MoreObjects.toStringHelper(getClass())
94 .add("ip", ip)
95 .add("mac", mac)
96 .toString();
97 }
98}