Thejaswi N K
Committed by Gerrit Code Review

[onos-2603] - Implement LinkState Link attributes-MPLS protocol mask

Change-Id: Ib0d8d098a5ecb77f98df60a6f6d6cfd8bd121c75
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 +package org.onosproject.bgpio.types.attr;
17 +
18 +import java.util.Objects;
19 +
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;
27 +
28 +import com.google.common.base.MoreObjects;
29 +
30 +/**
31 + * Implements BGP MPLS protocol mask attribute.
32 + */
33 +public class BgpLinkAttrMplsProtocolMask implements BGPValueType {
34 +
35 + protected static final Logger log = LoggerFactory
36 + .getLogger(BgpLinkAttrMplsProtocolMask.class);
37 +
38 + public static final int ATTRLINK_MPLSPROTOMASK = 1094;
39 + public static final int MASK_BYTE_LEN = 1;
40 +
41 + private final boolean bLdp;
42 + private final boolean bRsvpTe;
43 +
44 + public static final byte FIRST_BIT = (byte) 0x80;
45 + public static final byte SECOND_BIT = 0x40;
46 +
47 + /**
48 + * Constructor to initialize the values.
49 + *
50 + * @param bLdp boolean value true if LDP flag is available
51 + * @param bRsvpTe boolean value true if RSVP TE information is available
52 + */
53 + public BgpLinkAttrMplsProtocolMask(boolean bLdp, boolean bRsvpTe) {
54 + this.bLdp = bLdp;
55 + this.bRsvpTe = bRsvpTe;
56 + }
57 +
58 + /**
59 + * Returns object of this class with specified values.
60 + *
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
64 + */
65 + public static BgpLinkAttrMplsProtocolMask of(final boolean bLdp,
66 + final boolean bRsvpTe) {
67 + return new BgpLinkAttrMplsProtocolMask(bLdp, bRsvpTe);
68 + }
69 +
70 + /**
71 + * Reads the BGP link attributes MPLS protocol mask.
72 + *
73 + * @param cb Channel buffer
74 + * @return object of type BgpLinkAttrMPLSProtocolMask
75 + * @throws BGPParseException while parsing BgpLinkAttrMplsProtocolMask
76 + */
77 + public static BgpLinkAttrMplsProtocolMask read(ChannelBuffer cb)
78 + throws BGPParseException {
79 + boolean bLdp = false;
80 + boolean bRsvpTe = false;
81 +
82 + short lsAttrLength = cb.readShort();
83 +
84 + if ((lsAttrLength != MASK_BYTE_LEN)
85 + || (cb.readableBytes() < lsAttrLength)) {
86 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
87 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
88 + lsAttrLength);
89 + }
90 +
91 + byte flags = cb.readByte();
92 +
93 + bLdp = ((flags & (byte) FIRST_BIT) == FIRST_BIT);
94 + bRsvpTe = ((flags & (byte) SECOND_BIT) == SECOND_BIT);
95 +
96 + return BgpLinkAttrMplsProtocolMask.of(bLdp, bRsvpTe);
97 + }
98 +
99 + /**
100 + * Returns true if LDP bit is set.
101 + *
102 + * @return True if LDP information is set else false.
103 + */
104 + public boolean ldpBit() {
105 + return bLdp;
106 + }
107 +
108 + /**
109 + * Returns RSVP TE information.
110 + *
111 + * @return True if RSVP TE information is set else false.
112 + */
113 + public boolean rsvpBit() {
114 + return bRsvpTe;
115 + }
116 +
117 + @Override
118 + public short getType() {
119 + return ATTRLINK_MPLSPROTOMASK;
120 + }
121 +
122 + @Override
123 + public int hashCode() {
124 + return Objects.hash(bLdp, bRsvpTe);
125 + }
126 +
127 + @Override
128 + public boolean equals(Object obj) {
129 + if (this == obj) {
130 + return true;
131 + }
132 +
133 + if (obj instanceof BgpLinkAttrMplsProtocolMask) {
134 + BgpLinkAttrMplsProtocolMask other = (BgpLinkAttrMplsProtocolMask) obj;
135 + return Objects.equals(bLdp, other.bLdp)
136 + && Objects.equals(bRsvpTe, other.bRsvpTe);
137 + }
138 + return false;
139 + }
140 +
141 + @Override
142 + public int write(ChannelBuffer cb) {
143 + // TODO This will be implemented in the next version
144 + return 0;
145 + }
146 +
147 + @Override
148 + public String toString() {
149 + return MoreObjects.toStringHelper(getClass())
150 + .add("bLdp", bLdp).add("bRsvpTe", bRsvpTe).toString();
151 + }
152 +}