BUG-2218: Keep existing link augmentations during discovery process
[controller.git] / opendaylight / adsal / 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 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 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     @XmlTransient
28     private byte[] macAddress;
29
30     public static final EthernetAddress BROADCASTMAC = createWellKnownAddress(new byte[] {
31             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
32             (byte) 0xff });
33
34     public static final EthernetAddress INVALIDHOST = BROADCASTMAC;
35
36     public static final String addressName = "Ethernet MAC Address";
37     public static final int SIZE = 6;
38
39     private static final EthernetAddress createWellKnownAddress(byte[] mac) {
40         try {
41             return new EthernetAddress(mac);
42         } catch (ConstructionException ce) {
43             return null;
44         }
45     }
46
47     /* Private constructor to satisfy JAXB */
48     @SuppressWarnings("unused")
49     private EthernetAddress() {
50     }
51
52     /**
53      * Public constructor for an Ethernet MAC address starting from
54      * the byte constituing the address, the constructor validate the
55      * size of the arrive to make sure it met the expected size
56      *
57      * @param macAddress A byte array in big endian format
58      * representing the Ethernet MAC Address
59      *
60      * @return The constructed object if valid
61      */
62     public EthernetAddress(byte[] macAddress) throws ConstructionException {
63         super(addressName);
64
65         if (macAddress == null) {
66             throw new ConstructionException("Null input parameter passed");
67         }
68
69         if (macAddress.length != SIZE) {
70             throw new ConstructionException(
71                     "Wrong size of passed byte array, expected:" + SIZE
72                             + " got:" + macAddress.length);
73         }
74         this.macAddress = new byte[SIZE];
75         System.arraycopy(macAddress, 0, this.macAddress, 0, SIZE);
76     }
77
78     public EthernetAddress clone() {
79         try {
80             return new EthernetAddress(this.macAddress.clone());
81         } catch (ConstructionException ce) {
82             return null;
83         }
84     }
85
86     /**
87      * Return the Ethernet Mac address in byte array format
88      *
89      * @return The Ethernet Mac address in byte array format
90      */
91     public byte[] getValue() {
92         return this.macAddress;
93     }
94
95     @Override
96     public int hashCode() {
97         final int prime = 31;
98         int result = super.hashCode();
99         result = prime * result + Arrays.hashCode(macAddress);
100         return result;
101     }
102
103     @Override
104     public boolean equals(Object obj) {
105         if (this == obj)
106             return true;
107         if (!super.equals(obj))
108             return false;
109         if (getClass() != obj.getClass())
110             return false;
111         EthernetAddress other = (EthernetAddress) obj;
112         if (!Arrays.equals(macAddress, other.macAddress))
113             return false;
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 }