5d323f6a018ca1f3a150d31330ac13d2c8143132
[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.protocol.PcepVersion;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.MoreObjects;
27
28 /**
29  * Provides StatefulLspErrorCodeTlv.
30  */
31 public class StatefulLspErrorCodeTlv implements PcepValueType {
32
33     /*                  LSP-ERROR-CODE TLV format
34      *
35      * Reference :PCEP Extensions for Stateful PCE draft-ietf-pce-stateful-pce-10
36      *
37
38     0                   1                   2                   3
39     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
40     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41     |           Type=20             |            Length=4           |
42     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43     |                          LSP Error Code                       |
44     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45
46      */
47
48     protected static final Logger log = LoggerFactory.getLogger(StatefulLspErrorCodeTlv.class);
49
50     public static final short TYPE = 20;
51     public static final short LENGTH = 4;
52     private final int rawValue;
53
54     /**
55      * Constructor to initialize raw Value.
56      *
57      * @param rawValue lsp error code value
58      */
59     public StatefulLspErrorCodeTlv(int rawValue) {
60         this.rawValue = rawValue;
61     }
62
63     /**
64      * Creates object of StatefulLspErrorCodeTlv.
65      *
66      * @param raw lsp error code value
67      * @return object of StatefulLspErrorCodeTlv
68      */
69     public static StatefulLspErrorCodeTlv of(int raw) {
70         return new StatefulLspErrorCodeTlv(raw);
71     }
72
73     @Override
74     public PcepVersion getVersion() {
75         return PcepVersion.PCEP_1;
76     }
77
78     /**
79      * Returns lsp error code value.
80      *
81      * @return lsp error code value
82      */
83     public int getInt() {
84         return rawValue;
85     }
86
87     @Override
88     public short getLength() {
89         return LENGTH;
90     }
91
92     @Override
93     public short getType() {
94         return TYPE;
95     }
96
97     @Override
98     public int hashCode() {
99         return Objects.hash(rawValue);
100     }
101
102     @Override
103     public boolean equals(Object obj) {
104         if (this == obj) {
105             return true;
106         }
107         if (obj instanceof StatefulLspErrorCodeTlv) {
108             StatefulLspErrorCodeTlv other = (StatefulLspErrorCodeTlv) obj;
109             return Objects.equals(this.rawValue, other.rawValue);
110         }
111         return false;
112     }
113
114     @Override
115     public int write(ChannelBuffer c) {
116         int iLenStartIndex = c.writerIndex();
117         c.writeShort(TYPE);
118         c.writeShort(LENGTH);
119         c.writeInt(rawValue);
120         return c.writerIndex() - iLenStartIndex;
121     }
122
123     /**
124      * Reads the channel buffer and returns object of StatefulLspErrorCodeTlv.
125      *
126      * @param c of type channel buffer
127      * @return object of StatefulLspErrorCodeTlv
128      */
129     public static StatefulLspErrorCodeTlv read(ChannelBuffer c) {
130         return StatefulLspErrorCodeTlv.of(c.readInt());
131     }
132
133     @Override
134     public String toString() {
135         return MoreObjects.toStringHelper(getClass())
136                 .add("Type", TYPE)
137                 .add("Length", LENGTH)
138                 .add("Value", rawValue)
139                 .toString();
140     }
141
142 }