Bharat saraswal | 3077717 | 2015-08-21 15:51:10 +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.PcepLabelUpdate; |
| 25 | import org.onosproject.pcepio.protocol.PcepLabelUpdateMsg; |
| 26 | import org.onosproject.pcepio.protocol.PcepMessageReader; |
| 27 | import org.onosproject.pcepio.protocol.PcepMessageWriter; |
| 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 | class PcepLabelUpdateMsgVer1 implements PcepLabelUpdateMsg { |
| 36 | |
| 37 | // Pcep version: 1 |
| 38 | |
| 39 | /* |
| 40 | The format of the PCLabelUpd message: |
| 41 | |
| 42 | <PCLabelUpd Message> ::= <Common Header> |
| 43 | <pce-label-update-list> |
| 44 | Where: |
| 45 | |
| 46 | <pce-label-update-list> ::= <pce-label-update> |
| 47 | [<pce-label-update-list>] |
| 48 | <pce-label-update> ::= (<pce-label-download>|<pce-label-map>) |
| 49 | |
| 50 | Where: |
| 51 | <pce-label-download> ::= <SRP> |
| 52 | <LSP> |
| 53 | <label-list> |
| 54 | |
| 55 | <pce-label-map> ::= <SRP> |
| 56 | <LABEL> |
| 57 | <FEC> |
| 58 | |
| 59 | <label-list > ::= <LABEL> |
| 60 | [<label-list>] |
| 61 | |
| 62 | */ |
| 63 | protected static final Logger log = LoggerFactory.getLogger(PcepLabelUpdateMsgVer1.class); |
| 64 | |
| 65 | public static final byte PACKET_VERSION = 1; |
| 66 | |
| 67 | //LabelUpdateMsgMinLength = COMMON-HEADER(4)+SrpObjMinLentgh(12)+LabelObjectMinLength(12)+FECType1Object(8) |
| 68 | public static final int PACKET_MINIMUM_LENGTH = 36; |
| 69 | public static final PcepType MSG_TYPE = PcepType.LABEL_UPDATE; |
| 70 | //pce-label-update-list |
| 71 | private LinkedList<PcepLabelUpdate> llPcLabelUpdateList; |
| 72 | |
| 73 | static final PcepLabelUpdateMsgVer1.Reader READER = new Reader(); |
| 74 | |
| 75 | //Reader reads LabelUpdate Message from the channel. |
| 76 | static class Reader implements PcepMessageReader<PcepLabelUpdateMsg> { |
| 77 | |
| 78 | @Override |
| 79 | public PcepLabelUpdateMsg readFrom(ChannelBuffer cb) throws PcepParseException { |
| 80 | |
| 81 | if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) { |
| 82 | throw new PcepParseException("Readable bytes are less than Packet minimum length."); |
| 83 | } |
| 84 | |
| 85 | // fixed value property version == 1 |
| 86 | byte version = cb.readByte(); |
| 87 | version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG); |
| 88 | if (version != PACKET_VERSION) { |
| 89 | throw new PcepParseException("Wrong version.Expected=PcepVersion.PCEP_1(1), got=" + version); |
| 90 | } |
| 91 | // fixed value property type == 13 |
| 92 | byte type = cb.readByte(); |
| 93 | if (type != MSG_TYPE.getType()) { |
| 94 | throw new PcepParseException("Wrong type. Expected=PcepType.LABEL_UPDATE(13), got=" + type); |
| 95 | } |
| 96 | short length = cb.readShort(); |
| 97 | if (length < PACKET_MINIMUM_LENGTH) { |
| 98 | throw new PcepParseException("Wrong length. Expected to be >= " + PACKET_MINIMUM_LENGTH + ", is: " |
| 99 | + length); |
| 100 | } |
| 101 | // parse <pce-label-download> / <pce-label-map> |
| 102 | LinkedList<PcepLabelUpdate> llPcLabelUpdateList = parsePcLabelUpdateList(cb); |
| 103 | return new PcepLabelUpdateMsgVer1(llPcLabelUpdateList); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns list of PCEP Label Update object. |
| 108 | * |
| 109 | * @param cb of type channel buffer |
| 110 | * @return llPcLabelUpdateList list of PCEP label update object |
| 111 | * @throws PcepParseException when fails to parse list of PCEP label update object |
| 112 | */ |
| 113 | public LinkedList<PcepLabelUpdate> parsePcLabelUpdateList(ChannelBuffer cb) throws PcepParseException { |
| 114 | |
| 115 | LinkedList<PcepLabelUpdate> llPcLabelUpdateList; |
| 116 | llPcLabelUpdateList = new LinkedList<PcepLabelUpdate>(); |
| 117 | |
| 118 | while (0 < cb.readableBytes()) { |
| 119 | llPcLabelUpdateList.add(PcepLabelUpdateVer1.read(cb)); |
| 120 | } |
| 121 | return llPcLabelUpdateList; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Constructor to initialize PCEP Label Update List. |
| 127 | * |
| 128 | * @param llPcLabelUpdateList list of PCEP Label Update object |
| 129 | */ |
| 130 | PcepLabelUpdateMsgVer1(LinkedList<PcepLabelUpdate> llPcLabelUpdateList) { |
| 131 | this.llPcLabelUpdateList = llPcLabelUpdateList; |
| 132 | } |
| 133 | |
| 134 | static class Builder implements PcepLabelUpdateMsg.Builder { |
| 135 | |
| 136 | LinkedList<PcepLabelUpdate> llPcLabelUpdateList; |
| 137 | |
| 138 | @Override |
| 139 | public PcepVersion getVersion() { |
| 140 | return PcepVersion.PCEP_1; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public PcepType getType() { |
| 145 | return PcepType.LABEL_UPDATE; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public PcepLabelUpdateMsg build() { |
| 150 | return new PcepLabelUpdateMsgVer1(this.llPcLabelUpdateList); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public LinkedList<PcepLabelUpdate> getPcLabelUpdateList() { |
| 155 | return this.llPcLabelUpdateList; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Builder setPcLabelUpdateList(LinkedList<PcepLabelUpdate> ll) { |
| 160 | this.llPcLabelUpdateList = ll; |
| 161 | return this; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void writeTo(ChannelBuffer cb) throws PcepParseException { |
| 167 | WRITER.write(cb, this); |
| 168 | } |
| 169 | |
| 170 | static final Writer WRITER = new Writer(); |
| 171 | |
| 172 | //Writer writes LabelUpdate Message to the channel. |
| 173 | static class Writer implements PcepMessageWriter<PcepLabelUpdateMsgVer1> { |
| 174 | |
| 175 | @Override |
| 176 | public void write(ChannelBuffer cb, PcepLabelUpdateMsgVer1 message) throws PcepParseException { |
| 177 | |
| 178 | int startIndex = cb.writerIndex(); |
| 179 | |
| 180 | // first 3 bits set to version |
| 181 | cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG)); |
| 182 | |
| 183 | // message type |
| 184 | cb.writeByte(MSG_TYPE.getType()); |
| 185 | |
| 186 | // Length will be set after calculating length, but currently set it as 0. |
| 187 | int msgLenIndex = cb.writerIndex(); |
| 188 | |
| 189 | cb.writeShort((short) 0); |
| 190 | ListIterator<PcepLabelUpdate> listIterator = message.llPcLabelUpdateList.listIterator(); |
| 191 | |
| 192 | while (listIterator.hasNext()) { |
| 193 | PcepLabelUpdate labelUpdate = listIterator.next(); |
| 194 | labelUpdate.write(cb); |
| 195 | } |
| 196 | |
| 197 | // update message length field |
| 198 | int length = cb.writerIndex() - startIndex; |
| 199 | cb.setShort(msgLenIndex, (short) length); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public PcepVersion getVersion() { |
| 205 | return PcepVersion.PCEP_1; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public PcepType getType() { |
| 210 | return MSG_TYPE; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public LinkedList<PcepLabelUpdate> getPcLabelUpdateList() { |
| 215 | return this.llPcLabelUpdateList; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void setPcLabelUpdateList(LinkedList<PcepLabelUpdate> ll) { |
| 220 | this.llPcLabelUpdateList = ll; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public String toString() { |
| 225 | return MoreObjects.toStringHelper(getClass()).add("PcLabelUpdateList", llPcLabelUpdateList).toString(); |
| 226 | } |
| 227 | } |