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