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