Shashikanth VH | 73b1277 | 2016-04-18 14:28:45 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.bgpio.types; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | |
| 20 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 21 | import org.onosproject.bgpio.util.Validation; |
| 22 | import org.jboss.netty.buffer.ChannelBuffer; |
| 23 | |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | /** |
| 27 | * Provides implementation of BGP wide community attribute header. |
| 28 | */ |
| 29 | public 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 VH | 73b1277 | 2016-04-18 14:28:45 +0530 | [diff] [blame] | 166 | 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 |
| 174 | */ |
| 175 | public static WideCommunityAttrHeader read(ChannelBuffer c) throws BgpParseException { |
| 176 | |
| 177 | if (c.readableBytes() < HEADER_LENGTH) { |
| 178 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 179 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, c.readableBytes()); |
| 180 | } |
| 181 | |
| 182 | short type = c.readShort(); |
| 183 | byte flag = c.readByte(); |
| 184 | byte hopCount = c.readByte(); |
| 185 | short length = c.readShort(); |
| 186 | return WideCommunityAttrHeader.of(flag, hopCount, length); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public short getType() { |
| 191 | return TYPE; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public String toString() { |
| 196 | return MoreObjects.toStringHelper(getClass()) |
| 197 | .add("Type", TYPE) |
| 198 | .add("flag", flag) |
| 199 | .add("hopCount", hopCount) |
| 200 | .add("length", length) |
| 201 | .toString(); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public int compareTo(Object o) { |
| 206 | // TODO Auto-generated method stub |
| 207 | return 0; |
| 208 | } |
| 209 | } |