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