blob: 0eba814a369eccf2cd69fe770a7ed44bc3a21c7c [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
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 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 */
117 public StatefulRsvpErrorSpecTlv(PcepRsvpErrorSpec rsvpErrSpecObj) {
118 this.rsvpErrSpecObj = rsvpErrSpecObj;
119 this.isErrSpceObjSet = true;
120 }
121
122 /**
123 * Returns PcepRsvpErrorSpecObject.
124 *
125 * @return rsvpErrSpecObj
126 */
127 public PcepRsvpErrorSpec getPcepRsvpErrorSpec() {
128 return this.rsvpErrSpecObj;
129 }
130
131 @Override
132 public PcepVersion getVersion() {
133 return PcepVersion.PCEP_1;
134 }
135
136 @Override
137 public short getType() {
138 return TYPE;
139 }
140
141 @Override
142 public short getLength() {
143 return hLength;
144 }
145
146 /**
147 * Reads channel buffer and returns object of StatefulRsvpErrorSpecTlv.
148 *
149 * @param cb of type channel buffer
150 * @return object of StatefulRsvpErrorSpecTlv
151 * @throws PcepParseException while parsing this tlv from channel buffer
152 */
153 public static PcepValueType read(ChannelBuffer cb) throws PcepParseException {
154
155 PcepRsvpErrorSpec rsvpErrSpecObj = null;
156 PcepRsvpSpecObjHeader rsvpErrSpecObjHeader;
157
158 cb.markReaderIndex();
159 rsvpErrSpecObjHeader = PcepRsvpSpecObjHeader.read(cb);
160 cb.resetReaderIndex();
161
162 if (PcepRsvpIpv4ErrorSpec.CLASS_NUM == rsvpErrSpecObjHeader.getObjClassNum()
163 && PcepRsvpIpv4ErrorSpec.CLASS_TYPE == rsvpErrSpecObjHeader.getObjClassType()) {
164 rsvpErrSpecObj = PcepRsvpIpv4ErrorSpec.read(cb);
165 } else if (PcepRsvpIpv6ErrorSpec.CLASS_NUM == rsvpErrSpecObjHeader.getObjClassNum()
166 && PcepRsvpIpv6ErrorSpec.CLASS_TYPE == rsvpErrSpecObjHeader.getObjClassType()) {
167 rsvpErrSpecObj = PcepRsvpIpv6ErrorSpec.read(cb);
168 } else if (PcepRsvpUserErrorSpec.CLASS_NUM == rsvpErrSpecObjHeader.getObjClassNum()
169 && PcepRsvpUserErrorSpec.CLASS_TYPE == rsvpErrSpecObjHeader.getObjClassType()) {
170 rsvpErrSpecObj = PcepRsvpUserErrorSpec.read(cb);
171 }
172 return new StatefulRsvpErrorSpecTlv(rsvpErrSpecObj);
173 }
174
175 @Override
176 public int hashCode() {
177 return Objects.hash(rsvpErrSpecObj.hashCode());
178 }
179
180 @Override
181 public boolean equals(Object obj) {
182 if (this == obj) {
183 return true;
184 }
185 if (obj instanceof StatefulRsvpErrorSpecTlv) {
186 StatefulRsvpErrorSpecTlv other = (StatefulRsvpErrorSpecTlv) obj;
187 return Objects.equals(this.rsvpErrSpecObj, other.rsvpErrSpecObj);
188 }
189 return false;
190 }
191
192 @Override
193 public int write(ChannelBuffer c) {
194 int iStartIndex = c.writerIndex();
195 c.writeShort(TYPE);
196 int tlvLenIndex = c.writerIndex();
197 hLength = 0;
198 c.writeShort(hLength);
199 if (isErrSpceObjSet) {
200 rsvpErrSpecObj.write(c);
201 }
202 hLength = (short) (c.writerIndex() - iStartIndex);
203 c.setShort(tlvLenIndex, (hLength - OBJECT_HEADER_LENGTH));
204
205 return c.writerIndex() - iStartIndex;
206 }
207
208 @Override
209 public String toString() {
210 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
211
212 if (!isErrSpceObjSet) {
213 toStrHelper.add("Type", TYPE).add("Length", hLength);
214 } else {
215 toStrHelper.add("Type", TYPE).add("Length", hLength).add("RSVPErrorSpecObject", rsvpErrSpecObj);
216 }
217 return toStrHelper.toString();
218 }
219}