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