Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 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; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 21 | |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | |
| 24 | /** |
| 25 | * Provides AreaID Tlv which contains opaque value (32 Bit Area-ID). |
| 26 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 27 | public class AreaIDTlv implements BgpValueType { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 28 | |
| 29 | /* Reference :draft-ietf-idr-ls-distribution-11 |
| 30 | * 0 1 2 3 |
| 31 | 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 |
| 32 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 33 | | Type= 514 | Length=4 | |
| 34 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 35 | | opaque value (32 Bit Area-ID) | |
| 36 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 37 | */ |
| 38 | |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 39 | public static final short TYPE = 514; |
| 40 | public static final short LENGTH = 4; |
| 41 | |
| 42 | private final int areaID; |
| 43 | |
| 44 | /** |
| 45 | * Constructor to initialize areaID. |
| 46 | * |
| 47 | * @param areaID of BGP AreaID Tlv |
| 48 | */ |
| 49 | public AreaIDTlv(int areaID) { |
| 50 | this.areaID = areaID; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns object of this class with specified areaID. |
| 55 | * |
| 56 | * @param areaID opaque value of area id |
| 57 | * @return object of AreaIDTlv |
| 58 | */ |
| 59 | public static AreaIDTlv of(final int areaID) { |
| 60 | return new AreaIDTlv(areaID); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns opaque value of area id. |
| 65 | * |
| 66 | * @return opaque value of area id |
| 67 | */ |
| 68 | public int getAreaID() { |
| 69 | return areaID; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int hashCode() { |
| 74 | return Objects.hash(areaID); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean equals(Object obj) { |
| 79 | if (this == obj) { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | if (obj instanceof AreaIDTlv) { |
| 84 | AreaIDTlv other = (AreaIDTlv) obj; |
| 85 | return Objects.equals(areaID, other.areaID); |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int write(ChannelBuffer c) { |
| 92 | int iLenStartIndex = c.writerIndex(); |
| 93 | c.writeShort(TYPE); |
| 94 | c.writeShort(LENGTH); |
| 95 | c.writeInt(areaID); |
| 96 | return c.writerIndex() - iLenStartIndex; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Reads the channel buffer and returns object of AreaIDTlv. |
| 101 | * |
| 102 | * @param cb ChannelBuffer |
| 103 | * @return object of AreaIDTlv |
| 104 | */ |
| 105 | public static AreaIDTlv read(ChannelBuffer cb) { |
| 106 | return AreaIDTlv.of(cb.readInt()); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public short getType() { |
| 111 | return TYPE; |
| 112 | } |
| 113 | |
| 114 | @Override |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 115 | public int compareTo(Object o) { |
| 116 | if (this.equals(o)) { |
| 117 | return 0; |
| 118 | } |
| 119 | return ((Integer) (this.areaID)).compareTo((Integer) (((AreaIDTlv) o).areaID)); |
| 120 | } |
| 121 | |
| 122 | @Override |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 123 | public String toString() { |
| 124 | return MoreObjects.toStringHelper(getClass()) |
| 125 | .add("Type", TYPE) |
| 126 | .add("Length", LENGTH) |
| 127 | .add("Value", areaID) |
| 128 | .toString(); |
| 129 | } |
| 130 | } |