blob: 83ec8aae403057d5f5e564e6697ac69c26159637 [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.PcepLabelRange;
25import org.onosproject.pcepio.protocol.PcepLabelRangeObject;
26import org.onosproject.pcepio.protocol.PcepSrpObject;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.google.common.base.MoreObjects;
31
32/**
33 * Provides PCEP Label Range.
34 */
35public class PcepLabelRangeVer1 implements PcepLabelRange {
36
Ray Milkey9c9cde42018-01-12 14:22:06 -080037 private static final Logger log = LoggerFactory.getLogger(PcepLabelRangeVer1.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070038
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 SHIMIZU9b8274c2015-09-04 15:54:24 -0700107 LinkedList<PcepLabelRangeObject> llLabelRangeList = new LinkedList<>();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700108 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 SHIMIZU7cdbcf72015-09-03 14:43:05 -0700163 return MoreObjects.toStringHelper(getClass())
164 .add("srpObject", srpObject)
165 .add("LabelRangeList", llLabelRangeList)
166 .toString();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700167 }
168}