Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
| 16 | package org.onosproject.lisp.msg.types; |
| 17 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 18 | import io.netty.buffer.ByteBuf; |
Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 19 | import org.onlab.packet.MacAddress; |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 20 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame^] | 21 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 22 | |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | |
Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 25 | /** |
| 26 | * MAC address that is used by LISP Locator. |
| 27 | */ |
| 28 | public class LispMacAddress extends LispAfiAddress { |
| 29 | |
| 30 | protected final MacAddress address; |
| 31 | |
| 32 | /** |
| 33 | * Initializes MAC address. |
| 34 | * |
| 35 | * @param address MAC address |
| 36 | */ |
| 37 | public LispMacAddress(MacAddress address) { |
| 38 | super(AddressFamilyIdentifierEnum.MAC); |
| 39 | this.address = address; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Obtains LISP locator's MAC address. |
| 44 | * |
| 45 | * @return MAC address |
| 46 | */ |
| 47 | public MacAddress getAddress() { |
| 48 | return address; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public int hashCode() { |
| 53 | return address.hashCode(); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean equals(Object obj) { |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 58 | if (this == obj) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | if (obj instanceof LispMacAddress) { |
| 63 | final LispMacAddress other = (LispMacAddress) obj; |
| 64 | return Objects.equals(this.address, other.address) && |
| 65 | Objects.equals(this.getAfi(), other.getAfi()); |
| 66 | } |
| 67 | return false; |
Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String toString() { |
| 72 | return address.toString(); |
| 73 | } |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * MAC address reader class. |
| 77 | */ |
| 78 | public static class MacAddressReader implements LispAddressReader<LispMacAddress> { |
| 79 | |
| 80 | private static final int SIZE_OF_MAC_ADDRESS = 6; |
| 81 | |
| 82 | @Override |
| 83 | public LispMacAddress readFrom(ByteBuf byteBuf) throws LispParseError { |
| 84 | |
| 85 | byte[] macByte = new byte[SIZE_OF_MAC_ADDRESS]; |
| 86 | byteBuf.readBytes(macByte); |
| 87 | MacAddress macAddress = MacAddress.valueOf(macByte); |
| 88 | |
| 89 | return new LispMacAddress(macAddress); |
| 90 | } |
| 91 | } |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame^] | 92 | |
| 93 | /** |
| 94 | * MAC address writer class. |
| 95 | */ |
| 96 | public static class MacAddressWriter implements LispAddressWriter<LispMacAddress> { |
| 97 | |
| 98 | @Override |
| 99 | public void writeTo(ByteBuf byteBuf, LispMacAddress address) throws LispWriterException { |
| 100 | byte[] macByte = address.getAddress().toBytes(); |
| 101 | byteBuf.writeBytes(macByte); |
| 102 | } |
| 103 | } |
Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 104 | } |