blob: d9cfa25dd50eafce96f18dfa2108a62aa33fbbb8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.net.provider.ProviderId;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070019import org.onlab.packet.IpAddress;
tomf5d85d42014-10-02 05:27:56 -070020import org.onlab.packet.MacAddress;
21import org.onlab.packet.VlanId;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070022
23import java.util.Collections;
24import java.util.HashSet;
25import java.util.Objects;
26import java.util.Set;
27
tomf5d85d42014-10-02 05:27:56 -070028import static com.google.common.base.MoreObjects.toStringHelper;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070029
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070030/**
31 * A basic implementation of a Host.
32 */
Ayaka Koshibe74a23922014-09-09 16:45:39 -070033public class DefaultHost extends AbstractElement implements Host {
34
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070035 private final MacAddress mac;
36 private final VlanId vlan;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070037 private final HostLocation location;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070038 private final Set<IpAddress> ips;
sdn5e935452016-08-30 04:12:54 -070039 private final boolean configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070040
tomf5d85d42014-10-02 05:27:56 -070041 /**
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 Koshibea9c199f2014-09-16 16:21:40 -070052 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070053 VlanId vlan, HostLocation location, Set<IpAddress> ips,
tomf5d85d42014-10-02 05:27:56 -070054 Annotations... annotations) {
sdn5e935452016-08-30 04:12:54 -070055 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) {
tomf5d85d42014-10-02 05:27:56 -070073 super(providerId, id, annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -070074 this.mac = mac;
75 this.vlan = vlan;
tomf5d85d42014-10-02 05:27:56 -070076 this.location = location;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070077 this.ips = new HashSet<>(ips);
sdn5e935452016-08-30 04:12:54 -070078 this.configured = configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070079 }
80
81 @Override
82 public HostId id() {
tom5a9383a2014-10-02 07:33:52 -070083 return (HostId) id;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070084 }
85
86 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070087 public MacAddress mac() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070088 return mac;
89 }
90
Saurav Das018605f2017-02-18 14:05:44 -080091 /**
92 * Returns an unmodifiable set of IP addresses currently bound to the
93 * host MAC address.
94 */
Ayaka Koshibe74a23922014-09-09 16:45:39 -070095 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070096 public Set<IpAddress> ipAddresses() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070097 return Collections.unmodifiableSet(ips);
98 }
99
100 @Override
101 public HostLocation location() {
102 return location;
103 }
104
105 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700106 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700107 return vlan;
108 }
109
110 @Override
sdn5e935452016-08-30 04:12:54 -0700111 public boolean configured() {
112 return configured;
113 }
114
115 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700116 public int hashCode() {
117 return Objects.hash(id, mac, vlan, location);
118 }
119
120 @Override
121 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700122 if (this == obj) {
123 return true;
124 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700125 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 Chan33f28a92015-11-13 13:12:38 -0800130 Objects.equals(this.location, other.location) &&
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800131 Objects.equals(this.ipAddresses(), other.ipAddresses()) &&
132 Objects.equals(this.annotations(), other.annotations());
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700133 }
134 return false;
135 }
136
137 @Override
138 public String toString() {
139 return toStringHelper(this)
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800140 .add("id", id())
141 .add("mac", mac())
142 .add("vlan", vlan())
143 .add("location", location())
144 .add("ipAddresses", ipAddresses())
145 .add("annotations", annotations())
sdn5e935452016-08-30 04:12:54 -0700146 .add("configured", configured())
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700147 .toString();
148 }
149
150}