blob: 6828581912efcd6aa5670583e76bcfbe5261df3d [file] [log] [blame]
Jian Li6b77dc02016-07-14 16:04:25 +09001/*
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 */
16package org.onosproject.lisp.msg.types;
17
Jian Li115d8602016-08-15 20:21:53 +090018import io.netty.buffer.ByteBuf;
Jian Li6b77dc02016-07-14 16:04:25 +090019import org.onlab.packet.MacAddress;
Jian Li115d8602016-08-15 20:21:53 +090020import org.onosproject.lisp.msg.exceptions.LispParseError;
GUNiba871702016-08-22 21:06:02 +090021import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li6b77dc02016-07-14 16:04:25 +090022
Jian Lie9af3b42016-08-08 15:50:01 +090023import java.util.Objects;
24
Jian Li6b77dc02016-07-14 16:04:25 +090025/**
26 * MAC address that is used by LISP Locator.
27 */
28public 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 Lie9af3b42016-08-08 15:50:01 +090058 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 Li6b77dc02016-07-14 16:04:25 +090068 }
69
70 @Override
71 public String toString() {
72 return address.toString();
73 }
Jian Li115d8602016-08-15 20:21:53 +090074
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 }
GUNiba871702016-08-22 21:06:02 +090092
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 Li6b77dc02016-07-14 16:04:25 +0900104}