fb75532e395caa8712b91203fa4c849913a257fe
[controller.git] / opendaylight / commons / liblldp / src / main / java / org / opendaylight / controller / liblldp / EthernetAddress.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.liblldp;
10
11 import java.util.Arrays;
12
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;
18
19 @XmlRootElement
20 @XmlAccessorType(XmlAccessType.NONE)
21 public class EthernetAddress extends DataLinkAddress {
22     private static final long serialVersionUID = 1L;
23     @XmlTransient
24     private byte[] macAddress;
25
26     public static final EthernetAddress BROADCASTMAC = createWellKnownAddress(new byte[] {
27             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
28             (byte) 0xff });
29
30     public static final EthernetAddress INVALIDHOST = BROADCASTMAC;
31
32     public static final String addressName = "Ethernet MAC Address";
33     public static final int SIZE = 6;
34
35     private static final EthernetAddress createWellKnownAddress(final byte[] mac) {
36         try {
37             return new EthernetAddress(mac);
38         } catch (final ConstructionException ce) {
39             return null;
40         }
41     }
42
43     /* Private constructor to satisfy JAXB */
44     @SuppressWarnings("unused")
45     private EthernetAddress() {
46     }
47
48     /**
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
52      *
53      * @param macAddress A byte array in big endian format
54      * representing the Ethernet MAC Address
55      */
56     public EthernetAddress(final byte[] macAddress) throws ConstructionException {
57         super(addressName);
58
59         if (macAddress == null) {
60             throw new ConstructionException("Null input parameter passed");
61         }
62
63         if (macAddress.length != SIZE) {
64             throw new ConstructionException(
65                     "Wrong size of passed byte array, expected:" + SIZE
66                             + " got:" + macAddress.length);
67         }
68         this.macAddress = new byte[SIZE];
69         System.arraycopy(macAddress, 0, this.macAddress, 0, SIZE);
70     }
71
72     public EthernetAddress clone() {
73         try {
74             return new EthernetAddress(this.macAddress.clone());
75         } catch (final ConstructionException ce) {
76             return null;
77         }
78     }
79
80     /**
81      * Return the Ethernet Mac address in byte array format
82      *
83      * @return The Ethernet Mac address in byte array format
84      */
85     public byte[] getValue() {
86         return this.macAddress;
87     }
88
89     @Override
90     public int hashCode() {
91         final int prime = 31;
92         int result = super.hashCode();
93         result = prime * result + Arrays.hashCode(macAddress);
94         return result;
95     }
96
97     @Override
98     public boolean equals(final Object obj) {
99         if (this == obj) {
100             return true;
101         }
102         if (!super.equals(obj)) {
103             return false;
104         }
105         if (getClass() != obj.getClass()) {
106             return false;
107         }
108         EthernetAddress other = (EthernetAddress) obj;
109         if (!Arrays.equals(macAddress, other.macAddress)) {
110             return false;
111         }
112         return true;
113     }
114
115     @Override
116     public String toString() {
117         return "EthernetAddress [macAddress=" + HexEncode.bytesToHexStringFormat(macAddress)
118                 + "]";
119     }
120
121     @XmlElement(name = "macAddress")
122     public String getMacAddress() {
123         return HexEncode.bytesToHexStringFormat(macAddress);
124     }
125 }