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