2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.pcepio.protocol.ver1;
19 import java.util.LinkedList;
20 import java.util.ListIterator;
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;
35 import com.google.common.base.MoreObjects;
38 * Provides PCEP LABEL update .
39 * Reference :draft-zhao-pce-pcep-extension-for-pce-controller-01.
41 public class PcepLabelUpdateVer1 implements PcepLabelUpdate {
44 * <pce-label-update> ::= (<pce-label-download>|<pce-label-map>)
47 <pce-label-download> ::= <SRP>
51 <pce-label-map> ::= <SRP>
55 <label-list > ::= <LABEL>
58 protected static final Logger log = LoggerFactory.getLogger(PcepLabelUpdateVer1.class);
60 //Either PceLabelDownload or PceLabelMap is mandatory.
62 private PcepLabelDownload labelDownload;
63 private boolean isLabelDownloadSet;
65 private PcepLabelMap labelMap;
66 private boolean isLabelMapSet;
69 * Constructor to reset parameters.
71 public PcepLabelUpdateVer1() {
72 this.labelDownload = null;
73 this.isLabelDownloadSet = false;
75 this.isLabelMapSet = false;
79 * Constructor to initialize PCEP label download.
81 * @param labelDownload PCEP label download
83 public PcepLabelUpdateVer1(PcepLabelDownload labelDownload) {
84 this.labelDownload = labelDownload;
85 this.isLabelDownloadSet = true;
87 this.isLabelMapSet = false;
91 * Constructor to initialize PCEP label map.
93 * @param labelMap PCEP label map
95 public PcepLabelUpdateVer1(PcepLabelMap labelMap) {
96 this.labelDownload = null;
97 this.isLabelDownloadSet = false;
98 this.labelMap = labelMap;
99 this.isLabelMapSet = true;
103 * builder class for PCEP label update.
105 static class Builder implements PcepLabelUpdate.Builder {
107 private PcepLabelDownload labelDownload;
108 private boolean isLabelDownloadSet;
109 private PcepLabelMap labelMap;
110 private boolean isLabelMapSet;
113 public PcepLabelUpdate build() throws PcepParseException {
115 if (isLabelDownloadSet) {
116 return new PcepLabelUpdateVer1(labelDownload);
119 return new PcepLabelUpdateVer1(labelMap);
121 if (!isLabelDownloadSet && !isLabelMapSet) {
122 throw new PcepParseException(
123 "Label Download or Label Map is not set while building PcepLabelUpdate Message");
125 return new PcepLabelUpdateVer1();
129 public Builder setLabelDownload(PcepLabelDownload labelDownload) {
130 this.labelDownload = labelDownload;
131 this.isLabelDownloadSet = true;
136 public PcepLabelDownload getLabelDownload() {
137 return labelDownload;
141 public Builder setLabelMap(PcepLabelMap labelMap) {
142 this.labelMap = labelMap;
143 this.isLabelMapSet = true;
148 public PcepLabelMap getLabelMap() {
154 * Reads PcepLabels from the byte stream received from channel buffer.
156 * @param cb of type channel buffer.
157 * @return PcepLabelUpdate object.
158 * @throws PcepParseException when fails to read from channel buffer
160 public static PcepLabelUpdate read(ChannelBuffer cb) throws PcepParseException {
162 PcepLabelUpdateVer1 pceLabelUpdate = new PcepLabelUpdateVer1();
164 PcepSrpObject srpObject;
165 PcepObjectHeader tempObjHeader;
167 //read SRP mandatory Object
168 srpObject = PcepSrpObjectVer1.read(cb);
170 //checking next object
171 cb.markReaderIndex();
173 tempObjHeader = PcepObjectHeader.read(cb);
174 cb.resetReaderIndex();
176 if (tempObjHeader.getObjClass() == PcepLspObjectVer1.LSP_OBJ_CLASS) {
178 //now it is belong to <pce-label-download>
179 PcepLabelDownload labelDownload = new PcepLabelDownload();
182 labelDownload.setSrpObject(srpObject);
185 labelDownload.setLspObject(PcepLspObjectVer1.read(cb));
188 LinkedList<PcepLabelObject> llLabelList = new LinkedList<>();
189 PcepLabelObject labelObject;
191 while (0 < cb.readableBytes()) {
193 cb.markReaderIndex();
194 tempObjHeader = PcepObjectHeader.read(cb);
195 cb.resetReaderIndex();
197 if (tempObjHeader.getObjClass() != PcepLabelObjectVer1.LABEL_OBJ_CLASS) {
200 labelObject = PcepLabelObjectVer1.read(cb);
201 llLabelList.add(labelObject);
203 labelDownload.setLabelList(llLabelList);
204 pceLabelUpdate.setLabelDownload(labelDownload);
205 } else if (tempObjHeader.getObjClass() == PcepLabelObjectVer1.LABEL_OBJ_CLASS) {
206 //belong to <pce-label-map>
207 PcepLabelMap labelMap = new PcepLabelMap();
210 labelMap.setSrpObject(srpObject);
212 //read and set Label Object
213 labelMap.setLabelObject(PcepLabelObjectVer1.read(cb));
215 cb.markReaderIndex();
216 tempObjHeader = PcepObjectHeader.read(cb);
217 cb.resetReaderIndex();
219 PcepFecObject fecObject = null;
220 switch (tempObjHeader.getObjType()) {
221 case PcepFecObjectIPv4Ver1.FEC_OBJ_TYPE:
222 fecObject = PcepFecObjectIPv4Ver1.read(cb);
224 case PcepFecObjectIPv6Ver1.FEC_OBJ_TYPE:
225 fecObject = PcepFecObjectIPv6Ver1.read(cb);
227 case PcepFecObjectIPv4AdjacencyVer1.FEC_OBJ_TYPE:
228 fecObject = PcepFecObjectIPv4AdjacencyVer1.read(cb);
230 case PcepFecObjectIPv6AdjacencyVer1.FEC_OBJ_TYPE:
231 fecObject = PcepFecObjectIPv6AdjacencyVer1.read(cb);
233 case PcepFecObjectIPv4UnnumberedAdjacencyVer1.FEC_OBJ_TYPE:
234 fecObject = PcepFecObjectIPv4UnnumberedAdjacencyVer1.read(cb);
237 throw new PcepParseException("Unkown FEC object type " + tempObjHeader.getObjType());
239 labelMap.setFECObject(fecObject);
240 pceLabelUpdate.setLabelMap(labelMap);
242 throw new PcepParseException(
243 "Either <pce-label-download> or <pce-label-map> should be present. Received Class: "
244 + tempObjHeader.getObjClass());
246 return pceLabelUpdate;
250 public void write(ChannelBuffer cb) throws PcepParseException {
252 if ((labelDownload != null) && (labelMap != null)) {
253 throw new PcepParseException("Label Download and Label Map both can't be present.");
256 if ((labelDownload == null) && (labelMap == null)) {
257 throw new PcepParseException("Either Label Download or Label Map should be present.");
260 if (labelDownload != null) {
262 PcepLspObject lspObject;
263 PcepSrpObject srpObject;
264 PcepLabelObject labelObject;
265 LinkedList<PcepLabelObject> llLabelList;
267 srpObject = labelDownload.getSrpObject();
268 if (srpObject == null) {
269 throw new PcepParseException("SRP Object is mandatory object for Label Download.");
274 lspObject = labelDownload.getLspObject();
275 if (lspObject == null) {
276 throw new PcepParseException("LSP Object is mandatory object for Label Download.");
281 llLabelList = labelDownload.getLabelList();
282 if (llLabelList == null) {
283 throw new PcepParseException("Label list is mandatory object for Label Download.");
285 ListIterator<PcepLabelObject> listIterator = llLabelList.listIterator();
286 while (listIterator.hasNext()) {
287 labelObject = listIterator.next();
288 labelObject.write(cb);
293 if (labelMap != null) {
295 PcepSrpObject srpObject;
296 PcepLabelObject labelObject;
297 PcepFecObject fecObject;
299 srpObject = labelMap.getSrpObject();
300 if (srpObject == null) {
301 throw new PcepParseException("SRP Object is mandatory object for Label map.");
305 labelObject = labelMap.getLabelObject();
306 if (labelObject == null) {
307 throw new PcepParseException("label Object is mandatory object for Label map.");
309 labelObject.write(cb);
311 fecObject = labelMap.getFECObject();
312 if (fecObject == null) {
313 throw new PcepParseException("fec Object is mandatory object for Label map.");
321 public void setLabelDownload(PcepLabelDownload labelDownload) {
322 if (this.isLabelMapSet) {
325 this.labelDownload = labelDownload;
326 this.isLabelDownloadSet = true;
330 public PcepLabelDownload getLabelDownload() {
331 return this.labelDownload;
335 public void setLabelMap(PcepLabelMap labelMap) {
336 if (this.isLabelDownloadSet) {
339 this.labelMap = labelMap;
340 this.isLabelMapSet = true;
344 public PcepLabelMap getLabelMap() {
345 return this.labelMap;
349 public String toString() {
350 return MoreObjects.toStringHelper(getClass())
352 .add("LabelDownload", labelDownload)
353 .add("LabelMap", labelMap)