blob: ad2cc4727667a92ef0c47e0fc115dbad149c9638 [file] [log] [blame]
Bharat saraswalc6f81bc2015-08-21 16:14:56 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Bharat saraswalc6f81bc2015-08-21 16:14:56 +05303 *
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
17package org.onosproject.pcepio.protocol.ver1;
18
19import java.util.LinkedList;
20import java.util.ListIterator;
21
22import org.jboss.netty.buffer.ChannelBuffer;
23import org.onosproject.pcepio.exceptions.PcepParseException;
24import org.onosproject.pcepio.protocol.PcepLspObject;
25import org.onosproject.pcepio.protocol.PcepMessageReader;
26import org.onosproject.pcepio.protocol.PcepMessageWriter;
27import org.onosproject.pcepio.protocol.PcepReportMsg;
28import org.onosproject.pcepio.protocol.PcepSrpObject;
29import org.onosproject.pcepio.protocol.PcepStateReport;
30import org.onosproject.pcepio.protocol.PcepType;
31import org.onosproject.pcepio.protocol.PcepVersion;
32import org.onosproject.pcepio.types.PcepObjectHeader;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import com.google.common.base.MoreObjects;
37
Phanendra Manda51fb9c22015-09-01 16:17:41 +053038/**
39 * Provides PCEP report message.
40 */
Bharat saraswalc6f81bc2015-08-21 16:14:56 +053041class PcepReportMsgVer1 implements PcepReportMsg {
42
43 // Pcep version: 1
44
45 /*
46 * The format of the PCRpt message is as follows:
47 * <PCRpt Message> ::= <Common Header>
48 * <state-report-list>
49 *Where:
50 * <state-report-list> ::= <state-report>[<state-report-list>]
51 * <state-report> ::= [<SRP>]
52 * <LSP>
53 * <path>
54 * Where:
55 * <path> ::= <ERO><attribute-list>[<RRO>]
56 * Where:
57 * <attribute-list> is defined in [RFC5440] and extended by PCEP extensions.
58 * where:
59 * <attribute-list> ::=[<LSPA>]
60 * [<BANDWIDTH>]
61 * [<metric-list>]
62 * [<IRO>]
63 * <metric-list> ::=<METRIC>[<metric-list>]
64 */
Ray Milkey9c9cde42018-01-12 14:22:06 -080065 private static final Logger log = LoggerFactory.getLogger(PcepReportMsgVer1.class);
Bharat saraswalc6f81bc2015-08-21 16:14:56 +053066
67 public static final byte PACKET_VERSION = 1;
Avantika-Huaweifb630482016-08-09 11:42:11 +053068 //PACKET_MINIMUM_LENGTH = CommonHeaderLen(4)+LspObjMinLen(8)
69 public static final int PACKET_MINIMUM_LENGTH = 12;
Bharat saraswalc6f81bc2015-08-21 16:14:56 +053070 public static final PcepType MSG_TYPE = PcepType.REPORT;
71 public static final byte REPORT_OBJ_TYPE = 1;
72 //Optional TLV
73 private LinkedList<PcepStateReport> llStateReportList;
74
75 public static final PcepReportMsgVer1.Reader READER = new Reader();
76
Phanendra Manda51fb9c22015-09-01 16:17:41 +053077 /**
78 * Reader class for reading PCEP report message from channel buffer.
79 */
Bharat saraswalc6f81bc2015-08-21 16:14:56 +053080 static class Reader implements PcepMessageReader<PcepReportMsg> {
81
82 LinkedList<PcepStateReport> llStateReportList;
83
84 @Override
85 public PcepReportMsg readFrom(ChannelBuffer cb) throws PcepParseException {
86
87 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
88 throw new PcepParseException("Received packet size " + cb.readableBytes()
89 + " is less than the expected size: " + PACKET_MINIMUM_LENGTH);
90 }
Sho SHIMIZU9b8274c2015-09-04 15:54:24 -070091 llStateReportList = new LinkedList<>();
Bharat saraswalc6f81bc2015-08-21 16:14:56 +053092 byte version = cb.readByte();
93 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
94
95 if (version != PACKET_VERSION) {
96 throw new PcepParseException(" Invalid version: " + version);
97 }
98
99 byte type = cb.readByte();
100
101 if (type != MSG_TYPE.getType()) {
102 throw new PcepParseException("Unexpected type: " + type);
103 }
104
105 short length = cb.readShort();
106
107 if (length < PACKET_MINIMUM_LENGTH) {
108 throw new PcepParseException("Wrong length. Expected to be >= " + PACKET_MINIMUM_LENGTH + ", was: "
109 + length);
110 }
111 // parse state report list
112 parseStateReportList(cb);
113 return new PcepReportMsgVer1(llStateReportList);
114 }
115
116 // Parse State Report list
117 public void parseStateReportList(ChannelBuffer cb) throws PcepParseException {
118
119 /*
120 <state-report-list>
121 Where:
122 <state-report-list> ::= <state-report>[<state-report-list>]
123 <state-report> ::= [<SRP>]
124 <LSP>
125 <path>
126 Where:
127 <path> ::= <ERO><attribute-list>[<RRO>]
128 Where:
129 <attribute-list> is defined in [RFC5440] and extended by PCEP extensions.
130
131 */
132
133 while (0 < cb.readableBytes()) {
134
135 PcepStateReport pcestateReq = new PcepStateReportVer1();
136
137 /*
138 * SRP is optional
139 * Check whether SRP Object is available, if yes store it.
140 * First read common object header and check the Object Class whether it is SRP or LSP
141 * If it is LSP then store only LSP. So, SRP is optional. then read path and store.
142 * If it is SRP then store SRP and then read LSP, path and store them.
143 */
144
145 //mark the reader index to reset
146 cb.markReaderIndex();
147 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
148
149 byte yObjectClass = tempObjHeader.getObjClass();
150 byte yObjectType = tempObjHeader.getObjType();
151
152 //reset reader index
153 cb.resetReaderIndex();
154 //If SRP present then store it.
155 if ((PcepSrpObjectVer1.SRP_OBJ_CLASS == yObjectClass)
156 && (PcepSrpObjectVer1.SRP_OBJ_TYPE == yObjectType)) {
157 PcepSrpObject srpObj;
158 srpObj = PcepSrpObjectVer1.read(cb);
159 pcestateReq.setSrpObject(srpObj);
160 }
161
162 //store LSP object
163 PcepLspObject lspObj;
164 lspObj = PcepLspObjectVer1.read(cb);
165 pcestateReq.setLspObject(lspObj);
166
Avantika-Huaweifb630482016-08-09 11:42:11 +0530167 if (cb.readableBytes() > 0) {
168
169 //mark the reader index to reset
170 cb.markReaderIndex();
171 tempObjHeader = PcepObjectHeader.read(cb);
172
173 yObjectClass = tempObjHeader.getObjClass();
174 yObjectType = tempObjHeader.getObjType();
175
176 //reset reader index
177 cb.resetReaderIndex();
178
179 if ((PcepEroObjectVer1.ERO_OBJ_CLASS == yObjectClass)
180 && (PcepEroObjectVer1.ERO_OBJ_TYPE == yObjectType)) {
181 // store path
182 PcepStateReport.PcepMsgPath msgPath = new PcepStateReportVer1().new PcepMsgPath().read(cb);
183 pcestateReq.setMsgPath(msgPath);
184 }
185 }
Bharat saraswalc6f81bc2015-08-21 16:14:56 +0530186
187 llStateReportList.add(pcestateReq);
188 }
189 }
190 }
191
192 /**
193 * Constructor to initialize State Report List.
194 *
195 * @param llStateReportList list of type Pcep state report
196 */
197 PcepReportMsgVer1(LinkedList<PcepStateReport> llStateReportList) {
198 this.llStateReportList = llStateReportList;
199 }
200
201 /**
202 * Builder class for PCEP Report message.
203 */
204 static class Builder implements PcepReportMsg.Builder {
205 // Pcep report message fields
206 LinkedList<PcepStateReport> llStateReportList;
207
208 @Override
209 public PcepVersion getVersion() {
210 return PcepVersion.PCEP_1;
211 }
212
213 @Override
214 public PcepType getType() {
215 return PcepType.REPORT;
216 }
217
218 @Override
219 public PcepReportMsg build() {
220 return new PcepReportMsgVer1(this.llStateReportList);
221 }
222
223 @Override
224 public LinkedList<PcepStateReport> getStateReportList() {
225 return this.llStateReportList;
226 }
227
228 @Override
229 public Builder setStateReportList(LinkedList<PcepStateReport> ll) {
230 this.llStateReportList = ll;
231 return this;
232 }
233 }
234
235 @Override
236 public void writeTo(ChannelBuffer cb) throws PcepParseException {
237 WRITER.write(cb, this);
238 }
239
240 static final Writer WRITER = new Writer();
241
Phanendra Manda51fb9c22015-09-01 16:17:41 +0530242 /**
243 * Writer class for writing PCEP report message to channel buffer.
244 */
Bharat saraswalc6f81bc2015-08-21 16:14:56 +0530245 static class Writer implements PcepMessageWriter<PcepReportMsgVer1> {
246
247 @Override
248 public void write(ChannelBuffer cb, PcepReportMsgVer1 message) throws PcepParseException {
249
250 int startIndex = cb.writerIndex();
251
252 // first 3 bits set to version
253 cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
254
255 // message type
256 cb.writeByte(MSG_TYPE.getType());
257
258 // length is length of variable message, will be updated at the end
259 // Store the position of message
260 // length in buffer
261 int msgLenIndex = cb.writerIndex();
262
263 cb.writeShort((short) 0);
264 ListIterator<PcepStateReport> listIterator = message.llStateReportList.listIterator();
265
266 while (listIterator.hasNext()) {
267
268 PcepStateReport stateRpt = listIterator.next();
269 PcepSrpObject srpObj = stateRpt.getSrpObject();
270
271 //SRP object is optional
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700272 if (srpObj != null) {
Bharat saraswalc6f81bc2015-08-21 16:14:56 +0530273 srpObj.write(cb);
274 }
275
276 //LSP object is mandatory
277 PcepLspObject lspObj = stateRpt.getLspObject();
278 if (lspObj == null) {
279 throw new PcepParseException("LSP Object is mandatory object for PcRpt message.");
280 } else {
281 lspObj.write(cb);
282 }
283
284 //path is mandatory
285 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
286 if (msgPath == null) {
287 throw new PcepParseException("Message path is mandatory object for PcRpt message.");
288 } else {
289 msgPath.write(cb);
290 }
291 }
292
293 // update message length field
294 int length = cb.writerIndex() - startIndex;
295 cb.setShort(msgLenIndex, (short) length);
296 }
297 }
298
299 @Override
300 public PcepVersion getVersion() {
301 return PcepVersion.PCEP_1;
302 }
303
304 @Override
305 public PcepType getType() {
306 return MSG_TYPE;
307 }
308
309 @Override
310 public LinkedList<PcepStateReport> getStateReportList() {
311 return this.llStateReportList;
312 }
313
314 @Override
315 public void setStateReportList(LinkedList<PcepStateReport> ll) {
316 this.llStateReportList = ll;
317 }
318
319 @Override
320 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700321 return MoreObjects.toStringHelper(getClass())
322 .add("StateReportList", llStateReportList)
323 .toString();
Bharat saraswalc6f81bc2015-08-21 16:14:56 +0530324 }
325}