blob: 7590033dc6e0c7e59fbcc6b660202f573983eaf4 [file] [log] [blame]
bharat saraswalf7364db2015-08-11 13:39:31 +05301/*
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
17package org.onosproject.pcepio.types;
18
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.pcepio.exceptions.PcepParseException;
23import org.onosproject.pcepio.protocol.PcepVersion;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import com.google.common.base.MoreObjects;
28import com.google.common.base.MoreObjects.ToStringHelper;
29
30/**
31 * Provides StatefulRsvpErrorSpecTlv.
32 */
33public class StatefulRsvpErrorSpecTlv implements PcepValueType {
34
35 protected static final Logger log = LoggerFactory.getLogger(StatefulRsvpErrorSpecTlv.class);
36
37 /* RSVP-ERROR-SPEC TLV format
38 * Reference :PCEP Extensions for Stateful PCE draft-ietf-pce-stateful-pce-10
39 *
40 *
41
42 0 1 2 3
43 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 | Type=21 | Length (variable) |
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 | |
48 + RSVP ERROR_SPEC or USER_ERROR_SPEC Object +
49 | |
50 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51
52 0 1 2 3
53 +-------------+-------------+-------------+-------------+
54 | Length (bytes) | Class-Num | C-Type |
55 +-------------+-------------+-------------+-------------+
56 | |
57 // (Object contents) //
58 | |
59 +-------------+-------------+-------------+-------------+
60
61 Ref : ERROR_SPEC @ RFC2205
62
63 IPv4 ERROR_SPEC object: Class = 6, C-Type = 1
64 +-------------+-------------+-------------+-------------+
65 | IPv4 Error Node Address (4 bytes) |
66 +-------------+-------------+-------------+-------------+
67 | Flags | Error Code | Error Value |
68 +-------------+-------------+-------------+-------------+
69
70
71 IPv6 ERROR_SPEC object: Class = 6, C-Type = 2
72 +-------------+-------------+-------------+-------------+
73 | |
74 + +
75 | |
76 + IPv6 Error Node Address (16 bytes) +
77 | |
78 + +
79 | |
80 +-------------+-------------+-------------+-------------+
81 | Flags | Error Code | Error Value |
82 +-------------+-------------+-------------+-------------+
83
84
85 Ref : USER_ERROR_SPEC @ RFC5284
86 USER_ERROR_SPEC object: Class = 194, C-Type = 1
87 0 1 2 3
88 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
89 +---------------+---------------+---------------+---------------+
90 | Enterprise Number |
91 +---------------+---------------+---------------+---------------+
92 | Sub Org | Err Desc Len | User Error Value |
93 +---------------+---------------+---------------+---------------+
94 | |
95 ~ Error Description ~
96 | |
97 +---------------+---------------+---------------+---------------+
98 | |
99 ~ User-Defined Subobjects ~
100 | |
101 +---------------+---------------+---------------+---------------+
102
103 */
104
105 public static final short TYPE = 21;
106 public static final int OBJECT_HEADER_LENGTH = 4;
107 private final short hLength;
108
109 private final PcepRsvpErrorSpec rsvpErrSpecObj;
110 private final boolean isErrSpceObjSet;
111
112 /**
113 * Constructor to initialize errSpecObj.
114 *
115 * @param rsvpErrSpecObj Rsvp error spec object
116 * @param hLength length of rsvp error spec object
117 */
118 public StatefulRsvpErrorSpecTlv(PcepRsvpErrorSpec rsvpErrSpecObj, short hLength) {
119 this.rsvpErrSpecObj = rsvpErrSpecObj;
120 this.isErrSpceObjSet = true;
121 this.hLength = hLength;
122 }
123
124 /**
125 * Returns PcepRsvpErrorSpecObject.
126 *
127 * @return rsvpErrSpecObj
128 */
129 public PcepRsvpErrorSpec getPcepRsvpErrorSpec() {
130 return this.rsvpErrSpecObj;
131 }
132
133 @Override
134 public PcepVersion getVersion() {
135 return PcepVersion.PCEP_1;
136 }
137
138 @Override
139 public short getType() {
140 return TYPE;
141 }
142
143 @Override
144 public short getLength() {
145 return hLength;
146 }
147
148 /**
149 * Reads channel buffer and returns object of StatefulRsvpErrorSpecTlv.
150 *
151 * @param cb of type channel buffer
152 * @return object of StatefulRsvpErrorSpecTlv
153 */
154 public static PcepValueType read(ChannelBuffer cb) throws PcepParseException {
155
156 PcepRsvpErrorSpec rsvpErrSpecObj = null;
157 PcepRsvpSpecObjHeader rsvpErrSpecObjHeader;
158
159 cb.markReaderIndex();
160 rsvpErrSpecObjHeader = PcepRsvpSpecObjHeader.read(cb);
161 cb.resetReaderIndex();
162
163 if (PcepRsvpIpv4ErrorSpec.CLASS_NUM == rsvpErrSpecObjHeader.getObjClassNum()
164 && PcepRsvpIpv4ErrorSpec.CLASS_TYPE == rsvpErrSpecObjHeader.getObjClassType()) {
165 rsvpErrSpecObj = PcepRsvpIpv4ErrorSpec.read(cb);
166 } else if (PcepRsvpIpv6ErrorSpec.CLASS_NUM == rsvpErrSpecObjHeader.getObjClassNum()
167 && PcepRsvpIpv6ErrorSpec.CLASS_TYPE == rsvpErrSpecObjHeader.getObjClassType()) {
168 rsvpErrSpecObj = PcepRsvpIpv6ErrorSpec.read(cb);
169 } else if (PcepRsvpUserErrorSpec.CLASS_NUM == rsvpErrSpecObjHeader.getObjClassNum()
170 && PcepRsvpUserErrorSpec.CLASS_TYPE == rsvpErrSpecObjHeader.getObjClassType()) {
171 rsvpErrSpecObj = PcepRsvpUserErrorSpec.read(cb);
172 }
173 return rsvpErrSpecObj;
174 }
175
176 @Override
177 public int hashCode() {
178 return Objects.hash(rsvpErrSpecObj.hashCode());
179 }
180
181 @Override
182 public boolean equals(Object obj) {
183 if (this == obj) {
184 return true;
185 }
186 if (obj instanceof StatefulRsvpErrorSpecTlv) {
187 StatefulRsvpErrorSpecTlv other = (StatefulRsvpErrorSpecTlv) obj;
188 return Objects.equals(this.rsvpErrSpecObj, other.rsvpErrSpecObj);
189 }
190 return false;
191 }
192
193 @Override
194 public int write(ChannelBuffer c) {
195 int iStartIndex = c.writerIndex();
196 c.writeShort(TYPE);
197 int tlvLenIndex = c.writerIndex();
198 c.writeShort(hLength);
199 if (isErrSpceObjSet) {
200 rsvpErrSpecObj.write(c);
201 }
202 short tlvLen = (short) (c.writerIndex() - iStartIndex + 4);
203 c.setShort(tlvLenIndex, tlvLen);
204
205 return tlvLen;
206 }
207
208 @Override
bharat saraswalf7364db2015-08-11 13:39:31 +0530209 public String toString() {
210 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
211
212 if (!isErrSpceObjSet) {
bharat saraswale1806302015-08-21 12:16:46 +0530213 toStrHelper.add("Type", TYPE).add("Length", hLength);
bharat saraswalf7364db2015-08-11 13:39:31 +0530214 } else {
bharat saraswale1806302015-08-21 12:16:46 +0530215 toStrHelper.add("Type", TYPE).add("Length", hLength).add("RSVPErrorSpecObject", rsvpErrSpecObj);
bharat saraswalf7364db2015-08-11 13:39:31 +0530216 }
217 return toStrHelper.toString();
218 }
219}