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 | |
| 35 | /* |
| 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 | |
| 58 | static class Reader implements PcepMessageReader<PcepTEReportMsg> { |
| 59 | |
| 60 | LinkedList<PcepTEObject> teReportList; |
| 61 | |
| 62 | @Override |
| 63 | public PcepTEReportMsg readFrom(ChannelBuffer cb) throws PcepParseException { |
| 64 | |
| 65 | if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) { |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | teReportList = new LinkedList<PcepTEObject>(); |
| 70 | |
| 71 | byte version = cb.readByte(); |
| 72 | version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG); |
| 73 | if (version != PcepMessageVer1.PACKET_VERSION) { |
| 74 | throw new PcepParseException("Wrong version. Expected=PcepVersion.PCEP_1(1), got=" + version); |
| 75 | } |
| 76 | |
| 77 | byte type = cb.readByte(); |
| 78 | if (type != MSG_TYPE.getType()) { |
| 79 | throw new PcepParseException("Wrong type. Expected=PcepType.TE_REPORT(14), got=" + type); |
| 80 | } |
| 81 | |
| 82 | short length = cb.readShort(); |
| 83 | if (length < PACKET_MINIMUM_LENGTH) { |
| 84 | throw new PcepParseException( |
| 85 | "Wrong length. Expected to be >= " + PACKET_MINIMUM_LENGTH + ", is: " + length); |
| 86 | } |
| 87 | |
| 88 | // Parse state report list |
| 89 | parseTEReportList(cb); |
| 90 | |
| 91 | return new PcepTEReportMsgVer1(teReportList); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Parse te-report-list. |
| 96 | * |
| 97 | * @param cb input Channel Buffer |
| 98 | * @throws PcepParseException when fails to parse TE Report list. |
| 99 | */ |
| 100 | public void parseTEReportList(ChannelBuffer cb) throws PcepParseException { |
| 101 | // <te-report-list> ::= <TE>[<te-report-list>] |
| 102 | |
| 103 | while (0 < cb.readableBytes()) { |
| 104 | //store TE objectS |
| 105 | if (!teReportList.add(PcepTEObjectVer1.read(cb))) { |
| 106 | throw new PcepParseException("Failed to add TE object to TE report list"); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Constructor to initialize TE Report List. |
| 114 | * |
| 115 | * @param teReportList list of PCEP TE Object |
| 116 | */ |
| 117 | PcepTEReportMsgVer1(LinkedList<PcepTEObject> teReportList) { |
| 118 | this.teReportList = teReportList; |
| 119 | } |
| 120 | |
| 121 | static class Builder implements PcepTEReportMsg.Builder { |
| 122 | // PCEP TE Report message fields |
| 123 | LinkedList<PcepTEObject> teReportList; |
| 124 | |
| 125 | @Override |
| 126 | public PcepVersion getVersion() { |
| 127 | return PcepVersion.PCEP_1; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public PcepType getType() { |
| 132 | return PcepType.TE_REPORT; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public PcepTEReportMsg build() { |
| 137 | return new PcepTEReportMsgVer1(this.teReportList); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public LinkedList<PcepTEObject> getTEReportList() { |
| 142 | return this.teReportList; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Builder setTEReportList(LinkedList<PcepTEObject> ll) { |
| 147 | this.teReportList = ll; |
| 148 | return this; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void writeTo(ChannelBuffer bb) throws PcepParseException { |
| 154 | WRITER.write(bb, this); |
| 155 | } |
| 156 | |
| 157 | static final Writer WRITER = new Writer(); |
| 158 | |
| 159 | static class Writer implements PcepMessageWriter<PcepTEReportMsgVer1> { |
| 160 | |
| 161 | @Override |
| 162 | public void write(ChannelBuffer bb, PcepTEReportMsgVer1 message) throws PcepParseException { |
| 163 | |
| 164 | int startIndex = bb.writerIndex(); |
| 165 | |
| 166 | // first 3 bits set to version |
| 167 | bb.writeByte((byte) (PcepMessageVer1.PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG)); |
| 168 | |
| 169 | // message type |
| 170 | bb.writeByte(MSG_TYPE.getType()); |
| 171 | |
| 172 | // Length of the message will be updated at the end |
| 173 | // First write with 0s |
| 174 | int msgLenIndex = bb.writerIndex(); |
| 175 | bb.writeShort((short) 0); |
| 176 | |
| 177 | ListIterator<PcepTEObject> listIterator = message.teReportList.listIterator(); |
| 178 | |
| 179 | while (listIterator.hasNext()) { |
| 180 | PcepTEObject teObj = listIterator.next(); |
| 181 | teObj.write(bb); |
| 182 | } |
| 183 | |
| 184 | // update message length field |
| 185 | int length = bb.writerIndex() - startIndex; |
| 186 | bb.setShort(msgLenIndex, (short) length); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public PcepVersion getVersion() { |
| 192 | return PcepVersion.PCEP_1; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public PcepType getType() { |
| 197 | return MSG_TYPE; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public LinkedList<PcepTEObject> getTEReportList() { |
| 202 | return this.teReportList; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void setTEReportList(LinkedList<PcepTEObject> ll) { |
| 207 | this.teReportList = ll; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public String toString() { |
| 212 | return MoreObjects.toStringHelper(getClass()).add("TeReportList", teReportList).toString(); |
| 213 | } |
| 214 | } |