blob: 7c2f134328a9d7eaf86862f43dc4ea531510858d [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001package org.onosproject.pcepio.types;
2
3import java.util.Objects;
4import org.jboss.netty.buffer.ChannelBuffer;
5import org.onosproject.pcepio.protocol.PcepVersion;
6import org.slf4j.Logger;
7import org.slf4j.LoggerFactory;
8
9import com.google.common.base.MoreObjects;
10
11/**
12 * Provides PceccCapabilityTlv.
13 */
14public class PceccCapabilityTlv implements PcepValueType {
15
16 /* PCECC CAPABILITY TLV
17 * Reference : draft-zhao-pce-pcep-extension-for-pce-controller-01, section-7.1.1
18
19 0 1 2 3
20 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
21 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22 | Type=32 | Length=4 |
23 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24 | Flags |G|L|
25 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26
27 */
28 protected static final Logger log = LoggerFactory.getLogger(PceccCapabilityTlv.class);
29
30 public static final short TYPE = 32;
31 public static final short LENGTH = 4;
32 public static final int SET = 1;
33 public static final byte LFLAG_CHECK = 0x01;
34 public static final byte GFLAG_CHECK = 0x02;
35
36 private final boolean bGFlag;
37 private final boolean bLFlag;
38
39 private final int rawValue;
40 private final boolean isRawValueSet;
41
42 /**
43 * Constructor to initialize raw Value.
44 *
45 * @param rawValue raw value
46 */
47 public PceccCapabilityTlv(final int rawValue) {
48 this.rawValue = rawValue;
49 this.isRawValueSet = true;
50
Phanendra Mandaf346ea82015-09-04 15:21:39 -070051 bLFlag = (rawValue & LFLAG_CHECK) == LFLAG_CHECK;
52 bGFlag = (rawValue & GFLAG_CHECK) == GFLAG_CHECK;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070053 }
54
55 /**
56 * Constructor to initialize G-flag L-flag.
57 * @param bGFlag G-flag
58 * @param bLFlag L-flag
59 */
60 public PceccCapabilityTlv(boolean bGFlag, boolean bLFlag) {
61 this.bGFlag = bGFlag;
62 this.bLFlag = bLFlag;
63 this.rawValue = 0;
64 this.isRawValueSet = false;
65 }
66
67 /**
68 * Returns newly created PceccCapabilityTlv object.
69 *
70 * @param raw value
71 * @return object of Pcecc Capability Tlv
72 */
73 public static PceccCapabilityTlv of(final int raw) {
74 return new PceccCapabilityTlv(raw);
75 }
76
77 @Override
78 public PcepVersion getVersion() {
79 return PcepVersion.PCEP_1;
80 }
81
82 /**
83 * Returns G-flag.
84 * @return bGFlag G-flag
85 */
86 public boolean getGFlag() {
87 return bGFlag;
88 }
89
90 /**
91 * Returns L-flag.
92 * @return bLFlag L-flag
93 */
94 public boolean getLFlag() {
95 return bLFlag;
96 }
97
98 /**
99 * Returns the raw value.
100 * @return rawValue Flags
101 */
102 public int getInt() {
103 return rawValue;
104 }
105
106 @Override
107 public short getType() {
108 return TYPE;
109 }
110
111 @Override
112 public short getLength() {
113 return LENGTH;
114 }
115
116 @Override
117 public int hashCode() {
118 if (isRawValueSet) {
119 return Objects.hash(rawValue);
120 } else {
121 return Objects.hash(bLFlag, bGFlag);
122 }
123 }
124
125 @Override
126 public boolean equals(Object obj) {
127 if (this == obj) {
128 return true;
129 }
130 if (obj instanceof PceccCapabilityTlv) {
131 PceccCapabilityTlv other = (PceccCapabilityTlv) obj;
132 if (isRawValueSet) {
133 return Objects.equals(this.rawValue, other.rawValue);
134 } else {
135 return Objects.equals(this.bGFlag, other.bGFlag) && Objects.equals(this.bLFlag, other.bLFlag);
136 }
137 }
138 return false;
139 }
140
141 @Override
142 public int write(ChannelBuffer c) {
143 int iLenStartIndex = c.writerIndex();
144 int temp = 0;
145 c.writeShort(TYPE);
146 c.writeShort(LENGTH);
147 if (isRawValueSet) {
148 c.writeInt(rawValue);
149 } else {
150 if (bGFlag) {
151 temp = temp | GFLAG_CHECK;
152 }
153 if (bLFlag) {
154 temp = temp | LFLAG_CHECK;
155 }
156 c.writeInt(temp);
157 }
158 return c.writerIndex() - iLenStartIndex;
159 }
160
161 /**
162 * Reads channel buffer and returns object of PceccCapabilityTlv.
163 *
164 * @param c input channel buffer
165 * @return object of PceccCapabilityTlv
166 */
167 public static PceccCapabilityTlv read(ChannelBuffer c) {
168 return PceccCapabilityTlv.of(c.readInt());
169 }
170
171 @Override
172 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700173 return MoreObjects.toStringHelper(getClass())
174 .add("Type", TYPE)
175 .add("Length", LENGTH)
176 .add("Value", rawValue)
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700177 .toString();
178 }
179}