blob: b430020840310830d701428fb651edb94bfeed56 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Charles Chancd06c692017-04-27 20:46:06 -070024import java.util.Comparator;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070025import java.util.HashSet;
26import java.util.Objects;
27import java.util.Set;
28
tomf5d85d42014-10-02 05:27:56 -070029import static com.google.common.base.MoreObjects.toStringHelper;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070030
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070031/**
32 * A basic implementation of a Host.
33 */
Ayaka Koshibe74a23922014-09-09 16:45:39 -070034public class DefaultHost extends AbstractElement implements Host {
35
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070036 private final MacAddress mac;
37 private final VlanId vlan;
Charles Chancd06c692017-04-27 20:46:06 -070038 private final Set<HostLocation> locations;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070039 private final Set<IpAddress> ips;
sdn5e935452016-08-30 04:12:54 -070040 private final boolean configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070041
tomf5d85d42014-10-02 05:27:56 -070042 /**
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 Koshibea9c199f2014-09-16 16:21:40 -070053 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070054 VlanId vlan, HostLocation location, Set<IpAddress> ips,
tomf5d85d42014-10-02 05:27:56 -070055 Annotations... annotations) {
sdn5e935452016-08-30 04:12:54 -070056 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 Chancd06c692017-04-27 20:46:06 -070074 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) {
tomf5d85d42014-10-02 05:27:56 -070093 super(providerId, id, annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -070094 this.mac = mac;
95 this.vlan = vlan;
Charles Chancd06c692017-04-27 20:46:06 -070096 this.locations = new HashSet<>(locations);
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070097 this.ips = new HashSet<>(ips);
sdn5e935452016-08-30 04:12:54 -070098 this.configured = configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070099 }
100
101 @Override
102 public HostId id() {
tom5a9383a2014-10-02 07:33:52 -0700103 return (HostId) id;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700104 }
105
106 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700107 public MacAddress mac() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700108 return mac;
109 }
110
Saurav Das018605f2017-02-18 14:05:44 -0800111 /**
112 * Returns an unmodifiable set of IP addresses currently bound to the
113 * host MAC address.
114 */
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700115 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700116 public Set<IpAddress> ipAddresses() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700117 return Collections.unmodifiableSet(ips);
118 }
119
120 @Override
121 public HostLocation location() {
Charles Chancd06c692017-04-27 20:46:06 -0700122 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 Koshibe74a23922014-09-09 16:45:39 -0700130 }
131
132 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700133 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700134 return vlan;
135 }
136
137 @Override
sdn5e935452016-08-30 04:12:54 -0700138 public boolean configured() {
139 return configured;
140 }
141
142 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700143 public int hashCode() {
Charles Chancd06c692017-04-27 20:46:06 -0700144 return Objects.hash(id, mac, vlan, locations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700145 }
146
147 @Override
148 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700149 if (this == obj) {
150 return true;
151 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700152 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 Chancd06c692017-04-27 20:46:06 -0700157 Objects.equals(this.locations, other.locations) &&
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800158 Objects.equals(this.ipAddresses(), other.ipAddresses()) &&
159 Objects.equals(this.annotations(), other.annotations());
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700160 }
161 return false;
162 }
163
164 @Override
165 public String toString() {
166 return toStringHelper(this)
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800167 .add("id", id())
168 .add("mac", mac())
169 .add("vlan", vlan())
Charles Chancd06c692017-04-27 20:46:06 -0700170 .add("locations", locations())
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800171 .add("ipAddresses", ipAddresses())
172 .add("annotations", annotations())
sdn5e935452016-08-30 04:12:54 -0700173 .add("configured", configured())
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700174 .toString();
175 }
176
177}