blob: 0ca4fed602f5662fb01cce2a0cd100a2bea0727b [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07003 *
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 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053030public class MplsProtocolMaskSubTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070031
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 */
Ray Milkey9c9cde42018-01-12 14:22:06 -080041 private static final Logger log = LoggerFactory.getLogger(MplsProtocolMaskSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070042
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053043 public static final short TYPE = 28;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070044 public static final short LENGTH = 1;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070045 public static final byte LFLAG_SET = (byte) 0x80;
46 public static final byte RFLAG_SET = 0x40;
47
48 private final byte rawValue;
49 private final boolean bLFlag;
50 private final boolean bRFlag;
51 private final boolean isRawValueSet;
52
53 /**
54 * constructor to initialize rawValue.
55 *
56 * @param rawValue MPLS Protocol Mask Flag Bits
57 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053058 public MplsProtocolMaskSubTlv(byte rawValue) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070059 this.rawValue = rawValue;
Phanendra Mandad268a9a2015-09-04 15:19:21 -070060 this.isRawValueSet = true;
61 this.bLFlag = (rawValue & LFLAG_SET) == LFLAG_SET;
62 this.bRFlag = (rawValue & RFLAG_SET) == RFLAG_SET;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070063 }
64
65 /**
66 * constructor to initialize different Flags.
67 *
68 * @param bLFlag L-flag
69 * @param bRFlag R-flag
70 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053071 public MplsProtocolMaskSubTlv(boolean bLFlag, boolean bRFlag) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070072 this.bLFlag = bLFlag;
73 this.bRFlag = bRFlag;
74 this.rawValue = 0;
75 isRawValueSet = false;
76 }
77
78 /**
79 * Returns newly created MPLSProtocolMaskTlv object.
80 *
81 * @param raw MPLS Protocol Mask Tlv
82 * @return new object of MPLS Protocol Mask Tlv
83 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053084 public static MplsProtocolMaskSubTlv of(final byte raw) {
85 return new MplsProtocolMaskSubTlv(raw);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070086 }
87
88 /**
89 * Returns L-flag.
90 *
91 * @return bLFlag L-flag
92 */
93 public boolean getbLFlag() {
94 return bLFlag;
95 }
96
97 /**
98 * Returns R-flag.
99 *
100 * @return bRFlag R-flag
101 */
102 public boolean getbRFlag() {
103 return bRFlag;
104 }
105
106 /**
107 * Returns raw value.
108 *
109 * @return rawValue raw value
110 */
111 public byte getByte() {
112 return rawValue;
113 }
114
115 @Override
116 public PcepVersion getVersion() {
117 return PcepVersion.PCEP_1;
118 }
119
120 @Override
121 public short getType() {
122 return TYPE;
123 }
124
125 @Override
126 public short getLength() {
127 return LENGTH;
128 }
129
130 @Override
131 public int hashCode() {
132 if (isRawValueSet) {
133 return Objects.hash(rawValue);
134 } else {
135 return Objects.hash(bLFlag, bRFlag);
136 }
137 }
138
139 @Override
140 public boolean equals(Object obj) {
141 if (this == obj) {
142 return true;
143 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530144 if (obj instanceof MplsProtocolMaskSubTlv) {
145 MplsProtocolMaskSubTlv other = (MplsProtocolMaskSubTlv) obj;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700146 if (isRawValueSet) {
147 return Objects.equals(this.rawValue, other.rawValue);
148 } else {
149 return Objects.equals(this.bLFlag, other.bLFlag) && Objects.equals(this.bRFlag, other.bRFlag);
150 }
151 }
152 return false;
153 }
154
155 @Override
156 public int write(ChannelBuffer c) {
157 int iLenStartIndex = c.writerIndex();
158 c.writeShort(TYPE);
159 c.writeShort(LENGTH);
160 if (isRawValueSet) {
161 c.writeByte(rawValue);
162 } else {
163 byte temp = 0;
164 if (bLFlag) {
165 temp = (byte) (temp | LFLAG_SET);
166 }
167 if (bRFlag) {
168 temp = (byte) (temp | RFLAG_SET);
169 }
170 c.writeByte(temp);
171 }
172 return c.writerIndex() - iLenStartIndex;
173 }
174
175 /**
176 * Reads the channel buffer and returns object of MPLS Protocol Mask Tlv.
177 *
178 * @param c input channel buffer
179 * @return object of MPLS Protocol Mask Tlv
180 */
181 public static PcepValueType read(ChannelBuffer c) {
182 byte temp = c.readByte();
183 boolean bLFlag;
184 boolean bRFlag;
185
Phanendra Mandad268a9a2015-09-04 15:19:21 -0700186 bLFlag = (temp & LFLAG_SET) == LFLAG_SET;
187 bRFlag = (temp & RFLAG_SET) == RFLAG_SET;
188
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530189 return new MplsProtocolMaskSubTlv(bLFlag, bRFlag);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700190 }
191
192 @Override
193 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700194 return MoreObjects.toStringHelper(getClass())
195 .add("Type", TYPE)
196 .add("Length", LENGTH)
197 .add("Value", rawValue)
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700198 .toString();
199 }
200}