blob: e0964acfba0c658dc489d760feb2579902e7529b [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +05301/*
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
17package org.onosproject.bgpio.types;
18
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.google.common.base.MoreObjects;
26
27/**
28 * Provides BGPLSIdentifier Tlv which contains opaque value (32 Bit BGPLS-Identifier).
29 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053030public class BgpLSIdentifierTlv implements BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053031
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 VH5dd8dbe2015-11-26 13:22:18 +053042 protected static final Logger log = LoggerFactory.getLogger(BgpLSIdentifierTlv.class);
Priyanka Bb2988fa2015-10-09 12:45:36 +053043
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 VH5dd8dbe2015-11-26 13:22:18 +053052 * @param bgpLSIdentifier BGPLS-Identifier
Priyanka Bb2988fa2015-10-09 12:45:36 +053053 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053054 public BgpLSIdentifierTlv(int bgpLSIdentifier) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053055 this.bgpLSIdentifier = bgpLSIdentifier;
56 }
57
58 /**
59 * Returns object of this class with specified rbgpLSIdentifier.
60 *
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053061 * @param bgpLSIdentifier BGPLS-Identifier
62 * @return BGPLS-Identifier
Priyanka Bb2988fa2015-10-09 12:45:36 +053063 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053064 public static BgpLSIdentifierTlv of(final int bgpLSIdentifier) {
65 return new BgpLSIdentifierTlv(bgpLSIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053066 }
67
68 /**
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053069 * Returns opaque value of BGPLS-Identifier.
Priyanka Bb2988fa2015-10-09 12:45:36 +053070 *
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 * @return opaque value of BGPLS-Identifier
Priyanka Bb2988fa2015-10-09 12:45:36 +053072 */
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 VH5dd8dbe2015-11-26 13:22:18 +053088 if (obj instanceof BgpLSIdentifierTlv) {
89 BgpLSIdentifierTlv other = (BgpLSIdentifierTlv) obj;
Priyanka Bb2988fa2015-10-09 12:45:36 +053090 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 VH5dd8dbe2015-11-26 13:22:18 +0530110 public static BgpLSIdentifierTlv read(ChannelBuffer cb) {
111 return BgpLSIdentifierTlv.of(cb.readInt());
Priyanka Bb2988fa2015-10-09 12:45:36 +0530112 }
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}