Phaneendra Manda | 1c0061d | 2015-08-06 12:29:38 +0530 | [diff] [blame^] | 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 | /** |
| 24 | * Provides PCEP Object Header which is common for all the objects. |
| 25 | * Reference : RFC 5440. |
| 26 | */ |
| 27 | |
| 28 | public class PcepObjectHeader { |
| 29 | |
| 30 | /* |
| 31 | 0 1 2 3 |
| 32 | 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 |
| 33 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 34 | | Object-Class | OT |Res|P|I| Object Length (bytes) | |
| 35 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 36 | | | |
| 37 | // (Object body) // |
| 38 | | | |
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 | |
| 41 | PCEP Common Object Header |
| 42 | */ |
| 43 | |
| 44 | protected static final Logger log = LoggerFactory.getLogger(PcepObjectHeader.class); |
| 45 | |
| 46 | public static final boolean REQ_OBJ_MUST_PROCESS = true; |
| 47 | public static final boolean REQ_OBJ_OPTIONAL_PROCESS = false; |
| 48 | public static final boolean RSP_OBJ_IGNORED = true; |
| 49 | public static final boolean RSP_OBJ_PROCESSED = false; |
| 50 | public static final int OBJECT_TYPE_SHIFT_VALUE = 4; |
| 51 | public static final byte PFLAG_SET = 0x02; |
| 52 | public static final byte IFLAG_SET = 0x01; |
| 53 | public static final int SET = 1; |
| 54 | private byte objClass; |
| 55 | private byte objType; |
| 56 | private boolean bPFlag; |
| 57 | private boolean bIFlag; |
| 58 | private short objLen; |
| 59 | |
| 60 | /** |
| 61 | * Constructor to initialize all the variables in object header. |
| 62 | * |
| 63 | * @param objClass PCEP Object class |
| 64 | * @param objType PCEP Object type |
| 65 | * @param bPFlag P flag |
| 66 | * @param bIFlag I flag |
| 67 | * @param objLen PCEP object length |
| 68 | */ |
| 69 | |
| 70 | public PcepObjectHeader(byte objClass, byte objType, boolean bPFlag, boolean bIFlag, short objLen) { |
| 71 | this.objClass = objClass; |
| 72 | this.objType = objType; |
| 73 | this.bPFlag = bPFlag; |
| 74 | this.bIFlag = bIFlag; |
| 75 | this.objLen = objLen; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Sets the Object class. |
| 80 | * |
| 81 | * @param value object class |
| 82 | */ |
| 83 | public void setObjClass(byte value) { |
| 84 | this.objClass = value; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sets the Object TYPE. |
| 89 | * |
| 90 | * @param value object type |
| 91 | */ |
| 92 | public void setObjType(byte value) { |
| 93 | this.objType = value; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets the Object P flag. |
| 98 | * |
| 99 | * @param value p flag |
| 100 | */ |
| 101 | public void setPFlag(boolean value) { |
| 102 | this.bPFlag = value; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Sets the Object I flag. |
| 107 | * |
| 108 | * @param value I flag |
| 109 | */ |
| 110 | public void setIFlag(boolean value) { |
| 111 | this.bIFlag = value; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Sets the Object Length. |
| 116 | * |
| 117 | * @param value object length |
| 118 | */ |
| 119 | public void setObjLen(short value) { |
| 120 | this.objLen = value; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns Object's P flag. |
| 125 | * |
| 126 | * @return bPFlag P flag |
| 127 | */ |
| 128 | public boolean getPFlag() { |
| 129 | return this.bPFlag; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns Object's i flag. |
| 134 | * |
| 135 | * @return bIFlag I flag |
| 136 | */ |
| 137 | public boolean getIFlag() { |
| 138 | return this.bIFlag; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns Object Length. |
| 143 | * |
| 144 | * @return objLen object length |
| 145 | */ |
| 146 | public short getObjLen() { |
| 147 | return this.objLen; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns Object class. |
| 152 | * |
| 153 | * @return objClass object class |
| 154 | */ |
| 155 | public byte getObjClass() { |
| 156 | return this.objClass; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns Object Type. |
| 161 | * |
| 162 | * @return objType object type |
| 163 | */ |
| 164 | public byte getObjType() { |
| 165 | return this.objType; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Writes Byte stream of PCEP object header to channel buffer. |
| 170 | * |
| 171 | * @param bb of type channel buffer |
| 172 | * @return objLenIndex object length index in channel buffer |
| 173 | */ |
| 174 | public int write(ChannelBuffer bb) { |
| 175 | |
| 176 | bb.writeByte(this.objClass); |
| 177 | byte temp = (byte) (this.objType << OBJECT_TYPE_SHIFT_VALUE); |
| 178 | if (this.bPFlag) { |
| 179 | temp = (byte) (temp | PFLAG_SET); |
| 180 | } |
| 181 | if (this.bIFlag) { |
| 182 | temp = (byte) (temp | IFLAG_SET); |
| 183 | } |
| 184 | bb.writeByte(temp); |
| 185 | int objLenIndex = bb.writerIndex(); |
| 186 | bb.writeShort((short) 0); |
| 187 | return objLenIndex; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Read from channel buffer and Returns PCEP Objects header. |
| 192 | * |
| 193 | * @param bb of type channel buffer |
| 194 | * @return PCEP Object header |
| 195 | */ |
| 196 | public static PcepObjectHeader read(ChannelBuffer bb) { |
| 197 | |
| 198 | byte objClass; |
| 199 | byte objType; |
| 200 | boolean bPFlag; |
| 201 | boolean bIFlag; |
| 202 | short objLen; |
| 203 | objClass = bb.readByte(); |
| 204 | byte temp = bb.readByte(); |
| 205 | bIFlag = ((temp & IFLAG_SET) == IFLAG_SET) ? true : false; |
| 206 | bPFlag = ((temp & PFLAG_SET) == PFLAG_SET) ? true : false; |
| 207 | objType = (byte) (temp >> OBJECT_TYPE_SHIFT_VALUE); |
| 208 | objLen = bb.readShort(); |
| 209 | return new PcepObjectHeader(objClass, objType, bPFlag, bIFlag, objLen); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Prints the Attributes of PCEP Object header. |
| 214 | */ |
| 215 | public void print() { |
| 216 | |
| 217 | log.debug("PcepObjectHeader"); |
| 218 | log.debug("Object Class: " + objClass); |
| 219 | log.debug("Object Type: " + objType); |
| 220 | log.debug("Object Length: " + objLen); |
| 221 | log.debug("P flag: " + bPFlag); |
| 222 | log.debug("I flag: " + bIFlag); |
| 223 | } |
| 224 | } |