Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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 java.util.Objects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import com.google.common.base.MoreObjects; |
| 26 | |
| 27 | /** |
| 28 | * Provides MPLS Protocol Mask. |
| 29 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 30 | public class MplsProtocolMaskSubTlv implements PcepValueType { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 41 | protected static final Logger log = LoggerFactory.getLogger(MplsProtocolMaskSubTlv.class); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 42 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 43 | public static final short TYPE = 28; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 44 | public static final short LENGTH = 1; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 45 | 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 S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 58 | public MplsProtocolMaskSubTlv(byte rawValue) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 59 | this.rawValue = rawValue; |
Phanendra Manda | d268a9a | 2015-09-04 15:19:21 -0700 | [diff] [blame] | 60 | this.isRawValueSet = true; |
| 61 | this.bLFlag = (rawValue & LFLAG_SET) == LFLAG_SET; |
| 62 | this.bRFlag = (rawValue & RFLAG_SET) == RFLAG_SET; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * constructor to initialize different Flags. |
| 67 | * |
| 68 | * @param bLFlag L-flag |
| 69 | * @param bRFlag R-flag |
| 70 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 71 | public MplsProtocolMaskSubTlv(boolean bLFlag, boolean bRFlag) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 72 | 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 S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 84 | public static MplsProtocolMaskSubTlv of(final byte raw) { |
| 85 | return new MplsProtocolMaskSubTlv(raw); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 86 | } |
| 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 S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 144 | if (obj instanceof MplsProtocolMaskSubTlv) { |
| 145 | MplsProtocolMaskSubTlv other = (MplsProtocolMaskSubTlv) obj; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 146 | 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 Manda | d268a9a | 2015-09-04 15:19:21 -0700 | [diff] [blame] | 186 | bLFlag = (temp & LFLAG_SET) == LFLAG_SET; |
| 187 | bRFlag = (temp & RFLAG_SET) == RFLAG_SET; |
| 188 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 189 | return new MplsProtocolMaskSubTlv(bLFlag, bRFlag); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 194 | return MoreObjects.toStringHelper(getClass()) |
| 195 | .add("Type", TYPE) |
| 196 | .add("Length", LENGTH) |
| 197 | .add("Value", rawValue) |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 198 | .toString(); |
| 199 | } |
| 200 | } |