blob: 3503eca6cdc954ec665d403b8e80de28096f78cb [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.host;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070017
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070018import java.util.Collections;
19import java.util.Set;
20
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.AbstractDescription;
22import org.onosproject.net.HostLocation;
23import org.onosproject.net.SparseAnnotations;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070024import org.onlab.packet.IpAddress;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070025import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070027
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070028import com.google.common.collect.ImmutableSet;
29
tom27ae0e62014-10-01 20:35:01 -070030import static com.google.common.base.MoreObjects.toStringHelper;
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -080031import com.google.common.base.Objects;
tom27ae0e62014-10-01 20:35:01 -070032
33/**
34 * Default implementation of an immutable host description.
35 */
tomf5d85d42014-10-02 05:27:56 -070036public class DefaultHostDescription extends AbstractDescription
tom27ae0e62014-10-01 20:35:01 -070037 implements HostDescription {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070038
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070039 private final MacAddress mac;
40 private final VlanId vlan;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070041 private final HostLocation location;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070042 private final Set<IpAddress> ip;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070043
tom27ae0e62014-10-01 20:35:01 -070044 /**
45 * Creates a host description using the supplied information.
46 *
47 * @param mac host MAC address
48 * @param vlan host VLAN identifier
49 * @param location host location
50 * @param annotations optional key/value annotations map
51 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070052 public DefaultHostDescription(MacAddress mac, VlanId vlan,
tom27ae0e62014-10-01 20:35:01 -070053 HostLocation location,
tomf5d85d42014-10-02 05:27:56 -070054 SparseAnnotations... annotations) {
Sho SHIMIZU21d00692016-08-15 11:15:28 -070055 this(mac, vlan, location, Collections.emptySet(),
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070056 annotations);
Ayaka Koshibe1a100982014-09-13 19:32:19 -070057 }
58
tom27ae0e62014-10-01 20:35:01 -070059 /**
60 * Creates a host description using the supplied information.
61 *
62 * @param mac host MAC address
63 * @param vlan host VLAN identifier
64 * @param location host location
tom093340b2014-10-10 00:15:36 -070065 * @param ip host IP address
tom27ae0e62014-10-01 20:35:01 -070066 * @param annotations optional key/value annotations map
67 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070068 public DefaultHostDescription(MacAddress mac, VlanId vlan,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070069 HostLocation location, IpAddress ip,
tomf5d85d42014-10-02 05:27:56 -070070 SparseAnnotations... annotations) {
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070071 this(mac, vlan, location, ImmutableSet.of(ip), annotations);
72 }
73
74 /**
75 * Creates a host description using the supplied information.
76 *
77 * @param mac host MAC address
78 * @param vlan host VLAN identifier
79 * @param location host location
80 * @param ip host IP addresses
81 * @param annotations optional key/value annotations map
82 */
83 public DefaultHostDescription(MacAddress mac, VlanId vlan,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070084 HostLocation location, Set<IpAddress> ip,
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070085 SparseAnnotations... annotations) {
tom27ae0e62014-10-01 20:35:01 -070086 super(annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -070087 this.mac = mac;
88 this.vlan = vlan;
tom27ae0e62014-10-01 20:35:01 -070089 this.location = location;
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070090 this.ip = ImmutableSet.copyOf(ip);
Ayaka Koshibe74a23922014-09-09 16:45:39 -070091 }
92
93 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070094 public MacAddress hwAddress() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070095 return mac;
96 }
97
98 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070099 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700100 return vlan;
101 }
102
103 @Override
104 public HostLocation location() {
105 return location;
106 }
107
108 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700109 public Set<IpAddress> ipAddress() {
tom093340b2014-10-10 00:15:36 -0700110 return ip;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700111 }
112
113 @Override
114 public String toString() {
115 return toStringHelper(this)
116 .add("mac", mac)
117 .add("vlan", vlan)
118 .add("location", location)
tom093340b2014-10-10 00:15:36 -0700119 .add("ipAddress", ip)
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700120 .toString();
121 }
122
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800123 @Override
124 public int hashCode() {
125 return Objects.hashCode(super.hashCode(), mac, vlan, location, ip);
126 }
127
128 @Override
129 public boolean equals(Object object) {
130 if (object != null && getClass() == object.getClass()) {
131 if (!super.equals(object)) {
132 return false;
133 }
134 DefaultHostDescription that = (DefaultHostDescription) object;
135 return Objects.equal(this.mac, that.mac)
136 && Objects.equal(this.vlan, that.vlan)
137 && Objects.equal(this.location, that.location)
138 && Objects.equal(this.ip, that.ip);
139 }
140 return false;
141 }
Jonathan Hart38feb6e2016-08-29 22:54:16 +0000142
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700143}