1af332ebab2132b502b805bf18b40e73ab0a1bcd
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onosproject.pcepio.types;
17
18 import java.util.Objects;
19 import org.jboss.netty.buffer.ChannelBuffer;
20 import org.onosproject.pcepio.protocol.PcepVersion;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.common.base.MoreObjects;
25 import com.google.common.base.MoreObjects.ToStringHelper;
26
27 /**
28  * Provides Opaque Link Attribute.
29  */
30 public class OpaqueLinkAttributeTlv implements PcepValueType {
31
32     /*
33      * TLV format.
34      * Reference :[I-D.ietf-idr-attributesls-distribution] /3.3.2.6
35       0                   1                   2                   3
36       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
37      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38      |              Type=TBD42       |             Length            |
39      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40      //                Opaque link attributes (variable)            //
41      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42      */
43
44     protected static final Logger log = LoggerFactory.getLogger(OpaqueLinkAttributeTlv.class);
45
46     public static final short TYPE = 1097; //TODO:NEED TO HANDLE TDB42
47     private final short hLength;
48
49     private final byte[] rawValue;
50
51     /**
52      * constructor to initialize rawValue.
53      *
54      * @param rawValue of Opaque Link Attribute
55      * @param hLength length
56      */
57     public OpaqueLinkAttributeTlv(byte[] rawValue, short hLength) {
58         log.debug("OpaqueLinkAttributeTlv");
59         this.rawValue = rawValue;
60         if (0 == hLength) {
61             this.hLength = (short) rawValue.length;
62         } else {
63             this.hLength = hLength;
64         }
65     }
66
67     /**
68      * Returns newly created OpaqueLinkAttributeTlv object.
69      *
70      * @param raw of Opaque Link Attribute
71      * @param hLength length
72      * @return new object of OpaqueLinkAttributeTlv
73      */
74     public static OpaqueLinkAttributeTlv of(final byte[] raw, short hLength) {
75         return new OpaqueLinkAttributeTlv(raw, hLength);
76     }
77
78     /**
79      * Returns raw value of Opaque Link Attribute Tlv.
80      * @return rawValue raw value
81      */
82     public byte[] getValue() {
83         return rawValue;
84     }
85
86     @Override
87     public PcepVersion getVersion() {
88         return PcepVersion.PCEP_1;
89     }
90
91     @Override
92     public short getType() {
93         return TYPE;
94     }
95
96     @Override
97     public short getLength() {
98         return hLength;
99     }
100
101     @Override
102     public int hashCode() {
103         return Objects.hash(rawValue);
104     }
105
106     @Override
107     public boolean equals(Object obj) {
108         if (this == obj) {
109             return true;
110         }
111         if (obj instanceof OpaqueLinkAttributeTlv) {
112             OpaqueLinkAttributeTlv other = (OpaqueLinkAttributeTlv) obj;
113             return Objects.equals(this.rawValue, other.rawValue);
114         }
115         return false;
116     }
117
118     @Override
119     public int write(ChannelBuffer c) {
120         int iLenStartIndex = c.writerIndex();
121         c.writeShort(TYPE);
122         c.writeShort(hLength);
123         c.writeBytes(rawValue);
124         return c.writerIndex() - iLenStartIndex;
125     }
126
127     /**
128      * Reads the channel buffer and returns object of OpaqueLinkAttributeTlv.
129      *
130      * @param c input channel buffer
131      * @param hLength length
132      * @return object of Opaque Link Attribute Tlv
133      */
134     public static PcepValueType read(ChannelBuffer c, short hLength) {
135         byte[] iOpaqueValue = new byte[hLength];
136         c.readBytes(iOpaqueValue, 0, hLength);
137         return new OpaqueLinkAttributeTlv(iOpaqueValue, hLength);
138     }
139
140     @Override
141     public String toString() {
142         ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
143
144         toStrHelper.add("Type", TYPE);
145         toStrHelper.add("Length", hLength);
146
147         StringBuffer result = new StringBuffer();
148         for (byte b : rawValue) {
149             result.append(String.format("%02X ", b));
150         }
151         toStrHelper.add("Value", result);
152
153         return toStrHelper.toString();
154     }
155 }