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