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; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 39 | |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Creates an end-station host using the supplied information. |
| 42 | * |
| 43 | * @param providerId provider identity |
| 44 | * @param id host identifier |
| 45 | * @param mac host MAC address |
| 46 | * @param vlan host VLAN identifier |
| 47 | * @param location host location |
| 48 | * @param ips host IP addresses |
| 49 | * @param annotations optional key/value annotations |
| 50 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 51 | public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 52 | VlanId vlan, HostLocation location, Set<IpAddress> ips, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 53 | Annotations... annotations) { |
| 54 | super(providerId, id, annotations); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 55 | this.mac = mac; |
| 56 | this.vlan = vlan; |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 57 | this.location = location; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 58 | this.ips = new HashSet<>(ips); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public HostId id() { |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 63 | return (HostId) id; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 67 | public MacAddress mac() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 68 | return mac; |
| 69 | } |
| 70 | |
| 71 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 72 | public Set<IpAddress> ipAddresses() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 73 | return Collections.unmodifiableSet(ips); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public HostLocation location() { |
| 78 | return location; |
| 79 | } |
| 80 | |
| 81 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 82 | public VlanId vlan() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 83 | return vlan; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int hashCode() { |
| 88 | return Objects.hash(id, mac, vlan, location); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 93 | if (this == obj) { |
| 94 | return true; |
| 95 | } |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 96 | if (obj instanceof DefaultHost) { |
| 97 | final DefaultHost other = (DefaultHost) obj; |
| 98 | return Objects.equals(this.id, other.id) && |
| 99 | Objects.equals(this.mac, other.mac) && |
| 100 | Objects.equals(this.vlan, other.vlan) && |
Charles Chan | 33f28a9 | 2015-11-13 13:12:38 -0800 | [diff] [blame] | 101 | Objects.equals(this.location, other.location) && |
Charles Chan | f4f1f4b | 2016-02-22 11:12:53 -0800 | [diff] [blame] | 102 | Objects.equals(this.ipAddresses(), other.ipAddresses()) && |
| 103 | Objects.equals(this.annotations(), other.annotations()); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public String toString() { |
| 110 | return toStringHelper(this) |
Charles Chan | f4f1f4b | 2016-02-22 11:12:53 -0800 | [diff] [blame] | 111 | .add("id", id()) |
| 112 | .add("mac", mac()) |
| 113 | .add("vlan", vlan()) |
| 114 | .add("location", location()) |
| 115 | .add("ipAddresses", ipAddresses()) |
| 116 | .add("annotations", annotations()) |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 117 | .toString(); |
| 118 | } |
| 119 | |
| 120 | } |