blob: 58645d4f75f98a4e175c40906afa8616f77f1f97 [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;
Priyanka Bb2988fa2015-10-09 12:45:36 +053022
23import com.google.common.base.MoreObjects;
24
25/**
26 * Provides BGPLSIdentifier Tlv which contains opaque value (32 Bit BGPLS-Identifier).
27 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053028public class BgpLSIdentifierTlv implements BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053029
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 Bb2988fa2015-10-09 12:45:36 +053040 public static final short TYPE = 513;
41 public static final short LENGTH = 4;
42
Priyanka B02040732015-11-29 11:30:29 +053043 private final int bgpLsIdentifier;
Priyanka Bb2988fa2015-10-09 12:45:36 +053044
45 /**
Priyanka B02040732015-11-29 11:30:29 +053046 * Constructor to initialize bgpLsIdentifier.
Priyanka Bb2988fa2015-10-09 12:45:36 +053047 *
Priyanka B02040732015-11-29 11:30:29 +053048 * @param bgpLsIdentifier BGPLS-Identifier
Priyanka Bb2988fa2015-10-09 12:45:36 +053049 */
Priyanka B02040732015-11-29 11:30:29 +053050 public BgpLSIdentifierTlv(int bgpLsIdentifier) {
51 this.bgpLsIdentifier = bgpLsIdentifier;
Priyanka Bb2988fa2015-10-09 12:45:36 +053052 }
53
54 /**
Priyanka B02040732015-11-29 11:30:29 +053055 * Returns object of this class with specified bgpLsIdentifier.
Priyanka Bb2988fa2015-10-09 12:45:36 +053056 *
Priyanka B02040732015-11-29 11:30:29 +053057 * @param bgpLsIdentifier BGPLS-Identifier
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053058 * @return BGPLS-Identifier
Priyanka Bb2988fa2015-10-09 12:45:36 +053059 */
Priyanka B02040732015-11-29 11:30:29 +053060 public static BgpLSIdentifierTlv of(final int bgpLsIdentifier) {
61 return new BgpLSIdentifierTlv(bgpLsIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053062 }
63
64 /**
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053065 * Returns opaque value of BGPLS-Identifier.
Priyanka Bb2988fa2015-10-09 12:45:36 +053066 *
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053067 * @return opaque value of BGPLS-Identifier
Priyanka Bb2988fa2015-10-09 12:45:36 +053068 */
Priyanka B02040732015-11-29 11:30:29 +053069 public int getBgpLsIdentifier() {
70 return bgpLsIdentifier;
Priyanka Bb2988fa2015-10-09 12:45:36 +053071 }
72
73 @Override
74 public int hashCode() {
Priyanka B02040732015-11-29 11:30:29 +053075 return Objects.hash(bgpLsIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053076 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj) {
81 return true;
82 }
83
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053084 if (obj instanceof BgpLSIdentifierTlv) {
85 BgpLSIdentifierTlv other = (BgpLSIdentifierTlv) obj;
Priyanka B02040732015-11-29 11:30:29 +053086 return Objects.equals(bgpLsIdentifier, other.bgpLsIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053087 }
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 B02040732015-11-29 11:30:29 +053096 c.writeInt(bgpLsIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053097 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 VH5dd8dbe2015-11-26 13:22:18 +0530106 public static BgpLSIdentifierTlv read(ChannelBuffer cb) {
107 return BgpLSIdentifierTlv.of(cb.readInt());
Priyanka Bb2988fa2015-10-09 12:45:36 +0530108 }
109
110 @Override
111 public short getType() {
112 return TYPE;
113 }
114
115 @Override
Priyanka B02040732015-11-29 11:30:29 +0530116 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 Bb2988fa2015-10-09 12:45:36 +0530124 public String toString() {
125 return MoreObjects.toStringHelper(getClass())
126 .add("Type", TYPE)
127 .add("Length", LENGTH)
Priyanka B02040732015-11-29 11:30:29 +0530128 .add("Value", bgpLsIdentifier)
Priyanka Bb2988fa2015-10-09 12:45:36 +0530129 .toString();
130 }
131}