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