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