Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [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.PcepLabelRange; |
| 25 | import org.onosproject.pcepio.protocol.PcepLabelRangeObject; |
| 26 | import org.onosproject.pcepio.protocol.PcepSrpObject; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import com.google.common.base.MoreObjects; |
| 31 | |
| 32 | /** |
| 33 | * Provides PCEP Label Range. |
| 34 | */ |
| 35 | public class PcepLabelRangeVer1 implements PcepLabelRange { |
| 36 | |
| 37 | protected static final Logger log = LoggerFactory.getLogger(PcepLabelRangeVer1.class); |
| 38 | |
| 39 | /* |
| 40 | <label-range> ::= <SRP> |
| 41 | <labelrange-list> |
| 42 | Where |
| 43 | <labelrange-list>::=<LABEL-RANGE>[<labelrange-list>] |
| 44 | */ |
| 45 | |
| 46 | // PCEP SRP Object |
| 47 | private PcepSrpObject srpObject; |
| 48 | //<labelrange-list> of type PcepLabelRangeObject. |
| 49 | private LinkedList<PcepLabelRangeObject> llLabelRangeList; |
| 50 | |
| 51 | /** |
| 52 | * Default Constructor. |
| 53 | */ |
| 54 | public PcepLabelRangeVer1() { |
| 55 | srpObject = null; |
| 56 | llLabelRangeList = null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Constructor to initialize objects. |
| 61 | * |
| 62 | * @param srpObj PCEP Srp object. |
| 63 | * @param llLabelRangeList list of PcepLabelRangeObject. |
| 64 | */ |
| 65 | PcepLabelRangeVer1(PcepSrpObject srpObj, LinkedList<PcepLabelRangeObject> llLabelRangeList) { |
| 66 | this.srpObject = srpObj; |
| 67 | this.llLabelRangeList = llLabelRangeList; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public PcepSrpObject getSrpObject() { |
| 72 | return srpObject; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void setSrpObject(PcepSrpObject srpObject) { |
| 77 | this.srpObject = srpObject; |
| 78 | |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public LinkedList<PcepLabelRangeObject> getLabelRangeList() { |
| 83 | return llLabelRangeList; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void setLabelRangeList(LinkedList<PcepLabelRangeObject> ll) { |
| 88 | this.llLabelRangeList = ll; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Reads channel buffer and returns object of PcepLabelRange. |
| 93 | * |
| 94 | * @param cb of type channel buffer. |
| 95 | * @return object of PcepLabelRange |
| 96 | * @throws PcepParseException when fails to read from channel buffer |
| 97 | */ |
| 98 | public static PcepLabelRange read(ChannelBuffer cb) throws PcepParseException { |
| 99 | |
| 100 | //parse and store SRP mandatory object |
| 101 | PcepSrpObject srpObj = null; |
| 102 | srpObj = PcepSrpObjectVer1.read(cb); |
| 103 | if (srpObj == null) { |
| 104 | throw new PcepParseException("Exception while parsing srp object"); |
| 105 | } |
| 106 | |
Sho SHIMIZU | 9b8274c | 2015-09-04 15:54:24 -0700 | [diff] [blame] | 107 | LinkedList<PcepLabelRangeObject> llLabelRangeList = new LinkedList<>(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 108 | boolean bFoundLabelRangeObj = false; |
| 109 | while (0 < cb.readableBytes()) { |
| 110 | //parse and store <labelrange-list> |
| 111 | PcepLabelRangeObject lrObj; |
| 112 | lrObj = PcepLabelRangeObjectVer1.read(cb); |
| 113 | if (lrObj == null) { |
| 114 | throw new PcepParseException("Exception while parsing label range object"); |
| 115 | } else { |
| 116 | llLabelRangeList.add(lrObj); |
| 117 | bFoundLabelRangeObj = true; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (!bFoundLabelRangeObj) { |
| 122 | throw new PcepParseException("At least one LABEL-RANGE MUST be present."); |
| 123 | } |
| 124 | return new PcepLabelRangeVer1(srpObj, llLabelRangeList); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int write(ChannelBuffer cb) throws PcepParseException { |
| 129 | //write Object header |
| 130 | int objStartIndex = cb.writerIndex(); |
| 131 | |
| 132 | //write <SRP> |
| 133 | int objLenIndex = srpObject.write(cb); |
| 134 | |
| 135 | if (objLenIndex <= 0) { |
| 136 | throw new PcepParseException("bjectLength is " + objLenIndex); |
| 137 | } |
| 138 | |
| 139 | //write <labelrange-list> |
| 140 | ListIterator<PcepLabelRangeObject> listIterator = llLabelRangeList.listIterator(); |
| 141 | while (listIterator.hasNext()) { |
| 142 | listIterator.next().write(cb); |
| 143 | } |
| 144 | |
| 145 | //Update object length now |
| 146 | int length = cb.writerIndex() - objStartIndex; |
| 147 | // As per RFC the length of object should be |
| 148 | // multiples of 4 |
| 149 | int pad = length % 4; |
| 150 | if (pad != 0) { |
| 151 | pad = 4 - pad; |
| 152 | for (int i = 0; i < pad; i++) { |
| 153 | cb.writeByte((byte) 0); |
| 154 | } |
| 155 | length = length + pad; |
| 156 | } |
| 157 | cb.setShort(objLenIndex, (short) length); |
| 158 | return length; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 163 | return MoreObjects.toStringHelper(getClass()) |
| 164 | .add("srpObject", srpObject) |
| 165 | .add("LabelRangeList", llLabelRangeList) |
| 166 | .toString(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 167 | } |
| 168 | } |