blob: e28a4dd71d112cd319ab70ccc0fd2bac1be03be9 [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;
tom80c0e5e2014-09-08 18:08:58 -070017
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070018import org.onlab.packet.MacAddress;
19import org.onlab.packet.VlanId;
tom7869ad92014-09-09 14:32:08 -070020
tom545708e2014-10-09 17:10:02 -070021import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkArgument;
tom80c0e5e2014-09-08 18:08:58 -070024
25/**
26 * Immutable representation of a host identity.
27 */
28public final class HostId extends ElementId {
29
toma1d16b62014-10-02 23:45:11 -070030 /**
31 * Represents either no host, or an unspecified host; used for creating
32 * open ingress/egress edge links.
33 */
tom545708e2014-10-09 17:10:02 -070034 public static final HostId NONE = new HostId(MacAddress.ZERO, VlanId.NONE);
35
36 private static final int MAC_LENGTH = 17;
37 private static final int MIN_ID_LENGTH = 19;
38
39 private final MacAddress mac;
40 private final VlanId vlanId;
toma1d16b62014-10-02 23:45:11 -070041
tom80c0e5e2014-09-08 18:08:58 -070042 // Public construction is prohibited
tom545708e2014-10-09 17:10:02 -070043 private HostId(MacAddress mac, VlanId vlanId) {
44 this.mac = mac;
45 this.vlanId = vlanId;
46 }
47
48 // Default constructor for serialization
49 private HostId() {
50 this.mac = null;
51 this.vlanId = null;
tom80c0e5e2014-09-08 18:08:58 -070052 }
53
54 /**
tom545708e2014-10-09 17:10:02 -070055 * Returns the host MAC address.
tom80c0e5e2014-09-08 18:08:58 -070056 *
tom545708e2014-10-09 17:10:02 -070057 * @return MAC address
tom80c0e5e2014-09-08 18:08:58 -070058 */
tom545708e2014-10-09 17:10:02 -070059 public MacAddress mac() {
60 return mac;
tom80c0e5e2014-09-08 18:08:58 -070061 }
62
63 /**
Kedar Gupta47bd4802015-07-15 09:43:26 -070064 * Returns the host vlan Id.
tom545708e2014-10-09 17:10:02 -070065 *
Kedar Gupta47bd4802015-07-15 09:43:26 -070066 * @return vlan Id
tom545708e2014-10-09 17:10:02 -070067 */
68 public VlanId vlanId() {
69 return vlanId;
70 }
71
72 /**
73 * Creates a device id using the supplied ID string.
tom80c0e5e2014-09-08 18:08:58 -070074 *
75 * @param string device URI string
tom7869ad92014-09-09 14:32:08 -070076 * @return host identifier
tom80c0e5e2014-09-08 18:08:58 -070077 */
78 public static HostId hostId(String string) {
tom545708e2014-10-09 17:10:02 -070079 checkArgument(string.length() >= MIN_ID_LENGTH,
80 "Host ID must be at least %s characters", MIN_ID_LENGTH);
81 MacAddress mac = MacAddress.valueOf(string.substring(0, MAC_LENGTH));
Luca Prete283a9622016-03-29 16:12:20 -070082 VlanId vlanId = VlanId.vlanId(string.substring(MAC_LENGTH + 1));
tom545708e2014-10-09 17:10:02 -070083 return new HostId(mac, vlanId);
tom80c0e5e2014-09-08 18:08:58 -070084 }
85
tom7869ad92014-09-09 14:32:08 -070086 /**
87 * Creates a device id using the supplied MAC & VLAN ID.
88 *
89 * @param mac mac address
90 * @param vlanId vlan identifier
91 * @return host identifier
92 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070093 public static HostId hostId(MacAddress mac, VlanId vlanId) {
tom545708e2014-10-09 17:10:02 -070094 return new HostId(mac, vlanId);
tom7869ad92014-09-09 14:32:08 -070095 }
96
tomc370ebd2014-09-16 01:25:21 -070097 /**
98 * Creates a device id using the supplied MAC and default VLAN.
99 *
100 * @param mac mac address
101 * @return host identifier
102 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700103 public static HostId hostId(MacAddress mac) {
104 return hostId(mac, VlanId.vlanId(VlanId.UNTAGGED));
tomc370ebd2014-09-16 01:25:21 -0700105 }
106
tom545708e2014-10-09 17:10:02 -0700107 public String toString() {
108 return mac + "/" + vlanId;
109 }
110
111 @Override
112 public int hashCode() {
113 return Objects.hash(mac, vlanId);
114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (this == obj) {
119 return true;
120 }
121 if (obj instanceof HostId) {
122 final HostId other = (HostId) obj;
123 return Objects.equals(this.mac, other.mac) &&
124 Objects.equals(this.vlanId, other.vlanId);
125 }
126 return false;
127 }
128
tom80c0e5e2014-09-08 18:08:58 -0700129}