326b66b7acc8aedecb9bfc3f6d1e7e12aaf37344
[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 package org.onosproject.pcepio.types;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.onosproject.pcepio.protocol.PcepVersion;
20
21 import com.google.common.base.MoreObjects;
22
23 /**
24  * Provides Pcep Rsvp Ipv4 Error Spec.
25  */
26 public class PcepRsvpIpv4ErrorSpec implements PcepRsvpErrorSpec {
27
28     /*
29        RSVP error spec object header.
30         0             1              2             3
31     +-------------+-------------+-------------+-------------+
32     |       Length (bytes)      |  Class-Num  |   C-Type    |
33     +-------------+-------------+-------------+-------------+
34     |                                                       |
35     //                  (Object contents)                   //
36     |                                                       |
37     +-------------+-------------+-------------+-------------+
38
39     Ref :  ERROR_SPEC @ RFC2205
40
41     IPv4 ERROR_SPEC object: Class = 6, C-Type = 1
42     +-------------+-------------+-------------+-------------+
43     |            IPv4 Error Node Address (4 bytes)          |
44     +-------------+-------------+-------------+-------------+
45     |    Flags    |  Error Code |        Error Value        |
46     +-------------+-------------+-------------+-------------+
47
48      */
49
50     PcepRsvpSpecObjHeader objHeader;
51     public static final byte CLASS_NUM = 0x06;
52     public static final byte CLASS_TYPE = 0x01;
53     public static final byte CLASS_LENGTH = 0x0c;
54     private int ipv4Addr;
55     private byte flags;
56     private byte errCode;
57     private short errValue;
58
59     /**
60      * Constructor to initialize obj header, ipv4 addr, flags, err code and err value.
61      *
62      * @param objHeader rsvp ipv4 error spec object header
63      * @param ipv4Addr ipv4 address
64      * @param flags flags value
65      * @param errCode error code value
66      * @param errValue error value
67      */
68     public PcepRsvpIpv4ErrorSpec(PcepRsvpSpecObjHeader objHeader, int ipv4Addr, byte flags, byte errCode,
69             short errValue) {
70         this.objHeader = objHeader;
71         this.ipv4Addr = ipv4Addr;
72         this.flags = flags;
73         this.errCode = errCode;
74         this.errValue = errValue;
75     }
76
77     /**
78      * Constructor to initialize ipv4 address, flags, err code and err value.
79      *
80      * @param ipv4Addr ipv4 address
81      * @param flags flags value
82      * @param errCode error code
83      * @param errValue error value
84      */
85     public PcepRsvpIpv4ErrorSpec(int ipv4Addr, byte flags, byte errCode, short errValue) {
86         this.objHeader = new PcepRsvpSpecObjHeader(CLASS_LENGTH, CLASS_NUM, CLASS_TYPE);
87         this.ipv4Addr = ipv4Addr;
88         this.flags = flags;
89         this.errCode = errCode;
90         this.errValue = errValue;
91     }
92
93     @Override
94     public int write(ChannelBuffer cb) {
95         int objLenIndex = objHeader.write(cb);
96         cb.writeInt(ipv4Addr);
97         cb.writeByte(flags);
98         cb.writeByte(errCode);
99         cb.writeShort(errValue);
100         short objLen = (short) (cb.writerIndex() - objLenIndex);
101         cb.setShort(objLenIndex, objLen);
102         return objLen;
103     }
104
105     /**
106      * Reads PCPE RSVP error spec from channel buffer and returns PCEP rsvp IPv4 error spec object.
107      *
108      * @param cb channel buffer
109      * @return PCEP rsvp IPv4 error spec object
110      */
111     public static PcepRsvpErrorSpec read(ChannelBuffer cb) {
112         PcepRsvpSpecObjHeader objHeader;
113         int ipv4Addr;
114         byte flags;
115         byte errCode;
116         short errValue;
117
118         objHeader = PcepRsvpSpecObjHeader.read(cb);
119         ipv4Addr = cb.readInt();
120         flags = cb.readByte();
121         errCode = cb.readByte();
122         errValue = cb.readShort();
123         return new PcepRsvpIpv4ErrorSpec(objHeader, ipv4Addr, flags, errCode, errValue);
124     }
125
126     @Override
127     public PcepVersion getVersion() {
128         return PcepVersion.PCEP_1;
129     }
130
131     @Override
132     public short getType() {
133         return StatefulRsvpErrorSpecTlv.TYPE;
134     }
135
136     @Override
137     public short getLength() {
138         return CLASS_LENGTH;
139     }
140
141     @Override
142     public byte getClassNum() {
143         return CLASS_NUM;
144     }
145
146     @Override
147     public byte getClassType() {
148         return CLASS_TYPE;
149     }
150
151     @Override
152     public String toString() {
153         return MoreObjects.toStringHelper(getClass())
154                 .add("IPv4Address", ipv4Addr)
155                 .add("flags", flags)
156                 .add("errorCode", errCode)
157                 .add("errorValue", errValue)
158                 .toString();
159     }
160 }