Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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; |
| 19 | import java.util.Set; |
| 20 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.AbstractDescription; |
| 22 | import org.onosproject.net.HostLocation; |
| 23 | import org.onosproject.net.SparseAnnotations; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 27 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 28 | import com.google.common.collect.ImmutableSet; |
| 29 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 30 | import static com.google.common.base.MoreObjects.toStringHelper; |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 31 | import com.google.common.base.Objects; |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Default implementation of an immutable host description. |
| 35 | */ |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 36 | public class DefaultHostDescription extends AbstractDescription |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 37 | implements HostDescription { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 38 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 39 | private final MacAddress mac; |
| 40 | private final VlanId vlan; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 41 | private final HostLocation location; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 42 | private final Set<IpAddress> ip; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 43 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 44 | /** |
| 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 Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 52 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 53 | HostLocation location, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 54 | SparseAnnotations... annotations) { |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 55 | this(mac, vlan, location, Collections.<IpAddress>emptySet(), |
| 56 | annotations); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 57 | } |
| 58 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 59 | /** |
| 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 |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 65 | * @param ip host IP address |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 66 | * @param annotations optional key/value annotations map |
| 67 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 68 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 69 | HostLocation location, IpAddress ip, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 70 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 71 | 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 Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 84 | HostLocation location, Set<IpAddress> ip, |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 85 | SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 86 | super(annotations); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 87 | this.mac = mac; |
| 88 | this.vlan = vlan; |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 89 | this.location = location; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 90 | this.ip = ImmutableSet.copyOf(ip); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 94 | public MacAddress hwAddress() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 95 | return mac; |
| 96 | } |
| 97 | |
| 98 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 99 | public VlanId vlan() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 100 | return vlan; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public HostLocation location() { |
| 105 | return location; |
| 106 | } |
| 107 | |
| 108 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 109 | public Set<IpAddress> ipAddress() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 110 | return ip; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String toString() { |
| 115 | return toStringHelper(this) |
| 116 | .add("mac", mac) |
| 117 | .add("vlan", vlan) |
| 118 | .add("location", location) |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 119 | .add("ipAddress", ip) |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 120 | .toString(); |
| 121 | } |
| 122 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 123 | @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 | } |
| 142 | |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 143 | } |