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; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.net.provider.ProviderId; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.Collections; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 24 | import java.util.Comparator; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 25 | import java.util.HashSet; |
| 26 | import java.util.Objects; |
| 27 | import java.util.Set; |
| 28 | |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 30 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 31 | /** |
| 32 | * A basic implementation of a Host. |
| 33 | */ |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 34 | public class DefaultHost extends AbstractElement implements Host { |
| 35 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 36 | private final MacAddress mac; |
| 37 | private final VlanId vlan; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 38 | private final Set<HostLocation> locations; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 39 | private final Set<IpAddress> ips; |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 40 | private final boolean configured; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 41 | |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Creates an end-station host using the supplied information. |
| 44 | * |
| 45 | * @param providerId provider identity |
| 46 | * @param id host identifier |
| 47 | * @param mac host MAC address |
| 48 | * @param vlan host VLAN identifier |
| 49 | * @param location host location |
| 50 | * @param ips host IP addresses |
| 51 | * @param annotations optional key/value annotations |
| 52 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 53 | public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 54 | VlanId vlan, HostLocation location, Set<IpAddress> ips, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 55 | Annotations... annotations) { |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 56 | this(providerId, id, mac, vlan, location, ips, false, annotations); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates an end-station host using the supplied information. |
| 61 | * |
| 62 | * @param providerId provider identity |
| 63 | * @param id host identifier |
| 64 | * @param mac host MAC address |
| 65 | * @param vlan host VLAN identifier |
| 66 | * @param location host location |
| 67 | * @param ips host IP addresses |
| 68 | * @param configured true if configured via NetworkConfiguration |
| 69 | * @param annotations optional key/value annotations |
| 70 | */ |
| 71 | public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, |
| 72 | VlanId vlan, HostLocation location, Set<IpAddress> ips, |
| 73 | boolean configured, Annotations... annotations) { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 74 | this(providerId, id, mac, vlan, Collections.singleton(location), ips, |
| 75 | configured, annotations); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates an end-station host using the supplied information. |
| 80 | * |
| 81 | * @param providerId provider identity |
| 82 | * @param id host identifier |
| 83 | * @param mac host MAC address |
| 84 | * @param vlan host VLAN identifier |
| 85 | * @param locations set of host locations |
| 86 | * @param ips host IP addresses |
| 87 | * @param configured true if configured via NetworkConfiguration |
| 88 | * @param annotations optional key/value annotations |
| 89 | */ |
| 90 | public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, |
| 91 | VlanId vlan, Set<HostLocation> locations, Set<IpAddress> ips, |
| 92 | boolean configured, Annotations... annotations) { |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 93 | super(providerId, id, annotations); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 94 | this.mac = mac; |
| 95 | this.vlan = vlan; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 96 | this.locations = new HashSet<>(locations); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 97 | this.ips = new HashSet<>(ips); |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 98 | this.configured = configured; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public HostId id() { |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 103 | return (HostId) id; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 107 | public MacAddress mac() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 108 | return mac; |
| 109 | } |
| 110 | |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Returns an unmodifiable set of IP addresses currently bound to the |
| 113 | * host MAC address. |
| 114 | */ |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 115 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 116 | public Set<IpAddress> ipAddresses() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 117 | return Collections.unmodifiableSet(ips); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public HostLocation location() { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 122 | return locations.stream() |
| 123 | .sorted(Comparator.comparingLong(HostLocation::time).reversed()) |
| 124 | .findFirst().orElse(null); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Set<HostLocation> locations() { |
| 129 | return locations; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 133 | public VlanId vlan() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 134 | return vlan; |
| 135 | } |
| 136 | |
| 137 | @Override |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 138 | public boolean configured() { |
| 139 | return configured; |
| 140 | } |
| 141 | |
| 142 | @Override |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 143 | public int hashCode() { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 144 | return Objects.hash(id, mac, vlan, locations); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 149 | if (this == obj) { |
| 150 | return true; |
| 151 | } |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 152 | if (obj instanceof DefaultHost) { |
| 153 | final DefaultHost other = (DefaultHost) obj; |
| 154 | return Objects.equals(this.id, other.id) && |
| 155 | Objects.equals(this.mac, other.mac) && |
| 156 | Objects.equals(this.vlan, other.vlan) && |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 157 | Objects.equals(this.locations, other.locations) && |
Charles Chan | f4f1f4b | 2016-02-22 11:12:53 -0800 | [diff] [blame] | 158 | Objects.equals(this.ipAddresses(), other.ipAddresses()) && |
| 159 | Objects.equals(this.annotations(), other.annotations()); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 160 | } |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public String toString() { |
| 166 | return toStringHelper(this) |
Charles Chan | f4f1f4b | 2016-02-22 11:12:53 -0800 | [diff] [blame] | 167 | .add("id", id()) |
| 168 | .add("mac", mac()) |
| 169 | .add("vlan", vlan()) |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 170 | .add("locations", locations()) |
Charles Chan | f4f1f4b | 2016-02-22 11:12:53 -0800 | [diff] [blame] | 171 | .add("ipAddresses", ipAddresses()) |
| 172 | .add("annotations", annotations()) |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 173 | .add("configured", configured()) |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 174 | .toString(); |
| 175 | } |
| 176 | |
| 177 | } |