blob: af88863b5fe03316c4be532337b1067a86b35611 [file] [log] [blame]
Shashikanth VH73b12772016-04-18 14:28:45 +05301/*
2 * Copyright 2016-present 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 */
16package org.onosproject.bgpio.types;
17
18import com.google.common.base.MoreObjects;
19
20import org.onosproject.bgpio.exceptions.BgpParseException;
21import org.onosproject.bgpio.util.Validation;
22import org.jboss.netty.buffer.ChannelBuffer;
23
24import java.util.Objects;
25
26/**
27 * Provides implementation of BGP wide community attribute header.
28 */
29public class WideCommunityAttrHeader implements BgpValueType {
30
31 /* 0 1 2 3
32 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
33 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 | Type | Flags | Hop Count |
35 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 | Length |
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*/
38
39 /*FLAG
40 +------+-------+----------------------------------------------------+
41 | Bit | Value | Meaning |
42 +------+-------+----------------------------------------------------+
43 | 0 | 0 | Local community value. |
44 | | 1 | Registered community value. |
45 | 1 | 0 | Do not decrement Hop Count field across |
46 | | | confederation boundaries. |
47 | | 1 | Decrement Hop Count field across confederation |
48 | | | boundaries. |
49 | 2..7 | - | MUST be zero when sent and ignored upon receipt. |
50 +------+-------+----------------------------------------------------+*/
51
52 public static final short TYPE = 1;
53 public static final short HEADER_LENGTH = 6;
54 private byte flag;
55 private byte hopCount;
56 private short length;
57
58 /**
59 * Wide community attribute header.
60 *
61 * @param flag to apply to all wide community container types
62 * @param hopCount represents the forwarding radius, in units of AS hops, for the given Wide BGP Community
63 * @param length field represents the total length of a given container
64 */
65 public WideCommunityAttrHeader(byte flag, byte hopCount, short length) {
66 this.flag = flag;
67 this.hopCount = hopCount;
68 this.length = length;
69 }
70
71 /**
72 * Returns object of this class with specified values.
73 *
74 * @param flag flag to apply to all wide community container types
75 * @param hopCount represents the forwarding radius, in units of AS hops, for the given Wide BGP Community
76 * @param length field represents the total length of a given container
77 * @return wide community attribute header
78 */
79 public static WideCommunityAttrHeader of(byte flag, byte hopCount, short length) {
80 return new WideCommunityAttrHeader(flag, hopCount, length);
81 }
82
83 /**
84 * Returns wide community flag.
85 *
86 * @return wide community flag
87 */
88 public byte flag() {
89 return flag;
90 }
91
92 /**
93 * Sets wide community flag.
94 *
95 * @param flag to apply to all wide community container types
96 */
97 public void setFlag(byte flag) {
98 this.flag = flag;
99 }
100
101 /**
102 * Returns hop count for wide community attribute.
103 *
104 * @return hop count from wide community
105 */
106 public byte hopCount() {
107 return hopCount;
108 }
109
110 /**
111 * Sets wide community hop count.
112 *
113 * @param hopCount represents the forwarding radius, in units of AS hops, for the given Wide BGP Community
114 */
115 public void setHopCount(byte hopCount) {
116 this.hopCount = hopCount;
117 }
118
119 /**
120 * Returns length of wide community attribute.
121 *
122 * @return length of wide community attribute
123 */
124 public short length() {
125 return length;
126 }
127
128 /**
129 * Sets wide community length.
130 *
131 * @param length total length of a given container
132 */
133 public void setLength(short length) {
134 this.length = length;
135 }
136
137 @Override
138 public int hashCode() {
139 return Objects.hash(flag, hopCount, length);
140 }
141
142 @Override
143 public boolean equals(Object obj) {
144 if (this == obj) {
145 return true;
146 }
147
148 if (obj == null) {
149 return false;
150 }
151
152 if (obj instanceof WideCommunityAttrHeader) {
153 WideCommunityAttrHeader other = (WideCommunityAttrHeader) obj;
154 return Objects.equals(flag, other.flag) && Objects.equals(hopCount, other.hopCount)
155 && Objects.equals(length, other.length);
156 }
157 return false;
158 }
159
160 @Override
161 public int write(ChannelBuffer c) {
162 int iLenStartIndex = c.writerIndex();
163 c.writeShort(TYPE);
164 c.writeByte(flag);
165 c.writeByte(hopCount);
Shashikanth VH73b12772016-04-18 14:28:45 +0530166 return c.writerIndex() - iLenStartIndex;
167 }
168
169 /**
170 * Reads the channel buffer and returns object of WideCommunityAttrHeader.
171 *
172 * @param c ChannelBuffer
173 * @return object of WideCommunityAttrHeader
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700174 * @throws BgpParseException if a parsing error occurs
Shashikanth VH73b12772016-04-18 14:28:45 +0530175 */
176 public static WideCommunityAttrHeader read(ChannelBuffer c) throws BgpParseException {
177
178 if (c.readableBytes() < HEADER_LENGTH) {
179 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
180 BgpErrorType.ATTRIBUTE_LENGTH_ERROR, c.readableBytes());
181 }
182
183 short type = c.readShort();
184 byte flag = c.readByte();
185 byte hopCount = c.readByte();
186 short length = c.readShort();
187 return WideCommunityAttrHeader.of(flag, hopCount, length);
188 }
189
190 @Override
191 public short getType() {
192 return TYPE;
193 }
194
195 @Override
196 public String toString() {
197 return MoreObjects.toStringHelper(getClass())
198 .add("Type", TYPE)
199 .add("flag", flag)
200 .add("hopCount", hopCount)
201 .add("length", length)
202 .toString();
203 }
204
205 @Override
206 public int compareTo(Object o) {
207 // TODO Auto-generated method stub
208 return 0;
209 }
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700210}