blob: ba5231b5ba81a25c744a3360f27d5ef73739d230 [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 */
16package org.onosproject.pcepio.types;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.pcepio.protocol.PcepVersion;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.google.common.base.MoreObjects;
26
27/**
28 * Provides MPLS Protocol Mask.
29 */
30public class MPLSProtocolMaskTlv implements PcepValueType {
31
32 /* Reference :[I-D.ietf-idr-ls-distribution]/3.3.2.2
33 * 0 1 2 3
34 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
35 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 | Type=TDB39 | Length =1 |
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 |L|R| Reserved |
39 +-+-+-+-+-+-+-+-+
40 */
41 protected static final Logger log = LoggerFactory.getLogger(MPLSProtocolMaskTlv.class);
42
43 public static final short TYPE = 1094; //TDB39
44 public static final short LENGTH = 1;
45 public static final int SET = 1;
46 public static final byte LFLAG_SET = (byte) 0x80;
47 public static final byte RFLAG_SET = 0x40;
48
49 private final byte rawValue;
50 private final boolean bLFlag;
51 private final boolean bRFlag;
52 private final boolean isRawValueSet;
53
54 /**
55 * constructor to initialize rawValue.
56 *
57 * @param rawValue MPLS Protocol Mask Flag Bits
58 */
59 public MPLSProtocolMaskTlv(byte rawValue) {
60 this.rawValue = rawValue;
61 isRawValueSet = true;
62 byte temp = rawValue;
63 if ((temp & LFLAG_SET) == SET) {
64 this.bLFlag = true;
65
66 } else {
67 this.bLFlag = false;
68 }
69 if ((temp & RFLAG_SET) == SET) {
70 this.bRFlag = true;
71 } else {
72 this.bRFlag = false;
73 }
74 }
75
76 /**
77 * constructor to initialize different Flags.
78 *
79 * @param bLFlag L-flag
80 * @param bRFlag R-flag
81 */
82 public MPLSProtocolMaskTlv(boolean bLFlag, boolean bRFlag) {
83 this.bLFlag = bLFlag;
84 this.bRFlag = bRFlag;
85 this.rawValue = 0;
86 isRawValueSet = false;
87 }
88
89 /**
90 * Returns newly created MPLSProtocolMaskTlv object.
91 *
92 * @param raw MPLS Protocol Mask Tlv
93 * @return new object of MPLS Protocol Mask Tlv
94 */
95 public static MPLSProtocolMaskTlv of(final byte raw) {
96 return new MPLSProtocolMaskTlv(raw);
97 }
98
99 /**
100 * Returns L-flag.
101 *
102 * @return bLFlag L-flag
103 */
104 public boolean getbLFlag() {
105 return bLFlag;
106 }
107
108 /**
109 * Returns R-flag.
110 *
111 * @return bRFlag R-flag
112 */
113 public boolean getbRFlag() {
114 return bRFlag;
115 }
116
117 /**
118 * Returns raw value.
119 *
120 * @return rawValue raw value
121 */
122 public byte getByte() {
123 return rawValue;
124 }
125
126 @Override
127 public PcepVersion getVersion() {
128 return PcepVersion.PCEP_1;
129 }
130
131 @Override
132 public short getType() {
133 return TYPE;
134 }
135
136 @Override
137 public short getLength() {
138 return LENGTH;
139 }
140
141 @Override
142 public int hashCode() {
143 if (isRawValueSet) {
144 return Objects.hash(rawValue);
145 } else {
146 return Objects.hash(bLFlag, bRFlag);
147 }
148 }
149
150 @Override
151 public boolean equals(Object obj) {
152 if (this == obj) {
153 return true;
154 }
155 if (obj instanceof MPLSProtocolMaskTlv) {
156 MPLSProtocolMaskTlv other = (MPLSProtocolMaskTlv) obj;
157 if (isRawValueSet) {
158 return Objects.equals(this.rawValue, other.rawValue);
159 } else {
160 return Objects.equals(this.bLFlag, other.bLFlag) && Objects.equals(this.bRFlag, other.bRFlag);
161 }
162 }
163 return false;
164 }
165
166 @Override
167 public int write(ChannelBuffer c) {
168 int iLenStartIndex = c.writerIndex();
169 c.writeShort(TYPE);
170 c.writeShort(LENGTH);
171 if (isRawValueSet) {
172 c.writeByte(rawValue);
173 } else {
174 byte temp = 0;
175 if (bLFlag) {
176 temp = (byte) (temp | LFLAG_SET);
177 }
178 if (bRFlag) {
179 temp = (byte) (temp | RFLAG_SET);
180 }
181 c.writeByte(temp);
182 }
183 return c.writerIndex() - iLenStartIndex;
184 }
185
186 /**
187 * Reads the channel buffer and returns object of MPLS Protocol Mask Tlv.
188 *
189 * @param c input channel buffer
190 * @return object of MPLS Protocol Mask Tlv
191 */
192 public static PcepValueType read(ChannelBuffer c) {
193 byte temp = c.readByte();
194 boolean bLFlag;
195 boolean bRFlag;
196
197 if ((temp & LFLAG_SET) == SET) {
198 bLFlag = true;
199 } else {
200 bLFlag = false;
201 }
202 if ((temp & RFLAG_SET) == SET) {
203 bRFlag = true;
204 } else {
205 bRFlag = false;
206 }
207 return new MPLSProtocolMaskTlv(bLFlag, bRFlag);
208 }
209
210 @Override
211 public String toString() {
212 return MoreObjects.toStringHelper(getClass()).add("Type", TYPE).add("Length", LENGTH).add("Value", rawValue)
213 .toString();
214 }
215}