Merge "Randomize port to allow concurrent execution"
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / tlv / NodeIdentifierTlv.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 package org.opendaylight.protocol.pcep.tlv;
9
10 import java.nio.ByteBuffer;
11 import java.nio.charset.CharacterCodingException;
12 import java.nio.charset.Charset;
13 import java.util.Arrays;
14
15 import org.opendaylight.protocol.pcep.PCEPTlv;
16
17 /**
18  * Structure of Node Identifier TLV.
19  *
20  * @see draft-ietf-pce-stateful-pce-01 (sec. 7.1.3) - NODE_IDENTIFIER_TLV
21  */
22 public class NodeIdentifierTlv implements PCEPTlv {
23         private static final long serialVersionUID = -7959631526276210055L;
24         private final byte[] value;
25
26         /**
27          * Constructs new Node Identifier TLV.
28          *
29          * @param value
30          *            byte[]
31          */
32         public NodeIdentifierTlv(byte[] value) {
33                 if (value == null)
34                         throw new IllegalArgumentException("Value is mandatory.");
35                 if (value.length == 0)
36                         throw new IllegalArgumentException("Value has to be long at least 1 byte.");
37
38                 this.value = value;
39         }
40
41         /**
42          * Gets value of Node Identifier TLV as Bytes Array.
43          *
44          * @return byte[]
45          */
46         public byte[] getValue() {
47                 return this.value;
48         }
49
50         @Override
51         public int hashCode() {
52                 final int prime = 31;
53                 int result = 1;
54                 result = prime * result + Arrays.hashCode(this.value);
55                 return result;
56         }
57
58         @Override
59         public boolean equals(Object obj) {
60                 if (this == obj)
61                         return true;
62                 if (obj == null)
63                         return false;
64                 if (this.getClass() != obj.getClass())
65                         return false;
66                 final NodeIdentifierTlv other = (NodeIdentifierTlv) obj;
67                 if (!Arrays.equals(this.value, other.value))
68                         return false;
69                 return true;
70         }
71
72         @Override
73         public String toString() {
74                 final StringBuilder builder = new StringBuilder();
75                 builder.append("NodeIdentifierTlv [value=");
76                 try {
77                         builder.append(Charset.forName("UTF-8").newDecoder().decode(ByteBuffer.wrap(this.value)).toString());
78                 } catch (final CharacterCodingException e) {
79                         builder.append(Arrays.toString(this.value));
80                 }
81                 builder.append("]");
82                 return builder.toString();
83         }
84 }