2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.liblldp;
11 import java.util.Arrays;
13 import javax.xml.bind.annotation.XmlAccessType;
14 import javax.xml.bind.annotation.XmlAccessorType;
15 import javax.xml.bind.annotation.XmlElement;
16 import javax.xml.bind.annotation.XmlRootElement;
17 import javax.xml.bind.annotation.XmlTransient;
20 @XmlAccessorType(XmlAccessType.NONE)
21 public class EthernetAddress extends DataLinkAddress {
22 private static final long serialVersionUID = 1L;
24 private byte[] macAddress;
26 public static final EthernetAddress BROADCASTMAC = createWellKnownAddress(new byte[] {
27 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
30 public static final EthernetAddress INVALIDHOST = BROADCASTMAC;
32 public static final String addressName = "Ethernet MAC Address";
33 public static final int SIZE = 6;
35 private static final EthernetAddress createWellKnownAddress(byte[] mac) {
37 return new EthernetAddress(mac);
38 } catch (ConstructionException ce) {
43 /* Private constructor to satisfy JAXB */
44 @SuppressWarnings("unused")
45 private EthernetAddress() {
49 * Public constructor for an Ethernet MAC address starting from
50 * the byte constituing the address, the constructor validate the
51 * size of the arrive to make sure it met the expected size
53 * @param macAddress A byte array in big endian format
54 * representing the Ethernet MAC Address
56 * @return The constructed object if valid
58 public EthernetAddress(byte[] macAddress) throws ConstructionException {
61 if (macAddress == null) {
62 throw new ConstructionException("Null input parameter passed");
65 if (macAddress.length != SIZE) {
66 throw new ConstructionException(
67 "Wrong size of passed byte array, expected:" + SIZE
68 + " got:" + macAddress.length);
70 this.macAddress = new byte[SIZE];
71 System.arraycopy(macAddress, 0, this.macAddress, 0, SIZE);
74 public EthernetAddress clone() {
76 return new EthernetAddress(this.macAddress.clone());
77 } catch (ConstructionException ce) {
83 * Return the Ethernet Mac address in byte array format
85 * @return The Ethernet Mac address in byte array format
87 public byte[] getValue() {
88 return this.macAddress;
92 public int hashCode() {
94 int result = super.hashCode();
95 result = prime * result + Arrays.hashCode(macAddress);
100 public boolean equals(Object obj) {
104 if (!super.equals(obj)) {
107 if (getClass() != obj.getClass()) {
110 EthernetAddress other = (EthernetAddress) obj;
111 if (!Arrays.equals(macAddress, other.macAddress)) {
118 public String toString() {
119 return "EthernetAddress [macAddress=" + HexEncode.bytesToHexStringFormat(macAddress)
123 @XmlElement(name = "macAddress")
124 public String getMacAddress() {
125 return HexEncode.bytesToHexStringFormat(macAddress);