blob: 38a8e0dc738b225b0f29571084bb58e25648fa4a [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;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070039
tomf5d85d42014-10-02 05:27:56 -070040 /**
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 Koshibea9c199f2014-09-16 16:21:40 -070051 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070052 VlanId vlan, HostLocation location, Set<IpAddress> ips,
tomf5d85d42014-10-02 05:27:56 -070053 Annotations... annotations) {
54 super(providerId, id, annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -070055 this.mac = mac;
56 this.vlan = vlan;
tomf5d85d42014-10-02 05:27:56 -070057 this.location = location;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070058 this.ips = new HashSet<>(ips);
Ayaka Koshibe74a23922014-09-09 16:45:39 -070059 }
60
61 @Override
62 public HostId id() {
tom5a9383a2014-10-02 07:33:52 -070063 return (HostId) id;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070064 }
65
66 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070067 public MacAddress mac() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070068 return mac;
69 }
70
71 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070072 public Set<IpAddress> ipAddresses() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070073 return Collections.unmodifiableSet(ips);
74 }
75
76 @Override
77 public HostLocation location() {
78 return location;
79 }
80
81 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070082 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070083 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) {
tomfc9a4ff2014-09-22 18:22:47 -070093 if (this == obj) {
94 return true;
95 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -070096 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 Chan33f28a92015-11-13 13:12:38 -0800101 Objects.equals(this.location, other.location) &&
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800102 Objects.equals(this.ipAddresses(), other.ipAddresses()) &&
103 Objects.equals(this.annotations(), other.annotations());
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700104 }
105 return false;
106 }
107
108 @Override
109 public String toString() {
110 return toStringHelper(this)
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800111 .add("id", id())
112 .add("mac", mac())
113 .add("vlan", vlan())
114 .add("location", location())
115 .add("ipAddresses", ipAddresses())
116 .add("annotations", annotations())
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700117 .toString();
118 }
119
120}