2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.types.attr;
18 import java.util.Objects;
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.bgpio.exceptions.BgpParseException;
22 import org.onosproject.bgpio.types.BgpErrorType;
23 import org.onosproject.bgpio.types.BgpValueType;
24 import org.onosproject.bgpio.util.Validation;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.MoreObjects;
31 * Implements BGP MPLS protocol mask attribute.
33 public class BgpLinkAttrMplsProtocolMask implements BgpValueType {
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpLinkAttrMplsProtocolMask.class);
38 public static final int ATTRLINK_MPLSPROTOMASK = 1094;
39 public static final int MASK_BYTE_LEN = 1;
41 private final boolean bLdp;
42 private final boolean bRsvpTe;
44 public static final byte FIRST_BIT = (byte) 0x80;
45 public static final byte SECOND_BIT = 0x40;
48 * Constructor to initialize the values.
50 * @param bLdp boolean value true if LDP flag is available
51 * @param bRsvpTe boolean value true if RSVP TE information is available
53 public BgpLinkAttrMplsProtocolMask(boolean bLdp, boolean bRsvpTe) {
55 this.bRsvpTe = bRsvpTe;
59 * Returns object of this class with specified values.
61 * @param bLdp boolean value true if LDP flag is available
62 * @param bRsvpTe boolean value true if RSVP TE information is available
63 * @return object of BgpLinkAttrMplsProtocolMask
65 public static BgpLinkAttrMplsProtocolMask of(final boolean bLdp,
66 final boolean bRsvpTe) {
67 return new BgpLinkAttrMplsProtocolMask(bLdp, bRsvpTe);
71 * Reads the BGP link attributes MPLS protocol mask.
73 * @param cb Channel buffer
74 * @return object of type BgpLinkAttrMPLSProtocolMask
75 * @throws BgpParseException while parsing BgpLinkAttrMplsProtocolMask
77 public static BgpLinkAttrMplsProtocolMask read(ChannelBuffer cb)
78 throws BgpParseException {
80 boolean bRsvpTe = false;
82 short lsAttrLength = cb.readShort();
84 if ((lsAttrLength != MASK_BYTE_LEN)
85 || (cb.readableBytes() < lsAttrLength)) {
86 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
87 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
91 byte flags = cb.readByte();
93 bLdp = ((flags & (byte) FIRST_BIT) == FIRST_BIT);
94 bRsvpTe = ((flags & (byte) SECOND_BIT) == SECOND_BIT);
96 return BgpLinkAttrMplsProtocolMask.of(bLdp, bRsvpTe);
100 * Returns true if LDP bit is set.
102 * @return True if LDP information is set else false.
104 public boolean ldpBit() {
109 * Returns RSVP TE information.
111 * @return True if RSVP TE information is set else false.
113 public boolean rsvpBit() {
118 public short getType() {
119 return ATTRLINK_MPLSPROTOMASK;
123 public int hashCode() {
124 return Objects.hash(bLdp, bRsvpTe);
128 public boolean equals(Object obj) {
133 if (obj instanceof BgpLinkAttrMplsProtocolMask) {
134 BgpLinkAttrMplsProtocolMask other = (BgpLinkAttrMplsProtocolMask) obj;
135 return Objects.equals(bLdp, other.bLdp)
136 && Objects.equals(bRsvpTe, other.bRsvpTe);
142 public int write(ChannelBuffer cb) {
143 // TODO This will be implemented in the next version
148 public String toString() {
149 return MoreObjects.toStringHelper(getClass())
150 .add("bLdp", bLdp).add("bRsvpTe", bRsvpTe).toString();
154 public int compareTo(Object o) {
155 // TODO Auto-generated method stub