132ff862aa0b3fd7c43bb7be5ed9bad2ea5eea5b
[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.types;
18
19 import org.jboss.netty.buffer.ChannelBuffer;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.base.MoreObjects;
24
25 /**
26  * Provides PcepRsvpObjectHeader.
27  */
28 public class PcepRsvpSpecObjHeader {
29
30     /*
31     0             1              2             3
32     +-------------+-------------+-------------+-------------+
33     |       Length (bytes)      |  Class-Num  |   C-Type    |
34     +-------------+-------------+-------------+-------------+
35     |                                                       |
36     //                  (Object contents)                   //
37     |                                                       |
38     +-------------+-------------+-------------+-------------+
39
40               ERROR_SPEC object Header
41      */
42
43     protected static final Logger log = LoggerFactory.getLogger(PcepRsvpSpecObjHeader.class);
44
45     private short objLen;
46     private byte objClassNum;
47     private byte objClassType;
48
49     /**
50      * Constructor to initialize length, class num and type.
51      *
52      * @param objLen object length
53      * @param objClassNum pcep rsvp error spec object class num
54      * @param objClassType pcep rsvp error spec object class type
55      */
56     public PcepRsvpSpecObjHeader(short objLen, byte objClassNum, byte objClassType) {
57         this.objLen = objLen;
58         this.objClassNum = objClassNum;
59         this.objClassType = objClassType;
60     }
61
62     /**
63      * Sets the Class num.
64      *
65      * @param value pcep rsvp error spec object class num
66      */
67     public void setObjClassNum(byte value) {
68         this.objClassNum = value;
69     }
70
71     /**
72      * Sets the Class type.
73      *
74      * @param value pcep rsvp error spec object class type
75      */
76     public void setObjClassType(byte value) {
77         this.objClassType = value;
78     }
79
80     /**
81      * Sets the Class Length.
82      *
83      * @param value pcep rsvp error spec object length
84      */
85     public void setObjLen(short value) {
86         this.objLen = value;
87     }
88
89     /**
90      * Returns Object Length.
91      *
92      * @return objLen pcep rsvp error spec object length
93      */
94     public short getObjLen() {
95         return this.objLen;
96     }
97
98     /**
99      * Returns Object num.
100      *
101      * @return objClassNum pcep rsvp error spec object class num
102      */
103     public byte getObjClassNum() {
104         return this.objClassNum;
105     }
106
107     /**
108      * Returns Object type.
109      *
110      * @return objClassType pcep rsvp error spec object class type
111      */
112     public byte getObjClassType() {
113         return this.objClassType;
114     }
115
116     /**
117      * Writes the byte stream of PcepRsvpObjectHeader to channel buffer.
118      *
119      * @param cb of type channel buffer
120      * @return object length index
121      */
122     public int write(ChannelBuffer cb) {
123         int objLenIndex = cb.writerIndex();
124         objLen = 0;
125         cb.writeShort(objLen);
126         cb.writeByte(objClassNum);
127         cb.writeByte(objClassType);
128         return objLenIndex;
129     }
130
131     /**
132      * Reads the PcepRsvpObjectHeader.
133      *
134      * @param cb of type channel buffer
135      * @return PcepRsvpObjectHeader
136      */
137     public static PcepRsvpSpecObjHeader read(ChannelBuffer cb) {
138         byte objClassNum;
139         byte objClassType;
140         short objLen;
141         objLen = cb.readShort();
142         objClassNum = cb.readByte();
143         objClassType = cb.readByte();
144
145         return new PcepRsvpSpecObjHeader(objLen, objClassNum, objClassType);
146     }
147
148     @Override
149     public String toString() {
150         return MoreObjects.toStringHelper(getClass())
151                 .add("ObjectClassNum: ", objClassNum)
152                 .add("ObjectCType: ", objClassType)
153                 .add("ObjectLength: ", objLen)
154                 .toString();
155     }
156 }