Mahesh Poojary S | 3f8fcac | 2015-08-21 18:53:28 +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.PcepMessageReader; |
| 25 | import org.onosproject.pcepio.protocol.PcepMessageWriter; |
| 26 | import org.onosproject.pcepio.protocol.PcepTEObject; |
| 27 | import org.onosproject.pcepio.protocol.PcepTEReportMsg; |
| 28 | import org.onosproject.pcepio.protocol.PcepType; |
| 29 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import com.google.common.base.MoreObjects; |
| 34 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 35 | /** |
Mahesh Poojary S | 3f8fcac | 2015-08-21 18:53:28 +0530 | [diff] [blame] | 36 | * Provides PCEP TE Report Message. |
| 37 | */ |
| 38 | class PcepTEReportMsgVer1 implements PcepTEReportMsg { |
| 39 | |
| 40 | /* |
| 41 | * Ref : draft-dhodylee-pce-pcep-te-data-extn-02, section 8.1 |
| 42 | |
| 43 | <TERpt Message> ::= <Common Header> |
| 44 | <te-report-list> |
| 45 | Where: |
| 46 | <te-report-list> ::= <TE>[<te-report-list>] |
| 47 | */ |
| 48 | |
| 49 | private static final Logger log = LoggerFactory.getLogger(PcepTEReportMsgVer1.class); |
| 50 | //PACKET_MINIMUM_LENGTH = CommonHeaderLen(4)+TEObjMinLen(12) |
| 51 | public static final int PACKET_MINIMUM_LENGTH = 16; |
| 52 | public static final PcepType MSG_TYPE = PcepType.TE_REPORT; |
| 53 | // <te-report-list> |
| 54 | private LinkedList<PcepTEObject> teReportList; |
| 55 | |
| 56 | public static final PcepTEReportMsgVer1.Reader READER = new Reader(); |
| 57 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 58 | /** |
| 59 | * Reader class for reading PCPE te report message form channel buffer. |
| 60 | */ |
Mahesh Poojary S | 3f8fcac | 2015-08-21 18:53:28 +0530 | [diff] [blame] | 61 | static class Reader implements PcepMessageReader<PcepTEReportMsg> { |
| 62 | |
| 63 | LinkedList<PcepTEObject> teReportList; |
| 64 | |
| 65 | @Override |
| 66 | public PcepTEReportMsg readFrom(ChannelBuffer cb) throws PcepParseException { |
| 67 | |
| 68 | if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) { |
| 69 | return null; |
| 70 | } |
| 71 | |
Sho SHIMIZU | 9b8274c | 2015-09-04 15:54:24 -0700 | [diff] [blame] | 72 | teReportList = new LinkedList<>(); |
Mahesh Poojary S | 3f8fcac | 2015-08-21 18:53:28 +0530 | [diff] [blame] | 73 | |
| 74 | byte version = cb.readByte(); |
| 75 | version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG); |
| 76 | if (version != PcepMessageVer1.PACKET_VERSION) { |
| 77 | throw new PcepParseException("Wrong version. Expected=PcepVersion.PCEP_1(1), got=" + version); |
| 78 | } |
| 79 | |
| 80 | byte type = cb.readByte(); |
| 81 | if (type != MSG_TYPE.getType()) { |
| 82 | throw new PcepParseException("Wrong type. Expected=PcepType.TE_REPORT(14), got=" + type); |
| 83 | } |
| 84 | |
| 85 | short length = cb.readShort(); |
| 86 | if (length < PACKET_MINIMUM_LENGTH) { |
| 87 | throw new PcepParseException( |
| 88 | "Wrong length. Expected to be >= " + PACKET_MINIMUM_LENGTH + ", is: " + length); |
| 89 | } |
| 90 | |
| 91 | // Parse state report list |
| 92 | parseTEReportList(cb); |
| 93 | |
| 94 | return new PcepTEReportMsgVer1(teReportList); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Parse te-report-list. |
| 99 | * |
| 100 | * @param cb input Channel Buffer |
| 101 | * @throws PcepParseException when fails to parse TE Report list. |
| 102 | */ |
| 103 | public void parseTEReportList(ChannelBuffer cb) throws PcepParseException { |
| 104 | // <te-report-list> ::= <TE>[<te-report-list>] |
| 105 | |
| 106 | while (0 < cb.readableBytes()) { |
| 107 | //store TE objectS |
| 108 | if (!teReportList.add(PcepTEObjectVer1.read(cb))) { |
| 109 | throw new PcepParseException("Failed to add TE object to TE report list"); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Constructor to initialize TE Report List. |
| 117 | * |
| 118 | * @param teReportList list of PCEP TE Object |
| 119 | */ |
| 120 | PcepTEReportMsgVer1(LinkedList<PcepTEObject> teReportList) { |
| 121 | this.teReportList = teReportList; |
| 122 | } |
| 123 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 124 | /** |
| 125 | * Builder class for PCEP te report message. |
| 126 | */ |
Mahesh Poojary S | 3f8fcac | 2015-08-21 18:53:28 +0530 | [diff] [blame] | 127 | static class Builder implements PcepTEReportMsg.Builder { |
| 128 | // PCEP TE Report message fields |
| 129 | LinkedList<PcepTEObject> teReportList; |
| 130 | |
| 131 | @Override |
| 132 | public PcepVersion getVersion() { |
| 133 | return PcepVersion.PCEP_1; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public PcepType getType() { |
| 138 | return PcepType.TE_REPORT; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public PcepTEReportMsg build() { |
| 143 | return new PcepTEReportMsgVer1(this.teReportList); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public LinkedList<PcepTEObject> getTEReportList() { |
| 148 | return this.teReportList; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Builder setTEReportList(LinkedList<PcepTEObject> ll) { |
| 153 | this.teReportList = ll; |
| 154 | return this; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void writeTo(ChannelBuffer bb) throws PcepParseException { |
| 160 | WRITER.write(bb, this); |
| 161 | } |
| 162 | |
| 163 | static final Writer WRITER = new Writer(); |
| 164 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 165 | /** |
| 166 | * Writer class for writing PCEP te report message to channel buffer. |
| 167 | */ |
Mahesh Poojary S | 3f8fcac | 2015-08-21 18:53:28 +0530 | [diff] [blame] | 168 | static class Writer implements PcepMessageWriter<PcepTEReportMsgVer1> { |
| 169 | |
| 170 | @Override |
| 171 | public void write(ChannelBuffer bb, PcepTEReportMsgVer1 message) throws PcepParseException { |
| 172 | |
| 173 | int startIndex = bb.writerIndex(); |
| 174 | |
| 175 | // first 3 bits set to version |
| 176 | bb.writeByte((byte) (PcepMessageVer1.PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG)); |
| 177 | |
| 178 | // message type |
| 179 | bb.writeByte(MSG_TYPE.getType()); |
| 180 | |
| 181 | // Length of the message will be updated at the end |
| 182 | // First write with 0s |
| 183 | int msgLenIndex = bb.writerIndex(); |
| 184 | bb.writeShort((short) 0); |
| 185 | |
| 186 | ListIterator<PcepTEObject> listIterator = message.teReportList.listIterator(); |
| 187 | |
| 188 | while (listIterator.hasNext()) { |
| 189 | PcepTEObject teObj = listIterator.next(); |
| 190 | teObj.write(bb); |
| 191 | } |
| 192 | |
| 193 | // update message length field |
| 194 | int length = bb.writerIndex() - startIndex; |
| 195 | bb.setShort(msgLenIndex, (short) length); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public PcepVersion getVersion() { |
| 201 | return PcepVersion.PCEP_1; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public PcepType getType() { |
| 206 | return MSG_TYPE; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public LinkedList<PcepTEObject> getTEReportList() { |
| 211 | return this.teReportList; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void setTEReportList(LinkedList<PcepTEObject> ll) { |
| 216 | this.teReportList = ll; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 221 | return MoreObjects.toStringHelper(getClass()) |
| 222 | .add("TeReportList", teReportList) |
| 223 | .toString(); |
Mahesh Poojary S | 3f8fcac | 2015-08-21 18:53:28 +0530 | [diff] [blame] | 224 | } |
| 225 | } |