blob: 61d5dd2723af7f3336695869dc904d8cac016a07 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
Ayaka Koshibe74a23922014-09-09 16:45:39 -070016package org.onlab.onos.net;
17
tomf5d85d42014-10-02 05:27:56 -070018import org.onlab.onos.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) &&
101 Objects.equals(this.location, other.location);
102 }
103 return false;
104 }
105
106 @Override
107 public String toString() {
108 return toStringHelper(this)
109 .add("id", id)
110 .add("mac", mac)
111 .add("vlan", vlan)
112 .add("location", location)
113 .add("ipAddresses", ips)
114 .toString();
115 }
116
117}