Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / address / 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.sal.packet.address;
11
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;
16
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;
22
23 @XmlRootElement
24 @XmlAccessorType(XmlAccessType.NONE)
25 public class EthernetAddress extends DataLinkAddress {
26     private static final long serialVersionUID = 1L;
27     private byte[] macAddress;
28
29     public static final EthernetAddress BROADCASTMAC = createWellKnownAddress(new byte[] {
30             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
31             (byte) 0xff });
32
33     public static final EthernetAddress INVALIDHOST = BROADCASTMAC;
34
35     public static final String addressName = "Ethernet MAC Address";
36     public static final int SIZE = 6;
37
38     private static final EthernetAddress createWellKnownAddress(byte[] mac) {
39         try {
40             return new EthernetAddress(mac);
41         } catch (ConstructionException ce) {
42             return null;
43         }
44     }
45
46     /* Private constructor to satisfy JAXB */
47     private EthernetAddress() {
48
49     }
50
51     /**
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
55      *
56      * @param macAddress A byte array in big endian format
57      * representing the Ethernet MAC Address
58      *
59      * @return The constructed object if valid
60      */
61     public EthernetAddress(byte[] macAddress) throws ConstructionException {
62         super(addressName);
63
64         if (macAddress == null) {
65             throw new ConstructionException("Null input parameter passed");
66         }
67
68         if (macAddress.length != SIZE) {
69             throw new ConstructionException(
70                     "Wrong size of passed byte array, expected:" + SIZE
71                             + " got:" + macAddress.length);
72         }
73         this.macAddress = new byte[SIZE];
74         System.arraycopy(macAddress, 0, this.macAddress, 0, SIZE);
75     }
76
77     public EthernetAddress clone() {
78         try {
79             return new EthernetAddress(this.macAddress.clone());
80         } catch (ConstructionException ce) {
81             return null;
82         }
83     }
84
85     /**
86      * Return the Ethernet Mac address in byte array format
87      *
88      * @return The Ethernet Mac address in byte array format
89      */
90     public byte[] getValue() {
91         return this.macAddress;
92     }
93
94     @Override
95     public int hashCode() {
96         return HashCodeBuilder.reflectionHashCode(this);
97     }
98
99     @Override
100     public boolean equals(Object obj) {
101         return EqualsBuilder.reflectionEquals(this, obj);
102     }
103
104     @Override
105     public String toString() {
106         return "EthernetAddress[" + ReflectionToStringBuilder.toString(this)
107                 + "]";
108     }
109
110     @XmlElement(name = "macAddress")
111     public String getMacAddress() {
112         return HexEncode.bytesToHexString(macAddress);
113     }
114 }