blob: 587fc7fc20608acb57e6c3e41cc16d0d4c52e876 [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;
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +053019import org.onosproject.store.service.WallClockTimestamp;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070020import org.onlab.packet.IpAddress;
tomf5d85d42014-10-02 05:27:56 -070021import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070023
24import java.util.Collections;
25import 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;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070038 private final HostLocation location;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070039 private final Set<IpAddress> ips;
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +053040 private final WallClockTimestamp timestamp;
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) {
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +053056 this(providerId, id, mac, vlan, location, ips, new WallClockTimestamp(), 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 timestamp last host updated time
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 WallClockTimestamp timestamp, Annotations... annotations) {
tomf5d85d42014-10-02 05:27:56 -070074 super(providerId, id, annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -070075 this.mac = mac;
76 this.vlan = vlan;
tomf5d85d42014-10-02 05:27:56 -070077 this.location = location;
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +053078 this.timestamp = timestamp;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070079 this.ips = new HashSet<>(ips);
Ayaka Koshibe74a23922014-09-09 16:45:39 -070080 }
81
82 @Override
83 public HostId id() {
tom5a9383a2014-10-02 07:33:52 -070084 return (HostId) id;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070085 }
86
87 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070088 public MacAddress mac() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070089 return mac;
90 }
91
92 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070093 public Set<IpAddress> ipAddresses() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070094 return Collections.unmodifiableSet(ips);
95 }
96
97 @Override
98 public HostLocation location() {
99 return location;
100 }
101
102 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700103 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700104 return vlan;
105 }
106
107 @Override
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530108 public WallClockTimestamp timestamp() {
109 return timestamp;
110 }
111
112 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700113 public int hashCode() {
114 return Objects.hash(id, mac, vlan, location);
115 }
116
117 @Override
118 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700119 if (this == obj) {
120 return true;
121 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700122 if (obj instanceof DefaultHost) {
123 final DefaultHost other = (DefaultHost) obj;
124 return Objects.equals(this.id, other.id) &&
125 Objects.equals(this.mac, other.mac) &&
126 Objects.equals(this.vlan, other.vlan) &&
Charles Chan33f28a92015-11-13 13:12:38 -0800127 Objects.equals(this.location, other.location) &&
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800128 Objects.equals(this.ipAddresses(), other.ipAddresses()) &&
129 Objects.equals(this.annotations(), other.annotations());
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700130 }
131 return false;
132 }
133
134 @Override
135 public String toString() {
136 return toStringHelper(this)
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800137 .add("id", id())
138 .add("mac", mac())
139 .add("vlan", vlan())
140 .add("location", location())
141 .add("ipAddresses", ipAddresses())
142 .add("annotations", annotations())
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530143 .add("timestamp", timestamp())
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700144 .toString();
145 }
146
147}