a3f8d5075d6728c569e631a7c0f8f51766145bb4
[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(byte[] mac) {
36         try {
37             return new EthernetAddress(mac);
38         } catch (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      * @return The constructed object if valid
57      */
58     public EthernetAddress(byte[] macAddress) throws ConstructionException {
59         super(addressName);
60
61         if (macAddress == null) {
62             throw new ConstructionException("Null input parameter passed");
63         }
64
65         if (macAddress.length != SIZE) {
66             throw new ConstructionException(
67                     "Wrong size of passed byte array, expected:" + SIZE
68                             + " got:" + macAddress.length);
69         }
70         this.macAddress = new byte[SIZE];
71         System.arraycopy(macAddress, 0, this.macAddress, 0, SIZE);
72     }
73
74     public EthernetAddress clone() {
75         try {
76             return new EthernetAddress(this.macAddress.clone());
77         } catch (ConstructionException ce) {
78             return null;
79         }
80     }
81
82     /**
83      * Return the Ethernet Mac address in byte array format
84      *
85      * @return The Ethernet Mac address in byte array format
86      */
87     public byte[] getValue() {
88         return this.macAddress;
89     }
90
91     @Override
92     public int hashCode() {
93         final int prime = 31;
94         int result = super.hashCode();
95         result = prime * result + Arrays.hashCode(macAddress);
96         return result;
97     }
98
99     @Override
100     public boolean equals(Object obj) {
101         if (this == obj) {
102             return true;
103         }
104         if (!super.equals(obj)) {
105             return false;
106         }
107         if (getClass() != obj.getClass()) {
108             return false;
109         }
110         EthernetAddress other = (EthernetAddress) obj;
111         if (!Arrays.equals(macAddress, other.macAddress)) {
112             return false;
113         }
114         return true;
115     }
116
117     @Override
118     public String toString() {
119         return "EthernetAddress [macAddress=" + HexEncode.bytesToHexStringFormat(macAddress)
120                 + "]";
121     }
122
123     @XmlElement(name = "macAddress")
124     public String getMacAddress() {
125         return HexEncode.bytesToHexStringFormat(macAddress);
126     }
127 }