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.PcepFecObject; |
| 25 | import org.onosproject.pcepio.protocol.PcepLabelObject; |
| 26 | import org.onosproject.pcepio.protocol.PcepLabelUpdate; |
| 27 | import org.onosproject.pcepio.protocol.PcepLspObject; |
| 28 | import org.onosproject.pcepio.protocol.PcepSrpObject; |
| 29 | import org.onosproject.pcepio.types.PcepLabelDownload; |
| 30 | import org.onosproject.pcepio.types.PcepLabelMap; |
| 31 | import org.onosproject.pcepio.types.PcepObjectHeader; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import com.google.common.base.MoreObjects; |
| 36 | import com.google.common.base.MoreObjects.ToStringHelper; |
| 37 | |
| 38 | /** |
| 39 | * Provides PCEP LABEL update . |
| 40 | * Reference :draft-zhao-pce-pcep-extension-for-pce-controller-01. |
| 41 | */ |
| 42 | public class PcepLabelUpdateVer1 implements PcepLabelUpdate { |
| 43 | |
| 44 | /* |
| 45 | * <pce-label-update> ::= (<pce-label-download>|<pce-label-map>) |
| 46 | |
| 47 | Where: |
| 48 | <pce-label-download> ::= <SRP> |
| 49 | <LSP> |
| 50 | <label-list> |
| 51 | |
| 52 | <pce-label-map> ::= <SRP> |
| 53 | <LABEL> |
| 54 | <FEC> |
| 55 | |
| 56 | <label-list > ::= <LABEL> |
| 57 | [<label-list>] |
| 58 | */ |
| 59 | protected static final Logger log = LoggerFactory.getLogger(PcepLabelUpdateVer1.class); |
| 60 | |
| 61 | //Either PceLabelDownload or PceLabelMap is mandatory. |
| 62 | //label Download |
| 63 | private PcepLabelDownload labelDownload; |
| 64 | private boolean isLabelDownloadSet; |
| 65 | //label Map |
| 66 | private PcepLabelMap labelMap; |
| 67 | private boolean isLabelMapSet; |
| 68 | |
| 69 | /** |
| 70 | * Constructor to reset parameters. |
| 71 | */ |
| 72 | public PcepLabelUpdateVer1() { |
| 73 | this.labelDownload = null; |
| 74 | this.isLabelDownloadSet = false; |
| 75 | this.labelMap = null; |
| 76 | this.isLabelMapSet = false; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Constructor to initialize PCEP label download. |
| 81 | * |
| 82 | * @param labelDownload PCEP label download |
| 83 | */ |
| 84 | public PcepLabelUpdateVer1(PcepLabelDownload labelDownload) { |
| 85 | this.labelDownload = labelDownload; |
| 86 | this.isLabelDownloadSet = true; |
| 87 | this.labelMap = null; |
| 88 | this.isLabelMapSet = false; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Constructor to initialize PCEP label map. |
| 93 | * |
| 94 | * @param labelMap PCEP label map |
| 95 | */ |
| 96 | public PcepLabelUpdateVer1(PcepLabelMap labelMap) { |
| 97 | this.labelDownload = null; |
| 98 | this.isLabelDownloadSet = false; |
| 99 | this.labelMap = labelMap; |
| 100 | this.isLabelMapSet = true; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * builder class for PCEP label update. |
| 105 | */ |
| 106 | static class Builder implements PcepLabelUpdate.Builder { |
| 107 | |
| 108 | private PcepLabelDownload labelDownload; |
| 109 | private boolean isLabelDownloadSet; |
| 110 | private PcepLabelMap labelMap; |
| 111 | private boolean isLabelMapSet; |
| 112 | |
| 113 | @Override |
| 114 | public PcepLabelUpdate build() throws PcepParseException { |
| 115 | |
| 116 | if (isLabelDownloadSet) { |
| 117 | return new PcepLabelUpdateVer1(labelDownload); |
| 118 | } |
| 119 | if (isLabelMapSet) { |
| 120 | return new PcepLabelUpdateVer1(labelMap); |
| 121 | } |
| 122 | if (!isLabelDownloadSet && !isLabelMapSet) { |
| 123 | throw new PcepParseException( |
| 124 | "Label Download or Label Map is not set while building PcepLabelUpdate Message"); |
| 125 | } |
| 126 | return new PcepLabelUpdateVer1(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Builder setLabelDownload(PcepLabelDownload labelDownload) { |
| 131 | this.labelDownload = labelDownload; |
| 132 | this.isLabelDownloadSet = true; |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public PcepLabelDownload getLabelDownload() { |
| 138 | return labelDownload; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Builder setLabelMap(PcepLabelMap labelMap) { |
| 143 | this.labelMap = labelMap; |
| 144 | this.isLabelMapSet = true; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public PcepLabelMap getLabelMap() { |
| 150 | return labelMap; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Reads PcepLabels from the byte stream received from channel buffer. |
| 156 | * |
| 157 | * @param cb of type channel buffer. |
| 158 | * @return PcepLabelUpdate object. |
| 159 | * @throws PcepParseException when fails to read from channel buffer |
| 160 | */ |
| 161 | public static PcepLabelUpdate read(ChannelBuffer cb) throws PcepParseException { |
| 162 | |
| 163 | PcepLabelUpdateVer1 pceLabelUpdate = new PcepLabelUpdateVer1(); |
| 164 | |
| 165 | PcepSrpObject srpObject; |
| 166 | PcepObjectHeader tempObjHeader; |
| 167 | |
| 168 | //read SRP mandatory Object |
| 169 | srpObject = PcepSrpObjectVer1.read(cb); |
| 170 | |
| 171 | //checking next object |
| 172 | cb.markReaderIndex(); |
| 173 | |
| 174 | tempObjHeader = PcepObjectHeader.read(cb); |
| 175 | cb.resetReaderIndex(); |
| 176 | |
| 177 | if (tempObjHeader.getObjClass() == PcepLspObjectVer1.LSP_OBJ_CLASS) { |
| 178 | |
| 179 | //now it is belong to <pce-label-download> |
| 180 | PcepLabelDownload labelDownload = new PcepLabelDownload(); |
| 181 | |
| 182 | //set SRP |
| 183 | labelDownload.setSrpObject(srpObject); |
| 184 | |
| 185 | //read and set LSP |
| 186 | labelDownload.setLspObject(PcepLspObjectVer1.read(cb)); |
| 187 | |
| 188 | //<label-list> |
| 189 | LinkedList<PcepLabelObject> llLabelList = new LinkedList<PcepLabelObject>(); |
| 190 | PcepLabelObject labelObject; |
| 191 | |
| 192 | while (0 < cb.readableBytes()) { |
| 193 | |
| 194 | cb.markReaderIndex(); |
| 195 | tempObjHeader = PcepObjectHeader.read(cb); |
| 196 | cb.resetReaderIndex(); |
| 197 | |
| 198 | if (tempObjHeader.getObjClass() != PcepLabelObjectVer1.LABEL_OBJ_CLASS) { |
| 199 | break; |
| 200 | } |
| 201 | labelObject = PcepLabelObjectVer1.read(cb); |
| 202 | llLabelList.add(labelObject); |
| 203 | } |
| 204 | labelDownload.setLabelList(llLabelList); |
| 205 | pceLabelUpdate.setLabelDownload(labelDownload); |
| 206 | } else if (tempObjHeader.getObjClass() == PcepLabelObjectVer1.LABEL_OBJ_CLASS) { |
| 207 | //belong to <pce-label-map> |
| 208 | PcepLabelMap labelMap = new PcepLabelMap(); |
| 209 | |
| 210 | //set SRP Object |
| 211 | labelMap.setSrpObject(srpObject); |
| 212 | |
| 213 | //read and set Label Object |
| 214 | labelMap.setLabelObject(PcepLabelObjectVer1.read(cb)); |
| 215 | |
| 216 | cb.markReaderIndex(); |
| 217 | tempObjHeader = PcepObjectHeader.read(cb); |
| 218 | cb.resetReaderIndex(); |
| 219 | |
| 220 | PcepFecObject fecObject = null; |
| 221 | switch (tempObjHeader.getObjType()) { |
| 222 | case PcepFecObjectIPv4Ver1.FEC_OBJ_TYPE: |
| 223 | fecObject = PcepFecObjectIPv4Ver1.read(cb); |
| 224 | break; |
| 225 | case PcepFecObjectIPv6Ver1.FEC_OBJ_TYPE: |
| 226 | fecObject = PcepFecObjectIPv6Ver1.read(cb); |
| 227 | break; |
| 228 | case PcepFecObjectIPv4AdjacencyVer1.FEC_OBJ_TYPE: |
| 229 | fecObject = PcepFecObjectIPv4AdjacencyVer1.read(cb); |
| 230 | break; |
| 231 | case PcepFecObjectIPv6AdjacencyVer1.FEC_OBJ_TYPE: |
| 232 | fecObject = PcepFecObjectIPv6AdjacencyVer1.read(cb); |
| 233 | break; |
| 234 | case PcepFecObjectIPv4UnnumberedAdjacencyVer1.FEC_OBJ_TYPE: |
| 235 | fecObject = PcepFecObjectIPv4UnnumberedAdjacencyVer1.read(cb); |
| 236 | break; |
| 237 | default: |
| 238 | throw new PcepParseException("Unkown FEC object type " + tempObjHeader.getObjType()); |
| 239 | } |
| 240 | labelMap.setFECObject(fecObject); |
| 241 | pceLabelUpdate.setLabelMap(labelMap); |
| 242 | } else { |
| 243 | throw new PcepParseException( |
| 244 | "Either <pce-label-download> or <pce-label-map> should be present. Received Class: " |
| 245 | + tempObjHeader.getObjClass()); |
| 246 | } |
| 247 | return pceLabelUpdate; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void write(ChannelBuffer cb) throws PcepParseException { |
| 252 | |
| 253 | if ((labelDownload != null) && (labelMap != null)) { |
| 254 | throw new PcepParseException("Label Download and Label Map both can't be present."); |
| 255 | } |
| 256 | |
| 257 | if ((labelDownload == null) && (labelMap == null)) { |
| 258 | throw new PcepParseException("Either Label Download or Label Map should be present."); |
| 259 | } |
| 260 | |
| 261 | if (labelDownload != null) { |
| 262 | |
| 263 | PcepLspObject lspObject; |
| 264 | PcepSrpObject srpObject; |
| 265 | PcepLabelObject labelObject; |
| 266 | LinkedList<PcepLabelObject> llLabelList; |
| 267 | |
| 268 | srpObject = labelDownload.getSrpObject(); |
| 269 | if (srpObject == null) { |
| 270 | throw new PcepParseException("SRP Object is mandatory object for Label Download."); |
| 271 | } else { |
| 272 | srpObject.write(cb); |
| 273 | } |
| 274 | |
| 275 | lspObject = labelDownload.getLspObject(); |
| 276 | if (lspObject == null) { |
| 277 | throw new PcepParseException("LSP Object is mandatory object for Label Download."); |
| 278 | } else { |
| 279 | lspObject.write(cb); |
| 280 | } |
| 281 | |
| 282 | llLabelList = labelDownload.getLabelList(); |
| 283 | if (llLabelList == null) { |
| 284 | throw new PcepParseException("Label list is mandatory object for Label Download."); |
| 285 | } else { |
| 286 | ListIterator<PcepLabelObject> listIterator = llLabelList.listIterator(); |
| 287 | while (listIterator.hasNext()) { |
| 288 | labelObject = listIterator.next(); |
| 289 | labelObject.write(cb); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (labelMap != null) { |
| 295 | |
| 296 | PcepSrpObject srpObject; |
| 297 | PcepLabelObject labelObject; |
| 298 | PcepFecObject fecObject; |
| 299 | |
| 300 | srpObject = labelMap.getSrpObject(); |
| 301 | if (srpObject == null) { |
| 302 | throw new PcepParseException("SRP Object is mandatory object for Label map."); |
| 303 | } else { |
| 304 | srpObject.write(cb); |
| 305 | } |
| 306 | labelObject = labelMap.getLabelObject(); |
| 307 | if (labelObject == null) { |
| 308 | throw new PcepParseException("label Object is mandatory object for Label map."); |
| 309 | } else { |
| 310 | labelObject.write(cb); |
| 311 | } |
| 312 | fecObject = labelMap.getFECObject(); |
| 313 | if (fecObject == null) { |
| 314 | throw new PcepParseException("fec Object is mandatory object for Label map."); |
| 315 | } else { |
| 316 | fecObject.write(cb); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void setLabelDownload(PcepLabelDownload labelDownload) { |
| 323 | if (this.isLabelMapSet) { |
| 324 | return; |
| 325 | } |
| 326 | this.labelDownload = labelDownload; |
| 327 | this.isLabelDownloadSet = true; |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public PcepLabelDownload getLabelDownload() { |
| 332 | return this.labelDownload; |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public void setLabelMap(PcepLabelMap labelMap) { |
| 337 | if (this.isLabelDownloadSet) { |
| 338 | return; |
| 339 | } |
| 340 | this.labelMap = labelMap; |
| 341 | this.isLabelMapSet = true; |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public PcepLabelMap getLabelMap() { |
| 346 | return this.labelMap; |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public String toString() { |
| 351 | ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass()); |
| 352 | |
| 353 | if (labelDownload instanceof PcepLabelDownload) { |
| 354 | toStrHelper.add("LabelDownload", labelDownload); |
| 355 | } |
| 356 | if (labelMap instanceof PcepLabelMap) { |
| 357 | toStrHelper.add("LabelMap", labelMap); |
| 358 | } |
| 359 | return toStrHelper.toString(); |
| 360 | } |
| 361 | } |