3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.packet.address;
12 import javax.xml.bind.annotation.XmlAccessType;
13 import javax.xml.bind.annotation.XmlAccessorType;
14 import javax.xml.bind.annotation.XmlElement;
15 import javax.xml.bind.annotation.XmlRootElement;
17 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
18 import org.apache.commons.lang3.builder.EqualsBuilder;
19 import org.apache.commons.lang3.builder.HashCodeBuilder;
20 import org.opendaylight.controller.sal.core.ConstructionException;
21 import org.opendaylight.controller.sal.utils.HexEncode;
24 @XmlAccessorType(XmlAccessType.NONE)
25 public class EthernetAddress extends DataLinkAddress {
26 private static final long serialVersionUID = 1L;
27 private byte[] macAddress;
29 public static final EthernetAddress BROADCASTMAC = createWellKnownAddress(new byte[] {
30 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
33 public static final EthernetAddress INVALIDHOST = BROADCASTMAC;
35 public static final String addressName = "Ethernet MAC Address";
36 public static final int SIZE = 6;
38 private static final EthernetAddress createWellKnownAddress(byte[] mac) {
40 return new EthernetAddress(mac);
41 } catch (ConstructionException ce) {
46 /* Private constructor to satisfy JAXB */
47 private EthernetAddress() {
52 * Public constructor for an Ethernet MAC address starting from
53 * the byte constituing the address, the constructor validate the
54 * size of the arrive to make sure it met the expected size
56 * @param macAddress A byte array in big endian format
57 * representing the Ethernet MAC Address
59 * @return The constructed object if valid
61 public EthernetAddress(byte[] macAddress) throws ConstructionException {
64 if (macAddress == null) {
65 throw new ConstructionException("Null input parameter passed");
68 if (macAddress.length != SIZE) {
69 throw new ConstructionException(
70 "Wrong size of passed byte array, expected:" + SIZE
71 + " got:" + macAddress.length);
73 this.macAddress = new byte[SIZE];
74 System.arraycopy(macAddress, 0, this.macAddress, 0, SIZE);
77 public EthernetAddress clone() {
79 return new EthernetAddress(this.macAddress.clone());
80 } catch (ConstructionException ce) {
86 * Return the Ethernet Mac address in byte array format
88 * @return The Ethernet Mac address in byte array format
90 public byte[] getValue() {
91 return this.macAddress;
95 public int hashCode() {
96 return HashCodeBuilder.reflectionHashCode(this);
100 public boolean equals(Object obj) {
101 return EqualsBuilder.reflectionEquals(this, obj);
105 public String toString() {
106 return "EthernetAddress[" + ReflectionToStringBuilder.toString(this)
110 @XmlElement(name = "macAddress")
111 public String getMacAddress() {
112 return HexEncode.bytesToHexString(macAddress);