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