blob: 1a0419cd36f950bc761572cc0d7e3ed95b1aad0c [file] [log] [blame]
bharat saraswalf7364db2015-08-11 13:39:31 +05301/*
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 */
16package org.onosproject.pcepio.types;
17
18import java.util.LinkedList;
19import java.util.ListIterator;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.pcepio.exceptions.PcepParseException;
23import org.onosproject.pcepio.protocol.PcepVersion;
24
25import com.google.common.base.MoreObjects;
26
27public class PcepRsvpUserErrorSpec implements PcepRsvpErrorSpec {
28
29 /*
30 RSVP error spec object header.
31 0 1 2 3
32 +-------------+-------------+-------------+-------------+
33 | Length (bytes) | Class-Num | C-Type |
34 +-------------+-------------+-------------+-------------+
35 | |
36 // (Object contents) //
37 | |
38 +-------------+-------------+-------------+-------------+
39
40 Ref : USER_ERROR_SPEC @ RFC5284.
41 USER_ERROR_SPEC object: Class = 194, C-Type = 1
42
43 0 1 2 3
44 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
45 +---------------+---------------+---------------+---------------+
46 | Enterprise Number |
47 +---------------+---------------+---------------+---------------+
48 | Sub Org | Err Desc Len | User Error Value |
49 +---------------+---------------+---------------+---------------+
50 | |
51 ~ Error Description ~
52 | |
53 +---------------+---------------+---------------+---------------+
54 | |
55 ~ User-Defined Subobjects ~
56 | |
57 +---------------+---------------+---------------+---------------+
58 */
59
60 public static final byte CLASS_NUM = (byte) 0xc2;
61 public static final byte CLASS_TYPE = 0x01;
62
63 private PcepRsvpSpecObjHeader objHeader;
64 private int enterpriseNum;
65 private byte subOrg;
66 private byte errDescLen;
67 private short userErrorValue;
68 private byte[] errDesc;
69 private LinkedList<PcepValueType> llRsvpUserSpecSubObj;
70
71 /**
72 * Default constructor.
73 *
74 * @param objHeader pcep rsvp spec object header
75 * @param enterpriseNum enterprise number
76 * @param subOrg organization identifier value
77 * @param errDescLen error description length
78 * @param userErrorValue user error value
79 * @param errDesc error description
80 * @param llRsvpUserSpecSubObj list of subobjects
81 */
82 public PcepRsvpUserErrorSpec(PcepRsvpSpecObjHeader objHeader, int enterpriseNum, byte subOrg, byte errDescLen,
83 short userErrorValue, byte[] errDesc, LinkedList<PcepValueType> llRsvpUserSpecSubObj) {
84 this.objHeader = objHeader;
85 this.enterpriseNum = enterpriseNum;
86 this.subOrg = subOrg;
87 this.errDescLen = errDescLen;
88 this.userErrorValue = userErrorValue;
89 this.errDesc = errDesc;
90 this.llRsvpUserSpecSubObj = llRsvpUserSpecSubObj;
91 }
92
93 @Override
94 public int write(ChannelBuffer cb) {
95 int objLenIndex = objHeader.write(cb);
96 cb.writeInt(enterpriseNum);
97 cb.writeByte(subOrg);
98 cb.writeByte(errDescLen);
99 cb.writeShort(userErrorValue);
100 cb.writeBytes(errDesc);
101
102 if (null != llRsvpUserSpecSubObj) {
103
104 ListIterator<PcepValueType> listIterator = llRsvpUserSpecSubObj.listIterator();
105
106 while (listIterator.hasNext()) {
107 PcepValueType tlv = listIterator.next();
108 if (null == tlv) {
109 continue;
110 }
111 tlv.write(cb);
112 // need to take care of padding
113 int pad = tlv.getLength() % 4;
114 if (0 != pad) {
115 pad = 4 - pad;
116 for (int i = 0; i < pad; ++i) {
117 cb.writeByte((byte) 0);
118 }
119 }
120 }
121 }
122 short objLen = (short) (cb.writerIndex() - objLenIndex);
123 cb.setShort(objLenIndex, objLen);
124 return objLen;
125 }
126
127 /**
128 * Reads the channel buffer and returns object of PcepRsvpErrorSpec.
129 *
130 * @param cb of type channel buffer
131 * @return object of PcepRsvpErrorSpec
132 * @throws PcepParseException when expected object is not received
133 */
134 public static PcepRsvpErrorSpec read(ChannelBuffer cb) throws PcepParseException {
135 PcepRsvpSpecObjHeader objHeader;
136 int enterpriseNum;
137 byte subOrg;
138 byte errDescLen;
139 short userErrorValue;
140 byte[] errDesc;
141 LinkedList<PcepValueType> llRsvpUserSpecSubObj = null;
142
143 objHeader = PcepRsvpSpecObjHeader.read(cb);
144
145 if (CLASS_NUM != objHeader.getObjClassNum() || CLASS_TYPE != objHeader.getObjClassType()) {
146 throw new PcepParseException("Expected PcepRsvpUserErrorSpec object.");
147 }
148 enterpriseNum = cb.readInt();
149 subOrg = cb.readByte();
150 errDescLen = cb.readByte();
151 userErrorValue = cb.readShort();
152 errDesc = new byte[errDescLen];
153 cb.readBytes(errDesc, 0, errDescLen);
154
155 llRsvpUserSpecSubObj = parseErrSpecSubObj(cb);
156
157 return new PcepRsvpUserErrorSpec(objHeader, enterpriseNum, subOrg, errDescLen, userErrorValue, errDesc,
158 llRsvpUserSpecSubObj);
159 }
160
161 private static LinkedList<PcepValueType> parseErrSpecSubObj(ChannelBuffer cb) throws PcepParseException {
162 LinkedList<PcepValueType> llRsvpUserSpecSubObj = new LinkedList<PcepValueType>();
163 while (0 < cb.readableBytes()) {
164 PcepValueType tlv = null;
165 short hType = cb.readShort();
166 int iValue = 0;
167 //short hLength = cb.readShort();
168 switch (hType) {
169 case AutonomousSystemTlv.TYPE:
170 iValue = cb.readInt();
171 tlv = new AutonomousSystemTlv(iValue);
172 break;
173 default:
174 throw new PcepParseException("Unsupported Sub TLV type :" + hType);
175 }
176 llRsvpUserSpecSubObj.add(tlv);
177 }
178 return llRsvpUserSpecSubObj;
179 }
180
181 @Override
182 public PcepVersion getVersion() {
183 return PcepVersion.PCEP_1;
184 }
185
186 @Override
187 public short getType() {
188 return StatefulRsvpErrorSpecTlv.TYPE;
189 }
190
191 @Override
192 public short getLength() {
193 return objHeader.getObjLen();
194 }
195
196 @Override
197 public byte getClassNum() {
198 return CLASS_NUM;
199 }
200
201 @Override
202 public byte getClassType() {
203 return CLASS_TYPE;
204 }
205
206 @Override
bharat saraswalf7364db2015-08-11 13:39:31 +0530207 public String toString() {
208 return MoreObjects.toStringHelper(getClass())
bharat saraswale1806302015-08-21 12:16:46 +0530209 .add("enterpriseNumber:", enterpriseNum)
210 .add("subOrganization:", subOrg)
211 .add("errDescLength:", errDescLen)
212 .add("userErrorValue:", userErrorValue)
213 .add("errDesc:", errDesc)
214 .add("RsvpUserSpecSubObject:", llRsvpUserSpecSubObj)
bharat saraswalf7364db2015-08-11 13:39:31 +0530215 .toString();
216 }
217}