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