Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 17 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 18 | import org.onlab.packet.MacAddress; |
| 19 | import org.onlab.packet.VlanId; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 20 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Immutable representation of a host identity. |
| 27 | */ |
| 28 | public final class HostId extends ElementId { |
| 29 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Represents either no host, or an unspecified host; used for creating |
| 32 | * open ingress/egress edge links. |
| 33 | */ |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 34 | 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; |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 41 | |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 42 | // Public construction is prohibited |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 43 | 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; |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 55 | * Returns the host MAC address. |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 56 | * |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 57 | * @return MAC address |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 58 | */ |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 59 | public MacAddress mac() { |
| 60 | return mac; |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 64 | * Returns the host vlan Id. |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 65 | * |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 66 | * @return vlan Id |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 67 | */ |
| 68 | public VlanId vlanId() { |
| 69 | return vlanId; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Creates a device id using the supplied ID string. |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 74 | * |
| 75 | * @param string device URI string |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 76 | * @return host identifier |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 77 | */ |
| 78 | public static HostId hostId(String string) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 79 | 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)); |
| 82 | VlanId vlanId = VlanId.vlanId(Short.parseShort(string.substring(MAC_LENGTH + 1))); |
| 83 | return new HostId(mac, vlanId); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 84 | } |
| 85 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 86 | /** |
| 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 Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 93 | public static HostId hostId(MacAddress mac, VlanId vlanId) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 94 | return new HostId(mac, vlanId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 95 | } |
| 96 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Creates a device id using the supplied MAC and default VLAN. |
| 99 | * |
| 100 | * @param mac mac address |
| 101 | * @return host identifier |
| 102 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 103 | public static HostId hostId(MacAddress mac) { |
| 104 | return hostId(mac, VlanId.vlanId(VlanId.UNTAGGED)); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 105 | } |
| 106 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 107 | 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 | |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 129 | } |