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(final byte[] mac) {
37 return new EthernetAddress(mac);
38 } catch (final 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 public EthernetAddress(final byte[] macAddress) throws ConstructionException {
59 if (macAddress == null) {
60 throw new ConstructionException("Null input parameter passed");
63 if (macAddress.length != SIZE) {
64 throw new ConstructionException(
65 "Wrong size of passed byte array, expected:" + SIZE
66 + " got:" + macAddress.length);
68 this.macAddress = new byte[SIZE];
69 System.arraycopy(macAddress, 0, this.macAddress, 0, SIZE);
72 public EthernetAddress clone() {
74 return new EthernetAddress(this.macAddress.clone());
75 } catch (final ConstructionException ce) {
81 * Return the Ethernet Mac address in byte array format
83 * @return The Ethernet Mac address in byte array format
85 public byte[] getValue() {
86 return this.macAddress;
90 public int hashCode() {
92 int result = super.hashCode();
93 result = prime * result + Arrays.hashCode(macAddress);
98 public boolean equals(final Object obj) {
102 if (!super.equals(obj)) {
105 if (getClass() != obj.getClass()) {
108 EthernetAddress other = (EthernetAddress) obj;
109 if (!Arrays.equals(macAddress, other.macAddress)) {
116 public String toString() {
117 return "EthernetAddress [macAddress=" + HexEncode.bytesToHexStringFormat(macAddress)
121 @XmlElement(name = "macAddress")
122 public String getMacAddress() {
123 return HexEncode.bytesToHexStringFormat(macAddress);