Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.host; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 19 | import java.util.Comparator; |
| 20 | import java.util.HashSet; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 21 | import java.util.Set; |
| 22 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.AbstractDescription; |
| 24 | import org.onosproject.net.HostLocation; |
| 25 | import org.onosproject.net.SparseAnnotations; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 29 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableSet; |
| 31 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 32 | import static com.google.common.base.MoreObjects.toStringHelper; |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 33 | import com.google.common.base.Objects; |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Default implementation of an immutable host description. |
| 37 | */ |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 38 | public class DefaultHostDescription extends AbstractDescription |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 39 | implements HostDescription { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 40 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 41 | private final MacAddress mac; |
| 42 | private final VlanId vlan; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 43 | private final Set<HostLocation> locations; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 44 | private final Set<IpAddress> ip; |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 45 | private final boolean configured; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 46 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Creates a host description using the supplied information. |
| 49 | * |
| 50 | * @param mac host MAC address |
| 51 | * @param vlan host VLAN identifier |
| 52 | * @param location host location |
| 53 | * @param annotations optional key/value annotations map |
| 54 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 55 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 56 | HostLocation location, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 57 | SparseAnnotations... annotations) { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 58 | this(mac, vlan, location, Collections.emptySet(), annotations); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 59 | } |
| 60 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Creates a host description using the supplied information. |
| 63 | * |
| 64 | * @param mac host MAC address |
| 65 | * @param vlan host VLAN identifier |
| 66 | * @param location host location |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 67 | * @param ip host IP address |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 68 | * @param annotations optional key/value annotations map |
| 69 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 70 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 71 | HostLocation location, IpAddress ip, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 72 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 73 | this(mac, vlan, location, ImmutableSet.of(ip), annotations); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Creates a host description using the supplied information. |
| 78 | * |
| 79 | * @param mac host MAC address |
| 80 | * @param vlan host VLAN identifier |
| 81 | * @param location host location |
| 82 | * @param ip host IP addresses |
| 83 | * @param annotations optional key/value annotations map |
| 84 | */ |
| 85 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 86 | HostLocation location, Set<IpAddress> ip, |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 87 | SparseAnnotations... annotations) { |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 88 | this(mac, vlan, location, ip, false, annotations); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Creates a host description using the supplied information. |
| 93 | * |
| 94 | * @param mac host MAC address |
| 95 | * @param vlan host VLAN identifier |
| 96 | * @param location host location |
| 97 | * @param configured true if configured via NetworkConfiguration |
| 98 | * @param annotations optional key/value annotations map |
| 99 | */ |
| 100 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
| 101 | HostLocation location, |
| 102 | boolean configured, |
| 103 | SparseAnnotations... annotations) { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 104 | this(mac, vlan, location, Collections.emptySet(), configured, annotations); |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Creates a host description using the supplied information. |
| 109 | * |
| 110 | * @param mac host MAC address |
| 111 | * @param vlan host VLAN identifier |
| 112 | * @param location host location |
| 113 | * @param ip host IP address |
| 114 | * @param configured true if configured via NetworkConfiguration |
| 115 | * @param annotations optional key/value annotations map |
| 116 | */ |
| 117 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
| 118 | HostLocation location, Set<IpAddress> ip, |
| 119 | boolean configured, |
| 120 | SparseAnnotations... annotations) { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 121 | this(mac, vlan, Collections.singleton(location), ip, configured, annotations); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Creates a host description using the supplied information. |
| 126 | * |
| 127 | * @param mac host MAC address |
| 128 | * @param vlan host VLAN identifier |
| 129 | * @param locations host locations |
| 130 | * @param ip host IP address |
| 131 | * @param configured true if configured via NetworkConfiguration |
| 132 | * @param annotations optional key/value annotations map |
| 133 | */ |
| 134 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
| 135 | Set<HostLocation> locations, |
| 136 | Set<IpAddress> ip, boolean configured, |
| 137 | SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 138 | super(annotations); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 139 | this.mac = mac; |
| 140 | this.vlan = vlan; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 141 | this.locations = new HashSet<>(locations); |
| 142 | this.ip = new HashSet<>(ip); |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 143 | this.configured = configured; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 147 | public MacAddress hwAddress() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 148 | return mac; |
| 149 | } |
| 150 | |
| 151 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 152 | public VlanId vlan() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 153 | return vlan; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public HostLocation location() { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 158 | return locations.stream() |
| 159 | .sorted(Comparator.comparingLong(HostLocation::time).reversed()) |
| 160 | .findFirst().orElse(null); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Set<HostLocation> locations() { |
| 165 | return locations; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 169 | public Set<IpAddress> ipAddress() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 170 | return ip; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 174 | public boolean configured() { |
| 175 | return configured; |
| 176 | } |
| 177 | |
| 178 | @Override |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 179 | public String toString() { |
| 180 | return toStringHelper(this) |
| 181 | .add("mac", mac) |
| 182 | .add("vlan", vlan) |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 183 | .add("locations", locations) |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 184 | .add("ipAddress", ip) |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 185 | .add("configured", configured) |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 186 | .toString(); |
| 187 | } |
| 188 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 189 | @Override |
| 190 | public int hashCode() { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 191 | return Objects.hashCode(super.hashCode(), mac, vlan, locations, ip); |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public boolean equals(Object object) { |
| 196 | if (object != null && getClass() == object.getClass()) { |
| 197 | if (!super.equals(object)) { |
| 198 | return false; |
| 199 | } |
| 200 | DefaultHostDescription that = (DefaultHostDescription) object; |
| 201 | return Objects.equal(this.mac, that.mac) |
| 202 | && Objects.equal(this.vlan, that.vlan) |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 203 | && Objects.equal(this.locations, that.locations) |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 204 | && Objects.equal(this.ip, that.ip); |
| 205 | } |
| 206 | return false; |
| 207 | } |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 208 | } |