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