blob: 272c55fb9e71bc326e2027f1e8d1f468b66dad1e [file] [log] [blame]
Thejaswi N K9ad67e52015-10-14 16:35:23 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thejaswi N K9ad67e52015-10-14 16:35:23 +05303 *
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.bgpio.types.attr;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.BgpErrorType;
23import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N K9ad67e52015-10-14 16:35:23 +053024import org.onosproject.bgpio.util.Validation;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.common.base.MoreObjects;
29
30/**
31 * Implements BGP MPLS protocol mask attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public class BgpLinkAttrMplsProtocolMask implements BgpValueType {
Thejaswi N K9ad67e52015-10-14 16:35:23 +053034
Ray Milkey9c9cde42018-01-12 14:22:06 -080035 private static final Logger log = LoggerFactory
Thejaswi N K9ad67e52015-10-14 16:35:23 +053036 .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
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 * @throws BgpParseException while parsing BgpLinkAttrMplsProtocolMask
Thejaswi N K9ad67e52015-10-14 16:35:23 +053076 */
77 public static BgpLinkAttrMplsProtocolMask read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053078 throws BgpParseException {
Thejaswi N K9ad67e52015-10-14 16:35:23 +053079 boolean bLdp = false;
80 boolean bRsvpTe = false;
81
82 short lsAttrLength = cb.readShort();
83
84 if ((lsAttrLength != MASK_BYTE_LEN)
85 || (cb.readableBytes() < lsAttrLength)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053086 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
87 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N K9ad67e52015-10-14 16:35:23 +053088 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 }
Priyanka B02040732015-11-29 11:30:29 +0530152
153 @Override
154 public int compareTo(Object o) {
155 // TODO Auto-generated method stub
156 return 0;
157 }
Thejaswi N K9ad67e52015-10-14 16:35:23 +0530158}