Bharat saraswal | f1a52b8 | 2015-08-21 16:34:20 +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 org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 21 | import org.onosproject.pcepio.protocol.PcepLabelRange; |
| 22 | import org.onosproject.pcepio.protocol.PcepLabelRangeResvMsg; |
| 23 | import org.onosproject.pcepio.protocol.PcepMessageReader; |
| 24 | import org.onosproject.pcepio.protocol.PcepMessageWriter; |
| 25 | import org.onosproject.pcepio.protocol.PcepType; |
| 26 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import com.google.common.base.MoreObjects; |
| 31 | |
| 32 | class PcepLabelRangeResvMsgVer1 implements PcepLabelRangeResvMsg { |
| 33 | |
| 34 | // Pcep version: 1 |
| 35 | |
| 36 | /* |
| 37 | The format of a PCLRResv message is as follows: |
| 38 | |
| 39 | PCLRResv Message>::= <Common Header> |
| 40 | <label-range> |
| 41 | Where: |
| 42 | |
| 43 | <label-range> ::= <SRP> |
| 44 | <labelrange-list> |
| 45 | |
| 46 | Where |
| 47 | <labelrange-list>::=<LABEL-RANGE>[<labelrange-list>] |
| 48 | */ |
| 49 | protected static final Logger log = LoggerFactory.getLogger(PcepLabelRangeResvMsgVer1.class); |
| 50 | |
| 51 | public static final byte PACKET_VERSION = 1; |
| 52 | // LabelRangeResvMsgMinLength = COMMON-HEADER(4)+SrpObjMinLentgh(12)+LABEL-RANGE-MIN-LENGTH(12) |
| 53 | public static final int PACKET_MINIMUM_LENGTH = 28; |
| 54 | public static final PcepType MSG_TYPE = PcepType.LABEL_RANGE_RESERV; |
| 55 | //<label-range> |
| 56 | PcepLabelRange labelRange; |
| 57 | |
| 58 | public static final PcepLabelRangeResvMsgVer1.Reader READER = new Reader(); |
| 59 | |
| 60 | //Reader reads LabelRangeResv Message from the channel. |
| 61 | static class Reader implements PcepMessageReader<PcepLabelRangeResvMsg> { |
| 62 | |
| 63 | @Override |
| 64 | public PcepLabelRangeResvMsg readFrom(ChannelBuffer cb) throws PcepParseException { |
| 65 | |
| 66 | if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) { |
| 67 | throw new PcepParseException("Channel buffer has less readable bytes than Packet minimum length."); |
| 68 | } |
| 69 | // fixed value property version == 1 |
| 70 | byte version = cb.readByte(); |
| 71 | version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG); |
| 72 | if (version != PACKET_VERSION) { |
| 73 | throw new PcepParseException("Wrong version. Expected=PcepVersion.PCEP_1(1), got=" + version); |
| 74 | } |
| 75 | // fixed value property type == 15 |
| 76 | byte type = cb.readByte(); |
| 77 | if (type != MSG_TYPE.getType()) { |
| 78 | throw new PcepParseException("Wrong type. Expected=PcepType.LABEL_RANGE_RESERV(15), got=" + type); |
| 79 | } |
| 80 | short length = cb.readShort(); |
| 81 | if (length < PACKET_MINIMUM_LENGTH) { |
| 82 | throw new PcepParseException("Wrong length.Expected to be >= " + PACKET_MINIMUM_LENGTH + ", is: " |
| 83 | + length); |
| 84 | } |
| 85 | // parse <label-range> |
| 86 | PcepLabelRange labelRange = PcepLabelRangeVer1.read(cb); |
| 87 | return new PcepLabelRangeResvMsgVer1(labelRange); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Constructor to initialize PCEP label range. |
| 93 | * |
| 94 | * @param labelRange PCEP label range |
| 95 | */ |
| 96 | PcepLabelRangeResvMsgVer1(PcepLabelRange labelRange) { |
| 97 | this.labelRange = labelRange; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Builder class for PCEP label range reserve message. |
| 102 | */ |
| 103 | static class Builder implements PcepLabelRangeResvMsg.Builder { |
| 104 | |
| 105 | PcepLabelRange labelRange = new PcepLabelRangeVer1(); |
| 106 | |
| 107 | @Override |
| 108 | public PcepVersion getVersion() { |
| 109 | return PcepVersion.PCEP_1; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public PcepType getType() { |
| 114 | return PcepType.LABEL_RANGE_RESERV; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public PcepLabelRangeResvMsg build() { |
| 119 | return new PcepLabelRangeResvMsgVer1(this.labelRange); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public PcepLabelRange getLabelRange() { |
| 124 | return this.labelRange; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Builder setLabelRange(PcepLabelRange labelRange) { |
| 129 | this.labelRange = labelRange; |
| 130 | return this; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void writeTo(ChannelBuffer cb) throws PcepParseException { |
| 136 | WRITER.write(cb, this); |
| 137 | } |
| 138 | |
| 139 | static final Writer WRITER = new Writer(); |
| 140 | |
| 141 | //Writer writes LabelRangeResv Message to the channel. |
| 142 | static class Writer implements PcepMessageWriter<PcepLabelRangeResvMsgVer1> { |
| 143 | |
| 144 | @Override |
| 145 | public void write(ChannelBuffer cb, PcepLabelRangeResvMsgVer1 message) throws PcepParseException { |
| 146 | |
| 147 | int startIndex = cb.writerIndex(); |
| 148 | // first 3 bits set to version |
| 149 | cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG)); |
| 150 | // message type |
| 151 | cb.writeByte(MSG_TYPE.getType()); |
| 152 | // Length will be set after calculating length, but currently set it as 0. |
| 153 | int msgLenIndex = cb.writerIndex(); |
| 154 | |
| 155 | cb.writeShort((short) 0); |
| 156 | //write Label Range |
| 157 | message.labelRange.write(cb); |
| 158 | |
| 159 | // update message length field |
| 160 | int length = cb.writerIndex() - startIndex; |
| 161 | cb.setShort(msgLenIndex, (short) length); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public PcepVersion getVersion() { |
| 167 | return PcepVersion.PCEP_1; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public PcepType getType() { |
| 172 | return MSG_TYPE; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public PcepLabelRange getLabelRange() { |
| 177 | return this.labelRange; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void setLabelRange(PcepLabelRange lr) { |
| 182 | this.labelRange = lr; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public String toString() { |
| 187 | return MoreObjects.toStringHelper(getClass()).add("labelRange", labelRange).toString(); |
| 188 | } |
| 189 | } |