edb3537c1273c9f6a02cc956b64e481161914b08
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / core / MacAddress.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.sal.core;
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
18 import org.opendaylight.controller.sal.utils.HexEncode;
19 /**
20  * The class contains MAC address property.
21  */
22 @XmlRootElement
23 @XmlAccessorType(XmlAccessType.NONE)
24 public class MacAddress extends Property implements Cloneable {
25     private static final long serialVersionUID = 1L;
26     @XmlElement(name="macAddress")
27     private final byte[] address;
28     public static final String name = "macAddress";
29
30     /*
31      * Private constructor used for JAXB mapping
32      */
33     private MacAddress() {
34         super(name);
35         this.address = null;
36     }
37
38     /**
39      * Constructor to create DatalinkAddress property which contains the MAC
40      * address. The property will be attached to a
41      * {@link org.opendaylight.controller.sal.core.Node}.
42      *
43      *
44      * @param nodeMacAddress
45      *            Data Link Address for the node
46      *
47      * @return the constructed object
48      */
49     public MacAddress(byte[] nodeMacAddress) {
50         super(name);
51         this.address = nodeMacAddress.clone();
52     }
53
54     /**
55      * @return the node MAC address
56      */
57     public byte[] getMacAddress() {
58         return this.address.clone();
59     }
60
61     @Override
62     public MacAddress clone() {
63         return new MacAddress(this.address);
64     }
65
66     @Override
67     public int hashCode() {
68         final int prime = 31;
69         int result = super.hashCode();
70         result = prime * result + Arrays.hashCode(address);
71         return result;
72     }
73
74     @Override
75     public boolean equals(Object obj) {
76         if (this == obj) {
77             return true;
78         }
79         if (!super.equals(obj)) {
80             return false;
81         }
82         if (getClass() != obj.getClass()) {
83             return false;
84         }
85         MacAddress other = (MacAddress) obj;
86         if (!Arrays.equals(address, other.address)) {
87             return false;
88         }
89         return true;
90     }
91
92     @Override
93     public String toString() {
94         return "MacAddress [address=" +
95             HexEncode.bytesToHexStringFormat(address) + "]";
96     }
97 }