blob: 7fc77c334160dd21f1d5cfa5ee065160c84b4cdf [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
91 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070092 public Set<IpAddress> ipAddresses() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070093 return Collections.unmodifiableSet(ips);
94 }
95
96 @Override
97 public HostLocation location() {
98 return location;
99 }
100
101 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700102 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700103 return vlan;
104 }
105
106 @Override
sdn5e935452016-08-30 04:12:54 -0700107 public boolean configured() {
108 return configured;
109 }
110
111 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700112 public int hashCode() {
113 return Objects.hash(id, mac, vlan, location);
114 }
115
116 @Override
117 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700118 if (this == obj) {
119 return true;
120 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700121 if (obj instanceof DefaultHost) {
122 final DefaultHost other = (DefaultHost) obj;
123 return Objects.equals(this.id, other.id) &&
124 Objects.equals(this.mac, other.mac) &&
125 Objects.equals(this.vlan, other.vlan) &&
Charles Chan33f28a92015-11-13 13:12:38 -0800126 Objects.equals(this.location, other.location) &&
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800127 Objects.equals(this.ipAddresses(), other.ipAddresses()) &&
128 Objects.equals(this.annotations(), other.annotations());
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700129 }
130 return false;
131 }
132
133 @Override
134 public String toString() {
135 return toStringHelper(this)
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800136 .add("id", id())
137 .add("mac", mac())
138 .add("vlan", vlan())
139 .add("location", location())
140 .add("ipAddresses", ipAddresses())
141 .add("annotations", annotations())
sdn5e935452016-08-30 04:12:54 -0700142 .add("configured", configured())
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700143 .toString();
144 }
145
146}