ce589bc052243d3d7e0d57ef111d05504ebc2c53
[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
17 package org.onosproject.pcepio.protocol.ver1;
18
19 import org.jboss.netty.buffer.ChannelBuffer;
20 import org.onosproject.pcepio.exceptions.PcepParseException;
21 import org.onosproject.pcepio.protocol.PcepAttribute;
22 import org.onosproject.pcepio.protocol.PcepBandwidthObject;
23 import org.onosproject.pcepio.protocol.PcepEroObject;
24 import org.onosproject.pcepio.protocol.PcepLspObject;
25 import org.onosproject.pcepio.protocol.PcepRroObject;
26 import org.onosproject.pcepio.protocol.PcepSrpObject;
27 import org.onosproject.pcepio.protocol.PcepStateReport;
28 import org.onosproject.pcepio.types.PcepObjectHeader;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.common.base.MoreObjects;
33 import com.google.common.base.MoreObjects.ToStringHelper;
34
35 /**
36  * Provide the State Report for the Pcep Report Message.
37  * Reference :PCE extensions for stateful draft-ietf-pce-stateful-pce-10.
38  */
39 public class PcepStateReportVer1 implements PcepStateReport {
40     /*
41      * <state-report>     ::= [<SRP>]
42                                <LSP>
43                                <path>
44        Where:
45                <path>     ::= <ERO><attribute-list>[<RRO>]
46      */
47
48     protected static final Logger log = LoggerFactory.getLogger(PcepStateReport.class);
49
50     public static final int OBJECT_HEADER_LENGTH = 4;
51
52     /**
53      * Provides PCEP Message path for report message.
54      */
55     public class PcepMsgPath implements PcepStateReport.PcepMsgPath {
56
57         /*
58          * <path>                  ::= <ERO><attribute-list>[<RRO>]
59          */
60
61         //PcepEroObject
62         private PcepEroObject eroObj;
63         private boolean isEroObjectSet;
64         //PcepAttribute List
65         private PcepAttribute attrList;
66         private boolean isAttributeListSet;
67         //PcepRroObject
68         private PcepRroObject rroObj;
69         private boolean isRroObjectSet;
70         private PcepBandwidthObject bandwidth;
71         private boolean isBandwidthObjectSet;
72
73         /**
74          * Constructor to reset the parameters.
75          */
76         public PcepMsgPath() {
77             eroObj = null;
78             attrList = null;
79             rroObj = null;
80             this.isEroObjectSet = false;
81             this.isAttributeListSet = false;
82             this.isRroObjectSet = false;
83             this.isBandwidthObjectSet = false;
84         }
85
86         /**
87          * Constructor to initialize the parameters from PCEP Message path.
88          *
89          * @param eroObj PCEP ERO Object
90          * @param attrList PCEP Attribute
91          * @param rroObj PCEP Rro Object
92          * @param bandwidth PCEP bandwidth object
93          */
94         public PcepMsgPath(PcepEroObject eroObj, PcepAttribute attrList, PcepRroObject rroObj,
95                            PcepBandwidthObject bandwidth) {
96
97             this.eroObj = eroObj;
98             this.attrList = attrList;
99             this.rroObj = rroObj;
100             this.bandwidth = bandwidth;
101             if (rroObj == null) {
102                 this.isRroObjectSet = false;
103             } else {
104                 this.isRroObjectSet = true;
105             }
106             if (eroObj == null) {
107                 this.isEroObjectSet = false;
108             } else {
109                 this.isEroObjectSet = true;
110             }
111             if (attrList == null) {
112                 this.isAttributeListSet = false;
113             } else {
114                 this.isAttributeListSet = true;
115             }
116             if (bandwidth == null) {
117                 this.isBandwidthObjectSet = false;
118             } else {
119                 this.isBandwidthObjectSet = true;
120             }
121         }
122
123         /**
124          * Returns PcepEroObject.
125          *
126          * @return eroObj PCEP ERO Object
127          */
128         @Override
129         public PcepEroObject getEroObject() {
130             return this.eroObj;
131         }
132
133         /**
134          * Returns PCEP Attribute.
135          *
136          * @return attrList Attribute list
137          */
138         @Override
139         public PcepAttribute getPcepAttribute() {
140             return this.attrList;
141         }
142
143         /**
144          * Returns PcepRroObject.
145          *
146          * @return rroObj PCEP RRO Object
147          */
148         @Override
149         public PcepRroObject getRroObject() {
150             return this.rroObj;
151         }
152
153         @Override
154         public PcepBandwidthObject getBandwidthObject() {
155             return this.bandwidth;
156         }
157
158         @Override
159         public void setEroObject(PcepEroObject eroObj) {
160             this.eroObj = eroObj;
161         }
162
163         @Override
164         public void setPcepAttribute(PcepAttribute attrList) {
165             this.attrList = attrList;
166         }
167
168         @Override
169         public void setRroObject(PcepRroObject rroObj) {
170             this.rroObj = rroObj;
171         }
172
173         @Override
174         public void setBandwidthObject(PcepBandwidthObject bandwidth) {
175             this.bandwidth = bandwidth;
176         }
177
178         /**
179          * Reads all the Objects for PCEP Message Path.
180          *
181          * @param bb of type channel buffer
182          * @return PCEP Message path
183          * @throws PcepParseException when fails to read pcep message path
184          */
185         @Override
186         public PcepMsgPath read(ChannelBuffer bb) throws PcepParseException {
187
188             PcepEroObject eroObj;
189             PcepAttribute attrList;
190             PcepRroObject rroObj = null;
191             PcepBandwidthObject bandwidth = null;
192
193             eroObj = PcepEroObjectVer1.read(bb);
194             attrList = PcepAttributeVer1.read(bb);
195
196             boolean bBreakWhile = false;
197             while (0 < bb.readableBytes()) {
198
199                 if (bb.readableBytes() < OBJECT_HEADER_LENGTH) {
200                     break;
201                 }
202                 bb.markReaderIndex();
203                 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(bb);
204                 bb.resetReaderIndex();
205                 byte yObjClass = tempObjHeader.getObjClass();
206
207                 switch (yObjClass) {
208                 case PcepRroObjectVer1.RRO_OBJ_CLASS:
209                     rroObj = PcepRroObjectVer1.read(bb);
210                     break;
211                 case PcepInterLayerObjectVer1.INTER_LAYER_OBJ_CLASS:
212                     bb.skipBytes(tempObjHeader.getObjLen());
213                     break;
214                 case PcepBandwidthObjectVer1.BANDWIDTH_OBJ_CLASS:
215                     bandwidth = PcepBandwidthObjectVer1.read(bb);
216                     break;
217                 default:
218                     //Otherthan above objects handle those objects in caller.
219                     bBreakWhile = true;
220                     break;
221                 }
222                 if (bBreakWhile) {
223                     break;
224                 }
225             }
226             return new PcepMsgPath(eroObj, attrList, rroObj, bandwidth);
227         }
228
229         /**
230          * Writes all the objects for PCEP message path.
231          *
232          * @param bb of type channel buffer.
233          * @return object length index
234          * @throws PcepParseException when fails to write to channel buffer
235          */
236         @Override
237         public int write(ChannelBuffer bb) throws PcepParseException {
238             int iLenStartIndex = bb.writerIndex();
239
240             //write Object header
241             if (this.isEroObjectSet) {
242                 this.eroObj.write(bb);
243             } else {
244                 throw new PcepParseException("Ero object is not set in path");
245             }
246
247             if (this.isAttributeListSet) {
248                 this.attrList.write(bb);
249             }
250
251             // RRO is optional check and read
252             if (this.isRroObjectSet) {
253                 this.rroObj.write(bb);
254                 // bandwidth should come along with RRO.
255                 if (this.isBandwidthObjectSet) {
256                     this.bandwidth.write(bb);
257                 }
258             }
259             return bb.writerIndex() - iLenStartIndex;
260         }
261
262         @Override
263         public String toString() {
264             ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
265
266             if (attrList != null) {
267                 toStrHelper.add("AttributeList", attrList);
268             }
269             if (rroObj instanceof PcepRroObjectVer1) {
270                 toStrHelper.add("RroObject", rroObj);
271             }
272             if (bandwidth instanceof PcepBandwidthObjectVer1) {
273                 toStrHelper.add("bandwidthObject", bandwidth);
274             }
275             return toStrHelper.toString();
276         }
277     }
278
279     //SRP Object
280     private PcepSrpObject srpObject;
281     //LSP Object
282     private PcepLspObject lspObject;
283     //PcepMsgPath
284     private PcepStateReport.PcepMsgPath msgPath;
285
286     /**
287      * Constructor to reset objects.
288      */
289     public PcepStateReportVer1() {
290         this.srpObject = null;
291         this.lspObject = null;
292         this.msgPath = null;
293     }
294
295     public PcepStateReportVer1(PcepSrpObject srpObject, PcepLspObject lspObject, PcepStateReport.PcepMsgPath msgPath) {
296         this.srpObject = srpObject;
297         this.lspObject = lspObject;
298         this.msgPath = msgPath;
299     }
300
301     @Override
302     public PcepSrpObject getSrpObject() {
303         return srpObject;
304     }
305
306     @Override
307     public PcepLspObject getLspObject() {
308         return lspObject;
309     }
310
311     @Override
312     public PcepStateReport.PcepMsgPath getMsgPath() {
313         return msgPath;
314     }
315
316     @Override
317     public void setSrpObject(PcepSrpObject srpObj) {
318         this.srpObject = srpObj;
319     }
320
321     @Override
322     public void setLspObject(PcepLspObject lspObject) {
323         this.lspObject = lspObject;
324     }
325
326     @Override
327     public void setMsgPath(PcepStateReport.PcepMsgPath msgPath) {
328         this.msgPath = msgPath;
329     }
330
331     /**
332      * Builder class for PCEP state report.
333      */
334     public static class Builder implements PcepStateReport.Builder {
335
336         private boolean bIsSRPObjectSet = false;
337         private boolean bIsLSPObjectSet = false;
338         private boolean bIsPcepMsgPathSet = false;
339
340         //PCEP SRP Object
341         private PcepSrpObject srpObject;
342         //PCEP LSP Object
343         private PcepLspObject lspObject;
344         //PCEP Attribute list
345         private PcepStateReport.PcepMsgPath msgPath;
346
347         @Override
348         public PcepStateReport build() throws PcepParseException {
349
350             //PCEP SRP Object
351             PcepSrpObject srpObject = null;
352             //PCEP LSP Object
353             PcepLspObject lspObject = null;
354             //PCEP Attribute list
355             PcepStateReport.PcepMsgPath msgPath = null;
356
357             if (this.bIsSRPObjectSet) {
358                 srpObject = this.srpObject;
359             }
360
361             if (!this.bIsLSPObjectSet) {
362                 throw new PcepParseException(" LSP Object NOT Set while building PcepStateReport.");
363             } else {
364                 lspObject = this.lspObject;
365             }
366             if (!this.bIsPcepMsgPathSet) {
367                 throw new PcepParseException(" Message Path NOT Set while building PcepStateReport.");
368             } else {
369                 msgPath = this.msgPath;
370             }
371
372             return new PcepStateReportVer1(srpObject, lspObject, msgPath);
373         }
374
375         @Override
376         public PcepSrpObject getSrpObject() {
377             return this.srpObject;
378         }
379
380         @Override
381         public PcepLspObject getLspObject() {
382             return this.lspObject;
383         }
384
385         @Override
386         public PcepStateReport.PcepMsgPath getMsgPath() {
387             return this.msgPath;
388         }
389
390         @Override
391         public Builder setSrpObject(PcepSrpObject srpobj) {
392             this.srpObject = srpobj;
393             this.bIsSRPObjectSet = true;
394             return this;
395         }
396
397         @Override
398         public Builder setLspObject(PcepLspObject lspObject) {
399             this.lspObject = lspObject;
400             this.bIsLSPObjectSet = true;
401             return this;
402         }
403
404         @Override
405         public Builder setMsgPath(PcepStateReport.PcepMsgPath msgPath) {
406             this.msgPath = msgPath;
407             this.bIsPcepMsgPathSet = true;
408             return this;
409         }
410     }
411
412     @Override
413     public String toString() {
414         return MoreObjects.toStringHelper(getClass())
415                 .omitNullValues()
416                 .add("SrpObject", srpObject)
417                 .add("LspObject", lspObject)
418                 .add("MsgPath", msgPath)
419                 .toString();
420     }
421 }