Mahesh Poojary S | 5afd06c | 2015-08-21 14:51:04 +0530 | [diff] [blame^] | 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 StatefulPceCapabilityTlv.
|
| 30 | */
|
| 31 | public class StatefulPceCapabilityTlv implements PcepValueType {
|
| 32 |
|
| 33 | /* STATEFUL-PCE-CAPABILITY TLV format
|
| 34 | *
|
| 35 | * Reference :PCEP Extensions for Stateful PCE draft-ietf-pce-stateful-pce-10
|
| 36 |
|
| 37 | 0 1 2 3
|
| 38 | 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
|
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 40 | | Type=16 | Length=4 |
|
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 42 | | Flags |D|T|I|S|U|
|
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 44 |
|
| 45 | */
|
| 46 | protected static final Logger log = LoggerFactory.getLogger(StatefulPceCapabilityTlv.class);
|
| 47 |
|
| 48 | public static final short TYPE = 16;
|
| 49 | public static final short LENGTH = 4;
|
| 50 | public static final byte UFLAG_SET = 0x01;
|
| 51 | public static final byte SFLAG_SET = 0x02;
|
| 52 | public static final byte IFLAG_SET = 0x04;
|
| 53 | public static final byte TFLAG_SET = 0x08;
|
| 54 | public static final byte DFLAG_SET = 0x10;
|
| 55 | public static final int SET = 1;
|
| 56 |
|
| 57 | private final int rawValue;
|
| 58 | private final boolean bDFlag;
|
| 59 | private final boolean bTFlag;
|
| 60 | private final boolean bIFlag;
|
| 61 | private final boolean bSFlag;
|
| 62 | private final boolean bUFlag;
|
| 63 | private final boolean isRawValueSet;
|
| 64 |
|
| 65 | /**
|
| 66 | * Constructor to initialize variables.
|
| 67 | *
|
| 68 | * @param rawValue Flags
|
| 69 | */
|
| 70 | public StatefulPceCapabilityTlv(int rawValue) {
|
| 71 | this.rawValue = rawValue;
|
| 72 | isRawValueSet = true;
|
| 73 | this.bUFlag = (rawValue & UFLAG_SET) == UFLAG_SET ? true : false;
|
| 74 | this.bSFlag = (rawValue & SFLAG_SET) == SFLAG_SET ? true : false;
|
| 75 | this.bIFlag = (rawValue & IFLAG_SET) == IFLAG_SET ? true : false;
|
| 76 | this.bTFlag = (rawValue & TFLAG_SET) == TFLAG_SET ? true : false;
|
| 77 | this.bDFlag = (rawValue & DFLAG_SET) == DFLAG_SET ? true : false;
|
| 78 | }
|
| 79 |
|
| 80 | /**
|
| 81 | * Constructor to initialize variables.
|
| 82 | *
|
| 83 | * @param bDFlag D-flag
|
| 84 | * @param bTFlag T-flag
|
| 85 | * @param bIFlag I-flag
|
| 86 | * @param bSFlag S-flag
|
| 87 | * @param bUFlag U-flag
|
| 88 | */
|
| 89 | public StatefulPceCapabilityTlv(boolean bDFlag, boolean bTFlag, boolean bIFlag, boolean bSFlag, boolean bUFlag) {
|
| 90 | this.bDFlag = bDFlag;
|
| 91 | this.bTFlag = bTFlag;
|
| 92 | this.bIFlag = bIFlag;
|
| 93 | this.bSFlag = bSFlag;
|
| 94 | this.bUFlag = bUFlag;
|
| 95 | this.rawValue = 0;
|
| 96 | isRawValueSet = false;
|
| 97 | }
|
| 98 |
|
| 99 | /**
|
| 100 | * Returns object of StatefulPceCapabilityTlv.
|
| 101 | *
|
| 102 | * @param raw value Flags
|
| 103 | * @return object of StatefulPceCapabilityTlv
|
| 104 | */
|
| 105 | public static StatefulPceCapabilityTlv of(final int raw) {
|
| 106 | return new StatefulPceCapabilityTlv(raw);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public PcepVersion getVersion() {
|
| 111 | return PcepVersion.PCEP_1;
|
| 112 | }
|
| 113 |
|
| 114 | /**
|
| 115 | * Returns D-flag.
|
| 116 | *
|
| 117 | * @return bDFlag D-flag
|
| 118 | */
|
| 119 | public boolean getDFlag() {
|
| 120 | return bDFlag;
|
| 121 | }
|
| 122 |
|
| 123 | /**
|
| 124 | * Returns T-flag.
|
| 125 | *
|
| 126 | * @return bTFlag T-flag
|
| 127 | */
|
| 128 | public boolean getTFlag() {
|
| 129 | return bTFlag;
|
| 130 | }
|
| 131 |
|
| 132 | /**
|
| 133 | * Returns I-flag.
|
| 134 | *
|
| 135 | * @return bIFlag I-flag
|
| 136 | */
|
| 137 | public boolean getIFlag() {
|
| 138 | return bIFlag;
|
| 139 | }
|
| 140 |
|
| 141 | /**
|
| 142 | * Returns S-flag.
|
| 143 | *
|
| 144 | * @return bSFlag S-flag
|
| 145 | */
|
| 146 | public boolean getSFlag() {
|
| 147 | return bSFlag;
|
| 148 | }
|
| 149 |
|
| 150 | /**
|
| 151 | * Returns U-flag.
|
| 152 | *
|
| 153 | * @return bUFlag U-flag
|
| 154 | */
|
| 155 | public boolean getUFlag() {
|
| 156 | return bUFlag;
|
| 157 | }
|
| 158 |
|
| 159 | /**
|
| 160 | * Returns raw value Flags.
|
| 161 | *
|
| 162 | * @return rawValue Flags
|
| 163 | */
|
| 164 | public int getInt() {
|
| 165 | return rawValue;
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | public short getType() {
|
| 170 | return TYPE;
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public short getLength() {
|
| 175 | return LENGTH;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public int hashCode() {
|
| 180 | if (isRawValueSet) {
|
| 181 | return Objects.hash(rawValue);
|
| 182 | } else {
|
| 183 | return Objects.hash(bDFlag, bTFlag, bIFlag, bSFlag, bUFlag);
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public boolean equals(Object obj) {
|
| 189 | if (this == obj) {
|
| 190 | return true;
|
| 191 | }
|
| 192 | if (obj instanceof StatefulPceCapabilityTlv) {
|
| 193 | StatefulPceCapabilityTlv other = (StatefulPceCapabilityTlv) obj;
|
| 194 | if (isRawValueSet) {
|
| 195 | return Objects.equals(this.rawValue, other.rawValue);
|
| 196 | } else {
|
| 197 | return Objects.equals(this.bDFlag, other.bDFlag) && Objects.equals(this.bTFlag, other.bTFlag)
|
| 198 | && Objects.equals(this.bIFlag, other.bIFlag) && Objects.equals(this.bSFlag, other.bSFlag)
|
| 199 | && Objects.equals(this.bUFlag, other.bUFlag);
|
| 200 | }
|
| 201 | }
|
| 202 | return false;
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public int write(ChannelBuffer c) {
|
| 207 | int iLenStartIndex = c.writerIndex();
|
| 208 | c.writeShort(TYPE);
|
| 209 | c.writeShort(LENGTH);
|
| 210 | if (isRawValueSet) {
|
| 211 | c.writeInt(rawValue);
|
| 212 | } else {
|
| 213 | int temp = 0;
|
| 214 | if (bUFlag) {
|
| 215 | temp = temp | UFLAG_SET;
|
| 216 | }
|
| 217 | if (bSFlag) {
|
| 218 | temp = temp | SFLAG_SET;
|
| 219 | }
|
| 220 | if (bIFlag) {
|
| 221 | temp = temp | IFLAG_SET;
|
| 222 | }
|
| 223 | if (bTFlag) {
|
| 224 | temp = temp | TFLAG_SET;
|
| 225 | }
|
| 226 | if (bDFlag) {
|
| 227 | temp = temp | DFLAG_SET;
|
| 228 | }
|
| 229 | c.writeInt(temp);
|
| 230 | }
|
| 231 | return c.writerIndex() - iLenStartIndex;
|
| 232 | }
|
| 233 |
|
| 234 | /**
|
| 235 | * Reads from channel buffer and returns object of StatefulPceCapabilityTlv.
|
| 236 | *
|
| 237 | * @param c input channel buffer
|
| 238 | * @return object of StatefulPceCapabilityTlv
|
| 239 | */
|
| 240 | public static PcepValueType read(ChannelBuffer c) {
|
| 241 | int temp = c.readInt();
|
| 242 | boolean bDFlag;
|
| 243 | boolean bTFlag;
|
| 244 | boolean bIFlag;
|
| 245 | boolean bSFlag;
|
| 246 | boolean bUFlag;
|
| 247 |
|
| 248 | bUFlag = (temp & UFLAG_SET) == UFLAG_SET ? true : false;
|
| 249 | bSFlag = (temp & SFLAG_SET) == SFLAG_SET ? true : false;
|
| 250 | bIFlag = (temp & IFLAG_SET) == IFLAG_SET ? true : false;
|
| 251 | bTFlag = (temp & TFLAG_SET) == TFLAG_SET ? true : false;
|
| 252 | bDFlag = (temp & DFLAG_SET) == DFLAG_SET ? true : false;
|
| 253 |
|
| 254 | return new StatefulPceCapabilityTlv(bDFlag, bTFlag, bIFlag, bSFlag, bUFlag);
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | public String toString() {
|
| 259 | return MoreObjects.toStringHelper(getClass()).add("type", TYPE).add("Length", LENGTH).add("DFlag", bDFlag)
|
| 260 | .add("TFlag", bTFlag).add("IFlag", bIFlag).add("SFlag", bSFlag).add("UFlag", bUFlag).toString();
|
| 261 | }
|
| 262 | }
|